3
0

handshake_manager_test.go 2.5 KB

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