Skip to content

Commit

Permalink
add test for HandshakeManager trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
wadey committed Jun 29, 2020
1 parent 5f0c223 commit 68c015b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
50 changes: 50 additions & 0 deletions handshake_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,56 @@ func Test_NewHandshakeManagerVpnIP(t *testing.T) {
}
}

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)

lh.addrMap = map[uint32][]udpAddr{
ip: {*NewUDPAddrFromString("10.1.1.1:4242")},
}

// 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
}

func Test_NewHandshakeManagerVpnIPcleanup(t *testing.T) {
_, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
_, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
Expand Down
10 changes: 4 additions & 6 deletions lighthouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,12 +361,10 @@ func (lh *LightHouse) HandleRequest(rAddr *udpAddr, vpnIp uint32, p []byte, c *c
ans := NewUDPAddr(a.Ip, uint16(a.Port))
lh.AddRemote(n.Details.VpnIp, ans, false)
}
if lh.handshakeTrigger != nil {
// Non-blocking attempt to trigger, skip if it would block
select {
case lh.handshakeTrigger <- n.Details.VpnIp:
default:
}
// Non-blocking attempt to trigger, skip if it would block
select {
case lh.handshakeTrigger <- n.Details.VpnIp:
default:
}

case NebulaMeta_HostUpdateNotification:
Expand Down

0 comments on commit 68c015b

Please sign in to comment.