3
0

handshake_manager_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. lh.addrMap = map[uint32][]*udpAddr{
  123. ip: {NewUDPAddrFromString("10.1.1.1:4242")},
  124. }
  125. // This should trigger the hostmap to populate the hostinfo
  126. blah.handleOutbound(ip, mw, true)
  127. assert.NotNil(t, hi.remote)
  128. assert.Equal(t, 1, testCountTimerWheelEntries(blah.OutboundHandshakeTimer))
  129. }
  130. func testCountTimerWheelEntries(tw *SystemTimerWheel) (c int) {
  131. for _, i := range tw.wheel {
  132. n := i.Head
  133. for n != nil {
  134. c++
  135. n = n.Next
  136. }
  137. }
  138. return c
  139. }
  140. func Test_NewHandshakeManagerVpnIPcleanup(t *testing.T) {
  141. l := NewTestLogger()
  142. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  143. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  144. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  145. vpnIP = ip2int(net.ParseIP("172.1.1.2"))
  146. preferredRanges := []*net.IPNet{localrange}
  147. mw := &mockEncWriter{}
  148. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  149. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  150. now := time.Now()
  151. blah.NextOutboundHandshakeTimerTick(now, mw)
  152. hostinfo := blah.AddVpnIP(vpnIP)
  153. // Pretned we have an index too
  154. err := blah.AddIndexHostInfo(hostinfo)
  155. assert.NoError(t, err)
  156. blah.InboundHandshakeTimer.Add(hostinfo.localIndexId, time.Second*10)
  157. assert.NotZero(t, hostinfo.localIndexId)
  158. assert.Contains(t, blah.pendingHostMap.Indexes, hostinfo.localIndexId)
  159. // Jump ahead `HandshakeRetries` ticks. Eviction should happen in pending
  160. // but not main hostmap
  161. cumulative := time.Duration(0)
  162. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  163. cumulative += DefaultHandshakeTryInterval * time.Duration(i)
  164. next_tick := now.Add(cumulative)
  165. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  166. }
  167. /*
  168. for i := 0; i <= HandshakeRetries+1; i++ {
  169. next_tick := now.Add(cumulative)
  170. //l.Infoln(next_tick)
  171. blah.NextOutboundHandshakeTimerTick(next_tick)
  172. }
  173. */
  174. /*
  175. for i := 0; i <= HandshakeRetries+1; i++ {
  176. next_tick := now.Add(time.Duration(i) * time.Second)
  177. blah.NextOutboundHandshakeTimerTick(next_tick)
  178. }
  179. */
  180. /*
  181. cumulative += HandshakeTryInterval*time.Duration(HandshakeRetries) + 3
  182. next_tick := now.Add(cumulative)
  183. l.Infoln(cumulative, next_tick)
  184. blah.NextOutboundHandshakeTimerTick(next_tick)
  185. */
  186. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(vpnIP))
  187. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  188. }
  189. func Test_NewHandshakeManagerIndexcleanup(t *testing.T) {
  190. l := NewTestLogger()
  191. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  192. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  193. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  194. preferredRanges := []*net.IPNet{localrange}
  195. mainHM := NewHostMap(l, "test", vpncidr, preferredRanges)
  196. blah := NewHandshakeManager(l, tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  197. now := time.Now()
  198. blah.NextInboundHandshakeTimerTick(now)
  199. hostinfo := &HostInfo{ConnectionState: &ConnectionState{}}
  200. err := blah.AddIndexHostInfo(hostinfo)
  201. assert.NoError(t, err)
  202. blah.InboundHandshakeTimer.Add(hostinfo.localIndexId, time.Second*10)
  203. // Pretned we have an index too
  204. blah.pendingHostMap.AddVpnIPHostInfo(101010, hostinfo)
  205. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(101010))
  206. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  207. next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
  208. blah.NextInboundHandshakeTimerTick(next_tick)
  209. }
  210. next_tick := now.Add(DefaultHandshakeTryInterval*DefaultHandshakeRetries + 3)
  211. blah.NextInboundHandshakeTimerTick(next_tick)
  212. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
  213. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(hostinfo.localIndexId))
  214. }
  215. type mockEncWriter struct {
  216. }
  217. func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  218. return
  219. }
  220. func (mw *mockEncWriter) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  221. return
  222. }