handshakes_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // +build e2e_testing
  2. package e2e
  3. import (
  4. "net"
  5. "testing"
  6. "time"
  7. )
  8. func TestGoodHandshake(t *testing.T) {
  9. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  10. defMask := net.IPMask{0, 0, 0, 0}
  11. myUdpAddr := &net.UDPAddr{IP: net.IP{10, 0, 0, 1}, Port: 4242}
  12. myVpnIpNet := &net.IPNet{IP: net.IP{10, 128, 0, 1}, Mask: defMask}
  13. myControl := newSimpleServer(ca, caKey, "me", myUdpAddr, myVpnIpNet)
  14. theirUdpAddr := &net.UDPAddr{IP: net.IP{10, 0, 0, 2}, Port: 4242}
  15. theirVpnIpNet := &net.IPNet{IP: net.IP{10, 128, 0, 2}, Mask: defMask}
  16. theirControl := newSimpleServer(ca, caKey, "them", theirUdpAddr, theirVpnIpNet)
  17. // Put their info in our lighthouse
  18. myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
  19. // Start the servers
  20. myControl.Start()
  21. theirControl.Start()
  22. // Send a udp packet through to begin standing up the tunnel, this should come out the other side
  23. myControl.InjectTunUDPPacket(theirVpnIpNet.IP, 80, 80, []byte("Hi from me"))
  24. // Have them consume my stage 0 packet. They have a tunnel now
  25. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  26. // Have me consume their stage 1 packet. I have a tunnel now
  27. myControl.InjectUDPPacket(theirControl.GetFromUDP(true))
  28. // Wait until we see my cached packet come through
  29. myControl.WaitForType(1, 0, theirControl)
  30. // Make sure our host infos are correct
  31. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl)
  32. // Get that cached packet and make sure it looks right
  33. myCachedPacket := theirControl.GetFromTun(true)
  34. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet.IP, theirVpnIpNet.IP, 80, 80)
  35. // Send a packet from them to me
  36. theirControl.InjectTunUDPPacket(myVpnIpNet.IP, 80, 80, []byte("Hi from them"))
  37. myControl.InjectUDPPacket(theirControl.GetFromUDP(true))
  38. theirPacket := myControl.GetFromTun(true)
  39. assertUdpPacket(t, []byte("Hi from them"), theirPacket, theirVpnIpNet.IP, myVpnIpNet.IP, 80, 80)
  40. // And once more from me to them
  41. myControl.InjectTunUDPPacket(theirVpnIpNet.IP, 80, 80, []byte("Hello again from me"))
  42. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  43. myPacket := theirControl.GetFromTun(true)
  44. assertUdpPacket(t, []byte("Hello again from me"), myPacket, myVpnIpNet.IP, theirVpnIpNet.IP, 80, 80)
  45. }