RouteGen_test.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. Allocator_free(alloc);
  106. alloc = Allocator_child(allocator);
  107. Log_debug(log, "Reverse");
  108. runTest0(prefixes, expectedOut4, expectedOut6, exceptions4, exceptions6, alloc, log);
  109. Allocator_free(alloc);
  110. }
  111. int main()
  112. {
  113. struct Allocator* alloc = MallocAllocator_new(1<<21);
  114. struct Log* log = FileWriterLog_new(stdout, alloc);
  115. runTest((char*[]){ "0.0.0.0/0", NULL },
  116. (char*[]){ "77.66.55.44/32", NULL },
  117. NULL,
  118. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  119. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  120. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  121. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17",
  122. "77.67.0.0/16", "77.64.0.0/15", "77.68.0.0/14", "77.72.0.0/13",
  123. "77.80.0.0/12", "77.96.0.0/11", "77.0.0.0/10", "77.128.0.0/9",
  124. "76.0.0.0/8", "78.0.0.0/7", "72.0.0.0/6", "64.0.0.0/5",
  125. "80.0.0.0/4", "96.0.0.0/3", "0.0.0.0/2", "128.0.0.0/1", NULL },
  126. NULL,
  127. alloc,
  128. log);
  129. runTest((char*[]){ "77.66.0.0/16", NULL },
  130. (char*[]){ "77.66.55.44/32", NULL },
  131. NULL,
  132. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  133. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  134. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  135. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17", NULL },
  136. NULL,
  137. alloc,
  138. log);
  139. runTest0((char*[]){ "77.66.0.0/16", "77.99.0.0/16", NULL },
  140. (char*[]){ "77.66.55.44/32", NULL },
  141. NULL,
  142. (char*[]){ "77.66.55.45/32", "77.66.55.46/31", "77.66.55.40/30", "77.66.55.32/29",
  143. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  144. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  145. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17",
  146. "77.99.0.0/16", NULL },
  147. NULL,
  148. alloc,
  149. log);
  150. runTest((char*[]){ "0.0.0.0/0", NULL },
  151. (char*[]){ "77.66.55.44/32", "133.33.66.9/32", NULL },
  152. NULL,
  153. (char*[]){ "77.66.55.45/32", "133.33.66.8/32", "77.66.55.46/31", "133.33.66.10/31",
  154. "77.66.55.40/30", "133.33.66.12/30", "77.66.55.32/29", "133.33.66.0/29",
  155. "77.66.55.48/28", "133.33.66.16/28", "77.66.55.0/27", "133.33.66.32/27",
  156. "77.66.55.64/26", "133.33.66.64/26", "77.66.55.128/25", "133.33.66.128/25",
  157. "77.66.54.0/24", "133.33.67.0/24", "77.66.52.0/23", "133.33.64.0/23",
  158. "77.66.48.0/22", "133.33.68.0/22", "77.66.56.0/21", "133.33.72.0/21",
  159. "77.66.32.0/20", "133.33.80.0/20", "77.66.0.0/19", "133.33.96.0/19",
  160. "77.66.64.0/18", "133.33.0.0/18", "77.66.128.0/17", "133.33.128.0/17",
  161. "77.67.0.0/16", "133.32.0.0/16", "77.64.0.0/15", "133.34.0.0/15",
  162. "77.68.0.0/14", "133.36.0.0/14", "77.72.0.0/13", "133.40.0.0/13",
  163. "77.80.0.0/12", "133.48.0.0/12", "77.96.0.0/11", "133.0.0.0/11",
  164. "77.0.0.0/10", "133.64.0.0/10", "77.128.0.0/9", "133.128.0.0/9",
  165. "76.0.0.0/8", "132.0.0.0/8", "78.0.0.0/7", "134.0.0.0/7",
  166. "72.0.0.0/6", "128.0.0.0/6", "64.0.0.0/5", "136.0.0.0/5",
  167. "80.0.0.0/4", "144.0.0.0/4", "96.0.0.0/3", "160.0.0.0/3",
  168. "0.0.0.0/2", "192.0.0.0/2", NULL },
  169. NULL,
  170. alloc,
  171. log);
  172. runTest((char*[]){ "0.0.0.0/0", NULL },
  173. (char*[]){ "77.33.55.44/32", "77.66.55.44/32", NULL },
  174. NULL,
  175. (char*[]){ "77.33.55.45/32", "77.66.55.45/32", "77.33.55.46/31", "77.66.55.46/31",
  176. "77.33.55.40/30", "77.66.55.40/30", "77.33.55.32/29", "77.66.55.32/29",
  177. "77.33.55.48/28", "77.66.55.48/28", "77.33.55.0/27", "77.66.55.0/27",
  178. "77.33.55.64/26", "77.66.55.64/26", "77.33.55.128/25", "77.66.55.128/25",
  179. "77.33.54.0/24", "77.66.54.0/24", "77.33.52.0/23", "77.66.52.0/23",
  180. "77.33.48.0/22", "77.66.48.0/22", "77.33.56.0/21", "77.66.56.0/21",
  181. "77.33.32.0/20", "77.66.32.0/20", "77.33.0.0/19", "77.66.0.0/19",
  182. "77.33.64.0/18", "77.66.64.0/18", "77.33.128.0/17", "77.66.128.0/17",
  183. "77.32.0.0/16", "77.67.0.0/16", "77.34.0.0/15", "77.64.0.0/15",
  184. "77.36.0.0/14", "77.68.0.0/14", "77.40.0.0/13", "77.72.0.0/13",
  185. "77.48.0.0/12", "77.80.0.0/12", "77.0.0.0/11", "77.96.0.0/11",
  186. "77.128.0.0/9", "76.0.0.0/8", "78.0.0.0/7", "72.0.0.0/6",
  187. "64.0.0.0/5", "80.0.0.0/4", "96.0.0.0/3", "0.0.0.0/2",
  188. "128.0.0.0/1", NULL },
  189. NULL,
  190. alloc,
  191. log);
  192. runTest((char*[]){ "77.33.0.0/16", "77.66.0.0/16", NULL },
  193. (char*[]){ "77.33.55.44/32", "77.66.55.44/32", NULL },
  194. NULL,
  195. (char*[]){ "77.33.55.45/32", "77.66.55.45/32", "77.33.55.46/31", "77.66.55.46/31",
  196. "77.33.55.40/30", "77.66.55.40/30", "77.33.55.32/29", "77.66.55.32/29",
  197. "77.33.55.48/28", "77.66.55.48/28", "77.33.55.0/27", "77.66.55.0/27",
  198. "77.33.55.64/26", "77.66.55.64/26", "77.33.55.128/25", "77.66.55.128/25",
  199. "77.33.54.0/24", "77.66.54.0/24", "77.33.52.0/23", "77.66.52.0/23",
  200. "77.33.48.0/22", "77.66.48.0/22", "77.33.56.0/21", "77.66.56.0/21",
  201. "77.33.32.0/20", "77.66.32.0/20", "77.33.0.0/19", "77.66.0.0/19",
  202. "77.33.64.0/18", "77.66.64.0/18", "77.33.128.0/17", "77.66.128.0/17",
  203. NULL },
  204. NULL,
  205. alloc,
  206. log);
  207. runTest((char*[]){ "77.66.0.0/16", NULL },
  208. (char*[]){ "77.66.55.44/30", NULL },
  209. NULL,
  210. (char*[]){ "77.66.55.40/30", "77.66.55.32/29",
  211. "77.66.55.48/28", "77.66.55.0/27", "77.66.55.64/26", "77.66.55.128/25",
  212. "77.66.54.0/24", "77.66.52.0/23", "77.66.48.0/22", "77.66.56.0/21",
  213. "77.66.32.0/20", "77.66.0.0/19", "77.66.64.0/18", "77.66.128.0/17", NULL },
  214. NULL,
  215. alloc,
  216. log);
  217. runTest((char*[]){ "::/0", NULL },
  218. NULL,
  219. (char*[]){ "2a00:1450:400d:807::200e/128", NULL },
  220. NULL,
  221. (char*[]){ "2a00:1450:400d:807::200f/128",
  222. "2a00:1450:400d:807::200c/127", "2a00:1450:400d:807::2008/126",
  223. "2a00:1450:400d:807::2000/125", "2a00:1450:400d:807::2010/124",
  224. "2a00:1450:400d:807::2020/123", "2a00:1450:400d:807::2040/122",
  225. "2a00:1450:400d:807::2080/121", "2a00:1450:400d:807::2100/120",
  226. "2a00:1450:400d:807::2200/119", "2a00:1450:400d:807::2400/118",
  227. "2a00:1450:400d:807::2800/117", "2a00:1450:400d:807::3000/116",
  228. "2a00:1450:400d:807::/115", "2a00:1450:400d:807::4000/114",
  229. "2a00:1450:400d:807::8000/113", "2a00:1450:400d:807::1:0/112",
  230. "2a00:1450:400d:807::2:0/111", "2a00:1450:400d:807::4:0/110",
  231. "2a00:1450:400d:807::8:0/109", "2a00:1450:400d:807::10:0/108",
  232. "2a00:1450:400d:807::20:0/107", "2a00:1450:400d:807::40:0/106",
  233. "2a00:1450:400d:807::80:0/105", "2a00:1450:400d:807::100:0/104",
  234. "2a00:1450:400d:807::200:0/103", "2a00:1450:400d:807::400:0/102",
  235. "2a00:1450:400d:807::800:0/101", "2a00:1450:400d:807::1000:0/100",
  236. "2a00:1450:400d:807::2000:0/99", "2a00:1450:400d:807::4000:0/98",
  237. "2a00:1450:400d:807::8000:0/97", "2a00:1450:400d:807:0:1::/96",
  238. "2a00:1450:400d:807:0:2::/95", "2a00:1450:400d:807:0:4::/94",
  239. "2a00:1450:400d:807:0:8::/93", "2a00:1450:400d:807:0:10::/92",
  240. "2a00:1450:400d:807:0:20::/91", "2a00:1450:400d:807:0:40::/90",
  241. "2a00:1450:400d:807:0:80::/89", "2a00:1450:400d:807:0:100::/88",
  242. "2a00:1450:400d:807:0:200::/87", "2a00:1450:400d:807:0:400::/86",
  243. "2a00:1450:400d:807:0:800::/85", "2a00:1450:400d:807:0:1000::/84",
  244. "2a00:1450:400d:807:0:2000::/83", "2a00:1450:400d:807:0:4000::/82",
  245. "2a00:1450:400d:807:0:8000::/81", "2a00:1450:400d:807:1::/80",
  246. "2a00:1450:400d:807:2::/79", "2a00:1450:400d:807:4::/78",
  247. "2a00:1450:400d:807:8::/77", "2a00:1450:400d:807:10::/76",
  248. "2a00:1450:400d:807:20::/75", "2a00:1450:400d:807:40::/74",
  249. "2a00:1450:400d:807:80::/73", "2a00:1450:400d:807:100::/72",
  250. "2a00:1450:400d:807:200::/71", "2a00:1450:400d:807:400::/70",
  251. "2a00:1450:400d:807:800::/69", "2a00:1450:400d:807:1000::/68",
  252. "2a00:1450:400d:807:2000::/67", "2a00:1450:400d:807:4000::/66",
  253. "2a00:1450:400d:807:8000::/65", "2a00:1450:400d:806::/64",
  254. "2a00:1450:400d:804::/63", "2a00:1450:400d:800::/62",
  255. "2a00:1450:400d:808::/61", "2a00:1450:400d:810::/60",
  256. "2a00:1450:400d:820::/59", "2a00:1450:400d:840::/58",
  257. "2a00:1450:400d:880::/57", "2a00:1450:400d:900::/56",
  258. "2a00:1450:400d:a00::/55", "2a00:1450:400d:c00::/54",
  259. "2a00:1450:400d::/53", "2a00:1450:400d:1000::/52",
  260. "2a00:1450:400d:2000::/51", "2a00:1450:400d:4000::/50",
  261. "2a00:1450:400d:8000::/49", "2a00:1450:400c::/48",
  262. "2a00:1450:400e::/47", "2a00:1450:4008::/46",
  263. "2a00:1450:4000::/45", "2a00:1450:4010::/44", "2a00:1450:4020::/43",
  264. "2a00:1450:4040::/42", "2a00:1450:4080::/41", "2a00:1450:4100::/40",
  265. "2a00:1450:4200::/39", "2a00:1450:4400::/38", "2a00:1450:4800::/37",
  266. "2a00:1450:5000::/36", "2a00:1450:6000::/35", "2a00:1450::/34",
  267. "2a00:1450:8000::/33", "2a00:1451::/32", "2a00:1452::/31",
  268. "2a00:1454::/30", "2a00:1458::/29", "2a00:1440::/28",
  269. "2a00:1460::/27", "2a00:1400::/26", "2a00:1480::/25", "2a00:1500::/24",
  270. "2a00:1600::/23", "2a00:1000::/22", "2a00:1800::/21", "2a00::/20",
  271. "2a00:2000::/19", "2a00:4000::/18", "2a00:8000::/17", "2a01::/16",
  272. "2a02::/15", "2a04::/14", "2a08::/13", "2a10::/12", "2a20::/11",
  273. "2a40::/10", "2a80::/9", "2b00::/8", "2800::/7", "2c00::/6", "2000::/5",
  274. "3000::/4", "::/3", "4000::/2", "8000::/1", NULL },
  275. alloc,
  276. log);
  277. runTest((char*[]){ "2a00:1450:400d:807::2100/119", NULL },
  278. NULL,
  279. (char*[]){ "2a00:1450:400d:807::200e/128", NULL },
  280. NULL,
  281. (char*[]){ "2a00:1450:400d:807::200f/128",
  282. "2a00:1450:400d:807::200c/127", "2a00:1450:400d:807::2008/126",
  283. "2a00:1450:400d:807::2000/125", "2a00:1450:400d:807::2010/124",
  284. "2a00:1450:400d:807::2020/123", "2a00:1450:400d:807::2040/122",
  285. "2a00:1450:400d:807::2080/121", "2a00:1450:400d:807::2100/120", NULL },
  286. alloc,
  287. log);
  288. runTest((char*[]){ "2a00:1450:400d:807::2100/119", NULL },
  289. NULL,
  290. (char*[]){ "2a00:1450:400d:807::2000/128", "2a00:1450:400d:807::200e/128", NULL },
  291. NULL,
  292. (char*[]){ "2a00:1450:400d:807::2001/128", "2a00:1450:400d:807::200f/128",
  293. "2a00:1450:400d:807::2002/127", "2a00:1450:400d:807::200c/127",
  294. "2a00:1450:400d:807::2004/126", "2a00:1450:400d:807::2008/126",
  295. "2a00:1450:400d:807::2010/124", "2a00:1450:400d:807::2020/123",
  296. "2a00:1450:400d:807::2040/122", "2a00:1450:400d:807::2080/121",
  297. "2a00:1450:400d:807::2100/120", NULL },
  298. alloc,
  299. log);
  300. Allocator_free(alloc);
  301. return 0;
  302. }