5 "github.com/stretchr/testify/assert"
9 func TestReadFields(t *testing.T) {
21 name: "valid field data",
23 paramCount: []byte{0x00, 0x02},
25 0x00, 0x65, // ID: FieldData
26 0x00, 0x04, // Size: 2 bytes
27 0x01, 0x02, 0x03, 0x04, // Data
28 0x00, 0x66, // ID: FieldUserName
29 0x00, 0x02, // Size: 2 bytes
35 ID: [2]byte{0x00, 0x65},
36 FieldSize: [2]byte{0x00, 0x04},
37 Data: []byte{0x01, 0x02, 0x03, 0x04},
40 ID: [2]byte{0x00, 0x66},
41 FieldSize: [2]byte{0x00, 0x02},
42 Data: []byte{0x00, 0x01},
50 paramCount: []byte{0x00, 0x00},
57 name: "when field size does not match data length",
59 paramCount: []byte{0x00, 0x01},
61 0x00, 0x65, // ID: FieldData
62 0x00, 0x04, // Size: 4 bytes
63 0x01, 0x02, 0x03, // Data
70 name: "when field size of second field does not match data length",
72 paramCount: []byte{0x00, 0x01},
74 0x00, 0x65, // ID: FieldData
75 0x00, 0x02, // Size: 2 bytes
77 0x00, 0x65, // ID: FieldData
78 0x00, 0x04, // Size: 4 bytes
79 0x01, 0x02, 0x03, // Data
86 name: "when field data has extra bytes",
88 paramCount: []byte{0x00, 0x01},
90 0x00, 0x65, // ID: FieldData
91 0x00, 0x02, // Size: 2 bytes
92 0x01, 0x02, 0x03, // Data
99 for _, tt := range tests {
100 t.Run(tt.name, func(t *testing.T) {
101 got, err := ReadFields(tt.args.paramCount, tt.args.buf)
102 if (err != nil) != tt.wantErr {
103 t.Errorf("ReadFields() error = %v, wantErr %v", err, tt.wantErr)
107 if !assert.Equal(t, tt.want, got) {
108 t.Errorf("ReadFields() got = %v, want %v", got, tt.want)
114 func Test_transactionScanner(t *testing.T) {
124 wantErr assert.ErrorAssertionFunc
127 name: "when too few bytes are provided to read the transaction size",
133 wantToken: []byte(nil),
134 wantErr: assert.NoError,
137 name: "when too few bytes are provided to read the full payload",
151 wantToken: []byte(nil),
152 wantErr: assert.NoError,
155 name: "when a full transaction is provided",
166 00, 0x6c, // 108 - FieldTransferSize
169 00, 0x6b, // 107 = FieldRefNum
171 00, 0x02, 0x93, 0x47,
185 00, 0x6c, // 108 - FieldTransferSize
188 00, 0x6b, // 107 = FieldRefNum
190 00, 0x02, 0x93, 0x47,
192 wantErr: assert.NoError,
195 name: "when a full transaction plus extra bytes are provided",
206 00, 0x6c, // 108 - FieldTransferSize
209 00, 0x6b, // 107 = FieldRefNum
211 00, 0x02, 0x93, 0x47,
212 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
226 00, 0x6c, // 108 - FieldTransferSize
229 00, 0x6b, // 107 = FieldRefNum
231 00, 0x02, 0x93, 0x47,
233 wantErr: assert.NoError,
236 name: "when two full transactions are provided",
247 00, 0x6c, // 108 - FieldTransferSize
250 00, 0x6b, // 107 = FieldRefNum
252 00, 0x02, 0x93, 0x47,
261 00, 0x6c, // 108 - FieldTransferSize
264 00, 0x6b, // 107 = FieldRefNum
266 00, 0x02, 0x93, 0x47,
280 00, 0x6c, // 108 - FieldTransferSize
283 00, 0x6b, // 107 = FieldRefNum
285 00, 0x02, 0x93, 0x47,
287 wantErr: assert.NoError,
290 for _, tt := range tests {
291 t.Run(tt.name, func(t *testing.T) {
292 gotAdvance, gotToken, err := transactionScanner(tt.args.data, tt.args.in1)
293 if !tt.wantErr(t, err, fmt.Sprintf("transactionScanner(%v, %v)", tt.args.data, tt.args.in1)) {
296 assert.Equalf(t, tt.wantAdvance, gotAdvance, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
297 assert.Equalf(t, tt.wantToken, gotToken, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
302 func TestTransaction_Read(t1 *testing.T) {
324 wantErr assert.ErrorAssertionFunc
328 name: "returns transaction bytes",
333 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
334 ErrorCode: []byte{0, 0, 0, 0},
336 NewField(FieldData, []byte("TEST")),
340 p: make([]byte, 1024),
343 wantErr: assert.NoError,
344 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},
347 name: "returns transaction bytes from readOffset",
352 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
353 ErrorCode: []byte{0, 0, 0, 0},
355 NewField(FieldData, []byte("TEST")),
360 p: make([]byte, 1024),
363 wantErr: assert.NoError,
364 wantBytes: []byte{0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
367 name: "returns io.EOF when all bytes read",
372 ID: []byte{0x9a, 0xcb, 0x04, 0x42},
373 ErrorCode: []byte{0, 0, 0, 0},
375 NewField(FieldData, []byte("TEST")),
380 p: make([]byte, 1024),
383 wantErr: assert.Error,
387 for _, tt := range tests {
388 t1.Run(tt.name, func(t1 *testing.T) {
390 clientID: tt.fields.clientID,
391 Flags: tt.fields.Flags,
392 IsReply: tt.fields.IsReply,
393 Type: tt.fields.Type,
395 ErrorCode: tt.fields.ErrorCode,
396 TotalSize: tt.fields.TotalSize,
397 DataSize: tt.fields.DataSize,
398 ParamCount: tt.fields.ParamCount,
399 Fields: tt.fields.Fields,
400 readOffset: tt.fields.readOffset,
402 got, err := t.Read(tt.args.p)
403 if !tt.wantErr(t1, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
406 assert.Equalf(t1, tt.want, got, "Read(%v)", tt.args.p)
407 assert.Equalf(t1, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)