| 1 |
package nebula |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/netip" |
| 5 |
"slices" |
| 6 |
"testing" |
| 7 |
|
| 8 |
"github.com/slackhq/nebula/config" |
| 9 |
"github.com/slackhq/nebula/test" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
// chainIds returns the localIndexIds of the hostinfos holding addr, primary (index 0) first. It |
| 15 |
// also validates the Hosts/moreHosts sync contract on every call so a mutation that broke it |
| 16 |
// fails fast. |
| 17 |
func chainIds(t *testing.T, hm *HostMap, addr netip.Addr) []uint32 { |
| 18 |
t.Helper() |
| 19 |
assertHostMapInvariants(t, hm) |
| 20 |
list := hm.unlockedGetHostList(addr) |
| 21 |
ids := make([]uint32, len(list)) |
| 22 |
for i, h := range list { |
| 23 |
ids[i] = h.localIndexId |
| 24 |
} |
| 25 |
return ids |
| 26 |
} |
| 27 |
|
| 28 |
// assertHostMapInvariants checks the Hosts/moreHosts contract: moreHosts only holds addresses |
| 29 |
// with 2 or more hostinfos, its first entry is always the primary in Hosts, lists never hold |
| 30 |
// duplicates, every hostinfo in a list owns the address and is registered in Indexes, and every |
| 31 |
// indexed hostinfo is reachable through each of its addresses. |
| 32 |
func assertHostMapInvariants(t *testing.T, hm *HostMap) { |
| 33 |
t.Helper() |
| 34 |
for addr, list := range hm.moreHosts { |
| 35 |
require.GreaterOrEqualf(t, len(list), 2, "moreHosts[%s] must hold at least 2 hostinfos", addr) |
| 36 |
require.Samef(t, hm.Hosts[addr], list[0], "moreHosts[%s][0] must match the primary in Hosts", addr) |
| 37 |
seen := map[*HostInfo]bool{} |
| 38 |
for _, h := range list { |
| 39 |
require.NotNilf(t, h, "moreHosts[%s] must never hold a nil hostinfo", addr) |
| 40 |
require.Falsef(t, seen[h], "moreHosts[%s] holds hostinfo %d twice", addr, h.localIndexId) |
| 41 |
seen[h] = true |
| 42 |
require.Samef(t, hm.Indexes[h.localIndexId], h, "moreHosts[%s] member %d is not registered in Indexes", addr, h.localIndexId) |
| 43 |
require.Truef(t, slices.Contains(h.vpnAddrs, addr), "moreHosts[%s] member %d does not own the address", addr, h.localIndexId) |
| 44 |
} |
| 45 |
} |
| 46 |
for addr, h := range hm.Hosts { |
| 47 |
require.NotNilf(t, h, "Hosts[%s] must never be nil", addr) |
| 48 |
require.Samef(t, hm.Indexes[h.localIndexId], h, "Hosts[%s] primary %d is not registered in Indexes", addr, h.localIndexId) |
| 49 |
require.Truef(t, slices.Contains(h.vpnAddrs, addr), "Hosts[%s] primary (index %d) does not own the address", addr, h.localIndexId) |
| 50 |
} |
| 51 |
for idx, h := range hm.Indexes { |
| 52 |
require.Equalf(t, idx, h.localIndexId, "Indexes[%d] holds hostinfo with localIndexId %d", idx, h.localIndexId) |
| 53 |
for _, va := range h.vpnAddrs { |
| 54 |
require.Truef(t, slices.Contains(hm.unlockedGetHostList(va), h), "indexed hostinfo %d is missing from the list for %s", idx, va) |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
func TestHostMap_MakePrimary(t *testing.T) { |
| 60 |
l := test.NewLogger() |
| 61 |
hm := newHostMap(l) |
| 62 |
|
| 63 |
f := &Interface{} |
| 64 |
a := netip.MustParseAddr("0.0.0.1") |
| 65 |
|
| 66 |
h1 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 1} |
| 67 |
h2 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 2} |
| 68 |
h3 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 3} |
| 69 |
h4 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 4} |
| 70 |
|
| 71 |
hm.unlockedAddHostInfo(h4, f) |
| 72 |
hm.unlockedAddHostInfo(h3, f) |
| 73 |
hm.unlockedAddHostInfo(h2, f) |
| 74 |
hm.unlockedAddHostInfo(h1, f) |
| 75 |
|
| 76 |
// Most-recently-added is primary: h1, h2, h3, h4 |
| 77 |
assert.Equal(t, []uint32{1, 2, 3, 4}, chainIds(t, hm, a)) |
| 78 |
assert.Equal(t, h1, hm.QueryVpnAddr(a)) |
| 79 |
|
| 80 |
// Swap the middle to primary: h3, h1, h2, h4 |
| 81 |
hm.MakePrimary(h3) |
| 82 |
assert.Equal(t, []uint32{3, 1, 2, 4}, chainIds(t, hm, a)) |
| 83 |
assert.Equal(t, h3, hm.QueryVpnAddr(a)) |
| 84 |
|
| 85 |
// Swap the tail to primary: h4, h3, h1, h2 |
| 86 |
hm.MakePrimary(h4) |
| 87 |
assert.Equal(t, []uint32{4, 3, 1, 2}, chainIds(t, hm, a)) |
| 88 |
|
| 89 |
// Swapping the current primary again is a no-op |
| 90 |
hm.MakePrimary(h4) |
| 91 |
assert.Equal(t, []uint32{4, 3, 1, 2}, chainIds(t, hm, a)) |
| 92 |
} |
| 93 |
|
| 94 |
func TestHostMap_DeleteHostInfo(t *testing.T) { |
| 95 |
l := test.NewLogger() |
| 96 |
hm := newHostMap(l) |
| 97 |
|
| 98 |
f := &Interface{} |
| 99 |
a := netip.MustParseAddr("0.0.0.1") |
| 100 |
|
| 101 |
h1 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 1} |
| 102 |
h2 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 2} |
| 103 |
h3 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 3} |
| 104 |
h4 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 4} |
| 105 |
h5 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 5} |
| 106 |
h6 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 6} |
| 107 |
|
| 108 |
hm.unlockedAddHostInfo(h6, f) |
| 109 |
hm.unlockedAddHostInfo(h5, f) |
| 110 |
hm.unlockedAddHostInfo(h4, f) |
| 111 |
hm.unlockedAddHostInfo(h3, f) |
| 112 |
hm.unlockedAddHostInfo(h2, f) |
| 113 |
hm.unlockedAddHostInfo(h1, f) |
| 114 |
|
| 115 |
// h6 is evicted by the MaxHostInfosPerVpnIp cap; the rest are newest-first. |
| 116 |
assert.Nil(t, hm.QueryIndex(h6.localIndexId)) |
| 117 |
assert.Equal(t, []uint32{1, 2, 3, 4, 5}, chainIds(t, hm, a)) |
| 118 |
|
| 119 |
// Delete primary; not final since siblings remain. |
| 120 |
assert.False(t, hm.DeleteHostInfo(h1)) |
| 121 |
assert.Equal(t, []uint32{2, 3, 4, 5}, chainIds(t, hm, a)) |
| 122 |
|
| 123 |
// Deleting the same hostinfo again must not report final while siblings remain and must not |
| 124 |
// disturb the list. The old chain code got this wrong: the first delete nil'd next/prev, so a |
| 125 |
// second delete looked final and wiped lighthouse state out from under the live sibling. |
| 126 |
assert.False(t, hm.DeleteHostInfo(h1)) |
| 127 |
assert.Equal(t, []uint32{2, 3, 4, 5}, chainIds(t, hm, a)) |
| 128 |
|
| 129 |
// Delete a middle node. |
| 130 |
assert.False(t, hm.DeleteHostInfo(h3)) |
| 131 |
assert.Equal(t, []uint32{2, 4, 5}, chainIds(t, hm, a)) |
| 132 |
|
| 133 |
// Delete the tail. |
| 134 |
assert.False(t, hm.DeleteHostInfo(h5)) |
| 135 |
assert.Equal(t, []uint32{2, 4}, chainIds(t, hm, a)) |
| 136 |
|
| 137 |
// Delete the head; h4 remains and becomes primary. |
| 138 |
assert.False(t, hm.DeleteHostInfo(h2)) |
| 139 |
assert.Equal(t, []uint32{4}, chainIds(t, hm, a)) |
| 140 |
assert.Equal(t, h4, hm.QueryVpnAddr(a)) |
| 141 |
|
| 142 |
// Delete the only remaining item; final is true and the address is gone. |
| 143 |
assert.True(t, hm.DeleteHostInfo(h4)) |
| 144 |
assert.Empty(t, chainIds(t, hm, a)) |
| 145 |
assert.Nil(t, hm.QueryVpnAddr(a)) |
| 146 |
|
| 147 |
// Deleting an already-gone hostinfo is still final; nothing holds the address anymore. |
| 148 |
assert.True(t, hm.DeleteHostInfo(h4)) |
| 149 |
assert.Empty(t, chainIds(t, hm, a)) |
| 150 |
} |
| 151 |
|
| 152 |
// TestHostMap_MakePrimary_DeletedHostInfo covers promoting a hostinfo that lost a race with |
| 153 |
// tunnel teardown: swapPrimary and AddRelay decide to promote while holding a stale pointer and |
| 154 |
// only take the write lock after a delete fully unlinked the hostinfo. MakePrimary must be a |
| 155 |
// no-op, not a resurrection that installs an unmanaged primary. |
| 156 |
func TestHostMap_MakePrimary_DeletedHostInfo(t *testing.T) { |
| 157 |
l := test.NewLogger() |
| 158 |
hm := newHostMap(l) |
| 159 |
f := &Interface{} |
| 160 |
a := netip.MustParseAddr("0.0.0.1") |
| 161 |
|
| 162 |
h1 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 1} |
| 163 |
h2 := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 2} |
| 164 |
hm.unlockedAddHostInfo(h1, f) |
| 165 |
hm.unlockedAddHostInfo(h2, f) |
| 166 |
|
| 167 |
// h1 is fully deleted while another goroutine still holds a pointer to it. |
| 168 |
assert.False(t, hm.DeleteHostInfo(h1)) |
| 169 |
assert.Equal(t, []uint32{2}, chainIds(t, hm, a)) |
| 170 |
|
| 171 |
// The stale promote must not bring it back. |
| 172 |
hm.MakePrimary(h1) |
| 173 |
assert.Equal(t, []uint32{2}, chainIds(t, hm, a)) |
| 174 |
assert.Equal(t, h2, hm.QueryVpnAddr(a)) |
| 175 |
assert.Nil(t, hm.QueryIndex(h1.localIndexId)) |
| 176 |
} |
| 177 |
|
| 178 |
// TestHostMap_QueryVpnAddrsRelayFor_NonPrimary makes sure a relay established on an older |
| 179 |
// hostinfo is still found after a newer tunnel without relay state takes primary for the same |
| 180 |
// address. The lookup checks the primary first and falls back to the rest of the list. |
| 181 |
func TestHostMap_QueryVpnAddrsRelayFor_NonPrimary(t *testing.T) { |
| 182 |
l := test.NewLogger() |
| 183 |
hm := newHostMap(l) |
| 184 |
f := &Interface{} |
| 185 |
relayAddr := netip.MustParseAddr("0.0.0.9") |
| 186 |
target := netip.MustParseAddr("0.0.0.1") |
| 187 |
|
| 188 |
older := &HostInfo{ |
| 189 |
vpnAddrs: []netip.Addr{relayAddr}, |
| 190 |
localIndexId: 1, |
| 191 |
relayState: RelayState{ |
| 192 |
relayForByAddr: map[netip.Addr]*Relay{}, |
| 193 |
relayForByIdx: map[uint32]*Relay{}, |
| 194 |
}, |
| 195 |
} |
| 196 |
older.relayState.InsertRelay(target, 100, &Relay{Type: ForwardingType, State: Established, LocalIndex: 100, PeerAddr: target}) |
| 197 |
hm.unlockedAddHostInfo(older, f) |
| 198 |
|
| 199 |
// The relay is found on the primary. |
| 200 |
h, r, err := hm.QueryVpnAddrsRelayFor([]netip.Addr{target}, relayAddr) |
| 201 |
require.NoError(t, err) |
| 202 |
assert.Equal(t, older, h) |
| 203 |
assert.Equal(t, uint32(100), r.LocalIndex) |
| 204 |
|
| 205 |
// A re-handshake with no relay state takes primary; the established relay on the older |
| 206 |
// hostinfo must still be found through the fallback. |
| 207 |
newer := &HostInfo{vpnAddrs: []netip.Addr{relayAddr}, localIndexId: 2} |
| 208 |
hm.unlockedAddHostInfo(newer, f) |
| 209 |
assert.Equal(t, []uint32{2, 1}, chainIds(t, hm, relayAddr)) |
| 210 |
|
| 211 |
h, r, err = hm.QueryVpnAddrsRelayFor([]netip.Addr{target}, relayAddr) |
| 212 |
require.NoError(t, err) |
| 213 |
assert.Equal(t, older, h) |
| 214 |
assert.Equal(t, uint32(100), r.LocalIndex) |
| 215 |
|
| 216 |
// No hostinfo at all is a plain miss. |
| 217 |
_, _, err = hm.QueryVpnAddrsRelayFor([]netip.Addr{target}, netip.MustParseAddr("0.0.0.42")) |
| 218 |
require.Error(t, err) |
| 219 |
} |
| 220 |
|
| 221 |
// TestHostMap_DeleteHostInfo_MultipleVpnAddrs exercises the case where a hostinfo carries more than one |
| 222 |
// vpnAddr and shares its next/prev chain with a live sibling. Deleting the head must not corrupt the |
| 223 |
// sibling: every address the sibling owns has to keep pointing at it. The pre-fix code unlinked the shared |
| 224 |
// chain once per vpnAddr, so on the first address it nil'd next/prev, and on the second address the node |
| 225 |
// looked already-detached: it dropped the map entry instead of promoting the sibling (and tripped the |
| 226 |
// isLastHostinfo relay teardown). See unlockedDeleteHostInfo. |
| 227 |
func TestHostMap_DeleteHostInfo_MultipleVpnAddrs(t *testing.T) { |
| 228 |
l := test.NewLogger() |
| 229 |
hm := newHostMap(l) |
| 230 |
|
| 231 |
f := &Interface{} |
| 232 |
|
| 233 |
a := netip.MustParseAddr("0.0.0.1") |
| 234 |
b := netip.MustParseAddr("0.0.0.2") |
| 235 |
|
| 236 |
// Two tunnels for the same peer, each reachable at both a and b. |
| 237 |
other := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 1} |
| 238 |
head := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 2} |
| 239 |
|
| 240 |
hm.unlockedAddHostInfo(other, f) |
| 241 |
hm.unlockedAddHostInfo(head, f) |
| 242 |
|
| 243 |
// head is primary for both addresses, other is next in each address's list. |
| 244 |
assert.Equal(t, head, hm.QueryVpnAddr(a)) |
| 245 |
assert.Equal(t, head, hm.QueryVpnAddr(b)) |
| 246 |
assert.Equal(t, []uint32{2, 1}, chainIds(t, hm, a)) |
| 247 |
assert.Equal(t, []uint32{2, 1}, chainIds(t, hm, b)) |
| 248 |
|
| 249 |
// Delete the head. other is still live, so it must become primary for BOTH addresses. |
| 250 |
assert.False(t, hm.DeleteHostInfo(head)) |
| 251 |
assert.Equal(t, other, hm.QueryVpnAddr(a)) |
| 252 |
assert.Equal(t, other, hm.QueryVpnAddr(b)) |
| 253 |
assert.Equal(t, []uint32{1}, chainIds(t, hm, a)) |
| 254 |
assert.Equal(t, []uint32{1}, chainIds(t, hm, b)) |
| 255 |
|
| 256 |
// head is fully removed from the index map. |
| 257 |
assert.Nil(t, hm.QueryIndex(head.localIndexId)) |
| 258 |
} |
| 259 |
|
| 260 |
// TestHostMap_DeleteHostInfo_DivergentVpnAddrs covers chained hostinfos for the same peer whose |
| 261 |
// vpnAddrs sets differ (a re-handshake cert added a second address). Deleting the superset node |
| 262 |
// must not promote a sibling to an address it does not own. |
| 263 |
func TestHostMap_DeleteHostInfo_DivergentVpnAddrs(t *testing.T) { |
| 264 |
l := test.NewLogger() |
| 265 |
hm := newHostMap(l) |
| 266 |
f := &Interface{} |
| 267 |
a := netip.MustParseAddr("0.0.0.1") |
| 268 |
b := netip.MustParseAddr("0.0.0.2") |
| 269 |
|
| 270 |
// sub owns only a; super (a newer handshake) owns a and b. |
| 271 |
sub := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 1} |
| 272 |
super := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 2} |
| 273 |
hm.unlockedAddHostInfo(sub, f) |
| 274 |
hm.unlockedAddHostInfo(super, f) |
| 275 |
|
| 276 |
assert.Equal(t, []uint32{2, 1}, chainIds(t, hm, a)) |
| 277 |
assert.Equal(t, []uint32{2}, chainIds(t, hm, b)) |
| 278 |
|
| 279 |
// Delete super: a promotes to sub (which owns it); b has no remaining owner and must be |
| 280 |
// removed, not dangled at sub (which does not own b). |
| 281 |
assert.False(t, hm.DeleteHostInfo(super)) |
| 282 |
assert.Equal(t, []uint32{1}, chainIds(t, hm, a)) |
| 283 |
assert.Empty(t, chainIds(t, hm, b)) |
| 284 |
assert.Equal(t, sub, hm.QueryVpnAddr(a)) |
| 285 |
assert.Nil(t, hm.QueryVpnAddr(b)) |
| 286 |
assert.Nil(t, hm.QueryIndex(super.localIndexId)) |
| 287 |
|
| 288 |
// Deleting sub cleans up fully. |
| 289 |
assert.True(t, hm.DeleteHostInfo(sub)) |
| 290 |
assert.Nil(t, hm.QueryVpnAddr(a)) |
| 291 |
assertHostMapInvariants(t, hm) |
| 292 |
} |
| 293 |
|
| 294 |
// TestHostMap_AddDivergentOverlap covers a new hostinfo claiming addresses currently owned by two |
| 295 |
// DIFFERENT hostinfos. The old single shared next/prev chain overwrote a pointer and orphaned one |
| 296 |
// of them (in Indexes but unreachable via its address); independent per-address lists cannot. |
| 297 |
func TestHostMap_AddDivergentOverlap(t *testing.T) { |
| 298 |
l := test.NewLogger() |
| 299 |
hm := newHostMap(l) |
| 300 |
f := &Interface{} |
| 301 |
a := netip.MustParseAddr("0.0.0.1") |
| 302 |
b := netip.MustParseAddr("0.0.0.2") |
| 303 |
|
| 304 |
hiA := &HostInfo{vpnAddrs: []netip.Addr{a}, localIndexId: 1} |
| 305 |
hiP := &HostInfo{vpnAddrs: []netip.Addr{b}, localIndexId: 2} |
| 306 |
hm.unlockedAddHostInfo(hiA, f) |
| 307 |
hm.unlockedAddHostInfo(hiP, f) |
| 308 |
|
| 309 |
hiB := &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: 3} |
| 310 |
hm.unlockedAddHostInfo(hiB, f) |
| 311 |
|
| 312 |
assert.Equal(t, []uint32{3, 1}, chainIds(t, hm, a)) |
| 313 |
assert.Equal(t, []uint32{3, 2}, chainIds(t, hm, b)) |
| 314 |
// hiA is still reachable via its address (not orphaned) and still indexed. |
| 315 |
assert.Contains(t, chainIds(t, hm, a), hiA.localIndexId) |
| 316 |
assert.NotNil(t, hm.QueryIndex(hiA.localIndexId)) |
| 317 |
} |
| 318 |
|
| 319 |
// TestHostMap_MaxHostInfosPerVpnIp_MultipleVpnAddrs verifies the MaxHostInfosPerVpnIp overflow prune |
| 320 |
// (unlockedInnerAddHostInfo calls unlockedDeleteHostInfo on the oldest node once the chain is too long) |
| 321 |
// still behaves when hostinfos carry more than one vpnAddr. The pruned node is always the tail, so it is |
| 322 |
// primary for none of the addresses, and both address chains must stay consistent afterwards. |
| 323 |
func TestHostMap_MaxHostInfosPerVpnIp_MultipleVpnAddrs(t *testing.T) { |
| 324 |
l := test.NewLogger() |
| 325 |
hm := newHostMap(l) |
| 326 |
|
| 327 |
f := &Interface{} |
| 328 |
|
| 329 |
a := netip.MustParseAddr("0.0.0.1") |
| 330 |
b := netip.MustParseAddr("0.0.0.2") |
| 331 |
|
| 332 |
// Add one more than the cap, newest last so it becomes head. Every hostinfo owns both a and b. |
| 333 |
hostinfos := make([]*HostInfo, 0, MaxHostInfosPerVpnIp+1) |
| 334 |
for i := 0; i <= MaxHostInfosPerVpnIp; i++ { |
| 335 |
hostinfos = append(hostinfos, &HostInfo{vpnAddrs: []netip.Addr{a, b}, localIndexId: uint32(i + 1)}) |
| 336 |
} |
| 337 |
// Add oldest first (highest index in our slice) so the very first one added is the overflow victim. |
| 338 |
for i := len(hostinfos) - 1; i >= 0; i-- { |
| 339 |
hm.unlockedAddHostInfo(hostinfos[i], f) |
| 340 |
} |
| 341 |
|
| 342 |
oldest := hostinfos[len(hostinfos)-1] |
| 343 |
|
| 344 |
// The oldest hostinfo was pruned from both lists and the index map. |
| 345 |
assert.Nil(t, hm.QueryIndex(oldest.localIndexId)) |
| 346 |
|
| 347 |
// Both addresses hold exactly MaxHostInfosPerVpnIp survivors in the same order; oldest is absent. |
| 348 |
require.Len(t, chainIds(t, hm, a), MaxHostInfosPerVpnIp) |
| 349 |
assert.Equal(t, chainIds(t, hm, a), chainIds(t, hm, b), "both addresses must list the same survivors in the same order") |
| 350 |
assert.NotContains(t, chainIds(t, hm, a), oldest.localIndexId) |
| 351 |
assert.Equal(t, hm.QueryVpnAddr(a), hm.QueryVpnAddr(b)) |
| 352 |
} |
| 353 |
|
| 354 |
func TestHostMap_reload(t *testing.T) { |
| 355 |
l := test.NewLogger() |
| 356 |
c := config.NewC(test.NewLogger()) |
| 357 |
|
| 358 |
hm := NewHostMapFromConfig(l, c) |
| 359 |
|
| 360 |
toS := func(ipn []netip.Prefix) []string { |
| 361 |
var s []string |
| 362 |
for _, n := range ipn { |
| 363 |
s = append(s, n.String()) |
| 364 |
} |
| 365 |
return s |
| 366 |
} |
| 367 |
|
| 368 |
assert.Empty(t, hm.GetPreferredRanges()) |
| 369 |
|
| 370 |
c.ReloadConfigString("preferred_ranges: [1.1.1.0/24, 10.1.1.0/24]") |
| 371 |
assert.Equal(t, []string{"1.1.1.0/24", "10.1.1.0/24"}, toS(hm.GetPreferredRanges())) |
| 372 |
|
| 373 |
c.ReloadConfigString("preferred_ranges: [1.1.1.1/32]") |
| 374 |
assert.Equal(t, []string{"1.1.1.1/32"}, toS(hm.GetPreferredRanges())) |
| 375 |
} |
| 376 |
|
| 377 |
func TestHostMap_RelayState(t *testing.T) { |
| 378 |
h1 := &HostInfo{vpnAddrs: []netip.Addr{netip.MustParseAddr("0.0.0.1")}, localIndexId: 1} |
| 379 |
a1 := netip.MustParseAddr("::1") |
| 380 |
a2 := netip.MustParseAddr("2001::1") |
| 381 |
|
| 382 |
h1.relayState.InsertRelayTo(a1) |
| 383 |
assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays) |
| 384 |
h1.relayState.InsertRelayTo(a2) |
| 385 |
assert.Equal(t, []netip.Addr{a1, a2}, h1.relayState.relays) |
| 386 |
// Ensure that the first relay added is the first one returned in the copy |
| 387 |
currentRelays := h1.relayState.CopyRelayIps() |
| 388 |
require.Len(t, currentRelays, 2) |
| 389 |
assert.Equal(t, a1, currentRelays[0]) |
| 390 |
|
| 391 |
// Deleting the last one in the list works ok |
| 392 |
h1.relayState.DeleteRelay(a2) |
| 393 |
assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays) |
| 394 |
|
| 395 |
// Deleting an element not in the list works ok |
| 396 |
h1.relayState.DeleteRelay(a2) |
| 397 |
assert.Equal(t, []netip.Addr{a1}, h1.relayState.relays) |
| 398 |
|
| 399 |
// Deleting the only element in the list works ok |
| 400 |
h1.relayState.DeleteRelay(a1) |
| 401 |
assert.Equal(t, []netip.Addr{}, h1.relayState.relays) |
| 402 |
|
| 403 |
} |