test_config.c 120 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545
  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 void
  1187. test_config_adding_trusted_dir_server(void *arg)
  1188. {
  1189. (void)arg;
  1190. const char digest[DIGEST_LEN] = "";
  1191. dir_server_t *ds = NULL;
  1192. tor_addr_port_t ipv6;
  1193. int rv = -1;
  1194. clear_dir_servers();
  1195. routerlist_free_all();
  1196. /* create a trusted ds without an IPv6 address and port */
  1197. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1198. NULL, V3_DIRINFO, 1.0);
  1199. tt_assert(ds);
  1200. dir_server_add(ds);
  1201. tt_assert(get_n_authorities(V3_DIRINFO) == 1);
  1202. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1203. /* create a trusted ds with an IPv6 address and port */
  1204. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1205. tt_assert(rv == 0);
  1206. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
  1207. NULL, V3_DIRINFO, 1.0);
  1208. tt_assert(ds);
  1209. dir_server_add(ds);
  1210. tt_assert(get_n_authorities(V3_DIRINFO) == 2);
  1211. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1212. done:
  1213. clear_dir_servers();
  1214. routerlist_free_all();
  1215. }
  1216. static void
  1217. test_config_adding_fallback_dir_server(void *arg)
  1218. {
  1219. (void)arg;
  1220. const char digest[DIGEST_LEN] = "";
  1221. dir_server_t *ds = NULL;
  1222. tor_addr_t ipv4;
  1223. tor_addr_port_t ipv6;
  1224. int rv = -1;
  1225. clear_dir_servers();
  1226. routerlist_free_all();
  1227. rv = tor_addr_parse(&ipv4, "127.0.0.1");
  1228. tt_assert(rv == AF_INET);
  1229. /* create a trusted ds without an IPv6 address and port */
  1230. ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
  1231. tt_assert(ds);
  1232. dir_server_add(ds);
  1233. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1234. /* create a trusted ds with an IPv6 address and port */
  1235. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1236. tt_assert(rv == 0);
  1237. ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
  1238. tt_assert(ds);
  1239. dir_server_add(ds);
  1240. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1241. done:
  1242. clear_dir_servers();
  1243. routerlist_free_all();
  1244. }
  1245. /* No secrets here:
  1246. * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1247. * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1248. * with added spaces
  1249. */
  1250. #define TEST_DIR_AUTH_LINE_START \
  1251. "foobar orport=12345 " \
  1252. "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
  1253. #define TEST_DIR_AUTH_LINE_END \
  1254. "1.2.3.4:54321 " \
  1255. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1256. #define TEST_DIR_AUTH_IPV6_FLAG \
  1257. "ipv6=[feed::beef]:9 "
  1258. static void
  1259. test_config_parsing_trusted_dir_server(void *arg)
  1260. {
  1261. (void)arg;
  1262. int rv = -1;
  1263. /* parse a trusted dir server without an IPv6 address and port */
  1264. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1265. TEST_DIR_AUTH_LINE_END,
  1266. V3_DIRINFO, 1);
  1267. tt_assert(rv == 0);
  1268. /* parse a trusted dir server with an IPv6 address and port */
  1269. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1270. TEST_DIR_AUTH_IPV6_FLAG
  1271. TEST_DIR_AUTH_LINE_END,
  1272. V3_DIRINFO, 1);
  1273. tt_assert(rv == 0);
  1274. /* Since we are only validating, there is no cleanup. */
  1275. done:
  1276. ;
  1277. }
  1278. #undef TEST_DIR_AUTH_LINE_START
  1279. #undef TEST_DIR_AUTH_LINE_END
  1280. #undef TEST_DIR_AUTH_IPV6_FLAG
  1281. /* No secrets here:
  1282. * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
  1283. */
  1284. #define TEST_DIR_FALLBACK_LINE \
  1285. "1.2.3.4:54321 orport=12345 " \
  1286. "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
  1287. #define TEST_DIR_FALLBACK_IPV6_FLAG \
  1288. "ipv6=[2015:c0de::deed]:9"
  1289. static void
  1290. test_config_parsing_fallback_dir_server(void *arg)
  1291. {
  1292. (void)arg;
  1293. int rv = -1;
  1294. /* parse a trusted dir server without an IPv6 address and port */
  1295. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
  1296. tt_assert(rv == 0);
  1297. /* parse a trusted dir server with an IPv6 address and port */
  1298. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
  1299. TEST_DIR_FALLBACK_IPV6_FLAG,
  1300. 1);
  1301. tt_assert(rv == 0);
  1302. /* Since we are only validating, there is no cleanup. */
  1303. done:
  1304. ;
  1305. }
  1306. #undef TEST_DIR_FALLBACK_LINE
  1307. #undef TEST_DIR_FALLBACK_IPV6_FLAG
  1308. static void
  1309. test_config_adding_default_trusted_dir_servers(void *arg)
  1310. {
  1311. (void)arg;
  1312. clear_dir_servers();
  1313. routerlist_free_all();
  1314. /* Assume we only have one bridge authority */
  1315. add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
  1316. tt_assert(get_n_authorities(BRIDGE_DIRINFO) == 1);
  1317. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1318. /* Assume we have nine V3 authorities */
  1319. add_default_trusted_dir_authorities(V3_DIRINFO);
  1320. tt_assert(get_n_authorities(V3_DIRINFO) == 9);
  1321. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 10);
  1322. done:
  1323. clear_dir_servers();
  1324. routerlist_free_all();
  1325. }
  1326. static int n_add_default_fallback_dir_servers_known_default = 0;
  1327. /**
  1328. * This mock function is meant to replace add_default_fallback_dir_servers().
  1329. * It will parse and add one known default fallback dir server,
  1330. * which has a dir_port of 99.
  1331. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1332. * one every time this function is called.
  1333. */
  1334. static void
  1335. add_default_fallback_dir_servers_known_default(void)
  1336. {
  1337. int i;
  1338. const char *fallback[] = {
  1339. "127.0.0.1:60099 orport=9009 "
  1340. "id=0923456789012345678901234567890123456789",
  1341. NULL
  1342. };
  1343. for (i=0; fallback[i]; i++) {
  1344. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1345. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1346. fallback[i]);
  1347. }
  1348. }
  1349. n_add_default_fallback_dir_servers_known_default++;
  1350. }
  1351. /* Test all the different combinations of adding dir servers */
  1352. static void
  1353. test_config_adding_dir_servers(void *arg)
  1354. {
  1355. (void)arg;
  1356. /* allocate options */
  1357. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  1358. /* Allocate and populate configuration lines:
  1359. *
  1360. * Use the same format as the hard-coded directories in
  1361. * add_default_trusted_dir_authorities().
  1362. * Zeroing the structure has the same effect as initialising to:
  1363. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1364. */
  1365. config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
  1366. test_dir_authority->key = tor_strdup("DirAuthority");
  1367. test_dir_authority->value = tor_strdup(
  1368. "D0 orport=9000 "
  1369. "v3ident=0023456789012345678901234567890123456789 "
  1370. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1371. );
  1372. config_line_t *test_alt_bridge_authority = tor_malloc_zero(
  1373. sizeof(config_line_t));
  1374. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1375. test_alt_bridge_authority->value = tor_strdup(
  1376. "B1 orport=9001 bridge "
  1377. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1378. );
  1379. config_line_t *test_alt_dir_authority = tor_malloc_zero(
  1380. sizeof(config_line_t));
  1381. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1382. test_alt_dir_authority->value = tor_strdup(
  1383. "A2 orport=9002 "
  1384. "v3ident=0223456789012345678901234567890123456789 "
  1385. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1386. );
  1387. /* Use the format specified in the manual page */
  1388. config_line_t *test_fallback_directory = tor_malloc_zero(
  1389. sizeof(config_line_t));
  1390. test_fallback_directory->key = tor_strdup("FallbackDir");
  1391. test_fallback_directory->value = tor_strdup(
  1392. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1393. );
  1394. /* We need to know if add_default_fallback_dir_servers is called,
  1395. * whatever the size of the list in fallback_dirs.inc,
  1396. * so we use a version of add_default_fallback_dir_servers that adds
  1397. * one known default fallback directory. */
  1398. MOCK(add_default_fallback_dir_servers,
  1399. add_default_fallback_dir_servers_known_default);
  1400. /* There are 16 different cases, covering each combination of set/NULL for:
  1401. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1402. * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
  1403. * But validate_dir_servers() ensures that:
  1404. * "You cannot set both DirAuthority and Alternate*Authority."
  1405. * This reduces the number of cases to 10.
  1406. *
  1407. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1408. * So 1001 or case 9 is:
  1409. * DirAuthorities set,
  1410. * AlternateBridgeAuthority NULL,
  1411. * AlternateDirAuthority NULL
  1412. * FallbackDir set
  1413. * The valid cases are cases 0-9 counting using this method, as every case
  1414. * greater than or equal to 10 = 1010 is invalid.
  1415. *
  1416. * 1. Outcome: Use Set Directory Authorities
  1417. * - No Default Authorities
  1418. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1419. * if they are set
  1420. * Cases expected to yield this outcome:
  1421. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1422. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1423. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1424. *
  1425. * 2. Outcome: Use Set Bridge Authority
  1426. * - Use Default Non-Bridge Directory Authorities
  1427. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1428. * Cases expected to yield this outcome:
  1429. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1430. * AlternateBridgeAuthority is set, and
  1431. * AlternateDirAuthority is NULL)
  1432. *
  1433. * 3. Outcome: Use Set Alternate Directory Authority
  1434. * - Use Default Bridge Authorities
  1435. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1436. * Cases expected to yield this outcome:
  1437. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1438. * are both NULL, but AlternateDirAuthority is set)
  1439. *
  1440. * 4. Outcome: Use Set Custom Fallback Directory
  1441. * - Use Default Bridge & Directory Authorities
  1442. * Cases expected to yield this outcome:
  1443. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1444. * are all NULL, but FallbackDir is set)
  1445. *
  1446. * 5. Outcome: Use All Defaults
  1447. * - Use Default Bridge & Directory Authorities, and
  1448. * Default Fallback Directories
  1449. * Cases expected to yield this outcome:
  1450. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1451. * and FallbackDir are all NULL)
  1452. */
  1453. /*
  1454. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1455. * are hard-coded into this build.
  1456. * This code makes some assumptions about the implementation.
  1457. * If they are wrong, one or more of cases 0-5 could fail.
  1458. */
  1459. int n_default_alt_bridge_authority = 0;
  1460. int n_default_alt_dir_authority = 0;
  1461. int n_default_fallback_dir = 0;
  1462. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1463. + (n_default_alt_dir_authority))
  1464. /* Pre-Count Number of Authorities of Each Type
  1465. * Use 0000: No Directory Authorities or Fallback Directories Set
  1466. */
  1467. {
  1468. /* clear fallback dirs counter */
  1469. n_add_default_fallback_dir_servers_known_default = 0;
  1470. /* clear options*/
  1471. memset(options, 0, sizeof(or_options_t));
  1472. /* clear any previous dir servers:
  1473. consider_adding_dir_servers() should do this anyway */
  1474. clear_dir_servers();
  1475. /* assign options: 0000 */
  1476. options->DirAuthorities = NULL;
  1477. options->AlternateBridgeAuthority = NULL;
  1478. options->AlternateDirAuthority = NULL;
  1479. options->FallbackDir = NULL;
  1480. options->UseDefaultFallbackDirs = 1;
  1481. /* parse options - ensure we always update by passing NULL old_options */
  1482. consider_adding_dir_servers(options, NULL);
  1483. /* check outcome */
  1484. /* we must have added the default fallback dirs */
  1485. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  1486. /* we have more fallbacks than just the authorities */
  1487. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1488. {
  1489. /* fallback_dir_servers */
  1490. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1491. /* Count Bridge Authorities */
  1492. SMARTLIST_FOREACH(fallback_servers,
  1493. dir_server_t *,
  1494. ds,
  1495. /* increment the found counter if it's a bridge auth */
  1496. n_default_alt_bridge_authority +=
  1497. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1498. 1 : 0)
  1499. );
  1500. /* If we have no default bridge authority, something has gone wrong */
  1501. tt_assert(n_default_alt_bridge_authority >= 1);
  1502. /* Count v3 Authorities */
  1503. SMARTLIST_FOREACH(fallback_servers,
  1504. dir_server_t *,
  1505. ds,
  1506. /* increment found counter if it's a v3 auth */
  1507. n_default_alt_dir_authority +=
  1508. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1509. 1 : 0)
  1510. );
  1511. /* If we have no default authorities, something has gone really wrong */
  1512. tt_assert(n_default_alt_dir_authority >= 1);
  1513. /* Calculate Fallback Directory Count */
  1514. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1515. n_default_alt_bridge_authority -
  1516. n_default_alt_dir_authority);
  1517. /* If we have a negative count, something has gone really wrong,
  1518. * or some authorities aren't being added as fallback directories.
  1519. * (networkstatus_consensus_can_use_extra_fallbacks depends on all
  1520. * authorities being fallback directories.) */
  1521. tt_assert(n_default_fallback_dir >= 0);
  1522. }
  1523. }
  1524. /*
  1525. * 1. Outcome: Use Set Directory Authorities
  1526. * - No Default Authorities
  1527. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1528. * if they are set
  1529. * Cases expected to yield this outcome:
  1530. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1531. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1532. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1533. */
  1534. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1535. AlternateDirAuthority Not Set, FallbackDir Set */
  1536. {
  1537. /* clear fallback dirs counter */
  1538. n_add_default_fallback_dir_servers_known_default = 0;
  1539. /* clear options*/
  1540. memset(options, 0, sizeof(or_options_t));
  1541. /* clear any previous dir servers:
  1542. consider_adding_dir_servers() should do this anyway */
  1543. clear_dir_servers();
  1544. /* assign options: 1001 */
  1545. options->DirAuthorities = test_dir_authority;
  1546. options->AlternateBridgeAuthority = NULL;
  1547. options->AlternateDirAuthority = NULL;
  1548. options->FallbackDir = test_fallback_directory;
  1549. options->UseDefaultFallbackDirs = 1;
  1550. /* parse options - ensure we always update by passing NULL old_options */
  1551. consider_adding_dir_servers(options, NULL);
  1552. /* check outcome */
  1553. /* we must not have added the default fallback dirs */
  1554. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1555. /* we have more fallbacks than just the authorities */
  1556. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1557. {
  1558. /* trusted_dir_servers */
  1559. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1560. /* D0, (No B1), (No A2) */
  1561. tt_assert(smartlist_len(dir_servers) == 1);
  1562. /* DirAuthority - D0 - dir_port: 60090 */
  1563. int found_D0 = 0;
  1564. SMARTLIST_FOREACH(dir_servers,
  1565. dir_server_t *,
  1566. ds,
  1567. /* increment the found counter if dir_port matches */
  1568. found_D0 +=
  1569. (ds->dir_port == 60090 ?
  1570. 1 : 0)
  1571. );
  1572. tt_assert(found_D0 == 1);
  1573. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1574. int found_B1 = 0;
  1575. SMARTLIST_FOREACH(dir_servers,
  1576. dir_server_t *,
  1577. ds,
  1578. /* increment the found counter if dir_port matches */
  1579. found_B1 +=
  1580. (ds->dir_port == 60091 ?
  1581. 1 : 0)
  1582. );
  1583. tt_assert(found_B1 == 0);
  1584. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1585. int found_A2 = 0;
  1586. SMARTLIST_FOREACH(dir_servers,
  1587. dir_server_t *,
  1588. ds,
  1589. /* increment the found counter if dir_port matches */
  1590. found_A2 +=
  1591. (ds->dir_port == 60092 ?
  1592. 1 : 0)
  1593. );
  1594. tt_assert(found_A2 == 0);
  1595. }
  1596. {
  1597. /* fallback_dir_servers */
  1598. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1599. /* D0, (No B1), (No A2), Custom Fallback */
  1600. tt_assert(smartlist_len(fallback_servers) == 2);
  1601. /* DirAuthority - D0 - dir_port: 60090 */
  1602. int found_D0 = 0;
  1603. SMARTLIST_FOREACH(fallback_servers,
  1604. dir_server_t *,
  1605. ds,
  1606. /* increment the found counter if dir_port matches */
  1607. found_D0 +=
  1608. (ds->dir_port == 60090 ?
  1609. 1 : 0)
  1610. );
  1611. tt_assert(found_D0 == 1);
  1612. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1613. int found_B1 = 0;
  1614. SMARTLIST_FOREACH(fallback_servers,
  1615. dir_server_t *,
  1616. ds,
  1617. /* increment the found counter if dir_port matches */
  1618. found_B1 +=
  1619. (ds->dir_port == 60091 ?
  1620. 1 : 0)
  1621. );
  1622. tt_assert(found_B1 == 0);
  1623. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1624. int found_A2 = 0;
  1625. SMARTLIST_FOREACH(fallback_servers,
  1626. dir_server_t *,
  1627. ds,
  1628. /* increment the found counter if dir_port matches */
  1629. found_A2 +=
  1630. (ds->dir_port == 60092 ?
  1631. 1 : 0)
  1632. );
  1633. tt_assert(found_A2 == 0);
  1634. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1635. int found_non_default_fallback = 0;
  1636. SMARTLIST_FOREACH(fallback_servers,
  1637. dir_server_t *,
  1638. ds,
  1639. /* increment the found counter if dir_port matches */
  1640. found_non_default_fallback +=
  1641. (ds->dir_port == 60093 ?
  1642. 1 : 0)
  1643. );
  1644. tt_assert(found_non_default_fallback == 1);
  1645. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1646. int found_default_fallback = 0;
  1647. SMARTLIST_FOREACH(fallback_servers,
  1648. dir_server_t *,
  1649. ds,
  1650. /* increment the found counter if dir_port matches */
  1651. found_default_fallback +=
  1652. (ds->dir_port == 60099 ?
  1653. 1 : 0)
  1654. );
  1655. tt_assert(found_default_fallback == 0);
  1656. }
  1657. }
  1658. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1659. {
  1660. /* clear fallback dirs counter */
  1661. n_add_default_fallback_dir_servers_known_default = 0;
  1662. /* clear options*/
  1663. memset(options, 0, sizeof(or_options_t));
  1664. /* clear any previous dir servers:
  1665. consider_adding_dir_servers() should do this anyway */
  1666. clear_dir_servers();
  1667. /* assign options: 1000 */
  1668. options->DirAuthorities = test_dir_authority;
  1669. options->AlternateBridgeAuthority = NULL;
  1670. options->AlternateDirAuthority = NULL;
  1671. options->FallbackDir = NULL;
  1672. options->UseDefaultFallbackDirs = 1;
  1673. /* parse options - ensure we always update by passing NULL old_options */
  1674. consider_adding_dir_servers(options, NULL);
  1675. /* check outcome */
  1676. /* we must not have added the default fallback dirs */
  1677. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1678. /* we just have the authorities */
  1679. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1680. {
  1681. /* trusted_dir_servers */
  1682. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1683. /* D0, (No B1), (No A2) */
  1684. tt_assert(smartlist_len(dir_servers) == 1);
  1685. /* DirAuthority - D0 - dir_port: 60090 */
  1686. int found_D0 = 0;
  1687. SMARTLIST_FOREACH(dir_servers,
  1688. dir_server_t *,
  1689. ds,
  1690. /* increment the found counter if dir_port matches */
  1691. found_D0 +=
  1692. (ds->dir_port == 60090 ?
  1693. 1 : 0)
  1694. );
  1695. tt_assert(found_D0 == 1);
  1696. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1697. int found_B1 = 0;
  1698. SMARTLIST_FOREACH(dir_servers,
  1699. dir_server_t *,
  1700. ds,
  1701. /* increment the found counter if dir_port matches */
  1702. found_B1 +=
  1703. (ds->dir_port == 60091 ?
  1704. 1 : 0)
  1705. );
  1706. tt_assert(found_B1 == 0);
  1707. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1708. int found_A2 = 0;
  1709. SMARTLIST_FOREACH(dir_servers,
  1710. dir_server_t *,
  1711. ds,
  1712. /* increment the found counter if dir_port matches */
  1713. found_A2 +=
  1714. (ds->dir_port == 60092 ?
  1715. 1 : 0)
  1716. );
  1717. tt_assert(found_A2 == 0);
  1718. }
  1719. {
  1720. /* fallback_dir_servers */
  1721. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1722. /* D0, (No B1), (No A2), (No Fallback) */
  1723. tt_assert(smartlist_len(fallback_servers) == 1);
  1724. /* DirAuthority - D0 - dir_port: 60090 */
  1725. int found_D0 = 0;
  1726. SMARTLIST_FOREACH(fallback_servers,
  1727. dir_server_t *,
  1728. ds,
  1729. /* increment the found counter if dir_port matches */
  1730. found_D0 +=
  1731. (ds->dir_port == 60090 ?
  1732. 1 : 0)
  1733. );
  1734. tt_assert(found_D0 == 1);
  1735. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1736. int found_B1 = 0;
  1737. SMARTLIST_FOREACH(fallback_servers,
  1738. dir_server_t *,
  1739. ds,
  1740. /* increment the found counter if dir_port matches */
  1741. found_B1 +=
  1742. (ds->dir_port == 60091 ?
  1743. 1 : 0)
  1744. );
  1745. tt_assert(found_B1 == 0);
  1746. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1747. int found_A2 = 0;
  1748. SMARTLIST_FOREACH(fallback_servers,
  1749. dir_server_t *,
  1750. ds,
  1751. /* increment the found counter if dir_port matches */
  1752. found_A2 +=
  1753. (ds->dir_port == 60092 ?
  1754. 1 : 0)
  1755. );
  1756. tt_assert(found_A2 == 0);
  1757. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1758. int found_non_default_fallback = 0;
  1759. SMARTLIST_FOREACH(fallback_servers,
  1760. dir_server_t *,
  1761. ds,
  1762. /* increment the found counter if dir_port matches */
  1763. found_non_default_fallback +=
  1764. (ds->dir_port == 60093 ?
  1765. 1 : 0)
  1766. );
  1767. tt_assert(found_non_default_fallback == 0);
  1768. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1769. int found_default_fallback = 0;
  1770. SMARTLIST_FOREACH(fallback_servers,
  1771. dir_server_t *,
  1772. ds,
  1773. /* increment the found counter if dir_port matches */
  1774. found_default_fallback +=
  1775. (ds->dir_port == 60099 ?
  1776. 1 : 0)
  1777. );
  1778. tt_assert(found_default_fallback == 0);
  1779. }
  1780. }
  1781. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1782. {
  1783. /* clear fallback dirs counter */
  1784. n_add_default_fallback_dir_servers_known_default = 0;
  1785. /* clear options*/
  1786. memset(options, 0, sizeof(or_options_t));
  1787. /* clear any previous dir servers:
  1788. consider_adding_dir_servers() should do this anyway */
  1789. clear_dir_servers();
  1790. /* assign options: 0111 */
  1791. options->DirAuthorities = NULL;
  1792. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1793. options->AlternateDirAuthority = test_alt_dir_authority;
  1794. options->FallbackDir = test_fallback_directory;
  1795. options->UseDefaultFallbackDirs = 1;
  1796. /* parse options - ensure we always update by passing NULL old_options */
  1797. consider_adding_dir_servers(options, NULL);
  1798. /* check outcome */
  1799. /* we must not have added the default fallback dirs */
  1800. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1801. /* we have more fallbacks than just the authorities */
  1802. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1803. {
  1804. /* trusted_dir_servers */
  1805. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1806. /* (No D0), B1, A2 */
  1807. tt_assert(smartlist_len(dir_servers) == 2);
  1808. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1809. int found_D0 = 0;
  1810. SMARTLIST_FOREACH(dir_servers,
  1811. dir_server_t *,
  1812. ds,
  1813. /* increment the found counter if dir_port matches */
  1814. found_D0 +=
  1815. (ds->dir_port == 60090 ?
  1816. 1 : 0)
  1817. );
  1818. tt_assert(found_D0 == 0);
  1819. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1820. int found_B1 = 0;
  1821. SMARTLIST_FOREACH(dir_servers,
  1822. dir_server_t *,
  1823. ds,
  1824. /* increment the found counter if dir_port matches */
  1825. found_B1 +=
  1826. (ds->dir_port == 60091 ?
  1827. 1 : 0)
  1828. );
  1829. tt_assert(found_B1 == 1);
  1830. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1831. int found_A2 = 0;
  1832. SMARTLIST_FOREACH(dir_servers,
  1833. dir_server_t *,
  1834. ds,
  1835. /* increment the found counter if dir_port matches */
  1836. found_A2 +=
  1837. (ds->dir_port == 60092 ?
  1838. 1 : 0)
  1839. );
  1840. tt_assert(found_A2 == 1);
  1841. }
  1842. {
  1843. /* fallback_dir_servers */
  1844. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1845. /* (No D0), B1, A2, Custom Fallback */
  1846. tt_assert(smartlist_len(fallback_servers) == 3);
  1847. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1848. int found_D0 = 0;
  1849. SMARTLIST_FOREACH(fallback_servers,
  1850. dir_server_t *,
  1851. ds,
  1852. /* increment the found counter if dir_port matches */
  1853. found_D0 +=
  1854. (ds->dir_port == 60090 ?
  1855. 1 : 0)
  1856. );
  1857. tt_assert(found_D0 == 0);
  1858. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1859. int found_B1 = 0;
  1860. SMARTLIST_FOREACH(fallback_servers,
  1861. dir_server_t *,
  1862. ds,
  1863. /* increment the found counter if dir_port matches */
  1864. found_B1 +=
  1865. (ds->dir_port == 60091 ?
  1866. 1 : 0)
  1867. );
  1868. tt_assert(found_B1 == 1);
  1869. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1870. int found_A2 = 0;
  1871. SMARTLIST_FOREACH(fallback_servers,
  1872. dir_server_t *,
  1873. ds,
  1874. /* increment the found counter if dir_port matches */
  1875. found_A2 +=
  1876. (ds->dir_port == 60092 ?
  1877. 1 : 0)
  1878. );
  1879. tt_assert(found_A2 == 1);
  1880. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1881. int found_non_default_fallback = 0;
  1882. SMARTLIST_FOREACH(fallback_servers,
  1883. dir_server_t *,
  1884. ds,
  1885. /* increment the found counter if dir_port matches */
  1886. found_non_default_fallback +=
  1887. (ds->dir_port == 60093 ?
  1888. 1 : 0)
  1889. );
  1890. tt_assert(found_non_default_fallback == 1);
  1891. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1892. int found_default_fallback = 0;
  1893. SMARTLIST_FOREACH(fallback_servers,
  1894. dir_server_t *,
  1895. ds,
  1896. /* increment the found counter if dir_port matches */
  1897. found_default_fallback +=
  1898. (ds->dir_port == 60099 ?
  1899. 1 : 0)
  1900. );
  1901. tt_assert(found_default_fallback == 0);
  1902. }
  1903. }
  1904. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  1905. AlternateDirAuthority Set, FallbackDir Not Set */
  1906. {
  1907. /* clear fallback dirs counter */
  1908. n_add_default_fallback_dir_servers_known_default = 0;
  1909. /* clear options*/
  1910. memset(options, 0, sizeof(or_options_t));
  1911. /* clear any previous dir servers:
  1912. consider_adding_dir_servers() should do this anyway */
  1913. clear_dir_servers();
  1914. /* assign options: 0110 */
  1915. options->DirAuthorities = NULL;
  1916. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1917. options->AlternateDirAuthority = test_alt_dir_authority;
  1918. options->FallbackDir = NULL;
  1919. options->UseDefaultFallbackDirs = 1;
  1920. /* parse options - ensure we always update by passing NULL old_options */
  1921. consider_adding_dir_servers(options, NULL);
  1922. /* check outcome */
  1923. /* we must not have added the default fallback dirs */
  1924. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1925. /* we have more fallbacks than just the authorities */
  1926. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1927. {
  1928. /* trusted_dir_servers */
  1929. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1930. /* (No D0), B1, A2 */
  1931. tt_assert(smartlist_len(dir_servers) == 2);
  1932. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1933. int found_D0 = 0;
  1934. SMARTLIST_FOREACH(dir_servers,
  1935. dir_server_t *,
  1936. ds,
  1937. /* increment the found counter if dir_port matches */
  1938. found_D0 +=
  1939. (ds->dir_port == 60090 ?
  1940. 1 : 0)
  1941. );
  1942. tt_assert(found_D0 == 0);
  1943. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1944. int found_B1 = 0;
  1945. SMARTLIST_FOREACH(dir_servers,
  1946. dir_server_t *,
  1947. ds,
  1948. /* increment the found counter if dir_port matches */
  1949. found_B1 +=
  1950. (ds->dir_port == 60091 ?
  1951. 1 : 0)
  1952. );
  1953. tt_assert(found_B1 == 1);
  1954. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1955. int found_A2 = 0;
  1956. SMARTLIST_FOREACH(dir_servers,
  1957. dir_server_t *,
  1958. ds,
  1959. /* increment the found counter if dir_port matches */
  1960. found_A2 +=
  1961. (ds->dir_port == 60092 ?
  1962. 1 : 0)
  1963. );
  1964. tt_assert(found_A2 == 1);
  1965. }
  1966. {
  1967. /* fallback_dir_servers */
  1968. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1969. /* (No D0), B1, A2, (No Fallback) */
  1970. tt_assert(smartlist_len(fallback_servers) == 2);
  1971. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1972. int found_D0 = 0;
  1973. SMARTLIST_FOREACH(fallback_servers,
  1974. dir_server_t *,
  1975. ds,
  1976. /* increment the found counter if dir_port matches */
  1977. found_D0 +=
  1978. (ds->dir_port == 60090 ?
  1979. 1 : 0)
  1980. );
  1981. tt_assert(found_D0 == 0);
  1982. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1983. int found_B1 = 0;
  1984. SMARTLIST_FOREACH(fallback_servers,
  1985. dir_server_t *,
  1986. ds,
  1987. /* increment the found counter if dir_port matches */
  1988. found_B1 +=
  1989. (ds->dir_port == 60091 ?
  1990. 1 : 0)
  1991. );
  1992. tt_assert(found_B1 == 1);
  1993. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1994. int found_A2 = 0;
  1995. SMARTLIST_FOREACH(fallback_servers,
  1996. dir_server_t *,
  1997. ds,
  1998. /* increment the found counter if dir_port matches */
  1999. found_A2 +=
  2000. (ds->dir_port == 60092 ?
  2001. 1 : 0)
  2002. );
  2003. tt_assert(found_A2 == 1);
  2004. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2005. int found_non_default_fallback = 0;
  2006. SMARTLIST_FOREACH(fallback_servers,
  2007. dir_server_t *,
  2008. ds,
  2009. /* increment the found counter if dir_port matches */
  2010. found_non_default_fallback +=
  2011. (ds->dir_port == 60093 ?
  2012. 1 : 0)
  2013. );
  2014. tt_assert(found_non_default_fallback == 0);
  2015. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2016. int found_default_fallback = 0;
  2017. SMARTLIST_FOREACH(fallback_servers,
  2018. dir_server_t *,
  2019. ds,
  2020. /* increment the found counter if dir_port matches */
  2021. found_default_fallback +=
  2022. (ds->dir_port == 60099 ?
  2023. 1 : 0)
  2024. );
  2025. tt_assert(found_default_fallback == 0);
  2026. }
  2027. }
  2028. /*
  2029. 2. Outcome: Use Set Bridge Authority
  2030. - Use Default Non-Bridge Directory Authorities
  2031. - Use FallbackDir if it is set, otherwise use default FallbackDir
  2032. Cases expected to yield this outcome:
  2033. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  2034. AlternateBridgeAuthority is set, and
  2035. AlternateDirAuthority is NULL)
  2036. */
  2037. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2038. AlternateDirAuthority Not Set, FallbackDir Set */
  2039. {
  2040. /* clear fallback dirs counter */
  2041. n_add_default_fallback_dir_servers_known_default = 0;
  2042. /* clear options*/
  2043. memset(options, 0, sizeof(or_options_t));
  2044. /* clear any previous dir servers:
  2045. consider_adding_dir_servers() should do this anyway */
  2046. clear_dir_servers();
  2047. /* assign options: 0101 */
  2048. options->DirAuthorities = NULL;
  2049. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2050. options->AlternateDirAuthority = NULL;
  2051. options->FallbackDir = test_fallback_directory;
  2052. options->UseDefaultFallbackDirs = 1;
  2053. /* parse options - ensure we always update by passing NULL old_options */
  2054. consider_adding_dir_servers(options, NULL);
  2055. /* check outcome */
  2056. /* we must not have added the default fallback dirs */
  2057. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2058. /* we have more fallbacks than just the authorities */
  2059. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2060. {
  2061. /* trusted_dir_servers */
  2062. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2063. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2064. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2065. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2066. int found_D0 = 0;
  2067. SMARTLIST_FOREACH(dir_servers,
  2068. dir_server_t *,
  2069. ds,
  2070. /* increment the found counter if dir_port matches */
  2071. found_D0 +=
  2072. (ds->dir_port == 60090 ?
  2073. 1 : 0)
  2074. );
  2075. tt_assert(found_D0 == 0);
  2076. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2077. int found_B1 = 0;
  2078. SMARTLIST_FOREACH(dir_servers,
  2079. dir_server_t *,
  2080. ds,
  2081. /* increment the found counter if dir_port matches */
  2082. found_B1 +=
  2083. (ds->dir_port == 60091 ?
  2084. 1 : 0)
  2085. );
  2086. tt_assert(found_B1 == 1);
  2087. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2088. int found_A2 = 0;
  2089. SMARTLIST_FOREACH(dir_servers,
  2090. dir_server_t *,
  2091. ds,
  2092. /* increment the found counter if dir_port matches */
  2093. found_A2 +=
  2094. (ds->dir_port == 60092 ?
  2095. 1 : 0)
  2096. );
  2097. tt_assert(found_A2 == 0);
  2098. /* There's no easy way of checking that we have included all the
  2099. * default v3 non-Bridge directory authorities, so let's assume that
  2100. * if the total count above is correct, we have the right ones.
  2101. */
  2102. }
  2103. {
  2104. /* fallback_dir_servers */
  2105. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2106. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2107. * Custom Fallback */
  2108. tt_assert(smartlist_len(fallback_servers) ==
  2109. 2 + n_default_alt_dir_authority);
  2110. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2111. int found_D0 = 0;
  2112. SMARTLIST_FOREACH(fallback_servers,
  2113. dir_server_t *,
  2114. ds,
  2115. /* increment the found counter if dir_port matches */
  2116. found_D0 +=
  2117. (ds->dir_port == 60090 ?
  2118. 1 : 0)
  2119. );
  2120. tt_assert(found_D0 == 0);
  2121. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2122. int found_B1 = 0;
  2123. SMARTLIST_FOREACH(fallback_servers,
  2124. dir_server_t *,
  2125. ds,
  2126. /* increment the found counter if dir_port matches */
  2127. found_B1 +=
  2128. (ds->dir_port == 60091 ?
  2129. 1 : 0)
  2130. );
  2131. tt_assert(found_B1 == 1);
  2132. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2133. int found_A2 = 0;
  2134. SMARTLIST_FOREACH(fallback_servers,
  2135. dir_server_t *,
  2136. ds,
  2137. /* increment the found counter if dir_port matches */
  2138. found_A2 +=
  2139. (ds->dir_port == 60092 ?
  2140. 1 : 0)
  2141. );
  2142. tt_assert(found_A2 == 0);
  2143. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2144. int found_non_default_fallback = 0;
  2145. SMARTLIST_FOREACH(fallback_servers,
  2146. dir_server_t *,
  2147. ds,
  2148. /* increment the found counter if dir_port matches */
  2149. found_non_default_fallback +=
  2150. (ds->dir_port == 60093 ?
  2151. 1 : 0)
  2152. );
  2153. tt_assert(found_non_default_fallback == 1);
  2154. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2155. int found_default_fallback = 0;
  2156. SMARTLIST_FOREACH(fallback_servers,
  2157. dir_server_t *,
  2158. ds,
  2159. /* increment the found counter if dir_port matches */
  2160. found_default_fallback +=
  2161. (ds->dir_port == 60099 ?
  2162. 1 : 0)
  2163. );
  2164. tt_assert(found_default_fallback == 0);
  2165. /* There's no easy way of checking that we have included all the
  2166. * default v3 non-Bridge directory authorities, so let's assume that
  2167. * if the total count above is correct, we have the right ones.
  2168. */
  2169. }
  2170. }
  2171. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2172. AlternateDirAuthority & FallbackDir Not Set */
  2173. {
  2174. /* clear fallback dirs counter */
  2175. n_add_default_fallback_dir_servers_known_default = 0;
  2176. /* clear options*/
  2177. memset(options, 0, sizeof(or_options_t));
  2178. /* clear any previous dir servers:
  2179. consider_adding_dir_servers() should do this anyway */
  2180. clear_dir_servers();
  2181. /* assign options: 0100 */
  2182. options->DirAuthorities = NULL;
  2183. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2184. options->AlternateDirAuthority = NULL;
  2185. options->FallbackDir = NULL;
  2186. options->UseDefaultFallbackDirs = 1;
  2187. /* parse options - ensure we always update by passing NULL old_options */
  2188. consider_adding_dir_servers(options, NULL);
  2189. /* check outcome */
  2190. /* we must have added the default fallback dirs */
  2191. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2192. /* we have more fallbacks than just the authorities */
  2193. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2194. {
  2195. /* trusted_dir_servers */
  2196. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2197. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2198. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2199. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2200. int found_D0 = 0;
  2201. SMARTLIST_FOREACH(dir_servers,
  2202. dir_server_t *,
  2203. ds,
  2204. /* increment the found counter if dir_port matches */
  2205. found_D0 +=
  2206. (ds->dir_port == 60090 ?
  2207. 1 : 0)
  2208. );
  2209. tt_assert(found_D0 == 0);
  2210. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2211. int found_B1 = 0;
  2212. SMARTLIST_FOREACH(dir_servers,
  2213. dir_server_t *,
  2214. ds,
  2215. /* increment the found counter if dir_port matches */
  2216. found_B1 +=
  2217. (ds->dir_port == 60091 ?
  2218. 1 : 0)
  2219. );
  2220. tt_assert(found_B1 == 1);
  2221. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2222. int found_A2 = 0;
  2223. SMARTLIST_FOREACH(dir_servers,
  2224. dir_server_t *,
  2225. ds,
  2226. /* increment the found counter if dir_port matches */
  2227. found_A2 +=
  2228. (ds->dir_port == 60092 ?
  2229. 1 : 0)
  2230. );
  2231. tt_assert(found_A2 == 0);
  2232. /* There's no easy way of checking that we have included all the
  2233. * default v3 non-Bridge directory authorities, so let's assume that
  2234. * if the total count above is correct, we have the right ones.
  2235. */
  2236. }
  2237. {
  2238. /* fallback_dir_servers */
  2239. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2240. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2241. * Default Fallback */
  2242. tt_assert(smartlist_len(fallback_servers) ==
  2243. 2 + n_default_alt_dir_authority);
  2244. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2245. int found_D0 = 0;
  2246. SMARTLIST_FOREACH(fallback_servers,
  2247. dir_server_t *,
  2248. ds,
  2249. /* increment the found counter if dir_port matches */
  2250. found_D0 +=
  2251. (ds->dir_port == 60090 ?
  2252. 1 : 0)
  2253. );
  2254. tt_assert(found_D0 == 0);
  2255. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2256. int found_B1 = 0;
  2257. SMARTLIST_FOREACH(fallback_servers,
  2258. dir_server_t *,
  2259. ds,
  2260. /* increment the found counter if dir_port matches */
  2261. found_B1 +=
  2262. (ds->dir_port == 60091 ?
  2263. 1 : 0)
  2264. );
  2265. tt_assert(found_B1 == 1);
  2266. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2267. int found_A2 = 0;
  2268. SMARTLIST_FOREACH(fallback_servers,
  2269. dir_server_t *,
  2270. ds,
  2271. /* increment the found counter if dir_port matches */
  2272. found_A2 +=
  2273. (ds->dir_port == 60092 ?
  2274. 1 : 0)
  2275. );
  2276. tt_assert(found_A2 == 0);
  2277. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2278. int found_non_default_fallback = 0;
  2279. SMARTLIST_FOREACH(fallback_servers,
  2280. dir_server_t *,
  2281. ds,
  2282. /* increment the found counter if dir_port matches */
  2283. found_non_default_fallback +=
  2284. (ds->dir_port == 60093 ?
  2285. 1 : 0)
  2286. );
  2287. tt_assert(found_non_default_fallback == 0);
  2288. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2289. int found_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_default_fallback +=
  2295. (ds->dir_port == 60099 ?
  2296. 1 : 0)
  2297. );
  2298. tt_assert(found_default_fallback == 1);
  2299. /* There's no easy way of checking that we have included all the
  2300. * default v3 non-Bridge directory authorities, so let's assume that
  2301. * if the total count above is correct, we have the right ones.
  2302. */
  2303. }
  2304. }
  2305. /*
  2306. 3. Outcome: Use Set Alternate Directory Authority
  2307. - Use Default Bridge Authorities
  2308. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2309. Cases expected to yield this outcome:
  2310. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2311. are both NULL, but AlternateDirAuthority is set)
  2312. */
  2313. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2314. AlternateDirAuthority & FallbackDir Set */
  2315. {
  2316. /* clear fallback dirs counter */
  2317. n_add_default_fallback_dir_servers_known_default = 0;
  2318. /* clear options*/
  2319. memset(options, 0, sizeof(or_options_t));
  2320. /* clear any previous dir servers:
  2321. consider_adding_dir_servers() should do this anyway */
  2322. clear_dir_servers();
  2323. /* assign options: 0011 */
  2324. options->DirAuthorities = NULL;
  2325. options->AlternateBridgeAuthority = NULL;
  2326. options->AlternateDirAuthority = test_alt_dir_authority;
  2327. options->FallbackDir = test_fallback_directory;
  2328. options->UseDefaultFallbackDirs = 1;
  2329. /* parse options - ensure we always update by passing NULL old_options */
  2330. consider_adding_dir_servers(options, NULL);
  2331. /* check outcome */
  2332. /* we must not have added the default fallback dirs */
  2333. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2334. /* we have more fallbacks than just the authorities */
  2335. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2336. {
  2337. /* trusted_dir_servers */
  2338. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2339. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2340. tt_assert(smartlist_len(dir_servers) ==
  2341. 1 + n_default_alt_bridge_authority);
  2342. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2343. int found_D0 = 0;
  2344. SMARTLIST_FOREACH(dir_servers,
  2345. dir_server_t *,
  2346. ds,
  2347. /* increment the found counter if dir_port matches */
  2348. found_D0 +=
  2349. (ds->dir_port == 60090 ?
  2350. 1 : 0)
  2351. );
  2352. tt_assert(found_D0 == 0);
  2353. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2354. int found_B1 = 0;
  2355. SMARTLIST_FOREACH(dir_servers,
  2356. dir_server_t *,
  2357. ds,
  2358. /* increment the found counter if dir_port matches */
  2359. found_B1 +=
  2360. (ds->dir_port == 60091 ?
  2361. 1 : 0)
  2362. );
  2363. tt_assert(found_B1 == 0);
  2364. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2365. int found_A2 = 0;
  2366. SMARTLIST_FOREACH(dir_servers,
  2367. dir_server_t *,
  2368. ds,
  2369. /* increment the found counter if dir_port matches */
  2370. found_A2 +=
  2371. (ds->dir_port == 60092 ?
  2372. 1 : 0)
  2373. );
  2374. tt_assert(found_A2 == 1);
  2375. /* There's no easy way of checking that we have included all the
  2376. * default Bridge authorities (except for hard-coding tonga's details),
  2377. * so let's assume that if the total count above is correct,
  2378. * we have the right ones.
  2379. */
  2380. }
  2381. {
  2382. /* fallback_dir_servers */
  2383. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2384. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2385. * Custom Fallback Directory, (No Default Fallback Directories) */
  2386. tt_assert(smartlist_len(fallback_servers) ==
  2387. 2 + n_default_alt_bridge_authority);
  2388. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2389. int found_D0 = 0;
  2390. SMARTLIST_FOREACH(fallback_servers,
  2391. dir_server_t *,
  2392. ds,
  2393. /* increment the found counter if dir_port matches */
  2394. found_D0 +=
  2395. (ds->dir_port == 60090 ?
  2396. 1 : 0)
  2397. );
  2398. tt_assert(found_D0 == 0);
  2399. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2400. int found_B1 = 0;
  2401. SMARTLIST_FOREACH(fallback_servers,
  2402. dir_server_t *,
  2403. ds,
  2404. /* increment the found counter if dir_port matches */
  2405. found_B1 +=
  2406. (ds->dir_port == 60091 ?
  2407. 1 : 0)
  2408. );
  2409. tt_assert(found_B1 == 0);
  2410. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2411. int found_A2 = 0;
  2412. SMARTLIST_FOREACH(fallback_servers,
  2413. dir_server_t *,
  2414. ds,
  2415. /* increment the found counter if dir_port matches */
  2416. found_A2 +=
  2417. (ds->dir_port == 60092 ?
  2418. 1 : 0)
  2419. );
  2420. tt_assert(found_A2 == 1);
  2421. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2422. int found_non_default_fallback = 0;
  2423. SMARTLIST_FOREACH(fallback_servers,
  2424. dir_server_t *,
  2425. ds,
  2426. /* increment the found counter if dir_port matches */
  2427. found_non_default_fallback +=
  2428. (ds->dir_port == 60093 ?
  2429. 1 : 0)
  2430. );
  2431. tt_assert(found_non_default_fallback == 1);
  2432. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2433. int found_default_fallback = 0;
  2434. SMARTLIST_FOREACH(fallback_servers,
  2435. dir_server_t *,
  2436. ds,
  2437. /* increment the found counter if dir_port matches */
  2438. found_default_fallback +=
  2439. (ds->dir_port == 60099 ?
  2440. 1 : 0)
  2441. );
  2442. tt_assert(found_default_fallback == 0);
  2443. /* There's no easy way of checking that we have included all the
  2444. * default Bridge authorities (except for hard-coding tonga's details),
  2445. * so let's assume that if the total count above is correct,
  2446. * we have the right ones.
  2447. */
  2448. }
  2449. }
  2450. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2451. AlternateDirAuthority Set, FallbackDir Not Set */
  2452. {
  2453. /* clear fallback dirs counter */
  2454. n_add_default_fallback_dir_servers_known_default = 0;
  2455. /* clear options*/
  2456. memset(options, 0, sizeof(or_options_t));
  2457. /* clear any previous dir servers:
  2458. consider_adding_dir_servers() should do this anyway */
  2459. clear_dir_servers();
  2460. /* assign options: 0010 */
  2461. options->DirAuthorities = NULL;
  2462. options->AlternateBridgeAuthority = NULL;
  2463. options->AlternateDirAuthority = test_alt_dir_authority;
  2464. options->FallbackDir = NULL;
  2465. options->UseDefaultFallbackDirs = 1;
  2466. /* parse options - ensure we always update by passing NULL old_options */
  2467. consider_adding_dir_servers(options, NULL);
  2468. /* check outcome */
  2469. /* we must not have added the default fallback dirs */
  2470. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2471. /* we just have the authorities */
  2472. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2473. {
  2474. /* trusted_dir_servers */
  2475. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2476. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2477. * No Default or Custom Fallback Directories */
  2478. tt_assert(smartlist_len(dir_servers) ==
  2479. 1 + n_default_alt_bridge_authority);
  2480. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2481. int found_D0 = 0;
  2482. SMARTLIST_FOREACH(dir_servers,
  2483. dir_server_t *,
  2484. ds,
  2485. /* increment the found counter if dir_port matches */
  2486. found_D0 +=
  2487. (ds->dir_port == 60090 ?
  2488. 1 : 0)
  2489. );
  2490. tt_assert(found_D0 == 0);
  2491. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2492. int found_B1 = 0;
  2493. SMARTLIST_FOREACH(dir_servers,
  2494. dir_server_t *,
  2495. ds,
  2496. /* increment the found counter if dir_port matches */
  2497. found_B1 +=
  2498. (ds->dir_port == 60091 ?
  2499. 1 : 0)
  2500. );
  2501. tt_assert(found_B1 == 0);
  2502. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2503. int found_A2 = 0;
  2504. SMARTLIST_FOREACH(dir_servers,
  2505. dir_server_t *,
  2506. ds,
  2507. /* increment the found counter if dir_port matches */
  2508. found_A2 +=
  2509. (ds->dir_port == 60092 ?
  2510. 1 : 0)
  2511. );
  2512. tt_assert(found_A2 == 1);
  2513. /* There's no easy way of checking that we have included all the
  2514. * default Bridge authorities (except for hard-coding tonga's details),
  2515. * so let's assume that if the total count above is correct,
  2516. * we have the right ones.
  2517. */
  2518. }
  2519. {
  2520. /* fallback_dir_servers */
  2521. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2522. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2523. * No Custom or Default Fallback Directories */
  2524. tt_assert(smartlist_len(fallback_servers) ==
  2525. 1 + n_default_alt_bridge_authority);
  2526. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2527. int found_D0 = 0;
  2528. SMARTLIST_FOREACH(fallback_servers,
  2529. dir_server_t *,
  2530. ds,
  2531. /* increment the found counter if dir_port matches */
  2532. found_D0 +=
  2533. (ds->dir_port == 60090 ?
  2534. 1 : 0)
  2535. );
  2536. tt_assert(found_D0 == 0);
  2537. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2538. int found_B1 = 0;
  2539. SMARTLIST_FOREACH(fallback_servers,
  2540. dir_server_t *,
  2541. ds,
  2542. /* increment the found counter if dir_port matches */
  2543. found_B1 +=
  2544. (ds->dir_port == 60091 ?
  2545. 1 : 0)
  2546. );
  2547. tt_assert(found_B1 == 0);
  2548. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2549. int found_A2 = 0;
  2550. SMARTLIST_FOREACH(fallback_servers,
  2551. dir_server_t *,
  2552. ds,
  2553. /* increment the found counter if dir_port matches */
  2554. found_A2 +=
  2555. (ds->dir_port == 60092 ?
  2556. 1 : 0)
  2557. );
  2558. tt_assert(found_A2 == 1);
  2559. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2560. int found_non_default_fallback = 0;
  2561. SMARTLIST_FOREACH(fallback_servers,
  2562. dir_server_t *,
  2563. ds,
  2564. /* increment the found counter if dir_port matches */
  2565. found_non_default_fallback +=
  2566. (ds->dir_port == 60093 ?
  2567. 1 : 0)
  2568. );
  2569. tt_assert(found_non_default_fallback == 0);
  2570. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2571. int found_default_fallback = 0;
  2572. SMARTLIST_FOREACH(fallback_servers,
  2573. dir_server_t *,
  2574. ds,
  2575. /* increment the found counter if dir_port matches */
  2576. found_default_fallback +=
  2577. (ds->dir_port == 60099 ?
  2578. 1 : 0)
  2579. );
  2580. tt_assert(found_default_fallback == 0);
  2581. /* There's no easy way of checking that we have included all the
  2582. * default Bridge authorities (except for hard-coding tonga's details),
  2583. * so let's assume that if the total count above is correct,
  2584. * we have the right ones.
  2585. */
  2586. }
  2587. }
  2588. /*
  2589. 4. Outcome: Use Set Custom Fallback Directory
  2590. - Use Default Bridge & Directory Authorities
  2591. Cases expected to yield this outcome:
  2592. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2593. are all NULL, but FallbackDir is set)
  2594. */
  2595. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2596. & AlternateDirAuthority Not Set, FallbackDir Set */
  2597. {
  2598. /* clear fallback dirs counter */
  2599. n_add_default_fallback_dir_servers_known_default = 0;
  2600. /* clear options*/
  2601. memset(options, 0, sizeof(or_options_t));
  2602. /* clear any previous dir servers:
  2603. consider_adding_dir_servers() should do this anyway */
  2604. clear_dir_servers();
  2605. /* assign options: 0001 */
  2606. options->DirAuthorities = NULL;
  2607. options->AlternateBridgeAuthority = NULL;
  2608. options->AlternateDirAuthority = NULL;
  2609. options->FallbackDir = test_fallback_directory;
  2610. options->UseDefaultFallbackDirs = 1;
  2611. /* parse options - ensure we always update by passing NULL old_options */
  2612. consider_adding_dir_servers(options, NULL);
  2613. /* check outcome */
  2614. /* we must not have added the default fallback dirs */
  2615. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2616. /* we have more fallbacks than just the authorities */
  2617. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2618. {
  2619. /* trusted_dir_servers */
  2620. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2621. /* (No D0), (No B1), Default Bridge Authorities,
  2622. * (No A2), Default v3 Directory Authorities */
  2623. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2624. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2625. int found_D0 = 0;
  2626. SMARTLIST_FOREACH(dir_servers,
  2627. dir_server_t *,
  2628. ds,
  2629. /* increment the found counter if dir_port matches */
  2630. found_D0 +=
  2631. (ds->dir_port == 60090 ?
  2632. 1 : 0)
  2633. );
  2634. tt_assert(found_D0 == 0);
  2635. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2636. int found_B1 = 0;
  2637. SMARTLIST_FOREACH(dir_servers,
  2638. dir_server_t *,
  2639. ds,
  2640. /* increment the found counter if dir_port matches */
  2641. found_B1 +=
  2642. (ds->dir_port == 60091 ?
  2643. 1 : 0)
  2644. );
  2645. tt_assert(found_B1 == 0);
  2646. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2647. int found_A2 = 0;
  2648. SMARTLIST_FOREACH(dir_servers,
  2649. dir_server_t *,
  2650. ds,
  2651. /* increment the found counter if dir_port matches */
  2652. found_A2 +=
  2653. (ds->dir_port == 60092 ?
  2654. 1 : 0)
  2655. );
  2656. tt_assert(found_A2 == 0);
  2657. /* There's no easy way of checking that we have included all the
  2658. * default Bridge & V3 Directory authorities, so let's assume that
  2659. * if the total count above is correct, we have the right ones.
  2660. */
  2661. }
  2662. {
  2663. /* fallback_dir_servers */
  2664. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2665. /* (No D0), (No B1), Default Bridge Authorities,
  2666. * (No A2), Default v3 Directory Authorities,
  2667. * Custom Fallback Directory, (No Default Fallback Directories) */
  2668. tt_assert(smartlist_len(fallback_servers) ==
  2669. 1 + n_default_authorities);
  2670. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2671. int found_D0 = 0;
  2672. SMARTLIST_FOREACH(fallback_servers,
  2673. dir_server_t *,
  2674. ds,
  2675. /* increment the found counter if dir_port matches */
  2676. found_D0 +=
  2677. (ds->dir_port == 60090 ?
  2678. 1 : 0)
  2679. );
  2680. tt_assert(found_D0 == 0);
  2681. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2682. int found_B1 = 0;
  2683. SMARTLIST_FOREACH(fallback_servers,
  2684. dir_server_t *,
  2685. ds,
  2686. /* increment the found counter if dir_port matches */
  2687. found_B1 +=
  2688. (ds->dir_port == 60091 ?
  2689. 1 : 0)
  2690. );
  2691. tt_assert(found_B1 == 0);
  2692. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2693. int found_A2 = 0;
  2694. SMARTLIST_FOREACH(fallback_servers,
  2695. dir_server_t *,
  2696. ds,
  2697. /* increment the found counter if dir_port matches */
  2698. found_A2 +=
  2699. (ds->dir_port == 60092 ?
  2700. 1 : 0)
  2701. );
  2702. tt_assert(found_A2 == 0);
  2703. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2704. int found_non_default_fallback = 0;
  2705. SMARTLIST_FOREACH(fallback_servers,
  2706. dir_server_t *,
  2707. ds,
  2708. /* increment the found counter if dir_port matches */
  2709. found_non_default_fallback +=
  2710. (ds->dir_port == 60093 ?
  2711. 1 : 0)
  2712. );
  2713. tt_assert(found_non_default_fallback == 1);
  2714. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2715. int found_default_fallback = 0;
  2716. SMARTLIST_FOREACH(fallback_servers,
  2717. dir_server_t *,
  2718. ds,
  2719. /* increment the found counter if dir_port matches */
  2720. found_default_fallback +=
  2721. (ds->dir_port == 60099 ?
  2722. 1 : 0)
  2723. );
  2724. tt_assert(found_default_fallback == 0);
  2725. /* There's no easy way of checking that we have included all the
  2726. * default Bridge & V3 Directory authorities, so let's assume that
  2727. * if the total count above is correct, we have the right ones.
  2728. */
  2729. }
  2730. }
  2731. /*
  2732. 5. Outcome: Use All Defaults
  2733. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2734. Cases expected to yield this outcome:
  2735. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2736. and FallbackDir are all NULL)
  2737. */
  2738. /* Case 0: 0000 - All Not Set */
  2739. {
  2740. /* clear fallback dirs counter */
  2741. n_add_default_fallback_dir_servers_known_default = 0;
  2742. /* clear options*/
  2743. memset(options, 0, sizeof(or_options_t));
  2744. /* clear any previous dir servers:
  2745. consider_adding_dir_servers() should do this anyway */
  2746. clear_dir_servers();
  2747. /* assign options: 0001 */
  2748. options->DirAuthorities = NULL;
  2749. options->AlternateBridgeAuthority = NULL;
  2750. options->AlternateDirAuthority = NULL;
  2751. options->FallbackDir = NULL;
  2752. options->UseDefaultFallbackDirs = 1;
  2753. /* parse options - ensure we always update by passing NULL old_options */
  2754. consider_adding_dir_servers(options, NULL);
  2755. /* check outcome */
  2756. /* we must have added the default fallback dirs */
  2757. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2758. /* we have more fallbacks than just the authorities */
  2759. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2760. {
  2761. /* trusted_dir_servers */
  2762. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2763. /* (No D0), (No B1), Default Bridge Authorities,
  2764. * (No A2), Default v3 Directory Authorities */
  2765. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2766. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2767. int found_D0 = 0;
  2768. SMARTLIST_FOREACH(dir_servers,
  2769. dir_server_t *,
  2770. ds,
  2771. /* increment the found counter if dir_port matches */
  2772. found_D0 +=
  2773. (ds->dir_port == 60090 ?
  2774. 1 : 0)
  2775. );
  2776. tt_assert(found_D0 == 0);
  2777. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2778. int found_B1 = 0;
  2779. SMARTLIST_FOREACH(dir_servers,
  2780. dir_server_t *,
  2781. ds,
  2782. /* increment the found counter if dir_port matches */
  2783. found_B1 +=
  2784. (ds->dir_port == 60091 ?
  2785. 1 : 0)
  2786. );
  2787. tt_assert(found_B1 == 0);
  2788. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2789. int found_A2 = 0;
  2790. SMARTLIST_FOREACH(dir_servers,
  2791. dir_server_t *,
  2792. ds,
  2793. /* increment the found counter if dir_port matches */
  2794. found_A2 +=
  2795. (ds->dir_port == 60092 ?
  2796. 1 : 0)
  2797. );
  2798. tt_assert(found_A2 == 0);
  2799. /* There's no easy way of checking that we have included all the
  2800. * default Bridge & V3 Directory authorities, so let's assume that
  2801. * if the total count above is correct, we have the right ones.
  2802. */
  2803. }
  2804. {
  2805. /* fallback_dir_servers */
  2806. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2807. /* (No D0), (No B1), Default Bridge Authorities,
  2808. * (No A2), Default v3 Directory Authorities,
  2809. * (No Custom Fallback Directory), Default Fallback Directories */
  2810. tt_assert(smartlist_len(fallback_servers) ==
  2811. n_default_authorities + n_default_fallback_dir);
  2812. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2813. int found_D0 = 0;
  2814. SMARTLIST_FOREACH(fallback_servers,
  2815. dir_server_t *,
  2816. ds,
  2817. /* increment the found counter if dir_port matches */
  2818. found_D0 +=
  2819. (ds->dir_port == 60090 ?
  2820. 1 : 0)
  2821. );
  2822. tt_assert(found_D0 == 0);
  2823. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2824. int found_B1 = 0;
  2825. SMARTLIST_FOREACH(fallback_servers,
  2826. dir_server_t *,
  2827. ds,
  2828. /* increment the found counter if dir_port matches */
  2829. found_B1 +=
  2830. (ds->dir_port == 60091 ?
  2831. 1 : 0)
  2832. );
  2833. tt_assert(found_B1 == 0);
  2834. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2835. int found_A2 = 0;
  2836. SMARTLIST_FOREACH(fallback_servers,
  2837. dir_server_t *,
  2838. ds,
  2839. /* increment the found counter if dir_port matches */
  2840. found_A2 +=
  2841. (ds->dir_port == 60092 ?
  2842. 1 : 0)
  2843. );
  2844. tt_assert(found_A2 == 0);
  2845. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2846. int found_non_default_fallback = 0;
  2847. SMARTLIST_FOREACH(fallback_servers,
  2848. dir_server_t *,
  2849. ds,
  2850. /* increment the found counter if dir_port matches */
  2851. found_non_default_fallback +=
  2852. (ds->dir_port == 60093 ?
  2853. 1 : 0)
  2854. );
  2855. tt_assert(found_non_default_fallback == 0);
  2856. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2857. int found_default_fallback = 0;
  2858. SMARTLIST_FOREACH(fallback_servers,
  2859. dir_server_t *,
  2860. ds,
  2861. /* increment the found counter if dir_port matches */
  2862. found_default_fallback +=
  2863. (ds->dir_port == 60099 ?
  2864. 1 : 0)
  2865. );
  2866. tt_assert(found_default_fallback == 1);
  2867. /* There's no easy way of checking that we have included all the
  2868. * default Bridge & V3 Directory authorities, and the default
  2869. * Fallback Directories, so let's assume that if the total count
  2870. * above is correct, we have the right ones.
  2871. */
  2872. }
  2873. }
  2874. done:
  2875. clear_dir_servers();
  2876. tor_free(test_dir_authority->key);
  2877. tor_free(test_dir_authority->value);
  2878. tor_free(test_dir_authority);
  2879. tor_free(test_alt_dir_authority->key);
  2880. tor_free(test_alt_dir_authority->value);
  2881. tor_free(test_alt_dir_authority);
  2882. tor_free(test_alt_bridge_authority->key);
  2883. tor_free(test_alt_bridge_authority->value);
  2884. tor_free(test_alt_bridge_authority);
  2885. tor_free(test_fallback_directory->key);
  2886. tor_free(test_fallback_directory->value);
  2887. tor_free(test_fallback_directory);
  2888. options->DirAuthorities = NULL;
  2889. options->AlternateBridgeAuthority = NULL;
  2890. options->AlternateDirAuthority = NULL;
  2891. options->FallbackDir = NULL;
  2892. or_options_free(options);
  2893. UNMOCK(add_default_fallback_dir_servers);
  2894. }
  2895. static void
  2896. test_config_default_dir_servers(void *arg)
  2897. {
  2898. or_options_t *opts = NULL;
  2899. (void)arg;
  2900. int trusted_count = 0;
  2901. int fallback_count = 0;
  2902. opts = tor_malloc_zero(sizeof(or_options_t));
  2903. opts->UseDefaultFallbackDirs = 0;
  2904. consider_adding_dir_servers(opts, opts);
  2905. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2906. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2907. or_options_free(opts);
  2908. opts = NULL;
  2909. /* assume a release will never go out with less than 7 authorities */
  2910. tt_assert(trusted_count >= 7);
  2911. /* if we disable the default fallbacks, there must not be any extra */
  2912. tt_assert(fallback_count == trusted_count);
  2913. opts = tor_malloc_zero(sizeof(or_options_t));
  2914. opts->UseDefaultFallbackDirs = 1;
  2915. consider_adding_dir_servers(opts, opts);
  2916. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2917. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2918. or_options_free(opts);
  2919. opts = NULL;
  2920. /* assume a release will never go out with less than 7 authorities */
  2921. tt_assert(trusted_count >= 7);
  2922. /* XX/teor - allow for default fallbacks to be added without breaking
  2923. * the unit tests. Set a minimum fallback count once the list is stable. */
  2924. tt_assert(fallback_count >= trusted_count);
  2925. done:
  2926. or_options_free(opts);
  2927. }
  2928. static void
  2929. test_config_use_multiple_directories(void *arg)
  2930. {
  2931. (void)arg;
  2932. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  2933. /* Clients can use multiple directory mirrors for bootstrap */
  2934. memset(options, 0, sizeof(or_options_t));
  2935. options->ClientOnly = 1;
  2936. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2937. == 1);
  2938. /* Bridge Clients can use multiple directory mirrors for bootstrap */
  2939. memset(options, 0, sizeof(or_options_t));
  2940. options->UseBridges = 1;
  2941. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2942. == 1);
  2943. /* Bridge Relays (Bridges) must act like clients, and use multiple
  2944. * directory mirrors for bootstrap */
  2945. memset(options, 0, sizeof(or_options_t));
  2946. options->BridgeRelay = 1;
  2947. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2948. == 1);
  2949. /* Clients set to FetchDirInfoEarly must fetch it from the authorities */
  2950. memset(options, 0, sizeof(or_options_t));
  2951. options->FetchDirInfoEarly = 1;
  2952. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2953. == 0);
  2954. /* OR servers must fetch the consensus from the authorities */
  2955. memset(options, 0, sizeof(or_options_t));
  2956. options->ORPort_set = 1;
  2957. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  2958. == 0);
  2959. done:
  2960. tor_free(options);
  2961. }
  2962. static void
  2963. test_config_default_fallback_dirs(void *arg)
  2964. {
  2965. const char *fallback[] = {
  2966. #include "../or/fallback_dirs.inc"
  2967. NULL
  2968. };
  2969. int n_included_fallback_dirs = 0;
  2970. int n_added_fallback_dirs = 0;
  2971. (void)arg;
  2972. clear_dir_servers();
  2973. while (fallback[n_included_fallback_dirs])
  2974. n_included_fallback_dirs++;
  2975. add_default_fallback_dir_servers();
  2976. n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
  2977. tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
  2978. done:
  2979. clear_dir_servers();
  2980. }
  2981. #define CONFIG_TEST(name, flags) \
  2982. { #name, test_config_ ## name, flags, NULL, NULL }
  2983. struct testcase_t config_tests[] = {
  2984. CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
  2985. CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
  2986. CONFIG_TEST(parsing_trusted_dir_server, 0),
  2987. CONFIG_TEST(parsing_fallback_dir_server, 0),
  2988. CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
  2989. CONFIG_TEST(adding_dir_servers, TT_FORK),
  2990. CONFIG_TEST(default_dir_servers, TT_FORK),
  2991. CONFIG_TEST(default_fallback_dirs, 0),
  2992. CONFIG_TEST(resolve_my_address, TT_FORK),
  2993. CONFIG_TEST(addressmap, 0),
  2994. CONFIG_TEST(parse_bridge_line, 0),
  2995. CONFIG_TEST(parse_transport_options_line, 0),
  2996. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  2997. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  2998. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  2999. CONFIG_TEST(fix_my_family, 0),
  3000. CONFIG_TEST(use_multiple_directories, 0),
  3001. END_OF_TESTCASES
  3002. };