handshake_manager_test.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/slackhq/nebula/header"
  7. "github.com/slackhq/nebula/iputil"
  8. "github.com/slackhq/nebula/test"
  9. "github.com/slackhq/nebula/udp"
  10. "github.com/stretchr/testify/assert"
  11. )
  12. func Test_NewHandshakeManagerVpnIp(t *testing.T) {
  13. l := test.NewLogger()
  14. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  15. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  16. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  17. ip := iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
  18. preferredRanges := []*net.IPNet{localrange}
  19. mw := &mockEncWriter{}
  20. mainHM := NewHostMap(l, vpncidr, preferredRanges)
  21. lh := newTestLighthouse()
  22. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
  23. now := time.Now()
  24. blah.NextOutboundHandshakeTimerTick(now, mw)
  25. i := blah.AddVpnIp(ip)
  26. i2 := blah.AddVpnIp(ip)
  27. assert.Same(t, i, i2)
  28. i.remotes = NewRemoteList(nil)
  29. i.HandshakeReady = true
  30. // Adding something to pending should not affect the main hostmap
  31. assert.Len(t, mainHM.Hosts, 0)
  32. // Confirm they are in the pending index list
  33. assert.Contains(t, blah.vpnIps, ip)
  34. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  35. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  36. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  37. blah.NextOutboundHandshakeTimerTick(now, mw)
  38. }
  39. // Confirm they are still in the pending index list
  40. assert.Contains(t, blah.vpnIps, ip)
  41. // Tick 1 more time, a minute will certainly flush it out
  42. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute), mw)
  43. // Confirm they have been removed
  44. assert.NotContains(t, blah.vpnIps, ip)
  45. }
  46. func testCountTimerWheelEntries(tw *LockingTimerWheel[iputil.VpnIp]) (c int) {
  47. for _, i := range tw.t.wheel {
  48. n := i.Head
  49. for n != nil {
  50. c++
  51. n = n.Next
  52. }
  53. }
  54. return c
  55. }
  56. type mockEncWriter struct {
  57. }
  58. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  59. return
  60. }
  61. func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  62. return
  63. }
  64. func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
  65. return
  66. }
  67. func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}