topology_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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/wireguard"
  21. )
  22. func allowedIPs(ips ...string) string {
  23. return strings.Join(ips, ", ")
  24. }
  25. func setup(t *testing.T) (map[string]*Node, map[string]*Peer, []byte, uint32) {
  26. key := []byte("private")
  27. e1 := &net.IPNet{IP: net.ParseIP("10.1.0.1").To4(), Mask: net.CIDRMask(16, 32)}
  28. e2 := &net.IPNet{IP: net.ParseIP("10.1.0.2").To4(), Mask: net.CIDRMask(16, 32)}
  29. e3 := &net.IPNet{IP: net.ParseIP("10.1.0.3").To4(), Mask: net.CIDRMask(16, 32)}
  30. i1 := &net.IPNet{IP: net.ParseIP("192.168.0.1").To4(), Mask: net.CIDRMask(32, 32)}
  31. i2 := &net.IPNet{IP: net.ParseIP("192.168.0.2").To4(), Mask: net.CIDRMask(32, 32)}
  32. nodes := map[string]*Node{
  33. "a": {
  34. Name: "a",
  35. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  36. InternalIP: i1,
  37. Location: "1",
  38. Subnet: &net.IPNet{IP: net.ParseIP("10.2.1.0"), Mask: net.CIDRMask(24, 32)},
  39. Key: []byte("key1"),
  40. PersistentKeepalive: 25,
  41. },
  42. "b": {
  43. Name: "b",
  44. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  45. InternalIP: i1,
  46. Location: "2",
  47. Subnet: &net.IPNet{IP: net.ParseIP("10.2.2.0"), Mask: net.CIDRMask(24, 32)},
  48. Key: []byte("key2"),
  49. },
  50. "c": {
  51. Name: "c",
  52. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e3.IP}, Port: DefaultKiloPort},
  53. InternalIP: i2,
  54. // Same location a node b.
  55. Location: "2",
  56. Subnet: &net.IPNet{IP: net.ParseIP("10.2.3.0"), Mask: net.CIDRMask(24, 32)},
  57. Key: []byte("key3"),
  58. },
  59. }
  60. peers := map[string]*Peer{
  61. "a": {
  62. Name: "a",
  63. Peer: wireguard.Peer{
  64. AllowedIPs: []*net.IPNet{
  65. {IP: net.ParseIP("10.5.0.1"), Mask: net.CIDRMask(24, 32)},
  66. {IP: net.ParseIP("10.5.0.2"), Mask: net.CIDRMask(24, 32)},
  67. },
  68. PublicKey: []byte("key4"),
  69. },
  70. },
  71. "b": {
  72. Name: "b",
  73. Peer: wireguard.Peer{
  74. AllowedIPs: []*net.IPNet{
  75. {IP: net.ParseIP("10.5.0.3"), Mask: net.CIDRMask(24, 32)},
  76. },
  77. Endpoint: &wireguard.Endpoint{
  78. DNSOrIP: wireguard.DNSOrIP{IP: net.ParseIP("192.168.0.1")},
  79. Port: DefaultKiloPort,
  80. },
  81. PublicKey: []byte("key5"),
  82. },
  83. },
  84. }
  85. return nodes, peers, key, DefaultKiloPort
  86. }
  87. func TestNewTopology(t *testing.T) {
  88. nodes, peers, key, port := setup(t)
  89. w1 := net.ParseIP("10.4.0.1").To4()
  90. w2 := net.ParseIP("10.4.0.2").To4()
  91. w3 := net.ParseIP("10.4.0.3").To4()
  92. for _, tc := range []struct {
  93. name string
  94. granularity Granularity
  95. hostname string
  96. result *Topology
  97. }{
  98. {
  99. name: "logical from a",
  100. granularity: LogicalGranularity,
  101. hostname: nodes["a"].Name,
  102. result: &Topology{
  103. hostname: nodes["a"].Name,
  104. leader: true,
  105. location: nodes["a"].Location,
  106. subnet: nodes["a"].Subnet,
  107. privateIP: nodes["a"].InternalIP,
  108. wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
  109. segments: []*segment{
  110. {
  111. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  112. endpoint: nodes["a"].Endpoint,
  113. key: nodes["a"].Key,
  114. location: nodes["a"].Location,
  115. cidrs: []*net.IPNet{nodes["a"].Subnet},
  116. hostnames: []string{"a"},
  117. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  118. wireGuardIP: w1,
  119. },
  120. {
  121. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  122. endpoint: nodes["b"].Endpoint,
  123. key: nodes["b"].Key,
  124. location: nodes["b"].Location,
  125. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  126. hostnames: []string{"b", "c"},
  127. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  128. wireGuardIP: w2,
  129. },
  130. },
  131. peers: []*Peer{peers["a"], peers["b"]},
  132. },
  133. },
  134. {
  135. name: "logical from b",
  136. granularity: LogicalGranularity,
  137. hostname: nodes["b"].Name,
  138. result: &Topology{
  139. hostname: nodes["b"].Name,
  140. leader: true,
  141. location: nodes["b"].Location,
  142. subnet: nodes["b"].Subnet,
  143. privateIP: nodes["b"].InternalIP,
  144. wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
  145. segments: []*segment{
  146. {
  147. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  148. endpoint: nodes["a"].Endpoint,
  149. key: nodes["a"].Key,
  150. location: nodes["a"].Location,
  151. cidrs: []*net.IPNet{nodes["a"].Subnet},
  152. hostnames: []string{"a"},
  153. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  154. wireGuardIP: w1,
  155. },
  156. {
  157. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  158. endpoint: nodes["b"].Endpoint,
  159. key: nodes["b"].Key,
  160. location: nodes["b"].Location,
  161. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  162. hostnames: []string{"b", "c"},
  163. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  164. wireGuardIP: w2,
  165. },
  166. },
  167. peers: []*Peer{peers["a"], peers["b"]},
  168. },
  169. },
  170. {
  171. name: "logical from c",
  172. granularity: LogicalGranularity,
  173. hostname: nodes["c"].Name,
  174. result: &Topology{
  175. hostname: nodes["c"].Name,
  176. leader: false,
  177. location: nodes["b"].Location,
  178. subnet: nodes["c"].Subnet,
  179. privateIP: nodes["c"].InternalIP,
  180. wireGuardCIDR: nil,
  181. segments: []*segment{
  182. {
  183. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  184. endpoint: nodes["a"].Endpoint,
  185. key: nodes["a"].Key,
  186. location: nodes["a"].Location,
  187. cidrs: []*net.IPNet{nodes["a"].Subnet},
  188. hostnames: []string{"a"},
  189. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  190. wireGuardIP: w1,
  191. },
  192. {
  193. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  194. endpoint: nodes["b"].Endpoint,
  195. key: nodes["b"].Key,
  196. location: nodes["b"].Location,
  197. cidrs: []*net.IPNet{nodes["b"].Subnet, nodes["c"].Subnet},
  198. hostnames: []string{"b", "c"},
  199. privateIPs: []net.IP{nodes["b"].InternalIP.IP, nodes["c"].InternalIP.IP},
  200. wireGuardIP: w2,
  201. },
  202. },
  203. peers: []*Peer{peers["a"], peers["b"]},
  204. },
  205. },
  206. {
  207. name: "full from a",
  208. granularity: FullGranularity,
  209. hostname: nodes["a"].Name,
  210. result: &Topology{
  211. hostname: nodes["a"].Name,
  212. leader: true,
  213. location: nodes["a"].Name,
  214. subnet: nodes["a"].Subnet,
  215. privateIP: nodes["a"].InternalIP,
  216. wireGuardCIDR: &net.IPNet{IP: w1, Mask: net.CIDRMask(16, 32)},
  217. segments: []*segment{
  218. {
  219. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  220. endpoint: nodes["a"].Endpoint,
  221. key: nodes["a"].Key,
  222. location: nodes["a"].Name,
  223. cidrs: []*net.IPNet{nodes["a"].Subnet},
  224. hostnames: []string{"a"},
  225. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  226. wireGuardIP: w1,
  227. },
  228. {
  229. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  230. endpoint: nodes["b"].Endpoint,
  231. key: nodes["b"].Key,
  232. location: nodes["b"].Name,
  233. cidrs: []*net.IPNet{nodes["b"].Subnet},
  234. hostnames: []string{"b"},
  235. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  236. wireGuardIP: w2,
  237. },
  238. {
  239. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  240. endpoint: nodes["c"].Endpoint,
  241. key: nodes["c"].Key,
  242. location: nodes["c"].Name,
  243. cidrs: []*net.IPNet{nodes["c"].Subnet},
  244. hostnames: []string{"c"},
  245. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  246. wireGuardIP: w3,
  247. },
  248. },
  249. peers: []*Peer{peers["a"], peers["b"]},
  250. },
  251. },
  252. {
  253. name: "full from b",
  254. granularity: FullGranularity,
  255. hostname: nodes["b"].Name,
  256. result: &Topology{
  257. hostname: nodes["b"].Name,
  258. leader: true,
  259. location: nodes["b"].Name,
  260. subnet: nodes["b"].Subnet,
  261. privateIP: nodes["b"].InternalIP,
  262. wireGuardCIDR: &net.IPNet{IP: w2, Mask: net.CIDRMask(16, 32)},
  263. segments: []*segment{
  264. {
  265. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  266. endpoint: nodes["a"].Endpoint,
  267. key: nodes["a"].Key,
  268. location: nodes["a"].Name,
  269. cidrs: []*net.IPNet{nodes["a"].Subnet},
  270. hostnames: []string{"a"},
  271. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  272. wireGuardIP: w1,
  273. },
  274. {
  275. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  276. endpoint: nodes["b"].Endpoint,
  277. key: nodes["b"].Key,
  278. location: nodes["b"].Name,
  279. cidrs: []*net.IPNet{nodes["b"].Subnet},
  280. hostnames: []string{"b"},
  281. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  282. wireGuardIP: w2,
  283. },
  284. {
  285. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  286. endpoint: nodes["c"].Endpoint,
  287. key: nodes["c"].Key,
  288. location: nodes["c"].Name,
  289. cidrs: []*net.IPNet{nodes["c"].Subnet},
  290. hostnames: []string{"c"},
  291. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  292. wireGuardIP: w3,
  293. },
  294. },
  295. peers: []*Peer{peers["a"], peers["b"]},
  296. },
  297. },
  298. {
  299. name: "full from c",
  300. granularity: FullGranularity,
  301. hostname: nodes["c"].Name,
  302. result: &Topology{
  303. hostname: nodes["c"].Name,
  304. leader: true,
  305. location: nodes["c"].Name,
  306. subnet: nodes["c"].Subnet,
  307. privateIP: nodes["c"].InternalIP,
  308. wireGuardCIDR: &net.IPNet{IP: w3, Mask: net.CIDRMask(16, 32)},
  309. segments: []*segment{
  310. {
  311. allowedIPs: []*net.IPNet{nodes["a"].Subnet, nodes["a"].InternalIP, {IP: w1, Mask: net.CIDRMask(32, 32)}},
  312. endpoint: nodes["a"].Endpoint,
  313. key: nodes["a"].Key,
  314. location: nodes["a"].Name,
  315. cidrs: []*net.IPNet{nodes["a"].Subnet},
  316. hostnames: []string{"a"},
  317. privateIPs: []net.IP{nodes["a"].InternalIP.IP},
  318. wireGuardIP: w1,
  319. },
  320. {
  321. allowedIPs: []*net.IPNet{nodes["b"].Subnet, nodes["b"].InternalIP, {IP: w2, Mask: net.CIDRMask(32, 32)}},
  322. endpoint: nodes["b"].Endpoint,
  323. key: nodes["b"].Key,
  324. location: nodes["b"].Name,
  325. cidrs: []*net.IPNet{nodes["b"].Subnet},
  326. hostnames: []string{"b"},
  327. privateIPs: []net.IP{nodes["b"].InternalIP.IP},
  328. wireGuardIP: w2,
  329. },
  330. {
  331. allowedIPs: []*net.IPNet{nodes["c"].Subnet, nodes["c"].InternalIP, {IP: w3, Mask: net.CIDRMask(32, 32)}},
  332. endpoint: nodes["c"].Endpoint,
  333. key: nodes["c"].Key,
  334. location: nodes["c"].Name,
  335. cidrs: []*net.IPNet{nodes["c"].Subnet},
  336. hostnames: []string{"c"},
  337. privateIPs: []net.IP{nodes["c"].InternalIP.IP},
  338. wireGuardIP: w3,
  339. },
  340. },
  341. peers: []*Peer{peers["a"], peers["b"]},
  342. },
  343. },
  344. } {
  345. tc.result.key = key
  346. tc.result.port = port
  347. topo, err := NewTopology(nodes, peers, tc.granularity, tc.hostname, port, key, DefaultKiloSubnet, 0)
  348. if err != nil {
  349. t.Errorf("test case %q: failed to generate Topology: %v", tc.name, err)
  350. }
  351. if diff := pretty.Compare(topo, tc.result); diff != "" {
  352. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  353. }
  354. }
  355. }
  356. 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 {
  357. topo, err := NewTopology(nodes, peers, granularity, hostname, port, key, subnet, persistentKeepalive)
  358. if err != nil {
  359. t.Errorf("failed to generate Topology: %v", err)
  360. }
  361. return topo
  362. }
  363. func TestConf(t *testing.T) {
  364. nodes, peers, key, port := setup(t)
  365. for _, tc := range []struct {
  366. name string
  367. topology *Topology
  368. result string
  369. }{
  370. {
  371. name: "logical from a",
  372. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["a"].Name, port, key, DefaultKiloSubnet, nodes["a"].PersistentKeepalive),
  373. result: `[Interface]
  374. PrivateKey = private
  375. ListenPort = 51820
  376. [Peer]
  377. PublicKey = key2
  378. Endpoint = 10.1.0.2:51820
  379. 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
  380. PersistentKeepalive = 25
  381. [Peer]
  382. PublicKey = key4
  383. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  384. PersistentKeepalive = 25
  385. [Peer]
  386. PublicKey = key5
  387. Endpoint = 192.168.0.1:51820
  388. AllowedIPs = 10.5.0.3/24
  389. PersistentKeepalive = 25
  390. `,
  391. },
  392. {
  393. name: "logical from b",
  394. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["b"].Name, port, key, DefaultKiloSubnet, nodes["b"].PersistentKeepalive),
  395. result: `[Interface]
  396. PrivateKey = private
  397. ListenPort = 51820
  398. [Peer]
  399. PublicKey = key1
  400. Endpoint = 10.1.0.1:51820
  401. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  402. [Peer]
  403. PublicKey = key4
  404. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  405. [Peer]
  406. PublicKey = key5
  407. Endpoint = 192.168.0.1:51820
  408. AllowedIPs = 10.5.0.3/24
  409. `,
  410. },
  411. {
  412. name: "logical from c",
  413. topology: mustTopo(t, nodes, peers, LogicalGranularity, nodes["c"].Name, port, key, DefaultKiloSubnet, nodes["c"].PersistentKeepalive),
  414. result: `[Interface]
  415. PrivateKey = private
  416. ListenPort = 51820
  417. [Peer]
  418. PublicKey = key1
  419. Endpoint = 10.1.0.1:51820
  420. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  421. [Peer]
  422. PublicKey = key4
  423. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  424. [Peer]
  425. PublicKey = key5
  426. Endpoint = 192.168.0.1:51820
  427. AllowedIPs = 10.5.0.3/24
  428. `,
  429. },
  430. {
  431. name: "full from a",
  432. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["a"].Name, port, key, DefaultKiloSubnet, nodes["a"].PersistentKeepalive),
  433. result: `[Interface]
  434. PrivateKey = private
  435. ListenPort = 51820
  436. [Peer]
  437. PublicKey = key2
  438. Endpoint = 10.1.0.2:51820
  439. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.4.0.2/32
  440. PersistentKeepalive = 25
  441. [Peer]
  442. PublicKey = key3
  443. Endpoint = 10.1.0.3:51820
  444. AllowedIPs = 10.2.3.0/24, 192.168.0.2/32, 10.4.0.3/32
  445. PersistentKeepalive = 25
  446. [Peer]
  447. PublicKey = key4
  448. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  449. PersistentKeepalive = 25
  450. [Peer]
  451. PublicKey = key5
  452. Endpoint = 192.168.0.1:51820
  453. AllowedIPs = 10.5.0.3/24
  454. PersistentKeepalive = 25
  455. `,
  456. },
  457. {
  458. name: "full from b",
  459. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["b"].Name, port, key, DefaultKiloSubnet, nodes["b"].PersistentKeepalive),
  460. result: `[Interface]
  461. PrivateKey = private
  462. ListenPort = 51820
  463. [Peer]
  464. PublicKey = key1
  465. Endpoint = 10.1.0.1:51820
  466. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  467. [Peer]
  468. PublicKey = key3
  469. Endpoint = 10.1.0.3:51820
  470. AllowedIPs = 10.2.3.0/24, 192.168.0.2/32, 10.4.0.3/32
  471. [Peer]
  472. PublicKey = key4
  473. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  474. [Peer]
  475. PublicKey = key5
  476. Endpoint = 192.168.0.1:51820
  477. AllowedIPs = 10.5.0.3/24
  478. `,
  479. },
  480. {
  481. name: "full from c",
  482. topology: mustTopo(t, nodes, peers, FullGranularity, nodes["c"].Name, port, key, DefaultKiloSubnet, nodes["c"].PersistentKeepalive),
  483. result: `[Interface]
  484. PrivateKey = private
  485. ListenPort = 51820
  486. [Peer]
  487. PublicKey = key1
  488. Endpoint = 10.1.0.1:51820
  489. AllowedIPs = 10.2.1.0/24, 192.168.0.1/32, 10.4.0.1/32
  490. [Peer]
  491. PublicKey = key2
  492. Endpoint = 10.1.0.2:51820
  493. AllowedIPs = 10.2.2.0/24, 192.168.0.1/32, 10.4.0.2/32
  494. [Peer]
  495. PublicKey = key4
  496. AllowedIPs = 10.5.0.1/24, 10.5.0.2/24
  497. [Peer]
  498. PublicKey = key5
  499. Endpoint = 192.168.0.1:51820
  500. AllowedIPs = 10.5.0.3/24
  501. `,
  502. },
  503. } {
  504. conf := tc.topology.Conf()
  505. if !conf.Equal(wireguard.Parse([]byte(tc.result))) {
  506. buf, err := conf.Bytes()
  507. if err != nil {
  508. t.Errorf("test case %q: failed to render conf: %v", tc.name, err)
  509. }
  510. t.Errorf("test case %q: expected %s got %s", tc.name, tc.result, string(buf))
  511. }
  512. }
  513. }
  514. func TestFindLeader(t *testing.T) {
  515. ip, e1, err := net.ParseCIDR("10.0.0.1/32")
  516. if err != nil {
  517. t.Fatalf("failed to parse external IP CIDR: %v", err)
  518. }
  519. e1.IP = ip
  520. ip, e2, err := net.ParseCIDR("8.8.8.8/32")
  521. if err != nil {
  522. t.Fatalf("failed to parse external IP CIDR: %v", err)
  523. }
  524. e2.IP = ip
  525. nodes := []*Node{
  526. {
  527. Name: "a",
  528. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  529. },
  530. {
  531. Name: "b",
  532. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  533. },
  534. {
  535. Name: "c",
  536. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  537. },
  538. {
  539. Name: "d",
  540. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e1.IP}, Port: DefaultKiloPort},
  541. Leader: true,
  542. },
  543. {
  544. Name: "2",
  545. Endpoint: &wireguard.Endpoint{DNSOrIP: wireguard.DNSOrIP{IP: e2.IP}, Port: DefaultKiloPort},
  546. Leader: true,
  547. },
  548. }
  549. for _, tc := range []struct {
  550. name string
  551. nodes []*Node
  552. out int
  553. }{
  554. {
  555. name: "nil",
  556. nodes: nil,
  557. out: 0,
  558. },
  559. {
  560. name: "one",
  561. nodes: []*Node{nodes[0]},
  562. out: 0,
  563. },
  564. {
  565. name: "non-leaders",
  566. nodes: []*Node{nodes[0], nodes[1], nodes[2]},
  567. out: 1,
  568. },
  569. {
  570. name: "leaders",
  571. nodes: []*Node{nodes[3], nodes[4]},
  572. out: 1,
  573. },
  574. {
  575. name: "public",
  576. nodes: []*Node{nodes[1], nodes[2], nodes[4]},
  577. out: 2,
  578. },
  579. {
  580. name: "private",
  581. nodes: []*Node{nodes[0], nodes[3]},
  582. out: 1,
  583. },
  584. {
  585. name: "all",
  586. nodes: nodes,
  587. out: 4,
  588. },
  589. } {
  590. l := findLeader(tc.nodes)
  591. if l != tc.out {
  592. t.Errorf("test case %q: expected %d got %d", tc.name, tc.out, l)
  593. }
  594. }
  595. }
  596. func TestDeduplicatePeerIPs(t *testing.T) {
  597. p1 := &Peer{
  598. Name: "1",
  599. Peer: wireguard.Peer{
  600. PublicKey: []byte("key1"),
  601. AllowedIPs: []*net.IPNet{
  602. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  603. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  604. },
  605. },
  606. }
  607. p2 := &Peer{
  608. Name: "2",
  609. Peer: wireguard.Peer{
  610. PublicKey: []byte("key2"),
  611. AllowedIPs: []*net.IPNet{
  612. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  613. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  614. },
  615. },
  616. }
  617. p3 := &Peer{
  618. Name: "3",
  619. Peer: wireguard.Peer{
  620. PublicKey: []byte("key3"),
  621. AllowedIPs: []*net.IPNet{
  622. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  623. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  624. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  625. },
  626. },
  627. }
  628. p4 := &Peer{
  629. Name: "4",
  630. Peer: wireguard.Peer{
  631. PublicKey: []byte("key4"),
  632. AllowedIPs: []*net.IPNet{
  633. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  634. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  635. },
  636. },
  637. }
  638. for _, tc := range []struct {
  639. name string
  640. peers []*Peer
  641. out []*Peer
  642. }{
  643. {
  644. name: "nil",
  645. peers: nil,
  646. out: nil,
  647. },
  648. {
  649. name: "simple dupe",
  650. peers: []*Peer{p1, p2},
  651. out: []*Peer{
  652. p1,
  653. {
  654. Name: "2",
  655. Peer: wireguard.Peer{
  656. PublicKey: []byte("key2"),
  657. AllowedIPs: []*net.IPNet{
  658. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  659. },
  660. },
  661. },
  662. },
  663. },
  664. {
  665. name: "simple dupe reversed",
  666. peers: []*Peer{p2, p1},
  667. out: []*Peer{
  668. p2,
  669. {
  670. Name: "1",
  671. Peer: wireguard.Peer{
  672. PublicKey: []byte("key1"),
  673. AllowedIPs: []*net.IPNet{
  674. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  675. },
  676. },
  677. },
  678. },
  679. },
  680. {
  681. name: "one duplicates all",
  682. peers: []*Peer{p3, p2, p1, p4},
  683. out: []*Peer{
  684. p3,
  685. {
  686. Name: "2",
  687. Peer: wireguard.Peer{
  688. PublicKey: []byte("key2"),
  689. },
  690. },
  691. {
  692. Name: "1",
  693. Peer: wireguard.Peer{
  694. PublicKey: []byte("key1"),
  695. },
  696. },
  697. {
  698. Name: "4",
  699. Peer: wireguard.Peer{
  700. PublicKey: []byte("key4"),
  701. },
  702. },
  703. },
  704. },
  705. {
  706. name: "one duplicates itself",
  707. peers: []*Peer{p4, p1},
  708. out: []*Peer{
  709. {
  710. Name: "4",
  711. Peer: wireguard.Peer{
  712. PublicKey: []byte("key4"),
  713. AllowedIPs: []*net.IPNet{
  714. {IP: net.ParseIP("10.0.0.3"), Mask: net.CIDRMask(24, 32)},
  715. },
  716. },
  717. },
  718. {
  719. Name: "1",
  720. Peer: wireguard.Peer{
  721. PublicKey: []byte("key1"),
  722. AllowedIPs: []*net.IPNet{
  723. {IP: net.ParseIP("10.0.0.1"), Mask: net.CIDRMask(24, 32)},
  724. {IP: net.ParseIP("10.0.0.2"), Mask: net.CIDRMask(24, 32)},
  725. },
  726. },
  727. },
  728. },
  729. },
  730. } {
  731. out := deduplicatePeerIPs(tc.peers)
  732. if diff := pretty.Compare(out, tc.out); diff != "" {
  733. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  734. }
  735. }
  736. }