handshake_manager_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  15. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  16. ip := iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
  17. preferredRanges := []*net.IPNet{localrange}
  18. mainHM := NewHostMap(l, vpncidr, preferredRanges)
  19. lh := newTestLighthouse()
  20. blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
  21. now := time.Now()
  22. blah.NextOutboundHandshakeTimerTick(now)
  23. i := blah.StartHandshake(ip, nil)
  24. i2 := blah.StartHandshake(ip, nil)
  25. assert.Same(t, i, i2)
  26. i.remotes = NewRemoteList(nil)
  27. i.HandshakeReady = true
  28. // Adding something to pending should not affect the main hostmap
  29. assert.Len(t, mainHM.Hosts, 0)
  30. // Confirm they are in the pending index list
  31. assert.Contains(t, blah.vpnIps, ip)
  32. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  33. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  34. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  35. blah.NextOutboundHandshakeTimerTick(now)
  36. }
  37. // Confirm they are still in the pending index list
  38. assert.Contains(t, blah.vpnIps, ip)
  39. // Tick 1 more time, a minute will certainly flush it out
  40. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
  41. // Confirm they have been removed
  42. assert.NotContains(t, blah.vpnIps, ip)
  43. }
  44. func testCountTimerWheelEntries(tw *LockingTimerWheel[iputil.VpnIp]) (c int) {
  45. for _, i := range tw.t.wheel {
  46. n := i.Head
  47. for n != nil {
  48. c++
  49. n = n.Next
  50. }
  51. }
  52. return c
  53. }
  54. type mockEncWriter struct {
  55. }
  56. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  57. return
  58. }
  59. func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  60. return
  61. }
  62. func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
  63. return
  64. }
  65. func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}