handshake_manager_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 = []uint32{9000}
  11. func Test_NewHandshakeManagerIndex(t *testing.T) {
  12. _, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  13. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  14. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  15. preferredRanges := []*net.IPNet{localrange}
  16. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  17. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{})
  18. now := time.Now()
  19. blah.NextInboundHandshakeTimerTick(now)
  20. // Add four indexes
  21. for _, v := range indexes {
  22. blah.AddIndex(v, &ConnectionState{})
  23. }
  24. // Confirm they are in the pending index list
  25. for _, v := range indexes {
  26. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
  27. }
  28. // Adding something to pending should not affect the main hostmap
  29. assert.Len(t, mainHM.Indexes, 0)
  30. // Jump ahead 8 seconds
  31. for i := 1; i <= HandshakeRetries; i++ {
  32. next_tick := now.Add(HandshakeTryInterval * time.Duration(i))
  33. blah.NextInboundHandshakeTimerTick(next_tick)
  34. }
  35. // Confirm they are still in the pending index list
  36. for _, v := range indexes {
  37. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(v))
  38. }
  39. // Jump ahead 4 more seconds
  40. next_tick := now.Add(12 * time.Second)
  41. blah.NextInboundHandshakeTimerTick(next_tick)
  42. // Confirm they have been removed
  43. for _, v := range indexes {
  44. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(v))
  45. }
  46. }
  47. func Test_NewHandshakeManagerVpnIP(t *testing.T) {
  48. _, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  49. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  50. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  51. preferredRanges := []*net.IPNet{localrange}
  52. mw := &mockEncWriter{}
  53. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  54. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{})
  55. now := time.Now()
  56. blah.NextOutboundHandshakeTimerTick(now, mw)
  57. // Add four "IPs" - which are just uint32s
  58. for _, v := range ips {
  59. blah.AddVpnIP(v)
  60. }
  61. // Adding something to pending should not affect the main hostmap
  62. assert.Len(t, mainHM.Hosts, 0)
  63. // Confirm they are in the pending index list
  64. for _, v := range ips {
  65. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
  66. }
  67. // Jump ahead `HandshakeRetries` ticks
  68. cumulative := time.Duration(0)
  69. for i := 0; i <= HandshakeRetries+1; i++ {
  70. cumulative += time.Duration(i)*HandshakeTryInterval + 1
  71. next_tick := now.Add(cumulative)
  72. //l.Infoln(next_tick)
  73. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  74. }
  75. // Confirm they are still in the pending index list
  76. for _, v := range ips {
  77. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(v))
  78. }
  79. // Jump ahead 1 more second
  80. cumulative += time.Duration(HandshakeRetries+1) * HandshakeTryInterval
  81. next_tick := now.Add(cumulative)
  82. //l.Infoln(next_tick)
  83. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  84. // Confirm they have been removed
  85. for _, v := range ips {
  86. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(v))
  87. }
  88. }
  89. func Test_NewHandshakeManagerVpnIPcleanup(t *testing.T) {
  90. _, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  91. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  92. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  93. preferredRanges := []*net.IPNet{localrange}
  94. mw := &mockEncWriter{}
  95. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  96. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{})
  97. now := time.Now()
  98. blah.NextOutboundHandshakeTimerTick(now, mw)
  99. hostinfo := blah.AddVpnIP(101010)
  100. // Pretned we have an index too
  101. blah.AddIndexHostInfo(12341234, hostinfo)
  102. assert.Contains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  103. // Jump ahead `HandshakeRetries` ticks. Eviction should happen in pending
  104. // but not main hostmap
  105. cumulative := time.Duration(0)
  106. for i := 1; i <= HandshakeRetries+2; i++ {
  107. cumulative += HandshakeTryInterval * time.Duration(i)
  108. next_tick := now.Add(cumulative)
  109. blah.NextOutboundHandshakeTimerTick(next_tick, mw)
  110. }
  111. /*
  112. for i := 0; i <= HandshakeRetries+1; i++ {
  113. next_tick := now.Add(cumulative)
  114. //l.Infoln(next_tick)
  115. blah.NextOutboundHandshakeTimerTick(next_tick)
  116. }
  117. */
  118. /*
  119. for i := 0; i <= HandshakeRetries+1; i++ {
  120. next_tick := now.Add(time.Duration(i) * time.Second)
  121. blah.NextOutboundHandshakeTimerTick(next_tick)
  122. }
  123. */
  124. /*
  125. cumulative += HandshakeTryInterval*time.Duration(HandshakeRetries) + 3
  126. next_tick := now.Add(cumulative)
  127. l.Infoln(cumulative, next_tick)
  128. blah.NextOutboundHandshakeTimerTick(next_tick)
  129. */
  130. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
  131. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  132. }
  133. func Test_NewHandshakeManagerIndexcleanup(t *testing.T) {
  134. _, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  135. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  136. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  137. preferredRanges := []*net.IPNet{localrange}
  138. mainHM := NewHostMap("test", vpncidr, preferredRanges)
  139. blah := NewHandshakeManager(tuncidr, preferredRanges, mainHM, &LightHouse{}, &udpConn{})
  140. now := time.Now()
  141. blah.NextInboundHandshakeTimerTick(now)
  142. hostinfo, _ := blah.AddIndex(12341234, &ConnectionState{})
  143. // Pretned we have an index too
  144. blah.pendingHostMap.AddVpnIPHostInfo(101010, hostinfo)
  145. assert.Contains(t, blah.pendingHostMap.Hosts, uint32(101010))
  146. for i := 1; i <= HandshakeRetries+2; i++ {
  147. next_tick := now.Add(HandshakeTryInterval * time.Duration(i))
  148. blah.NextInboundHandshakeTimerTick(next_tick)
  149. }
  150. next_tick := now.Add(HandshakeTryInterval*HandshakeRetries + 3)
  151. blah.NextInboundHandshakeTimerTick(next_tick)
  152. assert.NotContains(t, blah.pendingHostMap.Hosts, uint32(101010))
  153. assert.NotContains(t, blah.pendingHostMap.Indexes, uint32(12341234))
  154. }
  155. type mockEncWriter struct {
  156. }
  157. func (mw *mockEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  158. return
  159. }
  160. func (mw *mockEncWriter) SendMessageToAll(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, nb, out []byte) {
  161. return
  162. }