connection_manager_test.go 4.6 KB

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