topology_test.go 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. // Copyright 2019 the Kilo authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mesh
  15. import (
  16. "net"
  17. "strings"
  18. "testing"
  19. "github.com/kylelemons/godebug/pretty"
  20. "github.com/squat/kilo/pkg/encapsulation"
  21. "github.com/squat/kilo/pkg/wireguard"
  22. "github.com/vishvananda/netlink"
  23. "golang.org/x/sys/unix"
  24. )
  25. func allowedIPs(ips ...string) string {
  26. return strings.Join(ips, ", ")
  27. }
  28. func setup(t *testing.T) (map[string]*Node, map[string]*Peer, []byte, uint32) {
  29. key := []byte("private")
  30. e1 := &net.IPNet{IP: net.ParseIP("10.1.0.1").To4(), Mask: net.CIDRMask(16, 32)}
  31. e2 := &net.IPNet{IP: net.ParseIP("10.1.0.2").To4(), Mask: net.CIDRMask(16, 32)}
  32. e3 := &net.IPNet{IP: net.ParseIP("10.1.0.3").To4(), Mask: net.CIDRMask(16, 32)}
  33. i1 := &net.IPNet{IP: net.ParseIP("192.168.0.1").To4(), Mask: net.CIDRMask(32, 32)}
  34. i2 := &net.IPNet{IP: net.ParseIP("192.168.0.2").To4(), Mask: net.CIDRMask(32, 32)}
  35. nodes := map[string]*Node{
  36. "a": {
  37. Name: "a",
  38. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  39. InternalIP: i1,
  40. Location: "1",
  41. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  42. Key: []byte("key1"),
  43. PersistentKeepalive: 25,
  44. },
  45. "b": {
  46. Name: "b",
  47. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  48. InternalIP: i1,
  49. Location: "2",
  50. Subnet: &net.IPNet{IP: net.ParseIP("10.2.2.0"), Mask: net.CIDRMask(24, 32)},
  51. Key: []byte("key2"),
  52. },
  53. "c": {
  54. Name: "c",
  55. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e3.IP}, Port: DefaultKiloPort},
  56. InternalIP: i2,
  57. // Same location a node b.
  58. Location: "2",
  59. Subnet: &net.IPNet{IP: net.ParseIP("10.2.3.0"), Mask: net.CIDRMask(24, 32)},
  60. Key: []byte("key3"),
  61. },
  62. }
  63. peers := map[string]*Peer{
  64. "a": {
  65. Name: "a",
  66. Peer: wireguard.Peer{
  67. AllowedIPs: []*net.IPNet{
  68. {IP: net.ParseIP("10.5.0.1"), Mask: net.CIDRMask(24, 32)},
  69. {IP: net.ParseIP("10.5.0.2"), Mask: net.CIDRMask(24, 32)},
  70. },
  71. PublicKey: []byte("key4"),
  72. },
  73. },
  74. "b": {
  75. Name: "b",
  76. Peer: wireguard.Peer{
  77. AllowedIPs: []*net.IPNet{
  78. {IP: net.ParseIP("10.5.0.3"), Mask: net.CIDRMask(24, 32)},
  79. },
  80. Endpoint: &wireguard.Endpoint{
  81. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("192.168.0.1")},
  82. Port: DefaultKiloPort,
  83. },
  84. PublicKey: []byte("key5"),
  85. },
  86. },
  87. }
  88. return nodes, peers, key, DefaultKiloPort
  89. }
  90. func TestNewTopology(t *testing.T) {
  91. nodes, peers, key, port := setup(t)
  92. w1 := net.ParseIP("10.4.0.1").To4()
  93. w2 := net.ParseIP("10.4.0.2").To4()
  94. w3 := net.ParseIP("10.4.0.3").To4()
  95. for _, tc := range []struct {
  96. name string
  97. granularity Granularity
  98. hostname string
  99. result *Topology
  100. }{
  101. {
  102. name: "logical from a",
  103. granularity: LogicalGranularity,
  104. hostname: nodes["a"].Name,
  105. result: &Topology{
  106. hostname: nodes["a"].Name,
  107. leader: true,
  108. location: nodes["a"].Location,
  109. subnet: nodes["a"].Subnet,
  110. privateIP: nodes["a"].InternalIP,
  111. wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
  112. segments: []*segment{
  113. {
  114. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  115. endpoint: nodes["a"].Endpoint,
  116. key: nodes["a"].Key,
  117. location: nodes["a"].Location,
  118. cidrs: []*net.IPNet{nodes["a"].Subnet},
  119. hostnames: []string{"a"},
  120. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  121. wireGuardIP: w1,
  122. },
  123. {
  124. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  125. endpoint: nodes["b"].Endpoint,
  126. key: nodes["b"].Key,
  127. location: nodes["b"].Location,
  128. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  129. hostnames: []string{"b", "c"},
  130. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  131. wireGuardIP: w2,
  132. },
  133. },
  134. peers: []*Peer{peers["a"], peers["b"]},
  135. },
  136. },
  137. {
  138. name: "logical from b",
  139. granularity: LogicalGranularity,
  140. hostname: nodes["b"].Name,
  141. result: &Topology{
  142. hostname: nodes["b"].Name,
  143. leader: true,
  144. location: nodes["b"].Location,
  145. subnet: nodes["b"].Subnet,
  146. privateIP: nodes["b"].InternalIP,
  147. wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
  148. segments: []*segment{
  149. {
  150. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  151. endpoint: nodes["a"].Endpoint,
  152. key: nodes["a"].Key,
  153. location: nodes["a"].Location,
  154. cidrs: []*net.IPNet{nodes["a"].Subnet},
  155. hostnames: []string{"a"},
  156. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  157. wireGuardIP: w1,
  158. },
  159. {
  160. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  161. endpoint: nodes["b"].Endpoint,
  162. key: nodes["b"].Key,
  163. location: nodes["b"].Location,
  164. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  165. hostnames: []string{"b", "c"},
  166. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  167. wireGuardIP: w2,
  168. },
  169. },
  170. peers: []*Peer{peers["a"], peers["b"]},
  171. },
  172. },
  173. {
  174. name: "logical from c",
  175. granularity: LogicalGranularity,
  176. hostname: nodes["c"].Name,
  177. result: &Topology{
  178. hostname: nodes["c"].Name,
  179. leader: false,
  180. location: nodes["b"].Location,
  181. subnet: nodes["c"].Subnet,
  182. privateIP: nodes["c"].InternalIP,
  183. wireGuardCIDR: nil,
  184. segments: []*segment{
  185. {
  186. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  187. endpoint: nodes["a"].Endpoint,
  188. key: nodes["a"].Key,
  189. location: nodes["a"].Location,
  190. cidrs: []*net.IPNet{nodes["a"].Subnet},
  191. hostnames: []string{"a"},
  192. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  193. wireGuardIP: w1,
  194. },
  195. {
  196. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  197. endpoint: nodes["b"].Endpoint,
  198. key: nodes["b"].Key,
  199. location: nodes["b"].Location,
  200. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  201. hostnames: []string{"b", "c"},
  202. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  203. wireGuardIP: w2,
  204. },
  205. },
  206. peers: []*Peer{peers["a"], peers["b"]},
  207. },
  208. },
  209. {
  210. name: "full from a",
  211. granularity: FullGranularity,
  212. hostname: nodes["a"].Name,
  213. result: &Topology{
  214. hostname: nodes["a"].Name,
  215. leader: true,
  216. location: nodes["a"].Name,
  217. subnet: nodes["a"].Subnet,
  218. privateIP: nodes["a"].InternalIP,
  219. wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
  220. segments: []*segment{
  221. {
  222. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  223. endpoint: nodes["a"].Endpoint,
  224. key: nodes["a"].Key,
  225. location: nodes["a"].Name,
  226. cidrs: []*net.IPNet{nodes["a"].Subnet},
  227. hostnames: []string{"a"},
  228. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  229. wireGuardIP: w1,
  230. },
  231. {
  232. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  233. endpoint: nodes["b"].Endpoint,
  234. key: nodes["b"].Key,
  235. location: nodes["b"].Name,
  236. cidrs: []*net.IPNet{nodes["b"].Subnet},
  237. hostnames: []string{"b"},
  238. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  239. wireGuardIP: w2,
  240. },
  241. {
  242. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  243. endpoint: nodes["c"].Endpoint,
  244. key: nodes["c"].Key,
  245. location: nodes["c"].Name,
  246. cidrs: []*net.IPNet{nodes["c"].Subnet},
  247. hostnames: []string{"c"},
  248. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  249. wireGuardIP: w3,
  250. },
  251. },
  252. peers: []*Peer{peers["a"], peers["b"]},
  253. },
  254. },
  255. {
  256. name: "full from b",
  257. granularity: FullGranularity,
  258. hostname: nodes["b"].Name,
  259. result: &Topology{
  260. hostname: nodes["b"].Name,
  261. leader: true,
  262. location: nodes["b"].Name,
  263. subnet: nodes["b"].Subnet,
  264. privateIP: nodes["b"].InternalIP,
  265. wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
  266. segments: []*segment{
  267. {
  268. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  269. endpoint: nodes["a"].Endpoint,
  270. key: nodes["a"].Key,
  271. location: nodes["a"].Name,
  272. cidrs: []*net.IPNet{nodes["a"].Subnet},
  273. hostnames: []string{"a"},
  274. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  275. wireGuardIP: w1,
  276. },
  277. {
  278. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  279. endpoint: nodes["b"].Endpoint,
  280. key: nodes["b"].Key,
  281. location: nodes["b"].Name,
  282. cidrs: []*net.IPNet{nodes["b"].Subnet},
  283. hostnames: []string{"b"},
  284. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  285. wireGuardIP: w2,
  286. },
  287. {
  288. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  289. endpoint: nodes["c"].Endpoint,
  290. key: nodes["c"].Key,
  291. location: nodes["c"].Name,
  292. cidrs: []*net.IPNet{nodes["c"].Subnet},
  293. hostnames: []string{"c"},
  294. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  295. wireGuardIP: w3,
  296. },
  297. },
  298. peers: []*Peer{peers["a"], peers["b"]},
  299. },
  300. },
  301. {
  302. name: "full from c",
  303. granularity: FullGranularity,
  304. hostname: nodes["c"].Name,
  305. result: &Topology{
  306. hostname: nodes["c"].Name,
  307. leader: true,
  308. location: nodes["c"].Name,
  309. subnet: nodes["c"].Subnet,
  310. privateIP: nodes["c"].InternalIP,
  311. wireGuardCIDR: &net.IPNet{IP: w3, Mask: net.CIDRMask(16, 32)},
  312. segments: []*segment{
  313. {
  314. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  315. endpoint: nodes["a"].Endpoint,
  316. key: nodes["a"].Key,
  317. location: nodes["a"].Name,
  318. cidrs: []*net.IPNet{nodes["a"].Subnet},
  319. hostnames: []string{"a"},
  320. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  321. wireGuardIP: w1,
  322. },
  323. {
  324. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  325. endpoint: nodes["b"].Endpoint,
  326. key: nodes["b"].Key,
  327. location: nodes["b"].Name,
  328. cidrs: []*net.IPNet{nodes["b"].Subnet},
  329. hostnames: []string{"b"},
  330. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  331. wireGuardIP: w2,
  332. },
  333. {
  334. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  335. endpoint: nodes["c"].Endpoint,
  336. key: nodes["c"].Key,
  337. location: nodes["c"].Name,
  338. cidrs: []*net.IPNet{nodes["c"].Subnet},
  339. hostnames: []string{"c"},
  340. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  341. wireGuardIP: w3,
  342. },
  343. },
  344. peers: []*Peer{peers["a"], peers["b"]},
  345. },
  346. },
  347. } {
  348. tc.result.key = key
  349. tc.result.port = port
  350. topo, err := NewTopology(nodes, peers, tc.granularity, tc.hostname, port, key, DefaultKiloSubnet, 0)
  351. if err != nil {
  352. t.Errorf("test case %q: failed to generate Topology: %v", tc.name, err)
  353. }
  354. if diff := pretty.Compare(topo, tc.result); diff != "" {
  355. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  356. }
  357. }
  358. }
  359. func mustTopo(t *testing.T, nodes map[string]*Node, peers map[string]*Peer, granularity Granularity, hostname string, port uint32, key []byte, subnet *net.IPNet, persistentKeepalive int) *Topology {
  360. topo, err := NewTopology(nodes, peers, granularity, hostname, port, key, subnet, persistentKeepalive)
  361. if err != nil {
  362. t.Errorf("failed to generate Topology: %v", err)
  363. }
  364. return topo
  365. }
  366. func TestRoutes(t *testing.T) {
  367. nodes, peers, key, port := setup(t)
  368. kiloIface := 0
  369. privIface := 1
  370. tunlIface := 2
  371. mustTopoForGranularityAndHost := func(granularity Granularity, hostname string) *Topology {
  372. return mustTopo(t, nodes, peers, granularity, hostname, port, key, DefaultKiloSubnet, 0)
  373. }
  374. for _, tc := range []struct {
  375. name string
  376. local bool
  377. topology *Topology
  378. strategy encapsulation.Strategy
  379. routes []*netlink.Route
  380. rules []*netlink.Rule
  381. }{
  382. {
  383. name: "logical from a",
  384. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name),
  385. strategy: encapsulation.Never,
  386. routes: []*netlink.Route{
  387. {
  388. Dst: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].cidrs[0],
  389. Flags: int(netlink.FLAG_ONLINK),
  390. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  391. LinkIndex: kiloIface,
  392. Protocol: unix.RTPROT_STATIC,
  393. },
  394. {
  395. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  396. Flags: int(netlink.FLAG_ONLINK),
  397. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  398. LinkIndex: kiloIface,
  399. Protocol: unix.RTPROT_STATIC,
  400. },
  401. {
  402. Dst: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].cidrs[1],
  403. Flags: int(netlink.FLAG_ONLINK),
  404. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  405. LinkIndex: kiloIface,
  406. Protocol: unix.RTPROT_STATIC,
  407. },
  408. {
  409. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  410. Flags: int(netlink.FLAG_ONLINK),
  411. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  412. LinkIndex: kiloIface,
  413. Protocol: unix.RTPROT_STATIC,
  414. },
  415. {
  416. Dst: peers["a"].AllowedIPs[0],
  417. LinkIndex: kiloIface,
  418. Protocol: unix.RTPROT_STATIC,
  419. },
  420. {
  421. Dst: peers["a"].AllowedIPs[1],
  422. LinkIndex: kiloIface,
  423. Protocol: unix.RTPROT_STATIC,
  424. },
  425. {
  426. Dst: peers["b"].AllowedIPs[0],
  427. LinkIndex: kiloIface,
  428. Protocol: unix.RTPROT_STATIC,
  429. },
  430. },
  431. },
  432. {
  433. name: "logical from b",
  434. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name),
  435. strategy: encapsulation.Never,
  436. routes: []*netlink.Route{
  437. {
  438. Dst: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].cidrs[0],
  439. Flags: int(netlink.FLAG_ONLINK),
  440. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  441. LinkIndex: kiloIface,
  442. Protocol: unix.RTPROT_STATIC,
  443. },
  444. {
  445. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  446. Flags: int(netlink.FLAG_ONLINK),
  447. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  448. LinkIndex: kiloIface,
  449. Protocol: unix.RTPROT_STATIC,
  450. },
  451. {
  452. Dst: peers["a"].AllowedIPs[0],
  453. LinkIndex: kiloIface,
  454. Protocol: unix.RTPROT_STATIC,
  455. },
  456. {
  457. Dst: peers["a"].AllowedIPs[1],
  458. LinkIndex: kiloIface,
  459. Protocol: unix.RTPROT_STATIC,
  460. },
  461. {
  462. Dst: peers["b"].AllowedIPs[0],
  463. LinkIndex: kiloIface,
  464. Protocol: unix.RTPROT_STATIC,
  465. },
  466. },
  467. },
  468. {
  469. name: "logical from c",
  470. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name),
  471. strategy: encapsulation.Never,
  472. routes: []*netlink.Route{
  473. {
  474. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[0].wireGuardIP),
  475. Flags: int(netlink.FLAG_ONLINK),
  476. Gw: nodes["b"].InternalIP.IP,
  477. LinkIndex: privIface,
  478. Protocol: unix.RTPROT_STATIC,
  479. },
  480. {
  481. Dst: mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[0].cidrs[0],
  482. Flags: int(netlink.FLAG_ONLINK),
  483. Gw: nodes["b"].InternalIP.IP,
  484. LinkIndex: privIface,
  485. Protocol: unix.RTPROT_STATIC,
  486. },
  487. {
  488. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  489. Flags: int(netlink.FLAG_ONLINK),
  490. Gw: nodes["b"].InternalIP.IP,
  491. LinkIndex: privIface,
  492. Protocol: unix.RTPROT_STATIC,
  493. },
  494. {
  495. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[1].wireGuardIP),
  496. Flags: int(netlink.FLAG_ONLINK),
  497. Gw: nodes["b"].InternalIP.IP,
  498. LinkIndex: privIface,
  499. Protocol: unix.RTPROT_STATIC,
  500. },
  501. {
  502. Dst: peers["a"].AllowedIPs[0],
  503. Flags: int(netlink.FLAG_ONLINK),
  504. Gw: nodes["b"].InternalIP.IP,
  505. LinkIndex: privIface,
  506. Protocol: unix.RTPROT_STATIC,
  507. },
  508. {
  509. Dst: peers["a"].AllowedIPs[1],
  510. Flags: int(netlink.FLAG_ONLINK),
  511. Gw: nodes["b"].InternalIP.IP,
  512. LinkIndex: privIface,
  513. Protocol: unix.RTPROT_STATIC,
  514. },
  515. {
  516. Dst: peers["b"].AllowedIPs[0],
  517. Flags: int(netlink.FLAG_ONLINK),
  518. Gw: nodes["b"].InternalIP.IP,
  519. LinkIndex: privIface,
  520. Protocol: unix.RTPROT_STATIC,
  521. },
  522. },
  523. },
  524. {
  525. name: "full from a",
  526. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name),
  527. strategy: encapsulation.Never,
  528. routes: []*netlink.Route{
  529. {
  530. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[1].cidrs[0],
  531. Flags: int(netlink.FLAG_ONLINK),
  532. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  533. LinkIndex: kiloIface,
  534. Protocol: unix.RTPROT_STATIC,
  535. },
  536. {
  537. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  538. Flags: int(netlink.FLAG_ONLINK),
  539. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  540. LinkIndex: kiloIface,
  541. Protocol: unix.RTPROT_STATIC,
  542. },
  543. {
  544. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[2].cidrs[0],
  545. Flags: int(netlink.FLAG_ONLINK),
  546. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[2].wireGuardIP,
  547. LinkIndex: kiloIface,
  548. Protocol: unix.RTPROT_STATIC,
  549. },
  550. {
  551. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  552. Flags: int(netlink.FLAG_ONLINK),
  553. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[2].wireGuardIP,
  554. LinkIndex: kiloIface,
  555. Protocol: unix.RTPROT_STATIC,
  556. },
  557. {
  558. Dst: peers["a"].AllowedIPs[0],
  559. LinkIndex: kiloIface,
  560. Protocol: unix.RTPROT_STATIC,
  561. },
  562. {
  563. Dst: peers["a"].AllowedIPs[1],
  564. LinkIndex: kiloIface,
  565. Protocol: unix.RTPROT_STATIC,
  566. },
  567. {
  568. Dst: peers["b"].AllowedIPs[0],
  569. LinkIndex: kiloIface,
  570. Protocol: unix.RTPROT_STATIC,
  571. },
  572. },
  573. },
  574. {
  575. name: "full from b",
  576. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name),
  577. strategy: encapsulation.Never,
  578. routes: []*netlink.Route{
  579. {
  580. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[0].cidrs[0],
  581. Flags: int(netlink.FLAG_ONLINK),
  582. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  583. LinkIndex: kiloIface,
  584. Protocol: unix.RTPROT_STATIC,
  585. },
  586. {
  587. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  588. Flags: int(netlink.FLAG_ONLINK),
  589. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  590. LinkIndex: kiloIface,
  591. Protocol: unix.RTPROT_STATIC,
  592. },
  593. {
  594. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[2].cidrs[0],
  595. Flags: int(netlink.FLAG_ONLINK),
  596. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[2].wireGuardIP,
  597. LinkIndex: kiloIface,
  598. Protocol: unix.RTPROT_STATIC,
  599. },
  600. {
  601. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  602. Flags: int(netlink.FLAG_ONLINK),
  603. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[2].wireGuardIP,
  604. LinkIndex: kiloIface,
  605. Protocol: unix.RTPROT_STATIC,
  606. },
  607. {
  608. Dst: peers["a"].AllowedIPs[0],
  609. LinkIndex: kiloIface,
  610. Protocol: unix.RTPROT_STATIC,
  611. },
  612. {
  613. Dst: peers["a"].AllowedIPs[1],
  614. LinkIndex: kiloIface,
  615. Protocol: unix.RTPROT_STATIC,
  616. },
  617. {
  618. Dst: peers["b"].AllowedIPs[0],
  619. LinkIndex: kiloIface,
  620. Protocol: unix.RTPROT_STATIC,
  621. },
  622. },
  623. },
  624. {
  625. name: "full from c",
  626. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name),
  627. strategy: encapsulation.Never,
  628. routes: []*netlink.Route{
  629. {
  630. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[0].cidrs[0],
  631. Flags: int(netlink.FLAG_ONLINK),
  632. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[0].wireGuardIP,
  633. LinkIndex: kiloIface,
  634. Protocol: unix.RTPROT_STATIC,
  635. },
  636. {
  637. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  638. Flags: int(netlink.FLAG_ONLINK),
  639. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[0].wireGuardIP,
  640. LinkIndex: kiloIface,
  641. Protocol: unix.RTPROT_STATIC,
  642. },
  643. {
  644. Dst: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[1].cidrs[0],
  645. Flags: int(netlink.FLAG_ONLINK),
  646. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[1].wireGuardIP,
  647. LinkIndex: kiloIface,
  648. Protocol: unix.RTPROT_STATIC,
  649. },
  650. {
  651. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  652. Flags: int(netlink.FLAG_ONLINK),
  653. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[1].wireGuardIP,
  654. LinkIndex: kiloIface,
  655. Protocol: unix.RTPROT_STATIC,
  656. },
  657. {
  658. Dst: peers["a"].AllowedIPs[0],
  659. LinkIndex: kiloIface,
  660. Protocol: unix.RTPROT_STATIC,
  661. },
  662. {
  663. Dst: peers["a"].AllowedIPs[1],
  664. LinkIndex: kiloIface,
  665. Protocol: unix.RTPROT_STATIC,
  666. },
  667. {
  668. Dst: peers["b"].AllowedIPs[0],
  669. LinkIndex: kiloIface,
  670. Protocol: unix.RTPROT_STATIC,
  671. },
  672. },
  673. },
  674. {
  675. name: "logical from a local",
  676. local: true,
  677. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name),
  678. strategy: encapsulation.Never,
  679. routes: []*netlink.Route{
  680. {
  681. Dst: nodes["b"].Subnet,
  682. Flags: int(netlink.FLAG_ONLINK),
  683. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  684. LinkIndex: kiloIface,
  685. Protocol: unix.RTPROT_STATIC,
  686. },
  687. {
  688. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  689. Flags: int(netlink.FLAG_ONLINK),
  690. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  691. LinkIndex: kiloIface,
  692. Protocol: unix.RTPROT_STATIC,
  693. },
  694. {
  695. Dst: nodes["c"].Subnet,
  696. Flags: int(netlink.FLAG_ONLINK),
  697. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  698. LinkIndex: kiloIface,
  699. Protocol: unix.RTPROT_STATIC,
  700. },
  701. {
  702. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  703. Flags: int(netlink.FLAG_ONLINK),
  704. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  705. LinkIndex: kiloIface,
  706. Protocol: unix.RTPROT_STATIC,
  707. },
  708. {
  709. Dst: peers["a"].AllowedIPs[0],
  710. LinkIndex: kiloIface,
  711. Protocol: unix.RTPROT_STATIC,
  712. },
  713. {
  714. Dst: peers["a"].AllowedIPs[1],
  715. LinkIndex: kiloIface,
  716. Protocol: unix.RTPROT_STATIC,
  717. },
  718. {
  719. Dst: peers["b"].AllowedIPs[0],
  720. LinkIndex: kiloIface,
  721. Protocol: unix.RTPROT_STATIC,
  722. },
  723. },
  724. },
  725. {
  726. name: "logical from a local always",
  727. local: true,
  728. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name),
  729. strategy: encapsulation.Always,
  730. routes: []*netlink.Route{
  731. {
  732. Dst: nodes["b"].Subnet,
  733. Flags: int(netlink.FLAG_ONLINK),
  734. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  735. LinkIndex: kiloIface,
  736. Protocol: unix.RTPROT_STATIC,
  737. },
  738. {
  739. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  740. Flags: int(netlink.FLAG_ONLINK),
  741. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  742. LinkIndex: kiloIface,
  743. Protocol: unix.RTPROT_STATIC,
  744. },
  745. {
  746. Dst: nodes["c"].Subnet,
  747. Flags: int(netlink.FLAG_ONLINK),
  748. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  749. LinkIndex: kiloIface,
  750. Protocol: unix.RTPROT_STATIC,
  751. },
  752. {
  753. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  754. Flags: int(netlink.FLAG_ONLINK),
  755. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  756. LinkIndex: kiloIface,
  757. Protocol: unix.RTPROT_STATIC,
  758. },
  759. {
  760. Dst: peers["a"].AllowedIPs[0],
  761. LinkIndex: kiloIface,
  762. Protocol: unix.RTPROT_STATIC,
  763. },
  764. {
  765. Dst: peers["a"].AllowedIPs[1],
  766. LinkIndex: kiloIface,
  767. Protocol: unix.RTPROT_STATIC,
  768. },
  769. {
  770. Dst: peers["b"].AllowedIPs[0],
  771. LinkIndex: kiloIface,
  772. Protocol: unix.RTPROT_STATIC,
  773. },
  774. },
  775. },
  776. {
  777. name: "logical from b local",
  778. local: true,
  779. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name),
  780. strategy: encapsulation.Never,
  781. routes: []*netlink.Route{
  782. {
  783. Dst: nodes["a"].Subnet,
  784. Flags: int(netlink.FLAG_ONLINK),
  785. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  786. LinkIndex: kiloIface,
  787. Protocol: unix.RTPROT_STATIC,
  788. },
  789. {
  790. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  791. Flags: int(netlink.FLAG_ONLINK),
  792. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  793. LinkIndex: kiloIface,
  794. Protocol: unix.RTPROT_STATIC,
  795. },
  796. {
  797. Dst: nodes["c"].Subnet,
  798. Flags: int(netlink.FLAG_ONLINK),
  799. Gw: nodes["c"].InternalIP.IP,
  800. LinkIndex: privIface,
  801. Protocol: unix.RTPROT_STATIC,
  802. },
  803. {
  804. Dst: peers["a"].AllowedIPs[0],
  805. LinkIndex: kiloIface,
  806. Protocol: unix.RTPROT_STATIC,
  807. },
  808. {
  809. Dst: peers["a"].AllowedIPs[1],
  810. LinkIndex: kiloIface,
  811. Protocol: unix.RTPROT_STATIC,
  812. },
  813. {
  814. Dst: peers["b"].AllowedIPs[0],
  815. LinkIndex: kiloIface,
  816. Protocol: unix.RTPROT_STATIC,
  817. },
  818. },
  819. },
  820. {
  821. name: "logical from b local always",
  822. local: true,
  823. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name),
  824. strategy: encapsulation.Always,
  825. routes: []*netlink.Route{
  826. {
  827. Dst: nodes["a"].Subnet,
  828. Flags: int(netlink.FLAG_ONLINK),
  829. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  830. LinkIndex: kiloIface,
  831. Protocol: unix.RTPROT_STATIC,
  832. },
  833. {
  834. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  835. Flags: int(netlink.FLAG_ONLINK),
  836. Gw: mustTopoForGranularityAndHost(LogicalGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  837. LinkIndex: kiloIface,
  838. Protocol: unix.RTPROT_STATIC,
  839. },
  840. {
  841. Dst: nodes["c"].Subnet,
  842. Flags: int(netlink.FLAG_ONLINK),
  843. Gw: nodes["c"].InternalIP.IP,
  844. LinkIndex: tunlIface,
  845. Protocol: unix.RTPROT_STATIC,
  846. },
  847. {
  848. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  849. Flags: int(netlink.FLAG_ONLINK),
  850. Gw: nodes["c"].InternalIP.IP,
  851. LinkIndex: tunlIface,
  852. Protocol: unix.RTPROT_STATIC,
  853. Table: kiloTableIndex,
  854. },
  855. {
  856. Dst: peers["a"].AllowedIPs[0],
  857. LinkIndex: kiloIface,
  858. Protocol: unix.RTPROT_STATIC,
  859. },
  860. {
  861. Dst: peers["a"].AllowedIPs[1],
  862. LinkIndex: kiloIface,
  863. Protocol: unix.RTPROT_STATIC,
  864. },
  865. {
  866. Dst: peers["b"].AllowedIPs[0],
  867. LinkIndex: kiloIface,
  868. Protocol: unix.RTPROT_STATIC,
  869. },
  870. },
  871. rules: []*netlink.Rule{
  872. defaultRule(&netlink.Rule{
  873. Src: nodes["b"].Subnet,
  874. Dst: nodes["c"].InternalIP,
  875. Table: kiloTableIndex,
  876. }),
  877. defaultRule(&netlink.Rule{
  878. Dst: nodes["c"].InternalIP,
  879. IifName: DefaultKiloInterface,
  880. Table: kiloTableIndex,
  881. }),
  882. },
  883. },
  884. {
  885. name: "logical from c local",
  886. local: true,
  887. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name),
  888. strategy: encapsulation.Never,
  889. routes: []*netlink.Route{
  890. {
  891. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[0].wireGuardIP),
  892. Flags: int(netlink.FLAG_ONLINK),
  893. Gw: nodes["b"].InternalIP.IP,
  894. LinkIndex: privIface,
  895. Protocol: unix.RTPROT_STATIC,
  896. },
  897. {
  898. Dst: nodes["a"].Subnet,
  899. Flags: int(netlink.FLAG_ONLINK),
  900. Gw: nodes["b"].InternalIP.IP,
  901. LinkIndex: privIface,
  902. Protocol: unix.RTPROT_STATIC,
  903. },
  904. {
  905. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  906. Flags: int(netlink.FLAG_ONLINK),
  907. Gw: nodes["b"].InternalIP.IP,
  908. LinkIndex: privIface,
  909. Protocol: unix.RTPROT_STATIC,
  910. },
  911. {
  912. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[1].wireGuardIP),
  913. Flags: int(netlink.FLAG_ONLINK),
  914. Gw: nodes["b"].InternalIP.IP,
  915. LinkIndex: privIface,
  916. Protocol: unix.RTPROT_STATIC,
  917. },
  918. {
  919. Dst: nodes["b"].Subnet,
  920. Flags: int(netlink.FLAG_ONLINK),
  921. Gw: nodes["b"].InternalIP.IP,
  922. LinkIndex: privIface,
  923. Protocol: unix.RTPROT_STATIC,
  924. },
  925. {
  926. Dst: peers["a"].AllowedIPs[0],
  927. Flags: int(netlink.FLAG_ONLINK),
  928. Gw: nodes["b"].InternalIP.IP,
  929. LinkIndex: privIface,
  930. Protocol: unix.RTPROT_STATIC,
  931. },
  932. {
  933. Dst: peers["a"].AllowedIPs[1],
  934. Flags: int(netlink.FLAG_ONLINK),
  935. Gw: nodes["b"].InternalIP.IP,
  936. LinkIndex: privIface,
  937. Protocol: unix.RTPROT_STATIC,
  938. },
  939. {
  940. Dst: peers["b"].AllowedIPs[0],
  941. Flags: int(netlink.FLAG_ONLINK),
  942. Gw: nodes["b"].InternalIP.IP,
  943. LinkIndex: privIface,
  944. Protocol: unix.RTPROT_STATIC,
  945. },
  946. },
  947. },
  948. {
  949. name: "logical from c local always",
  950. local: true,
  951. topology: mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name),
  952. strategy: encapsulation.Always,
  953. routes: []*netlink.Route{
  954. {
  955. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[0].wireGuardIP),
  956. Flags: int(netlink.FLAG_ONLINK),
  957. Gw: nodes["b"].InternalIP.IP,
  958. LinkIndex: tunlIface,
  959. Protocol: unix.RTPROT_STATIC,
  960. },
  961. {
  962. Dst: nodes["a"].Subnet,
  963. Flags: int(netlink.FLAG_ONLINK),
  964. Gw: nodes["b"].InternalIP.IP,
  965. LinkIndex: tunlIface,
  966. Protocol: unix.RTPROT_STATIC,
  967. },
  968. {
  969. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  970. Flags: int(netlink.FLAG_ONLINK),
  971. Gw: nodes["b"].InternalIP.IP,
  972. LinkIndex: tunlIface,
  973. Protocol: unix.RTPROT_STATIC,
  974. },
  975. {
  976. Dst: oneAddressCIDR(mustTopoForGranularityAndHost(LogicalGranularity, nodes["c"].Name).segments[1].wireGuardIP),
  977. Flags: int(netlink.FLAG_ONLINK),
  978. Gw: nodes["b"].InternalIP.IP,
  979. LinkIndex: tunlIface,
  980. Protocol: unix.RTPROT_STATIC,
  981. },
  982. {
  983. Dst: nodes["b"].Subnet,
  984. Flags: int(netlink.FLAG_ONLINK),
  985. Gw: nodes["b"].InternalIP.IP,
  986. LinkIndex: tunlIface,
  987. Protocol: unix.RTPROT_STATIC,
  988. },
  989. {
  990. Dst: nodes["b"].InternalIP,
  991. Flags: int(netlink.FLAG_ONLINK),
  992. Gw: nodes["b"].InternalIP.IP,
  993. LinkIndex: tunlIface,
  994. Protocol: unix.RTPROT_STATIC,
  995. Table: kiloTableIndex,
  996. },
  997. {
  998. Dst: peers["a"].AllowedIPs[0],
  999. Flags: int(netlink.FLAG_ONLINK),
  1000. Gw: nodes["b"].InternalIP.IP,
  1001. LinkIndex: tunlIface,
  1002. Protocol: unix.RTPROT_STATIC,
  1003. },
  1004. {
  1005. Dst: peers["a"].AllowedIPs[1],
  1006. Flags: int(netlink.FLAG_ONLINK),
  1007. Gw: nodes["b"].InternalIP.IP,
  1008. LinkIndex: tunlIface,
  1009. Protocol: unix.RTPROT_STATIC,
  1010. },
  1011. {
  1012. Dst: peers["b"].AllowedIPs[0],
  1013. Flags: int(netlink.FLAG_ONLINK),
  1014. Gw: nodes["b"].InternalIP.IP,
  1015. LinkIndex: tunlIface,
  1016. Protocol: unix.RTPROT_STATIC,
  1017. },
  1018. },
  1019. rules: []*netlink.Rule{
  1020. defaultRule(&netlink.Rule{
  1021. Src: nodes["c"].Subnet,
  1022. Dst: nodes["b"].InternalIP,
  1023. Table: kiloTableIndex,
  1024. }),
  1025. },
  1026. },
  1027. {
  1028. name: "full from a local",
  1029. local: true,
  1030. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name),
  1031. strategy: encapsulation.Never,
  1032. routes: []*netlink.Route{
  1033. {
  1034. Dst: nodes["b"].Subnet,
  1035. Flags: int(netlink.FLAG_ONLINK),
  1036. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  1037. LinkIndex: kiloIface,
  1038. Protocol: unix.RTPROT_STATIC,
  1039. },
  1040. {
  1041. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  1042. Flags: int(netlink.FLAG_ONLINK),
  1043. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[1].wireGuardIP,
  1044. LinkIndex: kiloIface,
  1045. Protocol: unix.RTPROT_STATIC,
  1046. },
  1047. {
  1048. Dst: nodes["c"].Subnet,
  1049. Flags: int(netlink.FLAG_ONLINK),
  1050. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[2].wireGuardIP,
  1051. LinkIndex: kiloIface,
  1052. Protocol: unix.RTPROT_STATIC,
  1053. },
  1054. {
  1055. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  1056. Flags: int(netlink.FLAG_ONLINK),
  1057. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["a"].Name).segments[2].wireGuardIP,
  1058. LinkIndex: kiloIface,
  1059. Protocol: unix.RTPROT_STATIC,
  1060. },
  1061. {
  1062. Dst: peers["a"].AllowedIPs[0],
  1063. LinkIndex: kiloIface,
  1064. Protocol: unix.RTPROT_STATIC,
  1065. },
  1066. {
  1067. Dst: peers["a"].AllowedIPs[1],
  1068. LinkIndex: kiloIface,
  1069. Protocol: unix.RTPROT_STATIC,
  1070. },
  1071. {
  1072. Dst: peers["b"].AllowedIPs[0],
  1073. LinkIndex: kiloIface,
  1074. Protocol: unix.RTPROT_STATIC,
  1075. },
  1076. },
  1077. },
  1078. {
  1079. name: "full from b local",
  1080. local: true,
  1081. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name),
  1082. strategy: encapsulation.Never,
  1083. routes: []*netlink.Route{
  1084. {
  1085. Dst: nodes["a"].Subnet,
  1086. Flags: int(netlink.FLAG_ONLINK),
  1087. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  1088. LinkIndex: kiloIface,
  1089. Protocol: unix.RTPROT_STATIC,
  1090. },
  1091. {
  1092. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  1093. Flags: int(netlink.FLAG_ONLINK),
  1094. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[0].wireGuardIP,
  1095. LinkIndex: kiloIface,
  1096. Protocol: unix.RTPROT_STATIC,
  1097. },
  1098. {
  1099. Dst: nodes["c"].Subnet,
  1100. Flags: int(netlink.FLAG_ONLINK),
  1101. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[2].wireGuardIP,
  1102. LinkIndex: kiloIface,
  1103. Protocol: unix.RTPROT_STATIC,
  1104. },
  1105. {
  1106. Dst: oneAddressCIDR(nodes["c"].InternalIP.IP),
  1107. Flags: int(netlink.FLAG_ONLINK),
  1108. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["b"].Name).segments[2].wireGuardIP,
  1109. LinkIndex: kiloIface,
  1110. Protocol: unix.RTPROT_STATIC,
  1111. },
  1112. {
  1113. Dst: peers["a"].AllowedIPs[0],
  1114. LinkIndex: kiloIface,
  1115. Protocol: unix.RTPROT_STATIC,
  1116. },
  1117. {
  1118. Dst: peers["a"].AllowedIPs[1],
  1119. LinkIndex: kiloIface,
  1120. Protocol: unix.RTPROT_STATIC,
  1121. },
  1122. {
  1123. Dst: peers["b"].AllowedIPs[0],
  1124. LinkIndex: kiloIface,
  1125. Protocol: unix.RTPROT_STATIC,
  1126. },
  1127. },
  1128. },
  1129. {
  1130. name: "full from c local",
  1131. local: true,
  1132. topology: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name),
  1133. strategy: encapsulation.Never,
  1134. routes: []*netlink.Route{
  1135. {
  1136. Dst: nodes["a"].Subnet,
  1137. Flags: int(netlink.FLAG_ONLINK),
  1138. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[0].wireGuardIP,
  1139. LinkIndex: kiloIface,
  1140. Protocol: unix.RTPROT_STATIC,
  1141. },
  1142. {
  1143. Dst: oneAddressCIDR(nodes["a"].InternalIP.IP),
  1144. Flags: int(netlink.FLAG_ONLINK),
  1145. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[0].wireGuardIP,
  1146. LinkIndex: kiloIface,
  1147. Protocol: unix.RTPROT_STATIC,
  1148. },
  1149. {
  1150. Dst: nodes["b"].Subnet,
  1151. Flags: int(netlink.FLAG_ONLINK),
  1152. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[1].wireGuardIP,
  1153. LinkIndex: kiloIface,
  1154. Protocol: unix.RTPROT_STATIC,
  1155. },
  1156. {
  1157. Dst: oneAddressCIDR(nodes["b"].InternalIP.IP),
  1158. Flags: int(netlink.FLAG_ONLINK),
  1159. Gw: mustTopoForGranularityAndHost(FullGranularity, nodes["c"].Name).segments[1].wireGuardIP,
  1160. LinkIndex: kiloIface,
  1161. Protocol: unix.RTPROT_STATIC,
  1162. },
  1163. {
  1164. Dst: peers["a"].AllowedIPs[0],
  1165. LinkIndex: kiloIface,
  1166. Protocol: unix.RTPROT_STATIC,
  1167. },
  1168. {
  1169. Dst: peers["a"].AllowedIPs[1],
  1170. LinkIndex: kiloIface,
  1171. Protocol: unix.RTPROT_STATIC,
  1172. },
  1173. {
  1174. Dst: peers["b"].AllowedIPs[0],
  1175. LinkIndex: kiloIface,
  1176. Protocol: unix.RTPROT_STATIC,
  1177. },
  1178. },
  1179. },
  1180. } {
  1181. routes, rules := tc.topology.Routes(DefaultKiloInterface, kiloIface, privIface, tunlIface, tc.local, encapsulation.NewIPIP(tc.strategy))
  1182. if diff := pretty.Compare(routes, tc.routes); diff != "" {
  1183. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  1184. }
  1185. if diff := pretty.Compare(rules, tc.rules); diff != "" {
  1186. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  1187. }
  1188. }
  1189. }
  1190. func TestConf(t *testing.T) {
  1191. nodes, peers, key, port := setup(t)
  1192. for _, tc := range []struct {
  1193. name string
  1194. topology *Topology
  1195. result string
  1196. }{
  1197. {
  1198. name: "logical from a",
  1199. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["a"].Name, port, key, DefaultKiloSubnet, nodes["a"].PersistentKeepalive),
  1200. result: `[Interface]
  1201. PrivateKey = private
  1202. ListenPort = 51820
  1203. [Peer]
  1204. PublicKey = key2
  1205. Endpoint = 10.1.0.2:51820
  1206. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32
  1207. PersistentKeepalive = 25
  1208. [Peer]
  1209. PublicKey = key4
  1210. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1211. PersistentKeepalive = 25
  1212. [Peer]
  1213. PublicKey = key5
  1214. Endpoint = 192.168.0.1:51820
  1215. AllowedIPs = 10.5.0.3/24
  1216. PersistentKeepalive = 25
  1217. `,
  1218. },
  1219. {
  1220. name: "logical from b",
  1221. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["b"].Name, port, key, DefaultKiloSubnet, nodes["b"].PersistentKeepalive),
  1222. result: `[Interface]
  1223. PrivateKey = private
  1224. ListenPort = 51820
  1225. [Peer]
  1226. PublicKey = key1
  1227. Endpoint = 10.1.0.1:51820
  1228. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  1229. [Peer]
  1230. PublicKey = key4
  1231. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1232. [Peer]
  1233. PublicKey = key5
  1234. Endpoint = 192.168.0.1:51820
  1235. AllowedIPs = 10.5.0.3/24
  1236. `,
  1237. },
  1238. {
  1239. name: "logical from c",
  1240. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["c"].Name, port, key, DefaultKiloSubnet, nodes["c"].PersistentKeepalive),
  1241. result: `[Interface]
  1242. PrivateKey = private
  1243. ListenPort = 51820
  1244. [Peer]
  1245. PublicKey = key1
  1246. Endpoint = 10.1.0.1:51820
  1247. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  1248. [Peer]
  1249. PublicKey = key4
  1250. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1251. [Peer]
  1252. PublicKey = key5
  1253. Endpoint = 192.168.0.1:51820
  1254. AllowedIPs = 10.5.0.3/24
  1255. `,
  1256. },
  1257. {
  1258. name: "full from a",
  1259. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["a"].Name, port, key, DefaultKiloSubnet, nodes["a"].PersistentKeepalive),
  1260. result: `[Interface]
  1261. PrivateKey = private
  1262. ListenPort = 51820
  1263. [Peer]
  1264. PublicKey = key2
  1265. Endpoint = 10.1.0.2:51820
  1266. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.4.0.2/32
  1267. PersistentKeepalive = 25
  1268. [Peer]
  1269. PublicKey = key3
  1270. Endpoint = 10.1.0.3:51820
  1271. AllowedIPs = 10.2.3.0/24, 192.168.0.2/32, 10.4.0.3/32
  1272. PersistentKeepalive = 25
  1273. [Peer]
  1274. PublicKey = key4
  1275. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1276. PersistentKeepalive = 25
  1277. [Peer]
  1278. PublicKey = key5
  1279. Endpoint = 192.168.0.1:51820
  1280. AllowedIPs = 10.5.0.3/24
  1281. PersistentKeepalive = 25
  1282. `,
  1283. },
  1284. {
  1285. name: "full from b",
  1286. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["b"].Name, port, key, DefaultKiloSubnet, nodes["b"].PersistentKeepalive),
  1287. result: `[Interface]
  1288. PrivateKey = private
  1289. ListenPort = 51820
  1290. [Peer]
  1291. PublicKey = key1
  1292. Endpoint = 10.1.0.1:51820
  1293. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  1294. [Peer]
  1295. PublicKey = key3
  1296. Endpoint = 10.1.0.3:51820
  1297. AllowedIPs = 10.2.3.0/24, 192.168.0.2/32, 10.4.0.3/32
  1298. [Peer]
  1299. PublicKey = key4
  1300. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1301. [Peer]
  1302. PublicKey = key5
  1303. Endpoint = 192.168.0.1:51820
  1304. AllowedIPs = 10.5.0.3/24
  1305. `,
  1306. },
  1307. {
  1308. name: "full from c",
  1309. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["c"].Name, port, key, DefaultKiloSubnet, nodes["c"].PersistentKeepalive),
  1310. result: `[Interface]
  1311. PrivateKey = private
  1312. ListenPort = 51820
  1313. [Peer]
  1314. PublicKey = key1
  1315. Endpoint = 10.1.0.1:51820
  1316. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  1317. [Peer]
  1318. PublicKey = key2
  1319. Endpoint = 10.1.0.2:51820
  1320. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.4.0.2/32
  1321. [Peer]
  1322. PublicKey = key4
  1323. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  1324. [Peer]
  1325. PublicKey = key5
  1326. Endpoint = 192.168.0.1:51820
  1327. AllowedIPs = 10.5.0.3/24
  1328. `,
  1329. },
  1330. } {
  1331. conf := tc.topology.Conf()
  1332. if !conf.Equal(wireguard.Parse([]byte(tc.result))) {
  1333. buf, err := conf.Bytes()
  1334. if err != nil {
  1335. t.Errorf("test case %q: failed to render conf: %v", tc.name, err)
  1336. }
  1337. t.Errorf("test case %q: expected %s got %s", tc.name, tc.result, string(buf))
  1338. }
  1339. }
  1340. }
  1341. func TestFindLeader(t *testing.T) {
  1342. ip, e1, err := net.ParseCIDR("10.0.0.1/32")
  1343. if err != nil {
  1344. t.Fatalf("failed to parse external IP CIDR: %v", err)
  1345. }
  1346. e1.IP = ip
  1347. ip, e2, err := net.ParseCIDR("8.8.8.8/32")
  1348. if err != nil {
  1349. t.Fatalf("failed to parse external IP CIDR: %v", err)
  1350. }
  1351. e2.IP = ip
  1352. nodes := []*Node{
  1353. {
  1354. Name: "a",
  1355. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  1356. },
  1357. {
  1358. Name: "b",
  1359. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  1360. },
  1361. {
  1362. Name: "c",
  1363. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  1364. },
  1365. {
  1366. Name: "d",
  1367. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  1368. Leader: true,
  1369. },
  1370. {
  1371. Name: "2",
  1372. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  1373. Leader: true,
  1374. },
  1375. }
  1376. for _, tc := range []struct {
  1377. name string
  1378. nodes []*Node
  1379. out int
  1380. }{
  1381. {
  1382. name: "nil",
  1383. nodes: nil,
  1384. out: 0,
  1385. },
  1386. {
  1387. name: "one",
  1388. nodes: []*Node{nodes[0]},
  1389. out: 0,
  1390. },
  1391. {
  1392. name: "non-leaders",
  1393. nodes: []*Node{nodes[0], nodes[1], nodes[2]},
  1394. out: 1,
  1395. },
  1396. {
  1397. name: "leaders",
  1398. nodes: []*Node{nodes[3], nodes[4]},
  1399. out: 1,
  1400. },
  1401. {
  1402. name: "public",
  1403. nodes: []*Node{nodes[1], nodes[2], nodes[4]},
  1404. out: 2,
  1405. },
  1406. {
  1407. name: "private",
  1408. nodes: []*Node{nodes[0], nodes[3]},
  1409. out: 1,
  1410. },
  1411. {
  1412. name: "all",
  1413. nodes: nodes,
  1414. out: 4,
  1415. },
  1416. } {
  1417. l := findLeader(tc.nodes)
  1418. if l != tc.out {
  1419. t.Errorf("test case %q: expected %d got %d", tc.name, tc.out, l)
  1420. }
  1421. }
  1422. }
  1423. func TestDeduplicatePeerIPs(t *testing.T) {
  1424. p1 := &Peer{
  1425. Name: "1",
  1426. Peer: wireguard.Peer{
  1427. PublicKey: []byte("key1"),
  1428. AllowedIPs: []*net.IPNet{
  1429. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  1430. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  1431. },
  1432. },
  1433. }
  1434. p2 := &Peer{
  1435. Name: "2",
  1436. Peer: wireguard.Peer{
  1437. PublicKey: []byte("key2"),
  1438. AllowedIPs: []*net.IPNet{
  1439. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  1440. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1441. },
  1442. },
  1443. }
  1444. p3 := &Peer{
  1445. Name: "3",
  1446. Peer: wireguard.Peer{
  1447. PublicKey: []byte("key3"),
  1448. AllowedIPs: []*net.IPNet{
  1449. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  1450. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1451. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  1452. },
  1453. },
  1454. }
  1455. p4 := &Peer{
  1456. Name: "4",
  1457. Peer: wireguard.Peer{
  1458. PublicKey: []byte("key4"),
  1459. AllowedIPs: []*net.IPNet{
  1460. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1461. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1462. },
  1463. },
  1464. }
  1465. for _, tc := range []struct {
  1466. name string
  1467. peers []*Peer
  1468. out []*Peer
  1469. }{
  1470. {
  1471. name: "nil",
  1472. peers: nil,
  1473. out: nil,
  1474. },
  1475. {
  1476. name: "simple dupe",
  1477. peers: []*Peer{p1, p2},
  1478. out: []*Peer{
  1479. p1,
  1480. {
  1481. Name: "2",
  1482. Peer: wireguard.Peer{
  1483. PublicKey: []byte("key2"),
  1484. AllowedIPs: []*net.IPNet{
  1485. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1486. },
  1487. },
  1488. },
  1489. },
  1490. },
  1491. {
  1492. name: "simple dupe reversed",
  1493. peers: []*Peer{p2, p1},
  1494. out: []*Peer{
  1495. p2,
  1496. {
  1497. Name: "1",
  1498. Peer: wireguard.Peer{
  1499. PublicKey: []byte("key1"),
  1500. AllowedIPs: []*net.IPNet{
  1501. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  1502. },
  1503. },
  1504. },
  1505. },
  1506. },
  1507. {
  1508. name: "one duplicates all",
  1509. peers: []*Peer{p3, p2, p1, p4},
  1510. out: []*Peer{
  1511. p3,
  1512. {
  1513. Name: "2",
  1514. Peer: wireguard.Peer{
  1515. PublicKey: []byte("key2"),
  1516. },
  1517. },
  1518. {
  1519. Name: "1",
  1520. Peer: wireguard.Peer{
  1521. PublicKey: []byte("key1"),
  1522. },
  1523. },
  1524. {
  1525. Name: "4",
  1526. Peer: wireguard.Peer{
  1527. PublicKey: []byte("key4"),
  1528. },
  1529. },
  1530. },
  1531. },
  1532. {
  1533. name: "one duplicates itself",
  1534. peers: []*Peer{p4, p1},
  1535. out: []*Peer{
  1536. {
  1537. Name: "4",
  1538. Peer: wireguard.Peer{
  1539. PublicKey: []byte("key4"),
  1540. AllowedIPs: []*net.IPNet{
  1541. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  1542. },
  1543. },
  1544. },
  1545. {
  1546. Name: "1",
  1547. Peer: wireguard.Peer{
  1548. PublicKey: []byte("key1"),
  1549. AllowedIPs: []*net.IPNet{
  1550. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  1551. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  1552. },
  1553. },
  1554. },
  1555. },
  1556. },
  1557. } {
  1558. out := deduplicatePeerIPs(tc.peers)
  1559. if diff := pretty.Compare(out, tc.out); diff != "" {
  1560. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  1561. }
  1562. }
  1563. }