connection_manager_test.go 4.6 KB

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