forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 2
/
addr_test.go
291 lines (242 loc) · 6.52 KB
/
addr_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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// +build linux
package netlink
import (
"net"
"os"
"testing"
"time"
"golang.org/x/sys/unix"
)
func TestAddrAdd(t *testing.T) {
DoTestAddr(t, AddrAdd)
}
func TestAddrReplace(t *testing.T) {
DoTestAddr(t, AddrReplace)
}
func DoTestAddr(t *testing.T, FunctionUndertest func(Link, *Addr) error) {
if os.Getenv("TRAVIS_BUILD_DIR") != "" {
t.Skipf("Fails in travis with: addr_test.go:68: Address flags not set properly, got=0, expected=128")
}
// TODO: IFA_F_PERMANENT does not seem to be set by default on older kernels?
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(32, 32)}
var peer = &net.IPNet{IP: net.IPv4(127, 0, 0, 3), Mask: net.CIDRMask(24, 32)}
var addrTests = []struct {
addr *Addr
expected *Addr
}{
{
&Addr{IPNet: address},
&Addr{IPNet: address, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
},
{
&Addr{IPNet: address, Label: "local"},
&Addr{IPNet: address, Label: "local", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
},
{
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC},
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
},
{
&Addr{IPNet: address, Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED},
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_OPTIMISTIC | unix.IFA_F_DADFAILED | unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_UNIVERSE},
},
{
&Addr{IPNet: address, Scope: unix.RT_SCOPE_NOWHERE},
&Addr{IPNet: address, Label: "lo", Flags: unix.IFA_F_PERMANENT, Scope: unix.RT_SCOPE_NOWHERE},
},
{
&Addr{IPNet: address, Peer: peer},
&Addr{IPNet: address, Peer: peer, Label: "lo", Scope: unix.RT_SCOPE_UNIVERSE, Flags: unix.IFA_F_PERMANENT},
},
}
tearDown := setUpNetlinkTest(t)
defer tearDown()
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
for _, tt := range addrTests {
if err = FunctionUndertest(link, tt.addr); err != nil {
t.Fatal(err)
}
addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
if !addrs[0].Equal(*tt.expected) {
t.Fatalf("Address ip no set properly, got=%s, expected=%s", addrs[0], tt.expected)
}
if addrs[0].Label != tt.expected.Label {
t.Fatalf("Address label not set properly, got=%s, expected=%s", addrs[0].Label, tt.expected.Label)
}
if addrs[0].Flags != tt.expected.Flags {
t.Fatalf("Address flags not set properly, got=%d, expected=%d", addrs[0].Flags, tt.expected.Flags)
}
if addrs[0].Scope != tt.expected.Scope {
t.Fatalf("Address scope not set properly, got=%d, expected=%d", addrs[0].Scope, tt.expected.Scope)
}
if ifindex := link.Attrs().Index; ifindex != addrs[0].LinkIndex {
t.Fatalf("Address ifindex not set properly, got=%d, expected=%d", addrs[0].LinkIndex, ifindex)
}
if tt.expected.Peer != nil {
if !addrs[0].PeerEqual(*tt.expected) {
t.Fatalf("Peer Address ip no set properly, got=%s, expected=%s", addrs[0].Peer, tt.expected.Peer)
}
}
// Pass FAMILY_V4, we should get the same results as FAMILY_ALL
addrs, err = AddrList(link, FAMILY_V4)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
// Pass a wrong family number, we should get nil list
addrs, err = AddrList(link, 0x8)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not expected")
}
if err = AddrDel(link, tt.addr); err != nil {
t.Fatal(err)
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
}
}
func TestAddrAddReplace(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
var addr = &Addr{IPNet: address}
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
err = AddrAdd(link, addr)
if err != nil {
t.Fatal(err)
}
addrs, err := AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
err = AddrAdd(link, addr)
if err == nil {
t.Fatal("Re-adding address should fail (but succeeded unexpectedly).")
}
err = AddrReplace(link, addr)
if err != nil {
t.Fatal("Replacing address failed.")
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 1 {
t.Fatal("Address not added properly")
}
if err = AddrDel(link, addr); err != nil {
t.Fatal(err)
}
addrs, err = AddrList(link, FAMILY_ALL)
if err != nil {
t.Fatal(err)
}
if len(addrs) != 0 {
t.Fatal("Address not removed properly")
}
}
func expectAddrUpdate(ch <-chan AddrUpdate, add bool, dst net.IP) bool {
for {
timeout := time.After(time.Minute)
select {
case update := <-ch:
if update.NewAddr == add && update.LinkAddress.IP.Equal(dst) {
return true
}
case <-timeout:
return false
}
}
}
func TestAddrSubscribeWithOptions(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
ch := make(chan AddrUpdate)
done := make(chan struct{})
defer close(done)
var lastError error
defer func() {
if lastError != nil {
t.Fatalf("Fatal error received during subscription: %v", lastError)
}
}()
if err := AddrSubscribeWithOptions(ch, done, AddrSubscribeOptions{
ErrorCallback: func(err error) {
lastError = err
},
}); err != nil {
t.Fatal(err)
}
// get loopback interface
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
// bring the interface up
if err = LinkSetUp(link); err != nil {
t.Fatal(err)
}
ip := net.IPv4(127, 0, 0, 1)
if !expectAddrUpdate(ch, true, ip) {
t.Fatal("Add update not received as expected")
}
}
func TestAddrSubscribeListExisting(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
ch := make(chan AddrUpdate)
done := make(chan struct{})
defer close(done)
// get loopback interface
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
// bring the interface up
if err = LinkSetUp(link); err != nil {
t.Fatal(err)
}
var lastError error
defer func() {
if lastError != nil {
t.Fatalf("Fatal error received during subscription: %v", lastError)
}
}()
if err := AddrSubscribeWithOptions(ch, done, AddrSubscribeOptions{
ErrorCallback: func(err error) {
lastError = err
},
ListExisting: true,
}); err != nil {
t.Fatal(err)
}
ip := net.IPv4(127, 0, 0, 1)
if !expectAddrUpdate(ch, true, ip) {
t.Fatal("Add update not received as expected")
}
}