-
Notifications
You must be signed in to change notification settings - Fork 17
/
packet_test.go
143 lines (135 loc) · 3.14 KB
/
packet_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
package dhcp6
import (
"bytes"
"reflect"
"testing"
)
// TestPacketMarshalBinary verifies that Packet.MarshalBinary allocates and returns a correct
// byte slice for a variety of input data.
func TestPacketMarshalBinary(t *testing.T) {
var tests = []struct {
desc string
packet *Packet
buf []byte
}{
{
desc: "empty packet",
packet: &Packet{},
buf: []byte{0, 0, 0, 0},
},
{
desc: "Solicit only",
packet: &Packet{
MessageType: MessageTypeSolicit,
},
buf: []byte{1, 0, 0, 0},
},
{
desc: "Solicit, [1 2 3] transaction ID",
packet: &Packet{
MessageType: MessageTypeSolicit,
TransactionID: [3]byte{1, 2, 3},
},
buf: []byte{1, 1, 2, 3},
},
{
desc: "Solicit, [2, 3, 4] transaction ID, option client ID [0 1]",
packet: &Packet{
MessageType: MessageTypeSolicit,
TransactionID: [3]byte{1, 2, 3},
Options: Options{
OptionClientID: [][]byte{{0, 1}},
},
},
buf: []byte{1, 1, 2, 3, 0, 1, 0, 2, 0, 1},
},
}
for i, tt := range tests {
buf, err := tt.packet.MarshalBinary()
if err != nil {
t.Fatal(err)
}
if want, got := tt.buf, buf; !bytes.Equal(want, got) {
t.Fatalf("[%02d] test %q, unexpected packet bytes:\n- want: %v\n- got: %v",
i, tt.desc, want, got)
}
}
}
// TestPacketUnmarshalBinary verifies that Packet.UnmarshalBinary returns
// appropriate Packets and errors for various input byte slices.
func TestPacketUnmarshalBinary(t *testing.T) {
var tests = []struct {
desc string
buf []byte
packet *Packet
err error
}{
{
desc: "nil buffer, malformed packet",
err: ErrInvalidPacket,
},
{
desc: "empty buffer, malformed packet",
buf: []byte{},
err: ErrInvalidPacket,
},
{
desc: "length 1 buffer, malformed packet",
buf: []byte{0},
err: ErrInvalidPacket,
},
{
desc: "length 3 buffer, malformed packet",
buf: []byte{0, 0, 0},
err: ErrInvalidPacket,
},
{
desc: "invalid options in packet",
buf: []byte{0, 0, 0, 0, 0, 1, 0, 1},
err: ErrInvalidPacket,
},
{
desc: "length 4 buffer, OK",
buf: []byte{0, 0, 0, 0},
packet: &Packet{
MessageType: 0,
TransactionID: [3]byte{0, 0, 0},
Options: make(Options),
},
},
{
desc: "Solicit, [1 2 3] transaction ID, OK",
buf: []byte{1, 1, 2, 3},
packet: &Packet{
MessageType: MessageTypeSolicit,
TransactionID: [3]byte{1, 2, 3},
Options: make(Options),
},
},
{
desc: "Solicit, [2 3 4] transaction ID, option client ID [0 1], OK",
buf: []byte{1, 2, 3, 4, 0, 1, 0, 2, 0, 1},
packet: &Packet{
MessageType: MessageTypeSolicit,
TransactionID: [3]byte{2, 3, 4},
Options: Options{
OptionClientID: [][]byte{{0, 1}},
},
},
},
}
for i, tt := range tests {
p := new(Packet)
if err := p.UnmarshalBinary(tt.buf); err != nil {
if want, got := tt.err, err; want != got {
t.Fatalf("[%02d] test %q, unexpected error: %v != %v",
i, tt.desc, want, got)
}
continue
}
if want, got := tt.packet, p; !reflect.DeepEqual(want, got) {
t.Fatalf("[%02d] test %q, unexpected packet:\n- want: %v\n- got: %v",
i, tt.desc, want, got)
}
}
}