conf_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 wireguard
  15. import (
  16. "net"
  17. "testing"
  18. "github.com/kylelemons/godebug/pretty"
  19. )
  20. func TestCompareConf(t *testing.T) {
  21. for _, tc := range []struct {
  22. name string
  23. a []byte
  24. b []byte
  25. out bool
  26. }{
  27. {
  28. name: "empty",
  29. a: []byte{},
  30. b: []byte{},
  31. out: true,
  32. },
  33. {
  34. name: "key and value order",
  35. a: []byte(`[Interface]
  36. PrivateKey = private
  37. ListenPort = 51820
  38. [Peer]
  39. Endpoint = 10.1.0.2:51820
  40. PresharedKey = psk
  41. PublicKey = key
  42. 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
  43. `),
  44. b: []byte(`[Interface]
  45. ListenPort = 51820
  46. PrivateKey = private
  47. [Peer]
  48. PublicKey = key
  49. AllowedIPs = 192.168.0.1/32, 10.2.3.0/24, 192.168.0.2/32, 10.4.0.2/32, 10.2.2.0/24
  50. PresharedKey = psk
  51. Endpoint = 10.1.0.2:51820
  52. `),
  53. out: true,
  54. },
  55. {
  56. name: "whitespace",
  57. a: []byte(`[Interface]
  58. PrivateKey = private
  59. ListenPort = 51820
  60. [Peer]
  61. Endpoint = 10.1.0.2:51820
  62. PresharedKey = psk
  63. PublicKey = key
  64. 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
  65. `),
  66. b: []byte(`[Interface]
  67. PrivateKey=private
  68. ListenPort=51820
  69. [Peer]
  70. Endpoint=10.1.0.2:51820
  71. PresharedKey = psk
  72. PublicKey=key
  73. 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
  74. `),
  75. out: true,
  76. },
  77. {
  78. name: "missing key",
  79. a: []byte(`[Interface]
  80. PrivateKey = private
  81. ListenPort = 51820
  82. [Peer]
  83. Endpoint = 10.1.0.2:51820
  84. PublicKey = key
  85. 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
  86. `),
  87. b: []byte(`[Interface]
  88. PrivateKey = private
  89. ListenPort = 51820
  90. [Peer]
  91. PublicKey = key
  92. 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
  93. `),
  94. out: false,
  95. },
  96. {
  97. name: "different value",
  98. a: []byte(`[Interface]
  99. PrivateKey = private
  100. ListenPort = 51820
  101. [Peer]
  102. Endpoint = 10.1.0.2:51820
  103. PublicKey = key
  104. 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
  105. `),
  106. b: []byte(`[Interface]
  107. PrivateKey = private
  108. ListenPort = 51820
  109. [Peer]
  110. Endpoint = 10.1.0.2:51820
  111. PublicKey = key2
  112. 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
  113. `),
  114. out: false,
  115. },
  116. {
  117. name: "section order",
  118. a: []byte(`[Interface]
  119. PrivateKey = private
  120. ListenPort = 51820
  121. [Peer]
  122. Endpoint = 10.1.0.2:51820
  123. PresharedKey = psk
  124. PublicKey = key
  125. 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
  126. `),
  127. b: []byte(`[Peer]
  128. Endpoint = 10.1.0.2:51820
  129. PresharedKey = psk
  130. PublicKey = key
  131. 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
  132. [Interface]
  133. PrivateKey = private
  134. ListenPort = 51820
  135. `),
  136. out: true,
  137. },
  138. {
  139. name: "out of order peers",
  140. a: []byte(`[Interface]
  141. PrivateKey = private
  142. ListenPort = 51820
  143. [Peer]
  144. Endpoint = 10.1.0.2:51820
  145. PresharedKey = psk2
  146. PublicKey = key2
  147. 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
  148. [Peer]
  149. Endpoint = 10.1.0.2:51820
  150. PresharedKey = psk1
  151. PublicKey = key1
  152. 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
  153. `),
  154. b: []byte(`[Interface]
  155. PrivateKey = private
  156. ListenPort = 51820
  157. [Peer]
  158. Endpoint = 10.1.0.2:51820
  159. PresharedKey = psk1
  160. PublicKey = key1
  161. 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
  162. [Peer]
  163. Endpoint = 10.1.0.2:51820
  164. PresharedKey = psk2
  165. PublicKey = key2
  166. 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
  167. `),
  168. out: true,
  169. },
  170. {
  171. name: "one empty",
  172. a: []byte(`[Interface]
  173. PrivateKey = private
  174. ListenPort = 51820
  175. [Peer]
  176. Endpoint = 10.1.0.2:51820
  177. PresharedKey = psk
  178. PublicKey = key
  179. 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
  180. `),
  181. b: []byte(``),
  182. out: false,
  183. },
  184. } {
  185. equal := Parse(tc.a).Equal(Parse(tc.b))
  186. if equal != tc.out {
  187. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  188. }
  189. }
  190. }
  191. func TestCompareEndpoint(t *testing.T) {
  192. for _, tc := range []struct {
  193. name string
  194. a *Endpoint
  195. b *Endpoint
  196. dnsFirst bool
  197. out bool
  198. }{
  199. {
  200. name: "both nil",
  201. a: nil,
  202. b: nil,
  203. out: true,
  204. },
  205. {
  206. name: "a nil",
  207. a: nil,
  208. b: &Endpoint{},
  209. out: false,
  210. },
  211. {
  212. name: "b nil",
  213. a: &Endpoint{},
  214. b: nil,
  215. out: false,
  216. },
  217. {
  218. name: "zero",
  219. a: &Endpoint{},
  220. b: &Endpoint{},
  221. out: true,
  222. },
  223. {
  224. name: "diff port",
  225. a: &Endpoint{Port: 1234},
  226. b: &Endpoint{Port: 5678},
  227. out: false,
  228. },
  229. {
  230. name: "same IP",
  231. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  232. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  233. out: true,
  234. },
  235. {
  236. name: "diff IP",
  237. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  238. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2")}},
  239. out: false,
  240. },
  241. {
  242. name: "same IP ignore DNS",
  243. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "a"}},
  244. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "b"}},
  245. out: true,
  246. },
  247. {
  248. name: "no IP check DNS",
  249. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  250. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "b"}},
  251. out: false,
  252. },
  253. {
  254. name: "no IP check DNS (same)",
  255. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  256. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  257. out: true,
  258. },
  259. {
  260. name: "DNS first, ignore IP",
  261. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "a"}},
  262. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2"), DNS: "a"}},
  263. dnsFirst: true,
  264. out: true,
  265. },
  266. {
  267. name: "DNS first",
  268. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  269. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "b"}},
  270. dnsFirst: true,
  271. out: false,
  272. },
  273. {
  274. name: "DNS first, no DNS compare IP",
  275. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  276. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2"), DNS: ""}},
  277. dnsFirst: true,
  278. out: false,
  279. },
  280. {
  281. name: "DNS first, no DNS compare IP (same)",
  282. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  283. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  284. dnsFirst: true,
  285. out: true,
  286. },
  287. } {
  288. equal := tc.a.Equal(tc.b, tc.dnsFirst)
  289. if equal != tc.out {
  290. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  291. }
  292. }
  293. }
  294. func TestCompareDumpConf(t *testing.T) {
  295. for _, tc := range []struct {
  296. name string
  297. d []byte
  298. c []byte
  299. }{
  300. {
  301. name: "empty",
  302. d: []byte{},
  303. c: []byte{},
  304. },
  305. {
  306. name: "redacted copy from wg output",
  307. d: []byte(`private B7qk8EMlob0nfado0ABM6HulUV607r4yqtBKjhap7S4= 51820 off
  308. key1 (none) 10.254.1.1:51820 100.64.1.0/24,192.168.0.125/32,10.4.0.1/32 1619012801 67048 34952 10
  309. key2 (none) 10.254.2.1:51820 100.64.4.0/24,10.69.76.55/32,100.64.3.0/24,10.66.25.131/32,10.4.0.2/32 1619013058 1134456 10077852 10`),
  310. c: []byte(`[Interface]
  311. ListenPort = 51820
  312. PrivateKey = private
  313. [Peer]
  314. PublicKey = key1
  315. AllowedIPs = 100.64.1.0/24, 192.168.0.125/32, 10.4.0.1/32
  316. Endpoint = 10.254.1.1:51820
  317. PersistentKeepalive = 10
  318. [Peer]
  319. PublicKey = key2
  320. AllowedIPs = 100.64.4.0/24, 10.69.76.55/32, 100.64.3.0/24, 10.66.25.131/32, 10.4.0.2/32
  321. Endpoint = 10.254.2.1:51820
  322. PersistentKeepalive = 10`),
  323. },
  324. } {
  325. dumpConf, _ := ParseDump(tc.d)
  326. conf := Parse(tc.c)
  327. // Equal will ignore runtime fields and only compare configuration fields.
  328. if !dumpConf.Equal(conf) {
  329. diff := pretty.Compare(dumpConf, conf)
  330. t.Errorf("test case %q: got diff: %v", tc.name, diff)
  331. }
  332. }
  333. }