conf_test.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. )
  19. func TestCompareConf(t *testing.T) {
  20. for _, tc := range []struct {
  21. name string
  22. a []byte
  23. b []byte
  24. out bool
  25. }{
  26. {
  27. name: "empty",
  28. a: []byte{},
  29. b: []byte{},
  30. out: true,
  31. },
  32. {
  33. name: "key and value order",
  34. a: []byte(`[Interface]
  35. PrivateKey = private
  36. ListenPort = 51820
  37. [Peer]
  38. Endpoint = 10.1.0.2:51820
  39. PresharedKey = psk
  40. PublicKey = key
  41. 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
  42. `),
  43. b: []byte(`[Interface]
  44. ListenPort = 51820
  45. PrivateKey = private
  46. [Peer]
  47. PublicKey = key
  48. 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
  49. PresharedKey = psk
  50. Endpoint = 10.1.0.2:51820
  51. `),
  52. out: true,
  53. },
  54. {
  55. name: "whitespace",
  56. a: []byte(`[Interface]
  57. PrivateKey = private
  58. ListenPort = 51820
  59. [Peer]
  60. Endpoint = 10.1.0.2:51820
  61. PresharedKey = psk
  62. PublicKey = key
  63. 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
  64. `),
  65. b: []byte(`[Interface]
  66. PrivateKey=private
  67. ListenPort=51820
  68. [Peer]
  69. Endpoint=10.1.0.2:51820
  70. PresharedKey = psk
  71. PublicKey=key
  72. 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
  73. `),
  74. out: true,
  75. },
  76. {
  77. name: "missing key",
  78. a: []byte(`[Interface]
  79. PrivateKey = private
  80. ListenPort = 51820
  81. [Peer]
  82. Endpoint = 10.1.0.2:51820
  83. PublicKey = key
  84. 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
  85. `),
  86. b: []byte(`[Interface]
  87. PrivateKey = private
  88. ListenPort = 51820
  89. [Peer]
  90. PublicKey = key
  91. 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
  92. `),
  93. out: false,
  94. },
  95. {
  96. name: "different value",
  97. a: []byte(`[Interface]
  98. PrivateKey = private
  99. ListenPort = 51820
  100. [Peer]
  101. Endpoint = 10.1.0.2:51820
  102. PublicKey = key
  103. 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
  104. `),
  105. b: []byte(`[Interface]
  106. PrivateKey = private
  107. ListenPort = 51820
  108. [Peer]
  109. Endpoint = 10.1.0.2:51820
  110. PublicKey = key2
  111. 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
  112. `),
  113. out: false,
  114. },
  115. {
  116. name: "section order",
  117. a: []byte(`[Interface]
  118. PrivateKey = private
  119. ListenPort = 51820
  120. [Peer]
  121. Endpoint = 10.1.0.2:51820
  122. PresharedKey = psk
  123. PublicKey = key
  124. 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
  125. `),
  126. b: []byte(`[Peer]
  127. Endpoint = 10.1.0.2:51820
  128. PresharedKey = psk
  129. PublicKey = key
  130. 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
  131. [Interface]
  132. PrivateKey = private
  133. ListenPort = 51820
  134. `),
  135. out: true,
  136. },
  137. {
  138. name: "out of order peers",
  139. a: []byte(`[Interface]
  140. PrivateKey = private
  141. ListenPort = 51820
  142. [Peer]
  143. Endpoint = 10.1.0.2:51820
  144. PresharedKey = psk2
  145. PublicKey = key2
  146. 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
  147. [Peer]
  148. Endpoint = 10.1.0.2:51820
  149. PresharedKey = psk1
  150. PublicKey = key1
  151. 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
  152. `),
  153. b: []byte(`[Interface]
  154. PrivateKey = private
  155. ListenPort = 51820
  156. [Peer]
  157. Endpoint = 10.1.0.2:51820
  158. PresharedKey = psk1
  159. PublicKey = key1
  160. 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
  161. [Peer]
  162. Endpoint = 10.1.0.2:51820
  163. PresharedKey = psk2
  164. PublicKey = key2
  165. 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
  166. `),
  167. out: true,
  168. },
  169. {
  170. name: "one empty",
  171. a: []byte(`[Interface]
  172. PrivateKey = private
  173. ListenPort = 51820
  174. [Peer]
  175. Endpoint = 10.1.0.2:51820
  176. PresharedKey = psk
  177. PublicKey = key
  178. 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
  179. `),
  180. b: []byte(``),
  181. out: false,
  182. },
  183. } {
  184. equal := Parse(tc.a).Equal(Parse(tc.b))
  185. if equal != tc.out {
  186. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  187. }
  188. }
  189. }
  190. func TestCompareEndpoint(t *testing.T) {
  191. for _, tc := range []struct {
  192. name string
  193. a *Endpoint
  194. b *Endpoint
  195. dnsFirst bool
  196. out bool
  197. }{
  198. {
  199. name: "both nil",
  200. a: nil,
  201. b: nil,
  202. out: true,
  203. },
  204. {
  205. name: "a nil",
  206. a: nil,
  207. b: &Endpoint{},
  208. out: false,
  209. },
  210. {
  211. name: "b nil",
  212. a: &Endpoint{},
  213. b: nil,
  214. out: false,
  215. },
  216. {
  217. name: "zero",
  218. a: &Endpoint{},
  219. b: &Endpoint{},
  220. out: true,
  221. },
  222. {
  223. name: "diff port",
  224. a: &Endpoint{Port: 1234},
  225. b: &Endpoint{Port: 5678},
  226. out: false,
  227. },
  228. {
  229. name: "same IP",
  230. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  231. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  232. out: true,
  233. },
  234. {
  235. name: "diff IP",
  236. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1")}},
  237. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2")}},
  238. out: false,
  239. },
  240. {
  241. name: "same IP ignore DNS",
  242. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "a"}},
  243. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "b"}},
  244. out: true,
  245. },
  246. {
  247. name: "no IP check DNS",
  248. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  249. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "b"}},
  250. out: false,
  251. },
  252. {
  253. name: "no IP check DNS (same)",
  254. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  255. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  256. out: true,
  257. },
  258. {
  259. name: "DNS first, ignore IP",
  260. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: "a"}},
  261. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2"), DNS: "a"}},
  262. dnsFirst: true,
  263. out: true,
  264. },
  265. {
  266. name: "DNS first",
  267. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "a"}},
  268. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{DNS: "b"}},
  269. dnsFirst: true,
  270. out: false,
  271. },
  272. {
  273. name: "DNS first, no DNS compare IP",
  274. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  275. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.2"), DNS: ""}},
  276. dnsFirst: true,
  277. out: false,
  278. },
  279. {
  280. name: "DNS first, no DNS compare IP (same)",
  281. a: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  282. b: &Endpoint{Port: 1234, DNSOrIP: DNSOrIP{IP: net.ParseIP("192.168.0.1"), DNS: ""}},
  283. dnsFirst: true,
  284. out: true,
  285. },
  286. } {
  287. equal := tc.a.Equal(tc.b, tc.dnsFirst)
  288. if equal != tc.out {
  289. t.Errorf("test case %q: expected %t, got %t", tc.name, tc.out, equal)
  290. }
  291. }
  292. }