]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_test.go
Implement bufio.Scanner for transaction parsing
[rbdr/mobius] / hotline / transaction_test.go
index 1fade255c63bc5e2047624b2d459b7257e93b066..04bcde0e7851e1b1538bced6411411113a438bff 100644 (file)
@@ -1,6 +1,7 @@
 package hotline
 
 import (
+       "fmt"
        "github.com/stretchr/testify/assert"
        "testing"
 )
@@ -147,8 +148,8 @@ func TestReadTransaction(t *testing.T) {
                                        return b
                                }(),
                        },
-                       want:    sampleTransaction,
-                       want1:   func() int {
+                       want: sampleTransaction,
+                       want1: func() int {
                                b, _ := sampleTransaction.MarshalBinary()
                                return len(b)
                        }(),
@@ -183,3 +184,191 @@ func TestReadTransaction(t *testing.T) {
                })
        }
 }
+
+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,
+                       },
+                       wantErr: assert.NoError,
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       gotAdvance, gotToken, err := transactionScanner(tt.args.data, tt.args.in1)
+                       if !tt.wantErr(t, err, fmt.Sprintf("transactionScanner(%v, %v)", tt.args.data, tt.args.in1)) {
+                               return
+                       }
+                       assert.Equalf(t, tt.wantAdvance, gotAdvance, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
+                       assert.Equalf(t, tt.wantToken, gotToken, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
+               })
+       }
+}