]> git.r.bdr.sh - rbdr/mobius/blame_incremental - hotline/transaction_test.go
Delete docs/Screenshot 2024-05-03 at 4.40.09 PM.png
[rbdr/mobius] / hotline / transaction_test.go
... / ...
CommitLineData
1package hotline
2
3import (
4 "fmt"
5 "github.com/stretchr/testify/assert"
6 "testing"
7)
8
9func Test_transactionScanner(t *testing.T) {
10 type args struct {
11 data []byte
12 in1 bool
13 }
14 tests := []struct {
15 name string
16 args args
17 wantAdvance int
18 wantToken []byte
19 wantErr assert.ErrorAssertionFunc
20 }{
21 {
22 name: "when too few bytes are provided to read the transaction size",
23 args: args{
24 data: []byte{},
25 in1: false,
26 },
27 wantAdvance: 0,
28 wantToken: []byte(nil),
29 wantErr: assert.NoError,
30 },
31 {
32 name: "when too few bytes are provided to read the full payload",
33 args: args{
34 data: []byte{
35 0,
36 1,
37 0, 0,
38 0, 00, 00, 04,
39 00, 00, 00, 00,
40 00, 00, 00, 10,
41 00, 00, 00, 10,
42 },
43 in1: false,
44 },
45 wantAdvance: 0,
46 wantToken: []byte(nil),
47 wantErr: assert.NoError,
48 },
49 {
50 name: "when a full transaction is provided",
51 args: args{
52 data: []byte{
53 0,
54 1,
55 0, 0,
56 0, 00, 00, 0x04,
57 00, 00, 00, 0x00,
58 00, 00, 00, 0x10,
59 00, 00, 00, 0x10,
60 00, 02,
61 00, 0x6c, // 108 - FieldTransferSize
62 00, 02,
63 0x63, 0x3b,
64 00, 0x6b, // 107 = FieldRefNum
65 00, 0x04,
66 00, 0x02, 0x93, 0x47,
67 },
68 in1: false,
69 },
70 wantAdvance: 36,
71 wantToken: []byte{
72 0,
73 1,
74 0, 0,
75 0, 00, 00, 0x04,
76 00, 00, 00, 0x00,
77 00, 00, 00, 0x10,
78 00, 00, 00, 0x10,
79 00, 02,
80 00, 0x6c, // 108 - FieldTransferSize
81 00, 02,
82 0x63, 0x3b,
83 00, 0x6b, // 107 = FieldRefNum
84 00, 0x04,
85 00, 0x02, 0x93, 0x47,
86 },
87 wantErr: assert.NoError,
88 },
89 {
90 name: "when a full transaction plus extra bytes are provided",
91 args: args{
92 data: []byte{
93 0,
94 1,
95 0, 0,
96 0, 00, 00, 0x04,
97 00, 00, 00, 0x00,
98 00, 00, 00, 0x10,
99 00, 00, 00, 0x10,
100 00, 02,
101 00, 0x6c, // 108 - FieldTransferSize
102 00, 02,
103 0x63, 0x3b,
104 00, 0x6b, // 107 = FieldRefNum
105 00, 0x04,
106 00, 0x02, 0x93, 0x47,
107 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
108 },
109 in1: false,
110 },
111 wantAdvance: 36,
112 wantToken: []byte{
113 0,
114 1,
115 0, 0,
116 0, 00, 00, 0x04,
117 00, 00, 00, 0x00,
118 00, 00, 00, 0x10,
119 00, 00, 00, 0x10,
120 00, 02,
121 00, 0x6c, // 108 - FieldTransferSize
122 00, 02,
123 0x63, 0x3b,
124 00, 0x6b, // 107 = FieldRefNum
125 00, 0x04,
126 00, 0x02, 0x93, 0x47,
127 },
128 wantErr: assert.NoError,
129 },
130 {
131 name: "when two full transactions are provided",
132 args: args{
133 data: []byte{
134 0,
135 1,
136 0, 0,
137 0, 00, 00, 0x04,
138 00, 00, 00, 0x00,
139 00, 00, 00, 0x10,
140 00, 00, 00, 0x10,
141 00, 02,
142 00, 0x6c, // 108 - FieldTransferSize
143 00, 02,
144 0x63, 0x3b,
145 00, 0x6b, // 107 = FieldRefNum
146 00, 0x04,
147 00, 0x02, 0x93, 0x47,
148 0,
149 1,
150 0, 0,
151 0, 00, 00, 0x04,
152 00, 00, 00, 0x00,
153 00, 00, 00, 0x10,
154 00, 00, 00, 0x10,
155 00, 02,
156 00, 0x6c, // 108 - FieldTransferSize
157 00, 02,
158 0x63, 0x3b,
159 00, 0x6b, // 107 = FieldRefNum
160 00, 0x04,
161 00, 0x02, 0x93, 0x47,
162 },
163 in1: false,
164 },
165 wantAdvance: 36,
166 wantToken: []byte{
167 0,
168 1,
169 0, 0,
170 0, 00, 00, 0x04,
171 00, 00, 00, 0x00,
172 00, 00, 00, 0x10,
173 00, 00, 00, 0x10,
174 00, 02,
175 00, 0x6c, // 108 - FieldTransferSize
176 00, 02,
177 0x63, 0x3b,
178 00, 0x6b, // 107 = FieldRefNum
179 00, 0x04,
180 00, 0x02, 0x93, 0x47,
181 },
182 wantErr: assert.NoError,
183 },
184 }
185 for _, tt := range tests {
186 t.Run(tt.name, func(t *testing.T) {
187 gotAdvance, gotToken, err := transactionScanner(tt.args.data, tt.args.in1)
188 if !tt.wantErr(t, err, fmt.Sprintf("transactionScanner(%v, %v)", tt.args.data, tt.args.in1)) {
189 return
190 }
191 assert.Equalf(t, tt.wantAdvance, gotAdvance, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
192 assert.Equalf(t, tt.wantToken, gotToken, "transactionScanner(%v, %v)", tt.args.data, tt.args.in1)
193 })
194 }
195}
196
197func TestTransaction_Read(t1 *testing.T) {
198 type fields struct {
199 clientID [2]byte
200 Flags byte
201 IsReply byte
202 Type [2]byte
203 ID [4]byte
204 ErrorCode [4]byte
205 TotalSize [4]byte
206 DataSize [4]byte
207 ParamCount [2]byte
208 Fields []Field
209 readOffset int
210 }
211 type args struct {
212 p []byte
213 }
214 tests := []struct {
215 name string
216 fields fields
217 args args
218 want int
219 wantErr assert.ErrorAssertionFunc
220 wantBytes []byte
221 }{
222 {
223 name: "returns transaction bytes",
224 fields: fields{
225 Flags: 0x00,
226 IsReply: 0x01,
227 Type: [2]byte{0, 0},
228 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
229 ErrorCode: [4]byte{0, 0, 0, 0},
230 Fields: []Field{
231 NewField(FieldData, []byte("TEST")),
232 },
233 },
234 args: args{
235 p: make([]byte, 1024),
236 },
237 want: 30,
238 wantErr: assert.NoError,
239 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},
240 },
241 {
242 name: "returns transaction bytes from readOffset",
243 fields: fields{
244 Flags: 0x00,
245 IsReply: 0x01,
246 Type: [2]byte{0, 0},
247 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
248 ErrorCode: [4]byte{0, 0, 0, 0},
249 Fields: []Field{
250 NewField(FieldData, []byte("TEST")),
251 },
252 readOffset: 20,
253 },
254 args: args{
255 p: make([]byte, 1024),
256 },
257 want: 10,
258 wantErr: assert.NoError,
259 wantBytes: []byte{0x0, 0x1, 0x0, 0x65, 0x0, 0x4, 0x54, 0x45, 0x53, 0x54},
260 },
261 {
262 name: "returns io.EOF when all bytes read",
263 fields: fields{
264 Flags: 0x00,
265 IsReply: 0x01,
266 Type: [2]byte{0, 0},
267 ID: [4]byte{0x9a, 0xcb, 0x04, 0x42},
268 ErrorCode: [4]byte{0, 0, 0, 0},
269 Fields: []Field{
270 NewField(FieldData, []byte("TEST")),
271 },
272 readOffset: 30,
273 },
274 args: args{
275 p: make([]byte, 1024),
276 },
277 want: 0,
278 wantErr: assert.Error,
279 wantBytes: []byte{},
280 },
281 }
282 for _, tt := range tests {
283 t1.Run(tt.name, func(t1 *testing.T) {
284 t := &Transaction{
285 clientID: tt.fields.clientID,
286 Flags: tt.fields.Flags,
287 IsReply: tt.fields.IsReply,
288 Type: tt.fields.Type,
289 ID: tt.fields.ID,
290 ErrorCode: tt.fields.ErrorCode,
291 TotalSize: tt.fields.TotalSize,
292 DataSize: tt.fields.DataSize,
293 ParamCount: tt.fields.ParamCount,
294 Fields: tt.fields.Fields,
295 readOffset: tt.fields.readOffset,
296 }
297 got, err := t.Read(tt.args.p)
298 if !tt.wantErr(t1, err, fmt.Sprintf("Read(%v)", tt.args.p)) {
299 return
300 }
301 assert.Equalf(t1, tt.want, got, "Read(%v)", tt.args.p)
302 assert.Equalf(t1, tt.wantBytes, tt.args.p[:got], "Read(%v)", tt.args.p)
303 })
304 }
305}
306
307func TestTransaction_Write(t1 *testing.T) {
308 type args struct {
309 p []byte
310 }
311 tests := []struct {
312 name string
313 args args
314 wantN int
315 wantErr assert.ErrorAssertionFunc
316 wantTransaction Transaction
317 }{
318 {
319 name: "returns error if arg p is too small",
320 args: args{p: []byte{
321 0x00, 0x00,
322 }},
323 wantN: 0,
324 wantErr: assert.Error,
325 wantTransaction: Transaction{},
326 },
327 //{
328 // name: "returns error if param data is invalid",
329 // args: args{p: []byte{
330 // 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72,
331 // 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
332 // 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x65,
333 // 0x00, 0x03, 0x68, 0x61, 0x69,
334 // }},
335 // wantN: 0,
336 // wantErr: assert.Error,
337 // wantTransaction: Transaction{},
338 //},
339 {
340 name: "writes bytes to transaction",
341 args: args{p: []byte{
342 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x15, 0x72,
343 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09,
344 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x65,
345 0x00, 0x03, 0x68, 0x61, 0x69,
346 }},
347 wantN: 29,
348 wantErr: assert.NoError,
349 wantTransaction: Transaction{
350 Flags: 0,
351 IsReply: 0,
352 Type: TranChatSend,
353 ID: [4]byte{},
354 ErrorCode: [4]byte{},
355 TotalSize: [4]byte{0, 0, 0, 9},
356 DataSize: [4]byte{0, 0, 0, 9},
357 ParamCount: [2]byte{0, 1},
358 Fields: []Field{
359 {
360 Type: FieldData,
361 FieldSize: [2]byte{0, 3},
362 Data: []byte("hai"),
363 },
364 },
365 clientID: [2]byte{},
366 readOffset: 0,
367 },
368 },
369 }
370 for _, tt := range tests {
371 t1.Run(tt.name, func(t1 *testing.T) {
372 t := &Transaction{}
373 gotN, err := t.Write(tt.args.p)
374 if !tt.wantErr(t1, err, fmt.Sprintf("Write(%v)", tt.args.p)) {
375 return
376 }
377 assert.Equalf(t1, tt.wantN, gotN, "Write(%v)", tt.args.p)
378
379 tranAssertEqual(t1, []Transaction{tt.wantTransaction}, []Transaction{*t})
380 })
381 }
382}