handshake_manager_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Test_NewHandshakeManagerTrigger(t *testing.T) {
  54. l := test.NewLogger()
  55. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  56. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  57. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  58. ip := iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
  59. preferredRanges := []*net.IPNet{localrange}
  60. mw := &mockEncWriter{}
  61. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  62. lh := newTestLighthouse()
  63. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
  64. now := time.Now()
  65. blah.NextOutboundHandshakeTimerTick(now, mw)
  66. assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  67. hi := blah.AddVpnIp(ip, nil)
  68. hi.HandshakeReady = true
  69. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  70. assert.Equal(t, 0, hi.HandshakeCounter, "Should not have attempted a handshake yet")
  71. // Trigger the same method the channel will but, this should set our remotes pointer
  72. blah.handleOutbound(ip, mw, true)
  73. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have done a handshake attempt")
  74. assert.NotNil(t, hi.remotes, "Manager should have set my remotes pointer")
  75. // Make sure the trigger doesn't double schedule the timer entry
  76. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  77. uaddr := udp.NewAddrFromString("10.1.1.1:4242")
  78. hi.remotes.unlockedPrependV4(ip, NewIp4AndPort(uaddr.IP, uint32(uaddr.Port)))
  79. // We now have remotes but only the first trigger should have pushed things forward
  80. blah.handleOutbound(ip, mw, true)
  81. assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have not done a handshake attempt")
  82. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  83. }
  84. func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
  85. for _, i := range tw.wheel {
  86. n := i.Head
  87. for n != nil {
  88. c++
  89. n = n.Next
  90. }
  91. }
  92. return c
  93. }
  94. type mockEncWriter struct {
  95. }
  96. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  97. return
  98. }
  99. func (mw *mockEncWriter) SendVia(via interface{}, relay interface{}, ad, nb, out []byte, nocopy bool) {
  100. return
  101. }
  102. func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}