handshake_manager_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. func Test_NewHandshakeManagerVpnIP(t *testing.T) {
  9. l := NewTestLogger()
  10. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  11. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  12. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  13. ip := ip2int(net.ParseIP("172.1.1.2"))
  14. preferredRanges := []*net.IPNet{localrange}
  15. mw := &mockEncWriter{}
  16. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  17. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  18. now := time.Now()
  19. blah.NextOutboundHandshakeTimerTick(now, mw)
  20. i := blah.AddVpnIP(ip)
  21. i.remotes = NewRemoteList()
  22. i.HandshakeReady = true
  23. // Adding something to pending should not affect the main hostmap
  24. assert.Len(t, mainHM.Hosts, 0)
  25. // Confirm they are in the pending index list
  26. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  27. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  28. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  29. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  30. blah.NextOutboundHandshakeTimerTick(now, mw)
  31. }
  32. // Confirm they are still in the pending index list
  33. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  34. // Tick 1 more time, a minute will certainly flush it out
  35. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute), mw)
  36. // Confirm they have been removed
  37. assert.NotContains(t, blah.pendingHostMap.Hosts, ip)
  38. }
  39. func Test_NewHandshakeManagerTrigger(t *testing.T) {
  40. l := NewTestLogger()
  41. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  42. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  43. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  44. ip := ip2int(net.ParseIP("172.1.1.2"))
  45. preferredRanges := []*net.IPNet{localrange}
  46. mw := &mockEncWriter{}
  47. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  48. lh := &LightHouse{addrMap: make(map[uint32]*RemoteList), l: l}
  49. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udpConn{}, defaultHandshakeConfig)
  50. now := time.Now()
  51. blah.NextOutboundHandshakeTimerTick(now, mw)
  52. assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  53. hi := blah.AddVpnIP(ip)
  54. hi.HandshakeReady = true
  55. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  56. assert.Equal(t, 0, hi.HandshakeCounter, "Should not have attempted a handshake yet")
  57. // Trigger the same method the channel will but, this should set our remotes pointer
  58. blah.handleOutbound(ip, mw, true)
  59. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have done a handshake attempt")
  60. assert.NotNil(t, hi.remotes, "Manager should have set my remotes pointer")
  61. // Make sure the trigger doesn't double schedule the timer entry
  62. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  63. uaddr := NewUDPAddrFromString("10.1.1.1:4242")
  64. hi.remotes.unlockedPrependV4(ip, NewIp4AndPort(uaddr.IP, uint32(uaddr.Port)))
  65. // We now have remotes but only the first trigger should have pushed things forward
  66. blah.handleOutbound(ip, mw, true)
  67. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have not done a handshake attempt")
  68. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  69. }
  70. func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
  71. for _, i := range tw.wheel {
  72. n := i.Head
  73. for n != nil {
  74. c++
  75. n = n.Next
  76. }
  77. }
  78. return c
  79. }
  80. type mockEncWriter struct {
  81. }
  82. func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  83. return
  84. }