RouteGen_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "benc/String.h"
  16. #include "benc/Dict.h"
  17. #include "memory/Allocator.h"
  18. #include "memory/MallocAllocator.h"
  19. #include "util/log/Log.h"
  20. #include "util/log/FileWriterLog.h"
  21. #include "util/platform/Sockaddr.h"
  22. #include "crypto/random/Random.h"
  23. #include "tunnel/RouteGen.h"
  24. #include "util/Base10.h"
  25. static struct Sockaddr* mkSockaddr(char* str, struct Allocator* alloc)
  26. {
  27. struct Sockaddr_storage ss;
  28. Assert_true(!Sockaddr_parse(str, &ss));
  29. return Sockaddr_clone(&ss.addr, alloc);
  30. }
  31. static void runTest0(char** prefixes,
  32. char** exceptions4,
  33. char** exceptions6,
  34. char** expectedOut4,
  35. char** expectedOut6,
  36. struct Allocator* alloc,
  37. struct Log* log)
  38. {
  39. struct RouteGen* rg = RouteGen_new(alloc, log);
  40. for (int i = 0; prefixes[i]; i++) {
  41. RouteGen_addPrefix(rg, mkSockaddr(prefixes[i], alloc));
  42. }
  43. for (int i = 0; exceptions4 && exceptions4[i]; i++) {
  44. RouteGen_addException(rg, mkSockaddr(exceptions4[i], alloc));
  45. }
  46. for (int i = 0; exceptions6 && exceptions6[i]; i++) {
  47. RouteGen_addException(rg, mkSockaddr(exceptions6[i], alloc));
  48. }
  49. Dict* routes = RouteGen_getGeneratedRoutes(rg, alloc);
  50. List* routes4 = Dict_getListC(routes, "ipv4");
  51. List* routes6 = Dict_getListC(routes, "ipv6");
  52. if (expectedOut4) {
  53. for (int i = 0; expectedOut4[i]; i++) {
  54. Log_debug(log, "%s\n", expectedOut4[i]);
  55. }
  56. for (int i = 0; i < List_size(routes4); i++) {
  57. Log_debug(log, "%s\n", List_getString(routes4, i)->bytes);
  58. }
  59. Assert_true(!expectedOut4[List_size(routes4)]);
  60. for (int i = 0; i < List_size(routes4); i++) {
  61. String* str = List_getString(routes4, i);
  62. Assert_true(str);
  63. Assert_true(expectedOut4[i]);
  64. if (CString_strncmp(expectedOut4[i], str->bytes, str->len)) {
  65. Log_error(log, "Fail\nexpected: %s\nGot: %s\n", expectedOut4[i], str->bytes);
  66. Assert_failure("fail");
  67. }
  68. }
  69. } else {
  70. Assert_true(!List_size(routes4));
  71. }
  72. if (expectedOut6) {
  73. for (int i = 0; expectedOut6[i]; i++) {
  74. Log_debug(log, "%s\n", expectedOut6[i]);
  75. }
  76. for (int i = 0; i < List_size(routes6); i++) {
  77. Log_debug(log, "%s\n", List_getString(routes6, i)->bytes);
  78. }
  79. Assert_true(!expectedOut6[List_size(routes6)]);
  80. for (int i = 0; i < List_size(routes6); i++) {
  81. String* str = List_getString(routes6, i);
  82. Assert_true(str);
  83. Assert_true(expectedOut6[i]);
  84. if (CString_strncmp(expectedOut6[i], str->bytes, str->len)) {
  85. Log_error(log, "Fail\nexpected: %s\nGot: %s\n", expectedOut6[i], str->bytes);
  86. Assert_failure("fail");
  87. }
  88. }
  89. } else {
  90. Assert_true(!List_size(routes6));
  91. }
  92. }
  93. static void runTest(char** prefixes,
  94. char** exceptions4,
  95. char** exceptions6,
  96. char** expectedOut4,
  97. char** expectedOut6,
  98. struct Allocator* allocator,
  99. struct Log* log)
  100. {
  101. // Check that routes are invertible.
  102. struct Allocator* alloc = Allocator_child(allocator);
  103. Log_debug(log, "Forward");
  104. runTest0(prefixes, exceptions4, exceptions6, expectedOut4, expectedOut6, alloc, log);
  105. Log_debug(log, "Reverse");
  106. runTest0(prefixes, expectedOut4, expectedOut6, exceptions4, exceptions6, alloc, log);
  107. Allocator_free(alloc);
  108. }
  109. int main()
  110. {
  111. struct Allocator* alloc = MallocAllocator_new(1<<20);
  112. struct Log* log = FileWriterLog_new(stdout, alloc);
  113. runTest((char*[]){ "0.0.0.0/0", NULL },
  114. (char*[]){ "77.66.55.44/32", NULL },
  115. NULL,
  116. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  117. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  118. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  119. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17",
  120. "77.67.0.0/16", "77.64.0.0/15", "77.68.0.0/14", "77.72.0.0/13",
  121. "77.80.0.0/12", "77.96.0.0/11", "77.0.0.0/10", "77.128.0.0/9",
  122. "76.0.0.0/8", "78.0.0.0/7", "72.0.0.0/6", "64.0.0.0/5",
  123. "80.0.0.0/4", "96.0.0.0/3", "0.0.0.0/2", "128.0.0.0/1", NULL },
  124. NULL,
  125. alloc,
  126. log);
  127. runTest((char*[]){ "77.66.0.0/16", NULL },
  128. (char*[]){ "77.66.55.44/32", NULL },
  129. NULL,
  130. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  131. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  132. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  133. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17", NULL },
  134. NULL,
  135. alloc,
  136. log);
  137. runTest0((char*[]){ "77.66.0.0/16", "77.99.0.0/16", NULL },
  138. (char*[]){ "77.66.55.44/32", NULL },
  139. NULL,
  140. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  141. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  142. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  143. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17",
  144. "77.99.0.0/16", NULL },
  145. NULL,
  146. alloc,
  147. log);
  148. runTest((char*[]){ "0.0.0.0/0", NULL },
  149. (char*[]){ "77.66.55.44/32", "133.33.66.9/32", NULL },
  150. NULL,
  151. (char*[]){ "77.66.55.45/32", "133.33.66.8/32", "77.66.55.46/31", "133.33.66.10/31",
  152. "77.66.55.40/30", "133.33.66.12/30", "77.66.55.32/29", "133.33.66.0/29",
  153. "77.66.55.48/28", "133.33.66.16/28", "77.66.55.0/27", "133.33.66.32/27",
  154. "77.66.55.64/26", "133.33.66.64/26", "77.66.55.128/25", "133.33.66.128/25",
  155. "77.66.54.0/24", "133.33.67.0/24", "77.66.52.0/23", "133.33.64.0/23",
  156. "77.66.48.0/22", "133.33.68.0/22", "77.66.56.0/21", "133.33.72.0/21",
  157. "77.66.32.0/20", "133.33.80.0/20", "77.66.0.0/19", "133.33.96.0/19",
  158. "77.66.64.0/18", "133.33.0.0/18", "77.66.128.0/17", "133.33.128.0/17",
  159. "77.67.0.0/16", "133.32.0.0/16", "77.64.0.0/15", "133.34.0.0/15",
  160. "77.68.0.0/14", "133.36.0.0/14", "77.72.0.0/13", "133.40.0.0/13",
  161. "77.80.0.0/12", "133.48.0.0/12", "77.96.0.0/11", "133.0.0.0/11",
  162. "77.0.0.0/10", "133.64.0.0/10", "77.128.0.0/9", "133.128.0.0/9",
  163. "76.0.0.0/8", "132.0.0.0/8", "78.0.0.0/7", "134.0.0.0/7",
  164. "72.0.0.0/6", "128.0.0.0/6", "64.0.0.0/5", "136.0.0.0/5",
  165. "80.0.0.0/4", "144.0.0.0/4", "96.0.0.0/3", "160.0.0.0/3",
  166. "0.0.0.0/2", "192.0.0.0/2", NULL },
  167. NULL,
  168. alloc,
  169. log);
  170. runTest((char*[]){ "0.0.0.0/0", NULL },
  171. (char*[]){ "77.33.55.44/32", "77.66.55.44/32", NULL },
  172. NULL,
  173. (char*[]){ "77.33.55.45/32", "77.66.55.45/32", "77.33.55.46/31", "77.66.55.46/31",
  174. "77.33.55.40/30", "77.66.55.40/30", "77.33.55.32/29", "77.66.55.32/29",
  175. "77.33.55.48/28", "77.66.55.48/28", "77.33.55.0/27", "77.66.55.0/27",
  176. "77.33.55.64/26", "77.66.55.64/26", "77.33.55.128/25", "77.66.55.128/25",
  177. "77.33.54.0/24", "77.66.54.0/24", "77.33.52.0/23", "77.66.52.0/23",
  178. "77.33.48.0/22", "77.66.48.0/22", "77.33.56.0/21", "77.66.56.0/21",
  179. "77.33.32.0/20", "77.66.32.0/20", "77.33.0.0/19", "77.66.0.0/19",
  180. "77.33.64.0/18", "77.66.64.0/18", "77.33.128.0/17", "77.66.128.0/17",
  181. "77.32.0.0/16", "77.67.0.0/16", "77.34.0.0/15", "77.64.0.0/15",
  182. "77.36.0.0/14", "77.68.0.0/14", "77.40.0.0/13", "77.72.0.0/13",
  183. "77.48.0.0/12", "77.80.0.0/12", "77.0.0.0/11", "77.96.0.0/11",
  184. "77.128.0.0/9", "76.0.0.0/8", "78.0.0.0/7", "72.0.0.0/6",
  185. "64.0.0.0/5", "80.0.0.0/4", "96.0.0.0/3", "0.0.0.0/2",
  186. "128.0.0.0/1", NULL },
  187. NULL,
  188. alloc,
  189. log);
  190. runTest((char*[]){ "77.33.0.0/16", "77.66.0.0/16", NULL },
  191. (char*[]){ "77.33.55.44/32", "77.66.55.44/32", NULL },
  192. NULL,
  193. (char*[]){ "77.33.55.45/32", "77.66.55.45/32", "77.33.55.46/31", "77.66.55.46/31",
  194. "77.33.55.40/30", "77.66.55.40/30", "77.33.55.32/29", "77.66.55.32/29",
  195. "77.33.55.48/28", "77.66.55.48/28", "77.33.55.0/27", "77.66.55.0/27",
  196. "77.33.55.64/26", "77.66.55.64/26", "77.33.55.128/25", "77.66.55.128/25",
  197. "77.33.54.0/24", "77.66.54.0/24", "77.33.52.0/23", "77.66.52.0/23",
  198. "77.33.48.0/22", "77.66.48.0/22", "77.33.56.0/21", "77.66.56.0/21",
  199. "77.33.32.0/20", "77.66.32.0/20", "77.33.0.0/19", "77.66.0.0/19",
  200. "77.33.64.0/18", "77.66.64.0/18", "77.33.128.0/17", "77.66.128.0/17",
  201. NULL },
  202. NULL,
  203. alloc,
  204. log);
  205. runTest((char*[]){ "77.66.0.0/16", NULL },
  206. (char*[]){ "77.66.55.44/30", NULL },
  207. NULL,
  208. (char*[]){ "77.66.55.40/30", "77.66.55.32/29",
  209. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  210. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  211. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17", NULL },
  212. NULL,
  213. alloc,
  214. log);
  215. runTest((char*[]){ "::/0", NULL },
  216. NULL,
  217. (char*[]){ "2a00:1450:400d:807::200e/128", NULL },
  218. NULL,
  219. (char*[]){ "2a00:1450:400d:807::200f/128",
  220. "2a00:1450:400d:807::200c/127", "2a00:1450:400d:807::2008/126",
  221. "2a00:1450:400d:807::2000/125", "2a00:1450:400d:807::2010/124",
  222. "2a00:1450:400d:807::2020/123", "2a00:1450:400d:807::2040/122",
  223. "2a00:1450:400d:807::2080/121", "2a00:1450:400d:807::2100/120",
  224. "2a00:1450:400d:807::2200/119", "2a00:1450:400d:807::2400/118",
  225. "2a00:1450:400d:807::2800/117", "2a00:1450:400d:807::3000/116",
  226. "2a00:1450:400d:807::/115", "2a00:1450:400d:807::4000/114",
  227. "2a00:1450:400d:807::8000/113", "2a00:1450:400d:807::1:0/112",
  228. "2a00:1450:400d:807::2:0/111", "2a00:1450:400d:807::4:0/110",
  229. "2a00:1450:400d:807::8:0/109", "2a00:1450:400d:807::10:0/108",
  230. "2a00:1450:400d:807::20:0/107", "2a00:1450:400d:807::40:0/106",
  231. "2a00:1450:400d:807::80:0/105", "2a00:1450:400d:807::100:0/104",
  232. "2a00:1450:400d:807::200:0/103", "2a00:1450:400d:807::400:0/102",
  233. "2a00:1450:400d:807::800:0/101", "2a00:1450:400d:807::1000:0/100",
  234. "2a00:1450:400d:807::2000:0/99", "2a00:1450:400d:807::4000:0/98",
  235. "2a00:1450:400d:807::8000:0/97", "2a00:1450:400d:807:0:1::/96",
  236. "2a00:1450:400d:807:0:2::/95", "2a00:1450:400d:807:0:4::/94",
  237. "2a00:1450:400d:807:0:8::/93", "2a00:1450:400d:807:0:10::/92",
  238. "2a00:1450:400d:807:0:20::/91", "2a00:1450:400d:807:0:40::/90",
  239. "2a00:1450:400d:807:0:80::/89", "2a00:1450:400d:807:0:100::/88",
  240. "2a00:1450:400d:807:0:200::/87", "2a00:1450:400d:807:0:400::/86",
  241. "2a00:1450:400d:807:0:800::/85", "2a00:1450:400d:807:0:1000::/84",
  242. "2a00:1450:400d:807:0:2000::/83", "2a00:1450:400d:807:0:4000::/82",
  243. "2a00:1450:400d:807:0:8000::/81", "2a00:1450:400d:807:1::/80",
  244. "2a00:1450:400d:807:2::/79", "2a00:1450:400d:807:4::/78",
  245. "2a00:1450:400d:807:8::/77", "2a00:1450:400d:807:10::/76",
  246. "2a00:1450:400d:807:20::/75", "2a00:1450:400d:807:40::/74",
  247. "2a00:1450:400d:807:80::/73", "2a00:1450:400d:807:100::/72",
  248. "2a00:1450:400d:807:200::/71", "2a00:1450:400d:807:400::/70",
  249. "2a00:1450:400d:807:800::/69", "2a00:1450:400d:807:1000::/68",
  250. "2a00:1450:400d:807:2000::/67", "2a00:1450:400d:807:4000::/66",
  251. "2a00:1450:400d:807:8000::/65", "2a00:1450:400d:806::/64",
  252. "2a00:1450:400d:804::/63", "2a00:1450:400d:800::/62",
  253. "2a00:1450:400d:808::/61", "2a00:1450:400d:810::/60",
  254. "2a00:1450:400d:820::/59", "2a00:1450:400d:840::/58",
  255. "2a00:1450:400d:880::/57", "2a00:1450:400d:900::/56",
  256. "2a00:1450:400d:a00::/55", "2a00:1450:400d:c00::/54",
  257. "2a00:1450:400d::/53", "2a00:1450:400d:1000::/52",
  258. "2a00:1450:400d:2000::/51", "2a00:1450:400d:4000::/50",
  259. "2a00:1450:400d:8000::/49", "2a00:1450:400c::/48",
  260. "2a00:1450:400e::/47", "2a00:1450:4008::/46",
  261. "2a00:1450:4000::/45", "2a00:1450:4010::/44", "2a00:1450:4020::/43",
  262. "2a00:1450:4040::/42", "2a00:1450:4080::/41", "2a00:1450:4100::/40",
  263. "2a00:1450:4200::/39", "2a00:1450:4400::/38", "2a00:1450:4800::/37",
  264. "2a00:1450:5000::/36", "2a00:1450:6000::/35", "2a00:1450::/34",
  265. "2a00:1450:8000::/33", "2a00:1451::/32", "2a00:1452::/31",
  266. "2a00:1454::/30", "2a00:1458::/29", "2a00:1440::/28",
  267. "2a00:1460::/27", "2a00:1400::/26", "2a00:1480::/25", "2a00:1500::/24",
  268. "2a00:1600::/23", "2a00:1000::/22", "2a00:1800::/21", "2a00::/20",
  269. "2a00:2000::/19", "2a00:4000::/18", "2a00:8000::/17", "2a01::/16",
  270. "2a02::/15", "2a04::/14", "2a08::/13", "2a10::/12", "2a20::/11",
  271. "2a40::/10", "2a80::/9", "2b00::/8", "2800::/7", "2c00::/6", "2000::/5",
  272. "3000::/4", "::/3", "4000::/2", "8000::/1", NULL },
  273. alloc,
  274. log);
  275. runTest((char*[]){ "2a00:1450:400d:807::2100/119", NULL },
  276. NULL,
  277. (char*[]){ "2a00:1450:400d:807::200e/128", NULL },
  278. NULL,
  279. (char*[]){ "2a00:1450:400d:807::200f/128",
  280. "2a00:1450:400d:807::200c/127", "2a00:1450:400d:807::2008/126",
  281. "2a00:1450:400d:807::2000/125", "2a00:1450:400d:807::2010/124",
  282. "2a00:1450:400d:807::2020/123", "2a00:1450:400d:807::2040/122",
  283. "2a00:1450:400d:807::2080/121", "2a00:1450:400d:807::2100/120", NULL },
  284. alloc,
  285. log);
  286. runTest((char*[]){ "2a00:1450:400d:807::2100/119", NULL },
  287. NULL,
  288. (char*[]){ "2a00:1450:400d:807::2000/128", "2a00:1450:400d:807::200e/128", NULL },
  289. NULL,
  290. (char*[]){ "2a00:1450:400d:807::2001/128", "2a00:1450:400d:807::200f/128",
  291. "2a00:1450:400d:807::2002/127", "2a00:1450:400d:807::200c/127",
  292. "2a00:1450:400d:807::2004/126", "2a00:1450:400d:807::2008/126",
  293. "2a00:1450:400d:807::2010/124", "2a00:1450:400d:807::2020/123",
  294. "2a00:1450:400d:807::2040/122", "2a00:1450:400d:807::2080/121",
  295. "2a00:1450:400d:807::2100/120", NULL },
  296. alloc,
  297. log);
  298. Allocator_free(alloc);
  299. return 0;
  300. }