connection_manager_test.go 4.2 KB

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