test_routerset.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233
  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 "core/or/policies.h"
  6. #include "feature/dirparse/policy_parse.h"
  7. #include "feature/nodelist/nodelist.h"
  8. #include "feature/nodelist/routerset.h"
  9. #include "lib/geoip/geoip.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. memset(&NS(mock_node), 0, sizeof(NS(mock_node)));
  1167. NS(mock_node).ri = NULL;
  1168. NS(mock_node).rs = NULL;
  1169. r = routerset_contains_node(set, &NS(mock_node));
  1170. tt_int_op(r, OP_EQ, 0);
  1171. done:
  1172. routerset_free(set);
  1173. }
  1174. #undef NS_SUBMODULE
  1175. #define NS_SUBMODULE ASPECT(routerset_contains_node, routerstatus)
  1176. /*
  1177. * Functional test for routerset_contains_node, when the node has a
  1178. * routerset and no routerinfo.
  1179. */
  1180. static node_t NS(mock_node);
  1181. static void
  1182. NS(test_main)(void *arg)
  1183. {
  1184. routerset_t *set = routerset_new();
  1185. int r;
  1186. const char *nickname = "foo";
  1187. routerstatus_t rs;
  1188. (void)arg;
  1189. strmap_set_lc(set->names, nickname, (void *)1);
  1190. strncpy(rs.nickname, nickname, sizeof(rs.nickname) - 1);
  1191. rs.nickname[sizeof(rs.nickname) - 1] = '\0';
  1192. memset(&NS(mock_node), 0, sizeof(NS(mock_node)));
  1193. NS(mock_node).ri = NULL;
  1194. NS(mock_node).rs = &rs;
  1195. r = routerset_contains_node(set, &NS(mock_node));
  1196. tt_int_op(r, OP_EQ, 4);
  1197. done:
  1198. routerset_free(set);
  1199. }
  1200. #undef NS_SUBMODULE
  1201. #define NS_SUBMODULE ASPECT(routerset_contains_node, routerinfo)
  1202. /*
  1203. * Functional test for routerset_contains_node, when the node has no
  1204. * routerset and a routerinfo.
  1205. */
  1206. static void
  1207. NS(test_main)(void *arg)
  1208. {
  1209. routerset_t *set = routerset_new();
  1210. int r;
  1211. const char *nickname = "foo";
  1212. routerinfo_t ri;
  1213. node_t mock_node;
  1214. (void)arg;
  1215. strmap_set_lc(set->names, nickname, (void *)1);
  1216. ri.nickname = (char *)nickname;
  1217. memset(&mock_node, 0, sizeof(mock_node));
  1218. mock_node.ri = &ri;
  1219. mock_node.rs = NULL;
  1220. r = routerset_contains_node(set, &mock_node);
  1221. tt_int_op(r, OP_EQ, 4);
  1222. done:
  1223. routerset_free(set);
  1224. }
  1225. #undef NS_SUBMODULE
  1226. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, no_routerset)
  1227. /*
  1228. * Functional test for routerset_get_all_nodes, when routerset is NULL or
  1229. * the routerset list is NULL.
  1230. */
  1231. static void
  1232. NS(test_main)(void *arg)
  1233. {
  1234. smartlist_t *out = smartlist_new();
  1235. routerset_t *set = NULL;
  1236. (void)arg;
  1237. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1238. routerset_get_all_nodes(out, NULL, NULL, 0);
  1239. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1240. set = routerset_new();
  1241. smartlist_free(set->list);
  1242. routerset_get_all_nodes(out, NULL, NULL, 0);
  1243. tt_int_op(smartlist_len(out), OP_EQ, 0);
  1244. /* Just recreate list, so we can simply use routerset_free. */
  1245. set->list = smartlist_new();
  1246. done:
  1247. routerset_free(set);
  1248. smartlist_free(out);
  1249. }
  1250. #undef NS_SUBMODULE
  1251. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_with_no_nodes)
  1252. /*
  1253. * Structural test for routerset_get_all_nodes, when the routerset list
  1254. * is empty.
  1255. */
  1256. NS_DECL(const node_t *, node_get_by_nickname,
  1257. (const char *nickname, unsigned flags));
  1258. static const char *NS(mock_nickname);
  1259. static void
  1260. NS(test_main)(void *arg)
  1261. {
  1262. smartlist_t *out = smartlist_new();
  1263. routerset_t *set = routerset_new();
  1264. int out_len;
  1265. (void)arg;
  1266. NS_MOCK(node_get_by_nickname);
  1267. NS(mock_nickname) = "foo";
  1268. smartlist_add_strdup(set->list, NS(mock_nickname));
  1269. routerset_get_all_nodes(out, set, NULL, 0);
  1270. out_len = smartlist_len(out);
  1271. smartlist_free(out);
  1272. routerset_free(set);
  1273. tt_int_op(out_len, OP_EQ, 0);
  1274. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1275. done:
  1276. ;
  1277. }
  1278. const node_t *
  1279. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1280. {
  1281. CALLED(node_get_by_nickname)++;
  1282. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1283. tt_uint_op(flags, OP_EQ, 0);
  1284. done:
  1285. return NULL;
  1286. }
  1287. #undef NS_SUBMODULE
  1288. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list_flag_not_running)
  1289. /*
  1290. * Structural test for routerset_get_all_nodes, with the running_only flag
  1291. * is set but the nodes are not running.
  1292. */
  1293. NS_DECL(const node_t *, node_get_by_nickname,
  1294. (const char *nickname, unsigned flags));
  1295. static const char *NS(mock_nickname);
  1296. static node_t NS(mock_node);
  1297. static void
  1298. NS(test_main)(void *arg)
  1299. {
  1300. smartlist_t *out = smartlist_new();
  1301. routerset_t *set = routerset_new();
  1302. int out_len;
  1303. (void)arg;
  1304. NS_MOCK(node_get_by_nickname);
  1305. NS(mock_node).is_running = 0;
  1306. NS(mock_nickname) = "foo";
  1307. smartlist_add_strdup(set->list, NS(mock_nickname));
  1308. routerset_get_all_nodes(out, set, NULL, 1);
  1309. out_len = smartlist_len(out);
  1310. smartlist_free(out);
  1311. routerset_free(set);
  1312. tt_int_op(out_len, OP_EQ, 0);
  1313. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1314. done:
  1315. ;
  1316. }
  1317. const node_t *
  1318. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1319. {
  1320. CALLED(node_get_by_nickname)++;
  1321. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1322. tt_int_op(flags, OP_EQ, 0);
  1323. done:
  1324. return &NS(mock_node);
  1325. }
  1326. #undef NS_SUBMODULE
  1327. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, list)
  1328. /*
  1329. * Structural test for routerset_get_all_nodes.
  1330. */
  1331. NS_DECL(const node_t *, node_get_by_nickname,
  1332. (const char *nickname, unsigned flags));
  1333. static char *NS(mock_nickname);
  1334. static node_t NS(mock_node);
  1335. static void
  1336. NS(test_main)(void *arg)
  1337. {
  1338. smartlist_t *out = smartlist_new();
  1339. routerset_t *set = routerset_new();
  1340. int out_len;
  1341. node_t *ent;
  1342. (void)arg;
  1343. NS_MOCK(node_get_by_nickname);
  1344. NS(mock_nickname) = tor_strdup("foo");
  1345. smartlist_add(set->list, NS(mock_nickname));
  1346. routerset_get_all_nodes(out, set, NULL, 0);
  1347. out_len = smartlist_len(out);
  1348. ent = (node_t *)smartlist_get(out, 0);
  1349. smartlist_free(out);
  1350. routerset_free(set);
  1351. tt_int_op(out_len, OP_EQ, 1);
  1352. tt_ptr_op(ent, OP_EQ, &NS(mock_node));
  1353. tt_int_op(CALLED(node_get_by_nickname), OP_EQ, 1);
  1354. done:
  1355. ;
  1356. }
  1357. const node_t *
  1358. NS(node_get_by_nickname)(const char *nickname, unsigned flags)
  1359. {
  1360. CALLED(node_get_by_nickname)++;
  1361. tt_str_op(nickname, OP_EQ, NS(mock_nickname));
  1362. tt_int_op(flags, OP_EQ, 0);
  1363. done:
  1364. return &NS(mock_node);
  1365. }
  1366. #undef NS_SUBMODULE
  1367. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes)
  1368. /*
  1369. * Structural test for routerset_get_all_nodes, when the nodelist has no nodes.
  1370. */
  1371. NS_DECL(smartlist_t *, nodelist_get_list, (void));
  1372. static smartlist_t *NS(mock_smartlist);
  1373. static void
  1374. NS(test_main)(void *arg)
  1375. {
  1376. routerset_t *set = routerset_new();
  1377. smartlist_t *out = smartlist_new();
  1378. int r;
  1379. (void)arg;
  1380. NS_MOCK(nodelist_get_list);
  1381. smartlist_add_strdup(set->country_names, "{xx}");
  1382. NS(mock_smartlist) = smartlist_new();
  1383. routerset_get_all_nodes(out, set, NULL, 1);
  1384. r = smartlist_len(out);
  1385. routerset_free(set);
  1386. smartlist_free(out);
  1387. smartlist_free(NS(mock_smartlist));
  1388. tt_int_op(r, OP_EQ, 0);
  1389. tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
  1390. done:
  1391. ;
  1392. }
  1393. smartlist_t *
  1394. NS(nodelist_get_list)(void)
  1395. {
  1396. CALLED(nodelist_get_list)++;
  1397. return NS(mock_smartlist);
  1398. }
  1399. #undef NS_SUBMODULE
  1400. #define NS_SUBMODULE ASPECT(routerset_get_all_nodes, nodelist_flag_not_running)
  1401. /*
  1402. * Structural test for routerset_get_all_nodes, with a non-list routerset
  1403. * the running_only flag is set, but the nodes are not running.
  1404. */
  1405. NS_DECL(smartlist_t *, nodelist_get_list, (void));
  1406. static smartlist_t *NS(mock_smartlist);
  1407. static node_t NS(mock_node);
  1408. static void
  1409. NS(test_main)(void *arg)
  1410. {
  1411. routerset_t *set = routerset_new();
  1412. smartlist_t *out = smartlist_new();
  1413. int r;
  1414. (void)arg;
  1415. NS_MOCK(nodelist_get_list);
  1416. smartlist_add_strdup(set->country_names, "{xx}");
  1417. NS(mock_smartlist) = smartlist_new();
  1418. NS(mock_node).is_running = 0;
  1419. smartlist_add(NS(mock_smartlist), (void *)&NS(mock_node));
  1420. routerset_get_all_nodes(out, set, NULL, 1);
  1421. r = smartlist_len(out);
  1422. routerset_free(set);
  1423. smartlist_free(out);
  1424. smartlist_free(NS(mock_smartlist));
  1425. tt_int_op(r, OP_EQ, 0);
  1426. tt_int_op(CALLED(nodelist_get_list), OP_EQ, 1);
  1427. done:
  1428. ;
  1429. }
  1430. smartlist_t *
  1431. NS(nodelist_get_list)(void)
  1432. {
  1433. CALLED(nodelist_get_list)++;
  1434. return NS(mock_smartlist);
  1435. }
  1436. #undef NS_SUBMODULE
  1437. #define NS_SUBMODULE routerset_subtract_nodes
  1438. /*
  1439. * Functional test for routerset_subtract_nodes.
  1440. */
  1441. static void
  1442. NS(test_main)(void *arg)
  1443. {
  1444. routerset_t *set = routerset_new();
  1445. smartlist_t *list = smartlist_new();
  1446. const char *nickname = "foo";
  1447. routerinfo_t ri;
  1448. node_t mock_node;
  1449. (void)arg;
  1450. strmap_set_lc(set->names, nickname, (void *)1);
  1451. ri.nickname = (char *)nickname;
  1452. mock_node.rs = NULL;
  1453. mock_node.ri = &ri;
  1454. smartlist_add(list, (void *)&mock_node);
  1455. tt_int_op(smartlist_len(list), OP_NE, 0);
  1456. routerset_subtract_nodes(list, set);
  1457. tt_int_op(smartlist_len(list), OP_EQ, 0);
  1458. done:
  1459. routerset_free(set);
  1460. smartlist_free(list);
  1461. }
  1462. #undef NS_SUBMODULE
  1463. #define NS_SUBMODULE ASPECT(routerset_subtract_nodes, null_routerset)
  1464. /*
  1465. * Functional test for routerset_subtract_nodes, with a NULL routerset.
  1466. */
  1467. static void
  1468. NS(test_main)(void *arg)
  1469. {
  1470. routerset_t *set = NULL;
  1471. smartlist_t *list = smartlist_new();
  1472. const char *nickname = "foo";
  1473. routerinfo_t ri;
  1474. node_t mock_node;
  1475. (void)arg;
  1476. ri.nickname = (char *)nickname;
  1477. mock_node.ri = &ri;
  1478. smartlist_add(list, (void *)&mock_node);
  1479. tt_int_op(smartlist_len(list), OP_NE, 0);
  1480. routerset_subtract_nodes(list, set);
  1481. tt_int_op(smartlist_len(list), OP_NE, 0);
  1482. done:
  1483. routerset_free(set);
  1484. smartlist_free(list);
  1485. }
  1486. #undef NS_SUBMODULE
  1487. #define NS_SUBMODULE routerset_to_string
  1488. /*
  1489. * Functional test for routerset_to_string.
  1490. */
  1491. static void
  1492. NS(test_main)(void *arg)
  1493. {
  1494. routerset_t *set = NULL;
  1495. char *s = NULL;
  1496. (void)arg;
  1497. set = NULL;
  1498. s = routerset_to_string(set);
  1499. tt_str_op(s, OP_EQ, "");
  1500. tor_free(s);
  1501. set = routerset_new();
  1502. s = routerset_to_string(set);
  1503. tt_str_op(s, OP_EQ, "");
  1504. tor_free(s);
  1505. routerset_free(set); set = NULL;
  1506. set = routerset_new();
  1507. smartlist_add(set->list, tor_strndup("a", 1));
  1508. s = routerset_to_string(set);
  1509. tt_str_op(s, OP_EQ, "a");
  1510. tor_free(s);
  1511. routerset_free(set); set = NULL;
  1512. set = routerset_new();
  1513. smartlist_add(set->list, tor_strndup("a", 1));
  1514. smartlist_add(set->list, tor_strndup("b", 1));
  1515. s = routerset_to_string(set);
  1516. tt_str_op(s, OP_EQ, "a,b");
  1517. tor_free(s);
  1518. routerset_free(set); set = NULL;
  1519. done:
  1520. tor_free(s);
  1521. routerset_free(set);
  1522. }
  1523. #undef NS_SUBMODULE
  1524. #define NS_SUBMODULE ASPECT(routerset_equal, empty_empty)
  1525. /*
  1526. * Functional test for routerset_equal, with both routersets empty.
  1527. */
  1528. static void
  1529. NS(test_main)(void *arg)
  1530. {
  1531. routerset_t *a = routerset_new(), *b = routerset_new();
  1532. int r;
  1533. (void)arg;
  1534. r = routerset_equal(a, b);
  1535. routerset_free(a);
  1536. routerset_free(b);
  1537. tt_int_op(r, OP_EQ, 1);
  1538. done:
  1539. ;
  1540. }
  1541. #undef NS_SUBMODULE
  1542. #define NS_SUBMODULE ASPECT(routerset_equal, empty_not_empty)
  1543. /*
  1544. * Functional test for routerset_equal, with one routersets empty.
  1545. */
  1546. static void
  1547. NS(test_main)(void *arg)
  1548. {
  1549. routerset_t *a = routerset_new(), *b = routerset_new();
  1550. int r;
  1551. (void)arg;
  1552. smartlist_add_strdup(b->list, "{xx}");
  1553. r = routerset_equal(a, b);
  1554. routerset_free(a);
  1555. routerset_free(b);
  1556. tt_int_op(r, OP_EQ, 0);
  1557. done:
  1558. ;
  1559. }
  1560. #undef NS_SUBMODULE
  1561. #define NS_SUBMODULE ASPECT(routerset_equal, differing_lengths)
  1562. /*
  1563. * Functional test for routerset_equal, with the routersets having
  1564. * differing lengths.
  1565. */
  1566. static void
  1567. NS(test_main)(void *arg)
  1568. {
  1569. routerset_t *a = routerset_new(), *b = routerset_new();
  1570. int r;
  1571. (void)arg;
  1572. smartlist_add_strdup(a->list, "{aa}");
  1573. smartlist_add_strdup(b->list, "{b1}");
  1574. smartlist_add_strdup(b->list, "{b2}");
  1575. r = routerset_equal(a, b);
  1576. routerset_free(a);
  1577. routerset_free(b);
  1578. tt_int_op(r, OP_EQ, 0);
  1579. done:
  1580. ;
  1581. }
  1582. #undef NS_SUBMODULE
  1583. #define NS_SUBMODULE ASPECT(routerset_equal, unequal)
  1584. /*
  1585. * Functional test for routerset_equal, with the routersets being
  1586. * different.
  1587. */
  1588. static void
  1589. NS(test_main)(void *arg)
  1590. {
  1591. routerset_t *a = routerset_new(), *b = routerset_new();
  1592. int r;
  1593. (void)arg;
  1594. smartlist_add_strdup(a->list, "foo");
  1595. smartlist_add_strdup(b->list, "bar");
  1596. r = routerset_equal(a, b);
  1597. routerset_free(a);
  1598. routerset_free(b);
  1599. tt_int_op(r, OP_EQ, 0);
  1600. done:
  1601. ;
  1602. }
  1603. #undef NS_SUBMODULE
  1604. #define NS_SUBMODULE ASPECT(routerset_equal, equal)
  1605. /*
  1606. * Functional test for routerset_equal, with the routersets being
  1607. * equal.
  1608. */
  1609. static void
  1610. NS(test_main)(void *arg)
  1611. {
  1612. routerset_t *a = routerset_new(), *b = routerset_new();
  1613. int r;
  1614. (void)arg;
  1615. smartlist_add_strdup(a->list, "foo");
  1616. smartlist_add_strdup(b->list, "foo");
  1617. r = routerset_equal(a, b);
  1618. routerset_free(a);
  1619. routerset_free(b);
  1620. tt_int_op(r, OP_EQ, 1);
  1621. done:
  1622. ;
  1623. }
  1624. #undef NS_SUBMODULE
  1625. #define NS_SUBMODULE ASPECT(routerset_free, null_routerset)
  1626. /*
  1627. * Structural test for routerset_free, where the routerset is NULL.
  1628. */
  1629. NS_DECL(void, smartlist_free_, (smartlist_t *sl));
  1630. static void
  1631. NS(test_main)(void *arg)
  1632. {
  1633. (void)arg;
  1634. NS_MOCK(smartlist_free_);
  1635. routerset_free_(NULL);
  1636. tt_int_op(CALLED(smartlist_free_), OP_EQ, 0);
  1637. done:
  1638. ;
  1639. }
  1640. void
  1641. NS(smartlist_free_)(smartlist_t *s)
  1642. {
  1643. (void)s;
  1644. CALLED(smartlist_free_)++;
  1645. }
  1646. #undef NS_SUBMODULE
  1647. #define NS_SUBMODULE routerset_free
  1648. /*
  1649. * Structural test for routerset_free.
  1650. */
  1651. NS_DECL(void, smartlist_free_, (smartlist_t *sl));
  1652. NS_DECL(void, strmap_free_,(strmap_t *map, void (*free_val)(void*)));
  1653. NS_DECL(void, digestmap_free_, (digestmap_t *map, void (*free_val)(void*)));
  1654. static void
  1655. NS(test_main)(void *arg)
  1656. {
  1657. routerset_t *routerset = routerset_new();
  1658. (void)arg;
  1659. NS_MOCK(smartlist_free_);
  1660. NS_MOCK(strmap_free_);
  1661. NS_MOCK(digestmap_free_);
  1662. routerset_free(routerset);
  1663. tt_int_op(CALLED(smartlist_free_), OP_NE, 0);
  1664. tt_int_op(CALLED(strmap_free_), OP_NE, 0);
  1665. tt_int_op(CALLED(digestmap_free_), OP_NE, 0);
  1666. done:
  1667. ;
  1668. }
  1669. void
  1670. NS(smartlist_free_)(smartlist_t *s)
  1671. {
  1672. CALLED(smartlist_free_)++;
  1673. smartlist_free___real(s);
  1674. }
  1675. void
  1676. NS(strmap_free_)(strmap_t *map, void (*free_val)(void*))
  1677. {
  1678. CALLED(strmap_free_)++;
  1679. strmap_free___real(map, free_val);
  1680. }
  1681. void
  1682. NS(digestmap_free_)(digestmap_t *map, void (*free_val)(void*))
  1683. {
  1684. CALLED(digestmap_free_)++;
  1685. digestmap_free___real(map, free_val);
  1686. }
  1687. #undef NS_SUBMODULE
  1688. struct testcase_t routerset_tests[] = {
  1689. TEST_CASE(routerset_new),
  1690. TEST_CASE(routerset_get_countryname),
  1691. TEST_CASE(routerset_is_list),
  1692. TEST_CASE(routerset_needs_geoip),
  1693. TEST_CASE(routerset_is_empty),
  1694. TEST_CASE_ASPECT(routerset_contains, null_set_or_null_set_list),
  1695. TEST_CASE_ASPECT(routerset_contains, set_and_nickname),
  1696. TEST_CASE_ASPECT(routerset_contains, set_and_null_nickname),
  1697. TEST_CASE_ASPECT(routerset_contains, set_and_no_nickname),
  1698. TEST_CASE_ASPECT(routerset_contains, set_and_digest),
  1699. TEST_CASE_ASPECT(routerset_contains, set_and_no_digest),
  1700. TEST_CASE_ASPECT(routerset_contains, set_and_null_digest),
  1701. TEST_CASE_ASPECT(routerset_contains, set_and_addr),
  1702. TEST_CASE_ASPECT(routerset_contains, set_and_no_addr),
  1703. TEST_CASE_ASPECT(routerset_contains, set_and_null_addr),
  1704. TEST_CASE_ASPECT(routerset_contains, countries_no_geoip),
  1705. TEST_CASE_ASPECT(routerset_contains, countries_geoip),
  1706. TEST_CASE_ASPECT(routerset_add_unknown_ccs, only_flag_and_no_ccs),
  1707. TEST_CASE_ASPECT(routerset_add_unknown_ccs, creates_set),
  1708. TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_unknown),
  1709. TEST_CASE_ASPECT(routerset_add_unknown_ccs, add_a1),
  1710. TEST_CASE(routerset_contains_extendinfo),
  1711. TEST_CASE(routerset_contains_router),
  1712. TEST_CASE(routerset_contains_routerstatus),
  1713. TEST_CASE_ASPECT(routerset_contains_node, none),
  1714. TEST_CASE_ASPECT(routerset_contains_node, routerinfo),
  1715. TEST_CASE_ASPECT(routerset_contains_node, routerstatus),
  1716. TEST_CASE_ASPECT(routerset_get_all_nodes, no_routerset),
  1717. TEST_CASE_ASPECT(routerset_get_all_nodes, list_with_no_nodes),
  1718. TEST_CASE_ASPECT(routerset_get_all_nodes, list_flag_not_running),
  1719. TEST_CASE_ASPECT(routerset_get_all_nodes, list),
  1720. TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_with_no_nodes),
  1721. TEST_CASE_ASPECT(routerset_get_all_nodes, nodelist_flag_not_running),
  1722. TEST_CASE_ASPECT(routerset_refresh_counties, geoip_not_loaded),
  1723. TEST_CASE_ASPECT(routerset_refresh_counties, no_countries),
  1724. TEST_CASE_ASPECT(routerset_refresh_counties, one_valid_country),
  1725. TEST_CASE_ASPECT(routerset_refresh_counties, one_invalid_country),
  1726. TEST_CASE_ASPECT(routerset_union, source_bad),
  1727. TEST_CASE_ASPECT(routerset_union, one),
  1728. TEST_CASE_ASPECT(routerset_parse, malformed),
  1729. TEST_CASE_ASPECT(routerset_parse, valid_hexdigest),
  1730. TEST_CASE_ASPECT(routerset_parse, valid_nickname),
  1731. TEST_CASE_ASPECT(routerset_parse, get_countryname),
  1732. TEST_CASE_ASPECT(routerset_parse, policy_wildcard),
  1733. TEST_CASE_ASPECT(routerset_parse, policy_ipv4),
  1734. TEST_CASE_ASPECT(routerset_parse, policy_ipv6),
  1735. TEST_CASE(routerset_subtract_nodes),
  1736. TEST_CASE_ASPECT(routerset_subtract_nodes, null_routerset),
  1737. TEST_CASE(routerset_to_string),
  1738. TEST_CASE_ASPECT(routerset_equal, empty_empty),
  1739. TEST_CASE_ASPECT(routerset_equal, empty_not_empty),
  1740. TEST_CASE_ASPECT(routerset_equal, differing_lengths),
  1741. TEST_CASE_ASPECT(routerset_equal, unequal),
  1742. TEST_CASE_ASPECT(routerset_equal, equal),
  1743. TEST_CASE_ASPECT(routerset_free, null_routerset),
  1744. TEST_CASE(routerset_free),
  1745. END_OF_TESTCASES
  1746. };