/
vxlan_test.go
171 lines (155 loc) · 3.12 KB
/
vxlan_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
package vxlan
import (
"bytes"
"io"
"testing"
"github.com/mdlayher/ethernet"
)
func TestFrameMarshalBinary(t *testing.T) {
var tests = []struct {
desc string
f *Frame
b []byte
err error
}{
{
desc: "Frame with VNI value too large",
f: &Frame{
VNI: MaxVNI + 1,
},
err: ErrInvalidVNI,
},
{
desc: "Frame with Ethernet frame with VLAN too large",
f: &Frame{
Ethernet: ðernet.Frame{
VLAN: []*ethernet.VLAN{{
ID: ethernet.VLANMax + 1,
}},
},
},
err: ethernet.ErrInvalidVLAN,
},
{
desc: "Frame with VNI 1",
f: &Frame{
VNI: 1,
Ethernet: ðernet.Frame{},
},
b: append([]byte{
0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
}, ethernetFrame(t)...),
},
{
desc: "Frame with VNI Max",
f: &Frame{
VNI: MaxVNI,
Ethernet: ðernet.Frame{},
},
b: append([]byte{
0x08, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0x00,
}, ethernetFrame(t)...),
},
}
for i, tt := range tests {
t.Logf("[%02d] test %q", i, tt.desc)
b, err := tt.f.MarshalBinary()
if err != nil {
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error: %v != %v",
want, got)
}
continue
}
if want, got := tt.b, b; !bytes.Equal(want, got) {
t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v",
want, got)
}
}
}
func TestFrameUnmarshalBinary(t *testing.T) {
var tests = []struct {
desc string
b []byte
f *Frame
err error
}{
{
desc: "Frame too short",
b: []byte{0x00},
err: io.ErrUnexpectedEOF,
},
{
desc: "Frame with I flag not set",
b: append([]byte{
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
}, ethernetFrame(t)...),
err: ErrInvalidFrame,
},
{
desc: "Frame with invalid Ethernet frame check sequence",
b: func() []byte {
b := append([]byte{
0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
}, ethernetFrame(t)...)
// Break FCS
b[len(b)-1] = 0x00
return b
}(),
err: ethernet.ErrInvalidFCS,
},
{
desc: "Frame with VNI 1",
b: append([]byte{
0x08, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x00,
}, ethernetFrame(t)...),
f: &Frame{
VNI: 1,
Ethernet: ðernet.Frame{},
},
},
{
desc: "Frame with VNI Max",
b: append([]byte{
0x08, 0x00, 0x00, 0x00,
0xff, 0xff, 0xff, 0x00,
}, ethernetFrame(t)...),
f: &Frame{
VNI: MaxVNI,
Ethernet: ðernet.Frame{},
},
},
}
for i, tt := range tests {
t.Logf("[%02d] test %q", i, tt.desc)
f := new(Frame)
if err := f.UnmarshalBinary(tt.b); err != nil {
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error: %v != %v",
want, got)
}
continue
}
fb, err := f.MarshalBinary()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if want, got := tt.b, fb; !bytes.Equal(want, got) {
t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v",
want, got)
}
}
}
func ethernetFrame(t *testing.T) []byte {
f := new(ethernet.Frame)
fb, err := f.MarshalFCS()
if err != nil {
t.Fatalf("failed to marshal Ethernet frame: %v", err)
}
return fb
}