Debian Codegraph @

remote_list_test.go

raw
1 package nebula
2
3 import (
4 "encoding/binary"
5 "net/netip"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9 "github.com/stretchr/testify/require"
10 )
11
12 // trackedHostnameResults builds a *hostnamesResults with a known cancel function and a
13 // pre-populated ips map so tests can assert cancellation and verify previously-resolved
14 // IPs survive a cancel without spinning up a real DNS resolver.
15 func trackedHostnameResults(cancelFn func(), addrs ...string) *hostnamesResults {
16 hr := &hostnamesResults{cancelFn: cancelFn}
17 ips := map[netip.AddrPort]struct{}{}
18 for _, a := range addrs {
19 ips[netip.MustParseAddrPort(a)] = struct{}{}
20 }
21 hr.ips.Store(&ips)
22 return hr
23 }
24
25 func TestRemoteList_Rebuild(t *testing.T) {
26 rl := NewRemoteList([]netip.Addr{netip.MustParseAddr("0.0.0.0")}, nil)
27 rl.unlockedSetV4(
28 netip.MustParseAddr("0.0.0.0"),
29 netip.MustParseAddr("0.0.0.0"),
30 []*V4AddrPort{
31 newIp4AndPortFromString("70.199.182.92:1475"), // this is duped
32 newIp4AndPortFromString("172.17.0.182:10101"),
33 newIp4AndPortFromString("172.17.1.1:10101"), // this is duped
34 newIp4AndPortFromString("172.18.0.1:10101"), // this is duped
35 newIp4AndPortFromString("172.18.0.1:10101"), // this is a dupe
36 newIp4AndPortFromString("172.19.0.1:10101"),
37 newIp4AndPortFromString("172.31.0.1:10101"),
38 newIp4AndPortFromString("172.17.1.1:10101"), // this is a dupe
39 newIp4AndPortFromString("70.199.182.92:1476"), // almost dupe of 0 with a diff port
40 newIp4AndPortFromString("70.199.182.92:1475"), // this is a dupe
41 },
42 func(netip.Addr, *V4AddrPort) bool { return true },
43 )
44
45 rl.unlockedSetV6(
46 netip.MustParseAddr("0.0.0.1"),
47 netip.MustParseAddr("0.0.0.1"),
48 []*V6AddrPort{
49 newIp6AndPortFromString("[1::1]:1"), // this is duped
50 newIp6AndPortFromString("[1::1]:2"), // almost dupe of 0 with a diff port, also gets duped
51 newIp6AndPortFromString("[1:100::1]:1"),
52 newIp6AndPortFromString("[1::1]:1"), // this is a dupe
53 newIp6AndPortFromString("[1::1]:2"), // this is a dupe
54 },
55 func(netip.Addr, *V6AddrPort) bool { return true },
56 )
57
58 rl.unlockedSetRelay(
59 netip.MustParseAddr("0.0.0.1"),
60 []netip.Addr{
61 netip.MustParseAddr("1::1"),
62 netip.MustParseAddr("1.2.3.4"),
63 netip.MustParseAddr("1.2.3.4"),
64 netip.MustParseAddr("1::1"),
65 },
66 )
67
68 rl.Rebuild([]netip.Prefix{})
69 assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
70
71 // ipv6 first, sorted lexically within
72 assert.Equal(t, "[1::1]:1", rl.addrs[0].String())
73 assert.Equal(t, "[1::1]:2", rl.addrs[1].String())
74 assert.Equal(t, "[1:100::1]:1", rl.addrs[2].String())
75
76 // ipv4 last, sorted by public first, then private, lexically within them
77 assert.Equal(t, "70.199.182.92:1475", rl.addrs[3].String())
78 assert.Equal(t, "70.199.182.92:1476", rl.addrs[4].String())
79 assert.Equal(t, "172.17.0.182:10101", rl.addrs[5].String())
80 assert.Equal(t, "172.17.1.1:10101", rl.addrs[6].String())
81 assert.Equal(t, "172.18.0.1:10101", rl.addrs[7].String())
82 assert.Equal(t, "172.19.0.1:10101", rl.addrs[8].String())
83 assert.Equal(t, "172.31.0.1:10101", rl.addrs[9].String())
84
85 // Now ensure we can hoist ipv4 up
86 rl.Rebuild([]netip.Prefix{netip.MustParsePrefix("0.0.0.0/0")})
87 assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
88
89 // ipv4 first, public then private, lexically within them
90 assert.Equal(t, "70.199.182.92:1475", rl.addrs[0].String())
91 assert.Equal(t, "70.199.182.92:1476", rl.addrs[1].String())
92 assert.Equal(t, "172.17.0.182:10101", rl.addrs[2].String())
93 assert.Equal(t, "172.17.1.1:10101", rl.addrs[3].String())
94 assert.Equal(t, "172.18.0.1:10101", rl.addrs[4].String())
95 assert.Equal(t, "172.19.0.1:10101", rl.addrs[5].String())
96 assert.Equal(t, "172.31.0.1:10101", rl.addrs[6].String())
97
98 // ipv6 last, sorted by public first, then private, lexically within them
99 assert.Equal(t, "[1::1]:1", rl.addrs[7].String())
100 assert.Equal(t, "[1::1]:2", rl.addrs[8].String())
101 assert.Equal(t, "[1:100::1]:1", rl.addrs[9].String())
102
103 // assert relay deduplicated
104 assert.Len(t, rl.relays, 2)
105 assert.Equal(t, "1.2.3.4", rl.relays[0].String())
106 assert.Equal(t, "1::1", rl.relays[1].String())
107
108 // Ensure we can hoist a specific ipv4 range over anything else
109 rl.Rebuild([]netip.Prefix{netip.MustParsePrefix("172.17.0.0/16")})
110 assert.Len(t, rl.addrs, 10, "addrs contains too many entries")
111
112 // Preferred ipv4 first
113 assert.Equal(t, "172.17.0.182:10101", rl.addrs[0].String())
114 assert.Equal(t, "172.17.1.1:10101", rl.addrs[1].String())
115
116 // ipv6 next
117 assert.Equal(t, "[1::1]:1", rl.addrs[2].String())
118 assert.Equal(t, "[1::1]:2", rl.addrs[3].String())
119 assert.Equal(t, "[1:100::1]:1", rl.addrs[4].String())
120
121 // the remaining ipv4 last
122 assert.Equal(t, "70.199.182.92:1475", rl.addrs[5].String())
123 assert.Equal(t, "70.199.182.92:1476", rl.addrs[6].String())
124 assert.Equal(t, "172.18.0.1:10101", rl.addrs[7].String())
125 assert.Equal(t, "172.19.0.1:10101", rl.addrs[8].String())
126 assert.Equal(t, "172.31.0.1:10101", rl.addrs[9].String())
127 }
128
129 func TestRemoteList_ResetForOwner(t *testing.T) {
130 ourselves := netip.MustParseAddr("10.0.0.1")
131 otherOwner := netip.MustParseAddr("10.0.0.2")
132 vpnAddr := netip.MustParseAddr("10.0.0.99")
133
134 rl := NewRemoteList([]netip.Addr{vpnAddr}, nil)
135 rl.unlockedSetV4(ourselves, vpnAddr,
136 []*V4AddrPort{newIp4AndPortFromString("1.1.1.1:4242")},
137 func(netip.Addr, *V4AddrPort) bool { return true },
138 )
139 rl.unlockedSetV6(ourselves, vpnAddr,
140 []*V6AddrPort{newIp6AndPortFromString("[1::1]:4242")},
141 func(netip.Addr, *V6AddrPort) bool { return true },
142 )
143 rl.unlockedSetV4(otherOwner, vpnAddr,
144 []*V4AddrPort{newIp4AndPortFromString("2.2.2.2:4242")},
145 func(netip.Addr, *V4AddrPort) bool { return true },
146 )
147
148 canceled := 0
149 hr := trackedHostnameResults(func() { canceled++ }, "3.3.3.3:4242")
150 rl.Lock()
151 rl.unlockedSetHostnamesResults(hr)
152 rl.Unlock()
153
154 rl.ResetForOwner(ourselves)
155
156 rl.RLock()
157 defer rl.RUnlock()
158 assert.Empty(t, rl.cache[ourselves].v4.reported, "our v4 reported should be cleared")
159 assert.Empty(t, rl.cache[ourselves].v6.reported, "our v6 reported should be cleared")
160 assert.Len(t, rl.cache[otherOwner].v4.reported, 1, "other owner's contribution must be preserved")
161 assert.Equal(t, "2.2.2.2:4242", protoV4AddrPortToNetAddrPort(rl.cache[otherOwner].v4.reported[0]).String())
162 assert.Equal(t, 1, canceled, "DNS resolution goroutine should be canceled")
163 assert.Same(t, hr, rl.hr, "hostnamesResults must be preserved so DNS-resolved IPs keep feeding addrs until replaced")
164 assert.NotEmpty(t, rl.hr.GetAddrs(), "previously-resolved IPs should still be readable after cancel")
165 assert.True(t, rl.shouldRebuild, "shouldRebuild must be set so the next Rebuild recomputes addrs")
166 }
167
168 func TestRemoteList_ResetForOwner_NoEntry(t *testing.T) {
169 // An owner with no cache entry must not panic; shouldRebuild is still set and any
170 // existing hostnamesResults is canceled.
171 rl := NewRemoteList([]netip.Addr{netip.MustParseAddr("10.0.0.99")}, nil)
172 canceled := 0
173 rl.Lock()
174 rl.unlockedSetHostnamesResults(trackedHostnameResults(func() { canceled++ }, "3.3.3.3:4242"))
175 rl.Unlock()
176
177 rl.ResetForOwner(netip.MustParseAddr("10.0.0.1"))
178
179 rl.RLock()
180 defer rl.RUnlock()
181 assert.Equal(t, 1, canceled)
182 assert.True(t, rl.shouldRebuild)
183 }
184
185 func TestRemoteList_ClearHostnameResults(t *testing.T) {
186 rl := NewRemoteList([]netip.Addr{netip.MustParseAddr("10.0.0.99")}, nil)
187
188 canceled := 0
189 hr := trackedHostnameResults(func() { canceled++ }, "3.3.3.3:4242")
190 rl.Lock()
191 rl.unlockedSetHostnamesResults(hr)
192 rl.Unlock()
193 require.NotEmpty(t, hr.GetAddrs(), "hostnamesResults should have its fastrack IPs populated")
194
195 rl.ClearHostnameResults()
196
197 rl.RLock()
198 defer rl.RUnlock()
199 assert.Equal(t, 1, canceled, "DNS resolution goroutine should be canceled")
200 assert.Nil(t, rl.hr, "hostnamesResults should be dropped")
201 assert.True(t, rl.shouldRebuild)
202 }
203
204 func BenchmarkFullRebuild(b *testing.B) {
205 rl := NewRemoteList([]netip.Addr{netip.MustParseAddr("0.0.0.0")}, nil)
206 rl.unlockedSetV4(
207 netip.MustParseAddr("0.0.0.0"),
208 netip.MustParseAddr("0.0.0.0"),
209 []*V4AddrPort{
210 newIp4AndPortFromString("70.199.182.92:1475"),
211 newIp4AndPortFromString("172.17.0.182:10101"),
212 newIp4AndPortFromString("172.17.1.1:10101"),
213 newIp4AndPortFromString("172.18.0.1:10101"),
214 newIp4AndPortFromString("172.19.0.1:10101"),
215 newIp4AndPortFromString("172.31.0.1:10101"),
216 newIp4AndPortFromString("172.17.1.1:10101"), // this is a dupe
217 newIp4AndPortFromString("70.199.182.92:1476"), // dupe of 0 with a diff port
218 },
219 func(netip.Addr, *V4AddrPort) bool { return true },
220 )
221
222 rl.unlockedSetV6(
223 netip.MustParseAddr("0.0.0.0"),
224 netip.MustParseAddr("0.0.0.0"),
225 []*V6AddrPort{
226 newIp6AndPortFromString("[1::1]:1"),
227 newIp6AndPortFromString("[1::1]:2"), // dupe of 0 with a diff port
228 newIp6AndPortFromString("[1:100::1]:1"),
229 newIp6AndPortFromString("[1::1]:1"), // this is a dupe
230 },
231 func(netip.Addr, *V6AddrPort) bool { return true },
232 )
233
234 b.Run("no preferred", func(b *testing.B) {
235 for i := 0; i < b.N; i++ {
236 rl.shouldRebuild = true
237 rl.Rebuild([]netip.Prefix{})
238 }
239 })
240
241 ipNet1 := netip.MustParsePrefix("172.17.0.0/16")
242 b.Run("1 preferred", func(b *testing.B) {
243 for i := 0; i < b.N; i++ {
244 rl.shouldRebuild = true
245 rl.Rebuild([]netip.Prefix{ipNet1})
246 }
247 })
248
249 ipNet2 := netip.MustParsePrefix("70.0.0.0/8")
250 b.Run("2 preferred", func(b *testing.B) {
251 for i := 0; i < b.N; i++ {
252 rl.shouldRebuild = true
253 rl.Rebuild([]netip.Prefix{ipNet2})
254 }
255 })
256
257 ipNet3 := netip.MustParsePrefix("0.0.0.0/0")
258 b.Run("3 preferred", func(b *testing.B) {
259 for i := 0; i < b.N; i++ {
260 rl.shouldRebuild = true
261 rl.Rebuild([]netip.Prefix{ipNet1, ipNet2, ipNet3})
262 }
263 })
264 }
265
266 func BenchmarkSortRebuild(b *testing.B) {
267 rl := NewRemoteList([]netip.Addr{netip.MustParseAddr("0.0.0.0")}, nil)
268 rl.unlockedSetV4(
269 netip.MustParseAddr("0.0.0.0"),
270 netip.MustParseAddr("0.0.0.0"),
271 []*V4AddrPort{
272 newIp4AndPortFromString("70.199.182.92:1475"),
273 newIp4AndPortFromString("172.17.0.182:10101"),
274 newIp4AndPortFromString("172.17.1.1:10101"),
275 newIp4AndPortFromString("172.18.0.1:10101"),
276 newIp4AndPortFromString("172.19.0.1:10101"),
277 newIp4AndPortFromString("172.31.0.1:10101"),
278 newIp4AndPortFromString("172.17.1.1:10101"), // this is a dupe
279 newIp4AndPortFromString("70.199.182.92:1476"), // dupe of 0 with a diff port
280 },
281 func(netip.Addr, *V4AddrPort) bool { return true },
282 )
283
284 rl.unlockedSetV6(
285 netip.MustParseAddr("0.0.0.0"),
286 netip.MustParseAddr("0.0.0.0"),
287 []*V6AddrPort{
288 newIp6AndPortFromString("[1::1]:1"),
289 newIp6AndPortFromString("[1::1]:2"), // dupe of 0 with a diff port
290 newIp6AndPortFromString("[1:100::1]:1"),
291 newIp6AndPortFromString("[1::1]:1"), // this is a dupe
292 },
293 func(netip.Addr, *V6AddrPort) bool { return true },
294 )
295
296 b.Run("no preferred", func(b *testing.B) {
297 for i := 0; i < b.N; i++ {
298 rl.shouldRebuild = true
299 rl.Rebuild([]netip.Prefix{})
300 }
301 })
302
303 ipNet1 := netip.MustParsePrefix("172.17.0.0/16")
304 rl.Rebuild([]netip.Prefix{ipNet1})
305
306 b.Run("1 preferred", func(b *testing.B) {
307 for i := 0; i < b.N; i++ {
308 rl.Rebuild([]netip.Prefix{ipNet1})
309 }
310 })
311
312 ipNet2 := netip.MustParsePrefix("70.0.0.0/8")
313 rl.Rebuild([]netip.Prefix{ipNet1, ipNet2})
314
315 b.Run("2 preferred", func(b *testing.B) {
316 for i := 0; i < b.N; i++ {
317 rl.Rebuild([]netip.Prefix{ipNet1, ipNet2})
318 }
319 })
320
321 ipNet3 := netip.MustParsePrefix("0.0.0.0/0")
322 rl.Rebuild([]netip.Prefix{ipNet1, ipNet2, ipNet3})
323
324 b.Run("3 preferred", func(b *testing.B) {
325 for i := 0; i < b.N; i++ {
326 rl.Rebuild([]netip.Prefix{ipNet1, ipNet2, ipNet3})
327 }
328 })
329 }
330
331 func newIp4AndPortFromString(s string) *V4AddrPort {
332 a := netip.MustParseAddrPort(s)
333 v4Addr := a.Addr().As4()
334 return &V4AddrPort{
335 Addr: binary.BigEndian.Uint32(v4Addr[:]),
336 Port: uint32(a.Port()),
337 }
338 }
339
340 func newIp6AndPortFromString(s string) *V6AddrPort {
341 a := netip.MustParseAddrPort(s)
342 v6Addr := a.Addr().As16()
343 return &V6AddrPort{
344 Hi: binary.BigEndian.Uint64(v6Addr[:8]),
345 Lo: binary.BigEndian.Uint64(v6Addr[8:]),
346 Port: uint32(a.Port()),
347 }
348 }