handshake_manager_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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, "test", vpncidr, preferredRanges)
  21. lh := newTestLighthouse()
  22. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
  23. now := time.Now()
  24. blah.NextOutboundHandshakeTimerTick(now, mw)
  25. var initCalled bool
  26. initFunc := func(*HostInfo) {
  27. initCalled = true
  28. }
  29. i := blah.AddVpnIp(ip, initFunc)
  30. assert.True(t, initCalled)
  31. initCalled = false
  32. i2 := blah.AddVpnIp(ip, initFunc)
  33. assert.False(t, initCalled)
  34. assert.Same(t, i, i2)
  35. i.remotes = NewRemoteList()
  36. i.HandshakeReady = true
  37. // Adding something to pending should not affect the main hostmap
  38. assert.Len(t, mainHM.Hosts, 0)
  39. // Confirm they are in the pending index list
  40. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  41. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  42. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  43. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  44. blah.NextOutboundHandshakeTimerTick(now, mw)
  45. }
  46. // Confirm they are still in the pending index list
  47. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  48. // Tick 1 more time, a minute will certainly flush it out
  49. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute), mw)
  50. // Confirm they have been removed
  51. assert.NotContains(t, blah.pendingHostMap.Hosts, ip)
  52. }
  53. func testCountTimerWheelEntries(tw *LockingTimerWheel[iputil.VpnIp]) (c int) {
  54. for _, i := range tw.t.wheel {
  55. n := i.Head
  56. for n != nil {
  57. c++
  58. n = n.Next
  59. }
  60. }
  61. return c
  62. }
  63. type mockEncWriter struct {
  64. }
  65. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  66. return
  67. }
  68. func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  69. return
  70. }
  71. func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}