handshake_manager_test.go 8.6 KB

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