lighthouse_test.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "fmt"
  6. "net/netip"
  7. "testing"
  8. "github.com/gaissmai/bart"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/config"
  11. "github.com/slackhq/nebula/header"
  12. "github.com/slackhq/nebula/test"
  13. "github.com/stretchr/testify/assert"
  14. "gopkg.in/yaml.v2"
  15. )
  16. func TestOldIPv4Only(t *testing.T) {
  17. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  18. b := []byte{8, 129, 130, 132, 80, 16, 10}
  19. var m V4AddrPort
  20. err := m.Unmarshal(b)
  21. assert.NoError(t, err)
  22. ip := netip.MustParseAddr("10.1.1.1")
  23. bp := ip.As4()
  24. assert.Equal(t, binary.BigEndian.Uint32(bp[:]), m.GetAddr())
  25. }
  26. func Test_lhStaticMapping(t *testing.T) {
  27. l := test.NewLogger()
  28. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  29. nt := new(bart.Table[struct{}])
  30. nt.Insert(myVpnNet, struct{}{})
  31. cs := &CertState{
  32. myVpnNetworks: []netip.Prefix{myVpnNet},
  33. myVpnNetworksTable: nt,
  34. }
  35. lh1 := "10.128.0.2"
  36. c := config.NewC(l)
  37. c.Settings["lighthouse"] = map[interface{}]interface{}{"hosts": []interface{}{lh1}}
  38. c.Settings["static_host_map"] = map[interface{}]interface{}{lh1: []interface{}{"1.1.1.1:4242"}}
  39. _, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  40. assert.Nil(t, err)
  41. lh2 := "10.128.0.3"
  42. c = config.NewC(l)
  43. c.Settings["lighthouse"] = map[interface{}]interface{}{"hosts": []interface{}{lh1, lh2}}
  44. c.Settings["static_host_map"] = map[interface{}]interface{}{lh1: []interface{}{"100.1.1.1:4242"}}
  45. _, err = NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  46. assert.EqualError(t, err, "lighthouse 10.128.0.3 does not have a static_host_map entry")
  47. }
  48. func TestReloadLighthouseInterval(t *testing.T) {
  49. l := test.NewLogger()
  50. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  51. nt := new(bart.Table[struct{}])
  52. nt.Insert(myVpnNet, struct{}{})
  53. cs := &CertState{
  54. myVpnNetworks: []netip.Prefix{myVpnNet},
  55. myVpnNetworksTable: nt,
  56. }
  57. lh1 := "10.128.0.2"
  58. c := config.NewC(l)
  59. c.Settings["lighthouse"] = map[interface{}]interface{}{
  60. "hosts": []interface{}{lh1},
  61. "interval": "1s",
  62. }
  63. c.Settings["static_host_map"] = map[interface{}]interface{}{lh1: []interface{}{"1.1.1.1:4242"}}
  64. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  65. assert.NoError(t, err)
  66. lh.ifce = &mockEncWriter{}
  67. // The first one routine is kicked off by main.go currently, lets make sure that one dies
  68. assert.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5"))
  69. assert.Equal(t, int64(5), lh.interval.Load())
  70. // Subsequent calls are killed off by the LightHouse.Reload function
  71. assert.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10"))
  72. assert.Equal(t, int64(10), lh.interval.Load())
  73. // If this completes then nothing is stealing our reload routine
  74. assert.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11"))
  75. assert.Equal(t, int64(11), lh.interval.Load())
  76. }
  77. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  78. l := test.NewLogger()
  79. myVpnNet := netip.MustParsePrefix("10.128.0.1/0")
  80. nt := new(bart.Table[struct{}])
  81. nt.Insert(myVpnNet, struct{}{})
  82. cs := &CertState{
  83. myVpnNetworks: []netip.Prefix{myVpnNet},
  84. myVpnNetworksTable: nt,
  85. }
  86. c := config.NewC(l)
  87. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  88. if !assert.NoError(b, err) {
  89. b.Fatal()
  90. }
  91. hAddr := netip.MustParseAddrPort("4.5.6.7:12345")
  92. hAddr2 := netip.MustParseAddrPort("4.5.6.7:12346")
  93. vpnIp3 := netip.MustParseAddr("0.0.0.3")
  94. lh.addrMap[vpnIp3] = NewRemoteList([]netip.Addr{vpnIp3}, nil)
  95. lh.addrMap[vpnIp3].unlockedSetV4(
  96. vpnIp3,
  97. vpnIp3,
  98. []*V4AddrPort{
  99. netAddrToProtoV4AddrPort(hAddr.Addr(), hAddr.Port()),
  100. netAddrToProtoV4AddrPort(hAddr2.Addr(), hAddr2.Port()),
  101. },
  102. func(netip.Addr, *V4AddrPort) bool { return true },
  103. )
  104. rAddr := netip.MustParseAddrPort("1.2.2.3:12345")
  105. rAddr2 := netip.MustParseAddrPort("1.2.2.3:12346")
  106. vpnIp2 := netip.MustParseAddr("0.0.0.3")
  107. lh.addrMap[vpnIp2] = NewRemoteList([]netip.Addr{vpnIp2}, nil)
  108. lh.addrMap[vpnIp2].unlockedSetV4(
  109. vpnIp3,
  110. vpnIp3,
  111. []*V4AddrPort{
  112. netAddrToProtoV4AddrPort(rAddr.Addr(), rAddr.Port()),
  113. netAddrToProtoV4AddrPort(rAddr2.Addr(), rAddr2.Port()),
  114. },
  115. func(netip.Addr, *V4AddrPort) bool { return true },
  116. )
  117. mw := &mockEncWriter{}
  118. hi := []netip.Addr{vpnIp2}
  119. b.Run("notfound", func(b *testing.B) {
  120. lhh := lh.NewRequestHandler()
  121. req := &NebulaMeta{
  122. Type: NebulaMeta_HostQuery,
  123. Details: &NebulaMetaDetails{
  124. OldVpnAddr: 4,
  125. V4AddrPorts: nil,
  126. },
  127. }
  128. p, err := req.Marshal()
  129. assert.NoError(b, err)
  130. for n := 0; n < b.N; n++ {
  131. lhh.HandleRequest(rAddr, hi, p, mw)
  132. }
  133. })
  134. b.Run("found", func(b *testing.B) {
  135. lhh := lh.NewRequestHandler()
  136. req := &NebulaMeta{
  137. Type: NebulaMeta_HostQuery,
  138. Details: &NebulaMetaDetails{
  139. OldVpnAddr: 3,
  140. V4AddrPorts: nil,
  141. },
  142. }
  143. p, err := req.Marshal()
  144. assert.NoError(b, err)
  145. for n := 0; n < b.N; n++ {
  146. lhh.HandleRequest(rAddr, hi, p, mw)
  147. }
  148. })
  149. }
  150. func TestLighthouse_Memory(t *testing.T) {
  151. l := test.NewLogger()
  152. myUdpAddr0 := netip.MustParseAddrPort("10.0.0.2:4242")
  153. myUdpAddr1 := netip.MustParseAddrPort("192.168.0.2:4242")
  154. myUdpAddr2 := netip.MustParseAddrPort("172.16.0.2:4242")
  155. myUdpAddr3 := netip.MustParseAddrPort("100.152.0.2:4242")
  156. myUdpAddr4 := netip.MustParseAddrPort("24.15.0.2:4242")
  157. myUdpAddr5 := netip.MustParseAddrPort("192.168.0.2:4243")
  158. myUdpAddr6 := netip.MustParseAddrPort("192.168.0.2:4244")
  159. myUdpAddr7 := netip.MustParseAddrPort("192.168.0.2:4245")
  160. myUdpAddr8 := netip.MustParseAddrPort("192.168.0.2:4246")
  161. myUdpAddr9 := netip.MustParseAddrPort("192.168.0.2:4247")
  162. myUdpAddr10 := netip.MustParseAddrPort("192.168.0.2:4248")
  163. myUdpAddr11 := netip.MustParseAddrPort("192.168.0.2:4249")
  164. myVpnIp := netip.MustParseAddr("10.128.0.2")
  165. theirUdpAddr0 := netip.MustParseAddrPort("10.0.0.3:4242")
  166. theirUdpAddr1 := netip.MustParseAddrPort("192.168.0.3:4242")
  167. theirUdpAddr2 := netip.MustParseAddrPort("172.16.0.3:4242")
  168. theirUdpAddr3 := netip.MustParseAddrPort("100.152.0.3:4242")
  169. theirUdpAddr4 := netip.MustParseAddrPort("24.15.0.3:4242")
  170. theirVpnIp := netip.MustParseAddr("10.128.0.3")
  171. c := config.NewC(l)
  172. c.Settings["lighthouse"] = map[interface{}]interface{}{"am_lighthouse": true}
  173. c.Settings["listen"] = map[interface{}]interface{}{"port": 4242}
  174. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  175. nt := new(bart.Table[struct{}])
  176. nt.Insert(myVpnNet, struct{}{})
  177. cs := &CertState{
  178. myVpnNetworks: []netip.Prefix{myVpnNet},
  179. myVpnNetworksTable: nt,
  180. }
  181. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  182. lh.ifce = &mockEncWriter{}
  183. assert.NoError(t, err)
  184. lhh := lh.NewRequestHandler()
  185. // Test that my first update responds with just that
  186. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr2}, lhh)
  187. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  188. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr2)
  189. // Ensure we don't accumulate addresses
  190. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr3}, lhh)
  191. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  192. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr3)
  193. // Grow it back to 2
  194. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr4}, lhh)
  195. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  196. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  197. // Update a different host and ask about it
  198. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []netip.AddrPort{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  199. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, theirVpnIp, lhh)
  200. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  201. // Have both hosts ask about the other
  202. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  203. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  204. r = newLHHostRequest(myUdpAddr0, myVpnIp, theirVpnIp, lhh)
  205. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  206. // Make sure we didn't get changed
  207. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  208. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  209. // Ensure proper ordering and limiting
  210. // Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe)
  211. newLHHostUpdate(
  212. myUdpAddr0,
  213. myVpnIp,
  214. []netip.AddrPort{
  215. myUdpAddr1,
  216. myUdpAddr2,
  217. myUdpAddr3,
  218. myUdpAddr4,
  219. myUdpAddr5,
  220. myUdpAddr5, //Duplicated on purpose
  221. myUdpAddr6,
  222. myUdpAddr7,
  223. myUdpAddr8,
  224. myUdpAddr9,
  225. myUdpAddr10,
  226. myUdpAddr11, // This should get cut
  227. }, lhh)
  228. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  229. assertIp4InArray(
  230. t,
  231. r.msg.Details.V4AddrPorts,
  232. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9,
  233. )
  234. // Make sure we won't add ips in our vpn network
  235. bad1 := netip.MustParseAddrPort("10.128.0.99:4242")
  236. bad2 := netip.MustParseAddrPort("10.128.0.100:4242")
  237. good := netip.MustParseAddrPort("1.128.0.99:4242")
  238. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{bad1, bad2, good}, lhh)
  239. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  240. assertIp4InArray(t, r.msg.Details.V4AddrPorts, good)
  241. }
  242. func TestLighthouse_reload(t *testing.T) {
  243. l := test.NewLogger()
  244. c := config.NewC(l)
  245. c.Settings["lighthouse"] = map[interface{}]interface{}{"am_lighthouse": true}
  246. c.Settings["listen"] = map[interface{}]interface{}{"port": 4242}
  247. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  248. nt := new(bart.Table[struct{}])
  249. nt.Insert(myVpnNet, struct{}{})
  250. cs := &CertState{
  251. myVpnNetworks: []netip.Prefix{myVpnNet},
  252. myVpnNetworksTable: nt,
  253. }
  254. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  255. assert.NoError(t, err)
  256. nc := map[interface{}]interface{}{
  257. "static_host_map": map[interface{}]interface{}{
  258. "10.128.0.2": []interface{}{"1.1.1.1:4242"},
  259. },
  260. }
  261. rc, err := yaml.Marshal(nc)
  262. assert.NoError(t, err)
  263. c.ReloadConfigString(string(rc))
  264. err = lh.reload(c, false)
  265. assert.NoError(t, err)
  266. }
  267. func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply {
  268. //TODO: IPV6-WORK
  269. bip := queryVpnIp.As4()
  270. req := &NebulaMeta{
  271. Type: NebulaMeta_HostQuery,
  272. Details: &NebulaMetaDetails{
  273. OldVpnAddr: binary.BigEndian.Uint32(bip[:]),
  274. },
  275. }
  276. b, err := req.Marshal()
  277. if err != nil {
  278. panic(err)
  279. }
  280. filter := NebulaMeta_HostQueryReply
  281. w := &testEncWriter{
  282. metaFilter: &filter,
  283. }
  284. lhh.HandleRequest(fromAddr, []netip.Addr{myVpnIp}, b, w)
  285. return w.lastReply
  286. }
  287. func newLHHostUpdate(fromAddr netip.AddrPort, vpnIp netip.Addr, addrs []netip.AddrPort, lhh *LightHouseHandler) {
  288. //TODO: IPV6-WORK
  289. bip := vpnIp.As4()
  290. req := &NebulaMeta{
  291. Type: NebulaMeta_HostUpdateNotification,
  292. Details: &NebulaMetaDetails{
  293. OldVpnAddr: binary.BigEndian.Uint32(bip[:]),
  294. V4AddrPorts: make([]*V4AddrPort, len(addrs)),
  295. },
  296. }
  297. for k, v := range addrs {
  298. req.Details.V4AddrPorts[k] = netAddrToProtoV4AddrPort(v.Addr(), v.Port())
  299. }
  300. b, err := req.Marshal()
  301. if err != nil {
  302. panic(err)
  303. }
  304. w := &testEncWriter{}
  305. lhh.HandleRequest(fromAddr, []netip.Addr{vpnIp}, b, w)
  306. }
  307. //TODO: this is a RemoteList test
  308. //func Test_lhRemoteAllowList(t *testing.T) {
  309. // l := NewLogger()
  310. // c := NewConfig(l)
  311. // c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  312. // "10.20.0.0/12": false,
  313. // }
  314. // allowList, err := c.GetAllowList("remoteallowlist", false)
  315. // assert.Nil(t, err)
  316. //
  317. // lh1 := "10.128.0.2"
  318. // lh1IP := net.ParseIP(lh1)
  319. //
  320. // udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  321. //
  322. // lh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  323. // lh.SetRemoteAllowList(allowList)
  324. //
  325. // // A disallowed ip should not enter the cache but we should end up with an empty entry in the addrMap
  326. // remote1IP := net.ParseIP("10.20.0.3")
  327. // remotes := lh.unlockedGetRemoteList(ip2int(remote1IP))
  328. // remotes.unlockedPrependV4(ip2int(remote1IP), NewIp4AndPort(remote1IP, 4242))
  329. // assert.NotNil(t, lh.addrMap[ip2int(remote1IP)])
  330. // assert.Empty(t, lh.addrMap[ip2int(remote1IP)].CopyAddrs([]*net.IPNet{}))
  331. //
  332. // // Make sure a good ip enters the cache and addrMap
  333. // remote2IP := net.ParseIP("10.128.0.3")
  334. // remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  335. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote2UDPAddr.IP, uint32(remote2UDPAddr.Port)), false, false)
  336. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr)
  337. //
  338. // // Another good ip gets into the cache, ordering is inverted
  339. // remote3IP := net.ParseIP("10.128.0.4")
  340. // remote3UDPAddr := NewUDPAddr(remote3IP, uint16(4243))
  341. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote3UDPAddr.IP, uint32(remote3UDPAddr.Port)), false, false)
  342. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr, remote3UDPAddr)
  343. //
  344. // // If we exceed the length limit we should only have the most recent addresses
  345. // addedAddrs := []*udpAddr{}
  346. // for i := 0; i < 11; i++ {
  347. // remoteUDPAddr := NewUDPAddr(net.IP{10, 128, 0, 4}, uint16(4243+i))
  348. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remoteUDPAddr.IP, uint32(remoteUDPAddr.Port)), false, false)
  349. // // The first entry here is a duplicate, don't add it to the assert list
  350. // if i != 0 {
  351. // addedAddrs = append(addedAddrs, remoteUDPAddr)
  352. // }
  353. // }
  354. //
  355. // // We should only have the last 10 of what we tried to add
  356. // assert.True(t, len(addedAddrs) >= 10, "We should have tried to add at least 10 addresses")
  357. // assertUdpAddrInArray(
  358. // t,
  359. // lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}),
  360. // addedAddrs[0],
  361. // addedAddrs[1],
  362. // addedAddrs[2],
  363. // addedAddrs[3],
  364. // addedAddrs[4],
  365. // addedAddrs[5],
  366. // addedAddrs[6],
  367. // addedAddrs[7],
  368. // addedAddrs[8],
  369. // addedAddrs[9],
  370. // )
  371. //}
  372. type testLhReply struct {
  373. nebType header.MessageType
  374. nebSubType header.MessageSubType
  375. vpnIp netip.Addr
  376. msg *NebulaMeta
  377. }
  378. type testEncWriter struct {
  379. lastReply testLhReply
  380. metaFilter *NebulaMeta_MessageType
  381. protocolVersion cert.Version
  382. }
  383. func (tw *testEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  384. }
  385. func (tw *testEncWriter) Handshake(vpnIp netip.Addr) {
  386. }
  387. func (tw *testEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, _, _ []byte) {
  388. msg := &NebulaMeta{}
  389. err := msg.Unmarshal(p)
  390. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  391. tw.lastReply = testLhReply{
  392. nebType: t,
  393. nebSubType: st,
  394. vpnIp: hostinfo.vpnAddrs[0],
  395. msg: msg,
  396. }
  397. }
  398. if err != nil {
  399. panic(err)
  400. }
  401. }
  402. func (tw *testEncWriter) SendMessageToVpnAddr(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, _, _ []byte) {
  403. msg := &NebulaMeta{}
  404. err := msg.Unmarshal(p)
  405. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  406. tw.lastReply = testLhReply{
  407. nebType: t,
  408. nebSubType: st,
  409. vpnIp: vpnIp,
  410. msg: msg,
  411. }
  412. }
  413. if err != nil {
  414. panic(err)
  415. }
  416. }
  417. func (tw *testEncWriter) GetHostInfo(vpnIp netip.Addr) *HostInfo {
  418. return nil
  419. }
  420. func (tw *testEncWriter) GetCertState() *CertState {
  421. return &CertState{defaultVersion: tw.protocolVersion}
  422. }
  423. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  424. func assertIp4InArray(t *testing.T, have []*V4AddrPort, want ...netip.AddrPort) {
  425. if !assert.Len(t, have, len(want)) {
  426. return
  427. }
  428. for k, w := range want {
  429. //TODO: IPV6-WORK
  430. h := protoV4AddrPortToNetAddrPort(have[k])
  431. if !(h == w) {
  432. assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v, found %v", w, k, h))
  433. }
  434. }
  435. }
  436. func Test_findNetworkUnion(t *testing.T) {
  437. var out netip.Addr
  438. var ok bool
  439. tenDot := netip.MustParsePrefix("10.0.0.0/8")
  440. oneSevenTwo := netip.MustParsePrefix("172.16.0.0/16")
  441. fe80 := netip.MustParsePrefix("fe80::/8")
  442. fc00 := netip.MustParsePrefix("fc00::/7")
  443. a1 := netip.MustParseAddr("10.0.0.1")
  444. afe81 := netip.MustParseAddr("fe80::1")
  445. //simple
  446. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1})
  447. assert.True(t, ok)
  448. assert.Equal(t, out, a1)
  449. //mixed lengths
  450. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1, afe81})
  451. assert.True(t, ok)
  452. assert.Equal(t, out, a1)
  453. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo}, []netip.Addr{a1})
  454. assert.True(t, ok)
  455. assert.Equal(t, out, a1)
  456. //mixed family
  457. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1})
  458. assert.True(t, ok)
  459. assert.Equal(t, out, a1)
  460. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  461. assert.True(t, ok)
  462. assert.Equal(t, out, a1)
  463. //ordering
  464. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81, a1})
  465. assert.True(t, ok)
  466. assert.Equal(t, out, a1)
  467. out, ok = findNetworkUnion([]netip.Prefix{fe80, tenDot, oneSevenTwo}, []netip.Addr{afe81, a1})
  468. assert.True(t, ok)
  469. assert.Equal(t, out, afe81)
  470. //some mismatches
  471. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81})
  472. assert.True(t, ok)
  473. assert.Equal(t, out, afe81)
  474. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  475. assert.True(t, ok)
  476. assert.Equal(t, out, afe81)
  477. //falsey cases
  478. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1})
  479. assert.False(t, ok)
  480. out, ok = findNetworkUnion([]netip.Prefix{fc00, fe80}, []netip.Addr{a1})
  481. assert.False(t, ok)
  482. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fc00}, []netip.Addr{a1, afe81})
  483. assert.False(t, ok)
  484. out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81})
  485. assert.False(t, ok)
  486. }