5 "github.com/stretchr/testify/assert"
9 func Test_transactionScanner(t *testing.T) {
19 wantErr assert.ErrorAssertionFunc
22 name: "when too few bytes are provided to read the transaction size",
28 wantToken: []byte(nil),
29 wantErr: assert.NoError,
32 name: "when too few bytes are provided to read the full payload",
46 wantToken: []byte(nil),
47 wantErr: assert.NoError,
50 name: "when a full transaction is provided",
61 00, 0x6c, // 108 - FieldTransferSize
64 00, 0x6b, // 107 = FieldRefNum
80 00, 0x6c, // 108 - FieldTransferSize
83 00, 0x6b, // 107 = FieldRefNum
87 wantErr: assert.NoError,
90 name: "when a full transaction plus extra bytes are provided",
101 00, 0x6c, // 108 - FieldTransferSize
104 00, 0x6b, // 107 = FieldRefNum
106 00, 0x02, 0x93, 0x47,
107 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
121 00, 0x6c, // 108 - FieldTransferSize
124 00, 0x6b, // 107 = FieldRefNum
126 00, 0x02, 0x93, 0x47,
128 wantErr: assert.NoError,
131 name: "when two full transactions are provided",
142 00, 0x6c, // 108 - FieldTransferSize
145 00, 0x6b, // 107 = FieldRefNum
147 00, 0x02, 0x93, 0x47,
156 00, 0x6c, // 108 - FieldTransferSize
159 00, 0x6b, // 107 = FieldRefNum
161 00, 0x02, 0x93, 0x47,
175 00, 0x6c, // 108 - FieldTransferSize
178 00, 0x6b, // 107 = FieldRefNum
180 00, 0x02, 0x93, 0x47,
182 wantErr: assert.NoError,
185 for _, tt := range tests {
186 t.Run(tt.name, func(t *testing.T) {
187 gotAdvance, gotToken, err := transactionScanner(tt.args.data, tt.args.in1)
188 if !tt.wantErr(t, err, fmt.Sprintf("transactionScanner(%v, %v)", tt.args.data, tt.args.in1)) {
191 assert.Equalf(t, tt.wantAdvance, gotAdvance, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
192 assert.Equalf(t, tt.wantToken, gotToken, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
197 func TestTransaction_Read(t1 *testing.T) {
219 wantErr assert.ErrorAssertionFunc
223 name: "returns transaction bytes",
228 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
229 ErrorCode: [4]byte{0, 0, 0, 0},
231 NewField(FieldData, []byte("TEST")),
235 p: make([]byte, 1024),
238 wantErr: assert.NoError,
239 wantBytes: []byte{0x0, 0x1, 0x0, 0x0, 0x9a, 0xcb, 0x4, 0x42, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0xa, 0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
242 name: "returns transaction bytes from readOffset",
247 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
248 ErrorCode: [4]byte{0, 0, 0, 0},
250 NewField(FieldData, []byte("TEST")),
255 p: make([]byte, 1024),
258 wantErr: assert.NoError,
259 wantBytes: []byte{0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
262 name: "returns io.EOF when all bytes read",
267 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
268 ErrorCode: [4]byte{0, 0, 0, 0},
270 NewField(FieldData, []byte("TEST")),
275 p: make([]byte, 1024),
278 wantErr: assert.Error,
282 for _, tt := range tests {
283 t1.Run(tt.name, func(t1 *testing.T) {
285 clientID: tt.fields.clientID,
286 Flags: tt.fields.Flags,
287 IsReply: tt.fields.IsReply,
288 Type: tt.fields.Type,
290 ErrorCode: tt.fields.ErrorCode,
291 TotalSize: tt.fields.TotalSize,
292 DataSize: tt.fields.DataSize,
293 ParamCount: tt.fields.ParamCount,
294 Fields: tt.fields.Fields,
295 readOffset: tt.fields.readOffset,
297 got, err := t.Read(tt.args.p)
298 if !tt.wantErr(t1, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
301 assert.Equalf(t1, tt.want, got, "Read(%v)", tt.args.p)
302 assert.Equalf(t1, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)
307 func TestTransaction_Write(t1 *testing.T) {
315 wantErr assert.ErrorAssertionFunc
316 wantTransaction Transaction
319 name: "returns error if arg p is too small",
320 args: args{p: []byte{
324 wantErr: assert.Error,
325 wantTransaction: Transaction{},
328 // name: "returns error if param data is invalid",
329 // args: args{p: []byte{
330 // 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72,
331 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
332 // 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x65,
333 // 0x00, 0x03, 0x68, 0x61, 0x69,
336 // wantErr: assert.Error,
337 // wantTransaction: Transaction{},
340 name: "writes bytes to transaction",
341 args: args{p: []byte{
342 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72,
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
344 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x65,
345 0x00, 0x03, 0x68, 0x61, 0x69,
348 wantErr: assert.NoError,
349 wantTransaction: Transaction{
354 ErrorCode: [4]byte{},
355 TotalSize: [4]byte{0, 0, 0, 9},
356 DataSize: [4]byte{0, 0, 0, 9},
357 ParamCount: [2]byte{0, 1},
361 FieldSize: [2]byte{0, 3},
370 for _, tt := range tests {
371 t1.Run(tt.name, func(t1 *testing.T) {
373 gotN, err := t.Write(tt.args.p)
374 if !tt.wantErr(t1, err, fmt.Sprintf("Write(%v)", tt.args.p)) {
377 assert.Equalf(t1, tt.wantN, gotN, "Write(%v)", tt.args.p)
379 tranAssertEqual(t1, []Transaction{tt.wantTransaction}, []Transaction{*t})