handshake_manager_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package nebula
  2. import (
  3. "net/netip"
  4. "testing"
  5. "time"
  6. "github.com/slackhq/nebula/header"
  7. "github.com/slackhq/nebula/test"
  8. "github.com/slackhq/nebula/udp"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. func Test_NewHandshakeManagerVpnIp(t *testing.T) {
  12. l := test.NewLogger()
  13. vpncidr := netip.MustParsePrefix("172.1.1.1/24")
  14. localrange := netip.MustParsePrefix("10.1.1.1/24")
  15. ip := netip.MustParseAddr("172.1.1.2")
  16. preferredRanges := []netip.Prefix{localrange}
  17. mainHM := newHostMap(l, vpncidr)
  18. mainHM.preferredRanges.Store(&preferredRanges)
  19. lh := newTestLighthouse()
  20. cs := &CertState{
  21. RawCertificate: []byte{},
  22. PrivateKey: []byte{},
  23. Certificate: &dummyCert{},
  24. RawCertificateNoKey: []byte{},
  25. }
  26. blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
  27. blah.f = &Interface{handshakeManager: blah, pki: &PKI{}, l: l}
  28. blah.f.pki.cs.Store(cs)
  29. now := time.Now()
  30. blah.NextOutboundHandshakeTimerTick(now)
  31. i := blah.StartHandshake(ip, nil)
  32. i2 := blah.StartHandshake(ip, nil)
  33. assert.Same(t, i, i2)
  34. i.remotes = NewRemoteList(nil)
  35. // Adding something to pending should not affect the main hostmap
  36. assert.Len(t, mainHM.Hosts, 0)
  37. // Confirm they are in the pending index list
  38. assert.Contains(t, blah.vpnIps, ip)
  39. // Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
  40. for i := 1; i <= DefaultHandshakeRetries+1; i++ {
  41. now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
  42. blah.NextOutboundHandshakeTimerTick(now)
  43. }
  44. // Confirm they are still in the pending index list
  45. assert.Contains(t, blah.vpnIps, ip)
  46. // Tick 1 more time, a minute will certainly flush it out
  47. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
  48. // Confirm they have been removed
  49. assert.NotContains(t, blah.vpnIps, ip)
  50. }
  51. func testCountTimerWheelEntries(tw *LockingTimerWheel[netip.Addr]) (c int) {
  52. for _, i := range tw.t.wheel {
  53. n := i.Head
  54. for n != nil {
  55. c++
  56. n = n.Next
  57. }
  58. }
  59. return c
  60. }
  61. type mockEncWriter struct {
  62. }
  63. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, nb, out []byte) {
  64. return
  65. }
  66. func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  67. return
  68. }
  69. func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
  70. return
  71. }
  72. func (mw *mockEncWriter) Handshake(vpnIP netip.Addr) {}