handshake_manager_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package nebula
  2. import (
  3. "net/netip"
  4. "testing"
  5. "time"
  6. "github.com/slackhq/nebula/cert"
  7. "github.com/slackhq/nebula/header"
  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. 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)
  18. mainHM.preferredRanges.Store(&preferredRanges)
  19. lh := newTestLighthouse()
  20. cs := &CertState{
  21. initiatingVersion: cert.Version1,
  22. privateKey: []byte{},
  23. v1Cert: &dummyCert{version: cert.Version1},
  24. v1HandshakeBytes: []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([]netip.Addr{}, nil)
  35. // Adding something to pending should not affect the main hostmap
  36. assert.Empty(t, mainHM.Hosts)
  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. type mockEncWriter struct {
  52. }
  53. func (mw *mockEncWriter) SendMessageToVpnAddr(_ header.MessageType, _ header.MessageSubType, _ netip.Addr, _, _, _ []byte) {
  54. }
  55. func (mw *mockEncWriter) SendVia(_ *HostInfo, _ *Relay, _, _, _ []byte, _ bool) {
  56. }
  57. func (mw *mockEncWriter) SendMessageToHostInfo(_ header.MessageType, _ header.MessageSubType, _ *HostInfo, _, _, _ []byte) {
  58. }
  59. func (mw *mockEncWriter) Handshake(_ netip.Addr) {}
  60. func (mw *mockEncWriter) GetHostInfo(_ netip.Addr) *HostInfo {
  61. return nil
  62. }
  63. func (mw *mockEncWriter) GetCertState() *CertState {
  64. return &CertState{initiatingVersion: cert.Version2}
  65. }