connection_manager_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package nebula
  2. import (
  3. "net"
  4. "testing"
  5. "time"
  6. "github.com/flynn/noise"
  7. "github.com/slackhq/nebula/cert"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. var vpnIP uint32
  11. func Test_NewConnectionManagerTest(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. vpnIP = ip2int(net.ParseIP("172.1.1.2"))
  16. preferredRanges := []*net.IPNet{localrange}
  17. // Very incomplete mock objects
  18. hostMap := NewHostMap("test", vpncidr, preferredRanges)
  19. cs := &CertState{
  20. rawCertificate: []byte{},
  21. privateKey: []byte{},
  22. certificate: &cert.NebulaCertificate{},
  23. rawCertificateNoKey: []byte{},
  24. }
  25. lh := NewLightHouse(false, 0, []uint32{}, 1000, 0, &udpConn{}, false, 1, false)
  26. ifce := &Interface{
  27. hostMap: hostMap,
  28. inside: &Tun{},
  29. outside: &udpConn{},
  30. certState: cs,
  31. firewall: &Firewall{},
  32. lightHouse: lh,
  33. handshakeManager: NewHandshakeManager(vpncidr, preferredRanges, hostMap, lh, &udpConn{}, defaultHandshakeConfig),
  34. }
  35. now := time.Now()
  36. // Create manager
  37. nc := newConnectionManager(ifce, 5, 10)
  38. nc.HandleMonitorTick(now)
  39. // Add an ip we have established a connection w/ to hostmap
  40. hostinfo := nc.hostMap.AddVpnIP(vpnIP)
  41. hostinfo.ConnectionState = &ConnectionState{
  42. certState: cs,
  43. H: &noise.HandshakeState{},
  44. messageCounter: new(uint64),
  45. }
  46. // We saw traffic out to vpnIP
  47. nc.Out(vpnIP)
  48. assert.NotContains(t, nc.pendingDeletion, vpnIP)
  49. assert.Contains(t, nc.hostMap.Hosts, vpnIP)
  50. // Move ahead 5s. Nothing should happen
  51. next_tick := now.Add(5 * time.Second)
  52. nc.HandleMonitorTick(next_tick)
  53. nc.HandleDeletionTick(next_tick)
  54. // Move ahead 6s. We haven't heard back
  55. next_tick = now.Add(6 * time.Second)
  56. nc.HandleMonitorTick(next_tick)
  57. nc.HandleDeletionTick(next_tick)
  58. // This host should now be up for deletion
  59. assert.Contains(t, nc.pendingDeletion, vpnIP)
  60. assert.Contains(t, nc.hostMap.Hosts, vpnIP)
  61. // Move ahead some more
  62. next_tick = now.Add(45 * time.Second)
  63. nc.HandleMonitorTick(next_tick)
  64. nc.HandleDeletionTick(next_tick)
  65. // The host should be evicted
  66. assert.NotContains(t, nc.pendingDeletion, vpnIP)
  67. assert.NotContains(t, nc.hostMap.Hosts, vpnIP)
  68. }
  69. func Test_NewConnectionManagerTest2(t *testing.T) {
  70. //_, tuncidr, _ := net.ParseCIDR("1.1.1.1/24")
  71. _, vpncidr, _ := net.ParseCIDR("172.1.1.1/24")
  72. _, localrange, _ := net.ParseCIDR("10.1.1.1/24")
  73. preferredRanges := []*net.IPNet{localrange}
  74. // Very incomplete mock objects
  75. hostMap := NewHostMap("test", vpncidr, preferredRanges)
  76. cs := &CertState{
  77. rawCertificate: []byte{},
  78. privateKey: []byte{},
  79. certificate: &cert.NebulaCertificate{},
  80. rawCertificateNoKey: []byte{},
  81. }
  82. lh := NewLightHouse(false, 0, []uint32{}, 1000, 0, &udpConn{}, false, 1, false)
  83. ifce := &Interface{
  84. hostMap: hostMap,
  85. inside: &Tun{},
  86. outside: &udpConn{},
  87. certState: cs,
  88. firewall: &Firewall{},
  89. lightHouse: lh,
  90. handshakeManager: NewHandshakeManager(vpncidr, preferredRanges, hostMap, lh, &udpConn{}, defaultHandshakeConfig),
  91. }
  92. now := time.Now()
  93. // Create manager
  94. nc := newConnectionManager(ifce, 5, 10)
  95. nc.HandleMonitorTick(now)
  96. // Add an ip we have established a connection w/ to hostmap
  97. hostinfo := nc.hostMap.AddVpnIP(vpnIP)
  98. hostinfo.ConnectionState = &ConnectionState{
  99. certState: cs,
  100. H: &noise.HandshakeState{},
  101. messageCounter: new(uint64),
  102. }
  103. // We saw traffic out to vpnIP
  104. nc.Out(vpnIP)
  105. assert.NotContains(t, nc.pendingDeletion, vpnIP)
  106. assert.Contains(t, nc.hostMap.Hosts, vpnIP)
  107. // Move ahead 5s. Nothing should happen
  108. next_tick := now.Add(5 * time.Second)
  109. nc.HandleMonitorTick(next_tick)
  110. nc.HandleDeletionTick(next_tick)
  111. // Move ahead 6s. We haven't heard back
  112. next_tick = now.Add(6 * time.Second)
  113. nc.HandleMonitorTick(next_tick)
  114. nc.HandleDeletionTick(next_tick)
  115. // This host should now be up for deletion
  116. assert.Contains(t, nc.pendingDeletion, vpnIP)
  117. assert.Contains(t, nc.hostMap.Hosts, vpnIP)
  118. // We heard back this time
  119. nc.In(vpnIP)
  120. // Move ahead some more
  121. next_tick = now.Add(45 * time.Second)
  122. nc.HandleMonitorTick(next_tick)
  123. nc.HandleDeletionTick(next_tick)
  124. // The host should be evicted
  125. assert.NotContains(t, nc.pendingDeletion, vpnIP)
  126. assert.Contains(t, nc.hostMap.Hosts, vpnIP)
  127. }