-
Notifications
You must be signed in to change notification settings - Fork 837
Expand file tree
/
Copy pathfou_test.go
More file actions
177 lines (143 loc) · 4.42 KB
/
Copy pathfou_test.go
File metadata and controls
177 lines (143 loc) · 4.42 KB
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
//go:build linux
// +build linux
package netlink
import (
"net"
"testing"
)
func TestFouDeserializeMsg(t *testing.T) {
var msg []byte
// deserialize a valid message
msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0, 0, 6, 0, 1, 0, 21, 179, 0, 0, 5, 0, 3, 0, 4, 0, 0, 0, 5, 0, 4, 0, 1, 0, 0, 0}
fou := deserializeFouMsg(msg)
// check if message was deserialized correctly
if fou.Family != FAMILY_V4 {
t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family)
}
if fou.Port != 5555 {
t.Errorf("expected port 5555, got %d", fou.Port)
}
if fou.Protocol != 4 { // ipip
t.Errorf("expected protocol 4, got %d", fou.Protocol)
}
if fou.EncapType != FOU_ENCAP_DIRECT {
t.Errorf("expected encap type %d, got %d", FOU_ENCAP_DIRECT, fou.EncapType)
}
// deserialize a valid message(kernel >= 5.2)
msg = []byte{3, 1, 0, 0, 5, 0, 2, 0, 2, 0, 0, 0, 6, 0, 1, 0, 43, 103, 0, 0, 6, 0, 10, 0, 86, 206, 0, 0, 5, 0, 3, 0, 0, 0, 0, 0, 5, 0, 4, 0, 2, 0, 0, 0, 8, 0, 11, 0, 0, 0, 0, 0, 8, 0, 6, 0, 1, 2, 3, 4, 8, 0, 8, 0, 5, 6, 7, 8}
fou = deserializeFouMsg(msg)
if fou.Family != FAMILY_V4 {
t.Errorf("expected family %d, got %d", FAMILY_V4, fou.Family)
}
if fou.Port != 11111 {
t.Errorf("expected port 5555, got %d", fou.Port)
}
if fou.Protocol != 0 { // gue
t.Errorf("expected protocol 0, got %d", fou.Protocol)
}
if fou.IfIndex != 0 {
t.Errorf("expected ifindex 0, got %d", fou.Protocol)
}
if fou.EncapType != FOU_ENCAP_GUE {
t.Errorf("expected encap type %d, got %d", FOU_ENCAP_GUE, fou.EncapType)
}
if expected := net.IPv4(1, 2, 3, 4); !fou.Local.Equal(expected) {
t.Errorf("expected local %v, got %v", expected, fou.Local)
}
if expected := net.IPv4(5, 6, 7, 8); !fou.Peer.Equal(expected) {
t.Errorf("expected peer %v, got %v", expected, fou.Peer)
}
if fou.PeerPort != 22222 {
t.Errorf("expected peer port 0, got %d", fou.PeerPort)
}
// unknown attribute should be skipped
msg = []byte{3, 1, 0, 0, 5, 0, 112, 0, 2, 0, 0, 0, 5, 0, 2, 0, 2, 0, 0}
fou = deserializeFouMsg(msg)
if fou.Family != 2 {
t.Errorf("expected family 2, got %d", fou.Family)
}
}
func TestFouAddDel(t *testing.T) {
// foo-over-udp was merged in 3.18 so skip these tests if the kernel is too old
minKernelRequired(t, 3, 18)
// the fou module is usually not compiled in the kernel so we'll load it
t.Cleanup(setUpNetlinkTestWithKModule(t, "fou"))
fou := Fou{
Port: 5555,
Family: FAMILY_V4,
Protocol: 4, // ipip
EncapType: FOU_ENCAP_DIRECT,
}
if err := FouAdd(fou); err != nil {
t.Fatal(err)
}
list, err := FouList(FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Fatalf("expected 1 fou, got %d", len(list))
}
if list[0].Port != fou.Port {
t.Errorf("expected port %d, got %d", fou.Port, list[0].Port)
}
if list[0].Family != fou.Family {
t.Errorf("expected family %d, got %d", fou.Family, list[0].Family)
}
if list[0].Protocol != fou.Protocol {
t.Errorf("expected protocol %d, got %d", fou.Protocol, list[0].Protocol)
}
if list[0].EncapType != fou.EncapType {
t.Errorf("expected encaptype %d, got %d", fou.EncapType, list[0].EncapType)
}
if err := FouDel(Fou{Port: fou.Port, Family: fou.Family}); err != nil {
t.Fatal(err)
}
list, err = FouList(FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatalf("expected 0 fou, got %d", len(list))
}
}
func TestFouAddDelGUE(t *testing.T) {
minKernelRequired(t, 3, 18)
t.Cleanup(setUpNetlinkTestWithKModule(t, "fou"))
// GUE with Protocol=0 must succeed on all kernels (including ≥ 6.x which
// rejects FOU_ATTR_IPPROTO when encap type is GUE).
gue := Fou{
Port: 5556,
Family: FAMILY_V4,
EncapType: FOU_ENCAP_GUE,
}
if err := FouAdd(gue); err != nil {
t.Fatalf("FouAdd GUE: %v", err)
}
list, err := FouList(FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(list) != 1 {
t.Fatalf("expected 1 fou entry, got %d", len(list))
}
if list[0].Port != gue.Port {
t.Errorf("expected port %d, got %d", gue.Port, list[0].Port)
}
if list[0].EncapType != FOU_ENCAP_GUE {
t.Errorf("expected encaptype GUE (%d), got %d", FOU_ENCAP_GUE, list[0].EncapType)
}
if list[0].Protocol != 0 {
t.Errorf("expected protocol 0 for GUE, got %d", list[0].Protocol)
}
if err := FouDel(Fou{Port: gue.Port, Family: gue.Family}); err != nil {
t.Fatalf("FouDel GUE: %v", err)
}
list, err = FouList(FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(list) != 0 {
t.Fatalf("expected 0 fou entries after delete, got %d", len(list))
}
}