nebula/handshake_manager_test.go

95 lines
2.4 KiB
Go
Raw Permalink Normal View History

2019-11-19 10:00:20 -07:00
package nebula
import (
"net/netip"
2019-11-19 10:00:20 -07:00
"testing"
"time"
"github.com/slackhq/nebula/header"
"github.com/slackhq/nebula/test"
"github.com/slackhq/nebula/udp"
2019-11-19 10:00:20 -07:00
"github.com/stretchr/testify/assert"
)
func Test_NewHandshakeManagerVpnIp(t *testing.T) {
l := test.NewLogger()
vpncidr := netip.MustParsePrefix("172.1.1.1/24")
localrange := netip.MustParsePrefix("10.1.1.1/24")
ip := netip.MustParseAddr("172.1.1.2")
preferredRanges := []netip.Prefix{localrange}
mainHM := newHostMap(l, vpncidr)
mainHM.preferredRanges.Store(&preferredRanges)
lh := newTestLighthouse()
2019-11-19 10:00:20 -07:00
cs := &CertState{
RawCertificate: []byte{},
PrivateKey: []byte{},
2024-10-10 17:00:22 -06:00
Certificate: &dummyCert{},
RawCertificateNoKey: []byte{},
}
blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
blah.f = &Interface{handshakeManager: blah, pki: &PKI{}, l: l}
blah.f.pki.cs.Store(cs)
2019-11-19 10:00:20 -07:00
now := time.Now()
blah.NextOutboundHandshakeTimerTick(now)
2019-11-19 10:00:20 -07:00
i := blah.StartHandshake(ip, nil)
i2 := blah.StartHandshake(ip, nil)
assert.Same(t, i, i2)
i.remotes = NewRemoteList(nil)
2019-11-19 10:00:20 -07:00
// Adding something to pending should not affect the main hostmap
assert.Len(t, mainHM.Hosts, 0)
2019-11-19 10:00:20 -07:00
// Confirm they are in the pending index list
assert.Contains(t, blah.vpnIps, ip)
2019-11-19 10:00:20 -07:00
// Jump ahead `HandshakeRetries` ticks, offset by one to get the sleep logic right
for i := 1; i <= DefaultHandshakeRetries+1; i++ {
now = now.Add(time.Duration(i) * DefaultHandshakeTryInterval)
blah.NextOutboundHandshakeTimerTick(now)
2019-11-19 10:00:20 -07:00
}
// Confirm they are still in the pending index list
assert.Contains(t, blah.vpnIps, ip)
// Tick 1 more time, a minute will certainly flush it out
blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
2019-11-19 10:00:20 -07:00
// Confirm they have been removed
assert.NotContains(t, blah.vpnIps, ip)
2019-11-19 10:00:20 -07:00
}
func testCountTimerWheelEntries(tw *LockingTimerWheel[netip.Addr]) (c int) {
2023-01-18 09:56:42 -07:00
for _, i := range tw.t.wheel {
n := i.Head
for n != nil {
c++
n = n.Next
}
}
return c
}
2019-11-19 10:00:20 -07:00
type mockEncWriter struct {
}
func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, nb, out []byte) {
2019-11-19 10:00:20 -07:00
return
}
func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
return
}
func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
return
}
func (mw *mockEncWriter) Handshake(vpnIP netip.Addr) {}