X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/95159e5585762c06c654945070ba54262b7dcec9..8f9edf2f3bb18f7ab1a04ead182a1daf2cfd41d9:/hotline/transaction_test.go?ds=sidebyside diff --git a/hotline/transaction_test.go b/hotline/transaction_test.go index cdb6f1e..97d865d 100644 --- a/hotline/transaction_test.go +++ b/hotline/transaction_test.go @@ -6,111 +6,6 @@ import ( "testing" ) -func TestReadFields(t *testing.T) { - type args struct { - paramCount []byte - buf []byte - } - tests := []struct { - name string - args args - want []Field - wantErr bool - }{ - { - name: "valid field data", - args: args{ - paramCount: []byte{0x00, 0x02}, - buf: []byte{ - 0x00, 0x65, // ID: FieldData - 0x00, 0x04, // Size: 2 bytes - 0x01, 0x02, 0x03, 0x04, // Data - 0x00, 0x66, // ID: FieldUserName - 0x00, 0x02, // Size: 2 bytes - 0x00, 0x01, // Data - }, - }, - want: []Field{ - { - ID: [2]byte{0x00, 0x65}, - FieldSize: [2]byte{0x00, 0x04}, - Data: []byte{0x01, 0x02, 0x03, 0x04}, - }, - { - ID: [2]byte{0x00, 0x66}, - FieldSize: [2]byte{0x00, 0x02}, - Data: []byte{0x00, 0x01}, - }, - }, - wantErr: false, - }, - { - name: "empty bytes", - args: args{ - paramCount: []byte{0x00, 0x00}, - buf: []byte{}, - }, - want: []Field(nil), - wantErr: false, - }, - { - name: "when field size does not match data length", - args: args{ - paramCount: []byte{0x00, 0x01}, - buf: []byte{ - 0x00, 0x65, // ID: FieldData - 0x00, 0x04, // Size: 4 bytes - 0x01, 0x02, 0x03, // Data - }, - }, - want: []Field{}, - wantErr: true, - }, - { - name: "when field size of second field does not match data length", - args: args{ - paramCount: []byte{0x00, 0x01}, - buf: []byte{ - 0x00, 0x65, // ID: FieldData - 0x00, 0x02, // Size: 2 bytes - 0x01, 0x02, // Data - 0x00, 0x65, // ID: FieldData - 0x00, 0x04, // Size: 4 bytes - 0x01, 0x02, 0x03, // Data - }, - }, - want: []Field{}, - wantErr: true, - }, - { - name: "when field data has extra bytes", - args: args{ - paramCount: []byte{0x00, 0x01}, - buf: []byte{ - 0x00, 0x65, // ID: FieldData - 0x00, 0x02, // Size: 2 bytes - 0x01, 0x02, 0x03, // Data - }, - }, - want: []Field{}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got, err := ReadFields(tt.args.paramCount, tt.args.buf) - if (err != nil) != tt.wantErr { - t.Errorf("ReadFields() error = %v, wantErr %v", err, tt.wantErr) - return - } - - if !assert.Equal(t, tt.want, got) { - t.Errorf("ReadFields() got = %v, want %v", got, tt.want) - } - }) - } -} - func Test_transactionScanner(t *testing.T) { type args struct { data []byte @@ -301,15 +196,15 @@ func Test_transactionScanner(t *testing.T) { func TestTransaction_Read(t1 *testing.T) { type fields struct { - clientID *[]byte + clientID [2]byte Flags byte IsReply byte - Type []byte - ID []byte - ErrorCode []byte - TotalSize []byte - DataSize []byte - ParamCount []byte + Type [2]byte + ID [4]byte + ErrorCode [4]byte + TotalSize [4]byte + DataSize [4]byte + ParamCount [2]byte Fields []Field readOffset int } @@ -329,9 +224,9 @@ func TestTransaction_Read(t1 *testing.T) { fields: fields{ Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0}, - ID: []byte{0x9a, 0xcb, 0x04, 0x42}, - ErrorCode: []byte{0, 0, 0, 0}, + Type: [2]byte{0, 0}, + ID: [4]byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: [4]byte{0, 0, 0, 0}, Fields: []Field{ NewField(FieldData, []byte("TEST")), }, @@ -348,9 +243,9 @@ func TestTransaction_Read(t1 *testing.T) { fields: fields{ Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0}, - ID: []byte{0x9a, 0xcb, 0x04, 0x42}, - ErrorCode: []byte{0, 0, 0, 0}, + Type: [2]byte{0, 0}, + ID: [4]byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: [4]byte{0, 0, 0, 0}, Fields: []Field{ NewField(FieldData, []byte("TEST")), }, @@ -368,9 +263,9 @@ func TestTransaction_Read(t1 *testing.T) { fields: fields{ Flags: 0x00, IsReply: 0x01, - Type: []byte{0, 0}, - ID: []byte{0x9a, 0xcb, 0x04, 0x42}, - ErrorCode: []byte{0, 0, 0, 0}, + Type: [2]byte{0, 0}, + ID: [4]byte{0x9a, 0xcb, 0x04, 0x42}, + ErrorCode: [4]byte{0, 0, 0, 0}, Fields: []Field{ NewField(FieldData, []byte("TEST")), }, @@ -387,7 +282,7 @@ func TestTransaction_Read(t1 *testing.T) { for _, tt := range tests { t1.Run(tt.name, func(t1 *testing.T) { t := &Transaction{ - clientID: tt.fields.clientID, + ClientID: tt.fields.clientID, Flags: tt.fields.Flags, IsReply: tt.fields.IsReply, Type: tt.fields.Type, @@ -408,3 +303,80 @@ func TestTransaction_Read(t1 *testing.T) { }) } } + +func TestTransaction_Write(t1 *testing.T) { + type args struct { + p []byte + } + tests := []struct { + name string + args args + wantN int + wantErr assert.ErrorAssertionFunc + wantTransaction Transaction + }{ + { + name: "returns error if arg p is too small", + args: args{p: []byte{ + 0x00, 0x00, + }}, + wantN: 0, + wantErr: assert.Error, + wantTransaction: Transaction{}, + }, + //{ + // name: "returns error if param data is invalid", + // args: args{p: []byte{ + // 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72, + // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, + // 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x65, + // 0x00, 0x03, 0x68, 0x61, 0x69, + // }}, + // wantN: 0, + // wantErr: assert.Error, + // wantTransaction: Transaction{}, + //}, + { + name: "writes bytes to transaction", + args: args{p: []byte{ + 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, + 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x65, + 0x00, 0x03, 0x68, 0x61, 0x69, + }}, + wantN: 29, + wantErr: assert.NoError, + wantTransaction: Transaction{ + Flags: 0, + IsReply: 0, + Type: TranChatSend, + ID: [4]byte{}, + ErrorCode: [4]byte{}, + TotalSize: [4]byte{0, 0, 0, 9}, + DataSize: [4]byte{0, 0, 0, 9}, + ParamCount: [2]byte{0, 1}, + Fields: []Field{ + { + Type: FieldData, + FieldSize: [2]byte{0, 3}, + Data: []byte("hai"), + }, + }, + ClientID: [2]byte{}, + readOffset: 0, + }, + }, + } + for _, tt := range tests { + t1.Run(tt.name, func(t1 *testing.T) { + t := &Transaction{} + gotN, err := t.Write(tt.args.p) + if !tt.wantErr(t1, err, fmt.Sprintf("Write(%v)", tt.args.p)) { + return + } + assert.Equalf(t1, tt.wantN, gotN, "Write(%v)", tt.args.p) + + TranAssertEqual(t1, []Transaction{tt.wantTransaction}, []Transaction{*t}) + }) + } +}