2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/header"
|
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
|
"github.com/slackhq/nebula/udp"
|
|
|
|
"github.com/slackhq/nebula/util"
|
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) {
|
|
|
|
l := util.NewTestLogger()
|
2019-12-12 09:34:17 -07:00
|
|
|
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
|
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}
|
|
|
|
mw := &mockEncWriter{}
|
2021-03-26 08:46:30 -06:00
|
|
|
mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, &LightHouse{}, &udp.Conn{}, defaultHandshakeConfig)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
|
|
|
|
2021-11-08 12:46:22 -07:00
|
|
|
var initCalled bool
|
|
|
|
initFunc := func(*HostInfo) {
|
|
|
|
initCalled = true
|
|
|
|
}
|
|
|
|
|
|
|
|
i := blah.AddVpnIp(ip, initFunc)
|
|
|
|
assert.True(t, initCalled)
|
|
|
|
|
|
|
|
initCalled = false
|
|
|
|
i2 := blah.AddVpnIp(ip, initFunc)
|
|
|
|
assert.False(t, initCalled)
|
|
|
|
assert.Same(t, i, i2)
|
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
i.remotes = NewRemoteList()
|
|
|
|
i.HandshakeReady = true
|
|
|
|
|
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
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.Contains(t, blah.pendingHostMap.Hosts, 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)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm they are still in the pending index list
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.Contains(t, blah.pendingHostMap.Hosts, ip)
|
|
|
|
|
|
|
|
// Tick 1 more time, a minute will certainly flush it out
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now.Add(time.Minute), mw)
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Confirm they have been removed
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.NotContains(t, blah.pendingHostMap.Hosts, ip)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2020-07-22 08:35:10 -06:00
|
|
|
func Test_NewHandshakeManagerTrigger(t *testing.T) {
|
2021-11-03 19:54:04 -06:00
|
|
|
l := util.NewTestLogger()
|
2020-07-22 08:35:10 -06:00
|
|
|
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
|
|
|
|
_, 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"))
|
2020-07-22 08:35:10 -06:00
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mw := &mockEncWriter{}
|
2021-03-26 08:46:30 -06:00
|
|
|
mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
|
2021-11-03 19:54:04 -06:00
|
|
|
lh := &LightHouse{addrMap: make(map[iputil.VpnIp]*RemoteList), l: l}
|
2020-07-22 08:35:10 -06:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, lh, &udp.Conn{}, defaultHandshakeConfig)
|
2020-07-22 08:35:10 -06:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
|
2021-11-08 12:46:22 -07:00
|
|
|
hi := blah.AddVpnIp(ip, nil)
|
2021-04-14 12:50:09 -06:00
|
|
|
hi.HandshakeReady = true
|
2020-07-22 08:35:10 -06:00
|
|
|
assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.Equal(t, 0, hi.HandshakeCounter, "Should not have attempted a handshake yet")
|
2020-07-22 08:35:10 -06:00
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// Trigger the same method the channel will but, this should set our remotes pointer
|
2020-07-22 08:35:10 -06:00
|
|
|
blah.handleOutbound(ip, mw, true)
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have done a handshake attempt")
|
|
|
|
assert.NotNil(t, hi.remotes, "Manager should have set my remotes pointer")
|
2020-07-22 08:35:10 -06:00
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// Make sure the trigger doesn't double schedule the timer entry
|
2020-07-22 08:35:10 -06:00
|
|
|
assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
uaddr := udp.NewAddrFromString("10.1.1.1:4242")
|
2021-04-14 12:50:09 -06:00
|
|
|
hi.remotes.unlockedPrependV4(ip, NewIp4AndPort(uaddr.IP, uint32(uaddr.Port)))
|
2020-07-22 08:35:10 -06:00
|
|
|
|
2021-04-14 12:50:09 -06:00
|
|
|
// We now have remotes but only the first trigger should have pushed things forward
|
2020-07-22 08:35:10 -06:00
|
|
|
blah.handleOutbound(ip, mw, true)
|
2021-04-14 12:50:09 -06:00
|
|
|
assert.Equal(t, 1, hi.HandshakeCounter, "Trigger should have not done a handshake attempt")
|
2020-07-22 08:35:10 -06:00
|
|
|
assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
}
|
|
|
|
|
|
|
|
func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
|
|
|
|
for _, i := range tw.wheel {
|
|
|
|
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
|
|
|
|
}
|