github.com/slackhq/nebula@v1.9.0/handshake_manager_test.go (about)

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