]> git.r.bdr.sh - rbdr/mobius/blobdiff - hotline/transaction_test.go
Fix broken io.Reader implementations
[rbdr/mobius] / hotline / transaction_test.go
index 04bcde0e7851e1b1538bced6411411113a438bff..cdb6f1e627a40ab0d4f0ec59459b15c8ae33d292 100644 (file)
@@ -22,23 +22,23 @@ func TestReadFields(t *testing.T) {
                        args: args{
                                paramCount: []byte{0x00, 0x02},
                                buf: []byte{
-                                       0x00, 0x65, // ID: fieldData
+                                       0x00, 0x65, // ID: FieldData
                                        0x00, 0x04, // Size: 2 bytes
                                        0x01, 0x02, 0x03, 0x04, // Data
-                                       0x00, 0x66, // ID: fieldUserName
+                                       0x00, 0x66, // ID: FieldUserName
                                        0x00, 0x02, // Size: 2 bytes
                                        0x00, 0x01, // Data
                                },
                        },
                        want: []Field{
                                {
-                                       ID:        []byte{0x00, 0x65},
-                                       FieldSize: []byte{0x00, 0x04},
+                                       ID:        [2]byte{0x00, 0x65},
+                                       FieldSize: [2]byte{0x00, 0x04},
                                        Data:      []byte{0x01, 0x02, 0x03, 0x04},
                                },
                                {
-                                       ID:        []byte{0x00, 0x66},
-                                       FieldSize: []byte{0x00, 0x02},
+                                       ID:        [2]byte{0x00, 0x66},
+                                       FieldSize: [2]byte{0x00, 0x02},
                                        Data:      []byte{0x00, 0x01},
                                },
                        },
@@ -58,7 +58,7 @@ func TestReadFields(t *testing.T) {
                        args: args{
                                paramCount: []byte{0x00, 0x01},
                                buf: []byte{
-                                       0x00, 0x65, // ID: fieldData
+                                       0x00, 0x65, // ID: FieldData
                                        0x00, 0x04, // Size: 4 bytes
                                        0x01, 0x02, 0x03, // Data
                                },
@@ -71,10 +71,10 @@ func TestReadFields(t *testing.T) {
                        args: args{
                                paramCount: []byte{0x00, 0x01},
                                buf: []byte{
-                                       0x00, 0x65, // ID: fieldData
+                                       0x00, 0x65, // ID: FieldData
                                        0x00, 0x02, // Size: 2 bytes
                                        0x01, 0x02, // Data
-                                       0x00, 0x65, // ID: fieldData
+                                       0x00, 0x65, // ID: FieldData
                                        0x00, 0x04, // Size: 4 bytes
                                        0x01, 0x02, 0x03, // Data
                                },
@@ -87,7 +87,7 @@ func TestReadFields(t *testing.T) {
                        args: args{
                                paramCount: []byte{0x00, 0x01},
                                buf: []byte{
-                                       0x00, 0x65, // ID: fieldData
+                                       0x00, 0x65, // ID: FieldData
                                        0x00, 0x02, // Size: 2 bytes
                                        0x01, 0x02, 0x03, // Data
                                },
@@ -111,80 +111,6 @@ func TestReadFields(t *testing.T) {
        }
 }
 
-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},
-                       },
-               },
-       }
-
-       type args struct {
-               buf []byte
-       }
-       tests := []struct {
-               name    string
-               args    args
-               want    *Transaction
-               want1   int
-               wantErr bool
-       }{
-               {
-                       name: "when buf contains all bytes for a single transaction",
-                       args: args{
-                               buf: func() []byte {
-                                       b, _ := sampleTransaction.MarshalBinary()
-                                       return b
-                               }(),
-                       },
-                       want: sampleTransaction,
-                       want1: func() int {
-                               b, _ := sampleTransaction.MarshalBinary()
-                               return len(b)
-                       }(),
-                       wantErr: false,
-               },
-               {
-                       name: "when len(buf) is less than the length of the transaction",
-                       args: args{
-                               buf: func() []byte {
-                                       b, _ := sampleTransaction.MarshalBinary()
-                                       return b[:len(b)-1]
-                               }(),
-                       },
-                       want:    nil,
-                       want1:   0,
-                       wantErr: true,
-               },
-       }
-       for _, tt := range tests {
-               t.Run(tt.name, func(t *testing.T) {
-                       got, got1, err := ReadTransaction(tt.args.buf)
-                       if (err != nil) != tt.wantErr {
-                               t.Errorf("ReadTransaction() error = %v, wantErr %v", err, tt.wantErr)
-                               return
-                       }
-                       if !assert.Equal(t, tt.want, got) {
-                               t.Errorf("ReadTransaction() got = %v, want %v", got, tt.want)
-                       }
-                       if got1 != tt.want1 {
-                               t.Errorf("ReadTransaction() got1 = %v, want %v", got1, tt.want1)
-                       }
-               })
-       }
-}
-
 func Test_transactionScanner(t *testing.T) {
        type args struct {
                data []byte
@@ -237,10 +163,10 @@ func Test_transactionScanner(t *testing.T) {
                                        00, 00, 00, 0x10,
                                        00, 00, 00, 0x10,
                                        00, 02,
-                                       00, 0x6c, // 108 - fieldTransferSize
+                                       00, 0x6c, // 108 - FieldTransferSize
                                        00, 02,
                                        0x63, 0x3b,
-                                       00, 0x6b, // 107 = fieldRefNum
+                                       00, 0x6b, // 107 = FieldRefNum
                                        00, 0x04,
                                        00, 0x02, 0x93, 0x47,
                                },
@@ -256,10 +182,10 @@ func Test_transactionScanner(t *testing.T) {
                                00, 00, 00, 0x10,
                                00, 00, 00, 0x10,
                                00, 02,
-                               00, 0x6c, // 108 - fieldTransferSize
+                               00, 0x6c, // 108 - FieldTransferSize
                                00, 02,
                                0x63, 0x3b,
-                               00, 0x6b, // 107 = fieldRefNum
+                               00, 0x6b, // 107 = FieldRefNum
                                00, 0x04,
                                00, 0x02, 0x93, 0x47,
                        },
@@ -277,10 +203,10 @@ func Test_transactionScanner(t *testing.T) {
                                        00, 00, 00, 0x10,
                                        00, 00, 00, 0x10,
                                        00, 02,
-                                       00, 0x6c, // 108 - fieldTransferSize
+                                       00, 0x6c, // 108 - FieldTransferSize
                                        00, 02,
                                        0x63, 0x3b,
-                                       00, 0x6b, // 107 = fieldRefNum
+                                       00, 0x6b, // 107 = FieldRefNum
                                        00, 0x04,
                                        00, 0x02, 0x93, 0x47,
                                        1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
@@ -297,10 +223,10 @@ func Test_transactionScanner(t *testing.T) {
                                00, 00, 00, 0x10,
                                00, 00, 00, 0x10,
                                00, 02,
-                               00, 0x6c, // 108 - fieldTransferSize
+                               00, 0x6c, // 108 - FieldTransferSize
                                00, 02,
                                0x63, 0x3b,
-                               00, 0x6b, // 107 = fieldRefNum
+                               00, 0x6b, // 107 = FieldRefNum
                                00, 0x04,
                                00, 0x02, 0x93, 0x47,
                        },
@@ -318,10 +244,10 @@ func Test_transactionScanner(t *testing.T) {
                                        00, 00, 00, 0x10,
                                        00, 00, 00, 0x10,
                                        00, 02,
-                                       00, 0x6c, // 108 - fieldTransferSize
+                                       00, 0x6c, // 108 - FieldTransferSize
                                        00, 02,
                                        0x63, 0x3b,
-                                       00, 0x6b, // 107 = fieldRefNum
+                                       00, 0x6b, // 107 = FieldRefNum
                                        00, 0x04,
                                        00, 0x02, 0x93, 0x47,
                                        0,
@@ -332,10 +258,10 @@ func Test_transactionScanner(t *testing.T) {
                                        00, 00, 00, 0x10,
                                        00, 00, 00, 0x10,
                                        00, 02,
-                                       00, 0x6c, // 108 - fieldTransferSize
+                                       00, 0x6c, // 108 - FieldTransferSize
                                        00, 02,
                                        0x63, 0x3b,
-                                       00, 0x6b, // 107 = fieldRefNum
+                                       00, 0x6b, // 107 = FieldRefNum
                                        00, 0x04,
                                        00, 0x02, 0x93, 0x47,
                                },
@@ -351,10 +277,10 @@ func Test_transactionScanner(t *testing.T) {
                                00, 00, 00, 0x10,
                                00, 00, 00, 0x10,
                                00, 02,
-                               00, 0x6c, // 108 - fieldTransferSize
+                               00, 0x6c, // 108 - FieldTransferSize
                                00, 02,
                                0x63, 0x3b,
-                               00, 0x6b, // 107 = fieldRefNum
+                               00, 0x6b, // 107 = FieldRefNum
                                00, 0x04,
                                00, 0x02, 0x93, 0x47,
                        },
@@ -372,3 +298,113 @@ func Test_transactionScanner(t *testing.T) {
                })
        }
 }
+
+func TestTransaction_Read(t1 *testing.T) {
+       type fields struct {
+               clientID   *[]byte
+               Flags      byte
+               IsReply    byte
+               Type       []byte
+               ID         []byte
+               ErrorCode  []byte
+               TotalSize  []byte
+               DataSize   []byte
+               ParamCount []byte
+               Fields     []Field
+               readOffset int
+       }
+       type args struct {
+               p []byte
+       }
+       tests := []struct {
+               name      string
+               fields    fields
+               args      args
+               want      int
+               wantErr   assert.ErrorAssertionFunc
+               wantBytes []byte
+       }{
+               {
+                       name: "returns transaction bytes",
+                       fields: fields{
+                               Flags:     0x00,
+                               IsReply:   0x01,
+                               Type:      []byte{0, 0},
+                               ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                               ErrorCode: []byte{0, 0, 0, 0},
+                               Fields: []Field{
+                                       NewField(FieldData, []byte("TEST")),
+                               },
+                       },
+                       args: args{
+                               p: make([]byte, 1024),
+                       },
+                       want:      30,
+                       wantErr:   assert.NoError,
+                       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},
+               },
+               {
+                       name: "returns transaction bytes from readOffset",
+                       fields: fields{
+                               Flags:     0x00,
+                               IsReply:   0x01,
+                               Type:      []byte{0, 0},
+                               ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                               ErrorCode: []byte{0, 0, 0, 0},
+                               Fields: []Field{
+                                       NewField(FieldData, []byte("TEST")),
+                               },
+                               readOffset: 20,
+                       },
+                       args: args{
+                               p: make([]byte, 1024),
+                       },
+                       want:      10,
+                       wantErr:   assert.NoError,
+                       wantBytes: []byte{0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
+               },
+               {
+                       name: "returns io.EOF when all bytes read",
+                       fields: fields{
+                               Flags:     0x00,
+                               IsReply:   0x01,
+                               Type:      []byte{0, 0},
+                               ID:        []byte{0x9a, 0xcb, 0x04, 0x42},
+                               ErrorCode: []byte{0, 0, 0, 0},
+                               Fields: []Field{
+                                       NewField(FieldData, []byte("TEST")),
+                               },
+                               readOffset: 30,
+                       },
+                       args: args{
+                               p: make([]byte, 1024),
+                       },
+                       want:      0,
+                       wantErr:   assert.Error,
+                       wantBytes: []byte{},
+               },
+       }
+       for _, tt := range tests {
+               t1.Run(tt.name, func(t1 *testing.T) {
+                       t := &Transaction{
+                               clientID:   tt.fields.clientID,
+                               Flags:      tt.fields.Flags,
+                               IsReply:    tt.fields.IsReply,
+                               Type:       tt.fields.Type,
+                               ID:         tt.fields.ID,
+                               ErrorCode:  tt.fields.ErrorCode,
+                               TotalSize:  tt.fields.TotalSize,
+                               DataSize:   tt.fields.DataSize,
+                               ParamCount: tt.fields.ParamCount,
+                               Fields:     tt.fields.Fields,
+                               readOffset: tt.fields.readOffset,
+                       }
+                       got, err := t.Read(tt.args.p)
+                       if !tt.wantErr(t1, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
+                               return
+                       }
+                       assert.Equalf(t1, tt.want, got, "Read(%v)", tt.args.p)
+                       assert.Equalf(t1, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)
+               })
+       }
+}