-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire_test.go
97 lines (85 loc) · 2.01 KB
/
wire_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
package tftp
import (
"reflect"
"testing"
)
func TestSerializationDeserialization(t *testing.T) {
tests := []struct {
bytes []byte
packet Packet
}{
{
[]byte("\x00\x01foo\x00bar\x00"),
&PacketRequest{OpRRQ, "foo", "bar"},
},
{
[]byte("\x00\x02foo\x00bar\x00"),
&PacketRequest{OpWRQ, "foo", "bar"},
},
{
[]byte("\x00\x03\x12\x34fnord"),
&PacketData{0x1234, []byte("fnord")},
},
{
[]byte("\x00\x03\x12\x34"),
&PacketData{0x1234, []byte("")},
},
{
[]byte("\x00\x04\xd0\x0f"),
&PacketAck{0xd00f},
},
{
[]byte("\x00\x05\xab\xcdparachute failure\x00"),
&PacketError{0xabcd, "parachute failure"},
},
}
for _, test := range tests {
actualBytes := test.packet.Serialize()
if !reflect.DeepEqual(test.bytes, actualBytes) {
t.Errorf("Serializing %#v: expected %q; got %q", test.packet, test.bytes, actualBytes)
}
actualPacket, err := ParsePacket(test.bytes)
if err != nil {
t.Errorf("Unable to parse packet %q: %s", test.bytes, err)
} else if !reflect.DeepEqual(test.packet, actualPacket) {
t.Errorf("Deserializing %q: expected %#v; got %#v", test.bytes, test.packet, actualPacket)
}
}
}
func TestDeserializationInvalid(t *testing.T) {
tests := [][]byte{
// no opcode
[]byte(""),
// invalid opcode
[]byte("\x00\x00"),
[]byte("\x00\x06"),
[]byte("\xff\x01"),
[]byte("\xff\xff"),
// short RRQ
[]byte("\x00\x01"),
[]byte("\x00\x01foo"),
[]byte("\x00\x01foo\x00"),
[]byte("\x00\x01foo\x00bar"),
// short WRQ
[]byte("\x00\x02"),
[]byte("\x00\x02foo"),
[]byte("\x00\x02foo\x00"),
[]byte("\x00\x02foo\x00bar"),
// short data
[]byte("\x00\x03"),
[]byte("\x00\x03\x01"),
// short ack
[]byte("\x00\x04"),
[]byte("\x00\x04\x01"),
// short error
[]byte("\x00\x05"),
[]byte("\x00\x05\xab"),
[]byte("\x00\x05\xab\xcd"),
[]byte("\x00\x05\xab\xcdparachute failure"),
}
for _, test := range tests {
if p, err := ParsePacket(test); err == nil {
t.Errorf("Parsing packet %q: expected error; got %#v", test, p)
}
}
}