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: []byte{0x00, 0x65},
36 FieldSize: []byte{0x00, 0x04},
37 Data: []byte{0x01, 0x02, 0x03, 0x04},
40 ID: []byte{0x00, 0x66},
41 FieldSize: []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_Write(t1 *testing.T) {
303 sampleTransaction := &Transaction{
306 Type: []byte{0x000, 0x93},
307 ID: []byte{0x000, 0x00, 0x00, 0x01},
308 ErrorCode: []byte{0x000, 0x00, 0x00, 0x00},
309 TotalSize: []byte{0x000, 0x00, 0x00, 0x08},
310 DataSize: []byte{0x000, 0x00, 0x00, 0x08},
311 ParamCount: []byte{0x00, 0x01},
314 ID: []byte{0x00, 0x01},
315 FieldSize: []byte{0x00, 0x02},
316 Data: []byte{0xff, 0xff},
341 wantErr assert.ErrorAssertionFunc
344 name: "when buf contains all bytes for a single transaction",
348 b, _ := sampleTransaction.MarshalBinary()
353 wantErr: assert.NoError,
356 for _, tt := range tests {
357 t1.Run(tt.name, func(t1 *testing.T) {
359 clientID: tt.fields.clientID,
360 Flags: tt.fields.Flags,
361 IsReply: tt.fields.IsReply,
362 Type: tt.fields.Type,
364 ErrorCode: tt.fields.ErrorCode,
365 TotalSize: tt.fields.TotalSize,
366 DataSize: tt.fields.DataSize,
367 ParamCount: tt.fields.ParamCount,
368 Fields: tt.fields.Fields,
370 gotN, err := t.Write(tt.args.p)
371 if !tt.wantErr(t1, err, fmt.Sprintf("Write(%v)", tt.args.p)) {
374 assert.Equalf(t1, tt.wantN, gotN, "Write(%v)", tt.args.p)