connection_manager_test.go 4.5 KB

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