-func TestReadTransaction(t *testing.T) {
- sampleTransaction := &Transaction{
- Flags: byte(0),
- IsReply: byte(0),
- Type: []byte{0x000, 0x93},
- ID: []byte{0x000, 0x00, 0x00, 0x01},
- ErrorCode: []byte{0x000, 0x00, 0x00, 0x00},
- TotalSize: []byte{0x000, 0x00, 0x00, 0x08},
- DataSize: []byte{0x000, 0x00, 0x00, 0x08},
- ParamCount: []byte{0x00, 0x01},
- Fields: []Field{
- {
- ID: []byte{0x00, 0x01},
- FieldSize: []byte{0x00, 0x02},
- Data: []byte{0xff, 0xff},
+func Test_transactionScanner(t *testing.T) {
+ type args struct {
+ data []byte
+ in1 bool
+ }
+ tests := []struct {
+ name string
+ args args
+ wantAdvance int
+ wantToken []byte
+ wantErr assert.ErrorAssertionFunc
+ }{
+ {
+ name: "when too few bytes are provided to read the transaction size",
+ args: args{
+ data: []byte{},
+ in1: false,
+ },
+ wantAdvance: 0,
+ wantToken: []byte(nil),
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when too few bytes are provided to read the full payload",
+ args: args{
+ data: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 04,
+ 00, 00, 00, 00,
+ 00, 00, 00, 10,
+ 00, 00, 00, 10,
+ },
+ in1: false,
+ },
+ wantAdvance: 0,
+ wantToken: []byte(nil),
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when a full transaction is provided",
+ args: args{
+ data: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ },
+ in1: false,
+ },
+ wantAdvance: 36,
+ wantToken: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when a full transaction plus extra bytes are provided",
+ args: args{
+ data: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ },
+ in1: false,
+ },
+ wantAdvance: 36,
+ wantToken: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ },
+ wantErr: assert.NoError,
+ },
+ {
+ name: "when two full transactions are provided",
+ args: args{
+ data: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,
+ },
+ in1: false,
+ },
+ wantAdvance: 36,
+ wantToken: []byte{
+ 0,
+ 1,
+ 0, 0,
+ 0, 00, 00, 0x04,
+ 00, 00, 00, 0x00,
+ 00, 00, 00, 0x10,
+ 00, 00, 00, 0x10,
+ 00, 02,
+ 00, 0x6c, // 108 - FieldTransferSize
+ 00, 02,
+ 0x63, 0x3b,
+ 00, 0x6b, // 107 = FieldRefNum
+ 00, 0x04,
+ 00, 0x02, 0x93, 0x47,