handshake_manager_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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_NewHandshakeManagerVpnIPcleanup(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. vpnIP = ip2int(net.ParseIP("172.1.1.2"))
  96. preferredRanges := []*net.IPNet{localrange}
  97. mw := &mockEncWriter{}
  98. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  99. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  100. now := time.Now()
  101. blah.NextOutboundHandshakeTimerTick(now, mw)
  102. hostinfo := blah.AddVpnIP(vpnIP)
  103. // Pretned we have an index too
  104. blah.AddIndexHostInfo(12341234, hostinfo)
  105. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  106. // Jump ahead `HandshakeRetries` ticks. Eviction should happen in pending
  107. // but not main hostmap
  108. cumulative := time.Duration(0)
  109. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  110. cumulative += DefaultHandshakeTryInterval * time.Duration(i)
  111. next_tick := now.Add(cumulative)
  112. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  113. }
  114. /*
  115. for i := 0; i <= HandshakeRetries+1; i++ {
  116. next_tick := now.Add(cumulative)
  117. //l.Infoln(next_tick)
  118. blah.NextOutboundHandshakeTimerTick(next_tick)
  119. }
  120. */
  121. /*
  122. for i := 0; i <= HandshakeRetries+1; i++ {
  123. next_tick := now.Add(time.Duration(i) * time.Second)
  124. blah.NextOutboundHandshakeTimerTick(next_tick)
  125. }
  126. */
  127. /*
  128. cumulative += HandshakeTryInterval*time.Duration(HandshakeRetries) + 3
  129. next_tick := now.Add(cumulative)
  130. l.Infoln(cumulative, next_tick)
  131. blah.NextOutboundHandshakeTimerTick(next_tick)
  132. */
  133. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(vpnIP))
  134. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  135. }
  136. func Test_NewHandshakeManagerIndexcleanup(t *testing.T) {
  137. _, tuncidr, _ := net.ParseCIDR("172.1.1.1/24")
  138. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  139. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  140. preferredRanges := []*net.IPNet{localrange}
  141. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  142. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{}, defaultHandshakeConfig)
  143. now := time.Now()
  144. blah.NextInboundHandshakeTimerTick(now)
  145. hostinfo, _ := blah.AddIndex(12341234, &ConnectionState{})
  146. // Pretned we have an index too
  147. blah.pendingHostMap.AddVpnIPHostInfo(101010, hostinfo)
  148. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(101010))
  149. for i := 1; i <= DefaultHandshakeRetries+2; i++ {
  150. next_tick := now.Add(DefaultHandshakeTryInterval * time.Duration(i))
  151. blah.NextInboundHandshakeTimerTick(next_tick)
  152. }
  153. next_tick := now.Add(DefaultHandshakeTryInterval*DefaultHandshakeRetries + 3)
  154. blah.NextInboundHandshakeTimerTick(next_tick)
  155. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
  156. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  157. }
  158. type mockEncWriter struct {
  159. }
  160. func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  161. return
  162. }
  163. func (mw *mockEncWriter) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  164. return
  165. }