test_routerset.c 44 KB

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