2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
//var ips []uint32 = []uint32{9000, 9999999, 3, 292394923}
|
2019-12-12 09:34:17 -07:00
|
|
|
var ips []uint32
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
func Test_NewHandshakeManagerIndex(t *testing.T) {
|
2021-03-12 12:16:25 -07:00
|
|
|
|
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")
|
2019-12-12 09:34:17 -07:00
|
|
|
ips = []uint32{ip2int(net.ParseIP("172.1.1.2"))}
|
2019-11-19 10:00:20 -07:00
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mainHM := NewHostMap("test", vpncidr, preferredRanges)
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextInboundHandshakeTimerTick(now)
|
|
|
|
|
2021-03-12 12:16:25 -07:00
|
|
|
var indexes = make([]uint32, 4)
|
|
|
|
var hostinfo = make([]*HostInfo, len(indexes))
|
|
|
|
for i := range indexes {
|
|
|
|
hostinfo[i] = &HostInfo{ConnectionState: &ConnectionState{}}
|
|
|
|
}
|
|
|
|
|
2019-11-19 10:00:20 -07:00
|
|
|
// Add four indexes
|
2021-03-12 12:16:25 -07:00
|
|
|
for i := range indexes {
|
|
|
|
err := blah.AddIndexHostInfo(hostinfo[i])
|
|
|
|
assert.NoError(t, err)
|
|
|
|
indexes[i] = hostinfo[i].localIndexId
|
|
|
|
blah.InboundHandshakeTimer.Add(indexes[i], time.Second*10)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
// Confirm they are in the pending index list
|
|
|
|
for _, v := range indexes {
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
|
|
|
|
}
|
|
|
|
// Adding something to pending should not affect the main hostmap
|
|
|
|
assert.Len(t, mainHM.Indexes, 0)
|
|
|
|
// Jump ahead 8 seconds
|
2020-02-21 14:25:11 -07:00
|
|
|
for i := 1; i <= DefaultHandshakeRetries; i++ {
|
|
|
|
next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
|
2019-11-19 10:00:20 -07:00
|
|
|
blah.NextInboundHandshakeTimerTick(next_tick)
|
|
|
|
}
|
|
|
|
// Confirm they are still in the pending index list
|
|
|
|
for _, v := range indexes {
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
|
|
|
|
}
|
|
|
|
// Jump ahead 4 more seconds
|
|
|
|
next_tick := now.Add(12 * time.Second)
|
|
|
|
blah.NextInboundHandshakeTimerTick(next_tick)
|
|
|
|
// Confirm they have been removed
|
|
|
|
for _, v := range indexes {
|
|
|
|
assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_NewHandshakeManagerVpnIP(t *testing.T) {
|
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")
|
2019-12-12 09:34:17 -07:00
|
|
|
ips = []uint32{ip2int(net.ParseIP("172.1.1.2"))}
|
2019-11-19 10:00:20 -07:00
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mw := &mockEncWriter{}
|
|
|
|
mainHM := NewHostMap("test", vpncidr, preferredRanges)
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
|
|
|
|
|
|
|
// Add four "IPs" - which are just uint32s
|
|
|
|
for _, v := range ips {
|
|
|
|
blah.AddVpnIP(v)
|
|
|
|
}
|
|
|
|
// Adding something to pending should not affect the main hostmap
|
|
|
|
assert.Len(t, mainHM.Hosts, 0)
|
|
|
|
// Confirm they are in the pending index list
|
|
|
|
for _, v := range ips {
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Jump ahead `HandshakeRetries` ticks
|
|
|
|
cumulative := time.Duration(0)
|
2020-02-21 14:25:11 -07:00
|
|
|
for i := 0; i <= DefaultHandshakeRetries+1; i++ {
|
|
|
|
cumulative += time.Duration(i)*DefaultHandshakeTryInterval + 1
|
2019-11-19 10:00:20 -07:00
|
|
|
next_tick := now.Add(cumulative)
|
|
|
|
//l.Infoln(next_tick)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick, mw)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Confirm they are still in the pending index list
|
|
|
|
for _, v := range ips {
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
|
|
|
|
}
|
|
|
|
// Jump ahead 1 more second
|
2020-02-21 14:25:11 -07:00
|
|
|
cumulative += time.Duration(DefaultHandshakeRetries+1) * DefaultHandshakeTryInterval
|
2019-11-19 10:00:20 -07:00
|
|
|
next_tick := now.Add(cumulative)
|
|
|
|
//l.Infoln(next_tick)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick, mw)
|
|
|
|
// Confirm they have been removed
|
|
|
|
for _, v := range ips {
|
|
|
|
assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 08:35:10 -06:00
|
|
|
func Test_NewHandshakeManagerTrigger(t *testing.T) {
|
|
|
|
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
|
|
|
|
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
|
|
|
|
_, localrange, _ := net.ParseCIDR("10.1.1.1/24")
|
|
|
|
ip := ip2int(net.ParseIP("172.1.1.2"))
|
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mw := &mockEncWriter{}
|
|
|
|
mainHM := NewHostMap("test", vpncidr, preferredRanges)
|
|
|
|
lh := &LightHouse{}
|
|
|
|
|
|
|
|
blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, lh, &udpConn{}, defaultHandshakeConfig)
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
|
|
|
|
|
|
|
assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
|
|
|
|
blah.AddVpnIP(ip)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
|
|
|
|
// Trigger the same method the channel will
|
|
|
|
blah.handleOutbound(ip, mw, true)
|
|
|
|
|
|
|
|
// Make sure the trigger doesn't schedule another timer entry
|
|
|
|
assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
|
|
|
|
hi := blah.pendingHostMap.Hosts[ip]
|
|
|
|
assert.Nil(t, hi.remote)
|
|
|
|
|
2021-03-18 19:37:24 -06:00
|
|
|
lh.addrMap = map[uint32][]*udpAddr{
|
|
|
|
ip: {NewUDPAddrFromString("10.1.1.1:4242")},
|
2020-07-22 08:35:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// This should trigger the hostmap to populate the hostinfo
|
|
|
|
blah.handleOutbound(ip, mw, true)
|
|
|
|
assert.NotNil(t, hi.remote)
|
|
|
|
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
|
|
|
func Test_NewHandshakeManagerVpnIPcleanup(t *testing.T) {
|
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")
|
2019-12-12 09:34:17 -07:00
|
|
|
vpnIP = ip2int(net.ParseIP("172.1.1.2"))
|
2019-11-19 10:00:20 -07:00
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mw := &mockEncWriter{}
|
|
|
|
mainHM := NewHostMap("test", vpncidr, preferredRanges)
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextOutboundHandshakeTimerTick(now, mw)
|
|
|
|
|
2019-12-12 09:34:17 -07:00
|
|
|
hostinfo := blah.AddVpnIP(vpnIP)
|
2019-11-19 10:00:20 -07:00
|
|
|
// Pretned we have an index too
|
2021-03-12 12:16:25 -07:00
|
|
|
err := blah.AddIndexHostInfo(hostinfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
blah.InboundHandshakeTimer.Add(hostinfo.localIndexId, time.Second*10)
|
|
|
|
assert.NotZero(t, hostinfo.localIndexId)
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Indexes, hostinfo.localIndexId)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
// Jump ahead `HandshakeRetries` ticks. Eviction should happen in pending
|
|
|
|
// but not main hostmap
|
|
|
|
cumulative := time.Duration(0)
|
2020-02-21 14:25:11 -07:00
|
|
|
for i := 1; i <= DefaultHandshakeRetries+2; i++ {
|
|
|
|
cumulative += DefaultHandshakeTryInterval * time.Duration(i)
|
2019-11-19 10:00:20 -07:00
|
|
|
next_tick := now.Add(cumulative)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick, mw)
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
for i := 0; i <= HandshakeRetries+1; i++ {
|
|
|
|
next_tick := now.Add(cumulative)
|
|
|
|
//l.Infoln(next_tick)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
for i := 0; i <= HandshakeRetries+1; i++ {
|
|
|
|
next_tick := now.Add(time.Duration(i) * time.Second)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
cumulative += HandshakeTryInterval*time.Duration(HandshakeRetries) + 3
|
|
|
|
next_tick := now.Add(cumulative)
|
|
|
|
l.Infoln(cumulative, next_tick)
|
|
|
|
blah.NextOutboundHandshakeTimerTick(next_tick)
|
|
|
|
*/
|
2019-12-12 09:34:17 -07:00
|
|
|
assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(vpnIP))
|
2019-11-19 10:00:20 -07:00
|
|
|
assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_NewHandshakeManagerIndexcleanup(t *testing.T) {
|
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")
|
|
|
|
preferredRanges := []*net.IPNet{localrange}
|
|
|
|
mainHM := NewHostMap("test", vpncidr, preferredRanges)
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
|
2019-11-19 10:00:20 -07:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
blah.NextInboundHandshakeTimerTick(now)
|
|
|
|
|
2021-03-12 12:16:25 -07:00
|
|
|
hostinfo := &HostInfo{ConnectionState: &ConnectionState{}}
|
|
|
|
err := blah.AddIndexHostInfo(hostinfo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
blah.InboundHandshakeTimer.Add(hostinfo.localIndexId, time.Second*10)
|
2019-11-19 10:00:20 -07:00
|
|
|
// Pretned we have an index too
|
|
|
|
blah.pendingHostMap.AddVpnIPHostInfo(101010, hostinfo)
|
|
|
|
assert.Contains(t, blah.pendingHostMap.Hosts, uint32(101010))
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
for i := 1; i <= DefaultHandshakeRetries+2; i++ {
|
|
|
|
next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
|
2019-11-19 10:00:20 -07:00
|
|
|
blah.NextInboundHandshakeTimerTick(next_tick)
|
|
|
|
}
|
|
|
|
|
2020-02-21 14:25:11 -07:00
|
|
|
next_tick := now.Add(DefaultHandshakeTryInterval*DefaultHandshakeRetries + 3)
|
2019-11-19 10:00:20 -07:00
|
|
|
blah.NextInboundHandshakeTimerTick(next_tick)
|
|
|
|
assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
|
2021-03-12 12:16:25 -07:00
|
|
|
assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(hostinfo.localIndexId))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type mockEncWriter struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mw *mockEncWriter) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
|
|
|
|
return
|
|
|
|
}
|