handshake_manager_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/stretchr/testify/assert"
  7. )
  8. var indexes []uint32 = []uint32{1000, 2000, 3000, 4000}
  9. //var ips []uint32 = []uint32{9000, 9999999, 3, 292394923}
  10. var ips []uint32
  11. func Test_NewHandshakeManagerIndex(t *testing.T) {
  12. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  13. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  14. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  15. ips = []uint32{ip2int(net.ParseIP("172.1.1.2"))}
  16. preferredRanges := []*net.IPNet{localrange}
  17. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  18. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  19. now := time.Now()
  20. blah.NextInboundHandshakeTimerTick(now)
  21. // Add four indexes
  22. for _, v := range indexes {
  23. blah.AddIndex(v, &ConnectionState{})
  24. }
  25. // Confirm they are in the pending index list
  26. for _, v := range indexes {
  27. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
  28. }
  29. // Adding something to pending should not affect the main hostmap
  30. assert.Len(t, mainHM.Indexes, 0)
  31. // Jump ahead 8 seconds
  32. for i := 1; i <= DefaultHandshakeRetries; i++ {
  33. next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
  34. blah.NextInboundHandshakeTimerTick(next_tick)
  35. }
  36. // Confirm they are still in the pending index list
  37. for _, v := range indexes {
  38. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
  39. }
  40. // Jump ahead 4 more seconds
  41. next_tick := now.Add(12 * time.Second)
  42. blah.NextInboundHandshakeTimerTick(next_tick)
  43. // Confirm they have been removed
  44. for _, v := range indexes {
  45. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(v))
  46. }
  47. }
  48. func Test_NewHandshakeManagerVpnIP(t *testing.T) {
  49. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  50. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  51. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  52. ips = []uint32{ip2int(net.ParseIP("172.1.1.2"))}
  53. preferredRanges := []*net.IPNet{localrange}
  54. mw := &mockEncWriter{}
  55. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  56. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  57. now := time.Now()
  58. blah.NextOutboundHandshakeTimerTick(now, mw)
  59. // Add four "IPs" - which are just uint32s
  60. for _, v := range ips {
  61. blah.AddVpnIP(v)
  62. }
  63. // Adding something to pending should not affect the main hostmap
  64. assert.Len(t, mainHM.Hosts, 0)
  65. // Confirm they are in the pending index list
  66. for _, v := range ips {
  67. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
  68. }
  69. // Jump ahead `HandshakeRetries` ticks
  70. cumulative := time.Duration(0)
  71. for i := 0; i <= DefaultHandshakeRetries+1; i++ {
  72. cumulative += time.Duration(i)*DefaultHandshakeTryInterval + 1
  73. next_tick := now.Add(cumulative)
  74. //l.Infoln(next_tick)
  75. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  76. }
  77. // Confirm they are still in the pending index list
  78. for _, v := range ips {
  79. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
  80. }
  81. // Jump ahead 1 more second
  82. cumulative += time.Duration(DefaultHandshakeRetries+1) * DefaultHandshakeTryInterval
  83. next_tick := now.Add(cumulative)
  84. //l.Infoln(next_tick)
  85. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  86. // Confirm they have been removed
  87. for _, v := range ips {
  88. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(v))
  89. }
  90. }
  91. func Test_NewHandshakeManagerTrigger(t *testing.T) {
  92. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  93. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  94. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  95. ip := ip2int(net.ParseIP("172.1.1.2"))
  96. preferredRanges := []*net.IPNet{localrange}
  97. mw := &mockEncWriter{}
  98. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  99. lh := &LightHouse{}
  100. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, lh, &udpConn{}, defaultHandshakeConfig)
  101. now := time.Now()
  102. blah.NextOutboundHandshakeTimerTick(now, mw)
  103. assert.Equal(t, 0, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  104. blah.AddVpnIP(ip)
  105. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  106. // Trigger the same method the channel will
  107. blah.handleOutbound(ip, mw, true)
  108. // Make sure the trigger doesn't schedule another timer entry
  109. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  110. hi := blah.pendingHostMap.Hosts[ip]
  111. assert.Nil(t, hi.remote)
  112. lh.addrMap = map[uint32][]udpAddr{
  113. ip: {*NewUDPAddrFromString("10.1.1.1:4242")},
  114. }
  115. // This should trigger the hostmap to populate the hostinfo
  116. blah.handleOutbound(ip, mw, true)
  117. assert.NotNil(t, hi.remote)
  118. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  119. }
  120. func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
  121. for _, i := range tw.wheel {
  122. n := i.Head
  123. for n != nil {
  124. c++
  125. n = n.Next
  126. }
  127. }
  128. return c
  129. }
  130. func Test_NewHandshakeManagerVpnIPcleanup(t *testing.T) {
  131. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  132. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  133. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  134. vpnIP = ip2int(net.ParseIP("172.1.1.2"))
  135. preferredRanges := []*net.IPNet{localrange}
  136. mw := &mockEncWriter{}
  137. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  138. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  139. now := time.Now()
  140. blah.NextOutboundHandshakeTimerTick(now, mw)
  141. hostinfo := blah.AddVpnIP(vpnIP)
  142. // Pretned we have an index too
  143. blah.AddIndexHostInfo(12341234, hostinfo)
  144. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  145. // Jump ahead `HandshakeRetries` ticks. Eviction should happen in pending
  146. // but not main hostmap
  147. cumulative := time.Duration(0)
  148. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  149. cumulative += DefaultHandshakeTryInterval * time.Duration(i)
  150. next_tick := now.Add(cumulative)
  151. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  152. }
  153. /*
  154. for i := 0; i <= HandshakeRetries+1; i++ {
  155. next_tick := now.Add(cumulative)
  156. //l.Infoln(next_tick)
  157. blah.NextOutboundHandshakeTimerTick(next_tick)
  158. }
  159. */
  160. /*
  161. for i := 0; i <= HandshakeRetries+1; i++ {
  162. next_tick := now.Add(time.Duration(i) * time.Second)
  163. blah.NextOutboundHandshakeTimerTick(next_tick)
  164. }
  165. */
  166. /*
  167. cumulative += HandshakeTryInterval*time.Duration(HandshakeRetries) + 3
  168. next_tick := now.Add(cumulative)
  169. l.Infoln(cumulative, next_tick)
  170. blah.NextOutboundHandshakeTimerTick(next_tick)
  171. */
  172. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(vpnIP))
  173. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  174. }
  175. func Test_NewHandshakeManagerIndexcleanup(t *testing.T) {
  176. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  177. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  178. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  179. preferredRanges := []*net.IPNet{localrange}
  180. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  181. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  182. now := time.Now()
  183. blah.NextInboundHandshakeTimerTick(now)
  184. hostinfo, _ := blah.AddIndex(12341234, &ConnectionState{})
  185. // Pretned we have an index too
  186. blah.pendingHostMap.AddVpnIPHostInfo(101010, hostinfo)
  187. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(101010))
  188. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  189. next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
  190. blah.NextInboundHandshakeTimerTick(next_tick)
  191. }
  192. next_tick := now.Add(DefaultHandshakeTryInterval*DefaultHandshakeRetries + 3)
  193. blah.NextInboundHandshakeTimerTick(next_tick)
  194. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
  195. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  196. }
  197. type mockEncWriter struct {
  198. }
  199. func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  200. return
  201. }
  202. func (mw *mockEncWriter) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  203. return
  204. }