diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-28 18:21:52 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2021-07-28 14:21:52 -0700 |
| commit | 22c599abc18895f73e96095f35b71cf3357d41b4 (patch) | |
| tree | 482ef57d386c955692ea43c43e4655b3c3763499 /hotline/transaction_test.go | |
| parent | 71c56068adca18f76ebee86355f000a3e51d3127 (diff) | |
Move code to hotline dir
Diffstat (limited to 'hotline/transaction_test.go')
| -rw-r--r-- | hotline/transaction_test.go | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/hotline/transaction_test.go b/hotline/transaction_test.go new file mode 100644 index 0000000..227b9e0 --- /dev/null +++ b/hotline/transaction_test.go @@ -0,0 +1,176 @@ +package hotline + +import ( + "github.com/stretchr/testify/assert" + "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: []byte{0x00, 0x65}, + FieldSize: []byte{0x00, 0x04}, + Data: []byte{0x01, 0x02, 0x03, 0x04}, + }, + { + ID: []byte{0x00, 0x66}, + FieldSize: []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 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: sampleTransaction.Payload(), + }, + want: sampleTransaction, + want1: len(sampleTransaction.Payload()), + wantErr: false, + }, + { + name: "when len(buf) is less than the length of the transaction", + args: args{ + buf: sampleTransaction.Payload()[:len(sampleTransaction.Payload()) - 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) + } + }) + } +} |