handshake_manager_test.go 3.7 KB

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