disco_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package disco
  4. import (
  5. "fmt"
  6. "net/netip"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. "time"
  11. "go4.org/mem"
  12. "tailscale.com/types/key"
  13. )
  14. func TestMarshalAndParse(t *testing.T) {
  15. relayHandshakeCommon := BindUDPRelayEndpointCommon{
  16. VNI: 1,
  17. Generation: 2,
  18. RemoteKey: key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
  19. Challenge: [BindUDPRelayChallengeLen]byte{
  20. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
  21. },
  22. }
  23. udpRelayEndpoint := UDPRelayEndpoint{
  24. ServerDisco: key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
  25. ClientDisco: [2]key.DiscoPublic{key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 3: 3, 30: 30, 31: 31})), key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 4: 4, 30: 30, 31: 31}))},
  26. LamportID: 123,
  27. VNI: 456,
  28. BindLifetime: time.Second,
  29. SteadyStateLifetime: time.Minute,
  30. AddrPorts: []netip.AddrPort{
  31. netip.MustParseAddrPort("1.2.3.4:567"),
  32. netip.MustParseAddrPort("[2001::3456]:789"),
  33. },
  34. }
  35. tests := []struct {
  36. name string
  37. want string
  38. m Message
  39. }{
  40. {
  41. name: "ping",
  42. m: &Ping{
  43. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  44. },
  45. want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c",
  46. },
  47. {
  48. name: "ping_with_nodekey_src",
  49. m: &Ping{
  50. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  51. NodeKey: key.NodePublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
  52. },
  53. want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f",
  54. },
  55. {
  56. name: "ping_with_padding",
  57. m: &Ping{
  58. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  59. Padding: 3,
  60. },
  61. want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 00 00",
  62. },
  63. {
  64. name: "ping_with_padding_and_nodekey_src",
  65. m: &Ping{
  66. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  67. NodeKey: key.NodePublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 30: 30, 31: 31})),
  68. Padding: 3,
  69. },
  70. want: "01 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 00 00",
  71. },
  72. {
  73. name: "pong",
  74. m: &Pong{
  75. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  76. Src: mustIPPort("2.3.4.5:1234"),
  77. },
  78. want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 00 00 00 00 00 00 00 00 00 00 ff ff 02 03 04 05 04 d2",
  79. },
  80. {
  81. name: "pongv6",
  82. m: &Pong{
  83. TxID: [12]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
  84. Src: mustIPPort("[fed0::12]:6666"),
  85. },
  86. want: "02 00 01 02 03 04 05 06 07 08 09 0a 0b 0c fe d0 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1a 0a",
  87. },
  88. {
  89. name: "call_me_maybe",
  90. m: &CallMeMaybe{},
  91. want: "03 00",
  92. },
  93. {
  94. name: "call_me_maybe_endpoints",
  95. m: &CallMeMaybe{
  96. MyNumber: []netip.AddrPort{
  97. netip.MustParseAddrPort("1.2.3.4:567"),
  98. netip.MustParseAddrPort("[2001::3456]:789"),
  99. },
  100. },
  101. want: "03 00 00 00 00 00 00 00 00 00 00 00 ff ff 01 02 03 04 02 37 20 01 00 00 00 00 00 00 00 00 00 00 00 00 34 56 03 15",
  102. },
  103. {
  104. name: "bind_udp_relay_endpoint",
  105. m: &BindUDPRelayEndpoint{
  106. relayHandshakeCommon,
  107. },
  108. want: "04 00 00 00 00 01 00 00 00 02 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
  109. },
  110. {
  111. name: "bind_udp_relay_endpoint_challenge",
  112. m: &BindUDPRelayEndpointChallenge{
  113. relayHandshakeCommon,
  114. },
  115. want: "05 00 00 00 00 01 00 00 00 02 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
  116. },
  117. {
  118. name: "bind_udp_relay_endpoint_answer",
  119. m: &BindUDPRelayEndpointAnswer{
  120. relayHandshakeCommon,
  121. },
  122. want: "06 00 00 00 00 01 00 00 00 02 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f",
  123. },
  124. {
  125. name: "call_me_maybe_via",
  126. m: &CallMeMaybeVia{
  127. UDPRelayEndpoint: udpRelayEndpoint,
  128. },
  129. want: "07 00 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 00 00 00 00 00 00 7b 00 00 01 c8 00 00 00 00 3b 9a ca 00 00 00 00 0d f8 47 58 00 00 00 00 00 00 00 00 00 00 00 ff ff 01 02 03 04 02 37 20 01 00 00 00 00 00 00 00 00 00 00 00 00 34 56 03 15",
  130. },
  131. {
  132. name: "allocate_udp_relay_endpoint_request",
  133. m: &AllocateUDPRelayEndpointRequest{
  134. ClientDisco: [2]key.DiscoPublic{key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 3: 3, 30: 30, 31: 31})), key.DiscoPublicFromRaw32(mem.B([]byte{1: 1, 2: 2, 4: 4, 30: 30, 31: 31}))},
  135. Generation: 1,
  136. },
  137. want: "08 00 00 01 02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 00 00 01",
  138. },
  139. {
  140. name: "allocate_udp_relay_endpoint_response",
  141. m: &AllocateUDPRelayEndpointResponse{
  142. Generation: 1,
  143. UDPRelayEndpoint: udpRelayEndpoint,
  144. },
  145. want: "09 00 00 00 00 01 00 01 02 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 01 02 00 04 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1e 1f 00 00 00 00 00 00 00 7b 00 00 01 c8 00 00 00 00 3b 9a ca 00 00 00 00 0d f8 47 58 00 00 00 00 00 00 00 00 00 00 00 ff ff 01 02 03 04 02 37 20 01 00 00 00 00 00 00 00 00 00 00 00 00 34 56 03 15",
  146. },
  147. }
  148. for _, tt := range tests {
  149. t.Run(tt.name, func(t *testing.T) {
  150. foo := []byte("foo")
  151. got := string(tt.m.AppendMarshal(foo))
  152. got, ok := strings.CutPrefix(got, "foo")
  153. if !ok {
  154. t.Fatalf("didn't start with foo: got %q", got)
  155. }
  156. gotHex := fmt.Sprintf("% x", got)
  157. if gotHex != tt.want {
  158. t.Fatalf("wrong marshal\n got: %s\nwant: %s\n", gotHex, tt.want)
  159. }
  160. back, err := Parse([]byte(got))
  161. if err != nil {
  162. t.Fatalf("parse back: %v", err)
  163. }
  164. if !reflect.DeepEqual(back, tt.m) {
  165. t.Errorf("message in %+v doesn't match Parse back result %+v", tt.m, back)
  166. }
  167. })
  168. }
  169. }
  170. func mustIPPort(s string) netip.AddrPort {
  171. ipp, err := netip.ParseAddrPort(s)
  172. if err != nil {
  173. panic(err)
  174. }
  175. return ipp
  176. }