-
Notifications
You must be signed in to change notification settings - Fork 38
/
vlan_test.go
166 lines (150 loc) · 2.74 KB
/
vlan_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
package ethernet
import (
"bytes"
"io"
"reflect"
"testing"
)
func TestVLANMarshalBinary(t *testing.T) {
tests := []struct {
desc string
v *VLAN
b []byte
err error
}{
{
desc: "VLAN priority too large",
v: &VLAN{
Priority: 8,
},
err: ErrInvalidVLAN,
},
{
desc: "VLAN ID too large",
v: &VLAN{
ID: 4095,
},
err: ErrInvalidVLAN,
},
{
desc: "empty VLAN",
v: &VLAN{},
b: []byte{0x00, 0x00},
},
{
desc: "VLAN: PRI 1, ID 101",
v: &VLAN{
Priority: 1,
ID: 101,
},
b: []byte{0x20, 0x65},
},
{
desc: "VLANs: PRI 0, DROP, ID 100",
v: &VLAN{
DropEligible: true,
ID: 100,
},
b: []byte{0x10, 0x64},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
b, err := tt.v.MarshalBinary()
if err != nil {
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error: %v != %v", want, got)
}
return
}
if want, got := tt.b, b; !bytes.Equal(want, got) {
t.Fatalf("unexpected VLAN bytes:\n- want: %v\n- got: %v", want, got)
}
})
}
}
func TestVLANUnmarshalBinary(t *testing.T) {
tests := []struct {
desc string
b []byte
v *VLAN
err error
}{
{
desc: "nil buffer",
err: io.ErrUnexpectedEOF,
},
{
desc: "short buffer",
b: []byte{0},
err: io.ErrUnexpectedEOF,
},
{
desc: "VLAN ID too large",
b: []byte{0xff, 0xff},
err: ErrInvalidVLAN,
},
{
desc: "VLAN: PRI 1, ID 101",
b: []byte{0x20, 0x65},
v: &VLAN{
Priority: 1,
ID: 101,
},
},
{
desc: "VLAN: PRI 0, DROP, ID 100",
b: []byte{0x10, 0x64},
v: &VLAN{
DropEligible: true,
ID: 100,
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
v := new(VLAN)
if err := v.UnmarshalBinary(tt.b); err != nil {
if want, got := tt.err, err; want != got {
t.Fatalf("unexpected error: %v != %v", want, got)
}
return
}
if want, got := tt.v, v; !reflect.DeepEqual(want, got) {
t.Fatalf("unexpected VLAN:\n- want: %v\n- got: %v", want, got)
}
})
}
}
// Benchmarks for VLAN.MarshalBinary
func BenchmarkVLANMarshalBinary(b *testing.B) {
v := &VLAN{
Priority: PriorityBackground,
ID: 10,
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if _, err := v.MarshalBinary(); err != nil {
b.Fatal(err)
}
}
}
// Benchmarks for VLAN.UnmarshalBinary
func BenchmarkVLANUnmarshalBinary(b *testing.B) {
v := &VLAN{
Priority: PriorityBestEffort,
ID: 20,
}
vb, err := v.MarshalBinary()
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := v.UnmarshalBinary(vb); err != nil {
b.Fatal(err)
}
}
}