2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-11-10 20:47:38 -07:00
|
|
|
"github.com/slackhq/nebula/test"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2019-11-19 10:00:20 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func Test_NewHandshakeManagerVpnIp(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2019-11-19 10:00:20 -07:00
|
|
|
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
|
|
|
|
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
|
2021-11-03 19:54:04 -06:00
|
|
|
ip := iputil.Ip2VpnIp(net.ParseIP("172.1.1.2"))
|
2019-11-19 10:00:20 -07:00
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
2023-07-24 11:37:52 -06:00
|
|
|
mainHM := NewHostMap(l, vpncidr, preferredRanges)
|
2022-10-31 11:37:41 -06:00
|
|
|
lh := newTestLighthouse()
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-11-02 15:53:59 -06:00
|
|
|
cs := &CertState{
|
|
|
|
RawCertificate: []byte{},
|
|
|
|
PrivateKey: []byte{},
|
|
|
|
Certificate: &cert.NebulaCertificate{},
|
|
|
|
RawCertificateNoKey: []byte{},
|
|
|
|
}
|
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
blah := NewHandshakeManager(l, mainHM, lh, &udp.NoopConn{}, defaultHandshakeConfig)
|
2023-11-02 15:53:59 -06:00
|
|
|
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()
|
2023-08-21 17:51:45 -06:00
|
|
|
blah.NextOutboundHandshakeTimerTick(now)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-08-21 17:51:45 -06:00
|
|
|
i := blah.StartHandshake(ip, nil)
|
|
|
|
i2 := blah.StartHandshake(ip, nil)
|
2021-11-08 12:46:22 -07:00
|
|
|
assert.Same(t, i, i2)
|
|
|
|
|
2023-05-09 09:22:08 -06:00
|
|
|
i.remotes = NewRemoteList(nil)
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Adding something to pending should not affect the main hostmap
|
|
|
|
assert.Len(t, mainHM.Hosts, 0)
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Confirm they are in the pending index list
|
2023-07-24 11:37:52 -06:00
|
|
|
assert.Contains(t, blah.vpnIps, ip)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-04-14 12:50:09 -06: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)
|
2023-08-21 17:51:45 -06:00
|
|
|
blah.NextOutboundHandshakeTimerTick(now)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm they are still in the pending index list
|
2023-07-24 11:37:52 -06:00
|
|
|
assert.Contains(t, blah.vpnIps, ip)
|
2021-04-14 12:50:09 -06:00
|
|
|
|
|
|
|
// Tick 1 more time, a minute will certainly flush it out
|
2023-08-21 17:51:45 -06:00
|
|
|
blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute))
|
2021-04-14 12:50:09 -06:00
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Confirm they have been removed
|
2023-07-24 11:37:52 -06:00
|
|
|
assert.NotContains(t, blah.vpnIps, ip)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2023-01-18 09:56:42 -07:00
|
|
|
func testCountTimerWheelEntries(tw *LockingTimerWheel[iputil.VpnIp]) (c int) {
|
|
|
|
for _, i := range tw.t.wheel {
|
2020-07-22 08:35:10 -06:00
|
|
|
n := i.Head
|
|
|
|
for n != nil {
|
|
|
|
c++
|
|
|
|
n = n.Next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
type mockEncWriter struct {
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func (mw *mockEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, nb, out []byte) {
|
2019-11-19 10:00:20 -07:00
|
|
|
return
|
|
|
|
}
|
2022-06-21 12:35:23 -06:00
|
|
|
|
2023-04-07 12:28:37 -06:00
|
|
|
func (mw *mockEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
|
2022-06-21 12:35:23 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-05-04 14:16:37 -06:00
|
|
|
func (mw *mockEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, nb, out []byte) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-21 12:35:23 -06:00
|
|
|
func (mw *mockEncWriter) Handshake(vpnIP iputil.VpnIp) {}
|