test_config.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONFIG_PRIVATE
  7. #define PT_PRIVATE
  8. #include "or.h"
  9. #include "addressmap.h"
  10. #include "config.h"
  11. #include "confparse.h"
  12. #include "connection_edge.h"
  13. #include "test.h"
  14. #include "util.h"
  15. #include "address.h"
  16. #include "entrynodes.h"
  17. #include "transports.h"
  18. #include "routerlist.h"
  19. #include "networkstatus.h"
  20. static void
  21. test_config_addressmap(void *arg)
  22. {
  23. char buf[1024];
  24. char address[256];
  25. time_t expires = TIME_MAX;
  26. (void)arg;
  27. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  28. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  29. "MapAddress *.google.com *.torserver.exit\n"
  30. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  31. "MapAddress *.cn.com www.cnn.com\n"
  32. "MapAddress *.cnn.com www.cnn.com\n"
  33. "MapAddress ex.com www.cnn.com\n"
  34. "MapAddress ey.com *.cnn.com\n"
  35. "MapAddress www.torproject.org 1.1.1.1\n"
  36. "MapAddress other.torproject.org "
  37. "this.torproject.org.otherserver.exit\n"
  38. "MapAddress test.torproject.org 2.2.2.2\n"
  39. "MapAddress www.google.com 3.3.3.3\n"
  40. "MapAddress www.example.org 4.4.4.4\n"
  41. "MapAddress 4.4.4.4 7.7.7.7\n"
  42. "MapAddress 4.4.4.4 5.5.5.5\n"
  43. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  44. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  45. , sizeof(buf));
  46. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  47. config_register_addressmaps(get_options());
  48. /* Use old interface for now, so we don't need to rewrite the unit tests */
  49. #define addressmap_rewrite(a,s,eo,ao) \
  50. addressmap_rewrite((a),(s), ~0, (eo),(ao))
  51. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  52. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  53. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  54. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  55. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  56. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  57. /* Where no mapping for FQDN match on top-level domain */
  58. /* MapAddress .google.com .torserver.exit */
  59. strlcpy(address, "reader.google.com", sizeof(address));
  60. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  61. tt_str_op(address,OP_EQ, "reader.torserver.exit");
  62. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  63. strlcpy(address, "reader.yahoo.com", sizeof(address));
  64. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  65. tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
  66. /*MapAddress *.cnn.com www.cnn.com */
  67. strlcpy(address, "cnn.com", sizeof(address));
  68. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  69. tt_str_op(address,OP_EQ, "www.cnn.com");
  70. /* MapAddress .cn.com www.cnn.com */
  71. strlcpy(address, "www.cn.com", sizeof(address));
  72. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  73. tt_str_op(address,OP_EQ, "www.cnn.com");
  74. /* MapAddress ex.com www.cnn.com - no match */
  75. strlcpy(address, "www.ex.com", sizeof(address));
  76. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  77. /* MapAddress ey.com *.cnn.com - invalid expression */
  78. strlcpy(address, "ey.com", sizeof(address));
  79. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  80. /* Where mapping for FQDN match on FQDN */
  81. strlcpy(address, "www.google.com", sizeof(address));
  82. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  83. tt_str_op(address,OP_EQ, "3.3.3.3");
  84. strlcpy(address, "www.torproject.org", sizeof(address));
  85. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  86. tt_str_op(address,OP_EQ, "1.1.1.1");
  87. strlcpy(address, "other.torproject.org", sizeof(address));
  88. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  89. tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
  90. strlcpy(address, "test.torproject.org", sizeof(address));
  91. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  92. tt_str_op(address,OP_EQ, "2.2.2.2");
  93. /* Test a chain of address mappings and the order in which they were added:
  94. "MapAddress www.example.org 4.4.4.4"
  95. "MapAddress 4.4.4.4 7.7.7.7"
  96. "MapAddress 4.4.4.4 5.5.5.5"
  97. */
  98. strlcpy(address, "www.example.org", sizeof(address));
  99. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  100. tt_str_op(address,OP_EQ, "5.5.5.5");
  101. /* Test infinite address mapping results in no change */
  102. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  103. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  104. tt_str_op(address,OP_EQ, "www.infiniteloop.org");
  105. /* Test we don't find false positives */
  106. strlcpy(address, "www.example.com", sizeof(address));
  107. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  108. /* Test top-level-domain matching a bit harder */
  109. config_free_lines(get_options_mutable()->AddressMap);
  110. addressmap_clear_configured();
  111. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  112. "MapAddress *.torproject.org 1.1.1.1\n"
  113. "MapAddress *.net 2.2.2.2\n"
  114. , sizeof(buf));
  115. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  116. config_register_addressmaps(get_options());
  117. strlcpy(address, "www.abc.com", sizeof(address));
  118. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  119. tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
  120. strlcpy(address, "www.def.com", sizeof(address));
  121. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  122. tt_str_op(address,OP_EQ, "www.def.torserver.exit");
  123. strlcpy(address, "www.torproject.org", sizeof(address));
  124. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  125. tt_str_op(address,OP_EQ, "1.1.1.1");
  126. strlcpy(address, "test.torproject.org", sizeof(address));
  127. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  128. tt_str_op(address,OP_EQ, "1.1.1.1");
  129. strlcpy(address, "torproject.net", sizeof(address));
  130. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  131. tt_str_op(address,OP_EQ, "2.2.2.2");
  132. /* We don't support '*' as a mapping directive */
  133. config_free_lines(get_options_mutable()->AddressMap);
  134. addressmap_clear_configured();
  135. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  136. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  137. config_register_addressmaps(get_options());
  138. strlcpy(address, "www.abc.com", sizeof(address));
  139. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  140. strlcpy(address, "www.def.net", sizeof(address));
  141. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  142. strlcpy(address, "www.torproject.org", sizeof(address));
  143. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  144. #undef addressmap_rewrite
  145. done:
  146. config_free_lines(get_options_mutable()->AddressMap);
  147. get_options_mutable()->AddressMap = NULL;
  148. addressmap_free_all();
  149. }
  150. static int
  151. is_private_dir(const char* path)
  152. {
  153. struct stat st;
  154. int r = stat(path, &st);
  155. if (r) {
  156. return 0;
  157. }
  158. #if !defined (_WIN32)
  159. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  160. return 0;
  161. }
  162. #endif
  163. return 1;
  164. }
  165. static void
  166. test_config_check_or_create_data_subdir(void *arg)
  167. {
  168. or_options_t *options = get_options_mutable();
  169. char *datadir;
  170. const char *subdir = "test_stats";
  171. char *subpath;
  172. struct stat st;
  173. int r;
  174. #if !defined (_WIN32)
  175. unsigned group_permission;
  176. #endif
  177. (void)arg;
  178. tor_free(options->DataDirectory);
  179. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  180. subpath = get_datadir_fname(subdir);
  181. #if defined (_WIN32)
  182. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  183. #else
  184. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  185. #endif
  186. r = stat(subpath, &st);
  187. // The subdirectory shouldn't exist yet,
  188. // but should be created by the call to check_or_create_data_subdir.
  189. tt_assert(r && (errno == ENOENT));
  190. tt_assert(!check_or_create_data_subdir(subdir));
  191. tt_assert(is_private_dir(subpath));
  192. // The check should return 0, if the directory already exists
  193. // and is private to the user.
  194. tt_assert(!check_or_create_data_subdir(subdir));
  195. r = stat(subpath, &st);
  196. if (r) {
  197. tt_abort_perror("stat");
  198. }
  199. #if !defined (_WIN32)
  200. group_permission = st.st_mode | 0070;
  201. r = chmod(subpath, group_permission);
  202. if (r) {
  203. tt_abort_perror("chmod");
  204. }
  205. // If the directory exists, but its mode is too permissive
  206. // a call to check_or_create_data_subdir should reset the mode.
  207. tt_assert(!is_private_dir(subpath));
  208. tt_assert(!check_or_create_data_subdir(subdir));
  209. tt_assert(is_private_dir(subpath));
  210. #endif
  211. done:
  212. rmdir(subpath);
  213. tor_free(datadir);
  214. tor_free(subpath);
  215. }
  216. static void
  217. test_config_write_to_data_subdir(void *arg)
  218. {
  219. or_options_t* options = get_options_mutable();
  220. char *datadir;
  221. char *cp = NULL;
  222. const char* subdir = "test_stats";
  223. const char* fname = "test_file";
  224. const char* str =
  225. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  226. "elitr, sed diam nonumy eirmod\n"
  227. "tempor invidunt ut labore et dolore magna aliquyam\n"
  228. "erat, sed diam voluptua.\n"
  229. "At vero eos et accusam et justo duo dolores et ea\n"
  230. "rebum. Stet clita kasd gubergren,\n"
  231. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  232. "Lorem ipsum dolor sit amet,\n"
  233. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  234. "tempor invidunt ut labore et dolore\n"
  235. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  236. "accusam et justo duo dolores et\n"
  237. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  238. "sanctus est Lorem ipsum dolor sit amet.";
  239. char* filepath = NULL;
  240. (void)arg;
  241. tor_free(options->DataDirectory);
  242. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  243. filepath = get_datadir_fname2(subdir, fname);
  244. #if defined (_WIN32)
  245. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  246. #else
  247. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  248. #endif
  249. // Write attempt shoudl fail, if subdirectory doesn't exist.
  250. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  251. tt_assert(! check_or_create_data_subdir(subdir));
  252. // Content of file after write attempt should be
  253. // equal to the original string.
  254. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  255. cp = read_file_to_str(filepath, 0, NULL);
  256. tt_str_op(cp,OP_EQ, str);
  257. tor_free(cp);
  258. // A second write operation should overwrite the old content.
  259. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  260. cp = read_file_to_str(filepath, 0, NULL);
  261. tt_str_op(cp,OP_EQ, str);
  262. tor_free(cp);
  263. done:
  264. (void) unlink(filepath);
  265. rmdir(options->DataDirectory);
  266. tor_free(datadir);
  267. tor_free(filepath);
  268. tor_free(cp);
  269. }
  270. /* Test helper function: Make sure that a bridge line gets parsed
  271. * properly. Also make sure that the resulting bridge_line_t structure
  272. * has its fields set correctly. */
  273. static void
  274. good_bridge_line_test(const char *string, const char *test_addrport,
  275. const char *test_digest, const char *test_transport,
  276. const smartlist_t *test_socks_args)
  277. {
  278. char *tmp = NULL;
  279. bridge_line_t *bridge_line = parse_bridge_line(string);
  280. tt_assert(bridge_line);
  281. /* test addrport */
  282. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  283. tt_str_op(test_addrport,OP_EQ, tmp);
  284. tor_free(tmp);
  285. /* If we were asked to validate a digest, but we did not get a
  286. digest after parsing, we failed. */
  287. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  288. tt_assert(0);
  289. /* If we were not asked to validate a digest, and we got a digest
  290. after parsing, we failed again. */
  291. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  292. tt_assert(0);
  293. /* If we were asked to validate a digest, and we got a digest after
  294. parsing, make sure it's correct. */
  295. if (test_digest) {
  296. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  297. tor_strlower(tmp);
  298. tt_str_op(test_digest,OP_EQ, tmp);
  299. tor_free(tmp);
  300. }
  301. /* If we were asked to validate a transport name, make sure tha it
  302. matches with the transport name that was parsed. */
  303. if (test_transport && !bridge_line->transport_name)
  304. tt_assert(0);
  305. if (!test_transport && bridge_line->transport_name)
  306. tt_assert(0);
  307. if (test_transport)
  308. tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
  309. /* Validate the SOCKS argument smartlist. */
  310. if (test_socks_args && !bridge_line->socks_args)
  311. tt_assert(0);
  312. if (!test_socks_args && bridge_line->socks_args)
  313. tt_assert(0);
  314. if (test_socks_args)
  315. tt_assert(smartlist_strings_eq(test_socks_args,
  316. bridge_line->socks_args));
  317. done:
  318. tor_free(tmp);
  319. bridge_line_free(bridge_line);
  320. }
  321. /* Test helper function: Make sure that a bridge line is
  322. * unparseable. */
  323. static void
  324. bad_bridge_line_test(const char *string)
  325. {
  326. bridge_line_t *bridge_line = parse_bridge_line(string);
  327. if (bridge_line)
  328. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  329. tt_assert(!bridge_line);
  330. done:
  331. bridge_line_free(bridge_line);
  332. }
  333. static void
  334. test_config_parse_bridge_line(void *arg)
  335. {
  336. (void) arg;
  337. good_bridge_line_test("192.0.2.1:4123",
  338. "192.0.2.1:4123", NULL, NULL, NULL);
  339. good_bridge_line_test("192.0.2.1",
  340. "192.0.2.1:443", NULL, NULL, NULL);
  341. good_bridge_line_test("transport [::1]",
  342. "[::1]:443", NULL, "transport", NULL);
  343. good_bridge_line_test("transport 192.0.2.1:12 "
  344. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  345. "192.0.2.1:12",
  346. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  347. "transport", NULL);
  348. {
  349. smartlist_t *sl_tmp = smartlist_new();
  350. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  351. good_bridge_line_test("transport 192.0.2.1:12 "
  352. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  353. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  354. "transport", sl_tmp);
  355. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  356. smartlist_free(sl_tmp);
  357. }
  358. {
  359. smartlist_t *sl_tmp = smartlist_new();
  360. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  361. smartlist_add_asprintf(sl_tmp, "z=z");
  362. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  363. "192.0.2.1:12", NULL, "transport", sl_tmp);
  364. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  365. smartlist_free(sl_tmp);
  366. }
  367. {
  368. smartlist_t *sl_tmp = smartlist_new();
  369. smartlist_add_asprintf(sl_tmp, "dub=come");
  370. smartlist_add_asprintf(sl_tmp, "save=me");
  371. good_bridge_line_test("transport 192.0.2.1:12 "
  372. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  373. "dub=come save=me",
  374. "192.0.2.1:12",
  375. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  376. "transport", sl_tmp);
  377. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  378. smartlist_free(sl_tmp);
  379. }
  380. good_bridge_line_test("192.0.2.1:1231 "
  381. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  382. "192.0.2.1:1231",
  383. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  384. NULL, NULL);
  385. /* Empty line */
  386. bad_bridge_line_test("");
  387. /* bad transport name */
  388. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  389. /* weird ip address */
  390. bad_bridge_line_test("a.b.c.d");
  391. /* invalid fpr */
  392. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  393. /* no k=v in the end */
  394. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  395. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  396. /* no addrport */
  397. bad_bridge_line_test("asdw");
  398. /* huge k=v value that can't fit in SOCKS fields */
  399. bad_bridge_line_test(
  400. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  401. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  402. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  403. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  404. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  405. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  406. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  407. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  408. "aa=b");
  409. }
  410. static void
  411. test_config_parse_transport_options_line(void *arg)
  412. {
  413. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  414. (void) arg;
  415. { /* too small line */
  416. options_sl = get_options_from_transport_options_line("valley", NULL);
  417. tt_assert(!options_sl);
  418. }
  419. { /* no k=v values */
  420. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  421. tt_assert(!options_sl);
  422. }
  423. { /* correct line, but wrong transport specified */
  424. options_sl =
  425. get_options_from_transport_options_line("trebuchet k=v", "rook");
  426. tt_assert(!options_sl);
  427. }
  428. { /* correct -- no transport specified */
  429. sl_tmp = smartlist_new();
  430. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  431. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  432. options_sl =
  433. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  434. NULL);
  435. tt_assert(options_sl);
  436. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  437. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  438. smartlist_free(sl_tmp);
  439. sl_tmp = NULL;
  440. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  441. smartlist_free(options_sl);
  442. options_sl = NULL;
  443. }
  444. { /* correct -- correct transport specified */
  445. sl_tmp = smartlist_new();
  446. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  447. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  448. options_sl =
  449. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  450. "rook");
  451. tt_assert(options_sl);
  452. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  453. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  454. smartlist_free(sl_tmp);
  455. sl_tmp = NULL;
  456. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  457. smartlist_free(options_sl);
  458. options_sl = NULL;
  459. }
  460. done:
  461. if (options_sl) {
  462. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  463. smartlist_free(options_sl);
  464. }
  465. if (sl_tmp) {
  466. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  467. smartlist_free(sl_tmp);
  468. }
  469. }
  470. /* Mocks needed for the transport plugin line test */
  471. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  472. char **proxy_argv, int is_server);
  473. static int transport_add_from_config_mock(const tor_addr_t *addr,
  474. uint16_t port, const char *name,
  475. int socks_ver);
  476. static int transport_is_needed_mock(const char *transport_name);
  477. static int pt_kickstart_proxy_mock_call_count = 0;
  478. static int transport_add_from_config_mock_call_count = 0;
  479. static int transport_is_needed_mock_call_count = 0;
  480. static int transport_is_needed_mock_return = 0;
  481. static void
  482. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  483. char **proxy_argv, int is_server)
  484. {
  485. (void) transport_list;
  486. (void) proxy_argv;
  487. (void) is_server;
  488. /* XXXX check that args are as expected. */
  489. ++pt_kickstart_proxy_mock_call_count;
  490. free_execve_args(proxy_argv);
  491. }
  492. static int
  493. transport_add_from_config_mock(const tor_addr_t *addr,
  494. uint16_t port, const char *name,
  495. int socks_ver)
  496. {
  497. (void) addr;
  498. (void) port;
  499. (void) name;
  500. (void) socks_ver;
  501. /* XXXX check that args are as expected. */
  502. ++transport_add_from_config_mock_call_count;
  503. return 0;
  504. }
  505. static int
  506. transport_is_needed_mock(const char *transport_name)
  507. {
  508. (void) transport_name;
  509. /* XXXX check that arg is as expected. */
  510. ++transport_is_needed_mock_call_count;
  511. return transport_is_needed_mock_return;
  512. }
  513. /**
  514. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  515. * options.
  516. */
  517. static void
  518. test_config_parse_transport_plugin_line(void *arg)
  519. {
  520. (void)arg;
  521. or_options_t *options = get_options_mutable();
  522. int r, tmp;
  523. int old_pt_kickstart_proxy_mock_call_count;
  524. int old_transport_add_from_config_mock_call_count;
  525. int old_transport_is_needed_mock_call_count;
  526. /* Bad transport lines - too short */
  527. r = parse_transport_line(options, "bad", 1, 0);
  528. tt_assert(r < 0);
  529. r = parse_transport_line(options, "bad", 1, 1);
  530. tt_assert(r < 0);
  531. r = parse_transport_line(options, "bad bad", 1, 0);
  532. tt_assert(r < 0);
  533. r = parse_transport_line(options, "bad bad", 1, 1);
  534. tt_assert(r < 0);
  535. /* Test transport list parsing */
  536. r = parse_transport_line(options,
  537. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  538. tt_assert(r == 0);
  539. r = parse_transport_line(options,
  540. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  541. tt_assert(r == 0);
  542. r = parse_transport_line(options,
  543. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  544. tt_assert(r == 0);
  545. r = parse_transport_line(options,
  546. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  547. tt_assert(r == 0);
  548. /* Bad transport identifiers */
  549. r = parse_transport_line(options,
  550. "transport_* exec /usr/bin/fake-transport", 1, 0);
  551. tt_assert(r < 0);
  552. r = parse_transport_line(options,
  553. "transport_* exec /usr/bin/fake-transport", 1, 1);
  554. tt_assert(r < 0);
  555. /* Check SOCKS cases for client transport */
  556. r = parse_transport_line(options,
  557. "transport_1 socks4 1.2.3.4:567", 1, 0);
  558. tt_assert(r == 0);
  559. r = parse_transport_line(options,
  560. "transport_1 socks5 1.2.3.4:567", 1, 0);
  561. tt_assert(r == 0);
  562. /* Proxy case for server transport */
  563. r = parse_transport_line(options,
  564. "transport_1 proxy 1.2.3.4:567", 1, 1);
  565. tt_assert(r == 0);
  566. /* Multiple-transport error exit */
  567. r = parse_transport_line(options,
  568. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  569. tt_assert(r < 0);
  570. r = parse_transport_line(options,
  571. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  572. /* No port error exit */
  573. r = parse_transport_line(options,
  574. "transport_1 socks5 1.2.3.4", 1, 0);
  575. tt_assert(r < 0);
  576. r = parse_transport_line(options,
  577. "transport_1 proxy 1.2.3.4", 1, 1);
  578. tt_assert(r < 0);
  579. /* Unparsable address error exit */
  580. r = parse_transport_line(options,
  581. "transport_1 socks5 1.2.3:6x7", 1, 0);
  582. tt_assert(r < 0);
  583. r = parse_transport_line(options,
  584. "transport_1 proxy 1.2.3:6x7", 1, 1);
  585. tt_assert(r < 0);
  586. /* "Strange {Client|Server}TransportPlugin field" error exit */
  587. r = parse_transport_line(options,
  588. "transport_1 foo bar", 1, 0);
  589. tt_assert(r < 0);
  590. r = parse_transport_line(options,
  591. "transport_1 foo bar", 1, 1);
  592. tt_assert(r < 0);
  593. /* No sandbox mode error exit */
  594. tmp = options->Sandbox;
  595. options->Sandbox = 1;
  596. r = parse_transport_line(options,
  597. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  598. tt_assert(r < 0);
  599. r = parse_transport_line(options,
  600. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  601. tt_assert(r < 0);
  602. options->Sandbox = tmp;
  603. /*
  604. * These final test cases cover code paths that only activate without
  605. * validate_only, so they need mocks in place.
  606. */
  607. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  608. old_pt_kickstart_proxy_mock_call_count =
  609. pt_kickstart_proxy_mock_call_count;
  610. r = parse_transport_line(options,
  611. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  612. tt_assert(r == 0);
  613. tt_assert(pt_kickstart_proxy_mock_call_count ==
  614. old_pt_kickstart_proxy_mock_call_count + 1);
  615. UNMOCK(pt_kickstart_proxy);
  616. /* This one hits a log line in the !validate_only case only */
  617. r = parse_transport_line(options,
  618. "transport_1 proxy 1.2.3.4:567", 0, 1);
  619. tt_assert(r == 0);
  620. /* Check mocked client transport cases */
  621. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  622. MOCK(transport_add_from_config, transport_add_from_config_mock);
  623. MOCK(transport_is_needed, transport_is_needed_mock);
  624. /* Unnecessary transport case */
  625. transport_is_needed_mock_return = 0;
  626. old_pt_kickstart_proxy_mock_call_count =
  627. pt_kickstart_proxy_mock_call_count;
  628. old_transport_add_from_config_mock_call_count =
  629. transport_add_from_config_mock_call_count;
  630. old_transport_is_needed_mock_call_count =
  631. transport_is_needed_mock_call_count;
  632. r = parse_transport_line(options,
  633. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  634. /* Should have succeeded */
  635. tt_assert(r == 0);
  636. /* transport_is_needed() should have been called */
  637. tt_assert(transport_is_needed_mock_call_count ==
  638. old_transport_is_needed_mock_call_count + 1);
  639. /*
  640. * pt_kickstart_proxy() and transport_add_from_config() should
  641. * not have been called.
  642. */
  643. tt_assert(pt_kickstart_proxy_mock_call_count ==
  644. old_pt_kickstart_proxy_mock_call_count);
  645. tt_assert(transport_add_from_config_mock_call_count ==
  646. old_transport_add_from_config_mock_call_count);
  647. /* Necessary transport case */
  648. transport_is_needed_mock_return = 1;
  649. old_pt_kickstart_proxy_mock_call_count =
  650. pt_kickstart_proxy_mock_call_count;
  651. old_transport_add_from_config_mock_call_count =
  652. transport_add_from_config_mock_call_count;
  653. old_transport_is_needed_mock_call_count =
  654. transport_is_needed_mock_call_count;
  655. r = parse_transport_line(options,
  656. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  657. /* Should have succeeded */
  658. tt_assert(r == 0);
  659. /*
  660. * transport_is_needed() and pt_kickstart_proxy() should have been
  661. * called.
  662. */
  663. tt_assert(pt_kickstart_proxy_mock_call_count ==
  664. old_pt_kickstart_proxy_mock_call_count + 1);
  665. tt_assert(transport_is_needed_mock_call_count ==
  666. old_transport_is_needed_mock_call_count + 1);
  667. /* transport_add_from_config() should not have been called. */
  668. tt_assert(transport_add_from_config_mock_call_count ==
  669. old_transport_add_from_config_mock_call_count);
  670. /* proxy case */
  671. transport_is_needed_mock_return = 1;
  672. old_pt_kickstart_proxy_mock_call_count =
  673. pt_kickstart_proxy_mock_call_count;
  674. old_transport_add_from_config_mock_call_count =
  675. transport_add_from_config_mock_call_count;
  676. old_transport_is_needed_mock_call_count =
  677. transport_is_needed_mock_call_count;
  678. r = parse_transport_line(options,
  679. "transport_1 socks5 1.2.3.4:567", 0, 0);
  680. /* Should have succeeded */
  681. tt_assert(r == 0);
  682. /*
  683. * transport_is_needed() and transport_add_from_config() should have
  684. * been called.
  685. */
  686. tt_assert(transport_add_from_config_mock_call_count ==
  687. old_transport_add_from_config_mock_call_count + 1);
  688. tt_assert(transport_is_needed_mock_call_count ==
  689. old_transport_is_needed_mock_call_count + 1);
  690. /* pt_kickstart_proxy() should not have been called. */
  691. tt_assert(pt_kickstart_proxy_mock_call_count ==
  692. old_pt_kickstart_proxy_mock_call_count);
  693. /* Done with mocked client transport cases */
  694. UNMOCK(transport_is_needed);
  695. UNMOCK(transport_add_from_config);
  696. UNMOCK(pt_kickstart_proxy);
  697. done:
  698. /* Make sure we undo all mocks */
  699. UNMOCK(pt_kickstart_proxy);
  700. UNMOCK(transport_add_from_config);
  701. UNMOCK(transport_is_needed);
  702. return;
  703. }
  704. // Tests if an options with MyFamily fingerprints missing '$' normalises
  705. // them correctly and also ensure it also works with multiple fingerprints
  706. static void
  707. test_config_fix_my_family(void *arg)
  708. {
  709. char *err = NULL;
  710. const char *family = "$1111111111111111111111111111111111111111, "
  711. "1111111111111111111111111111111111111112, "
  712. "$1111111111111111111111111111111111111113";
  713. or_options_t* options = options_new();
  714. or_options_t* defaults = options_new();
  715. (void) arg;
  716. options_init(options);
  717. options_init(defaults);
  718. options->MyFamily = tor_strdup(family);
  719. options_validate(NULL, options, defaults, 0, &err) ;
  720. if (err != NULL) {
  721. TT_FAIL(("options_validate failed: %s", err));
  722. }
  723. tt_str_op(options->MyFamily,OP_EQ,
  724. "$1111111111111111111111111111111111111111, "
  725. "$1111111111111111111111111111111111111112, "
  726. "$1111111111111111111111111111111111111113");
  727. done:
  728. if (err != NULL) {
  729. tor_free(err);
  730. }
  731. or_options_free(options);
  732. or_options_free(defaults);
  733. }
  734. static int n_hostname_01010101 = 0;
  735. /** This mock function is meant to replace tor_lookup_hostname().
  736. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  737. * This function increments <b>n_hostname_01010101</b> counter by one
  738. * every time it is called.
  739. */
  740. static int
  741. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  742. {
  743. n_hostname_01010101++;
  744. if (name && addr) {
  745. *addr = ntohl(0x01010101);
  746. }
  747. return 0;
  748. }
  749. static int n_hostname_localhost = 0;
  750. /** This mock function is meant to replace tor_lookup_hostname().
  751. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  752. * This function increments <b>n_hostname_localhost</b> counter by one
  753. * every time it is called.
  754. */
  755. static int
  756. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  757. {
  758. n_hostname_localhost++;
  759. if (name && addr) {
  760. *addr = 0x7f000001;
  761. }
  762. return 0;
  763. }
  764. static int n_hostname_failure = 0;
  765. /** This mock function is meant to replace tor_lookup_hostname().
  766. * It pretends to fail by returning -1 to caller. Also, this function
  767. * increments <b>n_hostname_failure</b> every time it is called.
  768. */
  769. static int
  770. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  771. {
  772. (void)name;
  773. (void)addr;
  774. n_hostname_failure++;
  775. return -1;
  776. }
  777. static int n_gethostname_replacement = 0;
  778. /** This mock function is meant to replace tor_gethostname(). It
  779. * responds with string "onionrouter!" as hostname. This function
  780. * increments <b>n_gethostname_replacement</b> by one every time
  781. * it is called.
  782. */
  783. static int
  784. tor_gethostname_replacement(char *name, size_t namelen)
  785. {
  786. n_gethostname_replacement++;
  787. if (name && namelen) {
  788. strlcpy(name,"onionrouter!",namelen);
  789. }
  790. return 0;
  791. }
  792. static int n_gethostname_localhost = 0;
  793. /** This mock function is meant to replace tor_gethostname(). It
  794. * responds with string "127.0.0.1" as hostname. This function
  795. * increments <b>n_gethostname_localhost</b> by one every time
  796. * it is called.
  797. */
  798. static int
  799. tor_gethostname_localhost(char *name, size_t namelen)
  800. {
  801. n_gethostname_localhost++;
  802. if (name && namelen) {
  803. strlcpy(name,"127.0.0.1",namelen);
  804. }
  805. return 0;
  806. }
  807. static int n_gethostname_failure = 0;
  808. /** This mock function is meant to replace tor_gethostname.
  809. * It pretends to fail by returning -1. This function increments
  810. * <b>n_gethostname_failure</b> by one every time it is called.
  811. */
  812. static int
  813. tor_gethostname_failure(char *name, size_t namelen)
  814. {
  815. (void)name;
  816. (void)namelen;
  817. n_gethostname_failure++;
  818. return -1;
  819. }
  820. static int n_get_interface_address = 0;
  821. /** This mock function is meant to replace get_interface_address().
  822. * It answers with address 8.8.8.8. This function increments
  823. * <b>n_get_interface_address</b> by one every time it is called.
  824. */
  825. static int
  826. get_interface_address_08080808(int severity, uint32_t *addr)
  827. {
  828. (void)severity;
  829. n_get_interface_address++;
  830. if (addr) {
  831. *addr = ntohl(0x08080808);
  832. }
  833. return 0;
  834. }
  835. static int n_get_interface_address6 = 0;
  836. static sa_family_t last_address6_family;
  837. /** This mock function is meant to replace get_interface_address6().
  838. * It answers with IP address 9.9.9.9 iff both of the following are true:
  839. * - <b>family</b> is AF_INET
  840. * - <b>addr</b> pointer is not NULL.
  841. * This function increments <b>n_get_interface_address6</b> by one every
  842. * time it is called.
  843. */
  844. static int
  845. get_interface_address6_replacement(int severity, sa_family_t family,
  846. tor_addr_t *addr)
  847. {
  848. (void)severity;
  849. last_address6_family = family;
  850. n_get_interface_address6++;
  851. if ((family != AF_INET) || !addr) {
  852. return -1;
  853. }
  854. tor_addr_from_ipv4h(addr,0x09090909);
  855. return 0;
  856. }
  857. static int n_get_interface_address_failure = 0;
  858. /**
  859. * This mock function is meant to replace get_interface_address().
  860. * It pretends to fail getting interface address by returning -1.
  861. * <b>n_get_interface_address_failure</b> is incremented by one
  862. * every time this function is called.
  863. */
  864. static int
  865. get_interface_address_failure(int severity, uint32_t *addr)
  866. {
  867. (void)severity;
  868. (void)addr;
  869. n_get_interface_address_failure++;
  870. return -1;
  871. }
  872. static int n_get_interface_address6_failure = 0;
  873. /**
  874. * This mock function is meant to replace get_interface_addres6().
  875. * It will pretend to fail by return -1.
  876. * <b>n_get_interface_address6_failure</b> is incremented by one
  877. * every time this function is called and <b>last_address6_family</b>
  878. * is assigned the value of <b>family</b> argument.
  879. */
  880. static int
  881. get_interface_address6_failure(int severity, sa_family_t family,
  882. tor_addr_t *addr)
  883. {
  884. (void)severity;
  885. (void)addr;
  886. n_get_interface_address6_failure++;
  887. last_address6_family = family;
  888. return -1;
  889. }
  890. static void
  891. test_config_resolve_my_address(void *arg)
  892. {
  893. or_options_t *options;
  894. uint32_t resolved_addr;
  895. const char *method_used;
  896. char *hostname_out = NULL;
  897. int retval;
  898. int prev_n_hostname_01010101;
  899. int prev_n_hostname_localhost;
  900. int prev_n_hostname_failure;
  901. int prev_n_gethostname_replacement;
  902. int prev_n_gethostname_failure;
  903. int prev_n_gethostname_localhost;
  904. int prev_n_get_interface_address;
  905. int prev_n_get_interface_address_failure;
  906. int prev_n_get_interface_address6;
  907. int prev_n_get_interface_address6_failure;
  908. (void)arg;
  909. options = options_new();
  910. options_init(options);
  911. /*
  912. * CASE 1:
  913. * If options->Address is a valid IPv4 address string, we want
  914. * the corresponding address to be parsed and returned.
  915. */
  916. options->Address = tor_strdup("128.52.128.105");
  917. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  918. &method_used,&hostname_out);
  919. tt_want(retval == 0);
  920. tt_want_str_op(method_used,==,"CONFIGURED");
  921. tt_want(hostname_out == NULL);
  922. tt_assert(resolved_addr == 0x80348069);
  923. tor_free(options->Address);
  924. /*
  925. * CASE 2:
  926. * If options->Address is a valid DNS address, we want resolve_my_address()
  927. * function to ask tor_lookup_hostname() for help with resolving it
  928. * and return the address that was resolved (in host order).
  929. */
  930. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  931. tor_free(options->Address);
  932. options->Address = tor_strdup("www.torproject.org");
  933. prev_n_hostname_01010101 = n_hostname_01010101;
  934. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  935. &method_used,&hostname_out);
  936. tt_want(retval == 0);
  937. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  938. tt_want_str_op(method_used,==,"RESOLVED");
  939. tt_want_str_op(hostname_out,==,"www.torproject.org");
  940. tt_assert(resolved_addr == 0x01010101);
  941. UNMOCK(tor_lookup_hostname);
  942. tor_free(options->Address);
  943. tor_free(hostname_out);
  944. /*
  945. * CASE 3:
  946. * Given that options->Address is NULL, we want resolve_my_address()
  947. * to try and use tor_gethostname() to get hostname AND use
  948. * tor_lookup_hostname() to get IP address.
  949. */
  950. resolved_addr = 0;
  951. tor_free(options->Address);
  952. options->Address = NULL;
  953. MOCK(tor_gethostname,tor_gethostname_replacement);
  954. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  955. prev_n_gethostname_replacement = n_gethostname_replacement;
  956. prev_n_hostname_01010101 = n_hostname_01010101;
  957. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  958. &method_used,&hostname_out);
  959. tt_want(retval == 0);
  960. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  961. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  962. tt_want_str_op(method_used,==,"GETHOSTNAME");
  963. tt_want_str_op(hostname_out,==,"onionrouter!");
  964. tt_assert(resolved_addr == 0x01010101);
  965. UNMOCK(tor_gethostname);
  966. UNMOCK(tor_lookup_hostname);
  967. tor_free(hostname_out);
  968. /*
  969. * CASE 4:
  970. * Given that options->Address is a local host address, we want
  971. * resolve_my_address() function to fail.
  972. */
  973. resolved_addr = 0;
  974. tor_free(options->Address);
  975. options->Address = tor_strdup("127.0.0.1");
  976. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  977. &method_used,&hostname_out);
  978. tt_want(resolved_addr == 0);
  979. tt_assert(retval == -1);
  980. tor_free(options->Address);
  981. tor_free(hostname_out);
  982. /*
  983. * CASE 5:
  984. * We want resolve_my_address() to fail if DNS address in options->Address
  985. * cannot be resolved.
  986. */
  987. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  988. prev_n_hostname_failure = n_hostname_failure;
  989. tor_free(options->Address);
  990. options->Address = tor_strdup("www.tor-project.org");
  991. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  992. &method_used,&hostname_out);
  993. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  994. tt_assert(retval == -1);
  995. UNMOCK(tor_lookup_hostname);
  996. tor_free(options->Address);
  997. tor_free(hostname_out);
  998. /*
  999. * CASE 6:
  1000. * If options->Address is NULL AND gettting local hostname fails, we want
  1001. * resolve_my_address() to fail as well.
  1002. */
  1003. MOCK(tor_gethostname,tor_gethostname_failure);
  1004. prev_n_gethostname_failure = n_gethostname_failure;
  1005. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1006. &method_used,&hostname_out);
  1007. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1008. tt_assert(retval == -1);
  1009. UNMOCK(tor_gethostname);
  1010. tor_free(hostname_out);
  1011. /*
  1012. * CASE 7:
  1013. * We want resolve_my_address() to try and get network interface address via
  1014. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1015. * resolved into IP address.
  1016. */
  1017. MOCK(tor_gethostname,tor_gethostname_replacement);
  1018. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1019. MOCK(get_interface_address,get_interface_address_08080808);
  1020. prev_n_gethostname_replacement = n_gethostname_replacement;
  1021. prev_n_get_interface_address = n_get_interface_address;
  1022. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1023. &method_used,&hostname_out);
  1024. tt_want(retval == 0);
  1025. tt_want_int_op(n_gethostname_replacement, ==,
  1026. prev_n_gethostname_replacement + 1);
  1027. tt_want_int_op(n_get_interface_address, ==,
  1028. prev_n_get_interface_address + 1);
  1029. tt_want_str_op(method_used,==,"INTERFACE");
  1030. tt_want(hostname_out == NULL);
  1031. tt_assert(resolved_addr == 0x08080808);
  1032. UNMOCK(get_interface_address);
  1033. tor_free(hostname_out);
  1034. /*
  1035. * CASE 8:
  1036. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1037. * is unresolvable. We want resolve_my_address to fail if
  1038. * get_interface_address() fails.
  1039. */
  1040. MOCK(get_interface_address,get_interface_address_failure);
  1041. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1042. prev_n_gethostname_replacement = n_gethostname_replacement;
  1043. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1044. &method_used,&hostname_out);
  1045. tt_want(n_get_interface_address_failure ==
  1046. prev_n_get_interface_address_failure + 1);
  1047. tt_want(n_gethostname_replacement ==
  1048. prev_n_gethostname_replacement + 1);
  1049. tt_assert(retval == -1);
  1050. UNMOCK(get_interface_address);
  1051. tor_free(hostname_out);
  1052. /*
  1053. * CASE 9:
  1054. * Given that options->Address is NULL AND tor_lookup_hostname()
  1055. * fails AND hostname returned by gethostname() resolves
  1056. * to local IP address, we want resolve_my_address() function to
  1057. * call get_interface_address6(.,AF_INET,.) and return IP address
  1058. * the latter function has found.
  1059. */
  1060. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1061. MOCK(tor_gethostname,tor_gethostname_replacement);
  1062. MOCK(get_interface_address6,get_interface_address6_replacement);
  1063. prev_n_gethostname_replacement = n_gethostname_replacement;
  1064. prev_n_hostname_failure = n_hostname_failure;
  1065. prev_n_get_interface_address6 = n_get_interface_address6;
  1066. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1067. &method_used,&hostname_out);
  1068. tt_want(last_address6_family == AF_INET);
  1069. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1070. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1071. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1072. tt_want(retval == 0);
  1073. tt_want_str_op(method_used,==,"INTERFACE");
  1074. tt_assert(resolved_addr == 0x09090909);
  1075. UNMOCK(tor_lookup_hostname);
  1076. UNMOCK(tor_gethostname);
  1077. UNMOCK(get_interface_address6);
  1078. tor_free(hostname_out);
  1079. /*
  1080. * CASE 10: We want resolve_my_address() to fail if all of the following
  1081. * are true:
  1082. * 1. options->Address is not NULL
  1083. * 2. ... but it cannot be converted to struct in_addr by
  1084. * tor_inet_aton()
  1085. * 3. ... and tor_lookup_hostname() fails to resolve the
  1086. * options->Address
  1087. */
  1088. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1089. prev_n_hostname_failure = n_hostname_failure;
  1090. tor_free(options->Address);
  1091. options->Address = tor_strdup("some_hostname");
  1092. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1093. &method_used,&hostname_out);
  1094. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1095. tt_assert(retval == -1);
  1096. UNMOCK(tor_gethostname);
  1097. UNMOCK(tor_lookup_hostname);
  1098. tor_free(hostname_out);
  1099. /*
  1100. * CASE 11:
  1101. * Suppose the following sequence of events:
  1102. * 1. options->Address is NULL
  1103. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1104. * if running on.
  1105. * 3. Hostname from previous step cannot be converted to
  1106. * address by using tor_inet_aton() function.
  1107. * 4. However, tor_lookup_hostname() succeds in resolving the
  1108. * hostname from step 2.
  1109. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1110. * to be internal.
  1111. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1112. * IPv4
  1113. *
  1114. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1115. * and address from step 6.
  1116. */
  1117. tor_free(options->Address);
  1118. options->Address = NULL;
  1119. MOCK(tor_gethostname,tor_gethostname_replacement);
  1120. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1121. MOCK(get_interface_address6,get_interface_address6_replacement);
  1122. prev_n_gethostname_replacement = n_gethostname_replacement;
  1123. prev_n_hostname_localhost = n_hostname_localhost;
  1124. prev_n_get_interface_address6 = n_get_interface_address6;
  1125. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1126. &method_used,&hostname_out);
  1127. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1128. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1129. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1130. tt_str_op(method_used,==,"INTERFACE");
  1131. tt_assert(!hostname_out);
  1132. tt_assert(retval == 0);
  1133. /*
  1134. * CASE 11b:
  1135. * 1-5 as above.
  1136. * 6. get_interface_address6() fails.
  1137. *
  1138. * In this subcase, we want resolve_my_address() to fail.
  1139. */
  1140. UNMOCK(get_interface_address6);
  1141. MOCK(get_interface_address6,get_interface_address6_failure);
  1142. prev_n_gethostname_replacement = n_gethostname_replacement;
  1143. prev_n_hostname_localhost = n_hostname_localhost;
  1144. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1145. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1146. &method_used,&hostname_out);
  1147. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1148. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1149. tt_want(n_get_interface_address6_failure ==
  1150. prev_n_get_interface_address6_failure + 1);
  1151. tt_assert(retval == -1);
  1152. UNMOCK(tor_gethostname);
  1153. UNMOCK(tor_lookup_hostname);
  1154. UNMOCK(get_interface_address6);
  1155. /* CASE 12:
  1156. * Suppose the following happens:
  1157. * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
  1158. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1159. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1160. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1161. * internal.
  1162. *
  1163. * In this case, we want resolve_my_address() to fail.
  1164. */
  1165. tor_free(options->Address);
  1166. options->Address = NULL;
  1167. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1168. MOCK(tor_gethostname,tor_gethostname_localhost);
  1169. prev_n_gethostname_localhost = n_gethostname_localhost;
  1170. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1171. &method_used,&hostname_out);
  1172. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1173. tt_assert(retval == -1);
  1174. UNMOCK(tor_gethostname);
  1175. done:
  1176. tor_free(options->Address);
  1177. tor_free(options->DirAuthorities);
  1178. or_options_free(options);
  1179. tor_free(hostname_out);
  1180. UNMOCK(tor_gethostname);
  1181. UNMOCK(tor_lookup_hostname);
  1182. UNMOCK(get_interface_address);
  1183. UNMOCK(get_interface_address6);
  1184. UNMOCK(tor_gethostname);
  1185. }
  1186. static int n_add_default_fallback_dir_servers_known_default = 0;
  1187. /**
  1188. * This mock function is meant to replace add_default_fallback_dir_servers().
  1189. * It will parse and add one known default fallback dir server,
  1190. * which has a dir_port of 99.
  1191. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1192. * one every time this function is called.
  1193. */
  1194. static void
  1195. add_default_fallback_dir_servers_known_default(void)
  1196. {
  1197. int i;
  1198. const char *fallback[] = {
  1199. "127.0.0.1:60099 orport=9009 "
  1200. "id=0923456789012345678901234567890123456789",
  1201. NULL
  1202. };
  1203. for (i=0; fallback[i]; i++) {
  1204. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1205. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1206. fallback[i]);
  1207. }
  1208. }
  1209. n_add_default_fallback_dir_servers_known_default++;
  1210. }
  1211. static void
  1212. test_config_adding_dir_servers(void *arg)
  1213. {
  1214. (void)arg;
  1215. /* allocate options */
  1216. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  1217. /* Allocate and populate configuration lines:
  1218. *
  1219. * Use the same format as the hard-coded directories in
  1220. * add_default_trusted_dir_authorities().
  1221. * Zeroing the structure has the same effect as initialising to:
  1222. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1223. */
  1224. config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
  1225. test_dir_authority->key = tor_strdup("DirAuthority");
  1226. test_dir_authority->value = tor_strdup(
  1227. "D0 orport=9000 "
  1228. "v3ident=0023456789012345678901234567890123456789 "
  1229. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1230. );
  1231. config_line_t *test_alt_bridge_authority = tor_malloc_zero(
  1232. sizeof(config_line_t));
  1233. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1234. test_alt_bridge_authority->value = tor_strdup(
  1235. "B1 orport=9001 bridge "
  1236. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1237. );
  1238. config_line_t *test_alt_dir_authority = tor_malloc_zero(
  1239. sizeof(config_line_t));
  1240. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1241. test_alt_dir_authority->value = tor_strdup(
  1242. "A2 orport=9002 "
  1243. "v3ident=0223456789012345678901234567890123456789 "
  1244. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1245. );
  1246. /* Use the format specified in the manual page */
  1247. config_line_t *test_fallback_directory = tor_malloc_zero(
  1248. sizeof(config_line_t));
  1249. test_fallback_directory->key = tor_strdup("FallbackDir");
  1250. test_fallback_directory->value = tor_strdup(
  1251. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1252. );
  1253. /* We need to know if add_default_fallback_dir_servers is called,
  1254. * so we use a version of add_default_fallback_dir_servers that adds
  1255. * one known default fallback directory.
  1256. * There doesn't appear to be any need to test it unmocked. */
  1257. MOCK(add_default_fallback_dir_servers,
  1258. add_default_fallback_dir_servers_known_default);
  1259. /* There are 16 different cases, covering each combination of set/NULL for:
  1260. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1261. * FallbackDir.
  1262. * But validate_dir_servers() ensures that:
  1263. * "You cannot set both DirAuthority and Alternate*Authority."
  1264. * This reduces the number of cases to 10.
  1265. *
  1266. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1267. * So 1001 or case 9 is:
  1268. * DirAuthorities set,
  1269. * AlternateBridgeAuthority NULL,
  1270. * AlternateDirAuthority NULL
  1271. * FallbackDir set
  1272. * The valid cases are cases 0-9 counting using this method, as every case
  1273. * greater than or equal to 10 = 1010 is invalid.
  1274. *
  1275. * After #15642 - Disable default fallback dirs when any custom dirs set
  1276. *
  1277. * 1. Outcome: Use Set Directory Authorities
  1278. * - No Default Authorities
  1279. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1280. * if they are set
  1281. * Cases expected to yield this outcome:
  1282. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1283. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1284. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1285. *
  1286. * 2. Outcome: Use Set Bridge Authority
  1287. * - Use Default Non-Bridge Directory Authorities
  1288. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1289. * Cases expected to yield this outcome:
  1290. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1291. * AlternateBridgeAuthority is set, and
  1292. * AlternateDirAuthority is NULL)
  1293. *
  1294. * 3. Outcome: Use Set Alternate Directory Authority
  1295. * - Use Default Bridge Authorities
  1296. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1297. * Cases expected to yield this outcome:
  1298. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1299. * are both NULL, but AlternateDirAuthority is set)
  1300. *
  1301. * 4. Outcome: Use Set Custom Fallback Directory
  1302. * - Use Default Bridge & Directory Authorities
  1303. * Cases expected to yield this outcome:
  1304. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1305. * are all NULL, but FallbackDir is set)
  1306. *
  1307. * 5. Outcome: Use All Defaults
  1308. * - Use Default Bridge & Directory Authorities, and
  1309. * Default Fallback Directories
  1310. * Cases expected to yield this outcome:
  1311. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1312. * and FallbackDir are all NULL)
  1313. *
  1314. * Before #15642 but after #13163 - Stop using default authorities when both
  1315. * Alternate Dir and Bridge Authority are set
  1316. * (#13163 was committed in 0.2.6 as c1dd43d823c7)
  1317. *
  1318. * The behaviour is different in the following cases
  1319. * where FallbackDir is NULL:
  1320. * 2, 6, 8
  1321. *
  1322. * In these cases, the Default Fallback Directories are applied, even when
  1323. * DirAuthorities or AlternateDirAuthority are set.
  1324. *
  1325. * However, as the list of default fallback directories is currently empty,
  1326. * this change doesn't modify any user-visible behaviour.
  1327. */
  1328. /*
  1329. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1330. * are hard-coded into this build.
  1331. * This code makes some assumptions about the implementation.
  1332. * If they are wrong, one or more of cases 0-5 could fail.
  1333. */
  1334. int n_default_alt_bridge_authority = 0;
  1335. int n_default_alt_dir_authority = 0;
  1336. int n_default_fallback_dir = 0;
  1337. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1338. + (n_default_alt_dir_authority))
  1339. /* Pre-Count Number of Authorities of Each Type
  1340. * Use 0000: No Directory Authorities or Fallback Directories Set
  1341. */
  1342. {
  1343. /* clear fallback dirs counter */
  1344. n_add_default_fallback_dir_servers_known_default = 0;
  1345. /* clear options*/
  1346. memset(options, 0, sizeof(or_options_t));
  1347. /* clear any previous dir servers:
  1348. consider_adding_dir_servers() should do this anyway */
  1349. clear_dir_servers();
  1350. /* assign options: 0000 */
  1351. options->DirAuthorities = NULL;
  1352. options->AlternateBridgeAuthority = NULL;
  1353. options->AlternateDirAuthority = NULL;
  1354. options->FallbackDir = NULL;
  1355. /* parse options - ensure we always update by passing NULL old_options */
  1356. consider_adding_dir_servers(options, NULL);
  1357. /* check outcome */
  1358. /* we must have added the default fallback dirs */
  1359. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  1360. /* we have more fallbacks than just the authorities */
  1361. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1362. {
  1363. /* fallback_dir_servers */
  1364. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1365. /* Count Bridge Authorities */
  1366. SMARTLIST_FOREACH(fallback_servers,
  1367. dir_server_t *,
  1368. ds,
  1369. /* increment the found counter if it's a bridge auth */
  1370. n_default_alt_bridge_authority +=
  1371. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1372. 1 : 0)
  1373. );
  1374. /* If we have no default bridge authority, something has gone wrong */
  1375. tt_assert(n_default_alt_bridge_authority >= 1);
  1376. /* Count v3 Authorities */
  1377. SMARTLIST_FOREACH(fallback_servers,
  1378. dir_server_t *,
  1379. ds,
  1380. /* increment found counter if it's a v3 auth */
  1381. n_default_alt_dir_authority +=
  1382. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1383. 1 : 0)
  1384. );
  1385. /* If we have no default authorities, something has gone really wrong */
  1386. tt_assert(n_default_alt_dir_authority >= 1);
  1387. /* Calculate Fallback Directory Count */
  1388. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1389. n_default_alt_bridge_authority -
  1390. n_default_alt_dir_authority);
  1391. /* If we have a negative count, something has gone really wrong,
  1392. * or some authorities aren't being added as fallback directories.
  1393. * (networkstatus_consensus_can_use_extra_fallbacks depends on all
  1394. * authorities being fallback directories.) */
  1395. tt_assert(n_default_fallback_dir >= 0);
  1396. }
  1397. }
  1398. /*
  1399. * 1. Outcome: Use Set Directory Authorities
  1400. * - No Default Authorities
  1401. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1402. * if they are set
  1403. * Cases expected to yield this outcome:
  1404. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1405. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1406. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1407. */
  1408. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1409. AlternateDirAuthority Not Set, FallbackDir Set */
  1410. {
  1411. /* clear fallback dirs counter */
  1412. n_add_default_fallback_dir_servers_known_default = 0;
  1413. /* clear options*/
  1414. memset(options, 0, sizeof(or_options_t));
  1415. /* clear any previous dir servers:
  1416. consider_adding_dir_servers() should do this anyway */
  1417. clear_dir_servers();
  1418. /* assign options: 1001 */
  1419. options->DirAuthorities = test_dir_authority;
  1420. options->AlternateBridgeAuthority = NULL;
  1421. options->AlternateDirAuthority = NULL;
  1422. options->FallbackDir = test_fallback_directory;
  1423. /* parse options - ensure we always update by passing NULL old_options */
  1424. consider_adding_dir_servers(options, NULL);
  1425. /* check outcome */
  1426. /* we must not have added the default fallback dirs */
  1427. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1428. /* we have more fallbacks than just the authorities */
  1429. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1430. {
  1431. /* trusted_dir_servers */
  1432. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1433. /* D0, (No B1), (No A2) */
  1434. tt_assert(smartlist_len(dir_servers) == 1);
  1435. /* DirAuthority - D0 - dir_port: 60090 */
  1436. int found_D0 = 0;
  1437. SMARTLIST_FOREACH(dir_servers,
  1438. dir_server_t *,
  1439. ds,
  1440. /* increment the found counter if dir_port matches */
  1441. found_D0 +=
  1442. (ds->dir_port == 60090 ?
  1443. 1 : 0)
  1444. );
  1445. tt_assert(found_D0 == 1);
  1446. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1447. int found_B1 = 0;
  1448. SMARTLIST_FOREACH(dir_servers,
  1449. dir_server_t *,
  1450. ds,
  1451. /* increment the found counter if dir_port matches */
  1452. found_B1 +=
  1453. (ds->dir_port == 60091 ?
  1454. 1 : 0)
  1455. );
  1456. tt_assert(found_B1 == 0);
  1457. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1458. int found_A2 = 0;
  1459. SMARTLIST_FOREACH(dir_servers,
  1460. dir_server_t *,
  1461. ds,
  1462. /* increment the found counter if dir_port matches */
  1463. found_A2 +=
  1464. (ds->dir_port == 60092 ?
  1465. 1 : 0)
  1466. );
  1467. tt_assert(found_A2 == 0);
  1468. }
  1469. {
  1470. /* fallback_dir_servers */
  1471. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1472. /* D0, (No B1), (No A2), Custom Fallback */
  1473. tt_assert(smartlist_len(fallback_servers) == 2);
  1474. /* DirAuthority - D0 - dir_port: 60090 */
  1475. int found_D0 = 0;
  1476. SMARTLIST_FOREACH(fallback_servers,
  1477. dir_server_t *,
  1478. ds,
  1479. /* increment the found counter if dir_port matches */
  1480. found_D0 +=
  1481. (ds->dir_port == 60090 ?
  1482. 1 : 0)
  1483. );
  1484. tt_assert(found_D0 == 1);
  1485. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1486. int found_B1 = 0;
  1487. SMARTLIST_FOREACH(fallback_servers,
  1488. dir_server_t *,
  1489. ds,
  1490. /* increment the found counter if dir_port matches */
  1491. found_B1 +=
  1492. (ds->dir_port == 60091 ?
  1493. 1 : 0)
  1494. );
  1495. tt_assert(found_B1 == 0);
  1496. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1497. int found_A2 = 0;
  1498. SMARTLIST_FOREACH(fallback_servers,
  1499. dir_server_t *,
  1500. ds,
  1501. /* increment the found counter if dir_port matches */
  1502. found_A2 +=
  1503. (ds->dir_port == 60092 ?
  1504. 1 : 0)
  1505. );
  1506. tt_assert(found_A2 == 0);
  1507. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1508. int found_non_default_fallback = 0;
  1509. SMARTLIST_FOREACH(fallback_servers,
  1510. dir_server_t *,
  1511. ds,
  1512. /* increment the found counter if dir_port matches */
  1513. found_non_default_fallback +=
  1514. (ds->dir_port == 60093 ?
  1515. 1 : 0)
  1516. );
  1517. tt_assert(found_non_default_fallback == 1);
  1518. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1519. int found_default_fallback = 0;
  1520. SMARTLIST_FOREACH(fallback_servers,
  1521. dir_server_t *,
  1522. ds,
  1523. /* increment the found counter if dir_port matches */
  1524. found_default_fallback +=
  1525. (ds->dir_port == 60099 ?
  1526. 1 : 0)
  1527. );
  1528. tt_assert(found_default_fallback == 0);
  1529. }
  1530. }
  1531. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1532. {
  1533. /* clear fallback dirs counter */
  1534. n_add_default_fallback_dir_servers_known_default = 0;
  1535. /* clear options*/
  1536. memset(options, 0, sizeof(or_options_t));
  1537. /* clear any previous dir servers:
  1538. consider_adding_dir_servers() should do this anyway */
  1539. clear_dir_servers();
  1540. /* assign options: 1000 */
  1541. options->DirAuthorities = test_dir_authority;
  1542. options->AlternateBridgeAuthority = NULL;
  1543. options->AlternateDirAuthority = NULL;
  1544. options->FallbackDir = NULL;
  1545. /* parse options - ensure we always update by passing NULL old_options */
  1546. consider_adding_dir_servers(options, NULL);
  1547. /* check outcome */
  1548. /* we must not have added the default fallback dirs */
  1549. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1550. /* we just have the authorities */
  1551. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1552. {
  1553. /* trusted_dir_servers */
  1554. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1555. /* D0, (No B1), (No A2) */
  1556. tt_assert(smartlist_len(dir_servers) == 1);
  1557. /* DirAuthority - D0 - dir_port: 60090 */
  1558. int found_D0 = 0;
  1559. SMARTLIST_FOREACH(dir_servers,
  1560. dir_server_t *,
  1561. ds,
  1562. /* increment the found counter if dir_port matches */
  1563. found_D0 +=
  1564. (ds->dir_port == 60090 ?
  1565. 1 : 0)
  1566. );
  1567. tt_assert(found_D0 == 1);
  1568. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1569. int found_B1 = 0;
  1570. SMARTLIST_FOREACH(dir_servers,
  1571. dir_server_t *,
  1572. ds,
  1573. /* increment the found counter if dir_port matches */
  1574. found_B1 +=
  1575. (ds->dir_port == 60091 ?
  1576. 1 : 0)
  1577. );
  1578. tt_assert(found_B1 == 0);
  1579. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1580. int found_A2 = 0;
  1581. SMARTLIST_FOREACH(dir_servers,
  1582. dir_server_t *,
  1583. ds,
  1584. /* increment the found counter if dir_port matches */
  1585. found_A2 +=
  1586. (ds->dir_port == 60092 ?
  1587. 1 : 0)
  1588. );
  1589. tt_assert(found_A2 == 0);
  1590. }
  1591. {
  1592. /* fallback_dir_servers */
  1593. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1594. /* D0, (No B1), (No A2), (No Fallback) */
  1595. tt_assert(smartlist_len(fallback_servers) == 1);
  1596. /* DirAuthority - D0 - dir_port: 60090 */
  1597. int found_D0 = 0;
  1598. SMARTLIST_FOREACH(fallback_servers,
  1599. dir_server_t *,
  1600. ds,
  1601. /* increment the found counter if dir_port matches */
  1602. found_D0 +=
  1603. (ds->dir_port == 60090 ?
  1604. 1 : 0)
  1605. );
  1606. tt_assert(found_D0 == 1);
  1607. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1608. int found_B1 = 0;
  1609. SMARTLIST_FOREACH(fallback_servers,
  1610. dir_server_t *,
  1611. ds,
  1612. /* increment the found counter if dir_port matches */
  1613. found_B1 +=
  1614. (ds->dir_port == 60091 ?
  1615. 1 : 0)
  1616. );
  1617. tt_assert(found_B1 == 0);
  1618. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1619. int found_A2 = 0;
  1620. SMARTLIST_FOREACH(fallback_servers,
  1621. dir_server_t *,
  1622. ds,
  1623. /* increment the found counter if dir_port matches */
  1624. found_A2 +=
  1625. (ds->dir_port == 60092 ?
  1626. 1 : 0)
  1627. );
  1628. tt_assert(found_A2 == 0);
  1629. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1630. int found_non_default_fallback = 0;
  1631. SMARTLIST_FOREACH(fallback_servers,
  1632. dir_server_t *,
  1633. ds,
  1634. /* increment the found counter if dir_port matches */
  1635. found_non_default_fallback +=
  1636. (ds->dir_port == 60093 ?
  1637. 1 : 0)
  1638. );
  1639. tt_assert(found_non_default_fallback == 0);
  1640. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1641. int found_default_fallback = 0;
  1642. SMARTLIST_FOREACH(fallback_servers,
  1643. dir_server_t *,
  1644. ds,
  1645. /* increment the found counter if dir_port matches */
  1646. found_default_fallback +=
  1647. (ds->dir_port == 60099 ?
  1648. 1 : 0)
  1649. );
  1650. tt_assert(found_default_fallback == 0);
  1651. }
  1652. }
  1653. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1654. {
  1655. /* clear fallback dirs counter */
  1656. n_add_default_fallback_dir_servers_known_default = 0;
  1657. /* clear options*/
  1658. memset(options, 0, sizeof(or_options_t));
  1659. /* clear any previous dir servers:
  1660. consider_adding_dir_servers() should do this anyway */
  1661. clear_dir_servers();
  1662. /* assign options: 0111 */
  1663. options->DirAuthorities = NULL;
  1664. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1665. options->AlternateDirAuthority = test_alt_dir_authority;
  1666. options->FallbackDir = test_fallback_directory;
  1667. /* parse options - ensure we always update by passing NULL old_options */
  1668. consider_adding_dir_servers(options, NULL);
  1669. /* check outcome */
  1670. /* we must not have added the default fallback dirs */
  1671. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1672. /* we have more fallbacks than just the authorities */
  1673. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1674. {
  1675. /* trusted_dir_servers */
  1676. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1677. /* (No D0), B1, A2 */
  1678. tt_assert(smartlist_len(dir_servers) == 2);
  1679. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1680. int found_D0 = 0;
  1681. SMARTLIST_FOREACH(dir_servers,
  1682. dir_server_t *,
  1683. ds,
  1684. /* increment the found counter if dir_port matches */
  1685. found_D0 +=
  1686. (ds->dir_port == 60090 ?
  1687. 1 : 0)
  1688. );
  1689. tt_assert(found_D0 == 0);
  1690. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1691. int found_B1 = 0;
  1692. SMARTLIST_FOREACH(dir_servers,
  1693. dir_server_t *,
  1694. ds,
  1695. /* increment the found counter if dir_port matches */
  1696. found_B1 +=
  1697. (ds->dir_port == 60091 ?
  1698. 1 : 0)
  1699. );
  1700. tt_assert(found_B1 == 1);
  1701. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1702. int found_A2 = 0;
  1703. SMARTLIST_FOREACH(dir_servers,
  1704. dir_server_t *,
  1705. ds,
  1706. /* increment the found counter if dir_port matches */
  1707. found_A2 +=
  1708. (ds->dir_port == 60092 ?
  1709. 1 : 0)
  1710. );
  1711. tt_assert(found_A2 == 1);
  1712. }
  1713. {
  1714. /* fallback_dir_servers */
  1715. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1716. /* (No D0), B1, A2, Custom Fallback */
  1717. tt_assert(smartlist_len(fallback_servers) == 3);
  1718. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1719. int found_D0 = 0;
  1720. SMARTLIST_FOREACH(fallback_servers,
  1721. dir_server_t *,
  1722. ds,
  1723. /* increment the found counter if dir_port matches */
  1724. found_D0 +=
  1725. (ds->dir_port == 60090 ?
  1726. 1 : 0)
  1727. );
  1728. tt_assert(found_D0 == 0);
  1729. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1730. int found_B1 = 0;
  1731. SMARTLIST_FOREACH(fallback_servers,
  1732. dir_server_t *,
  1733. ds,
  1734. /* increment the found counter if dir_port matches */
  1735. found_B1 +=
  1736. (ds->dir_port == 60091 ?
  1737. 1 : 0)
  1738. );
  1739. tt_assert(found_B1 == 1);
  1740. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1741. int found_A2 = 0;
  1742. SMARTLIST_FOREACH(fallback_servers,
  1743. dir_server_t *,
  1744. ds,
  1745. /* increment the found counter if dir_port matches */
  1746. found_A2 +=
  1747. (ds->dir_port == 60092 ?
  1748. 1 : 0)
  1749. );
  1750. tt_assert(found_A2 == 1);
  1751. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1752. int found_non_default_fallback = 0;
  1753. SMARTLIST_FOREACH(fallback_servers,
  1754. dir_server_t *,
  1755. ds,
  1756. /* increment the found counter if dir_port matches */
  1757. found_non_default_fallback +=
  1758. (ds->dir_port == 60093 ?
  1759. 1 : 0)
  1760. );
  1761. tt_assert(found_non_default_fallback == 1);
  1762. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1763. int found_default_fallback = 0;
  1764. SMARTLIST_FOREACH(fallback_servers,
  1765. dir_server_t *,
  1766. ds,
  1767. /* increment the found counter if dir_port matches */
  1768. found_default_fallback +=
  1769. (ds->dir_port == 60099 ?
  1770. 1 : 0)
  1771. );
  1772. tt_assert(found_default_fallback == 0);
  1773. }
  1774. }
  1775. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  1776. AlternateDirAuthority Set, FallbackDir Not Set */
  1777. {
  1778. /* clear fallback dirs counter */
  1779. n_add_default_fallback_dir_servers_known_default = 0;
  1780. /* clear options*/
  1781. memset(options, 0, sizeof(or_options_t));
  1782. /* clear any previous dir servers:
  1783. consider_adding_dir_servers() should do this anyway */
  1784. clear_dir_servers();
  1785. /* assign options: 0110 */
  1786. options->DirAuthorities = NULL;
  1787. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1788. options->AlternateDirAuthority = test_alt_dir_authority;
  1789. options->FallbackDir = NULL;
  1790. /* parse options - ensure we always update by passing NULL old_options */
  1791. consider_adding_dir_servers(options, NULL);
  1792. /* check outcome */
  1793. /* we must not have added the default fallback dirs */
  1794. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1795. /* we have more fallbacks than just the authorities */
  1796. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1797. {
  1798. /* trusted_dir_servers */
  1799. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1800. /* (No D0), B1, A2 */
  1801. tt_assert(smartlist_len(dir_servers) == 2);
  1802. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1803. int found_D0 = 0;
  1804. SMARTLIST_FOREACH(dir_servers,
  1805. dir_server_t *,
  1806. ds,
  1807. /* increment the found counter if dir_port matches */
  1808. found_D0 +=
  1809. (ds->dir_port == 60090 ?
  1810. 1 : 0)
  1811. );
  1812. tt_assert(found_D0 == 0);
  1813. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1814. int found_B1 = 0;
  1815. SMARTLIST_FOREACH(dir_servers,
  1816. dir_server_t *,
  1817. ds,
  1818. /* increment the found counter if dir_port matches */
  1819. found_B1 +=
  1820. (ds->dir_port == 60091 ?
  1821. 1 : 0)
  1822. );
  1823. tt_assert(found_B1 == 1);
  1824. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1825. int found_A2 = 0;
  1826. SMARTLIST_FOREACH(dir_servers,
  1827. dir_server_t *,
  1828. ds,
  1829. /* increment the found counter if dir_port matches */
  1830. found_A2 +=
  1831. (ds->dir_port == 60092 ?
  1832. 1 : 0)
  1833. );
  1834. tt_assert(found_A2 == 1);
  1835. }
  1836. {
  1837. /* fallback_dir_servers */
  1838. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1839. /* (No D0), B1, A2, (No Fallback) */
  1840. tt_assert(smartlist_len(fallback_servers) == 2);
  1841. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1842. int found_D0 = 0;
  1843. SMARTLIST_FOREACH(fallback_servers,
  1844. dir_server_t *,
  1845. ds,
  1846. /* increment the found counter if dir_port matches */
  1847. found_D0 +=
  1848. (ds->dir_port == 60090 ?
  1849. 1 : 0)
  1850. );
  1851. tt_assert(found_D0 == 0);
  1852. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1853. int found_B1 = 0;
  1854. SMARTLIST_FOREACH(fallback_servers,
  1855. dir_server_t *,
  1856. ds,
  1857. /* increment the found counter if dir_port matches */
  1858. found_B1 +=
  1859. (ds->dir_port == 60091 ?
  1860. 1 : 0)
  1861. );
  1862. tt_assert(found_B1 == 1);
  1863. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1864. int found_A2 = 0;
  1865. SMARTLIST_FOREACH(fallback_servers,
  1866. dir_server_t *,
  1867. ds,
  1868. /* increment the found counter if dir_port matches */
  1869. found_A2 +=
  1870. (ds->dir_port == 60092 ?
  1871. 1 : 0)
  1872. );
  1873. tt_assert(found_A2 == 1);
  1874. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1875. int found_non_default_fallback = 0;
  1876. SMARTLIST_FOREACH(fallback_servers,
  1877. dir_server_t *,
  1878. ds,
  1879. /* increment the found counter if dir_port matches */
  1880. found_non_default_fallback +=
  1881. (ds->dir_port == 60093 ?
  1882. 1 : 0)
  1883. );
  1884. tt_assert(found_non_default_fallback == 0);
  1885. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1886. int found_default_fallback = 0;
  1887. SMARTLIST_FOREACH(fallback_servers,
  1888. dir_server_t *,
  1889. ds,
  1890. /* increment the found counter if dir_port matches */
  1891. found_default_fallback +=
  1892. (ds->dir_port == 60099 ?
  1893. 1 : 0)
  1894. );
  1895. tt_assert(found_default_fallback == 0);
  1896. }
  1897. }
  1898. /*
  1899. 2. Outcome: Use Set Bridge Authority
  1900. - Use Default Non-Bridge Directory Authorities
  1901. - Use FallbackDir if it is set, otherwise use default FallbackDir
  1902. Cases expected to yield this outcome:
  1903. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1904. AlternateBridgeAuthority is set, and
  1905. AlternateDirAuthority is NULL)
  1906. */
  1907. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  1908. AlternateDirAuthority Not Set, FallbackDir Set */
  1909. {
  1910. /* clear fallback dirs counter */
  1911. n_add_default_fallback_dir_servers_known_default = 0;
  1912. /* clear options*/
  1913. memset(options, 0, sizeof(or_options_t));
  1914. /* clear any previous dir servers:
  1915. consider_adding_dir_servers() should do this anyway */
  1916. clear_dir_servers();
  1917. /* assign options: 0101 */
  1918. options->DirAuthorities = NULL;
  1919. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1920. options->AlternateDirAuthority = NULL;
  1921. options->FallbackDir = test_fallback_directory;
  1922. /* parse options - ensure we always update by passing NULL old_options */
  1923. consider_adding_dir_servers(options, NULL);
  1924. /* check outcome */
  1925. /* we must not have added the default fallback dirs */
  1926. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1927. /* we have more fallbacks than just the authorities */
  1928. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1929. {
  1930. /* trusted_dir_servers */
  1931. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1932. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  1933. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  1934. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  1935. int found_D0 = 0;
  1936. SMARTLIST_FOREACH(dir_servers,
  1937. dir_server_t *,
  1938. ds,
  1939. /* increment the found counter if dir_port matches */
  1940. found_D0 +=
  1941. (ds->dir_port == 60090 ?
  1942. 1 : 0)
  1943. );
  1944. tt_assert(found_D0 == 0);
  1945. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1946. int found_B1 = 0;
  1947. SMARTLIST_FOREACH(dir_servers,
  1948. dir_server_t *,
  1949. ds,
  1950. /* increment the found counter if dir_port matches */
  1951. found_B1 +=
  1952. (ds->dir_port == 60091 ?
  1953. 1 : 0)
  1954. );
  1955. tt_assert(found_B1 == 1);
  1956. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1957. int found_A2 = 0;
  1958. SMARTLIST_FOREACH(dir_servers,
  1959. dir_server_t *,
  1960. ds,
  1961. /* increment the found counter if dir_port matches */
  1962. found_A2 +=
  1963. (ds->dir_port == 60092 ?
  1964. 1 : 0)
  1965. );
  1966. tt_assert(found_A2 == 0);
  1967. /* There's no easy way of checking that we have included all the
  1968. * default v3 non-Bridge directory authorities, so let's assume that
  1969. * if the total count above is correct, we have the right ones.
  1970. */
  1971. }
  1972. {
  1973. /* fallback_dir_servers */
  1974. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1975. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  1976. * Custom Fallback */
  1977. tt_assert(smartlist_len(fallback_servers) ==
  1978. 2 + n_default_alt_dir_authority);
  1979. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  1980. int found_D0 = 0;
  1981. SMARTLIST_FOREACH(fallback_servers,
  1982. dir_server_t *,
  1983. ds,
  1984. /* increment the found counter if dir_port matches */
  1985. found_D0 +=
  1986. (ds->dir_port == 60090 ?
  1987. 1 : 0)
  1988. );
  1989. tt_assert(found_D0 == 0);
  1990. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1991. int found_B1 = 0;
  1992. SMARTLIST_FOREACH(fallback_servers,
  1993. dir_server_t *,
  1994. ds,
  1995. /* increment the found counter if dir_port matches */
  1996. found_B1 +=
  1997. (ds->dir_port == 60091 ?
  1998. 1 : 0)
  1999. );
  2000. tt_assert(found_B1 == 1);
  2001. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2002. int found_A2 = 0;
  2003. SMARTLIST_FOREACH(fallback_servers,
  2004. dir_server_t *,
  2005. ds,
  2006. /* increment the found counter if dir_port matches */
  2007. found_A2 +=
  2008. (ds->dir_port == 60092 ?
  2009. 1 : 0)
  2010. );
  2011. tt_assert(found_A2 == 0);
  2012. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2013. int found_non_default_fallback = 0;
  2014. SMARTLIST_FOREACH(fallback_servers,
  2015. dir_server_t *,
  2016. ds,
  2017. /* increment the found counter if dir_port matches */
  2018. found_non_default_fallback +=
  2019. (ds->dir_port == 60093 ?
  2020. 1 : 0)
  2021. );
  2022. tt_assert(found_non_default_fallback == 1);
  2023. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2024. int found_default_fallback = 0;
  2025. SMARTLIST_FOREACH(fallback_servers,
  2026. dir_server_t *,
  2027. ds,
  2028. /* increment the found counter if dir_port matches */
  2029. found_default_fallback +=
  2030. (ds->dir_port == 60099 ?
  2031. 1 : 0)
  2032. );
  2033. tt_assert(found_default_fallback == 0);
  2034. /* There's no easy way of checking that we have included all the
  2035. * default v3 non-Bridge directory authorities, so let's assume that
  2036. * if the total count above is correct, we have the right ones.
  2037. */
  2038. }
  2039. }
  2040. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2041. AlternateDirAuthority & FallbackDir Not Set */
  2042. {
  2043. /* clear fallback dirs counter */
  2044. n_add_default_fallback_dir_servers_known_default = 0;
  2045. /* clear options*/
  2046. memset(options, 0, sizeof(or_options_t));
  2047. /* clear any previous dir servers:
  2048. consider_adding_dir_servers() should do this anyway */
  2049. clear_dir_servers();
  2050. /* assign options: 0100 */
  2051. options->DirAuthorities = NULL;
  2052. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2053. options->AlternateDirAuthority = NULL;
  2054. options->FallbackDir = NULL;
  2055. /* parse options - ensure we always update by passing NULL old_options */
  2056. consider_adding_dir_servers(options, NULL);
  2057. /* check outcome */
  2058. /* we must have added the default fallback dirs */
  2059. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2060. /* we have more fallbacks than just the authorities */
  2061. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2062. {
  2063. /* trusted_dir_servers */
  2064. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2065. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2066. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2067. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2068. int found_D0 = 0;
  2069. SMARTLIST_FOREACH(dir_servers,
  2070. dir_server_t *,
  2071. ds,
  2072. /* increment the found counter if dir_port matches */
  2073. found_D0 +=
  2074. (ds->dir_port == 60090 ?
  2075. 1 : 0)
  2076. );
  2077. tt_assert(found_D0 == 0);
  2078. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2079. int found_B1 = 0;
  2080. SMARTLIST_FOREACH(dir_servers,
  2081. dir_server_t *,
  2082. ds,
  2083. /* increment the found counter if dir_port matches */
  2084. found_B1 +=
  2085. (ds->dir_port == 60091 ?
  2086. 1 : 0)
  2087. );
  2088. tt_assert(found_B1 == 1);
  2089. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2090. int found_A2 = 0;
  2091. SMARTLIST_FOREACH(dir_servers,
  2092. dir_server_t *,
  2093. ds,
  2094. /* increment the found counter if dir_port matches */
  2095. found_A2 +=
  2096. (ds->dir_port == 60092 ?
  2097. 1 : 0)
  2098. );
  2099. tt_assert(found_A2 == 0);
  2100. /* There's no easy way of checking that we have included all the
  2101. * default v3 non-Bridge directory authorities, so let's assume that
  2102. * if the total count above is correct, we have the right ones.
  2103. */
  2104. }
  2105. {
  2106. /* fallback_dir_servers */
  2107. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2108. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2109. * Default Fallback */
  2110. tt_assert(smartlist_len(fallback_servers) ==
  2111. 2 + n_default_alt_dir_authority);
  2112. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2113. int found_D0 = 0;
  2114. SMARTLIST_FOREACH(fallback_servers,
  2115. dir_server_t *,
  2116. ds,
  2117. /* increment the found counter if dir_port matches */
  2118. found_D0 +=
  2119. (ds->dir_port == 60090 ?
  2120. 1 : 0)
  2121. );
  2122. tt_assert(found_D0 == 0);
  2123. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2124. int found_B1 = 0;
  2125. SMARTLIST_FOREACH(fallback_servers,
  2126. dir_server_t *,
  2127. ds,
  2128. /* increment the found counter if dir_port matches */
  2129. found_B1 +=
  2130. (ds->dir_port == 60091 ?
  2131. 1 : 0)
  2132. );
  2133. tt_assert(found_B1 == 1);
  2134. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2135. int found_A2 = 0;
  2136. SMARTLIST_FOREACH(fallback_servers,
  2137. dir_server_t *,
  2138. ds,
  2139. /* increment the found counter if dir_port matches */
  2140. found_A2 +=
  2141. (ds->dir_port == 60092 ?
  2142. 1 : 0)
  2143. );
  2144. tt_assert(found_A2 == 0);
  2145. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2146. int found_non_default_fallback = 0;
  2147. SMARTLIST_FOREACH(fallback_servers,
  2148. dir_server_t *,
  2149. ds,
  2150. /* increment the found counter if dir_port matches */
  2151. found_non_default_fallback +=
  2152. (ds->dir_port == 60093 ?
  2153. 1 : 0)
  2154. );
  2155. tt_assert(found_non_default_fallback == 0);
  2156. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2157. int found_default_fallback = 0;
  2158. SMARTLIST_FOREACH(fallback_servers,
  2159. dir_server_t *,
  2160. ds,
  2161. /* increment the found counter if dir_port matches */
  2162. found_default_fallback +=
  2163. (ds->dir_port == 60099 ?
  2164. 1 : 0)
  2165. );
  2166. tt_assert(found_default_fallback == 1);
  2167. /* There's no easy way of checking that we have included all the
  2168. * default v3 non-Bridge directory authorities, so let's assume that
  2169. * if the total count above is correct, we have the right ones.
  2170. */
  2171. }
  2172. }
  2173. /*
  2174. 3. Outcome: Use Set Alternate Directory Authority
  2175. - Use Default Bridge Authorities
  2176. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2177. Cases expected to yield this outcome:
  2178. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2179. are both NULL, but AlternateDirAuthority is set)
  2180. */
  2181. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2182. AlternateDirAuthority & FallbackDir Set */
  2183. {
  2184. /* clear fallback dirs counter */
  2185. n_add_default_fallback_dir_servers_known_default = 0;
  2186. /* clear options*/
  2187. memset(options, 0, sizeof(or_options_t));
  2188. /* clear any previous dir servers:
  2189. consider_adding_dir_servers() should do this anyway */
  2190. clear_dir_servers();
  2191. /* assign options: 0011 */
  2192. options->DirAuthorities = NULL;
  2193. options->AlternateBridgeAuthority = NULL;
  2194. options->AlternateDirAuthority = test_alt_dir_authority;
  2195. options->FallbackDir = test_fallback_directory;
  2196. /* parse options - ensure we always update by passing NULL old_options */
  2197. consider_adding_dir_servers(options, NULL);
  2198. /* check outcome */
  2199. /* we must not have added the default fallback dirs */
  2200. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2201. /* we have more fallbacks than just the authorities */
  2202. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2203. {
  2204. /* trusted_dir_servers */
  2205. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2206. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2207. tt_assert(smartlist_len(dir_servers) ==
  2208. 1 + n_default_alt_bridge_authority);
  2209. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2210. int found_D0 = 0;
  2211. SMARTLIST_FOREACH(dir_servers,
  2212. dir_server_t *,
  2213. ds,
  2214. /* increment the found counter if dir_port matches */
  2215. found_D0 +=
  2216. (ds->dir_port == 60090 ?
  2217. 1 : 0)
  2218. );
  2219. tt_assert(found_D0 == 0);
  2220. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2221. int found_B1 = 0;
  2222. SMARTLIST_FOREACH(dir_servers,
  2223. dir_server_t *,
  2224. ds,
  2225. /* increment the found counter if dir_port matches */
  2226. found_B1 +=
  2227. (ds->dir_port == 60091 ?
  2228. 1 : 0)
  2229. );
  2230. tt_assert(found_B1 == 0);
  2231. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2232. int found_A2 = 0;
  2233. SMARTLIST_FOREACH(dir_servers,
  2234. dir_server_t *,
  2235. ds,
  2236. /* increment the found counter if dir_port matches */
  2237. found_A2 +=
  2238. (ds->dir_port == 60092 ?
  2239. 1 : 0)
  2240. );
  2241. tt_assert(found_A2 == 1);
  2242. /* There's no easy way of checking that we have included all the
  2243. * default Bridge authorities (except for hard-coding tonga's details),
  2244. * so let's assume that if the total count above is correct,
  2245. * we have the right ones.
  2246. */
  2247. }
  2248. {
  2249. /* fallback_dir_servers */
  2250. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2251. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2252. * Custom Fallback Directory, (No Default Fallback Directories) */
  2253. tt_assert(smartlist_len(fallback_servers) ==
  2254. 2 + n_default_alt_bridge_authority);
  2255. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2256. int found_D0 = 0;
  2257. SMARTLIST_FOREACH(fallback_servers,
  2258. dir_server_t *,
  2259. ds,
  2260. /* increment the found counter if dir_port matches */
  2261. found_D0 +=
  2262. (ds->dir_port == 60090 ?
  2263. 1 : 0)
  2264. );
  2265. tt_assert(found_D0 == 0);
  2266. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2267. int found_B1 = 0;
  2268. SMARTLIST_FOREACH(fallback_servers,
  2269. dir_server_t *,
  2270. ds,
  2271. /* increment the found counter if dir_port matches */
  2272. found_B1 +=
  2273. (ds->dir_port == 60091 ?
  2274. 1 : 0)
  2275. );
  2276. tt_assert(found_B1 == 0);
  2277. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2278. int found_A2 = 0;
  2279. SMARTLIST_FOREACH(fallback_servers,
  2280. dir_server_t *,
  2281. ds,
  2282. /* increment the found counter if dir_port matches */
  2283. found_A2 +=
  2284. (ds->dir_port == 60092 ?
  2285. 1 : 0)
  2286. );
  2287. tt_assert(found_A2 == 1);
  2288. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2289. int found_non_default_fallback = 0;
  2290. SMARTLIST_FOREACH(fallback_servers,
  2291. dir_server_t *,
  2292. ds,
  2293. /* increment the found counter if dir_port matches */
  2294. found_non_default_fallback +=
  2295. (ds->dir_port == 60093 ?
  2296. 1 : 0)
  2297. );
  2298. tt_assert(found_non_default_fallback == 1);
  2299. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2300. int found_default_fallback = 0;
  2301. SMARTLIST_FOREACH(fallback_servers,
  2302. dir_server_t *,
  2303. ds,
  2304. /* increment the found counter if dir_port matches */
  2305. found_default_fallback +=
  2306. (ds->dir_port == 60099 ?
  2307. 1 : 0)
  2308. );
  2309. tt_assert(found_default_fallback == 0);
  2310. /* There's no easy way of checking that we have included all the
  2311. * default Bridge authorities (except for hard-coding tonga's details),
  2312. * so let's assume that if the total count above is correct,
  2313. * we have the right ones.
  2314. */
  2315. }
  2316. }
  2317. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2318. AlternateDirAuthority Set, FallbackDir Not Set */
  2319. {
  2320. /* clear fallback dirs counter */
  2321. n_add_default_fallback_dir_servers_known_default = 0;
  2322. /* clear options*/
  2323. memset(options, 0, sizeof(or_options_t));
  2324. /* clear any previous dir servers:
  2325. consider_adding_dir_servers() should do this anyway */
  2326. clear_dir_servers();
  2327. /* assign options: 0010 */
  2328. options->DirAuthorities = NULL;
  2329. options->AlternateBridgeAuthority = NULL;
  2330. options->AlternateDirAuthority = test_alt_dir_authority;
  2331. options->FallbackDir = NULL;
  2332. /* parse options - ensure we always update by passing NULL old_options */
  2333. consider_adding_dir_servers(options, NULL);
  2334. /* check outcome */
  2335. /* we must not have added the default fallback dirs */
  2336. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2337. /* we just have the authorities */
  2338. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2339. {
  2340. /* trusted_dir_servers */
  2341. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2342. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2343. * No Default or Custom Fallback Directories */
  2344. tt_assert(smartlist_len(dir_servers) ==
  2345. 1 + n_default_alt_bridge_authority);
  2346. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2347. int found_D0 = 0;
  2348. SMARTLIST_FOREACH(dir_servers,
  2349. dir_server_t *,
  2350. ds,
  2351. /* increment the found counter if dir_port matches */
  2352. found_D0 +=
  2353. (ds->dir_port == 60090 ?
  2354. 1 : 0)
  2355. );
  2356. tt_assert(found_D0 == 0);
  2357. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2358. int found_B1 = 0;
  2359. SMARTLIST_FOREACH(dir_servers,
  2360. dir_server_t *,
  2361. ds,
  2362. /* increment the found counter if dir_port matches */
  2363. found_B1 +=
  2364. (ds->dir_port == 60091 ?
  2365. 1 : 0)
  2366. );
  2367. tt_assert(found_B1 == 0);
  2368. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2369. int found_A2 = 0;
  2370. SMARTLIST_FOREACH(dir_servers,
  2371. dir_server_t *,
  2372. ds,
  2373. /* increment the found counter if dir_port matches */
  2374. found_A2 +=
  2375. (ds->dir_port == 60092 ?
  2376. 1 : 0)
  2377. );
  2378. tt_assert(found_A2 == 1);
  2379. /* There's no easy way of checking that we have included all the
  2380. * default Bridge authorities (except for hard-coding tonga's details),
  2381. * so let's assume that if the total count above is correct,
  2382. * we have the right ones.
  2383. */
  2384. }
  2385. {
  2386. /* fallback_dir_servers */
  2387. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2388. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2389. * No Custom or Default Fallback Directories */
  2390. tt_assert(smartlist_len(fallback_servers) ==
  2391. 1 + n_default_alt_bridge_authority);
  2392. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2393. int found_D0 = 0;
  2394. SMARTLIST_FOREACH(fallback_servers,
  2395. dir_server_t *,
  2396. ds,
  2397. /* increment the found counter if dir_port matches */
  2398. found_D0 +=
  2399. (ds->dir_port == 60090 ?
  2400. 1 : 0)
  2401. );
  2402. tt_assert(found_D0 == 0);
  2403. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2404. int found_B1 = 0;
  2405. SMARTLIST_FOREACH(fallback_servers,
  2406. dir_server_t *,
  2407. ds,
  2408. /* increment the found counter if dir_port matches */
  2409. found_B1 +=
  2410. (ds->dir_port == 60091 ?
  2411. 1 : 0)
  2412. );
  2413. tt_assert(found_B1 == 0);
  2414. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2415. int found_A2 = 0;
  2416. SMARTLIST_FOREACH(fallback_servers,
  2417. dir_server_t *,
  2418. ds,
  2419. /* increment the found counter if dir_port matches */
  2420. found_A2 +=
  2421. (ds->dir_port == 60092 ?
  2422. 1 : 0)
  2423. );
  2424. tt_assert(found_A2 == 1);
  2425. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2426. int found_non_default_fallback = 0;
  2427. SMARTLIST_FOREACH(fallback_servers,
  2428. dir_server_t *,
  2429. ds,
  2430. /* increment the found counter if dir_port matches */
  2431. found_non_default_fallback +=
  2432. (ds->dir_port == 60093 ?
  2433. 1 : 0)
  2434. );
  2435. tt_assert(found_non_default_fallback == 0);
  2436. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2437. int found_default_fallback = 0;
  2438. SMARTLIST_FOREACH(fallback_servers,
  2439. dir_server_t *,
  2440. ds,
  2441. /* increment the found counter if dir_port matches */
  2442. found_default_fallback +=
  2443. (ds->dir_port == 60099 ?
  2444. 1 : 0)
  2445. );
  2446. tt_assert(found_default_fallback == 0);
  2447. /* There's no easy way of checking that we have included all the
  2448. * default Bridge authorities (except for hard-coding tonga's details),
  2449. * so let's assume that if the total count above is correct,
  2450. * we have the right ones.
  2451. */
  2452. }
  2453. }
  2454. /*
  2455. 4. Outcome: Use Set Custom Fallback Directory
  2456. - Use Default Bridge & Directory Authorities
  2457. Cases expected to yield this outcome:
  2458. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2459. are all NULL, but FallbackDir is set)
  2460. */
  2461. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2462. & AlternateDirAuthority Not Set, FallbackDir Set */
  2463. {
  2464. /* clear fallback dirs counter */
  2465. n_add_default_fallback_dir_servers_known_default = 0;
  2466. /* clear options*/
  2467. memset(options, 0, sizeof(or_options_t));
  2468. /* clear any previous dir servers:
  2469. consider_adding_dir_servers() should do this anyway */
  2470. clear_dir_servers();
  2471. /* assign options: 0001 */
  2472. options->DirAuthorities = NULL;
  2473. options->AlternateBridgeAuthority = NULL;
  2474. options->AlternateDirAuthority = NULL;
  2475. options->FallbackDir = test_fallback_directory;
  2476. /* parse options - ensure we always update by passing NULL old_options */
  2477. consider_adding_dir_servers(options, NULL);
  2478. /* check outcome */
  2479. /* we must not have added the default fallback dirs */
  2480. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2481. /* we have more fallbacks than just the authorities */
  2482. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2483. {
  2484. /* trusted_dir_servers */
  2485. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2486. /* (No D0), (No B1), Default Bridge Authorities,
  2487. * (No A2), Default v3 Directory Authorities */
  2488. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2489. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2490. int found_D0 = 0;
  2491. SMARTLIST_FOREACH(dir_servers,
  2492. dir_server_t *,
  2493. ds,
  2494. /* increment the found counter if dir_port matches */
  2495. found_D0 +=
  2496. (ds->dir_port == 60090 ?
  2497. 1 : 0)
  2498. );
  2499. tt_assert(found_D0 == 0);
  2500. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2501. int found_B1 = 0;
  2502. SMARTLIST_FOREACH(dir_servers,
  2503. dir_server_t *,
  2504. ds,
  2505. /* increment the found counter if dir_port matches */
  2506. found_B1 +=
  2507. (ds->dir_port == 60091 ?
  2508. 1 : 0)
  2509. );
  2510. tt_assert(found_B1 == 0);
  2511. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2512. int found_A2 = 0;
  2513. SMARTLIST_FOREACH(dir_servers,
  2514. dir_server_t *,
  2515. ds,
  2516. /* increment the found counter if dir_port matches */
  2517. found_A2 +=
  2518. (ds->dir_port == 60092 ?
  2519. 1 : 0)
  2520. );
  2521. tt_assert(found_A2 == 0);
  2522. /* There's no easy way of checking that we have included all the
  2523. * default Bridge & V3 Directory authorities, so let's assume that
  2524. * if the total count above is correct, we have the right ones.
  2525. */
  2526. }
  2527. {
  2528. /* fallback_dir_servers */
  2529. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2530. /* (No D0), (No B1), Default Bridge Authorities,
  2531. * (No A2), Default v3 Directory Authorities,
  2532. * Custom Fallback Directory, (No Default Fallback Directories) */
  2533. tt_assert(smartlist_len(fallback_servers) ==
  2534. 1 + n_default_authorities);
  2535. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2536. int found_D0 = 0;
  2537. SMARTLIST_FOREACH(fallback_servers,
  2538. dir_server_t *,
  2539. ds,
  2540. /* increment the found counter if dir_port matches */
  2541. found_D0 +=
  2542. (ds->dir_port == 60090 ?
  2543. 1 : 0)
  2544. );
  2545. tt_assert(found_D0 == 0);
  2546. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2547. int found_B1 = 0;
  2548. SMARTLIST_FOREACH(fallback_servers,
  2549. dir_server_t *,
  2550. ds,
  2551. /* increment the found counter if dir_port matches */
  2552. found_B1 +=
  2553. (ds->dir_port == 60091 ?
  2554. 1 : 0)
  2555. );
  2556. tt_assert(found_B1 == 0);
  2557. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2558. int found_A2 = 0;
  2559. SMARTLIST_FOREACH(fallback_servers,
  2560. dir_server_t *,
  2561. ds,
  2562. /* increment the found counter if dir_port matches */
  2563. found_A2 +=
  2564. (ds->dir_port == 60092 ?
  2565. 1 : 0)
  2566. );
  2567. tt_assert(found_A2 == 0);
  2568. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2569. int found_non_default_fallback = 0;
  2570. SMARTLIST_FOREACH(fallback_servers,
  2571. dir_server_t *,
  2572. ds,
  2573. /* increment the found counter if dir_port matches */
  2574. found_non_default_fallback +=
  2575. (ds->dir_port == 60093 ?
  2576. 1 : 0)
  2577. );
  2578. tt_assert(found_non_default_fallback == 1);
  2579. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2580. int found_default_fallback = 0;
  2581. SMARTLIST_FOREACH(fallback_servers,
  2582. dir_server_t *,
  2583. ds,
  2584. /* increment the found counter if dir_port matches */
  2585. found_default_fallback +=
  2586. (ds->dir_port == 60099 ?
  2587. 1 : 0)
  2588. );
  2589. tt_assert(found_default_fallback == 0);
  2590. /* There's no easy way of checking that we have included all the
  2591. * default Bridge & V3 Directory authorities, so let's assume that
  2592. * if the total count above is correct, we have the right ones.
  2593. */
  2594. }
  2595. }
  2596. /*
  2597. 5. Outcome: Use All Defaults
  2598. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2599. Cases expected to yield this outcome:
  2600. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2601. and FallbackDir are all NULL)
  2602. */
  2603. /* Case 0: 0000 - All Not Set */
  2604. {
  2605. /* clear fallback dirs counter */
  2606. n_add_default_fallback_dir_servers_known_default = 0;
  2607. /* clear options*/
  2608. memset(options, 0, sizeof(or_options_t));
  2609. /* clear any previous dir servers:
  2610. consider_adding_dir_servers() should do this anyway */
  2611. clear_dir_servers();
  2612. /* assign options: 0001 */
  2613. options->DirAuthorities = NULL;
  2614. options->AlternateBridgeAuthority = NULL;
  2615. options->AlternateDirAuthority = NULL;
  2616. options->FallbackDir = NULL;
  2617. /* parse options - ensure we always update by passing NULL old_options */
  2618. consider_adding_dir_servers(options, NULL);
  2619. /* check outcome */
  2620. /* we must have added the default fallback dirs */
  2621. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2622. /* we have more fallbacks than just the authorities */
  2623. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2624. {
  2625. /* trusted_dir_servers */
  2626. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2627. /* (No D0), (No B1), Default Bridge Authorities,
  2628. * (No A2), Default v3 Directory Authorities */
  2629. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2630. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2631. int found_D0 = 0;
  2632. SMARTLIST_FOREACH(dir_servers,
  2633. dir_server_t *,
  2634. ds,
  2635. /* increment the found counter if dir_port matches */
  2636. found_D0 +=
  2637. (ds->dir_port == 60090 ?
  2638. 1 : 0)
  2639. );
  2640. tt_assert(found_D0 == 0);
  2641. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2642. int found_B1 = 0;
  2643. SMARTLIST_FOREACH(dir_servers,
  2644. dir_server_t *,
  2645. ds,
  2646. /* increment the found counter if dir_port matches */
  2647. found_B1 +=
  2648. (ds->dir_port == 60091 ?
  2649. 1 : 0)
  2650. );
  2651. tt_assert(found_B1 == 0);
  2652. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2653. int found_A2 = 0;
  2654. SMARTLIST_FOREACH(dir_servers,
  2655. dir_server_t *,
  2656. ds,
  2657. /* increment the found counter if dir_port matches */
  2658. found_A2 +=
  2659. (ds->dir_port == 60092 ?
  2660. 1 : 0)
  2661. );
  2662. tt_assert(found_A2 == 0);
  2663. /* There's no easy way of checking that we have included all the
  2664. * default Bridge & V3 Directory authorities, so let's assume that
  2665. * if the total count above is correct, we have the right ones.
  2666. */
  2667. }
  2668. {
  2669. /* fallback_dir_servers */
  2670. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2671. /* (No D0), (No B1), Default Bridge Authorities,
  2672. * (No A2), Default v3 Directory Authorities,
  2673. * (No Custom Fallback Directory), Default Fallback Directories */
  2674. tt_assert(smartlist_len(fallback_servers) ==
  2675. n_default_authorities + n_default_fallback_dir);
  2676. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2677. int found_D0 = 0;
  2678. SMARTLIST_FOREACH(fallback_servers,
  2679. dir_server_t *,
  2680. ds,
  2681. /* increment the found counter if dir_port matches */
  2682. found_D0 +=
  2683. (ds->dir_port == 60090 ?
  2684. 1 : 0)
  2685. );
  2686. tt_assert(found_D0 == 0);
  2687. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2688. int found_B1 = 0;
  2689. SMARTLIST_FOREACH(fallback_servers,
  2690. dir_server_t *,
  2691. ds,
  2692. /* increment the found counter if dir_port matches */
  2693. found_B1 +=
  2694. (ds->dir_port == 60091 ?
  2695. 1 : 0)
  2696. );
  2697. tt_assert(found_B1 == 0);
  2698. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2699. int found_A2 = 0;
  2700. SMARTLIST_FOREACH(fallback_servers,
  2701. dir_server_t *,
  2702. ds,
  2703. /* increment the found counter if dir_port matches */
  2704. found_A2 +=
  2705. (ds->dir_port == 60092 ?
  2706. 1 : 0)
  2707. );
  2708. tt_assert(found_A2 == 0);
  2709. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2710. int found_non_default_fallback = 0;
  2711. SMARTLIST_FOREACH(fallback_servers,
  2712. dir_server_t *,
  2713. ds,
  2714. /* increment the found counter if dir_port matches */
  2715. found_non_default_fallback +=
  2716. (ds->dir_port == 60093 ?
  2717. 1 : 0)
  2718. );
  2719. tt_assert(found_non_default_fallback == 0);
  2720. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2721. int found_default_fallback = 0;
  2722. SMARTLIST_FOREACH(fallback_servers,
  2723. dir_server_t *,
  2724. ds,
  2725. /* increment the found counter if dir_port matches */
  2726. found_default_fallback +=
  2727. (ds->dir_port == 60099 ?
  2728. 1 : 0)
  2729. );
  2730. tt_assert(found_default_fallback == 1);
  2731. /* There's no easy way of checking that we have included all the
  2732. * default Bridge & V3 Directory authorities, and the default
  2733. * Fallback Directories, so let's assume that if the total count
  2734. * above is correct, we have the right ones.
  2735. */
  2736. }
  2737. }
  2738. done:
  2739. clear_dir_servers();
  2740. tor_free(test_dir_authority->key);
  2741. tor_free(test_dir_authority->value);
  2742. tor_free(test_dir_authority);
  2743. tor_free(test_alt_dir_authority->key);
  2744. tor_free(test_alt_dir_authority->value);
  2745. tor_free(test_alt_dir_authority);
  2746. tor_free(test_alt_bridge_authority->key);
  2747. tor_free(test_alt_bridge_authority->value);
  2748. tor_free(test_alt_bridge_authority);
  2749. tor_free(test_fallback_directory->key);
  2750. tor_free(test_fallback_directory->value);
  2751. tor_free(test_fallback_directory);
  2752. options->DirAuthorities = NULL;
  2753. options->AlternateBridgeAuthority = NULL;
  2754. options->AlternateDirAuthority = NULL;
  2755. options->FallbackDir = NULL;
  2756. or_options_free(options);
  2757. UNMOCK(add_default_fallback_dir_servers);
  2758. }
  2759. static void
  2760. test_config_use_multiple_directories(void *arg)
  2761. {
  2762. (void)arg;
  2763. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  2764. /* Clients can use multiple directory mirrors for bootstrap */
  2765. memset(options, 0, sizeof(or_options_t));
  2766. options->ClientOnly = 1;
  2767. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2768. == 1);
  2769. /* Bridge Clients can use multiple directory mirrors for bootstrap */
  2770. memset(options, 0, sizeof(or_options_t));
  2771. options->UseBridges = 1;
  2772. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2773. == 1);
  2774. /* Bridge Relays (Bridges) must act like clients, and use multiple
  2775. * directory mirrors for bootstrap */
  2776. memset(options, 0, sizeof(or_options_t));
  2777. options->BridgeRelay = 1;
  2778. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2779. == 1);
  2780. /* Clients set to FetchDirInfoEarly must fetch it from the authorities */
  2781. memset(options, 0, sizeof(or_options_t));
  2782. options->FetchDirInfoEarly = 1;
  2783. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2784. == 0);
  2785. /* OR servers must fetch the consensus from the authorities */
  2786. memset(options, 0, sizeof(or_options_t));
  2787. options->ORPort_set = 1;
  2788. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2789. == 0);
  2790. done:
  2791. tor_free(options);
  2792. }
  2793. #define CONFIG_TEST(name, flags) \
  2794. { #name, test_config_ ## name, flags, NULL, NULL }
  2795. struct testcase_t config_tests[] = {
  2796. CONFIG_TEST(adding_dir_servers, TT_FORK),
  2797. CONFIG_TEST(resolve_my_address, TT_FORK),
  2798. CONFIG_TEST(addressmap, 0),
  2799. CONFIG_TEST(parse_bridge_line, 0),
  2800. CONFIG_TEST(parse_transport_options_line, 0),
  2801. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  2802. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  2803. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  2804. CONFIG_TEST(fix_my_family, 0),
  2805. CONFIG_TEST(use_multiple_directories, 0),
  2806. END_OF_TESTCASES
  2807. };