test_routerset.c 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230
  1. /* Copyright (c) 2014-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define ROUTERSET_PRIVATE
  4. #include "core/or/or.h"
  5. #include "feature/stats/geoip.h"
  6. #include "feature/nodelist/routerset.h"
  7. #include "feature/nodelist/routerparse.h"
  8. #include "core/or/policies.h"
  9. #include "feature/nodelist/nodelist.h"
  10. #include "core/or/addr_policy_st.h"
  11. #include "core/or/extend_info_st.h"
  12. #include "feature/nodelist/node_st.h"
  13. #include "feature/nodelist/routerinfo_st.h"
  14. #include "feature/nodelist/routerstatus_st.h"
  15. #include "test/test.h"
  16. #define NS_MODULE routerset
  17. #define NS_SUBMODULE routerset_new
  18. /*
  19. * Functional (blackbox) test to determine that each member of the routerset
  20. * is non-NULL
  21. */
  22. static void
  23. NS(test_main)(void *arg)
  24. {
  25. routerset_t *rs;
  26. (void)arg;
  27. rs = routerset_new();
  28. tt_ptr_op(rs, OP_NE, NULL);
  29. tt_ptr_op(rs->list, OP_NE, NULL);
  30. tt_ptr_op(rs->names, OP_NE, NULL);
  31. tt_ptr_op(rs->digests, OP_NE, NULL);
  32. tt_ptr_op(rs->policies, OP_NE, NULL);
  33. tt_ptr_op(rs->country_names, OP_NE, NULL);
  34. done:
  35. routerset_free(rs);
  36. }
  37. #undef NS_SUBMODULE
  38. #define NS_SUBMODULE routerset_get_countryname
  39. /*
  40. * Functional test to strip the braces from a "{xx}" country code string.
  41. */
  42. static void
  43. NS(test_main)(void *arg)
  44. {
  45. const char *input;
  46. char *name;
  47. (void)arg;
  48. /* strlen(c) < 4 */
  49. input = "xxx";
  50. name = routerset_get_countryname(input);
  51. tt_ptr_op(name, OP_EQ, NULL);
  52. tor_free(name);
  53. /* c[0] != '{' */
  54. input = "xxx}";
  55. name = routerset_get_countryname(input);
  56. tt_ptr_op(name, OP_EQ, NULL);
  57. tor_free(name);
  58. /* c[3] != '}' */
  59. input = "{xxx";
  60. name = routerset_get_countryname(input);
  61. tt_ptr_op(name, OP_EQ, NULL);
  62. tor_free(name);
  63. /* tor_strlower */
  64. input = "{XX}";
  65. name = routerset_get_countryname(input);
  66. tt_str_op(name, OP_EQ, "xx");
  67. tor_free(name);
  68. input = "{xx}";
  69. name = routerset_get_countryname(input);
  70. tt_str_op(name, OP_EQ, "xx");
  71. done:
  72. tor_free(name);
  73. }
  74. #undef NS_SUBMODULE
  75. #define NS_SUBMODULE ASPECT(routerset_refresh_counties, geoip_not_loaded)
  76. /*
  77. * Structural (whitebox) test for routerset_refresh_counties, when the GeoIP DB
  78. * is not loaded.
  79. */
  80. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  81. NS_DECL(int, geoip_get_n_countries, (void));
  82. static void
  83. NS(test_main)(void *arg)
  84. {
  85. routerset_t *set = routerset_new();
  86. (void)arg;
  87. NS_MOCK(geoip_is_loaded);
  88. NS_MOCK(geoip_get_n_countries);
  89. routerset_refresh_countries(set);
  90. tt_ptr_op(set->countries, OP_EQ, NULL);
  91. tt_int_op(set->n_countries, OP_EQ, 0);
  92. tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
  93. tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 0);
  94. done:
  95. NS_UNMOCK(geoip_is_loaded);
  96. NS_UNMOCK(geoip_get_n_countries);
  97. routerset_free(set);
  98. }
  99. static int
  100. NS(geoip_is_loaded)(sa_family_t family)
  101. {
  102. (void)family;
  103. CALLED(geoip_is_loaded)++;
  104. return 0;
  105. }
  106. static int
  107. NS(geoip_get_n_countries)(void)
  108. {
  109. CALLED(geoip_get_n_countries)++;
  110. return 0;
  111. }
  112. #undef NS_SUBMODULE
  113. #define NS_SUBMODULE ASPECT(routerset_refresh_counties, no_countries)
  114. /*
  115. * Structural test for routerset_refresh_counties, when there are no countries.
  116. */
  117. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  118. NS_DECL(int, geoip_get_n_countries, (void));
  119. NS_DECL(country_t, geoip_get_country, (const char *country));
  120. static void
  121. NS(test_main)(void *arg)
  122. {
  123. routerset_t *set = routerset_new();
  124. (void)arg;
  125. NS_MOCK(geoip_is_loaded);
  126. NS_MOCK(geoip_get_n_countries);
  127. NS_MOCK(geoip_get_country);
  128. routerset_refresh_countries(set);
  129. tt_ptr_op(set->countries, OP_NE, NULL);
  130. tt_int_op(set->n_countries, OP_EQ, 1);
  131. tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
  132. tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
  133. tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
  134. tt_int_op(CALLED(geoip_get_country), OP_EQ, 0);
  135. done:
  136. NS_UNMOCK(geoip_is_loaded);
  137. NS_UNMOCK(geoip_get_n_countries);
  138. NS_UNMOCK(geoip_get_country);
  139. routerset_free(set);
  140. }
  141. static int
  142. NS(geoip_is_loaded)(sa_family_t family)
  143. {
  144. (void)family;
  145. CALLED(geoip_is_loaded)++;
  146. return 1;
  147. }
  148. static int
  149. NS(geoip_get_n_countries)(void)
  150. {
  151. CALLED(geoip_get_n_countries)++;
  152. return 1;
  153. }
  154. static country_t
  155. NS(geoip_get_country)(const char *countrycode)
  156. {
  157. (void)countrycode;
  158. CALLED(geoip_get_country)++;
  159. return 1;
  160. }
  161. #undef NS_SUBMODULE
  162. #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_valid_country)
  163. /*
  164. * Structural test for routerset_refresh_counties, with one valid country.
  165. */
  166. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  167. NS_DECL(int, geoip_get_n_countries, (void));
  168. NS_DECL(country_t, geoip_get_country, (const char *country));
  169. static void
  170. NS(test_main)(void *arg)
  171. {
  172. routerset_t *set = routerset_new();
  173. (void)arg;
  174. NS_MOCK(geoip_is_loaded);
  175. NS_MOCK(geoip_get_n_countries);
  176. NS_MOCK(geoip_get_country);
  177. smartlist_add(set->country_names, tor_strndup("foo", 3));
  178. routerset_refresh_countries(set);
  179. tt_ptr_op(set->countries, OP_NE, NULL);
  180. tt_int_op(set->n_countries, OP_EQ, 2);
  181. tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
  182. tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
  183. tt_int_op(CALLED(geoip_get_country), OP_EQ, 1);
  184. tt_int_op((unsigned int)(*set->countries), OP_NE, 0);
  185. done:
  186. NS_UNMOCK(geoip_is_loaded);
  187. NS_UNMOCK(geoip_get_n_countries);
  188. NS_UNMOCK(geoip_get_country);
  189. routerset_free(set);
  190. }
  191. static int
  192. NS(geoip_is_loaded)(sa_family_t family)
  193. {
  194. (void)family;
  195. CALLED(geoip_is_loaded)++;
  196. return 1;
  197. }
  198. static int
  199. NS(geoip_get_n_countries)(void)
  200. {
  201. CALLED(geoip_get_n_countries)++;
  202. return 2;
  203. }
  204. static country_t
  205. NS(geoip_get_country)(const char *countrycode)
  206. {
  207. (void)countrycode;
  208. CALLED(geoip_get_country)++;
  209. return 1;
  210. }
  211. #undef NS_SUBMODULE
  212. #define NS_SUBMODULE ASPECT(routerset_refresh_counties, one_invalid_country)
  213. /*
  214. * Structural test for routerset_refresh_counties, with one invalid
  215. * country code..
  216. */
  217. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  218. NS_DECL(int, geoip_get_n_countries, (void));
  219. NS_DECL(country_t, geoip_get_country, (const char *country));
  220. static void
  221. NS(test_main)(void *arg)
  222. {
  223. routerset_t *set = routerset_new();
  224. (void)arg;
  225. NS_MOCK(geoip_is_loaded);
  226. NS_MOCK(geoip_get_n_countries);
  227. NS_MOCK(geoip_get_country);
  228. smartlist_add(set->country_names, tor_strndup("foo", 3));
  229. routerset_refresh_countries(set);
  230. tt_ptr_op(set->countries, OP_NE, NULL);
  231. tt_int_op(set->n_countries, OP_EQ, 2);
  232. tt_int_op(CALLED(geoip_is_loaded), OP_EQ, 1);
  233. tt_int_op(CALLED(geoip_get_n_countries), OP_EQ, 1);
  234. tt_int_op(CALLED(geoip_get_country), OP_EQ, 1);
  235. tt_int_op((unsigned int)(*set->countries), OP_EQ, 0);
  236. done:
  237. NS_UNMOCK(geoip_is_loaded);
  238. NS_UNMOCK(geoip_get_n_countries);
  239. NS_UNMOCK(geoip_get_country);
  240. routerset_free(set);
  241. }
  242. static int
  243. NS(geoip_is_loaded)(sa_family_t family)
  244. {
  245. (void)family;
  246. CALLED(geoip_is_loaded)++;
  247. return 1;
  248. }
  249. static int
  250. NS(geoip_get_n_countries)(void)
  251. {
  252. CALLED(geoip_get_n_countries)++;
  253. return 2;
  254. }
  255. static country_t
  256. NS(geoip_get_country)(const char *countrycode)
  257. {
  258. (void)countrycode;
  259. CALLED(geoip_get_country)++;
  260. return -1;
  261. }
  262. #undef NS_SUBMODULE
  263. #define NS_SUBMODULE ASPECT(routerset_parse, malformed)
  264. /*
  265. * Functional test, with a malformed string to parse.
  266. */
  267. static void
  268. NS(test_main)(void *arg)
  269. {
  270. routerset_t *set = routerset_new();
  271. const char *s = "_";
  272. int r;
  273. (void)arg;
  274. r = routerset_parse(set, s, "");
  275. tt_int_op(r, OP_EQ, -1);
  276. done:
  277. routerset_free(set);
  278. }
  279. #undef NS_SUBMODULE
  280. #define NS_SUBMODULE ASPECT(routerset_parse, valid_hexdigest)
  281. /*
  282. * Functional test for routerset_parse, that routerset_parse returns 0
  283. * on a valid hexdigest entry.
  284. */
  285. static void
  286. NS(test_main)(void *arg)
  287. {
  288. routerset_t *set;
  289. const char *s;
  290. int r;
  291. (void)arg;
  292. set = routerset_new();
  293. s = "$0000000000000000000000000000000000000000";
  294. r = routerset_parse(set, s, "");
  295. tt_int_op(r, OP_EQ, 0);
  296. tt_int_op(digestmap_isempty(set->digests), OP_NE, 1);
  297. done:
  298. routerset_free(set);
  299. }
  300. #undef NS_SUBMODULE
  301. #define NS_SUBMODULE ASPECT(routerset_parse, valid_nickname)
  302. /*
  303. * Functional test for routerset_parse, when given a valid nickname as input.
  304. */
  305. static void
  306. NS(test_main)(void *arg)
  307. {
  308. routerset_t *set;
  309. const char *s;
  310. int r;
  311. (void)arg;
  312. set = routerset_new();
  313. s = "fred";
  314. r = routerset_parse(set, s, "");
  315. tt_int_op(r, OP_EQ, 0);
  316. tt_int_op(strmap_isempty(set->names), OP_NE, 1);
  317. done:
  318. routerset_free(set);
  319. }
  320. #undef NS_SUBMODULE
  321. #define NS_SUBMODULE ASPECT(routerset_parse, get_countryname)
  322. /*
  323. * Functional test for routerset_parse, when given a valid countryname.
  324. */
  325. static void
  326. NS(test_main)(void *arg)
  327. {
  328. routerset_t *set;
  329. const char *s;
  330. int r;
  331. (void)arg;
  332. set = routerset_new();
  333. s = "{cc}";
  334. r = routerset_parse(set, s, "");
  335. tt_int_op(r, OP_EQ, 0);
  336. tt_int_op(smartlist_len(set->country_names), OP_NE, 0);
  337. done:
  338. routerset_free(set);
  339. }
  340. #undef NS_SUBMODULE
  341. #define NS_SUBMODULE ASPECT(routerset_parse, policy_wildcard)
  342. /*
  343. * Structural test for routerset_parse, when given a valid wildcard policy.
  344. */
  345. NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
  346. (const char *s, int assume_action, int *malformed_list));
  347. static addr_policy_t *NS(mock_addr_policy);
  348. static void
  349. NS(test_main)(void *arg)
  350. {
  351. routerset_t *set;
  352. const char *s;
  353. int r;
  354. (void)arg;
  355. NS_MOCK(router_parse_addr_policy_item_from_string);
  356. NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
  357. set = routerset_new();
  358. s = "*";
  359. r = routerset_parse(set, s, "");
  360. tt_int_op(r, OP_EQ, 0);
  361. tt_int_op(smartlist_len(set->policies), OP_NE, 0);
  362. tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
  363. done:
  364. routerset_free(set);
  365. }
  366. addr_policy_t *
  367. NS(router_parse_addr_policy_item_from_string)(const char *s,
  368. int assume_action,
  369. int *malformed_list)
  370. {
  371. (void)s;
  372. (void)assume_action;
  373. (void)malformed_list;
  374. CALLED(router_parse_addr_policy_item_from_string)++;
  375. return NS(mock_addr_policy);
  376. }
  377. #undef NS_SUBMODULE
  378. #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv4)
  379. /*
  380. * Structural test for routerset_parse, when given a valid IPv4 address
  381. * literal policy.
  382. */
  383. NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
  384. (const char *s, int assume_action, int *bogus));
  385. static addr_policy_t *NS(mock_addr_policy);
  386. static void
  387. NS(test_main)(void *arg)
  388. {
  389. routerset_t *set;
  390. const char *s;
  391. int r;
  392. (void)arg;
  393. NS_MOCK(router_parse_addr_policy_item_from_string);
  394. NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
  395. set = routerset_new();
  396. s = "127.0.0.1";
  397. r = routerset_parse(set, s, "");
  398. tt_int_op(r, OP_EQ, 0);
  399. tt_int_op(smartlist_len(set->policies), OP_NE, 0);
  400. tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
  401. done:
  402. routerset_free(set);
  403. }
  404. addr_policy_t *
  405. NS(router_parse_addr_policy_item_from_string)(const char *s, int assume_action,
  406. int *bogus)
  407. {
  408. (void)s;
  409. (void)assume_action;
  410. CALLED(router_parse_addr_policy_item_from_string)++;
  411. *bogus = 0;
  412. return NS(mock_addr_policy);
  413. }
  414. #undef NS_SUBMODULE
  415. #define NS_SUBMODULE ASPECT(routerset_parse, policy_ipv6)
  416. /*
  417. * Structural test for routerset_parse, when given a valid IPv6 address
  418. * literal policy.
  419. */
  420. NS_DECL(addr_policy_t *, router_parse_addr_policy_item_from_string,
  421. (const char *s, int assume_action, int *bad));
  422. static addr_policy_t *NS(mock_addr_policy);
  423. static void
  424. NS(test_main)(void *arg)
  425. {
  426. routerset_t *set;
  427. const char *s;
  428. int r;
  429. (void)arg;
  430. NS_MOCK(router_parse_addr_policy_item_from_string);
  431. NS(mock_addr_policy) = tor_malloc_zero(sizeof(addr_policy_t));
  432. set = routerset_new();
  433. s = "::1";
  434. r = routerset_parse(set, s, "");
  435. tt_int_op(r, OP_EQ, 0);
  436. tt_int_op(smartlist_len(set->policies), OP_NE, 0);
  437. tt_int_op(CALLED(router_parse_addr_policy_item_from_string), OP_EQ, 1);
  438. done:
  439. routerset_free(set);
  440. }
  441. addr_policy_t *
  442. NS(router_parse_addr_policy_item_from_string)(const char *s,
  443. int assume_action, int *bad)
  444. {
  445. (void)s;
  446. (void)assume_action;
  447. CALLED(router_parse_addr_policy_item_from_string)++;
  448. *bad = 0;
  449. return NS(mock_addr_policy);
  450. }
  451. #undef NS_SUBMODULE
  452. #define NS_SUBMODULE ASPECT(routerset_union, source_bad)
  453. /*
  454. * Structural test for routerset_union, when given a bad source argument.
  455. */
  456. NS_DECL(smartlist_t *, smartlist_new, (void));
  457. static void
  458. NS(test_main)(void *arg)
  459. {
  460. routerset_t *set, *bad_set;
  461. (void)arg;
  462. set = routerset_new();
  463. bad_set = routerset_new();
  464. smartlist_free(bad_set->list);
  465. bad_set->list = NULL;
  466. NS_MOCK(smartlist_new);
  467. routerset_union(set, NULL);
  468. tt_int_op(CALLED(smartlist_new), OP_EQ, 0);
  469. routerset_union(set, bad_set);
  470. tt_int_op(CALLED(smartlist_new), OP_EQ, 0);
  471. done:
  472. NS_UNMOCK(smartlist_new);
  473. routerset_free(set);
  474. /* Just recreate list, so we can simply use routerset_free. */
  475. bad_set->list = smartlist_new();
  476. routerset_free(bad_set);
  477. }
  478. static smartlist_t *
  479. NS(smartlist_new)(void)
  480. {
  481. CALLED(smartlist_new)++;
  482. return NULL;
  483. }
  484. #undef NS_SUBMODULE
  485. #define NS_SUBMODULE ASPECT(routerset_union, one)
  486. /*
  487. * Functional test for routerset_union.
  488. */
  489. static void
  490. NS(test_main)(void *arg)
  491. {
  492. routerset_t *src = routerset_new();
  493. routerset_t *tgt;
  494. (void)arg;
  495. tgt = routerset_new();
  496. smartlist_add_strdup(src->list, "{xx}");
  497. routerset_union(tgt, src);
  498. tt_int_op(smartlist_len(tgt->list), OP_NE, 0);
  499. done:
  500. routerset_free(src);
  501. routerset_free(tgt);
  502. }
  503. #undef NS_SUBMODULE
  504. #define NS_SUBMODULE routerset_is_list
  505. /*
  506. * Functional tests for routerset_is_list.
  507. */
  508. static void
  509. NS(test_main)(void *arg)
  510. {
  511. routerset_t *set;
  512. addr_policy_t *policy;
  513. int is_list;
  514. (void)arg;
  515. /* len(set->country_names) == 0, len(set->policies) == 0 */
  516. set = routerset_new();
  517. is_list = routerset_is_list(set);
  518. routerset_free(set);
  519. set = NULL;
  520. tt_int_op(is_list, OP_NE, 0);
  521. /* len(set->country_names) != 0, len(set->policies) == 0 */
  522. set = routerset_new();
  523. smartlist_add(set->country_names, tor_strndup("foo", 3));
  524. is_list = routerset_is_list(set);
  525. routerset_free(set);
  526. set = NULL;
  527. tt_int_op(is_list, OP_EQ, 0);
  528. /* len(set->country_names) == 0, len(set->policies) != 0 */
  529. set = routerset_new();
  530. policy = tor_malloc_zero(sizeof(addr_policy_t));
  531. smartlist_add(set->policies, (void *)policy);
  532. is_list = routerset_is_list(set);
  533. routerset_free(set);
  534. set = NULL;
  535. tt_int_op(is_list, OP_EQ, 0);
  536. /* len(set->country_names) != 0, len(set->policies) != 0 */
  537. set = routerset_new();
  538. smartlist_add(set->country_names, tor_strndup("foo", 3));
  539. policy = tor_malloc_zero(sizeof(addr_policy_t));
  540. smartlist_add(set->policies, (void *)policy);
  541. is_list = routerset_is_list(set);
  542. routerset_free(set);
  543. set = NULL;
  544. tt_int_op(is_list, OP_EQ, 0);
  545. done:
  546. ;
  547. }
  548. #undef NS_SUBMODULE
  549. #define NS_SUBMODULE routerset_needs_geoip
  550. /*
  551. * Functional tests for routerset_needs_geoip.
  552. */
  553. static void
  554. NS(test_main)(void *arg)
  555. {
  556. routerset_t *set;
  557. int needs_geoip;
  558. (void)arg;
  559. set = NULL;
  560. needs_geoip = routerset_needs_geoip(set);
  561. tt_int_op(needs_geoip, OP_EQ, 0);
  562. set = routerset_new();
  563. needs_geoip = routerset_needs_geoip(set);
  564. routerset_free(set);
  565. tt_int_op(needs_geoip, OP_EQ, 0);
  566. set = NULL;
  567. set = routerset_new();
  568. smartlist_add(set->country_names, tor_strndup("xx", 2));
  569. needs_geoip = routerset_needs_geoip(set);
  570. routerset_free(set);
  571. set = NULL;
  572. tt_int_op(needs_geoip, OP_NE, 0);
  573. done:
  574. ;
  575. }
  576. #undef NS_SUBMODULE
  577. #define NS_SUBMODULE routerset_is_empty
  578. /*
  579. * Functional tests for routerset_is_empty.
  580. */
  581. static void
  582. NS(test_main)(void *arg)
  583. {
  584. routerset_t *set = NULL;
  585. int is_empty;
  586. (void)arg;
  587. is_empty = routerset_is_empty(set);
  588. tt_int_op(is_empty, OP_NE, 0);
  589. set = routerset_new();
  590. is_empty = routerset_is_empty(set);
  591. routerset_free(set);
  592. set = NULL;
  593. tt_int_op(is_empty, OP_NE, 0);
  594. set = routerset_new();
  595. smartlist_add_strdup(set->list, "{xx}");
  596. is_empty = routerset_is_empty(set);
  597. routerset_free(set);
  598. set = NULL;
  599. tt_int_op(is_empty, OP_EQ, 0);
  600. done:
  601. ;
  602. }
  603. #undef NS_SUBMODULE
  604. #define NS_SUBMODULE ASPECT(routerset_contains, null_set_or_null_set_list)
  605. /*
  606. * Functional test for routerset_contains, when given a NULL set or the
  607. * set has a NULL list.
  608. */
  609. static void
  610. NS(test_main)(void *arg)
  611. {
  612. routerset_t *set = NULL;
  613. int contains;
  614. (void)arg;
  615. contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
  616. tt_int_op(contains, OP_EQ, 0);
  617. set = tor_malloc_zero(sizeof(routerset_t));
  618. set->list = NULL;
  619. contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
  620. tor_free(set);
  621. tt_int_op(contains, OP_EQ, 0);
  622. done:
  623. ;
  624. }
  625. #undef NS_SUBMODULE
  626. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_nickname)
  627. /*
  628. * Functional test for routerset_contains, when given a valid routerset but a
  629. * NULL nickname.
  630. */
  631. static void
  632. NS(test_main)(void *arg)
  633. {
  634. routerset_t *set = routerset_new();
  635. char *nickname = NULL;
  636. int contains;
  637. (void)arg;
  638. contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
  639. routerset_free(set);
  640. tt_int_op(contains, OP_EQ, 0);
  641. done:
  642. ;
  643. }
  644. #undef NS_SUBMODULE
  645. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_nickname)
  646. /*
  647. * Functional test for routerset_contains, when given a valid routerset
  648. * and the nickname is in the routerset.
  649. */
  650. static void
  651. NS(test_main)(void *arg)
  652. {
  653. routerset_t *set = routerset_new();
  654. const char *nickname;
  655. int contains;
  656. (void)arg;
  657. nickname = "Foo"; /* This tests the lowercase comparison as well. */
  658. strmap_set_lc(set->names, nickname, (void *)1);
  659. contains = routerset_contains(set, NULL, 0, nickname, NULL, 0);
  660. routerset_free(set);
  661. tt_int_op(contains, OP_EQ, 4);
  662. done:
  663. ;
  664. }
  665. #undef NS_SUBMODULE
  666. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_nickname)
  667. /*
  668. * Functional test for routerset_contains, when given a valid routerset
  669. * and the nickname is not in the routerset.
  670. */
  671. static void
  672. NS(test_main)(void *arg)
  673. {
  674. routerset_t *set = routerset_new();
  675. int contains;
  676. (void)arg;
  677. strmap_set_lc(set->names, "bar", (void *)1);
  678. contains = routerset_contains(set, NULL, 0, "foo", NULL, 0);
  679. routerset_free(set);
  680. tt_int_op(contains, OP_EQ, 0);
  681. done:
  682. ;
  683. }
  684. #undef NS_SUBMODULE
  685. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_digest)
  686. /*
  687. * Functional test for routerset_contains, when given a valid routerset
  688. * and the digest is contained in the routerset.
  689. */
  690. static void
  691. NS(test_main)(void *arg)
  692. {
  693. routerset_t *set = routerset_new();
  694. int contains;
  695. uint8_t foo[20] = { 2, 3, 4 };
  696. (void)arg;
  697. digestmap_set(set->digests, (const char*)foo, (void *)1);
  698. contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
  699. routerset_free(set);
  700. tt_int_op(contains, OP_EQ, 4);
  701. done:
  702. ;
  703. }
  704. #undef NS_SUBMODULE
  705. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_digest)
  706. /*
  707. * Functional test for routerset_contains, when given a valid routerset
  708. * and the digest is not contained in the routerset.
  709. */
  710. static void
  711. NS(test_main)(void *arg)
  712. {
  713. routerset_t *set = routerset_new();
  714. int contains;
  715. uint8_t bar[20] = { 9, 10, 11, 55 };
  716. uint8_t foo[20] = { 1, 2, 3, 4};
  717. (void)arg;
  718. digestmap_set(set->digests, (const char*)bar, (void *)1);
  719. contains = routerset_contains(set, NULL, 0, NULL, (const char*)foo, 0);
  720. routerset_free(set);
  721. tt_int_op(contains, OP_EQ, 0);
  722. done:
  723. ;
  724. }
  725. #undef NS_SUBMODULE
  726. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_digest)
  727. /*
  728. * Functional test for routerset_contains, when given a valid routerset
  729. * and the digest is NULL.
  730. */
  731. static void
  732. NS(test_main)(void *arg)
  733. {
  734. routerset_t *set = routerset_new();
  735. int contains;
  736. uint8_t bar[20] = { 9, 10, 11, 55 };
  737. (void)arg;
  738. digestmap_set(set->digests, (const char*)bar, (void *)1);
  739. contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
  740. routerset_free(set);
  741. tt_int_op(contains, OP_EQ, 0);
  742. done:
  743. ;
  744. }
  745. #undef NS_SUBMODULE
  746. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_addr)
  747. /*
  748. * Structural test for routerset_contains, when given a valid routerset
  749. * and the address is rejected by policy.
  750. */
  751. NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
  752. (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
  753. static tor_addr_t MOCK_TOR_ADDR;
  754. #define MOCK_TOR_ADDR_PTR (&MOCK_TOR_ADDR)
  755. static void
  756. NS(test_main)(void *arg)
  757. {
  758. routerset_t *set = routerset_new();
  759. tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
  760. int contains;
  761. (void)arg;
  762. NS_MOCK(compare_tor_addr_to_addr_policy);
  763. contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
  764. routerset_free(set);
  765. tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
  766. tt_int_op(contains, OP_EQ, 3);
  767. done:
  768. ;
  769. }
  770. addr_policy_result_t
  771. NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
  772. const smartlist_t *policy)
  773. {
  774. (void)port;
  775. (void)policy;
  776. CALLED(compare_tor_addr_to_addr_policy)++;
  777. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  778. return ADDR_POLICY_REJECTED;
  779. done:
  780. return 0;
  781. }
  782. #undef NS_SUBMODULE
  783. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_no_addr)
  784. /*
  785. * Structural test for routerset_contains, when given a valid routerset
  786. * and the address is not rejected by policy.
  787. */
  788. NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
  789. (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
  790. static void
  791. NS(test_main)(void *arg)
  792. {
  793. routerset_t *set = routerset_new();
  794. tor_addr_t *addr = MOCK_TOR_ADDR_PTR;
  795. int contains;
  796. (void)arg;
  797. NS_MOCK(compare_tor_addr_to_addr_policy);
  798. contains = routerset_contains(set, addr, 0, NULL, NULL, 0);
  799. routerset_free(set);
  800. tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
  801. tt_int_op(contains, OP_EQ, 0);
  802. done:
  803. ;
  804. }
  805. addr_policy_result_t
  806. NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
  807. const smartlist_t *policy)
  808. {
  809. (void)port;
  810. (void)policy;
  811. CALLED(compare_tor_addr_to_addr_policy)++;
  812. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  813. return ADDR_POLICY_ACCEPTED;
  814. done:
  815. return 0;
  816. }
  817. #undef NS_SUBMODULE
  818. #define NS_SUBMODULE ASPECT(routerset_contains, set_and_null_addr)
  819. /*
  820. * Structural test for routerset_contains, when given a valid routerset
  821. * and the address is NULL.
  822. */
  823. NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
  824. (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
  825. static void
  826. NS(test_main)(void *arg)
  827. {
  828. routerset_t *set = routerset_new();
  829. int contains;
  830. (void)arg;
  831. NS_MOCK(compare_tor_addr_to_addr_policy);
  832. contains = routerset_contains(set, NULL, 0, NULL, NULL, 0);
  833. routerset_free(set);
  834. tt_int_op(contains, OP_EQ, 0);
  835. done:
  836. ;
  837. }
  838. addr_policy_result_t
  839. NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
  840. const smartlist_t *policy)
  841. {
  842. (void)port;
  843. (void)policy;
  844. CALLED(compare_tor_addr_to_addr_policy)++;
  845. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  846. return ADDR_POLICY_ACCEPTED;
  847. done:
  848. return 0;
  849. }
  850. #undef NS_SUBMODULE
  851. #define NS_SUBMODULE ASPECT(routerset_contains, countries_no_geoip)
  852. /*
  853. * Structural test for routerset_contains, when there is no matching country
  854. * for the address.
  855. */
  856. NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
  857. (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
  858. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  859. static void
  860. NS(test_main)(void *arg)
  861. {
  862. routerset_t *set = routerset_new();
  863. int contains = 1;
  864. (void)arg;
  865. NS_MOCK(compare_tor_addr_to_addr_policy);
  866. NS_MOCK(geoip_get_country_by_addr);
  867. set->countries = bitarray_init_zero(1);
  868. bitarray_set(set->countries, 1);
  869. contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
  870. routerset_free(set);
  871. tt_int_op(contains, OP_EQ, 0);
  872. tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
  873. tt_int_op(CALLED(geoip_get_country_by_addr), OP_EQ, 1);
  874. done:
  875. ;
  876. }
  877. addr_policy_result_t
  878. NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
  879. const smartlist_t *policy)
  880. {
  881. (void)port;
  882. (void)policy;
  883. CALLED(compare_tor_addr_to_addr_policy)++;
  884. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  885. done:
  886. return ADDR_POLICY_ACCEPTED;
  887. }
  888. int
  889. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  890. {
  891. CALLED(geoip_get_country_by_addr)++;
  892. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  893. done:
  894. return -1;
  895. }
  896. #undef NS_SUBMODULE
  897. #define NS_SUBMODULE ASPECT(routerset_contains, countries_geoip)
  898. /*
  899. * Structural test for routerset_contains, when there a matching country
  900. * for the address.
  901. */
  902. NS_DECL(addr_policy_result_t, compare_tor_addr_to_addr_policy,
  903. (const tor_addr_t *addr, uint16_t port, const smartlist_t *policy));
  904. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  905. static void
  906. NS(test_main)(void *arg)
  907. {
  908. routerset_t *set = routerset_new();
  909. int contains = 1;
  910. (void)arg;
  911. NS_MOCK(compare_tor_addr_to_addr_policy);
  912. NS_MOCK(geoip_get_country_by_addr);
  913. set->n_countries = 2;
  914. set->countries = bitarray_init_zero(1);
  915. bitarray_set(set->countries, 1);
  916. contains = routerset_contains(set, MOCK_TOR_ADDR_PTR, 0, NULL, NULL, -1);
  917. routerset_free(set);
  918. tt_int_op(contains, OP_EQ, 2);
  919. tt_int_op(CALLED(compare_tor_addr_to_addr_policy), OP_EQ, 1);
  920. tt_int_op(CALLED(geoip_get_country_by_addr), OP_EQ, 1);
  921. done:
  922. ;
  923. }
  924. addr_policy_result_t
  925. NS(compare_tor_addr_to_addr_policy)(const tor_addr_t *addr, uint16_t port,
  926. const smartlist_t *policy)
  927. {
  928. (void)port;
  929. (void)policy;
  930. CALLED(compare_tor_addr_to_addr_policy)++;
  931. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  932. done:
  933. return ADDR_POLICY_ACCEPTED;
  934. }
  935. int
  936. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  937. {
  938. CALLED(geoip_get_country_by_addr)++;
  939. tt_ptr_op(addr, OP_EQ, MOCK_TOR_ADDR_PTR);
  940. done:
  941. return 1;
  942. }
  943. #undef NS_SUBMODULE
  944. #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs)
  945. /*
  946. * Functional test for routerset_add_unknown_ccs, where only_if_some_cc_set
  947. * is set and there are no country names.
  948. */
  949. static void
  950. NS(test_main)(void *arg)
  951. {
  952. routerset_t *set = routerset_new();
  953. routerset_t **setp = &set;
  954. int r;
  955. (void)arg;
  956. r = routerset_add_unknown_ccs(setp, 1);
  957. tt_int_op(r, OP_EQ, 0);
  958. done:
  959. routerset_free(set);
  960. }
  961. #undef NS_SUBMODULE
  962. #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, creates_set)
  963. /*
  964. * Functional test for routerset_add_unknown_ccs, where the set argument
  965. * is created if passed in as NULL.
  966. */
  967. /* The mock is only used to stop the test from asserting erroneously. */
  968. NS_DECL(country_t, geoip_get_country, (const char *country));
  969. static void
  970. NS(test_main)(void *arg)
  971. {
  972. routerset_t *set = NULL;
  973. routerset_t **setp = &set;
  974. int r;
  975. (void)arg;
  976. NS_MOCK(geoip_get_country);
  977. r = routerset_add_unknown_ccs(setp, 0);
  978. tt_ptr_op(*setp, OP_NE, NULL);
  979. tt_int_op(r, OP_EQ, 0);
  980. done:
  981. if (set != NULL)
  982. routerset_free(set);
  983. }
  984. country_t
  985. NS(geoip_get_country)(const char *country)
  986. {
  987. (void)country;
  988. CALLED(geoip_get_country)++;
  989. return -1;
  990. }
  991. #undef NS_SUBMODULE
  992. #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_unknown)
  993. /*
  994. * Structural test for routerset_add_unknown_ccs, that the "{??}"
  995. * country code is added to the list.
  996. */
  997. NS_DECL(country_t, geoip_get_country, (const char *country));
  998. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  999. static void
  1000. NS(test_main)(void *arg)
  1001. {
  1002. routerset_t *set = routerset_new();
  1003. routerset_t **setp = &set;
  1004. int r;
  1005. (void)arg;
  1006. NS_MOCK(geoip_get_country);
  1007. NS_MOCK(geoip_is_loaded);
  1008. r = routerset_add_unknown_ccs(setp, 0);
  1009. tt_int_op(r, OP_EQ, 1);
  1010. tt_int_op(smartlist_contains_string(set->country_names, "??"), OP_EQ, 1);
  1011. tt_int_op(smartlist_contains_string(set->list, "{??}"), OP_EQ, 1);
  1012. done:
  1013. if (set != NULL)
  1014. routerset_free(set);
  1015. }
  1016. country_t
  1017. NS(geoip_get_country)(const char *country)
  1018. {
  1019. int arg_is_qq, arg_is_a1;
  1020. CALLED(geoip_get_country)++;
  1021. arg_is_qq = !strcmp(country, "??");
  1022. arg_is_a1 = !strcmp(country, "A1");
  1023. tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
  1024. if (arg_is_qq)
  1025. return 1;
  1026. done:
  1027. return -1;
  1028. }
  1029. int
  1030. NS(geoip_is_loaded)(sa_family_t family)
  1031. {
  1032. CALLED(geoip_is_loaded)++;
  1033. tt_int_op(family, OP_EQ, AF_INET);
  1034. done:
  1035. return 0;
  1036. }
  1037. #undef NS_SUBMODULE
  1038. #define NS_SUBMODULE ASPECT(routerset_add_unknown_ccs, add_a1)
  1039. /*
  1040. * Structural test for routerset_add_unknown_ccs, that the "{a1}"
  1041. * country code is added to the list.
  1042. */
  1043. NS_DECL(country_t, geoip_get_country, (const char *country));
  1044. NS_DECL(int, geoip_is_loaded, (sa_family_t family));
  1045. static void
  1046. NS(test_main)(void *arg)
  1047. {
  1048. routerset_t *set = routerset_new();
  1049. routerset_t **setp = &set;
  1050. int r;
  1051. (void)arg;
  1052. NS_MOCK(geoip_get_country);
  1053. NS_MOCK(geoip_is_loaded);
  1054. r = routerset_add_unknown_ccs(setp, 0);
  1055. tt_int_op(r, OP_EQ, 1);
  1056. tt_int_op(smartlist_contains_string(set->country_names, "a1"), OP_EQ, 1);
  1057. tt_int_op(smartlist_contains_string(set->list, "{a1}"), OP_EQ, 1);
  1058. done:
  1059. if (set != NULL)
  1060. routerset_free(set);
  1061. }
  1062. country_t
  1063. NS(geoip_get_country)(const char *country)
  1064. {
  1065. int arg_is_qq, arg_is_a1;
  1066. CALLED(geoip_get_country)++;
  1067. arg_is_qq = !strcmp(country, "??");
  1068. arg_is_a1 = !strcmp(country, "A1");
  1069. tt_int_op(arg_is_qq || arg_is_a1, OP_EQ, 1);
  1070. if (arg_is_a1)
  1071. return 1;
  1072. done:
  1073. return -1;
  1074. }
  1075. int
  1076. NS(geoip_is_loaded)(sa_family_t family)
  1077. {
  1078. CALLED(geoip_is_loaded)++;
  1079. tt_int_op(family, OP_EQ, AF_INET);
  1080. done:
  1081. return 0;
  1082. }
  1083. #undef NS_SUBMODULE
  1084. #define NS_SUBMODULE routerset_contains_extendinfo
  1085. /*
  1086. * Functional test for routerset_contains_extendinfo.
  1087. */
  1088. static void
  1089. NS(test_main)(void *arg)
  1090. {
  1091. routerset_t *set = routerset_new();
  1092. extend_info_t ei;
  1093. int r;
  1094. const char *nickname = "foo";
  1095. (void)arg;
  1096. memset(&ei, 0, sizeof(ei));
  1097. strmap_set_lc(set->names, nickname, (void *)1);
  1098. strncpy(ei.nickname, nickname, sizeof(ei.nickname) - 1);
  1099. ei.nickname[sizeof(ei.nickname) - 1] = '\0';
  1100. r = routerset_contains_extendinfo(set, &ei);
  1101. tt_int_op(r, OP_EQ, 4);
  1102. done:
  1103. routerset_free(set);
  1104. }
  1105. #undef NS_SUBMODULE
  1106. #define NS_SUBMODULE routerset_contains_router
  1107. /*
  1108. * Functional test for routerset_contains_router.
  1109. */
  1110. static void
  1111. NS(test_main)(void *arg)
  1112. {
  1113. routerset_t *set = routerset_new();
  1114. routerinfo_t ri;
  1115. country_t country = 1;
  1116. int r;
  1117. const char *nickname = "foo";
  1118. (void)arg;
  1119. memset(&ri, 0, sizeof(ri));
  1120. strmap_set_lc(set->names, nickname, (void *)1);
  1121. ri.nickname = (char *)nickname;
  1122. r = routerset_contains_router(set, &ri, country);
  1123. tt_int_op(r, OP_EQ, 4);
  1124. done:
  1125. routerset_free(set);
  1126. }
  1127. #undef NS_SUBMODULE
  1128. #define NS_SUBMODULE routerset_contains_routerstatus
  1129. /*
  1130. * Functional test for routerset_contains_routerstatus.
  1131. */
  1132. // XXX: This is a bit brief. It only populates and tests the nickname fields
  1133. // ie., enough to make the containment check succeed. Perhaps it should do
  1134. // a bit more or test a bit more.
  1135. static void
  1136. NS(test_main)(void *arg)
  1137. {
  1138. routerset_t *set = routerset_new();
  1139. routerstatus_t rs;
  1140. country_t country = 1;
  1141. int r;
  1142. const char *nickname = "foo";
  1143. (void)arg;
  1144. memset(&rs, 0, sizeof(rs));
  1145. strmap_set_lc(set->names, nickname, (void *)1);
  1146. strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
  1147. rs.nickname[sizeof(rs.nickname) - 1] = '\0';
  1148. r = routerset_contains_routerstatus(set, &rs, country);
  1149. tt_int_op(r, OP_EQ, 4);
  1150. done:
  1151. routerset_free(set);
  1152. }
  1153. #undef NS_SUBMODULE
  1154. #define NS_SUBMODULE ASPECT(routerset_contains_node, none)
  1155. /*
  1156. * Functional test for routerset_contains_node, when the node has no
  1157. * routerset or routerinfo.
  1158. */
  1159. static node_t NS(mock_node);
  1160. static void
  1161. NS(test_main)(void *arg)
  1162. {
  1163. routerset_t *set = routerset_new();
  1164. int r;
  1165. (void)arg;
  1166. NS(mock_node).ri = NULL;
  1167. NS(mock_node).rs = NULL;
  1168. r = routerset_contains_node(set, &NS(mock_node));
  1169. tt_int_op(r, OP_EQ, 0);
  1170. done:
  1171. routerset_free(set);
  1172. }
  1173. #undef NS_SUBMODULE
  1174. #define NS_SUBMODULE ASPECT(routerset_contains_node, routerstatus)
  1175. /*
  1176. * Functional test for routerset_contains_node, when the node has a
  1177. * routerset and no routerinfo.
  1178. */
  1179. static node_t NS(mock_node);
  1180. static void
  1181. NS(test_main)(void *arg)
  1182. {
  1183. routerset_t *set = routerset_new();
  1184. int r;
  1185. const char *nickname = "foo";
  1186. routerstatus_t rs;
  1187. (void)arg;
  1188. strmap_set_lc(set->names, nickname, (void *)1);
  1189. strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
  1190. rs.nickname[sizeof(rs.nickname) - 1] = '\0';
  1191. NS(mock_node).ri = NULL;
  1192. NS(mock_node).rs = &rs;
  1193. r = routerset_contains_node(set, &NS(mock_node));
  1194. tt_int_op(r, OP_EQ, 4);
  1195. done:
  1196. routerset_free(set);
  1197. }
  1198. #undef NS_SUBMODULE
  1199. #define NS_SUBMODULE ASPECT(routerset_contains_node, routerinfo)
  1200. /*
  1201. * Functional test for routerset_contains_node, when the node has no
  1202. * routerset and a routerinfo.
  1203. */
  1204. static void
  1205. NS(test_main)(void *arg)
  1206. {
  1207. routerset_t *set = routerset_new();
  1208. int r;
  1209. const char *nickname = "foo";
  1210. routerinfo_t ri;
  1211. node_t mock_node;
  1212. (void)arg;
  1213. strmap_set_lc(set->names, nickname, (void *)1);
  1214. ri.nickname = (char *)nickname;
  1215. mock_node.ri = &ri;
  1216. mock_node.rs = NULL;
  1217. r = routerset_contains_node(set, &mock_node);
  1218. tt_int_op(r, OP_EQ, 4);
  1219. done:
  1220. routerset_free(set);
  1221. }
  1222. #undef NS_SUBMODULE
  1223. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, no_routerset)
  1224. /*
  1225. * Functional test for routerset_get_all_nodes, when routerset is NULL or
  1226. * the routerset list is NULL.
  1227. */
  1228. static void
  1229. NS(test_main)(void *arg)
  1230. {
  1231. smartlist_t *out = smartlist_new();
  1232. routerset_t *set = NULL;
  1233. (void)arg;
  1234. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1235. routerset_get_all_nodes(out, NULL, NULL, 0);
  1236. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1237. set = routerset_new();
  1238. smartlist_free(set->list);
  1239. routerset_get_all_nodes(out, NULL, NULL, 0);
  1240. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1241. /* Just recreate list, so we can simply use routerset_free. */
  1242. set->list = smartlist_new();
  1243. done:
  1244. routerset_free(set);
  1245. smartlist_free(out);
  1246. }
  1247. #undef NS_SUBMODULE
  1248. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_with_no_nodes)
  1249. /*
  1250. * Structural test for routerset_get_all_nodes, when the routerset list
  1251. * is empty.
  1252. */
  1253. NS_DECL(const node_t *, node_get_by_nickname,
  1254. (const char *nickname, unsigned flags));
  1255. static const char *NS(mock_nickname);
  1256. static void
  1257. NS(test_main)(void *arg)
  1258. {
  1259. smartlist_t *out = smartlist_new();
  1260. routerset_t *set = routerset_new();
  1261. int out_len;
  1262. (void)arg;
  1263. NS_MOCK(node_get_by_nickname);
  1264. NS(mock_nickname) = "foo";
  1265. smartlist_add_strdup(set->list, NS(mock_nickname));
  1266. routerset_get_all_nodes(out, set, NULL, 0);
  1267. out_len = smartlist_len(out);
  1268. smartlist_free(out);
  1269. routerset_free(set);
  1270. tt_int_op(out_len, OP_EQ, 0);
  1271. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1272. done:
  1273. ;
  1274. }
  1275. const node_t *
  1276. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1277. {
  1278. CALLED(node_get_by_nickname)++;
  1279. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1280. tt_uint_op(flags, OP_EQ, 0);
  1281. done:
  1282. return NULL;
  1283. }
  1284. #undef NS_SUBMODULE
  1285. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_flag_not_running)
  1286. /*
  1287. * Structural test for routerset_get_all_nodes, with the running_only flag
  1288. * is set but the nodes are not running.
  1289. */
  1290. NS_DECL(const node_t *, node_get_by_nickname,
  1291. (const char *nickname, unsigned flags));
  1292. static const char *NS(mock_nickname);
  1293. static node_t NS(mock_node);
  1294. static void
  1295. NS(test_main)(void *arg)
  1296. {
  1297. smartlist_t *out = smartlist_new();
  1298. routerset_t *set = routerset_new();
  1299. int out_len;
  1300. (void)arg;
  1301. NS_MOCK(node_get_by_nickname);
  1302. NS(mock_node).is_running = 0;
  1303. NS(mock_nickname) = "foo";
  1304. smartlist_add_strdup(set->list, NS(mock_nickname));
  1305. routerset_get_all_nodes(out, set, NULL, 1);
  1306. out_len = smartlist_len(out);
  1307. smartlist_free(out);
  1308. routerset_free(set);
  1309. tt_int_op(out_len, OP_EQ, 0);
  1310. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1311. done:
  1312. ;
  1313. }
  1314. const node_t *
  1315. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1316. {
  1317. CALLED(node_get_by_nickname)++;
  1318. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1319. tt_int_op(flags, OP_EQ, 0);
  1320. done:
  1321. return &NS(mock_node);
  1322. }
  1323. #undef NS_SUBMODULE
  1324. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list)
  1325. /*
  1326. * Structural test for routerset_get_all_nodes.
  1327. */
  1328. NS_DECL(const node_t *, node_get_by_nickname,
  1329. (const char *nickname, unsigned flags));
  1330. static char *NS(mock_nickname);
  1331. static node_t NS(mock_node);
  1332. static void
  1333. NS(test_main)(void *arg)
  1334. {
  1335. smartlist_t *out = smartlist_new();
  1336. routerset_t *set = routerset_new();
  1337. int out_len;
  1338. node_t *ent;
  1339. (void)arg;
  1340. NS_MOCK(node_get_by_nickname);
  1341. NS(mock_nickname) = tor_strdup("foo");
  1342. smartlist_add(set->list, NS(mock_nickname));
  1343. routerset_get_all_nodes(out, set, NULL, 0);
  1344. out_len = smartlist_len(out);
  1345. ent = (node_t *)smartlist_get(out, 0);
  1346. smartlist_free(out);
  1347. routerset_free(set);
  1348. tt_int_op(out_len, OP_EQ, 1);
  1349. tt_ptr_op(ent, OP_EQ, &NS(mock_node));
  1350. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1351. done:
  1352. ;
  1353. }
  1354. const node_t *
  1355. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1356. {
  1357. CALLED(node_get_by_nickname)++;
  1358. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1359. tt_int_op(flags, OP_EQ, 0);
  1360. done:
  1361. return &NS(mock_node);
  1362. }
  1363. #undef NS_SUBMODULE
  1364. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes)
  1365. /*
  1366. * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
  1367. */
  1368. NS_DECL(smartlist_t *, nodelist_get_list, (void));
  1369. static smartlist_t *NS(mock_smartlist);
  1370. static void
  1371. NS(test_main)(void *arg)
  1372. {
  1373. routerset_t *set = routerset_new();
  1374. smartlist_t *out = smartlist_new();
  1375. int r;
  1376. (void)arg;
  1377. NS_MOCK(nodelist_get_list);
  1378. smartlist_add_strdup(set->country_names, "{xx}");
  1379. NS(mock_smartlist) = smartlist_new();
  1380. routerset_get_all_nodes(out, set, NULL, 1);
  1381. r = smartlist_len(out);
  1382. routerset_free(set);
  1383. smartlist_free(out);
  1384. smartlist_free(NS(mock_smartlist));
  1385. tt_int_op(r, OP_EQ, 0);
  1386. tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
  1387. done:
  1388. ;
  1389. }
  1390. smartlist_t *
  1391. NS(nodelist_get_list)(void)
  1392. {
  1393. CALLED(nodelist_get_list)++;
  1394. return NS(mock_smartlist);
  1395. }
  1396. #undef NS_SUBMODULE
  1397. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_flag_not_running)
  1398. /*
  1399. * Structural test for routerset_get_all_nodes, with a non-list routerset
  1400. * the running_only flag is set, but the nodes are not running.
  1401. */
  1402. NS_DECL(smartlist_t *, nodelist_get_list, (void));
  1403. static smartlist_t *NS(mock_smartlist);
  1404. static node_t NS(mock_node);
  1405. static void
  1406. NS(test_main)(void *arg)
  1407. {
  1408. routerset_t *set = routerset_new();
  1409. smartlist_t *out = smartlist_new();
  1410. int r;
  1411. (void)arg;
  1412. NS_MOCK(nodelist_get_list);
  1413. smartlist_add_strdup(set->country_names, "{xx}");
  1414. NS(mock_smartlist) = smartlist_new();
  1415. NS(mock_node).is_running = 0;
  1416. smartlist_add(NS(mock_smartlist), (void *)&NS(mock_node));
  1417. routerset_get_all_nodes(out, set, NULL, 1);
  1418. r = smartlist_len(out);
  1419. routerset_free(set);
  1420. smartlist_free(out);
  1421. smartlist_free(NS(mock_smartlist));
  1422. tt_int_op(r, OP_EQ, 0);
  1423. tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
  1424. done:
  1425. ;
  1426. }
  1427. smartlist_t *
  1428. NS(nodelist_get_list)(void)
  1429. {
  1430. CALLED(nodelist_get_list)++;
  1431. return NS(mock_smartlist);
  1432. }
  1433. #undef NS_SUBMODULE
  1434. #define NS_SUBMODULE routerset_subtract_nodes
  1435. /*
  1436. * Functional test for routerset_subtract_nodes.
  1437. */
  1438. static void
  1439. NS(test_main)(void *arg)
  1440. {
  1441. routerset_t *set = routerset_new();
  1442. smartlist_t *list = smartlist_new();
  1443. const char *nickname = "foo";
  1444. routerinfo_t ri;
  1445. node_t mock_node;
  1446. (void)arg;
  1447. strmap_set_lc(set->names, nickname, (void *)1);
  1448. ri.nickname = (char *)nickname;
  1449. mock_node.rs = NULL;
  1450. mock_node.ri = &ri;
  1451. smartlist_add(list, (void *)&mock_node);
  1452. tt_int_op(smartlist_len(list), OP_NE, 0);
  1453. routerset_subtract_nodes(list, set);
  1454. tt_int_op(smartlist_len(list), OP_EQ, 0);
  1455. done:
  1456. routerset_free(set);
  1457. smartlist_free(list);
  1458. }
  1459. #undef NS_SUBMODULE
  1460. #define NS_SUBMODULE ASPECT(routerset_subtract_nodes, null_routerset)
  1461. /*
  1462. * Functional test for routerset_subtract_nodes, with a NULL routerset.
  1463. */
  1464. static void
  1465. NS(test_main)(void *arg)
  1466. {
  1467. routerset_t *set = NULL;
  1468. smartlist_t *list = smartlist_new();
  1469. const char *nickname = "foo";
  1470. routerinfo_t ri;
  1471. node_t mock_node;
  1472. (void)arg;
  1473. ri.nickname = (char *)nickname;
  1474. mock_node.ri = &ri;
  1475. smartlist_add(list, (void *)&mock_node);
  1476. tt_int_op(smartlist_len(list), OP_NE, 0);
  1477. routerset_subtract_nodes(list, set);
  1478. tt_int_op(smartlist_len(list), OP_NE, 0);
  1479. done:
  1480. routerset_free(set);
  1481. smartlist_free(list);
  1482. }
  1483. #undef NS_SUBMODULE
  1484. #define NS_SUBMODULE routerset_to_string
  1485. /*
  1486. * Functional test for routerset_to_string.
  1487. */
  1488. static void
  1489. NS(test_main)(void *arg)
  1490. {
  1491. routerset_t *set = NULL;
  1492. char *s = NULL;
  1493. (void)arg;
  1494. set = NULL;
  1495. s = routerset_to_string(set);
  1496. tt_str_op(s, OP_EQ, "");
  1497. tor_free(s);
  1498. set = routerset_new();
  1499. s = routerset_to_string(set);
  1500. tt_str_op(s, OP_EQ, "");
  1501. tor_free(s);
  1502. routerset_free(set); set = NULL;
  1503. set = routerset_new();
  1504. smartlist_add(set->list, tor_strndup("a", 1));
  1505. s = routerset_to_string(set);
  1506. tt_str_op(s, OP_EQ, "a");
  1507. tor_free(s);
  1508. routerset_free(set); set = NULL;
  1509. set = routerset_new();
  1510. smartlist_add(set->list, tor_strndup("a", 1));
  1511. smartlist_add(set->list, tor_strndup("b", 1));
  1512. s = routerset_to_string(set);
  1513. tt_str_op(s, OP_EQ, "a,b");
  1514. tor_free(s);
  1515. routerset_free(set); set = NULL;
  1516. done:
  1517. tor_free(s);
  1518. routerset_free(set);
  1519. }
  1520. #undef NS_SUBMODULE
  1521. #define NS_SUBMODULE ASPECT(routerset_equal, empty_empty)
  1522. /*
  1523. * Functional test for routerset_equal, with both routersets empty.
  1524. */
  1525. static void
  1526. NS(test_main)(void *arg)
  1527. {
  1528. routerset_t *a = routerset_new(), *b = routerset_new();
  1529. int r;
  1530. (void)arg;
  1531. r = routerset_equal(a, b);
  1532. routerset_free(a);
  1533. routerset_free(b);
  1534. tt_int_op(r, OP_EQ, 1);
  1535. done:
  1536. ;
  1537. }
  1538. #undef NS_SUBMODULE
  1539. #define NS_SUBMODULE ASPECT(routerset_equal, empty_not_empty)
  1540. /*
  1541. * Functional test for routerset_equal, with one routersets empty.
  1542. */
  1543. static void
  1544. NS(test_main)(void *arg)
  1545. {
  1546. routerset_t *a = routerset_new(), *b = routerset_new();
  1547. int r;
  1548. (void)arg;
  1549. smartlist_add_strdup(b->list, "{xx}");
  1550. r = routerset_equal(a, b);
  1551. routerset_free(a);
  1552. routerset_free(b);
  1553. tt_int_op(r, OP_EQ, 0);
  1554. done:
  1555. ;
  1556. }
  1557. #undef NS_SUBMODULE
  1558. #define NS_SUBMODULE ASPECT(routerset_equal, differing_lengths)
  1559. /*
  1560. * Functional test for routerset_equal, with the routersets having
  1561. * differing lengths.
  1562. */
  1563. static void
  1564. NS(test_main)(void *arg)
  1565. {
  1566. routerset_t *a = routerset_new(), *b = routerset_new();
  1567. int r;
  1568. (void)arg;
  1569. smartlist_add_strdup(a->list, "{aa}");
  1570. smartlist_add_strdup(b->list, "{b1}");
  1571. smartlist_add_strdup(b->list, "{b2}");
  1572. r = routerset_equal(a, b);
  1573. routerset_free(a);
  1574. routerset_free(b);
  1575. tt_int_op(r, OP_EQ, 0);
  1576. done:
  1577. ;
  1578. }
  1579. #undef NS_SUBMODULE
  1580. #define NS_SUBMODULE ASPECT(routerset_equal, unequal)
  1581. /*
  1582. * Functional test for routerset_equal, with the routersets being
  1583. * different.
  1584. */
  1585. static void
  1586. NS(test_main)(void *arg)
  1587. {
  1588. routerset_t *a = routerset_new(), *b = routerset_new();
  1589. int r;
  1590. (void)arg;
  1591. smartlist_add_strdup(a->list, "foo");
  1592. smartlist_add_strdup(b->list, "bar");
  1593. r = routerset_equal(a, b);
  1594. routerset_free(a);
  1595. routerset_free(b);
  1596. tt_int_op(r, OP_EQ, 0);
  1597. done:
  1598. ;
  1599. }
  1600. #undef NS_SUBMODULE
  1601. #define NS_SUBMODULE ASPECT(routerset_equal, equal)
  1602. /*
  1603. * Functional test for routerset_equal, with the routersets being
  1604. * equal.
  1605. */
  1606. static void
  1607. NS(test_main)(void *arg)
  1608. {
  1609. routerset_t *a = routerset_new(), *b = routerset_new();
  1610. int r;
  1611. (void)arg;
  1612. smartlist_add_strdup(a->list, "foo");
  1613. smartlist_add_strdup(b->list, "foo");
  1614. r = routerset_equal(a, b);
  1615. routerset_free(a);
  1616. routerset_free(b);
  1617. tt_int_op(r, OP_EQ, 1);
  1618. done:
  1619. ;
  1620. }
  1621. #undef NS_SUBMODULE
  1622. #define NS_SUBMODULE ASPECT(routerset_free, null_routerset)
  1623. /*
  1624. * Structural test for routerset_free, where the routerset is NULL.
  1625. */
  1626. NS_DECL(void, smartlist_free_, (smartlist_t *sl));
  1627. static void
  1628. NS(test_main)(void *arg)
  1629. {
  1630. (void)arg;
  1631. NS_MOCK(smartlist_free_);
  1632. routerset_free_(NULL);
  1633. tt_int_op(CALLED(smartlist_free_), OP_EQ, 0);
  1634. done:
  1635. ;
  1636. }
  1637. void
  1638. NS(smartlist_free_)(smartlist_t *s)
  1639. {
  1640. (void)s;
  1641. CALLED(smartlist_free_)++;
  1642. }
  1643. #undef NS_SUBMODULE
  1644. #define NS_SUBMODULE routerset_free
  1645. /*
  1646. * Structural test for routerset_free.
  1647. */
  1648. NS_DECL(void, smartlist_free_, (smartlist_t *sl));
  1649. NS_DECL(void, strmap_free_,(strmap_t *map, void (*free_val)(void*)));
  1650. NS_DECL(void, digestmap_free_, (digestmap_t *map, void (*free_val)(void*)));
  1651. static void
  1652. NS(test_main)(void *arg)
  1653. {
  1654. routerset_t *routerset = routerset_new();
  1655. (void)arg;
  1656. NS_MOCK(smartlist_free_);
  1657. NS_MOCK(strmap_free_);
  1658. NS_MOCK(digestmap_free_);
  1659. routerset_free(routerset);
  1660. tt_int_op(CALLED(smartlist_free_), OP_NE, 0);
  1661. tt_int_op(CALLED(strmap_free_), OP_NE, 0);
  1662. tt_int_op(CALLED(digestmap_free_), OP_NE, 0);
  1663. done:
  1664. ;
  1665. }
  1666. void
  1667. NS(smartlist_free_)(smartlist_t *s)
  1668. {
  1669. CALLED(smartlist_free_)++;
  1670. smartlist_free___real(s);
  1671. }
  1672. void
  1673. NS(strmap_free_)(strmap_t *map, void (*free_val)(void*))
  1674. {
  1675. CALLED(strmap_free_)++;
  1676. strmap_free___real(map, free_val);
  1677. }
  1678. void
  1679. NS(digestmap_free_)(digestmap_t *map, void (*free_val)(void*))
  1680. {
  1681. CALLED(digestmap_free_)++;
  1682. digestmap_free___real(map, free_val);
  1683. }
  1684. #undef NS_SUBMODULE
  1685. struct testcase_t routerset_tests[] = {
  1686. TEST_CASE(routerset_new),
  1687. TEST_CASE(routerset_get_countryname),
  1688. TEST_CASE(routerset_is_list),
  1689. TEST_CASE(routerset_needs_geoip),
  1690. TEST_CASE(routerset_is_empty),
  1691. TEST_CASE_ASPECT(routerset_contains, null_set_or_null_set_list),
  1692. TEST_CASE_ASPECT(routerset_contains, set_and_nickname),
  1693. TEST_CASE_ASPECT(routerset_contains, set_and_null_nickname),
  1694. TEST_CASE_ASPECT(routerset_contains, set_and_no_nickname),
  1695. TEST_CASE_ASPECT(routerset_contains, set_and_digest),
  1696. TEST_CASE_ASPECT(routerset_contains, set_and_no_digest),
  1697. TEST_CASE_ASPECT(routerset_contains, set_and_null_digest),
  1698. TEST_CASE_ASPECT(routerset_contains, set_and_addr),
  1699. TEST_CASE_ASPECT(routerset_contains, set_and_no_addr),
  1700. TEST_CASE_ASPECT(routerset_contains, set_and_null_addr),
  1701. TEST_CASE_ASPECT(routerset_contains, countries_no_geoip),
  1702. TEST_CASE_ASPECT(routerset_contains, countries_geoip),
  1703. TEST_CASE_ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs),
  1704. TEST_CASE_ASPECT(routerset_add_unknown_ccs, creates_set),
  1705. TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_unknown),
  1706. TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_a1),
  1707. TEST_CASE(routerset_contains_extendinfo),
  1708. TEST_CASE(routerset_contains_router),
  1709. TEST_CASE(routerset_contains_routerstatus),
  1710. TEST_CASE_ASPECT(routerset_contains_node, none),
  1711. TEST_CASE_ASPECT(routerset_contains_node, routerinfo),
  1712. TEST_CASE_ASPECT(routerset_contains_node, routerstatus),
  1713. TEST_CASE_ASPECT(routerset_get_all_nodes, no_routerset),
  1714. TEST_CASE_ASPECT(routerset_get_all_nodes, list_with_no_nodes),
  1715. TEST_CASE_ASPECT(routerset_get_all_nodes, list_flag_not_running),
  1716. TEST_CASE_ASPECT(routerset_get_all_nodes, list),
  1717. TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes),
  1718. TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_flag_not_running),
  1719. TEST_CASE_ASPECT(routerset_refresh_counties, geoip_not_loaded),
  1720. TEST_CASE_ASPECT(routerset_refresh_counties, no_countries),
  1721. TEST_CASE_ASPECT(routerset_refresh_counties, one_valid_country),
  1722. TEST_CASE_ASPECT(routerset_refresh_counties, one_invalid_country),
  1723. TEST_CASE_ASPECT(routerset_union, source_bad),
  1724. TEST_CASE_ASPECT(routerset_union, one),
  1725. TEST_CASE_ASPECT(routerset_parse, malformed),
  1726. TEST_CASE_ASPECT(routerset_parse, valid_hexdigest),
  1727. TEST_CASE_ASPECT(routerset_parse, valid_nickname),
  1728. TEST_CASE_ASPECT(routerset_parse, get_countryname),
  1729. TEST_CASE_ASPECT(routerset_parse, policy_wildcard),
  1730. TEST_CASE_ASPECT(routerset_parse, policy_ipv4),
  1731. TEST_CASE_ASPECT(routerset_parse, policy_ipv6),
  1732. TEST_CASE(routerset_subtract_nodes),
  1733. TEST_CASE_ASPECT(routerset_subtract_nodes, null_routerset),
  1734. TEST_CASE(routerset_to_string),
  1735. TEST_CASE_ASPECT(routerset_equal, empty_empty),
  1736. TEST_CASE_ASPECT(routerset_equal, empty_not_empty),
  1737. TEST_CASE_ASPECT(routerset_equal, differing_lengths),
  1738. TEST_CASE_ASPECT(routerset_equal, unequal),
  1739. TEST_CASE_ASPECT(routerset_equal, equal),
  1740. TEST_CASE_ASPECT(routerset_free, null_routerset),
  1741. TEST_CASE(routerset_free),
  1742. END_OF_TESTCASES
  1743. };