1
0

echo_test.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/cert_test"
  10. "github.com/slackhq/nebula/e2e/router"
  11. "github.com/slackhq/nebula/header"
  12. "github.com/slackhq/nebula/udp"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. )
  16. func assertTestRequestEchoed(t *testing.T, cipher string) {
  17. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  18. over := m{"cipher": cipher}
  19. a, aNet, aUdp, _ := newSimpleServer(cert.Version1, ca, caKey, "a", "10.128.0.1/24", over)
  20. b, bNet, bUdp, _ := newSimpleServer(cert.Version1, ca, caKey, "b", "10.128.0.2/24", over)
  21. a.InjectLightHouseAddr(bNet[0].Addr(), bUdp)
  22. b.InjectLightHouseAddr(aNet[0].Addr(), aUdp)
  23. a.Start()
  24. b.Start()
  25. t.Cleanup(func() { a.Stop(); b.Stop() })
  26. r := router.NewR(t, a, b)
  27. defer r.RenderFlow()
  28. assertTunnel(t, aNet[0].Addr(), bNet[0].Addr(), a, b, r)
  29. drainUDPTx(a)
  30. drainUDPTx(b)
  31. payload := []byte("a test payload well over sixteen bytes long, wow it's so very long long long!")
  32. require.Greater(t, len(payload), header.Len)
  33. a.GetF().SendMessageToVpnAddr(header.Test, header.TestRequest, bNet[0].Addr(), payload, make([]byte, 12, 12), make([]byte, udp.MTU))
  34. // Deliver A's request to B; B must echo a reply back
  35. b.InjectUDPPacket(a.GetFromUDP(true))
  36. reply := nextUDPTxOfType(t, b, header.Test, header.TestReply, 2*time.Second)
  37. assert.Equal(t, aUdp, reply.To, "the reply must go back to the requester")
  38. // header + echoed payload + 16-byte AEAD tag: proves the whole payload
  39. // round-tripped rather than being dropped or truncated.
  40. assert.Equal(t, header.Len+len(payload)+16, len(reply.Data), "the full payload must be echoed back")
  41. }
  42. func TestTestRequestEchoesLongPayloadAES(t *testing.T) {
  43. assertTestRequestEchoed(t, "aes")
  44. }
  45. func TestTestRequestEchoesLongPayloadChaChaPoly(t *testing.T) {
  46. assertTestRequestEchoed(t, "chachapoly")
  47. }
  48. // drainUDPTx empties a control's UDP tx queue without blocking.
  49. func drainUDPTx(c *nebula.Control) {
  50. for c.GetFromUDP(false) != nil {
  51. }
  52. }
  53. // nextUDPTxOfType returns the next packet a control transmits whose nebula
  54. // header matches (wantType, wantSub), skipping unrelated packets.
  55. // It fails the test if none arrives within the timeout.
  56. func nextUDPTxOfType(t *testing.T, c *nebula.Control, wantType header.MessageType, wantSub header.MessageSubType, within time.Duration) *udp.Packet {
  57. t.Helper()
  58. ch := c.GetUDPTxChan()
  59. timeout := time.After(within)
  60. for {
  61. select {
  62. case p := <-ch:
  63. var h header.H
  64. if err := h.Parse(p.Data); err == nil && h.Type == wantType && h.Subtype == wantSub {
  65. return p
  66. }
  67. case <-timeout:
  68. t.Fatalf("timed out waiting for a %v/%v packet on the udp tx queue", wantType, wantSub)
  69. return nil
  70. }
  71. }
  72. }