]> git.r.bdr.sh - rbdr/mobius/blob - hotline/transaction_test.go
1fade255c63bc5e2047624b2d459b7257e93b066
[rbdr/mobius] / hotline / transaction_test.go
1 package hotline
2
3 import (
4 "github.com/stretchr/testify/assert"
5 "testing"
6 )
7
8 func TestReadFields(t *testing.T) {
9 type args struct {
10 paramCount []byte
11 buf []byte
12 }
13 tests := []struct {
14 name string
15 args args
16 want []Field
17 wantErr bool
18 }{
19 {
20 name: "valid field data",
21 args: args{
22 paramCount: []byte{0x00, 0x02},
23 buf: []byte{
24 0x00, 0x65, // ID: fieldData
25 0x00, 0x04, // Size: 2 bytes
26 0x01, 0x02, 0x03, 0x04, // Data
27 0x00, 0x66, // ID: fieldUserName
28 0x00, 0x02, // Size: 2 bytes
29 0x00, 0x01, // Data
30 },
31 },
32 want: []Field{
33 {
34 ID: []byte{0x00, 0x65},
35 FieldSize: []byte{0x00, 0x04},
36 Data: []byte{0x01, 0x02, 0x03, 0x04},
37 },
38 {
39 ID: []byte{0x00, 0x66},
40 FieldSize: []byte{0x00, 0x02},
41 Data: []byte{0x00, 0x01},
42 },
43 },
44 wantErr: false,
45 },
46 {
47 name: "empty bytes",
48 args: args{
49 paramCount: []byte{0x00, 0x00},
50 buf: []byte{},
51 },
52 want: []Field(nil),
53 wantErr: false,
54 },
55 {
56 name: "when field size does not match data length",
57 args: args{
58 paramCount: []byte{0x00, 0x01},
59 buf: []byte{
60 0x00, 0x65, // ID: fieldData
61 0x00, 0x04, // Size: 4 bytes
62 0x01, 0x02, 0x03, // Data
63 },
64 },
65 want: []Field{},
66 wantErr: true,
67 },
68 {
69 name: "when field size of second field does not match data length",
70 args: args{
71 paramCount: []byte{0x00, 0x01},
72 buf: []byte{
73 0x00, 0x65, // ID: fieldData
74 0x00, 0x02, // Size: 2 bytes
75 0x01, 0x02, // Data
76 0x00, 0x65, // ID: fieldData
77 0x00, 0x04, // Size: 4 bytes
78 0x01, 0x02, 0x03, // Data
79 },
80 },
81 want: []Field{},
82 wantErr: true,
83 },
84 {
85 name: "when field data has extra bytes",
86 args: args{
87 paramCount: []byte{0x00, 0x01},
88 buf: []byte{
89 0x00, 0x65, // ID: fieldData
90 0x00, 0x02, // Size: 2 bytes
91 0x01, 0x02, 0x03, // Data
92 },
93 },
94 want: []Field{},
95 wantErr: true,
96 },
97 }
98 for _, tt := range tests {
99 t.Run(tt.name, func(t *testing.T) {
100 got, err := ReadFields(tt.args.paramCount, tt.args.buf)
101 if (err != nil) != tt.wantErr {
102 t.Errorf("ReadFields() error = %v, wantErr %v", err, tt.wantErr)
103 return
104 }
105
106 if !assert.Equal(t, tt.want, got) {
107 t.Errorf("ReadFields() got = %v, want %v", got, tt.want)
108 }
109 })
110 }
111 }
112
113 func TestReadTransaction(t *testing.T) {
114 sampleTransaction := &Transaction{
115 Flags: byte(0),
116 IsReply: byte(0),
117 Type: []byte{0x000, 0x93},
118 ID: []byte{0x000, 0x00, 0x00, 0x01},
119 ErrorCode: []byte{0x000, 0x00, 0x00, 0x00},
120 TotalSize: []byte{0x000, 0x00, 0x00, 0x08},
121 DataSize: []byte{0x000, 0x00, 0x00, 0x08},
122 ParamCount: []byte{0x00, 0x01},
123 Fields: []Field{
124 {
125 ID: []byte{0x00, 0x01},
126 FieldSize: []byte{0x00, 0x02},
127 Data: []byte{0xff, 0xff},
128 },
129 },
130 }
131
132 type args struct {
133 buf []byte
134 }
135 tests := []struct {
136 name string
137 args args
138 want *Transaction
139 want1 int
140 wantErr bool
141 }{
142 {
143 name: "when buf contains all bytes for a single transaction",
144 args: args{
145 buf: func() []byte {
146 b, _ := sampleTransaction.MarshalBinary()
147 return b
148 }(),
149 },
150 want: sampleTransaction,
151 want1: func() int {
152 b, _ := sampleTransaction.MarshalBinary()
153 return len(b)
154 }(),
155 wantErr: false,
156 },
157 {
158 name: "when len(buf) is less than the length of the transaction",
159 args: args{
160 buf: func() []byte {
161 b, _ := sampleTransaction.MarshalBinary()
162 return b[:len(b)-1]
163 }(),
164 },
165 want: nil,
166 want1: 0,
167 wantErr: true,
168 },
169 }
170 for _, tt := range tests {
171 t.Run(tt.name, func(t *testing.T) {
172 got, got1, err := ReadTransaction(tt.args.buf)
173 if (err != nil) != tt.wantErr {
174 t.Errorf("ReadTransaction() error = %v, wantErr %v", err, tt.wantErr)
175 return
176 }
177 if !assert.Equal(t, tt.want, got) {
178 t.Errorf("ReadTransaction() got = %v, want %v", got, tt.want)
179 }
180 if got1 != tt.want1 {
181 t.Errorf("ReadTransaction() got1 = %v, want %v", got1, tt.want1)
182 }
183 })
184 }
185 }