handshake_manager_test.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/slackhq/nebula/cert"
  7. "github.com/slackhq/nebula/header"
  8. "github.com/slackhq/nebula/iputil"
  9. "github.com/slackhq/nebula/test"
  10. "github.com/slackhq/nebula/udp"
  11. "github.com/stretchr/testify/assert"
  12. )
  13. func Test_NewHandshakeManagerVpnIp(t *testing.T) {
  14. l := test.NewLogger()
  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. mainHM := newHostMap(l, vpncidr)
  20. mainHM.preferredRanges.Store(&preferredRanges)
  21. lh := newTestLighthouse()
  22. cs := &CertState{
  23. RawCertificate: []byte{},
  24. PrivateKey: []byte{},
  25. Certificate: &cert.NebulaCertificate{},
  26. RawCertificateNoKey: []byte{},
  27. }
  28. blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
  29. blah.f = &Interface{handshakeManager: blah, pki: &PKI{}, l: l}
  30. blah.f.pki.cs.Store(cs)
  31. now := time.Now()
  32. blah.NextOutboundHandshakeTimerTick(now)
  33. i := blah.StartHandshake(ip, nil)
  34. i2 := blah.StartHandshake(ip, nil)
  35. assert.Same(t, i, i2)
  36. i.remotes = NewRemoteList(nil)
  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.vpnIps, 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)
  45. }
  46. // Confirm they are still in the pending index list
  47. assert.Contains(t, blah.vpnIps, ip)
  48. // Tick 1 more time, a minute will certainly flush it out
  49. blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
  50. // Confirm they have been removed
  51. assert.NotContains(t, blah.vpnIps, ip)
  52. }
  53. func testCountTimerWheelEntries(tw *LockingTimerWheel[iputil.VpnIp]) (c int) {
  54. for _, i := range tw.t.wheel {
  55. n := i.Head
  56. for n != nil {
  57. c++
  58. n = n.Next
  59. }
  60. }
  61. return c
  62. }
  63. type mockEncWriter struct {
  64. }
  65. func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
  66. return
  67. }
  68. func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  69. return
  70. }
  71. func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
  72. return
  73. }
  74. func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}