netmap_test.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package netmap
  4. import (
  5. "encoding/hex"
  6. "net/netip"
  7. "testing"
  8. "go4.org/mem"
  9. "tailscale.com/net/netaddr"
  10. "tailscale.com/tailcfg"
  11. "tailscale.com/types/key"
  12. )
  13. func testNodeKey(b byte) (ret key.NodePublic) {
  14. var bs [key.NodePublicRawLen]byte
  15. for i := range bs {
  16. bs[i] = b
  17. }
  18. return key.NodePublicFromRaw32(mem.B(bs[:]))
  19. }
  20. func testDiscoKey(hexPrefix string) (ret key.DiscoPublic) {
  21. b, err := hex.DecodeString(hexPrefix)
  22. if err != nil {
  23. panic(err)
  24. }
  25. // this function is used with short hexes, so zero-extend the raw
  26. // value.
  27. var bs [32]byte
  28. copy(bs[:], b)
  29. return key.DiscoPublicFromRaw32(mem.B(bs[:]))
  30. }
  31. func nodeViews(v []*tailcfg.Node) []tailcfg.NodeView {
  32. nv := make([]tailcfg.NodeView, len(v))
  33. for i, n := range v {
  34. nv[i] = n.View()
  35. }
  36. return nv
  37. }
  38. func eps(s ...string) []netip.AddrPort {
  39. var eps []netip.AddrPort
  40. for _, ep := range s {
  41. eps = append(eps, netip.MustParseAddrPort(ep))
  42. }
  43. return eps
  44. }
  45. func TestNetworkMapConcise(t *testing.T) {
  46. for _, tt := range []struct {
  47. name string
  48. nm *NetworkMap
  49. want string
  50. }{
  51. {
  52. name: "basic",
  53. nm: &NetworkMap{
  54. NodeKey: testNodeKey(1),
  55. Peers: nodeViews([]*tailcfg.Node{
  56. {
  57. Key: testNodeKey(2),
  58. HomeDERP: 2,
  59. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  60. },
  61. {
  62. Key: testNodeKey(3),
  63. HomeDERP: 4,
  64. Endpoints: eps("10.2.0.100:12", "10.1.0.100:12345"),
  65. },
  66. }),
  67. },
  68. want: "netmap: self: [AQEBA] auth=machine-unknown u=? []\n [AgICA] D2 : 192.168.0.100:12 192.168.0.100:12354\n [AwMDA] D4 : 10.2.0.100:12 10.1.0.100:12345\n",
  69. },
  70. } {
  71. t.Run(tt.name, func(t *testing.T) {
  72. var got string
  73. n := int(testing.AllocsPerRun(1000, func() {
  74. got = tt.nm.Concise()
  75. }))
  76. t.Logf("Allocs = %d", n)
  77. if got != tt.want {
  78. t.Errorf("Wrong output\n Got: %q\nWant: %q\n## Got (unescaped):\n%s\n## Want (unescaped):\n%s\n", got, tt.want, got, tt.want)
  79. }
  80. })
  81. }
  82. }
  83. func TestConciseDiffFrom(t *testing.T) {
  84. for _, tt := range []struct {
  85. name string
  86. a, b *NetworkMap
  87. want string
  88. }{
  89. {
  90. name: "no_change",
  91. a: &NetworkMap{
  92. NodeKey: testNodeKey(1),
  93. Peers: nodeViews([]*tailcfg.Node{
  94. {
  95. Key: testNodeKey(2),
  96. HomeDERP: 2,
  97. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  98. },
  99. }),
  100. },
  101. b: &NetworkMap{
  102. NodeKey: testNodeKey(1),
  103. Peers: nodeViews([]*tailcfg.Node{
  104. {
  105. Key: testNodeKey(2),
  106. HomeDERP: 2,
  107. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  108. },
  109. }),
  110. },
  111. want: "",
  112. },
  113. {
  114. name: "header_change",
  115. a: &NetworkMap{
  116. NodeKey: testNodeKey(1),
  117. Peers: nodeViews([]*tailcfg.Node{
  118. {
  119. Key: testNodeKey(2),
  120. HomeDERP: 2,
  121. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  122. },
  123. }),
  124. },
  125. b: &NetworkMap{
  126. NodeKey: testNodeKey(2),
  127. Peers: nodeViews([]*tailcfg.Node{
  128. {
  129. Key: testNodeKey(2),
  130. HomeDERP: 2,
  131. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  132. },
  133. }),
  134. },
  135. want: "-netmap: self: [AQEBA] auth=machine-unknown u=? []\n+netmap: self: [AgICA] auth=machine-unknown u=? []\n",
  136. },
  137. {
  138. name: "peer_add",
  139. a: &NetworkMap{
  140. NodeKey: testNodeKey(1),
  141. Peers: nodeViews([]*tailcfg.Node{
  142. {
  143. ID: 2,
  144. Key: testNodeKey(2),
  145. HomeDERP: 2,
  146. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  147. },
  148. }),
  149. },
  150. b: &NetworkMap{
  151. NodeKey: testNodeKey(1),
  152. Peers: nodeViews([]*tailcfg.Node{
  153. {
  154. ID: 1,
  155. Key: testNodeKey(1),
  156. HomeDERP: 1,
  157. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  158. },
  159. {
  160. ID: 2,
  161. Key: testNodeKey(2),
  162. HomeDERP: 2,
  163. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  164. },
  165. {
  166. ID: 3,
  167. Key: testNodeKey(3),
  168. HomeDERP: 3,
  169. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  170. },
  171. }),
  172. },
  173. want: "+ [AQEBA] D1 : 192.168.0.100:12 192.168.0.100:12354\n+ [AwMDA] D3 : 192.168.0.100:12 192.168.0.100:12354\n",
  174. },
  175. {
  176. name: "peer_remove",
  177. a: &NetworkMap{
  178. NodeKey: testNodeKey(1),
  179. Peers: nodeViews([]*tailcfg.Node{
  180. {
  181. ID: 1,
  182. Key: testNodeKey(1),
  183. HomeDERP: 1,
  184. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  185. },
  186. {
  187. ID: 2,
  188. Key: testNodeKey(2),
  189. HomeDERP: 2,
  190. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  191. },
  192. {
  193. ID: 3,
  194. Key: testNodeKey(3),
  195. HomeDERP: 3,
  196. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  197. },
  198. }),
  199. },
  200. b: &NetworkMap{
  201. NodeKey: testNodeKey(1),
  202. Peers: nodeViews([]*tailcfg.Node{
  203. {
  204. ID: 2,
  205. Key: testNodeKey(2),
  206. HomeDERP: 2,
  207. Endpoints: eps("192.168.0.100:12", "192.168.0.100:12354"),
  208. },
  209. }),
  210. },
  211. want: "- [AQEBA] D1 : 192.168.0.100:12 192.168.0.100:12354\n- [AwMDA] D3 : 192.168.0.100:12 192.168.0.100:12354\n",
  212. },
  213. {
  214. name: "peer_port_change",
  215. a: &NetworkMap{
  216. NodeKey: testNodeKey(1),
  217. Peers: nodeViews([]*tailcfg.Node{
  218. {
  219. ID: 2,
  220. Key: testNodeKey(2),
  221. HomeDERP: 2,
  222. Endpoints: eps("192.168.0.100:12", "1.1.1.1:1"),
  223. },
  224. }),
  225. },
  226. b: &NetworkMap{
  227. NodeKey: testNodeKey(1),
  228. Peers: nodeViews([]*tailcfg.Node{
  229. {
  230. ID: 2,
  231. Key: testNodeKey(2),
  232. HomeDERP: 2,
  233. Endpoints: eps("192.168.0.100:12", "1.1.1.1:2"),
  234. },
  235. }),
  236. },
  237. want: "- [AgICA] D2 : 192.168.0.100:12 1.1.1.1:1 \n+ [AgICA] D2 : 192.168.0.100:12 1.1.1.1:2 \n",
  238. },
  239. {
  240. name: "disco_key_only_change",
  241. a: &NetworkMap{
  242. NodeKey: testNodeKey(1),
  243. Peers: nodeViews([]*tailcfg.Node{
  244. {
  245. ID: 2,
  246. Key: testNodeKey(2),
  247. HomeDERP: 2,
  248. Endpoints: eps("192.168.0.100:41641", "1.1.1.1:41641"),
  249. DiscoKey: testDiscoKey("f00f00f00f"),
  250. AllowedIPs: []netip.Prefix{netip.PrefixFrom(netaddr.IPv4(100, 102, 103, 104), 32)},
  251. },
  252. }),
  253. },
  254. b: &NetworkMap{
  255. NodeKey: testNodeKey(1),
  256. Peers: nodeViews([]*tailcfg.Node{
  257. {
  258. ID: 2,
  259. Key: testNodeKey(2),
  260. HomeDERP: 2,
  261. Endpoints: eps("192.168.0.100:41641", "1.1.1.1:41641"),
  262. DiscoKey: testDiscoKey("ba4ba4ba4b"),
  263. AllowedIPs: []netip.Prefix{netip.PrefixFrom(netaddr.IPv4(100, 102, 103, 104), 32)},
  264. },
  265. }),
  266. },
  267. want: "- [AgICA] d:f00f00f00f000000 D2 100.102.103.104 : 192.168.0.100:41641 1.1.1.1:41641\n+ [AgICA] d:ba4ba4ba4b000000 D2 100.102.103.104 : 192.168.0.100:41641 1.1.1.1:41641\n",
  268. },
  269. } {
  270. t.Run(tt.name, func(t *testing.T) {
  271. var got string
  272. n := int(testing.AllocsPerRun(50, func() {
  273. got = tt.b.ConciseDiffFrom(tt.a)
  274. }))
  275. t.Logf("Allocs = %d", n)
  276. if got != tt.want {
  277. t.Errorf("Wrong output\n Got: %q\nWant: %q\n## Got (unescaped):\n%s\n## Want (unescaped):\n%s\n", got, tt.want, got, tt.want)
  278. }
  279. })
  280. }
  281. }
  282. func TestPeerIndexByNodeID(t *testing.T) {
  283. var nilPtr *NetworkMap
  284. if nilPtr.PeerIndexByNodeID(123) != -1 {
  285. t.Errorf("nil PeerIndexByNodeID should return -1")
  286. }
  287. var nm NetworkMap
  288. const min = 2
  289. const max = 10000
  290. const hole = max / 2
  291. for nid := tailcfg.NodeID(2); nid <= max; nid++ {
  292. if nid == hole {
  293. continue
  294. }
  295. nm.Peers = append(nm.Peers, (&tailcfg.Node{ID: nid}).View())
  296. }
  297. for want, nv := range nm.Peers {
  298. got := nm.PeerIndexByNodeID(nv.ID())
  299. if got != want {
  300. t.Errorf("PeerIndexByNodeID(%v) = %v; want %v", nv.ID(), got, want)
  301. }
  302. }
  303. for _, miss := range []tailcfg.NodeID{min - 1, hole, max + 1} {
  304. if got := nm.PeerIndexByNodeID(miss); got != -1 {
  305. t.Errorf("PeerIndexByNodeID(%v) = %v; want -1", miss, got)
  306. }
  307. }
  308. }