conf_test.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // Copyright 2021 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 TestNewEndpoint(t *testing.T) {
  21. for i, tc := range []struct {
  22. name string
  23. ip net.IP
  24. port int
  25. out *Endpoint
  26. }{
  27. {
  28. name: "no ip, no port",
  29. out: &Endpoint{
  30. udpAddr: &net.UDPAddr{},
  31. },
  32. },
  33. {
  34. name: "only port",
  35. ip: nil,
  36. port: 99,
  37. out: &Endpoint{
  38. udpAddr: &net.UDPAddr{
  39. Port: 99,
  40. },
  41. },
  42. },
  43. {
  44. name: "only ipv4",
  45. ip: net.ParseIP("10.0.0.0"),
  46. out: &Endpoint{
  47. udpAddr: &net.UDPAddr{
  48. IP: net.ParseIP("10.0.0.0").To4(),
  49. },
  50. },
  51. },
  52. {
  53. name: "only ipv6",
  54. ip: net.ParseIP("ff50::10"),
  55. out: &Endpoint{
  56. udpAddr: &net.UDPAddr{
  57. IP: net.ParseIP("ff50::10").To16(),
  58. },
  59. },
  60. },
  61. {
  62. name: "ipv4",
  63. ip: net.ParseIP("10.0.0.0"),
  64. port: 1000,
  65. out: &Endpoint{
  66. udpAddr: &net.UDPAddr{
  67. IP: net.ParseIP("10.0.0.0").To4(),
  68. Port: 1000,
  69. },
  70. },
  71. },
  72. {
  73. name: "ipv6",
  74. ip: net.ParseIP("ff50::10"),
  75. port: 1000,
  76. out: &Endpoint{
  77. udpAddr: &net.UDPAddr{
  78. IP: net.ParseIP("ff50::10").To16(),
  79. Port: 1000,
  80. },
  81. },
  82. },
  83. {
  84. name: "ipv6",
  85. ip: net.ParseIP("fc00:f853:ccd:e793::3"),
  86. port: 51820,
  87. out: &Endpoint{
  88. udpAddr: &net.UDPAddr{
  89. IP: net.ParseIP("fc00:f853:ccd:e793::3").To16(),
  90. Port: 51820,
  91. },
  92. },
  93. },
  94. } {
  95. out := NewEndpoint(tc.ip, tc.port)
  96. if diff := pretty.Compare(out, tc.out); diff != "" {
  97. t.Errorf("%d %s: got diff:\n%s\n", i, tc.name, diff)
  98. }
  99. }
  100. }
  101. func TestParseEndpoint(t *testing.T) {
  102. for i, tc := range []struct {
  103. name string
  104. str string
  105. out *Endpoint
  106. }{
  107. {
  108. name: "no ip, no port",
  109. },
  110. {
  111. name: "only port",
  112. str: ":1000",
  113. },
  114. {
  115. name: "only ipv4",
  116. str: "10.0.0.0",
  117. },
  118. {
  119. name: "only ipv6",
  120. str: "ff50::10",
  121. },
  122. {
  123. name: "ipv4",
  124. str: "10.0.0.0:1000",
  125. out: &Endpoint{
  126. udpAddr: &net.UDPAddr{
  127. IP: net.ParseIP("10.0.0.0").To4(),
  128. Port: 1000,
  129. },
  130. },
  131. },
  132. {
  133. name: "ipv6",
  134. str: "[ff50::10]:1000",
  135. out: &Endpoint{
  136. udpAddr: &net.UDPAddr{
  137. IP: net.ParseIP("ff50::10").To16(),
  138. Port: 1000,
  139. },
  140. },
  141. },
  142. } {
  143. out := ParseEndpoint(tc.str)
  144. if diff := pretty.Compare(out, tc.out); diff != "" {
  145. t.Errorf("ParseEndpoint %s(%d): got diff:\n%s\n", tc.name, i, diff)
  146. }
  147. }
  148. }
  149. func TestNewEndpointFromUDPAddr(t *testing.T) {
  150. for i, tc := range []struct {
  151. name string
  152. u *net.UDPAddr
  153. out *Endpoint
  154. }{
  155. {
  156. name: "no ip, no port",
  157. out: &Endpoint{
  158. addr: "",
  159. },
  160. },
  161. {
  162. name: "only port",
  163. u: &net.UDPAddr{
  164. Port: 1000,
  165. },
  166. out: &Endpoint{
  167. udpAddr: &net.UDPAddr{
  168. Port: 1000,
  169. },
  170. addr: "",
  171. },
  172. },
  173. {
  174. name: "only ipv4",
  175. u: &net.UDPAddr{
  176. IP: net.ParseIP("10.0.0.0"),
  177. },
  178. out: &Endpoint{
  179. udpAddr: &net.UDPAddr{
  180. IP: net.ParseIP("10.0.0.0").To4(),
  181. },
  182. addr: "",
  183. },
  184. },
  185. {
  186. name: "only ipv6",
  187. u: &net.UDPAddr{
  188. IP: net.ParseIP("ff60::10"),
  189. },
  190. out: &Endpoint{
  191. udpAddr: &net.UDPAddr{
  192. IP: net.ParseIP("ff60::10").To16(),
  193. },
  194. },
  195. },
  196. {
  197. name: "ipv4",
  198. u: &net.UDPAddr{
  199. IP: net.ParseIP("10.0.0.0"),
  200. Port: 1000,
  201. },
  202. out: &Endpoint{
  203. udpAddr: &net.UDPAddr{
  204. IP: net.ParseIP("10.0.0.0").To4(),
  205. Port: 1000,
  206. },
  207. },
  208. },
  209. {
  210. name: "ipv6",
  211. u: &net.UDPAddr{
  212. IP: net.ParseIP("ff50::10"),
  213. Port: 1000,
  214. },
  215. out: &Endpoint{
  216. udpAddr: &net.UDPAddr{
  217. IP: net.ParseIP("ff50::10").To16(),
  218. Port: 1000,
  219. },
  220. },
  221. },
  222. } {
  223. out := NewEndpointFromUDPAddr(tc.u)
  224. if diff := pretty.Compare(out, tc.out); diff != "" {
  225. t.Errorf("ParseEndpoint %s(%d): got diff:\n%s\n", tc.name, i, diff)
  226. }
  227. }
  228. }
  229. func TestReady(t *testing.T) {
  230. for i, tc := range []struct {
  231. name string
  232. in *Endpoint
  233. r bool
  234. }{
  235. {
  236. name: "nil",
  237. r: false,
  238. },
  239. {
  240. name: "no ip, no port",
  241. in: &Endpoint{
  242. addr: "",
  243. udpAddr: &net.UDPAddr{},
  244. },
  245. r: false,
  246. },
  247. {
  248. name: "only port",
  249. in: &Endpoint{
  250. udpAddr: &net.UDPAddr{
  251. Port: 1000,
  252. },
  253. },
  254. r: false,
  255. },
  256. {
  257. name: "only ipv4",
  258. in: &Endpoint{
  259. udpAddr: &net.UDPAddr{
  260. IP: net.ParseIP("10.0.0.0"),
  261. },
  262. },
  263. r: false,
  264. },
  265. {
  266. name: "only ipv6",
  267. in: &Endpoint{
  268. udpAddr: &net.UDPAddr{
  269. IP: net.ParseIP("ff60::10"),
  270. },
  271. },
  272. r: false,
  273. },
  274. {
  275. name: "ipv4",
  276. in: &Endpoint{
  277. udpAddr: &net.UDPAddr{
  278. IP: net.ParseIP("10.0.0.0"),
  279. Port: 1000,
  280. },
  281. },
  282. r: true,
  283. },
  284. {
  285. name: "ipv6",
  286. in: &Endpoint{
  287. udpAddr: &net.UDPAddr{
  288. IP: net.ParseIP("ff50::10"),
  289. Port: 1000,
  290. },
  291. },
  292. r: true,
  293. },
  294. } {
  295. if tc.r != tc.in.Ready() {
  296. t.Errorf("Endpoint.Ready() %s(%d): expected=%v\tgot=%v\n", tc.name, i, tc.r, tc.in.Ready())
  297. }
  298. }
  299. }
  300. func TestEqual(t *testing.T) {
  301. for i, tc := range []struct {
  302. name string
  303. a *Endpoint
  304. b *Endpoint
  305. df bool
  306. r bool
  307. }{
  308. {
  309. name: "nil dns last",
  310. r: true,
  311. },
  312. {
  313. name: "nil dns first",
  314. df: true,
  315. r: true,
  316. },
  317. {
  318. name: "equal: only port",
  319. a: &Endpoint{
  320. udpAddr: &net.UDPAddr{
  321. Port: 1000,
  322. },
  323. },
  324. b: &Endpoint{
  325. udpAddr: &net.UDPAddr{
  326. Port: 1000,
  327. },
  328. },
  329. r: true,
  330. },
  331. {
  332. name: "not equal: only port",
  333. a: &Endpoint{
  334. udpAddr: &net.UDPAddr{
  335. Port: 1000,
  336. },
  337. },
  338. b: &Endpoint{
  339. udpAddr: &net.UDPAddr{
  340. Port: 1001,
  341. },
  342. },
  343. r: false,
  344. },
  345. {
  346. name: "equal dns first",
  347. a: &Endpoint{
  348. udpAddr: &net.UDPAddr{
  349. Port: 1000,
  350. IP: net.ParseIP("10.0.0.0"),
  351. },
  352. addr: "example.com:1000",
  353. },
  354. b: &Endpoint{
  355. udpAddr: &net.UDPAddr{
  356. Port: 1000,
  357. IP: net.ParseIP("10.0.0.0"),
  358. },
  359. addr: "example.com:1000",
  360. },
  361. r: true,
  362. },
  363. {
  364. name: "equal dns last",
  365. a: &Endpoint{
  366. udpAddr: &net.UDPAddr{
  367. Port: 1000,
  368. IP: net.ParseIP("10.0.0.0"),
  369. },
  370. addr: "example.com:1000",
  371. },
  372. b: &Endpoint{
  373. udpAddr: &net.UDPAddr{
  374. Port: 1000,
  375. IP: net.ParseIP("10.0.0.0"),
  376. },
  377. addr: "foo",
  378. },
  379. r: true,
  380. },
  381. {
  382. name: "unequal dns first",
  383. a: &Endpoint{
  384. udpAddr: &net.UDPAddr{
  385. Port: 1000,
  386. IP: net.ParseIP("10.0.0.0"),
  387. },
  388. addr: "example.com:1000",
  389. },
  390. b: &Endpoint{
  391. udpAddr: &net.UDPAddr{
  392. Port: 1000,
  393. IP: net.ParseIP("10.0.0.0"),
  394. },
  395. addr: "foo",
  396. },
  397. df: true,
  398. r: false,
  399. },
  400. {
  401. name: "unequal dns last",
  402. a: &Endpoint{
  403. udpAddr: &net.UDPAddr{
  404. Port: 1000,
  405. IP: net.ParseIP("10.0.0.0"),
  406. },
  407. addr: "foo",
  408. },
  409. b: &Endpoint{
  410. udpAddr: &net.UDPAddr{
  411. Port: 1000,
  412. IP: net.ParseIP("11.0.0.0"),
  413. },
  414. addr: "foo",
  415. },
  416. r: false,
  417. },
  418. {
  419. name: "unequal dns last empty IP",
  420. a: &Endpoint{
  421. addr: "foo",
  422. },
  423. b: &Endpoint{
  424. addr: "bar",
  425. },
  426. r: false,
  427. },
  428. {
  429. name: "equal dns last empty IP",
  430. a: &Endpoint{
  431. addr: "foo",
  432. },
  433. b: &Endpoint{
  434. addr: "foo",
  435. },
  436. r: true,
  437. },
  438. } {
  439. if out := tc.a.Equal(tc.b, tc.df); out != tc.r {
  440. t.Errorf("ParseEndpoint %s(%d): expected: %v\tgot: %v\n", tc.name, i, tc.r, out)
  441. }
  442. }
  443. }