RouteGen_test.c 17 KB

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