map_test.go 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package controlclient
  4. import (
  5. "context"
  6. "encoding/json"
  7. "fmt"
  8. "maps"
  9. "net/netip"
  10. "reflect"
  11. "strings"
  12. "sync/atomic"
  13. "testing"
  14. "time"
  15. "github.com/google/go-cmp/cmp"
  16. "github.com/google/go-cmp/cmp/cmpopts"
  17. "go4.org/mem"
  18. "tailscale.com/control/controlknobs"
  19. "tailscale.com/health"
  20. "tailscale.com/tailcfg"
  21. "tailscale.com/tstest"
  22. "tailscale.com/tstime"
  23. "tailscale.com/types/dnstype"
  24. "tailscale.com/types/key"
  25. "tailscale.com/types/logger"
  26. "tailscale.com/types/netmap"
  27. "tailscale.com/types/ptr"
  28. "tailscale.com/util/mak"
  29. "tailscale.com/util/must"
  30. )
  31. func eps(s ...string) []netip.AddrPort {
  32. var eps []netip.AddrPort
  33. for _, ep := range s {
  34. eps = append(eps, netip.MustParseAddrPort(ep))
  35. }
  36. return eps
  37. }
  38. func TestUpdatePeersStateFromResponse(t *testing.T) {
  39. var curTime time.Time
  40. online := func(v bool) func(*tailcfg.Node) {
  41. return func(n *tailcfg.Node) {
  42. n.Online = &v
  43. }
  44. }
  45. seenAt := func(t time.Time) func(*tailcfg.Node) {
  46. return func(n *tailcfg.Node) {
  47. n.LastSeen = &t
  48. }
  49. }
  50. withDERP := func(regionID int) func(*tailcfg.Node) {
  51. return func(n *tailcfg.Node) {
  52. n.HomeDERP = regionID
  53. }
  54. }
  55. withEP := func(ep string) func(*tailcfg.Node) {
  56. return func(n *tailcfg.Node) {
  57. n.Endpoints = []netip.AddrPort{netip.MustParseAddrPort(ep)}
  58. }
  59. }
  60. n := func(id tailcfg.NodeID, name string, mod ...func(*tailcfg.Node)) *tailcfg.Node {
  61. n := &tailcfg.Node{ID: id, Name: name}
  62. for _, f := range mod {
  63. f(n)
  64. }
  65. return n
  66. }
  67. peers := func(nv ...*tailcfg.Node) []*tailcfg.Node { return nv }
  68. tests := []struct {
  69. name string
  70. mapRes *tailcfg.MapResponse
  71. curTime time.Time
  72. prev []*tailcfg.Node
  73. want []*tailcfg.Node
  74. wantStats updateStats
  75. }{
  76. {
  77. name: "full_peers",
  78. mapRes: &tailcfg.MapResponse{
  79. Peers: peers(n(1, "foo"), n(2, "bar")),
  80. },
  81. want: peers(n(1, "foo"), n(2, "bar")),
  82. wantStats: updateStats{
  83. allNew: true,
  84. added: 2,
  85. },
  86. },
  87. {
  88. name: "full_peers_ignores_deltas",
  89. mapRes: &tailcfg.MapResponse{
  90. Peers: peers(n(1, "foo"), n(2, "bar")),
  91. PeersRemoved: []tailcfg.NodeID{2},
  92. },
  93. want: peers(n(1, "foo"), n(2, "bar")),
  94. wantStats: updateStats{
  95. allNew: true,
  96. added: 2,
  97. },
  98. },
  99. {
  100. name: "add_and_update",
  101. prev: peers(n(1, "foo"), n(2, "bar")),
  102. mapRes: &tailcfg.MapResponse{
  103. PeersChanged: peers(n(0, "zero"), n(2, "bar2"), n(3, "three")),
  104. },
  105. want: peers(n(0, "zero"), n(1, "foo"), n(2, "bar2"), n(3, "three")),
  106. wantStats: updateStats{
  107. added: 2, // added IDs 0 and 3
  108. changed: 1, // changed ID 2
  109. },
  110. },
  111. {
  112. name: "remove",
  113. prev: peers(n(1, "foo"), n(2, "bar")),
  114. mapRes: &tailcfg.MapResponse{
  115. PeersRemoved: []tailcfg.NodeID{1, 3, 4},
  116. },
  117. want: peers(n(2, "bar")),
  118. wantStats: updateStats{
  119. removed: 1, // ID 1
  120. },
  121. },
  122. {
  123. name: "add_and_remove",
  124. prev: peers(n(1, "foo"), n(2, "bar")),
  125. mapRes: &tailcfg.MapResponse{
  126. PeersChanged: peers(n(1, "foo2")),
  127. PeersRemoved: []tailcfg.NodeID{2},
  128. },
  129. want: peers(n(1, "foo2")),
  130. wantStats: updateStats{
  131. changed: 1,
  132. removed: 1,
  133. },
  134. },
  135. {
  136. name: "unchanged",
  137. prev: peers(n(1, "foo"), n(2, "bar")),
  138. mapRes: &tailcfg.MapResponse{},
  139. want: peers(n(1, "foo"), n(2, "bar")),
  140. },
  141. {
  142. name: "online_change",
  143. prev: peers(n(1, "foo"), n(2, "bar")),
  144. mapRes: &tailcfg.MapResponse{
  145. OnlineChange: map[tailcfg.NodeID]bool{
  146. 1: true,
  147. 404: true,
  148. },
  149. },
  150. want: peers(
  151. n(1, "foo", online(true)),
  152. n(2, "bar"),
  153. ),
  154. wantStats: updateStats{changed: 1},
  155. },
  156. {
  157. name: "online_change_offline",
  158. prev: peers(n(1, "foo"), n(2, "bar")),
  159. mapRes: &tailcfg.MapResponse{
  160. OnlineChange: map[tailcfg.NodeID]bool{
  161. 1: false,
  162. 2: true,
  163. },
  164. },
  165. want: peers(
  166. n(1, "foo", online(false)),
  167. n(2, "bar", online(true)),
  168. ),
  169. wantStats: updateStats{changed: 2},
  170. },
  171. {
  172. name: "peer_seen_at",
  173. prev: peers(n(1, "foo", seenAt(time.Unix(111, 0))), n(2, "bar")),
  174. curTime: time.Unix(123, 0),
  175. mapRes: &tailcfg.MapResponse{
  176. PeerSeenChange: map[tailcfg.NodeID]bool{
  177. 1: false,
  178. 2: true,
  179. },
  180. },
  181. want: peers(
  182. n(1, "foo"),
  183. n(2, "bar", seenAt(time.Unix(123, 0))),
  184. ),
  185. wantStats: updateStats{changed: 2},
  186. },
  187. {
  188. name: "ep_change_derp",
  189. prev: peers(n(1, "foo", withDERP(3))),
  190. mapRes: &tailcfg.MapResponse{
  191. PeersChangedPatch: []*tailcfg.PeerChange{{
  192. NodeID: 1,
  193. DERPRegion: 4,
  194. }},
  195. },
  196. want: peers(n(1, "foo", withDERP(4))),
  197. wantStats: updateStats{changed: 1},
  198. },
  199. {
  200. name: "ep_change_udp",
  201. prev: peers(n(1, "foo", withEP("1.2.3.4:111"))),
  202. mapRes: &tailcfg.MapResponse{
  203. PeersChangedPatch: []*tailcfg.PeerChange{{
  204. NodeID: 1,
  205. Endpoints: eps("1.2.3.4:56"),
  206. }},
  207. },
  208. want: peers(n(1, "foo", withEP("1.2.3.4:56"))),
  209. wantStats: updateStats{changed: 1},
  210. },
  211. {
  212. name: "ep_change_udp_2",
  213. prev: peers(n(1, "foo", withDERP(3), withEP("1.2.3.4:111"))),
  214. mapRes: &tailcfg.MapResponse{
  215. PeersChangedPatch: []*tailcfg.PeerChange{{
  216. NodeID: 1,
  217. Endpoints: eps("1.2.3.4:56"),
  218. }},
  219. },
  220. want: peers(n(1, "foo", withDERP(3), withEP("1.2.3.4:56"))),
  221. wantStats: updateStats{changed: 1},
  222. },
  223. {
  224. name: "ep_change_both",
  225. prev: peers(n(1, "foo", withDERP(3), withEP("1.2.3.4:111"))),
  226. mapRes: &tailcfg.MapResponse{
  227. PeersChangedPatch: []*tailcfg.PeerChange{{
  228. NodeID: 1,
  229. DERPRegion: 2,
  230. Endpoints: eps("1.2.3.4:56"),
  231. }},
  232. },
  233. want: peers(n(1, "foo", withDERP(2), withEP("1.2.3.4:56"))),
  234. wantStats: updateStats{changed: 1},
  235. },
  236. {
  237. name: "change_key",
  238. prev: peers(n(1, "foo")),
  239. mapRes: &tailcfg.MapResponse{
  240. PeersChangedPatch: []*tailcfg.PeerChange{{
  241. NodeID: 1,
  242. Key: ptr.To(key.NodePublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))),
  243. }},
  244. }, want: peers(&tailcfg.Node{
  245. ID: 1,
  246. Name: "foo",
  247. Key: key.NodePublicFromRaw32(mem.B(append(make([]byte, 31), 'A'))),
  248. }),
  249. wantStats: updateStats{changed: 1},
  250. },
  251. {
  252. name: "change_key_signature",
  253. prev: peers(n(1, "foo")),
  254. mapRes: &tailcfg.MapResponse{
  255. PeersChangedPatch: []*tailcfg.PeerChange{{
  256. NodeID: 1,
  257. KeySignature: []byte{3, 4},
  258. }},
  259. },
  260. want: peers(&tailcfg.Node{
  261. ID: 1,
  262. Name: "foo",
  263. KeySignature: []byte{3, 4},
  264. }),
  265. wantStats: updateStats{changed: 1},
  266. },
  267. {
  268. name: "change_disco_key",
  269. prev: peers(n(1, "foo")),
  270. mapRes: &tailcfg.MapResponse{
  271. PeersChangedPatch: []*tailcfg.PeerChange{{
  272. NodeID: 1,
  273. DiscoKey: ptr.To(key.DiscoPublicFromRaw32(mem.B(append(make([]byte, 31), 'A')))),
  274. }},
  275. },
  276. want: peers(&tailcfg.Node{
  277. ID: 1,
  278. Name: "foo",
  279. DiscoKey: key.DiscoPublicFromRaw32(mem.B(append(make([]byte, 31), 'A'))),
  280. }),
  281. wantStats: updateStats{changed: 1},
  282. },
  283. {
  284. name: "change_online",
  285. prev: peers(n(1, "foo")),
  286. mapRes: &tailcfg.MapResponse{
  287. PeersChangedPatch: []*tailcfg.PeerChange{{
  288. NodeID: 1,
  289. Online: ptr.To(true),
  290. }},
  291. },
  292. want: peers(&tailcfg.Node{
  293. ID: 1,
  294. Name: "foo",
  295. Online: ptr.To(true),
  296. }),
  297. wantStats: updateStats{changed: 1},
  298. },
  299. {
  300. name: "change_last_seen",
  301. prev: peers(n(1, "foo")),
  302. mapRes: &tailcfg.MapResponse{
  303. PeersChangedPatch: []*tailcfg.PeerChange{{
  304. NodeID: 1,
  305. LastSeen: ptr.To(time.Unix(123, 0).UTC()),
  306. }},
  307. },
  308. want: peers(&tailcfg.Node{
  309. ID: 1,
  310. Name: "foo",
  311. LastSeen: ptr.To(time.Unix(123, 0).UTC()),
  312. }),
  313. wantStats: updateStats{changed: 1},
  314. },
  315. {
  316. name: "change_key_expiry",
  317. prev: peers(n(1, "foo")),
  318. mapRes: &tailcfg.MapResponse{
  319. PeersChangedPatch: []*tailcfg.PeerChange{{
  320. NodeID: 1,
  321. KeyExpiry: ptr.To(time.Unix(123, 0).UTC()),
  322. }},
  323. },
  324. want: peers(&tailcfg.Node{
  325. ID: 1,
  326. Name: "foo",
  327. KeyExpiry: time.Unix(123, 0).UTC(),
  328. }),
  329. wantStats: updateStats{changed: 1},
  330. },
  331. }
  332. for _, tt := range tests {
  333. t.Run(tt.name, func(t *testing.T) {
  334. if !tt.curTime.IsZero() {
  335. curTime = tt.curTime
  336. tstest.Replace(t, &clock, tstime.Clock(tstest.NewClock(tstest.ClockOpts{Start: curTime})))
  337. }
  338. ms := newTestMapSession(t, nil)
  339. for _, n := range tt.prev {
  340. mak.Set(&ms.peers, n.ID, n.View())
  341. }
  342. gotStats := ms.updatePeersStateFromResponse(tt.mapRes)
  343. if gotStats != tt.wantStats {
  344. t.Errorf("got stats = %+v; want %+v", gotStats, tt.wantStats)
  345. }
  346. var got []*tailcfg.Node
  347. for _, vp := range ms.sortedPeers() {
  348. got = append(got, vp.AsStruct())
  349. }
  350. if !reflect.DeepEqual(got, tt.want) {
  351. t.Errorf("wrong results\n got: %s\nwant: %s", formatNodes(got), formatNodes(tt.want))
  352. }
  353. })
  354. }
  355. }
  356. func formatNodes(nodes []*tailcfg.Node) string {
  357. var sb strings.Builder
  358. for i, n := range nodes {
  359. if i > 0 {
  360. sb.WriteString(", ")
  361. }
  362. fmt.Fprintf(&sb, "(%d, %q", n.ID, n.Name)
  363. if n.Online != nil {
  364. fmt.Fprintf(&sb, ", online=%v", *n.Online)
  365. }
  366. if n.LastSeen != nil {
  367. fmt.Fprintf(&sb, ", lastSeen=%v", n.LastSeen.Unix())
  368. }
  369. if n.Key != (key.NodePublic{}) {
  370. fmt.Fprintf(&sb, ", key=%v", n.Key.String())
  371. }
  372. if n.Expired {
  373. fmt.Fprintf(&sb, ", expired=true")
  374. }
  375. sb.WriteString(")")
  376. }
  377. return sb.String()
  378. }
  379. func newTestMapSession(t testing.TB, nu NetmapUpdater) *mapSession {
  380. ms := newMapSession(key.NewNode(), nu, new(controlknobs.Knobs))
  381. t.Cleanup(ms.Close)
  382. ms.logf = t.Logf
  383. return ms
  384. }
  385. func (ms *mapSession) netmapForResponse(res *tailcfg.MapResponse) *netmap.NetworkMap {
  386. ms.updateStateFromResponse(res)
  387. return ms.netmap()
  388. }
  389. func TestNetmapForResponse(t *testing.T) {
  390. t.Run("implicit_packetfilter", func(t *testing.T) {
  391. somePacketFilter := []tailcfg.FilterRule{
  392. {
  393. SrcIPs: []string{"*"},
  394. DstPorts: []tailcfg.NetPortRange{
  395. {IP: "10.2.3.4", Ports: tailcfg.PortRange{First: 22, Last: 22}},
  396. },
  397. },
  398. }
  399. ms := newTestMapSession(t, nil)
  400. nm1 := ms.netmapForResponse(&tailcfg.MapResponse{
  401. Node: new(tailcfg.Node),
  402. PacketFilter: somePacketFilter,
  403. })
  404. if len(nm1.PacketFilter) == 0 {
  405. t.Fatalf("zero length PacketFilter")
  406. }
  407. nm2 := ms.netmapForResponse(&tailcfg.MapResponse{
  408. Node: new(tailcfg.Node),
  409. PacketFilter: nil, // testing that the server can omit this.
  410. })
  411. if len(nm1.PacketFilter) == 0 {
  412. t.Fatalf("zero length PacketFilter in 2nd netmap")
  413. }
  414. if !reflect.DeepEqual(nm1.PacketFilter, nm2.PacketFilter) {
  415. t.Error("packet filters differ")
  416. }
  417. })
  418. t.Run("implicit_dnsconfig", func(t *testing.T) {
  419. someDNSConfig := &tailcfg.DNSConfig{Domains: []string{"foo", "bar"}}
  420. ms := newTestMapSession(t, nil)
  421. nm1 := ms.netmapForResponse(&tailcfg.MapResponse{
  422. Node: new(tailcfg.Node),
  423. DNSConfig: someDNSConfig,
  424. })
  425. if !reflect.DeepEqual(nm1.DNS, *someDNSConfig) {
  426. t.Fatalf("1st DNS wrong")
  427. }
  428. nm2 := ms.netmapForResponse(&tailcfg.MapResponse{
  429. Node: new(tailcfg.Node),
  430. DNSConfig: nil, // implicit
  431. })
  432. if !reflect.DeepEqual(nm2.DNS, *someDNSConfig) {
  433. t.Fatalf("2nd DNS wrong")
  434. }
  435. })
  436. t.Run("collect_services", func(t *testing.T) {
  437. ms := newTestMapSession(t, nil)
  438. var nm *netmap.NetworkMap
  439. wantCollect := func(v bool) {
  440. t.Helper()
  441. if nm.CollectServices != v {
  442. t.Errorf("netmap.CollectServices = %v; want %v", nm.CollectServices, v)
  443. }
  444. }
  445. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  446. Node: new(tailcfg.Node),
  447. })
  448. wantCollect(false)
  449. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  450. Node: new(tailcfg.Node),
  451. CollectServices: "false",
  452. })
  453. wantCollect(false)
  454. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  455. Node: new(tailcfg.Node),
  456. CollectServices: "true",
  457. })
  458. wantCollect(true)
  459. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  460. Node: new(tailcfg.Node),
  461. CollectServices: "",
  462. })
  463. wantCollect(true)
  464. })
  465. t.Run("implicit_domain", func(t *testing.T) {
  466. ms := newTestMapSession(t, nil)
  467. var nm *netmap.NetworkMap
  468. want := func(v string) {
  469. t.Helper()
  470. if nm.Domain != v {
  471. t.Errorf("netmap.Domain = %q; want %q", nm.Domain, v)
  472. }
  473. }
  474. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  475. Node: new(tailcfg.Node),
  476. Domain: "foo.com",
  477. })
  478. want("foo.com")
  479. nm = ms.netmapForResponse(&tailcfg.MapResponse{
  480. Node: new(tailcfg.Node),
  481. })
  482. want("foo.com")
  483. })
  484. t.Run("implicit_node", func(t *testing.T) {
  485. someNode := &tailcfg.Node{
  486. Name: "foo",
  487. }
  488. wantNode := (&tailcfg.Node{
  489. Name: "foo",
  490. ComputedName: "foo",
  491. ComputedNameWithHost: "foo",
  492. }).View()
  493. ms := newTestMapSession(t, nil)
  494. mapRes := &tailcfg.MapResponse{
  495. Node: someNode,
  496. }
  497. initDisplayNames(mapRes.Node.View(), mapRes)
  498. ms.updateStateFromResponse(mapRes)
  499. nm1 := ms.netmap()
  500. if !nm1.SelfNode.Valid() {
  501. t.Fatal("nil Node in 1st netmap")
  502. }
  503. if !reflect.DeepEqual(nm1.SelfNode, wantNode) {
  504. j, _ := json.Marshal(nm1.SelfNode)
  505. t.Errorf("Node mismatch in 1st netmap; got: %s", j)
  506. }
  507. ms.updateStateFromResponse(&tailcfg.MapResponse{})
  508. nm2 := ms.netmap()
  509. if !nm2.SelfNode.Valid() {
  510. t.Fatal("nil Node in 1st netmap")
  511. }
  512. if !reflect.DeepEqual(nm2.SelfNode, wantNode) {
  513. j, _ := json.Marshal(nm2.SelfNode)
  514. t.Errorf("Node mismatch in 2nd netmap; got: %s", j)
  515. }
  516. })
  517. t.Run("named_packetfilter", func(t *testing.T) {
  518. pfA := []tailcfg.FilterRule{
  519. {
  520. SrcIPs: []string{"10.0.0.1"},
  521. DstPorts: []tailcfg.NetPortRange{
  522. {IP: "10.2.3.4", Ports: tailcfg.PortRange{First: 22, Last: 22}},
  523. },
  524. },
  525. }
  526. pfB := []tailcfg.FilterRule{
  527. {
  528. SrcIPs: []string{"10.0.0.2"},
  529. DstPorts: []tailcfg.NetPortRange{
  530. {IP: "10.2.3.4", Ports: tailcfg.PortRange{First: 22, Last: 22}},
  531. },
  532. },
  533. }
  534. ms := newTestMapSession(t, nil)
  535. // Mix of old & new style (PacketFilter and PacketFilters).
  536. nm1 := ms.netmapForResponse(&tailcfg.MapResponse{
  537. Node: new(tailcfg.Node),
  538. PacketFilter: pfA,
  539. PacketFilters: map[string][]tailcfg.FilterRule{
  540. "pf-b": pfB,
  541. },
  542. })
  543. if got, want := len(nm1.PacketFilter), 2; got != want {
  544. t.Fatalf("PacketFilter length = %v; want %v", got, want)
  545. }
  546. if got, want := first(nm1.PacketFilter[0].Srcs).String(), "10.0.0.1/32"; got != want {
  547. t.Fatalf("PacketFilter[0].Srcs = %v; want %v", got, want)
  548. }
  549. if got, want := first(nm1.PacketFilter[1].Srcs).String(), "10.0.0.2/32"; got != want {
  550. t.Fatalf("PacketFilter[0].Srcs = %v; want %v", got, want)
  551. }
  552. // No-op change. Remember the old stuff.
  553. nm2 := ms.netmapForResponse(&tailcfg.MapResponse{
  554. Node: new(tailcfg.Node),
  555. PacketFilter: nil,
  556. PacketFilters: nil,
  557. })
  558. if got, want := len(nm2.PacketFilter), 2; got != want {
  559. t.Fatalf("PacketFilter length = %v; want %v", got, want)
  560. }
  561. if !reflect.DeepEqual(nm1.PacketFilter, nm2.PacketFilter) {
  562. t.Error("packet filters differ")
  563. }
  564. // New style only, with clear.
  565. nm3 := ms.netmapForResponse(&tailcfg.MapResponse{
  566. Node: new(tailcfg.Node),
  567. PacketFilter: nil,
  568. PacketFilters: map[string][]tailcfg.FilterRule{
  569. "*": nil,
  570. "pf-b": pfB,
  571. },
  572. })
  573. if got, want := len(nm3.PacketFilter), 1; got != want {
  574. t.Fatalf("PacketFilter length = %v; want %v", got, want)
  575. }
  576. if got, want := first(nm3.PacketFilter[0].Srcs).String(), "10.0.0.2/32"; got != want {
  577. t.Fatalf("PacketFilter[0].Srcs = %v; want %v", got, want)
  578. }
  579. // New style only, adding pfA back, not as the legacy "base" layer:.
  580. nm4 := ms.netmapForResponse(&tailcfg.MapResponse{
  581. Node: new(tailcfg.Node),
  582. PacketFilter: nil,
  583. PacketFilters: map[string][]tailcfg.FilterRule{
  584. "pf-a": pfA,
  585. },
  586. })
  587. if got, want := len(nm4.PacketFilter), 2; got != want {
  588. t.Fatalf("PacketFilter length = %v; want %v", got, want)
  589. }
  590. if got, want := first(nm4.PacketFilter[0].Srcs).String(), "10.0.0.1/32"; got != want {
  591. t.Fatalf("PacketFilter[0].Srcs = %v; want %v", got, want)
  592. }
  593. if got, want := first(nm4.PacketFilter[1].Srcs).String(), "10.0.0.2/32"; got != want {
  594. t.Fatalf("PacketFilter[0].Srcs = %v; want %v", got, want)
  595. }
  596. })
  597. }
  598. func first[T any](s []T) T {
  599. if len(s) == 0 {
  600. var zero T
  601. return zero
  602. }
  603. return s[0]
  604. }
  605. func TestDeltaDERPMap(t *testing.T) {
  606. regions1 := map[int]*tailcfg.DERPRegion{
  607. 1: {
  608. RegionID: 1,
  609. Nodes: []*tailcfg.DERPNode{{
  610. Name: "derp1a",
  611. RegionID: 1,
  612. HostName: "derp1a" + tailcfg.DotInvalid,
  613. IPv4: "169.254.169.254",
  614. IPv6: "none",
  615. }},
  616. },
  617. }
  618. // As above, but with a changed IPv4 addr
  619. regions2 := map[int]*tailcfg.DERPRegion{1: regions1[1].Clone()}
  620. regions2[1].Nodes[0].IPv4 = "127.0.0.1"
  621. type step struct {
  622. got *tailcfg.DERPMap
  623. want *tailcfg.DERPMap
  624. }
  625. tests := []struct {
  626. name string
  627. steps []step
  628. }{
  629. {
  630. name: "nothing-to-nothing",
  631. steps: []step{
  632. {nil, nil},
  633. {nil, nil},
  634. },
  635. },
  636. {
  637. name: "regions-sticky",
  638. steps: []step{
  639. {&tailcfg.DERPMap{Regions: regions1}, &tailcfg.DERPMap{Regions: regions1}},
  640. {&tailcfg.DERPMap{}, &tailcfg.DERPMap{Regions: regions1}},
  641. },
  642. },
  643. {
  644. name: "regions-change",
  645. steps: []step{
  646. {&tailcfg.DERPMap{Regions: regions1}, &tailcfg.DERPMap{Regions: regions1}},
  647. {&tailcfg.DERPMap{Regions: regions2}, &tailcfg.DERPMap{Regions: regions2}},
  648. },
  649. },
  650. {
  651. name: "home-params",
  652. steps: []step{
  653. // Send a DERP map
  654. {&tailcfg.DERPMap{Regions: regions1}, &tailcfg.DERPMap{Regions: regions1}},
  655. // Send home params, want to still have the same regions
  656. {
  657. &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{
  658. RegionScore: map[int]float64{1: 0.5},
  659. }},
  660. &tailcfg.DERPMap{Regions: regions1, HomeParams: &tailcfg.DERPHomeParams{
  661. RegionScore: map[int]float64{1: 0.5},
  662. }},
  663. },
  664. },
  665. },
  666. {
  667. name: "home-params-sub-fields",
  668. steps: []step{
  669. // Send a DERP map with home params
  670. {
  671. &tailcfg.DERPMap{Regions: regions1, HomeParams: &tailcfg.DERPHomeParams{
  672. RegionScore: map[int]float64{1: 0.5},
  673. }},
  674. &tailcfg.DERPMap{Regions: regions1, HomeParams: &tailcfg.DERPHomeParams{
  675. RegionScore: map[int]float64{1: 0.5},
  676. }},
  677. },
  678. // Sending a struct with a 'HomeParams' field but nil RegionScore doesn't change home params...
  679. {
  680. &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{RegionScore: nil}},
  681. &tailcfg.DERPMap{Regions: regions1, HomeParams: &tailcfg.DERPHomeParams{
  682. RegionScore: map[int]float64{1: 0.5},
  683. }},
  684. },
  685. // ... but sending one with a non-nil and empty RegionScore field zeroes that out.
  686. {
  687. &tailcfg.DERPMap{HomeParams: &tailcfg.DERPHomeParams{RegionScore: map[int]float64{}}},
  688. &tailcfg.DERPMap{Regions: regions1, HomeParams: &tailcfg.DERPHomeParams{
  689. RegionScore: map[int]float64{},
  690. }},
  691. },
  692. },
  693. },
  694. }
  695. for _, tt := range tests {
  696. t.Run(tt.name, func(t *testing.T) {
  697. ms := newTestMapSession(t, nil)
  698. for stepi, s := range tt.steps {
  699. nm := ms.netmapForResponse(&tailcfg.MapResponse{DERPMap: s.got})
  700. if !reflect.DeepEqual(nm.DERPMap, s.want) {
  701. t.Errorf("unexpected result at step index %v; got: %s", stepi, logger.AsJSON(nm.DERPMap))
  702. }
  703. }
  704. })
  705. }
  706. }
  707. func TestPeerChangeDiff(t *testing.T) {
  708. tests := []struct {
  709. name string
  710. a, b *tailcfg.Node
  711. want *tailcfg.PeerChange // nil means want ok=false, unless wantEqual is set
  712. wantEqual bool // means test wants (nil, true)
  713. }{
  714. {
  715. name: "eq",
  716. a: &tailcfg.Node{ID: 1},
  717. b: &tailcfg.Node{ID: 1},
  718. wantEqual: true,
  719. },
  720. {
  721. name: "patch-derp",
  722. a: &tailcfg.Node{ID: 1, HomeDERP: 1},
  723. b: &tailcfg.Node{ID: 1, HomeDERP: 2},
  724. want: &tailcfg.PeerChange{NodeID: 1, DERPRegion: 2},
  725. },
  726. {
  727. name: "patch-endpoints",
  728. a: &tailcfg.Node{ID: 1, Endpoints: eps("10.0.0.1:1")},
  729. b: &tailcfg.Node{ID: 1, Endpoints: eps("10.0.0.2:2")},
  730. want: &tailcfg.PeerChange{NodeID: 1, Endpoints: eps("10.0.0.2:2")},
  731. },
  732. {
  733. name: "patch-cap",
  734. a: &tailcfg.Node{ID: 1, Cap: 1},
  735. b: &tailcfg.Node{ID: 1, Cap: 2},
  736. want: &tailcfg.PeerChange{NodeID: 1, Cap: 2},
  737. },
  738. {
  739. name: "patch-lastseen",
  740. a: &tailcfg.Node{ID: 1, LastSeen: ptr.To(time.Unix(1, 0))},
  741. b: &tailcfg.Node{ID: 1, LastSeen: ptr.To(time.Unix(2, 0))},
  742. want: &tailcfg.PeerChange{NodeID: 1, LastSeen: ptr.To(time.Unix(2, 0))},
  743. },
  744. {
  745. name: "patch-online-to-true",
  746. a: &tailcfg.Node{ID: 1, Online: ptr.To(false)},
  747. b: &tailcfg.Node{ID: 1, Online: ptr.To(true)},
  748. want: &tailcfg.PeerChange{NodeID: 1, Online: ptr.To(true)},
  749. },
  750. {
  751. name: "patch-online-to-false",
  752. a: &tailcfg.Node{ID: 1, Online: ptr.To(true)},
  753. b: &tailcfg.Node{ID: 1, Online: ptr.To(false)},
  754. want: &tailcfg.PeerChange{NodeID: 1, Online: ptr.To(false)},
  755. },
  756. {
  757. name: "mix-patchable-and-not",
  758. a: &tailcfg.Node{ID: 1, Cap: 1},
  759. b: &tailcfg.Node{ID: 1, Cap: 2, StableID: "foo"},
  760. want: nil,
  761. },
  762. {
  763. name: "miss-change-stableid",
  764. a: &tailcfg.Node{ID: 1},
  765. b: &tailcfg.Node{ID: 1, StableID: "diff"},
  766. want: nil,
  767. },
  768. {
  769. name: "miss-change-id",
  770. a: &tailcfg.Node{ID: 1},
  771. b: &tailcfg.Node{ID: 2},
  772. want: nil,
  773. },
  774. {
  775. name: "miss-change-name",
  776. a: &tailcfg.Node{ID: 1, Name: "foo"},
  777. b: &tailcfg.Node{ID: 1, Name: "bar"},
  778. want: nil,
  779. },
  780. {
  781. name: "miss-change-user",
  782. a: &tailcfg.Node{ID: 1, User: 1},
  783. b: &tailcfg.Node{ID: 1, User: 2},
  784. want: nil,
  785. },
  786. {
  787. name: "miss-change-masq-v4",
  788. a: &tailcfg.Node{ID: 1, SelfNodeV4MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("100.64.0.1"))},
  789. b: &tailcfg.Node{ID: 1, SelfNodeV4MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("100.64.0.2"))},
  790. want: nil,
  791. },
  792. {
  793. name: "miss-change-masq-v6",
  794. a: &tailcfg.Node{ID: 1, SelfNodeV6MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("2001::3456"))},
  795. b: &tailcfg.Node{ID: 1, SelfNodeV6MasqAddrForThisPeer: ptr.To(netip.MustParseAddr("2001::3006"))},
  796. want: nil,
  797. },
  798. {
  799. name: "patch-capmap-add-value-to-existing-key",
  800. a: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  801. b: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: []tailcfg.RawMessage{"true"}}},
  802. want: &tailcfg.PeerChange{NodeID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: []tailcfg.RawMessage{"true"}}},
  803. },
  804. {
  805. name: "patch-capmap-add-new-key",
  806. a: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  807. b: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil, tailcfg.CapabilityDebug: nil}},
  808. want: &tailcfg.PeerChange{NodeID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil, tailcfg.CapabilityDebug: nil}},
  809. }, {
  810. name: "patch-capmap-remove-key",
  811. a: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  812. b: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{}},
  813. want: &tailcfg.PeerChange{NodeID: 1, CapMap: tailcfg.NodeCapMap{}},
  814. }, {
  815. name: "patch-capmap-remove-as-nil",
  816. a: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  817. b: &tailcfg.Node{ID: 1},
  818. want: &tailcfg.PeerChange{NodeID: 1, CapMap: tailcfg.NodeCapMap{}},
  819. }, {
  820. name: "patch-capmap-add-key-to-empty-map",
  821. a: &tailcfg.Node{ID: 1},
  822. b: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  823. want: &tailcfg.PeerChange{NodeID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  824. },
  825. {
  826. name: "patch-capmap-no-change",
  827. a: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  828. b: &tailcfg.Node{ID: 1, CapMap: tailcfg.NodeCapMap{tailcfg.CapabilityAdmin: nil}},
  829. wantEqual: true,
  830. },
  831. }
  832. for _, tt := range tests {
  833. t.Run(tt.name, func(t *testing.T) {
  834. pc, ok := peerChangeDiff(tt.a.View(), tt.b)
  835. if tt.wantEqual {
  836. if !ok || pc != nil {
  837. t.Errorf("got (%p, %v); want (nil, true); pc=%v", pc, ok, logger.AsJSON(pc))
  838. }
  839. return
  840. }
  841. if (pc != nil) != ok {
  842. t.Fatalf("inconsistent ok=%v, pc=%p", ok, pc)
  843. }
  844. if !reflect.DeepEqual(pc, tt.want) {
  845. t.Errorf("mismatch\n got: %v\nwant: %v\n", logger.AsJSON(pc), logger.AsJSON(tt.want))
  846. }
  847. })
  848. }
  849. }
  850. func TestPeerChangeDiffAllocs(t *testing.T) {
  851. a := &tailcfg.Node{ID: 1}
  852. b := &tailcfg.Node{ID: 1}
  853. n := testing.AllocsPerRun(10000, func() {
  854. diff, ok := peerChangeDiff(a.View(), b)
  855. if !ok || diff != nil {
  856. t.Fatalf("unexpected result: (%s, %v)", logger.AsJSON(diff), ok)
  857. }
  858. })
  859. if n != 0 {
  860. t.Errorf("allocs = %v; want 0", int(n))
  861. }
  862. }
  863. type countingNetmapUpdater struct {
  864. full atomic.Int64
  865. }
  866. func (nu *countingNetmapUpdater) UpdateFullNetmap(nm *netmap.NetworkMap) {
  867. nu.full.Add(1)
  868. }
  869. // tests (*mapSession).patchifyPeersChanged; smaller tests are in TestPeerChangeDiff
  870. func TestPatchifyPeersChanged(t *testing.T) {
  871. hi := (&tailcfg.Hostinfo{}).View()
  872. tests := []struct {
  873. name string
  874. mr0 *tailcfg.MapResponse // initial
  875. mr1 *tailcfg.MapResponse // incremental
  876. want *tailcfg.MapResponse // what the incremental one should've been mutated to
  877. }{
  878. {
  879. name: "change_one_endpoint",
  880. mr0: &tailcfg.MapResponse{
  881. Node: &tailcfg.Node{Name: "foo.bar.ts.net."},
  882. Peers: []*tailcfg.Node{
  883. {ID: 1, Hostinfo: hi},
  884. },
  885. },
  886. mr1: &tailcfg.MapResponse{
  887. PeersChanged: []*tailcfg.Node{
  888. {ID: 1, Endpoints: eps("10.0.0.1:1111"), Hostinfo: hi},
  889. },
  890. },
  891. want: &tailcfg.MapResponse{
  892. PeersChanged: nil,
  893. PeersChangedPatch: []*tailcfg.PeerChange{
  894. {NodeID: 1, Endpoints: eps("10.0.0.1:1111")},
  895. },
  896. },
  897. },
  898. {
  899. name: "change_some",
  900. mr0: &tailcfg.MapResponse{
  901. Node: &tailcfg.Node{Name: "foo.bar.ts.net."},
  902. Peers: []*tailcfg.Node{
  903. {ID: 1, HomeDERP: 1, Hostinfo: hi},
  904. {ID: 2, HomeDERP: 2, Hostinfo: hi},
  905. {ID: 3, HomeDERP: 3, Hostinfo: hi},
  906. },
  907. },
  908. mr1: &tailcfg.MapResponse{
  909. PeersChanged: []*tailcfg.Node{
  910. {ID: 1, HomeDERP: 11, Hostinfo: hi},
  911. {ID: 2, StableID: "other-change", Hostinfo: hi},
  912. {ID: 3, HomeDERP: 33, Hostinfo: hi},
  913. {ID: 4, HomeDERP: 4, Hostinfo: hi},
  914. },
  915. },
  916. want: &tailcfg.MapResponse{
  917. PeersChanged: []*tailcfg.Node{
  918. {ID: 2, StableID: "other-change", Hostinfo: hi},
  919. {ID: 4, HomeDERP: 4, Hostinfo: hi},
  920. },
  921. PeersChangedPatch: []*tailcfg.PeerChange{
  922. {NodeID: 1, DERPRegion: 11},
  923. {NodeID: 3, DERPRegion: 33},
  924. },
  925. },
  926. },
  927. {
  928. name: "change_exitnodednsresolvers",
  929. mr0: &tailcfg.MapResponse{
  930. Node: &tailcfg.Node{Name: "foo.bar.ts.net."},
  931. Peers: []*tailcfg.Node{
  932. {ID: 1, ExitNodeDNSResolvers: []*dnstype.Resolver{{Addr: "dns.exmaple.com"}}, Hostinfo: hi},
  933. },
  934. },
  935. mr1: &tailcfg.MapResponse{
  936. PeersChanged: []*tailcfg.Node{
  937. {ID: 1, ExitNodeDNSResolvers: []*dnstype.Resolver{{Addr: "dns2.exmaple.com"}}, Hostinfo: hi},
  938. },
  939. },
  940. want: &tailcfg.MapResponse{
  941. PeersChanged: []*tailcfg.Node{
  942. {ID: 1, ExitNodeDNSResolvers: []*dnstype.Resolver{{Addr: "dns2.exmaple.com"}}, Hostinfo: hi},
  943. },
  944. },
  945. },
  946. {
  947. name: "same_exitnoderesolvers",
  948. mr0: &tailcfg.MapResponse{
  949. Node: &tailcfg.Node{Name: "foo.bar.ts.net."},
  950. Peers: []*tailcfg.Node{
  951. {ID: 1, ExitNodeDNSResolvers: []*dnstype.Resolver{{Addr: "dns.exmaple.com"}}, Hostinfo: hi},
  952. },
  953. },
  954. mr1: &tailcfg.MapResponse{
  955. PeersChanged: []*tailcfg.Node{
  956. {ID: 1, ExitNodeDNSResolvers: []*dnstype.Resolver{{Addr: "dns.exmaple.com"}}, Hostinfo: hi},
  957. },
  958. },
  959. want: &tailcfg.MapResponse{},
  960. },
  961. }
  962. for _, tt := range tests {
  963. t.Run(tt.name, func(t *testing.T) {
  964. nu := &countingNetmapUpdater{}
  965. ms := newTestMapSession(t, nu)
  966. ms.updateStateFromResponse(tt.mr0)
  967. mr1 := new(tailcfg.MapResponse)
  968. must.Do(json.Unmarshal(must.Get(json.Marshal(tt.mr1)), mr1))
  969. ms.patchifyPeersChanged(mr1)
  970. opts := []cmp.Option{
  971. cmp.Comparer(func(a, b netip.AddrPort) bool { return a == b }),
  972. }
  973. if diff := cmp.Diff(tt.want, mr1, opts...); diff != "" {
  974. t.Errorf("wrong result (-want +got):\n%s", diff)
  975. }
  976. })
  977. }
  978. }
  979. func TestUpgradeNode(t *testing.T) {
  980. a1 := netip.MustParsePrefix("0.0.0.1/32")
  981. a2 := netip.MustParsePrefix("0.0.0.2/32")
  982. a3 := netip.MustParsePrefix("0.0.0.3/32")
  983. a4 := netip.MustParsePrefix("0.0.0.4/32")
  984. tests := []struct {
  985. name string
  986. in *tailcfg.Node
  987. want *tailcfg.Node
  988. also func(t *testing.T, got *tailcfg.Node) // optional
  989. }{
  990. {
  991. name: "nil",
  992. in: nil,
  993. want: nil,
  994. },
  995. {
  996. name: "empty",
  997. in: new(tailcfg.Node),
  998. want: new(tailcfg.Node),
  999. },
  1000. {
  1001. name: "derp-both",
  1002. in: &tailcfg.Node{HomeDERP: 1, LegacyDERPString: tailcfg.DerpMagicIP + ":2"},
  1003. want: &tailcfg.Node{HomeDERP: 1},
  1004. },
  1005. {
  1006. name: "derp-str-only",
  1007. in: &tailcfg.Node{LegacyDERPString: tailcfg.DerpMagicIP + ":2"},
  1008. want: &tailcfg.Node{HomeDERP: 2},
  1009. },
  1010. {
  1011. name: "derp-int-only",
  1012. in: &tailcfg.Node{HomeDERP: 2},
  1013. want: &tailcfg.Node{HomeDERP: 2},
  1014. },
  1015. {
  1016. name: "implicit-allowed-ips-all-set",
  1017. in: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}, AllowedIPs: []netip.Prefix{a3, a4}},
  1018. want: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}, AllowedIPs: []netip.Prefix{a3, a4}},
  1019. },
  1020. {
  1021. name: "implicit-allowed-ips-only-address-set",
  1022. in: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}},
  1023. want: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}, AllowedIPs: []netip.Prefix{a1, a2}},
  1024. also: func(t *testing.T, got *tailcfg.Node) {
  1025. if t.Failed() {
  1026. return
  1027. }
  1028. if &got.Addresses[0] == &got.AllowedIPs[0] {
  1029. t.Error("Addresses and AllowIPs alias the same memory")
  1030. }
  1031. },
  1032. },
  1033. {
  1034. name: "implicit-allowed-ips-set-empty-slice",
  1035. in: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}, AllowedIPs: []netip.Prefix{}},
  1036. want: &tailcfg.Node{Addresses: []netip.Prefix{a1, a2}, AllowedIPs: []netip.Prefix{}},
  1037. },
  1038. }
  1039. for _, tt := range tests {
  1040. t.Run(tt.name, func(t *testing.T) {
  1041. var got *tailcfg.Node
  1042. if tt.in != nil {
  1043. got = ptr.To(*tt.in) // shallow clone
  1044. }
  1045. upgradeNode(got)
  1046. if diff := cmp.Diff(tt.want, got); diff != "" {
  1047. t.Errorf("wrong result (-want +got):\n%s", diff)
  1048. }
  1049. if tt.also != nil {
  1050. tt.also(t, got)
  1051. }
  1052. })
  1053. }
  1054. }
  1055. func BenchmarkMapSessionDelta(b *testing.B) {
  1056. for _, size := range []int{10, 100, 1_000, 10_000} {
  1057. b.Run(fmt.Sprintf("size_%d", size), func(b *testing.B) {
  1058. ctx := context.Background()
  1059. nu := &countingNetmapUpdater{}
  1060. ms := newTestMapSession(b, nu)
  1061. res := &tailcfg.MapResponse{
  1062. Node: &tailcfg.Node{
  1063. ID: 1,
  1064. Name: "foo.bar.ts.net.",
  1065. },
  1066. }
  1067. for i := range size {
  1068. res.Peers = append(res.Peers, &tailcfg.Node{
  1069. ID: tailcfg.NodeID(i + 2),
  1070. Name: fmt.Sprintf("peer%d.bar.ts.net.", i),
  1071. HomeDERP: 10,
  1072. Addresses: []netip.Prefix{netip.MustParsePrefix("100.100.2.3/32"), netip.MustParsePrefix("fd7a:115c:a1e0::123/128")},
  1073. AllowedIPs: []netip.Prefix{netip.MustParsePrefix("100.100.2.3/32"), netip.MustParsePrefix("fd7a:115c:a1e0::123/128")},
  1074. Endpoints: eps("192.168.1.2:345", "192.168.1.3:678"),
  1075. Hostinfo: (&tailcfg.Hostinfo{
  1076. OS: "fooOS",
  1077. Hostname: "MyHostname",
  1078. Services: []tailcfg.Service{
  1079. {Proto: "peerapi4", Port: 1234},
  1080. {Proto: "peerapi6", Port: 1234},
  1081. {Proto: "peerapi-dns-proxy", Port: 1},
  1082. },
  1083. }).View(),
  1084. LastSeen: ptr.To(time.Unix(int64(i), 0)),
  1085. })
  1086. }
  1087. ms.HandleNonKeepAliveMapResponse(ctx, res)
  1088. b.ResetTimer()
  1089. b.ReportAllocs()
  1090. // Now for the core of the benchmark loop, just toggle
  1091. // a single node's online status.
  1092. for i := range b.N {
  1093. if err := ms.HandleNonKeepAliveMapResponse(ctx, &tailcfg.MapResponse{
  1094. OnlineChange: map[tailcfg.NodeID]bool{
  1095. 2: i%2 == 0,
  1096. },
  1097. }); err != nil {
  1098. b.Fatal(err)
  1099. }
  1100. }
  1101. })
  1102. }
  1103. }
  1104. // TestNetmapDisplayMessage checks that the various diff operations
  1105. // (add/update/delete/clear) for [tailcfg.DisplayMessage] in a
  1106. // [tailcfg.MapResponse] work as expected.
  1107. func TestNetmapDisplayMessage(t *testing.T) {
  1108. type test struct {
  1109. name string
  1110. initialState *tailcfg.MapResponse
  1111. mapResponse tailcfg.MapResponse
  1112. wantMessages map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage
  1113. }
  1114. tests := []test{
  1115. {
  1116. name: "basic-set",
  1117. mapResponse: tailcfg.MapResponse{
  1118. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1119. "test-message": {
  1120. Title: "Testing",
  1121. Text: "This is a test message",
  1122. Severity: tailcfg.SeverityHigh,
  1123. ImpactsConnectivity: true,
  1124. PrimaryAction: &tailcfg.DisplayMessageAction{
  1125. URL: "https://www.example.com",
  1126. Label: "Learn more",
  1127. },
  1128. },
  1129. },
  1130. },
  1131. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{
  1132. "test-message": {
  1133. Title: "Testing",
  1134. Text: "This is a test message",
  1135. Severity: tailcfg.SeverityHigh,
  1136. ImpactsConnectivity: true,
  1137. PrimaryAction: &tailcfg.DisplayMessageAction{
  1138. URL: "https://www.example.com",
  1139. Label: "Learn more",
  1140. },
  1141. },
  1142. },
  1143. },
  1144. {
  1145. name: "delete-one",
  1146. initialState: &tailcfg.MapResponse{
  1147. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1148. "message-a": {
  1149. Title: "Message A",
  1150. },
  1151. "message-b": {
  1152. Title: "Message B",
  1153. },
  1154. },
  1155. },
  1156. mapResponse: tailcfg.MapResponse{
  1157. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1158. "message-a": nil,
  1159. },
  1160. },
  1161. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{
  1162. "message-b": {
  1163. Title: "Message B",
  1164. },
  1165. },
  1166. },
  1167. {
  1168. name: "update-one",
  1169. initialState: &tailcfg.MapResponse{
  1170. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1171. "message-a": {
  1172. Title: "Message A",
  1173. },
  1174. "message-b": {
  1175. Title: "Message B",
  1176. },
  1177. },
  1178. },
  1179. mapResponse: tailcfg.MapResponse{
  1180. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1181. "message-a": {
  1182. Title: "Message A updated",
  1183. },
  1184. },
  1185. },
  1186. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{
  1187. "message-a": {
  1188. Title: "Message A updated",
  1189. },
  1190. "message-b": {
  1191. Title: "Message B",
  1192. },
  1193. },
  1194. },
  1195. {
  1196. name: "add-one",
  1197. initialState: &tailcfg.MapResponse{
  1198. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1199. "message-a": {
  1200. Title: "Message A",
  1201. },
  1202. },
  1203. },
  1204. mapResponse: tailcfg.MapResponse{
  1205. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1206. "message-b": {
  1207. Title: "Message B",
  1208. },
  1209. },
  1210. },
  1211. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{
  1212. "message-a": {
  1213. Title: "Message A",
  1214. },
  1215. "message-b": {
  1216. Title: "Message B",
  1217. },
  1218. },
  1219. },
  1220. {
  1221. name: "delete-all",
  1222. initialState: &tailcfg.MapResponse{
  1223. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1224. "message-a": {
  1225. Title: "Message A",
  1226. },
  1227. "message-b": {
  1228. Title: "Message B",
  1229. },
  1230. },
  1231. },
  1232. mapResponse: tailcfg.MapResponse{
  1233. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1234. "*": nil,
  1235. },
  1236. },
  1237. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{},
  1238. },
  1239. {
  1240. name: "delete-all-and-add",
  1241. initialState: &tailcfg.MapResponse{
  1242. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1243. "message-a": {
  1244. Title: "Message A",
  1245. },
  1246. "message-b": {
  1247. Title: "Message B",
  1248. },
  1249. },
  1250. },
  1251. mapResponse: tailcfg.MapResponse{
  1252. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1253. "*": nil,
  1254. "message-c": {
  1255. Title: "Message C",
  1256. },
  1257. },
  1258. },
  1259. wantMessages: map[tailcfg.DisplayMessageID]tailcfg.DisplayMessage{
  1260. "message-c": {
  1261. Title: "Message C",
  1262. },
  1263. },
  1264. },
  1265. }
  1266. for _, test := range tests {
  1267. t.Run(test.name, func(t *testing.T) {
  1268. ms := newTestMapSession(t, nil)
  1269. if test.initialState != nil {
  1270. ms.netmapForResponse(test.initialState)
  1271. }
  1272. nm := ms.netmapForResponse(&test.mapResponse)
  1273. if diff := cmp.Diff(test.wantMessages, nm.DisplayMessages, cmpopts.EquateEmpty()); diff != "" {
  1274. t.Errorf("unexpected warnings (-want +got):\n%s", diff)
  1275. }
  1276. })
  1277. }
  1278. }
  1279. // TestNetmapHealthIntegration checks that we get the expected health warnings
  1280. // from processing a [tailcfg.MapResponse] containing health messages and passing the
  1281. // [netmap.NetworkMap] to a [health.Tracker].
  1282. func TestNetmapHealthIntegration(t *testing.T) {
  1283. ms := newTestMapSession(t, nil)
  1284. ht := health.Tracker{}
  1285. ht.SetIPNState("NeedsLogin", true)
  1286. ht.GotStreamedMapResponse()
  1287. nm := ms.netmapForResponse(&tailcfg.MapResponse{
  1288. Health: []string{
  1289. "Test message",
  1290. "Another message",
  1291. },
  1292. })
  1293. ht.SetControlHealth(nm.DisplayMessages)
  1294. want := map[health.WarnableCode]health.UnhealthyState{
  1295. "control-health.health-c0719e9a8d5d838d861dc6f675c899d2b309a3a65bb9fe6b11e5afcbf9a2c0b1": {
  1296. WarnableCode: "control-health.health-c0719e9a8d5d838d861dc6f675c899d2b309a3a65bb9fe6b11e5afcbf9a2c0b1",
  1297. Title: "Coordination server reports an issue",
  1298. Severity: health.SeverityMedium,
  1299. Text: "The coordination server is reporting a health issue: Test message",
  1300. },
  1301. "control-health.health-1dc7017a73a3c55c0d6a8423e3813c7ab6562d9d3064c2ec6ac7822f61b1db9c": {
  1302. WarnableCode: "control-health.health-1dc7017a73a3c55c0d6a8423e3813c7ab6562d9d3064c2ec6ac7822f61b1db9c",
  1303. Title: "Coordination server reports an issue",
  1304. Severity: health.SeverityMedium,
  1305. Text: "The coordination server is reporting a health issue: Another message",
  1306. },
  1307. }
  1308. got := maps.Clone(ht.CurrentState().Warnings)
  1309. for k := range got {
  1310. if !strings.HasPrefix(string(k), "control-health") {
  1311. delete(got, k)
  1312. }
  1313. }
  1314. if d := cmp.Diff(want, got, cmpopts.IgnoreFields(health.UnhealthyState{}, "ETag")); d != "" {
  1315. t.Fatalf("CurrentStatus().Warnings[\"control-health*\"] different than expected (-want +got)\n%s", d)
  1316. }
  1317. }
  1318. // TestNetmapDisplayMessageIntegration checks that we get the expected health
  1319. // warnings from processing a [tailcfg.MapResponse] that contains DisplayMessages and
  1320. // passing the [netmap.NetworkMap] to a [health.Tracker].
  1321. func TestNetmapDisplayMessageIntegration(t *testing.T) {
  1322. ms := newTestMapSession(t, nil)
  1323. ht := health.Tracker{}
  1324. ht.SetIPNState("NeedsLogin", true)
  1325. ht.GotStreamedMapResponse()
  1326. baseWarnings := ht.CurrentState().Warnings
  1327. nm := ms.netmapForResponse(&tailcfg.MapResponse{
  1328. DisplayMessages: map[tailcfg.DisplayMessageID]*tailcfg.DisplayMessage{
  1329. "test-message": {
  1330. Title: "Testing",
  1331. Text: "This is a test message",
  1332. Severity: tailcfg.SeverityHigh,
  1333. ImpactsConnectivity: true,
  1334. PrimaryAction: &tailcfg.DisplayMessageAction{
  1335. URL: "https://www.example.com",
  1336. Label: "Learn more",
  1337. },
  1338. },
  1339. },
  1340. })
  1341. ht.SetControlHealth(nm.DisplayMessages)
  1342. state := ht.CurrentState()
  1343. // Ignore warnings that aren't from the netmap
  1344. for k := range baseWarnings {
  1345. delete(state.Warnings, k)
  1346. }
  1347. want := map[health.WarnableCode]health.UnhealthyState{
  1348. "control-health.test-message": {
  1349. WarnableCode: "control-health.test-message",
  1350. Title: "Testing",
  1351. Text: "This is a test message",
  1352. Severity: health.SeverityHigh,
  1353. ImpactsConnectivity: true,
  1354. PrimaryAction: &health.UnhealthyStateAction{
  1355. URL: "https://www.example.com",
  1356. Label: "Learn more",
  1357. },
  1358. },
  1359. }
  1360. if diff := cmp.Diff(want, state.Warnings, cmpopts.IgnoreFields(health.UnhealthyState{}, "ETag")); diff != "" {
  1361. t.Errorf("unexpected message contents (-want +got):\n%s", diff)
  1362. }
  1363. }