handshake_manager_test.go 3.9 KB

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