-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdatagram_test.go
506 lines (461 loc) · 9.81 KB
/
datagram_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
// Copyright (C) 2017 Kale Blankenship. All rights reserved.
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details
package tftp // import "pack.ag/tftp"
import (
"bytes"
"reflect"
"testing"
)
func TestOpcode_String(t *testing.T) {
cases := []struct {
code opcode
expected string
}{
{
code: opCodeRRQ,
expected: "READ_REQUEST",
},
{
code: opCodeWRQ,
expected: "WRITE_REQUEST",
},
{
code: opCodeDATA,
expected: "DATA",
},
{
code: opCodeACK,
expected: "ACK",
},
{
code: opCodeERROR,
expected: "ERROR",
},
{
code: opCodeOACK,
expected: "OPTION_ACK",
},
{
code: 13,
expected: "UNKNOWN_OPCODE_13",
},
}
for _, c := range cases {
t.Run(c.expected, func(t *testing.T) {
if c.code.String() != c.expected {
t.Errorf("Expected opcode(%d).String() to be %q, but it was %q", c.code, c.expected, c.code.String())
}
})
}
}
func TestErrorCode_String(t *testing.T) {
cases := []struct {
code ErrorCode
expected string
}{
{
code: ErrCodeNotDefined,
expected: "NOT_DEFINED",
},
{
code: ErrCodeFileNotFound,
expected: "FILE_NOT_FOUND",
},
{
code: ErrCodeAccessViolation,
expected: "ACCESS_VIOLATION",
},
{
code: ErrCodeDiskFull,
expected: "DISK_FULL",
},
{
code: ErrCodeIllegalOperation,
expected: "ILLEGAL_OPERATION",
},
{
code: ErrCodeUnknownTransferID,
expected: "UNKNOWN_TRANSFER_ID",
},
{
code: ErrCodeFileAlreadyExists,
expected: "FILE_ALREADY_EXISTS",
},
{
code: ErrCodeNoSuchUser,
expected: "NO_SUCH_USER",
},
{
code: 13,
expected: "UNKNOWN_ERROR_13",
},
}
for _, c := range cases {
t.Run(c.expected, func(t *testing.T) {
if c.code.String() != c.expected {
t.Errorf("Expected errCode(%d).String() to be %q, but it was %q", c.code, c.expected, c.code.String())
}
})
}
}
func TestDatagram_String(t *testing.T) {
cases := []struct {
name string
dg datagram
expected string
}{
{
name: "RRQ",
dg: func() datagram {
d := datagram{}
d.writeReadReq("readFile", ModeNetASCII, options{"first": "option"})
return d
}(),
expected: `READ_REQUEST[Filename: "readFile"; Mode: "netascii"; Options: {"first": "option"}]`,
},
{
name: "WRQ",
dg: func() datagram {
d := datagram{}
d.writeWriteReq("readFile", ModeNetASCII, options{})
return d
}(),
expected: `WRITE_REQUEST[Filename: "readFile"; Mode: "netascii"; Options: {}]`,
},
{
name: "DATA",
dg: func() datagram {
d := datagram{}
d.writeData(678, []byte("the data"))
return d
}(),
expected: `DATA[Block: 678; Data Length: 8]`,
},
{
name: "OACK",
dg: func() datagram {
d := datagram{}
d.writeOptionAck(options{"first": "option"})
return d
}(),
expected: `OPTION_ACK[Options: {"first": "option"}]`,
},
{
name: "ACK",
dg: func() datagram {
d := datagram{}
d.writeAck(65000)
return d
}(),
expected: `ACK[Block: 65000]`,
},
{
name: "ERROR",
dg: func() datagram {
d := datagram{}
d.writeError(ErrCodeDiskFull, "my error")
return d
}(),
expected: `ERROR[Code: DISK_FULL; Message: "my error"]`,
},
{
name: "Bad Datagram",
dg: datagram{},
expected: `INVALID_DATAGRAM[Error: "Datagram has no opcode"]`,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if c.dg.String() != c.expected {
t.Errorf("expected to be %q, but it was %q", c.expected, c.dg.String())
}
})
}
}
func TestDatagram(t *testing.T) {
cases := []struct {
name string
dg datagram
valid bool
len int
data []byte
offset int
code opcode
block uint16
filename *string
mode *TransferMode
opts options
errCode *ErrorCode
errMessage *string
}{
{
name: "ack",
dg: func() datagram {
dg := datagram{}
dg.writeAck(3)
return dg
}(),
valid: true,
len: 4,
data: []byte{},
offset: 4,
code: opCodeACK,
block: 3,
},
{
name: "data",
dg: func() datagram {
dg := datagram{}
dg.writeData(314, []byte("this is the data"))
return dg
}(),
valid: true,
len: 20,
offset: 20,
code: opCodeDATA,
},
{
name: "RRQ",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("the file", ModeNetASCII, options{})
return dg
}(),
valid: true,
len: 20,
offset: 20,
code: opCodeRRQ,
filename: ptrString("the file"),
mode: ptrMode(ModeNetASCII),
opts: options{},
},
{
name: "WRQ",
dg: func() datagram {
dg := datagram{}
dg.writeWriteReq("a file", ModeOctet, options{})
return dg
}(),
valid: true,
len: 15,
offset: 15,
code: opCodeWRQ,
filename: ptrString("a file"),
mode: ptrMode(ModeOctet),
opts: options{},
},
{
name: "OACK, no options",
dg: func() datagram {
dg := datagram{}
dg.writeOptionAck(options{})
return dg
}(),
valid: false,
},
{
name: "OACK",
dg: func() datagram {
dg := datagram{}
dg.writeOptionAck(options{optBlocksize: "345"})
return dg
}(),
valid: true,
len: 14,
offset: 14,
code: opCodeOACK,
opts: options{optBlocksize: "345"},
},
{
name: "error",
dg: func() datagram {
dg := datagram{}
dg.writeError(ErrCodeDiskFull, "the message")
return dg
}(),
valid: true,
len: 16,
offset: 16,
code: opCodeERROR,
errCode: ptrErrCode(ErrCodeDiskFull),
errMessage: ptrString("the message"),
},
{
name: "no opcode",
dg: func() datagram {
dg := datagram{}
return dg
}(),
valid: false,
},
{
name: "invalid opcode",
dg: func() datagram {
dg := datagram{}
dg.reset(2)
dg.writeUint16(13)
return dg
}(),
valid: false,
},
{
name: "empty filename",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("", ModeOctet, options{})
dg.buf[dg.offset-1] = 'x'
return dg
}(),
valid: false,
},
{
name: "request doesn't end with null",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("file", ModeOctet, options{})
dg.buf[dg.offset-1] = 'x'
return dg
}(),
valid: false,
},
{
name: "request has odd number of null",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("file\x00name", ModeOctet, options{})
return dg
}(),
valid: false,
},
{
name: "mail",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("file", modeMail, options{})
return dg
}(),
valid: false,
},
{
name: "invalid mode",
dg: func() datagram {
dg := datagram{}
dg.writeReadReq("file", "fast", options{})
return dg
}(),
valid: false,
},
{
name: "corrupt block #",
dg: func() datagram {
dg := datagram{}
dg.writeData(133, []byte("data"))
dg.offset = 3
return dg
}(),
valid: false,
},
{
name: "corrupt error",
dg: func() datagram {
dg := datagram{}
dg.reset(4)
dg.writeUint16(uint16(opCodeERROR))
dg.writeUint16(uint16(ErrCodeAccessViolation))
return dg
}(),
valid: false,
},
{
name: "error doesn't end with null",
dg: func() datagram {
dg := datagram{}
dg.reset(8)
dg.writeUint16(uint16(opCodeERROR))
dg.writeUint16(uint16(ErrCodeAccessViolation))
dg.writeString("data")
return dg
}(),
valid: false,
},
{
name: "error has more than one null",
dg: func() datagram {
dg := datagram{}
dg.reset(8)
dg.writeError(ErrCodeDiskFull, "the\x00data")
return dg
}(),
valid: false,
},
{
name: "corrupt options",
dg: func() datagram {
dg := datagram{}
dg.reset(10)
dg.writeUint16(uint16(opCodeOACK))
dg.writeString(optBlocksize)
dg.writeNull()
return dg
}(),
valid: false,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
// Valid
if err := c.dg.validate(); (err == nil) != c.valid {
t.Errorf("expected %s to be valid %t, but it wasn't: %s", c.dg, c.valid, err)
}
if !c.valid {
return // No point in checking an invalid datagram
}
// Len
if len(c.dg.buf) != c.len {
t.Errorf("expected %s to have len %d, but it was %d", c.dg, c.len, len(c.dg.buf))
}
// Data
if c.data != nil && !bytes.Equal(c.dg.data(), c.data) {
t.Errorf("expected %s, to have data %q, but it was %q", c.dg, c.data, c.dg.data())
}
// Offset
if c.offset != c.dg.offset {
t.Errorf("expected %s to have offset %d, but it was %d", c.dg, c.offset, c.dg.offset)
}
// Code
if c.code != c.dg.opcode() {
t.Errorf("expected %s to have code %d, but it was %d", c.dg, c.code, c.dg.opcode())
}
// Filename
if c.filename != nil && *c.filename != c.dg.filename() {
t.Errorf("expected %s to have filename %q, but it was %q", c.dg, *c.filename, c.dg.filename())
}
// Mode
if c.mode != nil && *c.mode != c.dg.mode() {
t.Errorf("expected %s to have mode %q, but it was %q", c.dg, *c.mode, c.dg.mode())
}
// Options
if c.opts != nil && !reflect.DeepEqual(c.opts, c.dg.options()) {
t.Errorf("expected %s to have options %q, but it was %q", c.dg, c.opts, c.dg.options())
}
// Error Code
if c.errCode != nil && *c.errCode != c.dg.errorCode() {
t.Errorf("expected %s to have error code %d, but it was %d", c.dg, *c.errCode, c.dg.errorCode())
}
// Error Message
if c.errMessage != nil && *c.errMessage != c.dg.errMsg() {
t.Errorf("expected %s to have error message %q, but it was %q", c.dg, *c.errMessage, c.dg.errMsg())
}
})
}
}
func ptrString(s string) *string {
return &s
}
func ptrMode(s TransferMode) *TransferMode {
return &s
}
func ptrErrCode(e ErrorCode) *ErrorCode {
return &e
}