handshake_manager_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 := &LightHouse{
  22. atomicStaticList: make(map[iputil.VpnIp]struct{}),
  23. atomicLighthouses: make(map[iputil.VpnIp]struct{}),
  24. }
  25. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
  26. now := time.Now()
  27. blah.NextOutboundHandshakeTimerTick(now, mw)
  28. var initCalled bool
  29. initFunc := func(*HostInfo) {
  30. initCalled = true
  31. }
  32. i := blah.AddVpnIp(ip, initFunc)
  33. assert.True(t, initCalled)
  34. initCalled = false
  35. i2 := blah.AddVpnIp(ip, initFunc)
  36. assert.False(t, initCalled)
  37. assert.Same(t, i, i2)
  38. i.remotes = NewRemoteList()
  39. i.HandshakeReady = true
  40. // Adding something to pending should not affect the main hostmap
  41. assert.Len(t, mainHM.Hosts, 0)
  42. // Confirm they are in the pending index list
  43. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  44. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  45. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  46. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  47. blah.NextOutboundHandshakeTimerTick(now, mw)
  48. }
  49. // Confirm they are still in the pending index list
  50. assert.Contains(t, blah.pendingHostMap.Hosts, ip)
  51. // Tick 1 more time, a minute will certainly flush it out
  52. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute), mw)
  53. // Confirm they have been removed
  54. assert.NotContains(t, blah.pendingHostMap.Hosts, ip)
  55. }
  56. func Test_NewHandshakeManagerTrigger(t *testing.T) {
  57. l := test.NewLogger()
  58. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  59. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  60. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  61. ip := iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
  62. preferredRanges := []*net.IPNet{localrange}
  63. mw := &mockEncWriter{}
  64. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  65. lh := &LightHouse{
  66. addrMap: make(map[iputil.VpnIp]*RemoteList),
  67. l: l,
  68. atomicStaticList: make(map[iputil.VpnIp]struct{}),
  69. atomicLighthouses: make(map[iputil.VpnIp]struct{}),
  70. }
  71. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
  72. now := time.Now()
  73. blah.NextOutboundHandshakeTimerTick(now, mw)
  74. assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  75. hi := blah.AddVpnIp(ip, nil)
  76. hi.HandshakeReady = true
  77. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  78. assert.Equal(t, 0, hi.HandshakeCounter, "Should not have attempted a handshake yet")
  79. // Trigger the same method the channel will but, this should set our remotes pointer
  80. blah.handleOutbound(ip, mw, true)
  81. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have done a handshake attempt")
  82. assert.NotNil(t, hi.remotes, "Manager should have set my remotes pointer")
  83. // Make sure the trigger doesn't double schedule the timer entry
  84. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  85. uaddr := udp.NewAddrFromString("10.1.1.1:4242")
  86. hi.remotes.unlockedPrependV4(ip, NewIp4AndPort(uaddr.IP, uint32(uaddr.Port)))
  87. // We now have remotes but only the first trigger should have pushed things forward
  88. blah.handleOutbound(ip, mw, true)
  89. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have not done a handshake attempt")
  90. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  91. }
  92. func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
  93. for _, i := range tw.wheel {
  94. n := i.Head
  95. for n != nil {
  96. c++
  97. n = n.Next
  98. }
  99. }
  100. return c
  101. }
  102. type mockEncWriter struct {
  103. }
  104. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  105. return
  106. }