handshakes_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // +build e2e_testing
  2. package e2e
  3. import (
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula/e2e/router"
  8. )
  9. func TestGoodHandshake(t *testing.T) {
  10. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  11. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1})
  12. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  13. // Put their info in our lighthouse
  14. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  15. // Start the servers
  16. myControl.Start()
  17. theirControl.Start()
  18. // Send a udp packet through to begin standing up the tunnel, this should come out the other side
  19. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  20. // Have them consume my stage 0 packet. They have a tunnel now
  21. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  22. // Have me consume their stage 1 packet. I have a tunnel now
  23. myControl.InjectUDPPacket(theirControl.GetFromUDP(true))
  24. // Wait until we see my cached packet come through
  25. myControl.WaitForType(1, 0, theirControl)
  26. // Make sure our host infos are correct
  27. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  28. // Get that cached packet and make sure it looks right
  29. myCachedPacket := theirControl.GetFromTun(true)
  30. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  31. // Do a bidirectional tunnel test
  32. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, router.NewR(myControl, theirControl))
  33. myControl.Stop()
  34. theirControl.Stop()
  35. //TODO: assert hostmaps
  36. }
  37. func TestWrongResponderHandshake(t *testing.T) {
  38. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  39. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1})
  40. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  41. evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 99})
  42. // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse
  43. myControl.InjectLightHouseAddr(theirVpnIp, evilUdpAddr)
  44. // But also add their real udp addr, which should be tried after evil
  45. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  46. // Build a router so we don't have to reason who gets which packet
  47. r := router.NewR(myControl, theirControl, evilControl)
  48. // Start the servers
  49. myControl.Start()
  50. theirControl.Start()
  51. evilControl.Start()
  52. t.Log("Stand up the tunnel with evil (because the lighthouse cache is lying to us about who it is)")
  53. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  54. r.OnceFrom(myControl)
  55. r.OnceFrom(evilControl)
  56. t.Log("I should have a tunnel with evil now and there should not be a cached packet waiting for us")
  57. assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  58. assertHostInfoPair(t, myUdpAddr, evilUdpAddr, myVpnIp, evilVpnIp, myControl, evilControl)
  59. //TODO: Assert pending hostmap - I should have a correct hostinfo for them now
  60. t.Log("Lets let the messages fly, this time we should have a tunnel with them")
  61. r.OnceFrom(myControl)
  62. r.OnceFrom(theirControl)
  63. t.Log("I should now have a tunnel with them now and my original packet should get there")
  64. r.RouteUntilAfterMsgType(myControl, 1, 0)
  65. myCachedPacket := theirControl.GetFromTun(true)
  66. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  67. t.Log("I should now have a proper tunnel with them")
  68. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  69. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  70. t.Log("Lets make sure evil is still good")
  71. assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  72. //TODO: assert hostmaps for everyone
  73. t.Log("Success!")
  74. //TODO: myControl is attempting to shut down 2 tunnels but is blocked on the udp txChan after the first close message
  75. // what we really need here is a way to exit all the go routines loops (there are many)
  76. //myControl.Stop()
  77. //theirControl.Stop()
  78. }
  79. ////TODO: We need to test lies both as the race winner and race loser
  80. //func TestManyWrongResponderHandshake(t *testing.T) {
  81. // ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  82. //
  83. // myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 99})
  84. // theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  85. // evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 1})
  86. //
  87. // t.Log("Build a router so we don't have to reason who gets which packet")
  88. // r := newRouter(myControl, theirControl, evilControl)
  89. //
  90. // t.Log("Lets add more than 10 evil addresses, this exceeds the hostinfo remotes limit")
  91. // for i := 0; i < 10; i++ {
  92. // addr := net.UDPAddr{IP: evilUdpAddr.IP, Port: evilUdpAddr.Port + i}
  93. // myControl.InjectLightHouseAddr(theirVpnIp, &addr)
  94. // // We also need to tell our router about it
  95. // r.AddRoute(addr.IP, uint16(addr.Port), evilControl)
  96. // }
  97. //
  98. // // Start the servers
  99. // myControl.Start()
  100. // theirControl.Start()
  101. // evilControl.Start()
  102. //
  103. // t.Log("Stand up the tunnel with evil (because the lighthouse cache is lying to us about who it is)")
  104. // myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  105. //
  106. // t.Log("We need to spin until we get to the right remote for them")
  107. // getOut := false
  108. // injected := false
  109. // for {
  110. // t.Log("Routing for me and evil while we work through the bad ips")
  111. // r.RouteExitFunc(myControl, func(packet *nebula.UdpPacket, receiver *nebula.Control) exitType {
  112. // // We should stop routing right after we see a packet coming from us to them
  113. // if *receiver == *theirControl {
  114. // getOut = true
  115. // return drainAndExit
  116. // }
  117. //
  118. // // We need to poke our real ip in at some point, this is a well protected check looking for that moment
  119. // if *receiver == *evilControl {
  120. // hi := myControl.GetHostInfoByVpnIP(ip2int(theirVpnIp), true)
  121. // if !injected && len(hi.RemoteAddrs) == 1 {
  122. // t.Log("I am on my last ip for them, time to inject the real one into my lighthouse")
  123. // myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  124. // injected = true
  125. // }
  126. // return drainAndExit
  127. // }
  128. //
  129. // return keepRouting
  130. // })
  131. //
  132. // if getOut {
  133. // break
  134. // }
  135. //
  136. // r.RouteForUntilAfterToAddr(evilControl, myUdpAddr, drainAndExit)
  137. // }
  138. //
  139. // t.Log("I should have a tunnel with evil and them, evil should not have a cached packet")
  140. // assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  141. // evilHostInfo := myControl.GetHostInfoByVpnIP(ip2int(evilVpnIp), false)
  142. // realEvilUdpAddr := &net.UDPAddr{IP: evilHostInfo.CurrentRemote.IP, Port: int(evilHostInfo.CurrentRemote.Port)}
  143. //
  144. // t.Log("Assert mine and evil's host pairs", evilUdpAddr, realEvilUdpAddr)
  145. // assertHostInfoPair(t, myUdpAddr, realEvilUdpAddr, myVpnIp, evilVpnIp, myControl, evilControl)
  146. //
  147. // //t.Log("Draining everyones packets")
  148. // //r.Drain(theirControl)
  149. // //r.DrainAll(myControl, theirControl, evilControl)
  150. // //
  151. // //go func() {
  152. // // for {
  153. // // time.Sleep(10 * time.Millisecond)
  154. // // t.Log(len(theirControl.GetUDPTxChan()))
  155. // // t.Log(len(theirControl.GetTunTxChan()))
  156. // // t.Log(len(myControl.GetUDPTxChan()))
  157. // // t.Log(len(evilControl.GetUDPTxChan()))
  158. // // t.Log("=====")
  159. // // }
  160. // //}()
  161. //
  162. // t.Log("I should have a tunnel with them now and my original packet should get there")
  163. // r.RouteUntilAfterMsgType(myControl, 1, 0)
  164. // myCachedPacket := theirControl.GetFromTun(true)
  165. //
  166. // t.Log("Got the cached packet, lets test the tunnel")
  167. // assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  168. //
  169. // t.Log("Testing tunnels with them")
  170. // assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  171. // assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  172. //
  173. // t.Log("Testing tunnels with evil")
  174. // assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  175. //
  176. // //TODO: assert hostmaps for everyone
  177. //}