helpers_test.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "fmt"
  6. "io"
  7. "net"
  8. "os"
  9. "testing"
  10. "time"
  11. "dario.cat/mergo"
  12. "github.com/google/gopacket"
  13. "github.com/google/gopacket/layers"
  14. "github.com/sirupsen/logrus"
  15. "github.com/slackhq/nebula"
  16. "github.com/slackhq/nebula/cert"
  17. "github.com/slackhq/nebula/config"
  18. "github.com/slackhq/nebula/e2e/router"
  19. "github.com/slackhq/nebula/iputil"
  20. "github.com/stretchr/testify/assert"
  21. "gopkg.in/yaml.v2"
  22. )
  23. type m map[string]interface{}
  24. // newSimpleServer creates a nebula instance with many assumptions
  25. func newSimpleServer(caCrt *cert.NebulaCertificate, caKey []byte, name string, udpIp net.IP, overrides m) (*nebula.Control, *net.IPNet, *net.UDPAddr, *config.C) {
  26. l := NewTestLogger()
  27. vpnIpNet := &net.IPNet{IP: make([]byte, len(udpIp)), Mask: net.IPMask{255, 255, 255, 0}}
  28. copy(vpnIpNet.IP, udpIp)
  29. vpnIpNet.IP[1] += 128
  30. udpAddr := net.UDPAddr{
  31. IP: udpIp,
  32. Port: 4242,
  33. }
  34. _, _, myPrivKey, myPEM := NewTestCert(caCrt, caKey, name, time.Now(), time.Now().Add(5*time.Minute), vpnIpNet, nil, []string{})
  35. caB, err := caCrt.MarshalToPEM()
  36. if err != nil {
  37. panic(err)
  38. }
  39. mc := m{
  40. "pki": m{
  41. "ca": string(caB),
  42. "cert": string(myPEM),
  43. "key": string(myPrivKey),
  44. },
  45. //"tun": m{"disabled": true},
  46. "firewall": m{
  47. "outbound": []m{{
  48. "proto": "any",
  49. "port": "any",
  50. "host": "any",
  51. }},
  52. "inbound": []m{{
  53. "proto": "any",
  54. "port": "any",
  55. "host": "any",
  56. }},
  57. },
  58. //"handshakes": m{
  59. // "try_interval": "1s",
  60. //},
  61. "listen": m{
  62. "host": udpAddr.IP.String(),
  63. "port": udpAddr.Port,
  64. },
  65. "logging": m{
  66. "timestamp_format": fmt.Sprintf("%v 15:04:05.000000", name),
  67. "level": l.Level.String(),
  68. },
  69. "timers": m{
  70. "pending_deletion_interval": 2,
  71. "connection_alive_interval": 2,
  72. },
  73. }
  74. if overrides != nil {
  75. err = mergo.Merge(&overrides, mc, mergo.WithAppendSlice)
  76. if err != nil {
  77. panic(err)
  78. }
  79. mc = overrides
  80. }
  81. cb, err := yaml.Marshal(mc)
  82. if err != nil {
  83. panic(err)
  84. }
  85. c := config.NewC(l)
  86. c.LoadString(string(cb))
  87. control, err := nebula.Main(c, false, "e2e-test", l, nil)
  88. if err != nil {
  89. panic(err)
  90. }
  91. return control, vpnIpNet, &udpAddr, c
  92. }
  93. type doneCb func()
  94. func deadline(t *testing.T, seconds time.Duration) doneCb {
  95. timeout := time.After(seconds * time.Second)
  96. done := make(chan bool)
  97. go func() {
  98. select {
  99. case <-timeout:
  100. t.Fatal("Test did not finish in time")
  101. case <-done:
  102. }
  103. }()
  104. return func() {
  105. done <- true
  106. }
  107. }
  108. func assertTunnel(t *testing.T, vpnIpA, vpnIpB net.IP, controlA, controlB *nebula.Control, r *router.R) {
  109. // Send a packet from them to me
  110. controlB.InjectTunUDPPacket(vpnIpA, 80, 90, []byte("Hi from B"))
  111. bPacket := r.RouteForAllUntilTxTun(controlA)
  112. assertUdpPacket(t, []byte("Hi from B"), bPacket, vpnIpB, vpnIpA, 90, 80)
  113. // And once more from me to them
  114. controlA.InjectTunUDPPacket(vpnIpB, 80, 90, []byte("Hello from A"))
  115. aPacket := r.RouteForAllUntilTxTun(controlB)
  116. assertUdpPacket(t, []byte("Hello from A"), aPacket, vpnIpA, vpnIpB, 90, 80)
  117. }
  118. func assertHostInfoPair(t *testing.T, addrA, addrB *net.UDPAddr, vpnIpA, vpnIpB net.IP, controlA, controlB *nebula.Control) {
  119. // Get both host infos
  120. hBinA := controlA.GetHostInfoByVpnIp(iputil.Ip2VpnIp(vpnIpB), false)
  121. assert.NotNil(t, hBinA, "Host B was not found by vpnIp in controlA")
  122. hAinB := controlB.GetHostInfoByVpnIp(iputil.Ip2VpnIp(vpnIpA), false)
  123. assert.NotNil(t, hAinB, "Host A was not found by vpnIp in controlB")
  124. // Check that both vpn and real addr are correct
  125. assert.Equal(t, vpnIpB, hBinA.VpnIp, "Host B VpnIp is wrong in control A")
  126. assert.Equal(t, vpnIpA, hAinB.VpnIp, "Host A VpnIp is wrong in control B")
  127. assert.Equal(t, addrB.IP.To16(), hBinA.CurrentRemote.IP.To16(), "Host B remote ip is wrong in control A")
  128. assert.Equal(t, addrA.IP.To16(), hAinB.CurrentRemote.IP.To16(), "Host A remote ip is wrong in control B")
  129. assert.Equal(t, addrB.Port, int(hBinA.CurrentRemote.Port), "Host B remote port is wrong in control A")
  130. assert.Equal(t, addrA.Port, int(hAinB.CurrentRemote.Port), "Host A remote port is wrong in control B")
  131. // Check that our indexes match
  132. assert.Equal(t, hBinA.LocalIndex, hAinB.RemoteIndex, "Host B local index does not match host A remote index")
  133. assert.Equal(t, hBinA.RemoteIndex, hAinB.LocalIndex, "Host B remote index does not match host A local index")
  134. //TODO: Would be nice to assert this memory
  135. //checkIndexes := func(name string, hm *HostMap, hi *HostInfo) {
  136. // hBbyIndex := hmA.Indexes[hBinA.localIndexId]
  137. // assert.NotNil(t, hBbyIndex, "Could not host info by local index in %s", name)
  138. // assert.Equal(t, &hBbyIndex, &hBinA, "%s Indexes map did not point to the right host info", name)
  139. //
  140. // //TODO: remote indexes are susceptible to collision
  141. // hBbyRemoteIndex := hmA.RemoteIndexes[hBinA.remoteIndexId]
  142. // assert.NotNil(t, hBbyIndex, "Could not host info by remote index in %s", name)
  143. // assert.Equal(t, &hBbyRemoteIndex, &hBinA, "%s RemoteIndexes did not point to the right host info", name)
  144. //}
  145. //
  146. //// Check hostmap indexes too
  147. //checkIndexes("hmA", hmA, hBinA)
  148. //checkIndexes("hmB", hmB, hAinB)
  149. }
  150. func assertUdpPacket(t *testing.T, expected, b []byte, fromIp, toIp net.IP, fromPort, toPort uint16) {
  151. packet := gopacket.NewPacket(b, layers.LayerTypeIPv4, gopacket.Lazy)
  152. v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
  153. assert.NotNil(t, v4, "No ipv4 data found")
  154. assert.Equal(t, fromIp, v4.SrcIP, "Source ip was incorrect")
  155. assert.Equal(t, toIp, v4.DstIP, "Dest ip was incorrect")
  156. udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
  157. assert.NotNil(t, udp, "No udp data found")
  158. assert.Equal(t, fromPort, uint16(udp.SrcPort), "Source port was incorrect")
  159. assert.Equal(t, toPort, uint16(udp.DstPort), "Dest port was incorrect")
  160. data := packet.ApplicationLayer()
  161. assert.NotNil(t, data)
  162. assert.Equal(t, expected, data.Payload(), "Data was incorrect")
  163. }
  164. func NewTestLogger() *logrus.Logger {
  165. l := logrus.New()
  166. v := os.Getenv("TEST_LOGS")
  167. if v == "" {
  168. l.SetOutput(io.Discard)
  169. l.SetLevel(logrus.PanicLevel)
  170. return l
  171. }
  172. switch v {
  173. case "2":
  174. l.SetLevel(logrus.DebugLevel)
  175. case "3":
  176. l.SetLevel(logrus.TraceLevel)
  177. default:
  178. l.SetLevel(logrus.InfoLevel)
  179. }
  180. return l
  181. }