test_config.c 161 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565
  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. #define ROUTERSET_PRIVATE
  9. #include "or.h"
  10. #include "address.h"
  11. #include "addressmap.h"
  12. #include "circuitmux_ewma.h"
  13. #include "circuitbuild.h"
  14. #include "config.h"
  15. #include "confparse.h"
  16. #include "connection.h"
  17. #include "connection_edge.h"
  18. #include "test.h"
  19. #include "util.h"
  20. #include "address.h"
  21. #include "connection_or.h"
  22. #include "control.h"
  23. #include "cpuworker.h"
  24. #include "dirserv.h"
  25. #include "dirvote.h"
  26. #include "dns.h"
  27. #include "entrynodes.h"
  28. #include "transports.h"
  29. #include "ext_orport.h"
  30. #include "geoip.h"
  31. #include "hibernate.h"
  32. #include "main.h"
  33. #include "networkstatus.h"
  34. #include "nodelist.h"
  35. #include "policies.h"
  36. #include "rendclient.h"
  37. #include "rendservice.h"
  38. #include "router.h"
  39. #include "routerlist.h"
  40. #include "routerset.h"
  41. #include "statefile.h"
  42. #include "test.h"
  43. #include "transports.h"
  44. #include "util.h"
  45. static void
  46. test_config_addressmap(void *arg)
  47. {
  48. char buf[1024];
  49. char address[256];
  50. time_t expires = TIME_MAX;
  51. (void)arg;
  52. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  53. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  54. "MapAddress *.google.com *.torserver.exit\n"
  55. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  56. "MapAddress *.cn.com www.cnn.com\n"
  57. "MapAddress *.cnn.com www.cnn.com\n"
  58. "MapAddress ex.com www.cnn.com\n"
  59. "MapAddress ey.com *.cnn.com\n"
  60. "MapAddress www.torproject.org 1.1.1.1\n"
  61. "MapAddress other.torproject.org "
  62. "this.torproject.org.otherserver.exit\n"
  63. "MapAddress test.torproject.org 2.2.2.2\n"
  64. "MapAddress www.google.com 3.3.3.3\n"
  65. "MapAddress www.example.org 4.4.4.4\n"
  66. "MapAddress 4.4.4.4 7.7.7.7\n"
  67. "MapAddress 4.4.4.4 5.5.5.5\n"
  68. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  69. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  70. , sizeof(buf));
  71. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  72. config_register_addressmaps(get_options());
  73. /* Use old interface for now, so we don't need to rewrite the unit tests */
  74. #define addressmap_rewrite(a,s,eo,ao) \
  75. addressmap_rewrite((a),(s), ~0, (eo),(ao))
  76. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  77. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  78. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  79. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  80. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  81. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  82. /* Where no mapping for FQDN match on top-level domain */
  83. /* MapAddress .google.com .torserver.exit */
  84. strlcpy(address, "reader.google.com", sizeof(address));
  85. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  86. tt_str_op(address,OP_EQ, "reader.torserver.exit");
  87. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  88. strlcpy(address, "reader.yahoo.com", sizeof(address));
  89. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  90. tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
  91. /*MapAddress *.cnn.com www.cnn.com */
  92. strlcpy(address, "cnn.com", sizeof(address));
  93. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  94. tt_str_op(address,OP_EQ, "www.cnn.com");
  95. /* MapAddress .cn.com www.cnn.com */
  96. strlcpy(address, "www.cn.com", sizeof(address));
  97. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  98. tt_str_op(address,OP_EQ, "www.cnn.com");
  99. /* MapAddress ex.com www.cnn.com - no match */
  100. strlcpy(address, "www.ex.com", sizeof(address));
  101. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  102. /* MapAddress ey.com *.cnn.com - invalid expression */
  103. strlcpy(address, "ey.com", sizeof(address));
  104. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  105. /* Where mapping for FQDN match on FQDN */
  106. strlcpy(address, "www.google.com", sizeof(address));
  107. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  108. tt_str_op(address,OP_EQ, "3.3.3.3");
  109. strlcpy(address, "www.torproject.org", sizeof(address));
  110. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  111. tt_str_op(address,OP_EQ, "1.1.1.1");
  112. strlcpy(address, "other.torproject.org", sizeof(address));
  113. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  114. tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
  115. strlcpy(address, "test.torproject.org", sizeof(address));
  116. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  117. tt_str_op(address,OP_EQ, "2.2.2.2");
  118. /* Test a chain of address mappings and the order in which they were added:
  119. "MapAddress www.example.org 4.4.4.4"
  120. "MapAddress 4.4.4.4 7.7.7.7"
  121. "MapAddress 4.4.4.4 5.5.5.5"
  122. */
  123. strlcpy(address, "www.example.org", sizeof(address));
  124. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  125. tt_str_op(address,OP_EQ, "5.5.5.5");
  126. /* Test infinite address mapping results in no change */
  127. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  128. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  129. tt_str_op(address,OP_EQ, "www.infiniteloop.org");
  130. /* Test we don't find false positives */
  131. strlcpy(address, "www.example.com", sizeof(address));
  132. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  133. /* Test top-level-domain matching a bit harder */
  134. config_free_lines(get_options_mutable()->AddressMap);
  135. addressmap_clear_configured();
  136. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  137. "MapAddress *.torproject.org 1.1.1.1\n"
  138. "MapAddress *.net 2.2.2.2\n"
  139. , sizeof(buf));
  140. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  141. config_register_addressmaps(get_options());
  142. strlcpy(address, "www.abc.com", sizeof(address));
  143. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  144. tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
  145. strlcpy(address, "www.def.com", sizeof(address));
  146. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  147. tt_str_op(address,OP_EQ, "www.def.torserver.exit");
  148. strlcpy(address, "www.torproject.org", sizeof(address));
  149. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  150. tt_str_op(address,OP_EQ, "1.1.1.1");
  151. strlcpy(address, "test.torproject.org", sizeof(address));
  152. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  153. tt_str_op(address,OP_EQ, "1.1.1.1");
  154. strlcpy(address, "torproject.net", sizeof(address));
  155. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  156. tt_str_op(address,OP_EQ, "2.2.2.2");
  157. /* We don't support '*' as a mapping directive */
  158. config_free_lines(get_options_mutable()->AddressMap);
  159. addressmap_clear_configured();
  160. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  161. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  162. config_register_addressmaps(get_options());
  163. strlcpy(address, "www.abc.com", sizeof(address));
  164. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  165. strlcpy(address, "www.def.net", sizeof(address));
  166. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  167. strlcpy(address, "www.torproject.org", sizeof(address));
  168. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  169. #undef addressmap_rewrite
  170. done:
  171. config_free_lines(get_options_mutable()->AddressMap);
  172. get_options_mutable()->AddressMap = NULL;
  173. addressmap_free_all();
  174. }
  175. static int
  176. is_private_dir(const char* path)
  177. {
  178. struct stat st;
  179. int r = stat(path, &st);
  180. if (r) {
  181. return 0;
  182. }
  183. #if !defined (_WIN32)
  184. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  185. return 0;
  186. }
  187. #endif
  188. return 1;
  189. }
  190. static void
  191. test_config_check_or_create_data_subdir(void *arg)
  192. {
  193. or_options_t *options = get_options_mutable();
  194. char *datadir;
  195. const char *subdir = "test_stats";
  196. char *subpath;
  197. struct stat st;
  198. int r;
  199. #if !defined (_WIN32)
  200. unsigned group_permission;
  201. #endif
  202. (void)arg;
  203. tor_free(options->DataDirectory);
  204. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  205. subpath = get_datadir_fname(subdir);
  206. #if defined (_WIN32)
  207. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  208. #else
  209. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  210. #endif
  211. r = stat(subpath, &st);
  212. // The subdirectory shouldn't exist yet,
  213. // but should be created by the call to check_or_create_data_subdir.
  214. tt_assert(r && (errno == ENOENT));
  215. tt_assert(!check_or_create_data_subdir(subdir));
  216. tt_assert(is_private_dir(subpath));
  217. // The check should return 0, if the directory already exists
  218. // and is private to the user.
  219. tt_assert(!check_or_create_data_subdir(subdir));
  220. r = stat(subpath, &st);
  221. if (r) {
  222. tt_abort_perror("stat");
  223. }
  224. #if !defined (_WIN32)
  225. group_permission = st.st_mode | 0070;
  226. r = chmod(subpath, group_permission);
  227. if (r) {
  228. tt_abort_perror("chmod");
  229. }
  230. // If the directory exists, but its mode is too permissive
  231. // a call to check_or_create_data_subdir should reset the mode.
  232. tt_assert(!is_private_dir(subpath));
  233. tt_assert(!check_or_create_data_subdir(subdir));
  234. tt_assert(is_private_dir(subpath));
  235. #endif
  236. done:
  237. rmdir(subpath);
  238. tor_free(datadir);
  239. tor_free(subpath);
  240. }
  241. static void
  242. test_config_write_to_data_subdir(void *arg)
  243. {
  244. or_options_t* options = get_options_mutable();
  245. char *datadir;
  246. char *cp = NULL;
  247. const char* subdir = "test_stats";
  248. const char* fname = "test_file";
  249. const char* str =
  250. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  251. "elitr, sed diam nonumy eirmod\n"
  252. "tempor invidunt ut labore et dolore magna aliquyam\n"
  253. "erat, sed diam voluptua.\n"
  254. "At vero eos et accusam et justo duo dolores et ea\n"
  255. "rebum. Stet clita kasd gubergren,\n"
  256. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  257. "Lorem ipsum dolor sit amet,\n"
  258. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  259. "tempor invidunt ut labore et dolore\n"
  260. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  261. "accusam et justo duo dolores et\n"
  262. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  263. "sanctus est Lorem ipsum dolor sit amet.";
  264. char* filepath = NULL;
  265. (void)arg;
  266. tor_free(options->DataDirectory);
  267. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  268. filepath = get_datadir_fname2(subdir, fname);
  269. #if defined (_WIN32)
  270. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  271. #else
  272. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  273. #endif
  274. // Write attempt shoudl fail, if subdirectory doesn't exist.
  275. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  276. tt_assert(! check_or_create_data_subdir(subdir));
  277. // Content of file after write attempt should be
  278. // equal to the original string.
  279. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  280. cp = read_file_to_str(filepath, 0, NULL);
  281. tt_str_op(cp,OP_EQ, str);
  282. tor_free(cp);
  283. // A second write operation should overwrite the old content.
  284. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  285. cp = read_file_to_str(filepath, 0, NULL);
  286. tt_str_op(cp,OP_EQ, str);
  287. tor_free(cp);
  288. done:
  289. (void) unlink(filepath);
  290. rmdir(options->DataDirectory);
  291. tor_free(datadir);
  292. tor_free(filepath);
  293. tor_free(cp);
  294. }
  295. /* Test helper function: Make sure that a bridge line gets parsed
  296. * properly. Also make sure that the resulting bridge_line_t structure
  297. * has its fields set correctly. */
  298. static void
  299. good_bridge_line_test(const char *string, const char *test_addrport,
  300. const char *test_digest, const char *test_transport,
  301. const smartlist_t *test_socks_args)
  302. {
  303. char *tmp = NULL;
  304. bridge_line_t *bridge_line = parse_bridge_line(string);
  305. tt_assert(bridge_line);
  306. /* test addrport */
  307. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  308. tt_str_op(test_addrport,OP_EQ, tmp);
  309. tor_free(tmp);
  310. /* If we were asked to validate a digest, but we did not get a
  311. digest after parsing, we failed. */
  312. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  313. tt_assert(0);
  314. /* If we were not asked to validate a digest, and we got a digest
  315. after parsing, we failed again. */
  316. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  317. tt_assert(0);
  318. /* If we were asked to validate a digest, and we got a digest after
  319. parsing, make sure it's correct. */
  320. if (test_digest) {
  321. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  322. tor_strlower(tmp);
  323. tt_str_op(test_digest,OP_EQ, tmp);
  324. tor_free(tmp);
  325. }
  326. /* If we were asked to validate a transport name, make sure tha it
  327. matches with the transport name that was parsed. */
  328. if (test_transport && !bridge_line->transport_name)
  329. tt_assert(0);
  330. if (!test_transport && bridge_line->transport_name)
  331. tt_assert(0);
  332. if (test_transport)
  333. tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
  334. /* Validate the SOCKS argument smartlist. */
  335. if (test_socks_args && !bridge_line->socks_args)
  336. tt_assert(0);
  337. if (!test_socks_args && bridge_line->socks_args)
  338. tt_assert(0);
  339. if (test_socks_args)
  340. tt_assert(smartlist_strings_eq(test_socks_args,
  341. bridge_line->socks_args));
  342. done:
  343. tor_free(tmp);
  344. bridge_line_free(bridge_line);
  345. }
  346. /* Test helper function: Make sure that a bridge line is
  347. * unparseable. */
  348. static void
  349. bad_bridge_line_test(const char *string)
  350. {
  351. bridge_line_t *bridge_line = parse_bridge_line(string);
  352. if (bridge_line)
  353. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  354. tt_assert(!bridge_line);
  355. done:
  356. bridge_line_free(bridge_line);
  357. }
  358. static void
  359. test_config_parse_bridge_line(void *arg)
  360. {
  361. (void) arg;
  362. good_bridge_line_test("192.0.2.1:4123",
  363. "192.0.2.1:4123", NULL, NULL, NULL);
  364. good_bridge_line_test("192.0.2.1",
  365. "192.0.2.1:443", NULL, NULL, NULL);
  366. good_bridge_line_test("transport [::1]",
  367. "[::1]:443", NULL, "transport", NULL);
  368. good_bridge_line_test("transport 192.0.2.1:12 "
  369. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  370. "192.0.2.1:12",
  371. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  372. "transport", NULL);
  373. {
  374. smartlist_t *sl_tmp = smartlist_new();
  375. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  376. good_bridge_line_test("transport 192.0.2.1:12 "
  377. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  378. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  379. "transport", sl_tmp);
  380. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  381. smartlist_free(sl_tmp);
  382. }
  383. {
  384. smartlist_t *sl_tmp = smartlist_new();
  385. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  386. smartlist_add_asprintf(sl_tmp, "z=z");
  387. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  388. "192.0.2.1:12", NULL, "transport", sl_tmp);
  389. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  390. smartlist_free(sl_tmp);
  391. }
  392. {
  393. smartlist_t *sl_tmp = smartlist_new();
  394. smartlist_add_asprintf(sl_tmp, "dub=come");
  395. smartlist_add_asprintf(sl_tmp, "save=me");
  396. good_bridge_line_test("transport 192.0.2.1:12 "
  397. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  398. "dub=come save=me",
  399. "192.0.2.1:12",
  400. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  401. "transport", sl_tmp);
  402. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  403. smartlist_free(sl_tmp);
  404. }
  405. good_bridge_line_test("192.0.2.1:1231 "
  406. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  407. "192.0.2.1:1231",
  408. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  409. NULL, NULL);
  410. /* Empty line */
  411. bad_bridge_line_test("");
  412. /* bad transport name */
  413. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  414. /* weird ip address */
  415. bad_bridge_line_test("a.b.c.d");
  416. /* invalid fpr */
  417. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  418. /* no k=v in the end */
  419. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  420. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  421. /* no addrport */
  422. bad_bridge_line_test("asdw");
  423. /* huge k=v value that can't fit in SOCKS fields */
  424. bad_bridge_line_test(
  425. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  426. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  427. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  428. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  429. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  430. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  431. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  432. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  433. "aa=b");
  434. }
  435. static void
  436. test_config_parse_transport_options_line(void *arg)
  437. {
  438. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  439. (void) arg;
  440. { /* too small line */
  441. options_sl = get_options_from_transport_options_line("valley", NULL);
  442. tt_assert(!options_sl);
  443. }
  444. { /* no k=v values */
  445. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  446. tt_assert(!options_sl);
  447. }
  448. { /* correct line, but wrong transport specified */
  449. options_sl =
  450. get_options_from_transport_options_line("trebuchet k=v", "rook");
  451. tt_assert(!options_sl);
  452. }
  453. { /* correct -- no transport specified */
  454. sl_tmp = smartlist_new();
  455. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  456. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  457. options_sl =
  458. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  459. NULL);
  460. tt_assert(options_sl);
  461. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  462. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  463. smartlist_free(sl_tmp);
  464. sl_tmp = NULL;
  465. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  466. smartlist_free(options_sl);
  467. options_sl = NULL;
  468. }
  469. { /* correct -- correct transport specified */
  470. sl_tmp = smartlist_new();
  471. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  472. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  473. options_sl =
  474. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  475. "rook");
  476. tt_assert(options_sl);
  477. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  478. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  479. smartlist_free(sl_tmp);
  480. sl_tmp = NULL;
  481. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  482. smartlist_free(options_sl);
  483. options_sl = NULL;
  484. }
  485. done:
  486. if (options_sl) {
  487. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  488. smartlist_free(options_sl);
  489. }
  490. if (sl_tmp) {
  491. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  492. smartlist_free(sl_tmp);
  493. }
  494. }
  495. /* Mocks needed for the transport plugin line test */
  496. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  497. char **proxy_argv, int is_server);
  498. static int transport_add_from_config_mock(const tor_addr_t *addr,
  499. uint16_t port, const char *name,
  500. int socks_ver);
  501. static int transport_is_needed_mock(const char *transport_name);
  502. static int pt_kickstart_proxy_mock_call_count = 0;
  503. static int transport_add_from_config_mock_call_count = 0;
  504. static int transport_is_needed_mock_call_count = 0;
  505. static int transport_is_needed_mock_return = 0;
  506. static void
  507. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  508. char **proxy_argv, int is_server)
  509. {
  510. (void) transport_list;
  511. (void) proxy_argv;
  512. (void) is_server;
  513. /* XXXX check that args are as expected. */
  514. ++pt_kickstart_proxy_mock_call_count;
  515. free_execve_args(proxy_argv);
  516. }
  517. static int
  518. transport_add_from_config_mock(const tor_addr_t *addr,
  519. uint16_t port, const char *name,
  520. int socks_ver)
  521. {
  522. (void) addr;
  523. (void) port;
  524. (void) name;
  525. (void) socks_ver;
  526. /* XXXX check that args are as expected. */
  527. ++transport_add_from_config_mock_call_count;
  528. return 0;
  529. }
  530. static int
  531. transport_is_needed_mock(const char *transport_name)
  532. {
  533. (void) transport_name;
  534. /* XXXX check that arg is as expected. */
  535. ++transport_is_needed_mock_call_count;
  536. return transport_is_needed_mock_return;
  537. }
  538. /**
  539. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  540. * options.
  541. */
  542. static void
  543. test_config_parse_transport_plugin_line(void *arg)
  544. {
  545. (void)arg;
  546. or_options_t *options = get_options_mutable();
  547. int r, tmp;
  548. int old_pt_kickstart_proxy_mock_call_count;
  549. int old_transport_add_from_config_mock_call_count;
  550. int old_transport_is_needed_mock_call_count;
  551. /* Bad transport lines - too short */
  552. r = parse_transport_line(options, "bad", 1, 0);
  553. tt_assert(r < 0);
  554. r = parse_transport_line(options, "bad", 1, 1);
  555. tt_assert(r < 0);
  556. r = parse_transport_line(options, "bad bad", 1, 0);
  557. tt_assert(r < 0);
  558. r = parse_transport_line(options, "bad bad", 1, 1);
  559. tt_assert(r < 0);
  560. /* Test transport list parsing */
  561. r = parse_transport_line(options,
  562. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  563. tt_assert(r == 0);
  564. r = parse_transport_line(options,
  565. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  566. tt_assert(r == 0);
  567. r = parse_transport_line(options,
  568. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  569. tt_assert(r == 0);
  570. r = parse_transport_line(options,
  571. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  572. tt_assert(r == 0);
  573. /* Bad transport identifiers */
  574. r = parse_transport_line(options,
  575. "transport_* exec /usr/bin/fake-transport", 1, 0);
  576. tt_assert(r < 0);
  577. r = parse_transport_line(options,
  578. "transport_* exec /usr/bin/fake-transport", 1, 1);
  579. tt_assert(r < 0);
  580. /* Check SOCKS cases for client transport */
  581. r = parse_transport_line(options,
  582. "transport_1 socks4 1.2.3.4:567", 1, 0);
  583. tt_assert(r == 0);
  584. r = parse_transport_line(options,
  585. "transport_1 socks5 1.2.3.4:567", 1, 0);
  586. tt_assert(r == 0);
  587. /* Proxy case for server transport */
  588. r = parse_transport_line(options,
  589. "transport_1 proxy 1.2.3.4:567", 1, 1);
  590. tt_assert(r == 0);
  591. /* Multiple-transport error exit */
  592. r = parse_transport_line(options,
  593. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  594. tt_assert(r < 0);
  595. r = parse_transport_line(options,
  596. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  597. /* No port error exit */
  598. r = parse_transport_line(options,
  599. "transport_1 socks5 1.2.3.4", 1, 0);
  600. tt_assert(r < 0);
  601. r = parse_transport_line(options,
  602. "transport_1 proxy 1.2.3.4", 1, 1);
  603. tt_assert(r < 0);
  604. /* Unparsable address error exit */
  605. r = parse_transport_line(options,
  606. "transport_1 socks5 1.2.3:6x7", 1, 0);
  607. tt_assert(r < 0);
  608. r = parse_transport_line(options,
  609. "transport_1 proxy 1.2.3:6x7", 1, 1);
  610. tt_assert(r < 0);
  611. /* "Strange {Client|Server}TransportPlugin field" error exit */
  612. r = parse_transport_line(options,
  613. "transport_1 foo bar", 1, 0);
  614. tt_assert(r < 0);
  615. r = parse_transport_line(options,
  616. "transport_1 foo bar", 1, 1);
  617. tt_assert(r < 0);
  618. /* No sandbox mode error exit */
  619. tmp = options->Sandbox;
  620. options->Sandbox = 1;
  621. r = parse_transport_line(options,
  622. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  623. tt_assert(r < 0);
  624. r = parse_transport_line(options,
  625. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  626. tt_assert(r < 0);
  627. options->Sandbox = tmp;
  628. /*
  629. * These final test cases cover code paths that only activate without
  630. * validate_only, so they need mocks in place.
  631. */
  632. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  633. old_pt_kickstart_proxy_mock_call_count =
  634. pt_kickstart_proxy_mock_call_count;
  635. r = parse_transport_line(options,
  636. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  637. tt_assert(r == 0);
  638. tt_assert(pt_kickstart_proxy_mock_call_count ==
  639. old_pt_kickstart_proxy_mock_call_count + 1);
  640. UNMOCK(pt_kickstart_proxy);
  641. /* This one hits a log line in the !validate_only case only */
  642. r = parse_transport_line(options,
  643. "transport_1 proxy 1.2.3.4:567", 0, 1);
  644. tt_assert(r == 0);
  645. /* Check mocked client transport cases */
  646. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  647. MOCK(transport_add_from_config, transport_add_from_config_mock);
  648. MOCK(transport_is_needed, transport_is_needed_mock);
  649. /* Unnecessary transport case */
  650. transport_is_needed_mock_return = 0;
  651. old_pt_kickstart_proxy_mock_call_count =
  652. pt_kickstart_proxy_mock_call_count;
  653. old_transport_add_from_config_mock_call_count =
  654. transport_add_from_config_mock_call_count;
  655. old_transport_is_needed_mock_call_count =
  656. transport_is_needed_mock_call_count;
  657. r = parse_transport_line(options,
  658. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  659. /* Should have succeeded */
  660. tt_assert(r == 0);
  661. /* transport_is_needed() should have been called */
  662. tt_assert(transport_is_needed_mock_call_count ==
  663. old_transport_is_needed_mock_call_count + 1);
  664. /*
  665. * pt_kickstart_proxy() and transport_add_from_config() should
  666. * not have been called.
  667. */
  668. tt_assert(pt_kickstart_proxy_mock_call_count ==
  669. old_pt_kickstart_proxy_mock_call_count);
  670. tt_assert(transport_add_from_config_mock_call_count ==
  671. old_transport_add_from_config_mock_call_count);
  672. /* Necessary transport case */
  673. transport_is_needed_mock_return = 1;
  674. old_pt_kickstart_proxy_mock_call_count =
  675. pt_kickstart_proxy_mock_call_count;
  676. old_transport_add_from_config_mock_call_count =
  677. transport_add_from_config_mock_call_count;
  678. old_transport_is_needed_mock_call_count =
  679. transport_is_needed_mock_call_count;
  680. r = parse_transport_line(options,
  681. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  682. /* Should have succeeded */
  683. tt_assert(r == 0);
  684. /*
  685. * transport_is_needed() and pt_kickstart_proxy() should have been
  686. * called.
  687. */
  688. tt_assert(pt_kickstart_proxy_mock_call_count ==
  689. old_pt_kickstart_proxy_mock_call_count + 1);
  690. tt_assert(transport_is_needed_mock_call_count ==
  691. old_transport_is_needed_mock_call_count + 1);
  692. /* transport_add_from_config() should not have been called. */
  693. tt_assert(transport_add_from_config_mock_call_count ==
  694. old_transport_add_from_config_mock_call_count);
  695. /* proxy case */
  696. transport_is_needed_mock_return = 1;
  697. old_pt_kickstart_proxy_mock_call_count =
  698. pt_kickstart_proxy_mock_call_count;
  699. old_transport_add_from_config_mock_call_count =
  700. transport_add_from_config_mock_call_count;
  701. old_transport_is_needed_mock_call_count =
  702. transport_is_needed_mock_call_count;
  703. r = parse_transport_line(options,
  704. "transport_1 socks5 1.2.3.4:567", 0, 0);
  705. /* Should have succeeded */
  706. tt_assert(r == 0);
  707. /*
  708. * transport_is_needed() and transport_add_from_config() should have
  709. * been called.
  710. */
  711. tt_assert(transport_add_from_config_mock_call_count ==
  712. old_transport_add_from_config_mock_call_count + 1);
  713. tt_assert(transport_is_needed_mock_call_count ==
  714. old_transport_is_needed_mock_call_count + 1);
  715. /* pt_kickstart_proxy() should not have been called. */
  716. tt_assert(pt_kickstart_proxy_mock_call_count ==
  717. old_pt_kickstart_proxy_mock_call_count);
  718. /* Done with mocked client transport cases */
  719. UNMOCK(transport_is_needed);
  720. UNMOCK(transport_add_from_config);
  721. UNMOCK(pt_kickstart_proxy);
  722. done:
  723. /* Make sure we undo all mocks */
  724. UNMOCK(pt_kickstart_proxy);
  725. UNMOCK(transport_add_from_config);
  726. UNMOCK(transport_is_needed);
  727. return;
  728. }
  729. // Tests if an options with MyFamily fingerprints missing '$' normalises
  730. // them correctly and also ensure it also works with multiple fingerprints
  731. static void
  732. test_config_fix_my_family(void *arg)
  733. {
  734. char *err = NULL;
  735. const char *family = "$1111111111111111111111111111111111111111, "
  736. "1111111111111111111111111111111111111112, "
  737. "$1111111111111111111111111111111111111113";
  738. or_options_t* options = options_new();
  739. or_options_t* defaults = options_new();
  740. (void) arg;
  741. options_init(options);
  742. options_init(defaults);
  743. options->MyFamily = tor_strdup(family);
  744. options_validate(NULL, options, defaults, 0, &err) ;
  745. if (err != NULL) {
  746. TT_FAIL(("options_validate failed: %s", err));
  747. }
  748. tt_str_op(options->MyFamily,OP_EQ,
  749. "$1111111111111111111111111111111111111111, "
  750. "$1111111111111111111111111111111111111112, "
  751. "$1111111111111111111111111111111111111113");
  752. done:
  753. if (err != NULL) {
  754. tor_free(err);
  755. }
  756. or_options_free(options);
  757. or_options_free(defaults);
  758. }
  759. static int n_hostname_01010101 = 0;
  760. /** This mock function is meant to replace tor_lookup_hostname().
  761. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  762. * This function increments <b>n_hostname_01010101</b> counter by one
  763. * every time it is called.
  764. */
  765. static int
  766. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  767. {
  768. n_hostname_01010101++;
  769. if (name && addr) {
  770. *addr = ntohl(0x01010101);
  771. }
  772. return 0;
  773. }
  774. static int n_hostname_localhost = 0;
  775. /** This mock function is meant to replace tor_lookup_hostname().
  776. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  777. * This function increments <b>n_hostname_localhost</b> counter by one
  778. * every time it is called.
  779. */
  780. static int
  781. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  782. {
  783. n_hostname_localhost++;
  784. if (name && addr) {
  785. *addr = 0x7f000001;
  786. }
  787. return 0;
  788. }
  789. static int n_hostname_failure = 0;
  790. /** This mock function is meant to replace tor_lookup_hostname().
  791. * It pretends to fail by returning -1 to caller. Also, this function
  792. * increments <b>n_hostname_failure</b> every time it is called.
  793. */
  794. static int
  795. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  796. {
  797. (void)name;
  798. (void)addr;
  799. n_hostname_failure++;
  800. return -1;
  801. }
  802. static int n_gethostname_replacement = 0;
  803. /** This mock function is meant to replace tor_gethostname(). It
  804. * responds with string "onionrouter!" as hostname. This function
  805. * increments <b>n_gethostname_replacement</b> by one every time
  806. * it is called.
  807. */
  808. static int
  809. tor_gethostname_replacement(char *name, size_t namelen)
  810. {
  811. n_gethostname_replacement++;
  812. if (name && namelen) {
  813. strlcpy(name,"onionrouter!",namelen);
  814. }
  815. return 0;
  816. }
  817. static int n_gethostname_localhost = 0;
  818. /** This mock function is meant to replace tor_gethostname(). It
  819. * responds with string "127.0.0.1" as hostname. This function
  820. * increments <b>n_gethostname_localhost</b> by one every time
  821. * it is called.
  822. */
  823. static int
  824. tor_gethostname_localhost(char *name, size_t namelen)
  825. {
  826. n_gethostname_localhost++;
  827. if (name && namelen) {
  828. strlcpy(name,"127.0.0.1",namelen);
  829. }
  830. return 0;
  831. }
  832. static int n_gethostname_failure = 0;
  833. /** This mock function is meant to replace tor_gethostname.
  834. * It pretends to fail by returning -1. This function increments
  835. * <b>n_gethostname_failure</b> by one every time it is called.
  836. */
  837. static int
  838. tor_gethostname_failure(char *name, size_t namelen)
  839. {
  840. (void)name;
  841. (void)namelen;
  842. n_gethostname_failure++;
  843. return -1;
  844. }
  845. static int n_get_interface_address = 0;
  846. /** This mock function is meant to replace get_interface_address().
  847. * It answers with address 8.8.8.8. This function increments
  848. * <b>n_get_interface_address</b> by one every time it is called.
  849. */
  850. static int
  851. get_interface_address_08080808(int severity, uint32_t *addr)
  852. {
  853. (void)severity;
  854. n_get_interface_address++;
  855. if (addr) {
  856. *addr = ntohl(0x08080808);
  857. }
  858. return 0;
  859. }
  860. static int n_get_interface_address6 = 0;
  861. static sa_family_t last_address6_family;
  862. /** This mock function is meant to replace get_interface_address6().
  863. * It answers with IP address 9.9.9.9 iff both of the following are true:
  864. * - <b>family</b> is AF_INET
  865. * - <b>addr</b> pointer is not NULL.
  866. * This function increments <b>n_get_interface_address6</b> by one every
  867. * time it is called.
  868. */
  869. static int
  870. get_interface_address6_replacement(int severity, sa_family_t family,
  871. tor_addr_t *addr)
  872. {
  873. (void)severity;
  874. last_address6_family = family;
  875. n_get_interface_address6++;
  876. if ((family != AF_INET) || !addr) {
  877. return -1;
  878. }
  879. tor_addr_from_ipv4h(addr,0x09090909);
  880. return 0;
  881. }
  882. static int n_get_interface_address_failure = 0;
  883. /**
  884. * This mock function is meant to replace get_interface_address().
  885. * It pretends to fail getting interface address by returning -1.
  886. * <b>n_get_interface_address_failure</b> is incremented by one
  887. * every time this function is called.
  888. */
  889. static int
  890. get_interface_address_failure(int severity, uint32_t *addr)
  891. {
  892. (void)severity;
  893. (void)addr;
  894. n_get_interface_address_failure++;
  895. return -1;
  896. }
  897. static int n_get_interface_address6_failure = 0;
  898. /**
  899. * This mock function is meant to replace get_interface_addres6().
  900. * It will pretend to fail by return -1.
  901. * <b>n_get_interface_address6_failure</b> is incremented by one
  902. * every time this function is called and <b>last_address6_family</b>
  903. * is assigned the value of <b>family</b> argument.
  904. */
  905. static int
  906. get_interface_address6_failure(int severity, sa_family_t family,
  907. tor_addr_t *addr)
  908. {
  909. (void)severity;
  910. (void)addr;
  911. n_get_interface_address6_failure++;
  912. last_address6_family = family;
  913. return -1;
  914. }
  915. static void
  916. test_config_resolve_my_address(void *arg)
  917. {
  918. or_options_t *options;
  919. uint32_t resolved_addr;
  920. const char *method_used;
  921. char *hostname_out = NULL;
  922. int retval;
  923. int prev_n_hostname_01010101;
  924. int prev_n_hostname_localhost;
  925. int prev_n_hostname_failure;
  926. int prev_n_gethostname_replacement;
  927. int prev_n_gethostname_failure;
  928. int prev_n_gethostname_localhost;
  929. int prev_n_get_interface_address;
  930. int prev_n_get_interface_address_failure;
  931. int prev_n_get_interface_address6;
  932. int prev_n_get_interface_address6_failure;
  933. (void)arg;
  934. options = options_new();
  935. options_init(options);
  936. /*
  937. * CASE 1:
  938. * If options->Address is a valid IPv4 address string, we want
  939. * the corresponding address to be parsed and returned.
  940. */
  941. options->Address = tor_strdup("128.52.128.105");
  942. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  943. &method_used,&hostname_out);
  944. tt_want(retval == 0);
  945. tt_want_str_op(method_used,==,"CONFIGURED");
  946. tt_want(hostname_out == NULL);
  947. tt_assert(resolved_addr == 0x80348069);
  948. tor_free(options->Address);
  949. /*
  950. * CASE 2:
  951. * If options->Address is a valid DNS address, we want resolve_my_address()
  952. * function to ask tor_lookup_hostname() for help with resolving it
  953. * and return the address that was resolved (in host order).
  954. */
  955. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  956. tor_free(options->Address);
  957. options->Address = tor_strdup("www.torproject.org");
  958. prev_n_hostname_01010101 = n_hostname_01010101;
  959. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  960. &method_used,&hostname_out);
  961. tt_want(retval == 0);
  962. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  963. tt_want_str_op(method_used,==,"RESOLVED");
  964. tt_want_str_op(hostname_out,==,"www.torproject.org");
  965. tt_assert(resolved_addr == 0x01010101);
  966. UNMOCK(tor_lookup_hostname);
  967. tor_free(options->Address);
  968. tor_free(hostname_out);
  969. /*
  970. * CASE 3:
  971. * Given that options->Address is NULL, we want resolve_my_address()
  972. * to try and use tor_gethostname() to get hostname AND use
  973. * tor_lookup_hostname() to get IP address.
  974. */
  975. resolved_addr = 0;
  976. tor_free(options->Address);
  977. options->Address = NULL;
  978. MOCK(tor_gethostname,tor_gethostname_replacement);
  979. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  980. prev_n_gethostname_replacement = n_gethostname_replacement;
  981. prev_n_hostname_01010101 = n_hostname_01010101;
  982. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  983. &method_used,&hostname_out);
  984. tt_want(retval == 0);
  985. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  986. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  987. tt_want_str_op(method_used,==,"GETHOSTNAME");
  988. tt_want_str_op(hostname_out,==,"onionrouter!");
  989. tt_assert(resolved_addr == 0x01010101);
  990. UNMOCK(tor_gethostname);
  991. UNMOCK(tor_lookup_hostname);
  992. tor_free(hostname_out);
  993. /*
  994. * CASE 4:
  995. * Given that options->Address is a local host address, we want
  996. * resolve_my_address() function to fail.
  997. */
  998. resolved_addr = 0;
  999. tor_free(options->Address);
  1000. options->Address = tor_strdup("127.0.0.1");
  1001. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1002. &method_used,&hostname_out);
  1003. tt_want(resolved_addr == 0);
  1004. tt_assert(retval == -1);
  1005. tor_free(options->Address);
  1006. tor_free(hostname_out);
  1007. /*
  1008. * CASE 5:
  1009. * We want resolve_my_address() to fail if DNS address in options->Address
  1010. * cannot be resolved.
  1011. */
  1012. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1013. prev_n_hostname_failure = n_hostname_failure;
  1014. tor_free(options->Address);
  1015. options->Address = tor_strdup("www.tor-project.org");
  1016. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1017. &method_used,&hostname_out);
  1018. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1019. tt_assert(retval == -1);
  1020. UNMOCK(tor_lookup_hostname);
  1021. tor_free(options->Address);
  1022. tor_free(hostname_out);
  1023. /*
  1024. * CASE 6:
  1025. * If options->Address is NULL AND gettting local hostname fails, we want
  1026. * resolve_my_address() to fail as well.
  1027. */
  1028. MOCK(tor_gethostname,tor_gethostname_failure);
  1029. prev_n_gethostname_failure = n_gethostname_failure;
  1030. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1031. &method_used,&hostname_out);
  1032. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1033. tt_assert(retval == -1);
  1034. UNMOCK(tor_gethostname);
  1035. tor_free(hostname_out);
  1036. /*
  1037. * CASE 7:
  1038. * We want resolve_my_address() to try and get network interface address via
  1039. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1040. * resolved into IP address.
  1041. */
  1042. MOCK(tor_gethostname,tor_gethostname_replacement);
  1043. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1044. MOCK(get_interface_address,get_interface_address_08080808);
  1045. prev_n_gethostname_replacement = n_gethostname_replacement;
  1046. prev_n_get_interface_address = n_get_interface_address;
  1047. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1048. &method_used,&hostname_out);
  1049. tt_want(retval == 0);
  1050. tt_want_int_op(n_gethostname_replacement, ==,
  1051. prev_n_gethostname_replacement + 1);
  1052. tt_want_int_op(n_get_interface_address, ==,
  1053. prev_n_get_interface_address + 1);
  1054. tt_want_str_op(method_used,==,"INTERFACE");
  1055. tt_want(hostname_out == NULL);
  1056. tt_assert(resolved_addr == 0x08080808);
  1057. UNMOCK(get_interface_address);
  1058. tor_free(hostname_out);
  1059. /*
  1060. * CASE 8:
  1061. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1062. * is unresolvable. We want resolve_my_address to fail if
  1063. * get_interface_address() fails.
  1064. */
  1065. MOCK(get_interface_address,get_interface_address_failure);
  1066. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1067. prev_n_gethostname_replacement = n_gethostname_replacement;
  1068. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1069. &method_used,&hostname_out);
  1070. tt_want(n_get_interface_address_failure ==
  1071. prev_n_get_interface_address_failure + 1);
  1072. tt_want(n_gethostname_replacement ==
  1073. prev_n_gethostname_replacement + 1);
  1074. tt_assert(retval == -1);
  1075. UNMOCK(get_interface_address);
  1076. tor_free(hostname_out);
  1077. /*
  1078. * CASE 9:
  1079. * Given that options->Address is NULL AND tor_lookup_hostname()
  1080. * fails AND hostname returned by gethostname() resolves
  1081. * to local IP address, we want resolve_my_address() function to
  1082. * call get_interface_address6(.,AF_INET,.) and return IP address
  1083. * the latter function has found.
  1084. */
  1085. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1086. MOCK(tor_gethostname,tor_gethostname_replacement);
  1087. MOCK(get_interface_address6,get_interface_address6_replacement);
  1088. prev_n_gethostname_replacement = n_gethostname_replacement;
  1089. prev_n_hostname_failure = n_hostname_failure;
  1090. prev_n_get_interface_address6 = n_get_interface_address6;
  1091. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1092. &method_used,&hostname_out);
  1093. tt_want(last_address6_family == AF_INET);
  1094. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1095. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1096. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1097. tt_want(retval == 0);
  1098. tt_want_str_op(method_used,==,"INTERFACE");
  1099. tt_assert(resolved_addr == 0x09090909);
  1100. UNMOCK(tor_lookup_hostname);
  1101. UNMOCK(tor_gethostname);
  1102. UNMOCK(get_interface_address6);
  1103. tor_free(hostname_out);
  1104. /*
  1105. * CASE 10: We want resolve_my_address() to fail if all of the following
  1106. * are true:
  1107. * 1. options->Address is not NULL
  1108. * 2. ... but it cannot be converted to struct in_addr by
  1109. * tor_inet_aton()
  1110. * 3. ... and tor_lookup_hostname() fails to resolve the
  1111. * options->Address
  1112. */
  1113. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1114. prev_n_hostname_failure = n_hostname_failure;
  1115. tor_free(options->Address);
  1116. options->Address = tor_strdup("some_hostname");
  1117. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1118. &method_used,&hostname_out);
  1119. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1120. tt_assert(retval == -1);
  1121. UNMOCK(tor_gethostname);
  1122. UNMOCK(tor_lookup_hostname);
  1123. tor_free(hostname_out);
  1124. /*
  1125. * CASE 11:
  1126. * Suppose the following sequence of events:
  1127. * 1. options->Address is NULL
  1128. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1129. * if running on.
  1130. * 3. Hostname from previous step cannot be converted to
  1131. * address by using tor_inet_aton() function.
  1132. * 4. However, tor_lookup_hostname() succeds in resolving the
  1133. * hostname from step 2.
  1134. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1135. * to be internal.
  1136. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1137. * IPv4
  1138. *
  1139. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1140. * and address from step 6.
  1141. */
  1142. tor_free(options->Address);
  1143. options->Address = NULL;
  1144. MOCK(tor_gethostname,tor_gethostname_replacement);
  1145. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1146. MOCK(get_interface_address6,get_interface_address6_replacement);
  1147. prev_n_gethostname_replacement = n_gethostname_replacement;
  1148. prev_n_hostname_localhost = n_hostname_localhost;
  1149. prev_n_get_interface_address6 = n_get_interface_address6;
  1150. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1151. &method_used,&hostname_out);
  1152. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1153. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1154. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1155. tt_str_op(method_used,==,"INTERFACE");
  1156. tt_assert(!hostname_out);
  1157. tt_assert(retval == 0);
  1158. /*
  1159. * CASE 11b:
  1160. * 1-5 as above.
  1161. * 6. get_interface_address6() fails.
  1162. *
  1163. * In this subcase, we want resolve_my_address() to fail.
  1164. */
  1165. UNMOCK(get_interface_address6);
  1166. MOCK(get_interface_address6,get_interface_address6_failure);
  1167. prev_n_gethostname_replacement = n_gethostname_replacement;
  1168. prev_n_hostname_localhost = n_hostname_localhost;
  1169. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1170. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1171. &method_used,&hostname_out);
  1172. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1173. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1174. tt_want(n_get_interface_address6_failure ==
  1175. prev_n_get_interface_address6_failure + 1);
  1176. tt_assert(retval == -1);
  1177. UNMOCK(tor_gethostname);
  1178. UNMOCK(tor_lookup_hostname);
  1179. UNMOCK(get_interface_address6);
  1180. /* CASE 12:
  1181. * Suppose the following happens:
  1182. * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
  1183. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1184. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1185. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1186. * internal.
  1187. *
  1188. * In this case, we want resolve_my_address() to fail.
  1189. */
  1190. tor_free(options->Address);
  1191. options->Address = NULL;
  1192. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1193. MOCK(tor_gethostname,tor_gethostname_localhost);
  1194. prev_n_gethostname_localhost = n_gethostname_localhost;
  1195. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1196. &method_used,&hostname_out);
  1197. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1198. tt_assert(retval == -1);
  1199. UNMOCK(tor_gethostname);
  1200. done:
  1201. tor_free(options->Address);
  1202. tor_free(options->DirAuthorities);
  1203. or_options_free(options);
  1204. tor_free(hostname_out);
  1205. UNMOCK(tor_gethostname);
  1206. UNMOCK(tor_lookup_hostname);
  1207. UNMOCK(get_interface_address);
  1208. UNMOCK(get_interface_address6);
  1209. UNMOCK(tor_gethostname);
  1210. }
  1211. static void
  1212. test_config_adding_trusted_dir_server(void *arg)
  1213. {
  1214. (void)arg;
  1215. const char digest[DIGEST_LEN] = "";
  1216. dir_server_t *ds = NULL;
  1217. tor_addr_port_t ipv6;
  1218. int rv = -1;
  1219. clear_dir_servers();
  1220. routerlist_free_all();
  1221. /* create a trusted ds without an IPv6 address and port */
  1222. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1223. NULL, V3_DIRINFO, 1.0);
  1224. tt_assert(ds);
  1225. dir_server_add(ds);
  1226. tt_assert(get_n_authorities(V3_DIRINFO) == 1);
  1227. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1228. /* create a trusted ds with an IPv6 address and port */
  1229. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1230. tt_assert(rv == 0);
  1231. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
  1232. NULL, V3_DIRINFO, 1.0);
  1233. tt_assert(ds);
  1234. dir_server_add(ds);
  1235. tt_assert(get_n_authorities(V3_DIRINFO) == 2);
  1236. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1237. done:
  1238. clear_dir_servers();
  1239. routerlist_free_all();
  1240. }
  1241. static void
  1242. test_config_adding_fallback_dir_server(void *arg)
  1243. {
  1244. (void)arg;
  1245. const char digest[DIGEST_LEN] = "";
  1246. dir_server_t *ds = NULL;
  1247. tor_addr_t ipv4;
  1248. tor_addr_port_t ipv6;
  1249. int rv = -1;
  1250. clear_dir_servers();
  1251. routerlist_free_all();
  1252. rv = tor_addr_parse(&ipv4, "127.0.0.1");
  1253. tt_assert(rv == AF_INET);
  1254. /* create a trusted ds without an IPv6 address and port */
  1255. ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
  1256. tt_assert(ds);
  1257. dir_server_add(ds);
  1258. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1259. /* create a trusted ds with an IPv6 address and port */
  1260. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1261. tt_assert(rv == 0);
  1262. ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
  1263. tt_assert(ds);
  1264. dir_server_add(ds);
  1265. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 2);
  1266. done:
  1267. clear_dir_servers();
  1268. routerlist_free_all();
  1269. }
  1270. /* No secrets here:
  1271. * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1272. * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1273. * with added spaces
  1274. */
  1275. #define TEST_DIR_AUTH_LINE_START \
  1276. "foobar orport=12345 " \
  1277. "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
  1278. #define TEST_DIR_AUTH_LINE_END \
  1279. "1.2.3.4:54321 " \
  1280. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1281. #define TEST_DIR_AUTH_IPV6_FLAG \
  1282. "ipv6=[feed::beef]:9 "
  1283. static void
  1284. test_config_parsing_trusted_dir_server(void *arg)
  1285. {
  1286. (void)arg;
  1287. int rv = -1;
  1288. /* parse a trusted dir server without an IPv6 address and port */
  1289. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1290. TEST_DIR_AUTH_LINE_END,
  1291. V3_DIRINFO, 1);
  1292. tt_assert(rv == 0);
  1293. /* parse a trusted dir server with an IPv6 address and port */
  1294. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1295. TEST_DIR_AUTH_IPV6_FLAG
  1296. TEST_DIR_AUTH_LINE_END,
  1297. V3_DIRINFO, 1);
  1298. tt_assert(rv == 0);
  1299. /* Since we are only validating, there is no cleanup. */
  1300. done:
  1301. ;
  1302. }
  1303. #undef TEST_DIR_AUTH_LINE_START
  1304. #undef TEST_DIR_AUTH_LINE_END
  1305. #undef TEST_DIR_AUTH_IPV6_FLAG
  1306. /* No secrets here:
  1307. * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
  1308. */
  1309. #define TEST_DIR_FALLBACK_LINE \
  1310. "1.2.3.4:54321 orport=12345 " \
  1311. "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
  1312. #define TEST_DIR_FALLBACK_IPV6_FLAG \
  1313. "ipv6=[2015:c0de::deed]:9"
  1314. static void
  1315. test_config_parsing_fallback_dir_server(void *arg)
  1316. {
  1317. (void)arg;
  1318. int rv = -1;
  1319. /* parse a trusted dir server without an IPv6 address and port */
  1320. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
  1321. tt_assert(rv == 0);
  1322. /* parse a trusted dir server with an IPv6 address and port */
  1323. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
  1324. TEST_DIR_FALLBACK_IPV6_FLAG,
  1325. 1);
  1326. tt_assert(rv == 0);
  1327. /* Since we are only validating, there is no cleanup. */
  1328. done:
  1329. ;
  1330. }
  1331. #undef TEST_DIR_FALLBACK_LINE
  1332. #undef TEST_DIR_FALLBACK_IPV6_FLAG
  1333. static void
  1334. test_config_adding_default_trusted_dir_servers(void *arg)
  1335. {
  1336. (void)arg;
  1337. clear_dir_servers();
  1338. routerlist_free_all();
  1339. /* Assume we only have one bridge authority */
  1340. add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
  1341. tt_assert(get_n_authorities(BRIDGE_DIRINFO) == 1);
  1342. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 1);
  1343. /* Assume we have nine V3 authorities */
  1344. add_default_trusted_dir_authorities(V3_DIRINFO);
  1345. tt_assert(get_n_authorities(V3_DIRINFO) == 9);
  1346. tt_assert(smartlist_len(router_get_fallback_dir_servers()) == 10);
  1347. done:
  1348. clear_dir_servers();
  1349. routerlist_free_all();
  1350. }
  1351. static int n_add_default_fallback_dir_servers_known_default = 0;
  1352. /**
  1353. * This mock function is meant to replace add_default_fallback_dir_servers().
  1354. * It will parse and add one known default fallback dir server,
  1355. * which has a dir_port of 99.
  1356. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1357. * one every time this function is called.
  1358. */
  1359. static void
  1360. add_default_fallback_dir_servers_known_default(void)
  1361. {
  1362. int i;
  1363. const char *fallback[] = {
  1364. "127.0.0.1:60099 orport=9009 "
  1365. "id=0923456789012345678901234567890123456789",
  1366. NULL
  1367. };
  1368. for (i=0; fallback[i]; i++) {
  1369. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1370. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1371. fallback[i]);
  1372. }
  1373. }
  1374. n_add_default_fallback_dir_servers_known_default++;
  1375. }
  1376. /* Test all the different combinations of adding dir servers */
  1377. static void
  1378. test_config_adding_dir_servers(void *arg)
  1379. {
  1380. (void)arg;
  1381. /* allocate options */
  1382. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  1383. /* Allocate and populate configuration lines:
  1384. *
  1385. * Use the same format as the hard-coded directories in
  1386. * add_default_trusted_dir_authorities().
  1387. * Zeroing the structure has the same effect as initialising to:
  1388. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1389. */
  1390. config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
  1391. test_dir_authority->key = tor_strdup("DirAuthority");
  1392. test_dir_authority->value = tor_strdup(
  1393. "D0 orport=9000 "
  1394. "v3ident=0023456789012345678901234567890123456789 "
  1395. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1396. );
  1397. config_line_t *test_alt_bridge_authority = tor_malloc_zero(
  1398. sizeof(config_line_t));
  1399. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1400. test_alt_bridge_authority->value = tor_strdup(
  1401. "B1 orport=9001 bridge "
  1402. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1403. );
  1404. config_line_t *test_alt_dir_authority = tor_malloc_zero(
  1405. sizeof(config_line_t));
  1406. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1407. test_alt_dir_authority->value = tor_strdup(
  1408. "A2 orport=9002 "
  1409. "v3ident=0223456789012345678901234567890123456789 "
  1410. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1411. );
  1412. /* Use the format specified in the manual page */
  1413. config_line_t *test_fallback_directory = tor_malloc_zero(
  1414. sizeof(config_line_t));
  1415. test_fallback_directory->key = tor_strdup("FallbackDir");
  1416. test_fallback_directory->value = tor_strdup(
  1417. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1418. );
  1419. /* We need to know if add_default_fallback_dir_servers is called,
  1420. * whatever the size of the list in fallback_dirs.inc,
  1421. * so we use a version of add_default_fallback_dir_servers that adds
  1422. * one known default fallback directory. */
  1423. MOCK(add_default_fallback_dir_servers,
  1424. add_default_fallback_dir_servers_known_default);
  1425. /* There are 16 different cases, covering each combination of set/NULL for:
  1426. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1427. * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
  1428. * But validate_dir_servers() ensures that:
  1429. * "You cannot set both DirAuthority and Alternate*Authority."
  1430. * This reduces the number of cases to 10.
  1431. *
  1432. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1433. * So 1001 or case 9 is:
  1434. * DirAuthorities set,
  1435. * AlternateBridgeAuthority NULL,
  1436. * AlternateDirAuthority NULL
  1437. * FallbackDir set
  1438. * The valid cases are cases 0-9 counting using this method, as every case
  1439. * greater than or equal to 10 = 1010 is invalid.
  1440. *
  1441. * 1. Outcome: Use Set Directory Authorities
  1442. * - No Default Authorities
  1443. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1444. * if they are set
  1445. * Cases expected to yield this outcome:
  1446. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1447. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1448. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1449. *
  1450. * 2. Outcome: Use Set Bridge Authority
  1451. * - Use Default Non-Bridge Directory Authorities
  1452. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1453. * Cases expected to yield this outcome:
  1454. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1455. * AlternateBridgeAuthority is set, and
  1456. * AlternateDirAuthority is NULL)
  1457. *
  1458. * 3. Outcome: Use Set Alternate Directory Authority
  1459. * - Use Default Bridge Authorities
  1460. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1461. * Cases expected to yield this outcome:
  1462. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1463. * are both NULL, but AlternateDirAuthority is set)
  1464. *
  1465. * 4. Outcome: Use Set Custom Fallback Directory
  1466. * - Use Default Bridge & Directory Authorities
  1467. * Cases expected to yield this outcome:
  1468. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1469. * are all NULL, but FallbackDir is set)
  1470. *
  1471. * 5. Outcome: Use All Defaults
  1472. * - Use Default Bridge & Directory Authorities, and
  1473. * Default Fallback Directories
  1474. * Cases expected to yield this outcome:
  1475. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1476. * and FallbackDir are all NULL)
  1477. */
  1478. /*
  1479. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1480. * are hard-coded into this build.
  1481. * This code makes some assumptions about the implementation.
  1482. * If they are wrong, one or more of cases 0-5 could fail.
  1483. */
  1484. int n_default_alt_bridge_authority = 0;
  1485. int n_default_alt_dir_authority = 0;
  1486. int n_default_fallback_dir = 0;
  1487. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1488. + (n_default_alt_dir_authority))
  1489. /* Pre-Count Number of Authorities of Each Type
  1490. * Use 0000: No Directory Authorities or Fallback Directories Set
  1491. */
  1492. {
  1493. /* clear fallback dirs counter */
  1494. n_add_default_fallback_dir_servers_known_default = 0;
  1495. /* clear options*/
  1496. memset(options, 0, sizeof(or_options_t));
  1497. /* clear any previous dir servers:
  1498. consider_adding_dir_servers() should do this anyway */
  1499. clear_dir_servers();
  1500. /* assign options: 0000 */
  1501. options->DirAuthorities = NULL;
  1502. options->AlternateBridgeAuthority = NULL;
  1503. options->AlternateDirAuthority = NULL;
  1504. options->FallbackDir = NULL;
  1505. options->UseDefaultFallbackDirs = 1;
  1506. /* parse options - ensure we always update by passing NULL old_options */
  1507. consider_adding_dir_servers(options, NULL);
  1508. /* check outcome */
  1509. /* we must have added the default fallback dirs */
  1510. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  1511. /* we have more fallbacks than just the authorities */
  1512. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1513. {
  1514. /* fallback_dir_servers */
  1515. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1516. /* Count Bridge Authorities */
  1517. SMARTLIST_FOREACH(fallback_servers,
  1518. dir_server_t *,
  1519. ds,
  1520. /* increment the found counter if it's a bridge auth */
  1521. n_default_alt_bridge_authority +=
  1522. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1523. 1 : 0)
  1524. );
  1525. /* If we have no default bridge authority, something has gone wrong */
  1526. tt_assert(n_default_alt_bridge_authority >= 1);
  1527. /* Count v3 Authorities */
  1528. SMARTLIST_FOREACH(fallback_servers,
  1529. dir_server_t *,
  1530. ds,
  1531. /* increment found counter if it's a v3 auth */
  1532. n_default_alt_dir_authority +=
  1533. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1534. 1 : 0)
  1535. );
  1536. /* If we have no default authorities, something has gone really wrong */
  1537. tt_assert(n_default_alt_dir_authority >= 1);
  1538. /* Calculate Fallback Directory Count */
  1539. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1540. n_default_alt_bridge_authority -
  1541. n_default_alt_dir_authority);
  1542. /* If we have a negative count, something has gone really wrong,
  1543. * or some authorities aren't being added as fallback directories.
  1544. * (networkstatus_consensus_can_use_extra_fallbacks depends on all
  1545. * authorities being fallback directories.) */
  1546. tt_assert(n_default_fallback_dir >= 0);
  1547. }
  1548. }
  1549. /*
  1550. * 1. Outcome: Use Set Directory Authorities
  1551. * - No Default Authorities
  1552. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1553. * if they are set
  1554. * Cases expected to yield this outcome:
  1555. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1556. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1557. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1558. */
  1559. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1560. AlternateDirAuthority Not Set, FallbackDir Set */
  1561. {
  1562. /* clear fallback dirs counter */
  1563. n_add_default_fallback_dir_servers_known_default = 0;
  1564. /* clear options*/
  1565. memset(options, 0, sizeof(or_options_t));
  1566. /* clear any previous dir servers:
  1567. consider_adding_dir_servers() should do this anyway */
  1568. clear_dir_servers();
  1569. /* assign options: 1001 */
  1570. options->DirAuthorities = test_dir_authority;
  1571. options->AlternateBridgeAuthority = NULL;
  1572. options->AlternateDirAuthority = NULL;
  1573. options->FallbackDir = test_fallback_directory;
  1574. options->UseDefaultFallbackDirs = 1;
  1575. /* parse options - ensure we always update by passing NULL old_options */
  1576. consider_adding_dir_servers(options, NULL);
  1577. /* check outcome */
  1578. /* we must not have added the default fallback dirs */
  1579. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1580. /* we have more fallbacks than just the authorities */
  1581. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1582. {
  1583. /* trusted_dir_servers */
  1584. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1585. /* D0, (No B1), (No A2) */
  1586. tt_assert(smartlist_len(dir_servers) == 1);
  1587. /* DirAuthority - D0 - dir_port: 60090 */
  1588. int found_D0 = 0;
  1589. SMARTLIST_FOREACH(dir_servers,
  1590. dir_server_t *,
  1591. ds,
  1592. /* increment the found counter if dir_port matches */
  1593. found_D0 +=
  1594. (ds->dir_port == 60090 ?
  1595. 1 : 0)
  1596. );
  1597. tt_assert(found_D0 == 1);
  1598. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1599. int found_B1 = 0;
  1600. SMARTLIST_FOREACH(dir_servers,
  1601. dir_server_t *,
  1602. ds,
  1603. /* increment the found counter if dir_port matches */
  1604. found_B1 +=
  1605. (ds->dir_port == 60091 ?
  1606. 1 : 0)
  1607. );
  1608. tt_assert(found_B1 == 0);
  1609. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1610. int found_A2 = 0;
  1611. SMARTLIST_FOREACH(dir_servers,
  1612. dir_server_t *,
  1613. ds,
  1614. /* increment the found counter if dir_port matches */
  1615. found_A2 +=
  1616. (ds->dir_port == 60092 ?
  1617. 1 : 0)
  1618. );
  1619. tt_assert(found_A2 == 0);
  1620. }
  1621. {
  1622. /* fallback_dir_servers */
  1623. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1624. /* D0, (No B1), (No A2), Custom Fallback */
  1625. tt_assert(smartlist_len(fallback_servers) == 2);
  1626. /* DirAuthority - D0 - dir_port: 60090 */
  1627. int found_D0 = 0;
  1628. SMARTLIST_FOREACH(fallback_servers,
  1629. dir_server_t *,
  1630. ds,
  1631. /* increment the found counter if dir_port matches */
  1632. found_D0 +=
  1633. (ds->dir_port == 60090 ?
  1634. 1 : 0)
  1635. );
  1636. tt_assert(found_D0 == 1);
  1637. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1638. int found_B1 = 0;
  1639. SMARTLIST_FOREACH(fallback_servers,
  1640. dir_server_t *,
  1641. ds,
  1642. /* increment the found counter if dir_port matches */
  1643. found_B1 +=
  1644. (ds->dir_port == 60091 ?
  1645. 1 : 0)
  1646. );
  1647. tt_assert(found_B1 == 0);
  1648. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1649. int found_A2 = 0;
  1650. SMARTLIST_FOREACH(fallback_servers,
  1651. dir_server_t *,
  1652. ds,
  1653. /* increment the found counter if dir_port matches */
  1654. found_A2 +=
  1655. (ds->dir_port == 60092 ?
  1656. 1 : 0)
  1657. );
  1658. tt_assert(found_A2 == 0);
  1659. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1660. int found_non_default_fallback = 0;
  1661. SMARTLIST_FOREACH(fallback_servers,
  1662. dir_server_t *,
  1663. ds,
  1664. /* increment the found counter if dir_port matches */
  1665. found_non_default_fallback +=
  1666. (ds->dir_port == 60093 ?
  1667. 1 : 0)
  1668. );
  1669. tt_assert(found_non_default_fallback == 1);
  1670. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1671. int found_default_fallback = 0;
  1672. SMARTLIST_FOREACH(fallback_servers,
  1673. dir_server_t *,
  1674. ds,
  1675. /* increment the found counter if dir_port matches */
  1676. found_default_fallback +=
  1677. (ds->dir_port == 60099 ?
  1678. 1 : 0)
  1679. );
  1680. tt_assert(found_default_fallback == 0);
  1681. }
  1682. }
  1683. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1684. {
  1685. /* clear fallback dirs counter */
  1686. n_add_default_fallback_dir_servers_known_default = 0;
  1687. /* clear options*/
  1688. memset(options, 0, sizeof(or_options_t));
  1689. /* clear any previous dir servers:
  1690. consider_adding_dir_servers() should do this anyway */
  1691. clear_dir_servers();
  1692. /* assign options: 1000 */
  1693. options->DirAuthorities = test_dir_authority;
  1694. options->AlternateBridgeAuthority = NULL;
  1695. options->AlternateDirAuthority = NULL;
  1696. options->FallbackDir = NULL;
  1697. options->UseDefaultFallbackDirs = 1;
  1698. /* parse options - ensure we always update by passing NULL old_options */
  1699. consider_adding_dir_servers(options, NULL);
  1700. /* check outcome */
  1701. /* we must not have added the default fallback dirs */
  1702. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1703. /* we just have the authorities */
  1704. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1705. {
  1706. /* trusted_dir_servers */
  1707. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1708. /* D0, (No B1), (No A2) */
  1709. tt_assert(smartlist_len(dir_servers) == 1);
  1710. /* DirAuthority - D0 - dir_port: 60090 */
  1711. int found_D0 = 0;
  1712. SMARTLIST_FOREACH(dir_servers,
  1713. dir_server_t *,
  1714. ds,
  1715. /* increment the found counter if dir_port matches */
  1716. found_D0 +=
  1717. (ds->dir_port == 60090 ?
  1718. 1 : 0)
  1719. );
  1720. tt_assert(found_D0 == 1);
  1721. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1722. int found_B1 = 0;
  1723. SMARTLIST_FOREACH(dir_servers,
  1724. dir_server_t *,
  1725. ds,
  1726. /* increment the found counter if dir_port matches */
  1727. found_B1 +=
  1728. (ds->dir_port == 60091 ?
  1729. 1 : 0)
  1730. );
  1731. tt_assert(found_B1 == 0);
  1732. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1733. int found_A2 = 0;
  1734. SMARTLIST_FOREACH(dir_servers,
  1735. dir_server_t *,
  1736. ds,
  1737. /* increment the found counter if dir_port matches */
  1738. found_A2 +=
  1739. (ds->dir_port == 60092 ?
  1740. 1 : 0)
  1741. );
  1742. tt_assert(found_A2 == 0);
  1743. }
  1744. {
  1745. /* fallback_dir_servers */
  1746. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1747. /* D0, (No B1), (No A2), (No Fallback) */
  1748. tt_assert(smartlist_len(fallback_servers) == 1);
  1749. /* DirAuthority - D0 - dir_port: 60090 */
  1750. int found_D0 = 0;
  1751. SMARTLIST_FOREACH(fallback_servers,
  1752. dir_server_t *,
  1753. ds,
  1754. /* increment the found counter if dir_port matches */
  1755. found_D0 +=
  1756. (ds->dir_port == 60090 ?
  1757. 1 : 0)
  1758. );
  1759. tt_assert(found_D0 == 1);
  1760. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1761. int found_B1 = 0;
  1762. SMARTLIST_FOREACH(fallback_servers,
  1763. dir_server_t *,
  1764. ds,
  1765. /* increment the found counter if dir_port matches */
  1766. found_B1 +=
  1767. (ds->dir_port == 60091 ?
  1768. 1 : 0)
  1769. );
  1770. tt_assert(found_B1 == 0);
  1771. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1772. int found_A2 = 0;
  1773. SMARTLIST_FOREACH(fallback_servers,
  1774. dir_server_t *,
  1775. ds,
  1776. /* increment the found counter if dir_port matches */
  1777. found_A2 +=
  1778. (ds->dir_port == 60092 ?
  1779. 1 : 0)
  1780. );
  1781. tt_assert(found_A2 == 0);
  1782. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1783. int found_non_default_fallback = 0;
  1784. SMARTLIST_FOREACH(fallback_servers,
  1785. dir_server_t *,
  1786. ds,
  1787. /* increment the found counter if dir_port matches */
  1788. found_non_default_fallback +=
  1789. (ds->dir_port == 60093 ?
  1790. 1 : 0)
  1791. );
  1792. tt_assert(found_non_default_fallback == 0);
  1793. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1794. int found_default_fallback = 0;
  1795. SMARTLIST_FOREACH(fallback_servers,
  1796. dir_server_t *,
  1797. ds,
  1798. /* increment the found counter if dir_port matches */
  1799. found_default_fallback +=
  1800. (ds->dir_port == 60099 ?
  1801. 1 : 0)
  1802. );
  1803. tt_assert(found_default_fallback == 0);
  1804. }
  1805. }
  1806. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1807. {
  1808. /* clear fallback dirs counter */
  1809. n_add_default_fallback_dir_servers_known_default = 0;
  1810. /* clear options*/
  1811. memset(options, 0, sizeof(or_options_t));
  1812. /* clear any previous dir servers:
  1813. consider_adding_dir_servers() should do this anyway */
  1814. clear_dir_servers();
  1815. /* assign options: 0111 */
  1816. options->DirAuthorities = NULL;
  1817. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1818. options->AlternateDirAuthority = test_alt_dir_authority;
  1819. options->FallbackDir = test_fallback_directory;
  1820. options->UseDefaultFallbackDirs = 1;
  1821. /* parse options - ensure we always update by passing NULL old_options */
  1822. consider_adding_dir_servers(options, NULL);
  1823. /* check outcome */
  1824. /* we must not have added the default fallback dirs */
  1825. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1826. /* we have more fallbacks than just the authorities */
  1827. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1828. {
  1829. /* trusted_dir_servers */
  1830. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1831. /* (No D0), B1, A2 */
  1832. tt_assert(smartlist_len(dir_servers) == 2);
  1833. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1834. int found_D0 = 0;
  1835. SMARTLIST_FOREACH(dir_servers,
  1836. dir_server_t *,
  1837. ds,
  1838. /* increment the found counter if dir_port matches */
  1839. found_D0 +=
  1840. (ds->dir_port == 60090 ?
  1841. 1 : 0)
  1842. );
  1843. tt_assert(found_D0 == 0);
  1844. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1845. int found_B1 = 0;
  1846. SMARTLIST_FOREACH(dir_servers,
  1847. dir_server_t *,
  1848. ds,
  1849. /* increment the found counter if dir_port matches */
  1850. found_B1 +=
  1851. (ds->dir_port == 60091 ?
  1852. 1 : 0)
  1853. );
  1854. tt_assert(found_B1 == 1);
  1855. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1856. int found_A2 = 0;
  1857. SMARTLIST_FOREACH(dir_servers,
  1858. dir_server_t *,
  1859. ds,
  1860. /* increment the found counter if dir_port matches */
  1861. found_A2 +=
  1862. (ds->dir_port == 60092 ?
  1863. 1 : 0)
  1864. );
  1865. tt_assert(found_A2 == 1);
  1866. }
  1867. {
  1868. /* fallback_dir_servers */
  1869. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1870. /* (No D0), B1, A2, Custom Fallback */
  1871. tt_assert(smartlist_len(fallback_servers) == 3);
  1872. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1873. int found_D0 = 0;
  1874. SMARTLIST_FOREACH(fallback_servers,
  1875. dir_server_t *,
  1876. ds,
  1877. /* increment the found counter if dir_port matches */
  1878. found_D0 +=
  1879. (ds->dir_port == 60090 ?
  1880. 1 : 0)
  1881. );
  1882. tt_assert(found_D0 == 0);
  1883. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1884. int found_B1 = 0;
  1885. SMARTLIST_FOREACH(fallback_servers,
  1886. dir_server_t *,
  1887. ds,
  1888. /* increment the found counter if dir_port matches */
  1889. found_B1 +=
  1890. (ds->dir_port == 60091 ?
  1891. 1 : 0)
  1892. );
  1893. tt_assert(found_B1 == 1);
  1894. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1895. int found_A2 = 0;
  1896. SMARTLIST_FOREACH(fallback_servers,
  1897. dir_server_t *,
  1898. ds,
  1899. /* increment the found counter if dir_port matches */
  1900. found_A2 +=
  1901. (ds->dir_port == 60092 ?
  1902. 1 : 0)
  1903. );
  1904. tt_assert(found_A2 == 1);
  1905. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1906. int found_non_default_fallback = 0;
  1907. SMARTLIST_FOREACH(fallback_servers,
  1908. dir_server_t *,
  1909. ds,
  1910. /* increment the found counter if dir_port matches */
  1911. found_non_default_fallback +=
  1912. (ds->dir_port == 60093 ?
  1913. 1 : 0)
  1914. );
  1915. tt_assert(found_non_default_fallback == 1);
  1916. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1917. int found_default_fallback = 0;
  1918. SMARTLIST_FOREACH(fallback_servers,
  1919. dir_server_t *,
  1920. ds,
  1921. /* increment the found counter if dir_port matches */
  1922. found_default_fallback +=
  1923. (ds->dir_port == 60099 ?
  1924. 1 : 0)
  1925. );
  1926. tt_assert(found_default_fallback == 0);
  1927. }
  1928. }
  1929. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  1930. AlternateDirAuthority Set, FallbackDir Not Set */
  1931. {
  1932. /* clear fallback dirs counter */
  1933. n_add_default_fallback_dir_servers_known_default = 0;
  1934. /* clear options*/
  1935. memset(options, 0, sizeof(or_options_t));
  1936. /* clear any previous dir servers:
  1937. consider_adding_dir_servers() should do this anyway */
  1938. clear_dir_servers();
  1939. /* assign options: 0110 */
  1940. options->DirAuthorities = NULL;
  1941. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1942. options->AlternateDirAuthority = test_alt_dir_authority;
  1943. options->FallbackDir = NULL;
  1944. options->UseDefaultFallbackDirs = 1;
  1945. /* parse options - ensure we always update by passing NULL old_options */
  1946. consider_adding_dir_servers(options, NULL);
  1947. /* check outcome */
  1948. /* we must not have added the default fallback dirs */
  1949. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1950. /* we have more fallbacks than just the authorities */
  1951. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1952. {
  1953. /* trusted_dir_servers */
  1954. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1955. /* (No D0), B1, A2 */
  1956. tt_assert(smartlist_len(dir_servers) == 2);
  1957. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1958. int found_D0 = 0;
  1959. SMARTLIST_FOREACH(dir_servers,
  1960. dir_server_t *,
  1961. ds,
  1962. /* increment the found counter if dir_port matches */
  1963. found_D0 +=
  1964. (ds->dir_port == 60090 ?
  1965. 1 : 0)
  1966. );
  1967. tt_assert(found_D0 == 0);
  1968. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1969. int found_B1 = 0;
  1970. SMARTLIST_FOREACH(dir_servers,
  1971. dir_server_t *,
  1972. ds,
  1973. /* increment the found counter if dir_port matches */
  1974. found_B1 +=
  1975. (ds->dir_port == 60091 ?
  1976. 1 : 0)
  1977. );
  1978. tt_assert(found_B1 == 1);
  1979. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1980. int found_A2 = 0;
  1981. SMARTLIST_FOREACH(dir_servers,
  1982. dir_server_t *,
  1983. ds,
  1984. /* increment the found counter if dir_port matches */
  1985. found_A2 +=
  1986. (ds->dir_port == 60092 ?
  1987. 1 : 0)
  1988. );
  1989. tt_assert(found_A2 == 1);
  1990. }
  1991. {
  1992. /* fallback_dir_servers */
  1993. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1994. /* (No D0), B1, A2, (No Fallback) */
  1995. tt_assert(smartlist_len(fallback_servers) == 2);
  1996. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1997. int found_D0 = 0;
  1998. SMARTLIST_FOREACH(fallback_servers,
  1999. dir_server_t *,
  2000. ds,
  2001. /* increment the found counter if dir_port matches */
  2002. found_D0 +=
  2003. (ds->dir_port == 60090 ?
  2004. 1 : 0)
  2005. );
  2006. tt_assert(found_D0 == 0);
  2007. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2008. int found_B1 = 0;
  2009. SMARTLIST_FOREACH(fallback_servers,
  2010. dir_server_t *,
  2011. ds,
  2012. /* increment the found counter if dir_port matches */
  2013. found_B1 +=
  2014. (ds->dir_port == 60091 ?
  2015. 1 : 0)
  2016. );
  2017. tt_assert(found_B1 == 1);
  2018. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2019. int found_A2 = 0;
  2020. SMARTLIST_FOREACH(fallback_servers,
  2021. dir_server_t *,
  2022. ds,
  2023. /* increment the found counter if dir_port matches */
  2024. found_A2 +=
  2025. (ds->dir_port == 60092 ?
  2026. 1 : 0)
  2027. );
  2028. tt_assert(found_A2 == 1);
  2029. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2030. int found_non_default_fallback = 0;
  2031. SMARTLIST_FOREACH(fallback_servers,
  2032. dir_server_t *,
  2033. ds,
  2034. /* increment the found counter if dir_port matches */
  2035. found_non_default_fallback +=
  2036. (ds->dir_port == 60093 ?
  2037. 1 : 0)
  2038. );
  2039. tt_assert(found_non_default_fallback == 0);
  2040. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2041. int found_default_fallback = 0;
  2042. SMARTLIST_FOREACH(fallback_servers,
  2043. dir_server_t *,
  2044. ds,
  2045. /* increment the found counter if dir_port matches */
  2046. found_default_fallback +=
  2047. (ds->dir_port == 60099 ?
  2048. 1 : 0)
  2049. );
  2050. tt_assert(found_default_fallback == 0);
  2051. }
  2052. }
  2053. /*
  2054. 2. Outcome: Use Set Bridge Authority
  2055. - Use Default Non-Bridge Directory Authorities
  2056. - Use FallbackDir if it is set, otherwise use default FallbackDir
  2057. Cases expected to yield this outcome:
  2058. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  2059. AlternateBridgeAuthority is set, and
  2060. AlternateDirAuthority is NULL)
  2061. */
  2062. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2063. AlternateDirAuthority Not Set, FallbackDir Set */
  2064. {
  2065. /* clear fallback dirs counter */
  2066. n_add_default_fallback_dir_servers_known_default = 0;
  2067. /* clear options*/
  2068. memset(options, 0, sizeof(or_options_t));
  2069. /* clear any previous dir servers:
  2070. consider_adding_dir_servers() should do this anyway */
  2071. clear_dir_servers();
  2072. /* assign options: 0101 */
  2073. options->DirAuthorities = NULL;
  2074. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2075. options->AlternateDirAuthority = NULL;
  2076. options->FallbackDir = test_fallback_directory;
  2077. options->UseDefaultFallbackDirs = 1;
  2078. /* parse options - ensure we always update by passing NULL old_options */
  2079. consider_adding_dir_servers(options, NULL);
  2080. /* check outcome */
  2081. /* we must not have added the default fallback dirs */
  2082. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2083. /* we have more fallbacks than just the authorities */
  2084. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2085. {
  2086. /* trusted_dir_servers */
  2087. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2088. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2089. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2090. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2091. int found_D0 = 0;
  2092. SMARTLIST_FOREACH(dir_servers,
  2093. dir_server_t *,
  2094. ds,
  2095. /* increment the found counter if dir_port matches */
  2096. found_D0 +=
  2097. (ds->dir_port == 60090 ?
  2098. 1 : 0)
  2099. );
  2100. tt_assert(found_D0 == 0);
  2101. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2102. int found_B1 = 0;
  2103. SMARTLIST_FOREACH(dir_servers,
  2104. dir_server_t *,
  2105. ds,
  2106. /* increment the found counter if dir_port matches */
  2107. found_B1 +=
  2108. (ds->dir_port == 60091 ?
  2109. 1 : 0)
  2110. );
  2111. tt_assert(found_B1 == 1);
  2112. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2113. int found_A2 = 0;
  2114. SMARTLIST_FOREACH(dir_servers,
  2115. dir_server_t *,
  2116. ds,
  2117. /* increment the found counter if dir_port matches */
  2118. found_A2 +=
  2119. (ds->dir_port == 60092 ?
  2120. 1 : 0)
  2121. );
  2122. tt_assert(found_A2 == 0);
  2123. /* There's no easy way of checking that we have included all the
  2124. * default v3 non-Bridge directory authorities, so let's assume that
  2125. * if the total count above is correct, we have the right ones.
  2126. */
  2127. }
  2128. {
  2129. /* fallback_dir_servers */
  2130. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2131. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2132. * Custom Fallback */
  2133. tt_assert(smartlist_len(fallback_servers) ==
  2134. 2 + n_default_alt_dir_authority);
  2135. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2136. int found_D0 = 0;
  2137. SMARTLIST_FOREACH(fallback_servers,
  2138. dir_server_t *,
  2139. ds,
  2140. /* increment the found counter if dir_port matches */
  2141. found_D0 +=
  2142. (ds->dir_port == 60090 ?
  2143. 1 : 0)
  2144. );
  2145. tt_assert(found_D0 == 0);
  2146. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2147. int found_B1 = 0;
  2148. SMARTLIST_FOREACH(fallback_servers,
  2149. dir_server_t *,
  2150. ds,
  2151. /* increment the found counter if dir_port matches */
  2152. found_B1 +=
  2153. (ds->dir_port == 60091 ?
  2154. 1 : 0)
  2155. );
  2156. tt_assert(found_B1 == 1);
  2157. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2158. int found_A2 = 0;
  2159. SMARTLIST_FOREACH(fallback_servers,
  2160. dir_server_t *,
  2161. ds,
  2162. /* increment the found counter if dir_port matches */
  2163. found_A2 +=
  2164. (ds->dir_port == 60092 ?
  2165. 1 : 0)
  2166. );
  2167. tt_assert(found_A2 == 0);
  2168. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2169. int found_non_default_fallback = 0;
  2170. SMARTLIST_FOREACH(fallback_servers,
  2171. dir_server_t *,
  2172. ds,
  2173. /* increment the found counter if dir_port matches */
  2174. found_non_default_fallback +=
  2175. (ds->dir_port == 60093 ?
  2176. 1 : 0)
  2177. );
  2178. tt_assert(found_non_default_fallback == 1);
  2179. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2180. int found_default_fallback = 0;
  2181. SMARTLIST_FOREACH(fallback_servers,
  2182. dir_server_t *,
  2183. ds,
  2184. /* increment the found counter if dir_port matches */
  2185. found_default_fallback +=
  2186. (ds->dir_port == 60099 ?
  2187. 1 : 0)
  2188. );
  2189. tt_assert(found_default_fallback == 0);
  2190. /* There's no easy way of checking that we have included all the
  2191. * default v3 non-Bridge directory authorities, so let's assume that
  2192. * if the total count above is correct, we have the right ones.
  2193. */
  2194. }
  2195. }
  2196. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2197. AlternateDirAuthority & FallbackDir Not Set */
  2198. {
  2199. /* clear fallback dirs counter */
  2200. n_add_default_fallback_dir_servers_known_default = 0;
  2201. /* clear options*/
  2202. memset(options, 0, sizeof(or_options_t));
  2203. /* clear any previous dir servers:
  2204. consider_adding_dir_servers() should do this anyway */
  2205. clear_dir_servers();
  2206. /* assign options: 0100 */
  2207. options->DirAuthorities = NULL;
  2208. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2209. options->AlternateDirAuthority = NULL;
  2210. options->FallbackDir = NULL;
  2211. options->UseDefaultFallbackDirs = 1;
  2212. /* parse options - ensure we always update by passing NULL old_options */
  2213. consider_adding_dir_servers(options, NULL);
  2214. /* check outcome */
  2215. /* we must have added the default fallback dirs */
  2216. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2217. /* we have more fallbacks than just the authorities */
  2218. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2219. {
  2220. /* trusted_dir_servers */
  2221. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2222. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2223. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2224. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2225. int found_D0 = 0;
  2226. SMARTLIST_FOREACH(dir_servers,
  2227. dir_server_t *,
  2228. ds,
  2229. /* increment the found counter if dir_port matches */
  2230. found_D0 +=
  2231. (ds->dir_port == 60090 ?
  2232. 1 : 0)
  2233. );
  2234. tt_assert(found_D0 == 0);
  2235. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2236. int found_B1 = 0;
  2237. SMARTLIST_FOREACH(dir_servers,
  2238. dir_server_t *,
  2239. ds,
  2240. /* increment the found counter if dir_port matches */
  2241. found_B1 +=
  2242. (ds->dir_port == 60091 ?
  2243. 1 : 0)
  2244. );
  2245. tt_assert(found_B1 == 1);
  2246. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2247. int found_A2 = 0;
  2248. SMARTLIST_FOREACH(dir_servers,
  2249. dir_server_t *,
  2250. ds,
  2251. /* increment the found counter if dir_port matches */
  2252. found_A2 +=
  2253. (ds->dir_port == 60092 ?
  2254. 1 : 0)
  2255. );
  2256. tt_assert(found_A2 == 0);
  2257. /* There's no easy way of checking that we have included all the
  2258. * default v3 non-Bridge directory authorities, so let's assume that
  2259. * if the total count above is correct, we have the right ones.
  2260. */
  2261. }
  2262. {
  2263. /* fallback_dir_servers */
  2264. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2265. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2266. * Default Fallback */
  2267. tt_assert(smartlist_len(fallback_servers) ==
  2268. 2 + n_default_alt_dir_authority);
  2269. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2270. int found_D0 = 0;
  2271. SMARTLIST_FOREACH(fallback_servers,
  2272. dir_server_t *,
  2273. ds,
  2274. /* increment the found counter if dir_port matches */
  2275. found_D0 +=
  2276. (ds->dir_port == 60090 ?
  2277. 1 : 0)
  2278. );
  2279. tt_assert(found_D0 == 0);
  2280. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2281. int found_B1 = 0;
  2282. SMARTLIST_FOREACH(fallback_servers,
  2283. dir_server_t *,
  2284. ds,
  2285. /* increment the found counter if dir_port matches */
  2286. found_B1 +=
  2287. (ds->dir_port == 60091 ?
  2288. 1 : 0)
  2289. );
  2290. tt_assert(found_B1 == 1);
  2291. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2292. int found_A2 = 0;
  2293. SMARTLIST_FOREACH(fallback_servers,
  2294. dir_server_t *,
  2295. ds,
  2296. /* increment the found counter if dir_port matches */
  2297. found_A2 +=
  2298. (ds->dir_port == 60092 ?
  2299. 1 : 0)
  2300. );
  2301. tt_assert(found_A2 == 0);
  2302. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2303. int found_non_default_fallback = 0;
  2304. SMARTLIST_FOREACH(fallback_servers,
  2305. dir_server_t *,
  2306. ds,
  2307. /* increment the found counter if dir_port matches */
  2308. found_non_default_fallback +=
  2309. (ds->dir_port == 60093 ?
  2310. 1 : 0)
  2311. );
  2312. tt_assert(found_non_default_fallback == 0);
  2313. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2314. int found_default_fallback = 0;
  2315. SMARTLIST_FOREACH(fallback_servers,
  2316. dir_server_t *,
  2317. ds,
  2318. /* increment the found counter if dir_port matches */
  2319. found_default_fallback +=
  2320. (ds->dir_port == 60099 ?
  2321. 1 : 0)
  2322. );
  2323. tt_assert(found_default_fallback == 1);
  2324. /* There's no easy way of checking that we have included all the
  2325. * default v3 non-Bridge directory authorities, so let's assume that
  2326. * if the total count above is correct, we have the right ones.
  2327. */
  2328. }
  2329. }
  2330. /*
  2331. 3. Outcome: Use Set Alternate Directory Authority
  2332. - Use Default Bridge Authorities
  2333. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2334. Cases expected to yield this outcome:
  2335. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2336. are both NULL, but AlternateDirAuthority is set)
  2337. */
  2338. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2339. AlternateDirAuthority & FallbackDir Set */
  2340. {
  2341. /* clear fallback dirs counter */
  2342. n_add_default_fallback_dir_servers_known_default = 0;
  2343. /* clear options*/
  2344. memset(options, 0, sizeof(or_options_t));
  2345. /* clear any previous dir servers:
  2346. consider_adding_dir_servers() should do this anyway */
  2347. clear_dir_servers();
  2348. /* assign options: 0011 */
  2349. options->DirAuthorities = NULL;
  2350. options->AlternateBridgeAuthority = NULL;
  2351. options->AlternateDirAuthority = test_alt_dir_authority;
  2352. options->FallbackDir = test_fallback_directory;
  2353. options->UseDefaultFallbackDirs = 1;
  2354. /* parse options - ensure we always update by passing NULL old_options */
  2355. consider_adding_dir_servers(options, NULL);
  2356. /* check outcome */
  2357. /* we must not have added the default fallback dirs */
  2358. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2359. /* we have more fallbacks than just the authorities */
  2360. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2361. {
  2362. /* trusted_dir_servers */
  2363. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2364. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2365. tt_assert(smartlist_len(dir_servers) ==
  2366. 1 + n_default_alt_bridge_authority);
  2367. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2368. int found_D0 = 0;
  2369. SMARTLIST_FOREACH(dir_servers,
  2370. dir_server_t *,
  2371. ds,
  2372. /* increment the found counter if dir_port matches */
  2373. found_D0 +=
  2374. (ds->dir_port == 60090 ?
  2375. 1 : 0)
  2376. );
  2377. tt_assert(found_D0 == 0);
  2378. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2379. int found_B1 = 0;
  2380. SMARTLIST_FOREACH(dir_servers,
  2381. dir_server_t *,
  2382. ds,
  2383. /* increment the found counter if dir_port matches */
  2384. found_B1 +=
  2385. (ds->dir_port == 60091 ?
  2386. 1 : 0)
  2387. );
  2388. tt_assert(found_B1 == 0);
  2389. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2390. int found_A2 = 0;
  2391. SMARTLIST_FOREACH(dir_servers,
  2392. dir_server_t *,
  2393. ds,
  2394. /* increment the found counter if dir_port matches */
  2395. found_A2 +=
  2396. (ds->dir_port == 60092 ?
  2397. 1 : 0)
  2398. );
  2399. tt_assert(found_A2 == 1);
  2400. /* There's no easy way of checking that we have included all the
  2401. * default Bridge authorities (except for hard-coding tonga's details),
  2402. * so let's assume that if the total count above is correct,
  2403. * we have the right ones.
  2404. */
  2405. }
  2406. {
  2407. /* fallback_dir_servers */
  2408. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2409. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2410. * Custom Fallback Directory, (No Default Fallback Directories) */
  2411. tt_assert(smartlist_len(fallback_servers) ==
  2412. 2 + n_default_alt_bridge_authority);
  2413. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2414. int found_D0 = 0;
  2415. SMARTLIST_FOREACH(fallback_servers,
  2416. dir_server_t *,
  2417. ds,
  2418. /* increment the found counter if dir_port matches */
  2419. found_D0 +=
  2420. (ds->dir_port == 60090 ?
  2421. 1 : 0)
  2422. );
  2423. tt_assert(found_D0 == 0);
  2424. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2425. int found_B1 = 0;
  2426. SMARTLIST_FOREACH(fallback_servers,
  2427. dir_server_t *,
  2428. ds,
  2429. /* increment the found counter if dir_port matches */
  2430. found_B1 +=
  2431. (ds->dir_port == 60091 ?
  2432. 1 : 0)
  2433. );
  2434. tt_assert(found_B1 == 0);
  2435. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2436. int found_A2 = 0;
  2437. SMARTLIST_FOREACH(fallback_servers,
  2438. dir_server_t *,
  2439. ds,
  2440. /* increment the found counter if dir_port matches */
  2441. found_A2 +=
  2442. (ds->dir_port == 60092 ?
  2443. 1 : 0)
  2444. );
  2445. tt_assert(found_A2 == 1);
  2446. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2447. int found_non_default_fallback = 0;
  2448. SMARTLIST_FOREACH(fallback_servers,
  2449. dir_server_t *,
  2450. ds,
  2451. /* increment the found counter if dir_port matches */
  2452. found_non_default_fallback +=
  2453. (ds->dir_port == 60093 ?
  2454. 1 : 0)
  2455. );
  2456. tt_assert(found_non_default_fallback == 1);
  2457. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2458. int found_default_fallback = 0;
  2459. SMARTLIST_FOREACH(fallback_servers,
  2460. dir_server_t *,
  2461. ds,
  2462. /* increment the found counter if dir_port matches */
  2463. found_default_fallback +=
  2464. (ds->dir_port == 60099 ?
  2465. 1 : 0)
  2466. );
  2467. tt_assert(found_default_fallback == 0);
  2468. /* There's no easy way of checking that we have included all the
  2469. * default Bridge authorities (except for hard-coding tonga's details),
  2470. * so let's assume that if the total count above is correct,
  2471. * we have the right ones.
  2472. */
  2473. }
  2474. }
  2475. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2476. AlternateDirAuthority Set, FallbackDir Not Set */
  2477. {
  2478. /* clear fallback dirs counter */
  2479. n_add_default_fallback_dir_servers_known_default = 0;
  2480. /* clear options*/
  2481. memset(options, 0, sizeof(or_options_t));
  2482. /* clear any previous dir servers:
  2483. consider_adding_dir_servers() should do this anyway */
  2484. clear_dir_servers();
  2485. /* assign options: 0010 */
  2486. options->DirAuthorities = NULL;
  2487. options->AlternateBridgeAuthority = NULL;
  2488. options->AlternateDirAuthority = test_alt_dir_authority;
  2489. options->FallbackDir = NULL;
  2490. options->UseDefaultFallbackDirs = 1;
  2491. /* parse options - ensure we always update by passing NULL old_options */
  2492. consider_adding_dir_servers(options, NULL);
  2493. /* check outcome */
  2494. /* we must not have added the default fallback dirs */
  2495. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2496. /* we just have the authorities */
  2497. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2498. {
  2499. /* trusted_dir_servers */
  2500. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2501. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2502. * No Default or Custom Fallback Directories */
  2503. tt_assert(smartlist_len(dir_servers) ==
  2504. 1 + n_default_alt_bridge_authority);
  2505. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2506. int found_D0 = 0;
  2507. SMARTLIST_FOREACH(dir_servers,
  2508. dir_server_t *,
  2509. ds,
  2510. /* increment the found counter if dir_port matches */
  2511. found_D0 +=
  2512. (ds->dir_port == 60090 ?
  2513. 1 : 0)
  2514. );
  2515. tt_assert(found_D0 == 0);
  2516. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2517. int found_B1 = 0;
  2518. SMARTLIST_FOREACH(dir_servers,
  2519. dir_server_t *,
  2520. ds,
  2521. /* increment the found counter if dir_port matches */
  2522. found_B1 +=
  2523. (ds->dir_port == 60091 ?
  2524. 1 : 0)
  2525. );
  2526. tt_assert(found_B1 == 0);
  2527. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2528. int found_A2 = 0;
  2529. SMARTLIST_FOREACH(dir_servers,
  2530. dir_server_t *,
  2531. ds,
  2532. /* increment the found counter if dir_port matches */
  2533. found_A2 +=
  2534. (ds->dir_port == 60092 ?
  2535. 1 : 0)
  2536. );
  2537. tt_assert(found_A2 == 1);
  2538. /* There's no easy way of checking that we have included all the
  2539. * default Bridge authorities (except for hard-coding tonga's details),
  2540. * so let's assume that if the total count above is correct,
  2541. * we have the right ones.
  2542. */
  2543. }
  2544. {
  2545. /* fallback_dir_servers */
  2546. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2547. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2548. * No Custom or Default Fallback Directories */
  2549. tt_assert(smartlist_len(fallback_servers) ==
  2550. 1 + n_default_alt_bridge_authority);
  2551. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2552. int found_D0 = 0;
  2553. SMARTLIST_FOREACH(fallback_servers,
  2554. dir_server_t *,
  2555. ds,
  2556. /* increment the found counter if dir_port matches */
  2557. found_D0 +=
  2558. (ds->dir_port == 60090 ?
  2559. 1 : 0)
  2560. );
  2561. tt_assert(found_D0 == 0);
  2562. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2563. int found_B1 = 0;
  2564. SMARTLIST_FOREACH(fallback_servers,
  2565. dir_server_t *,
  2566. ds,
  2567. /* increment the found counter if dir_port matches */
  2568. found_B1 +=
  2569. (ds->dir_port == 60091 ?
  2570. 1 : 0)
  2571. );
  2572. tt_assert(found_B1 == 0);
  2573. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2574. int found_A2 = 0;
  2575. SMARTLIST_FOREACH(fallback_servers,
  2576. dir_server_t *,
  2577. ds,
  2578. /* increment the found counter if dir_port matches */
  2579. found_A2 +=
  2580. (ds->dir_port == 60092 ?
  2581. 1 : 0)
  2582. );
  2583. tt_assert(found_A2 == 1);
  2584. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2585. int found_non_default_fallback = 0;
  2586. SMARTLIST_FOREACH(fallback_servers,
  2587. dir_server_t *,
  2588. ds,
  2589. /* increment the found counter if dir_port matches */
  2590. found_non_default_fallback +=
  2591. (ds->dir_port == 60093 ?
  2592. 1 : 0)
  2593. );
  2594. tt_assert(found_non_default_fallback == 0);
  2595. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2596. int found_default_fallback = 0;
  2597. SMARTLIST_FOREACH(fallback_servers,
  2598. dir_server_t *,
  2599. ds,
  2600. /* increment the found counter if dir_port matches */
  2601. found_default_fallback +=
  2602. (ds->dir_port == 60099 ?
  2603. 1 : 0)
  2604. );
  2605. tt_assert(found_default_fallback == 0);
  2606. /* There's no easy way of checking that we have included all the
  2607. * default Bridge authorities (except for hard-coding tonga's details),
  2608. * so let's assume that if the total count above is correct,
  2609. * we have the right ones.
  2610. */
  2611. }
  2612. }
  2613. /*
  2614. 4. Outcome: Use Set Custom Fallback Directory
  2615. - Use Default Bridge & Directory Authorities
  2616. Cases expected to yield this outcome:
  2617. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2618. are all NULL, but FallbackDir is set)
  2619. */
  2620. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2621. & AlternateDirAuthority Not Set, FallbackDir Set */
  2622. {
  2623. /* clear fallback dirs counter */
  2624. n_add_default_fallback_dir_servers_known_default = 0;
  2625. /* clear options*/
  2626. memset(options, 0, sizeof(or_options_t));
  2627. /* clear any previous dir servers:
  2628. consider_adding_dir_servers() should do this anyway */
  2629. clear_dir_servers();
  2630. /* assign options: 0001 */
  2631. options->DirAuthorities = NULL;
  2632. options->AlternateBridgeAuthority = NULL;
  2633. options->AlternateDirAuthority = NULL;
  2634. options->FallbackDir = test_fallback_directory;
  2635. options->UseDefaultFallbackDirs = 1;
  2636. /* parse options - ensure we always update by passing NULL old_options */
  2637. consider_adding_dir_servers(options, NULL);
  2638. /* check outcome */
  2639. /* we must not have added the default fallback dirs */
  2640. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2641. /* we have more fallbacks than just the authorities */
  2642. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2643. {
  2644. /* trusted_dir_servers */
  2645. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2646. /* (No D0), (No B1), Default Bridge Authorities,
  2647. * (No A2), Default v3 Directory Authorities */
  2648. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2649. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2650. int found_D0 = 0;
  2651. SMARTLIST_FOREACH(dir_servers,
  2652. dir_server_t *,
  2653. ds,
  2654. /* increment the found counter if dir_port matches */
  2655. found_D0 +=
  2656. (ds->dir_port == 60090 ?
  2657. 1 : 0)
  2658. );
  2659. tt_assert(found_D0 == 0);
  2660. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2661. int found_B1 = 0;
  2662. SMARTLIST_FOREACH(dir_servers,
  2663. dir_server_t *,
  2664. ds,
  2665. /* increment the found counter if dir_port matches */
  2666. found_B1 +=
  2667. (ds->dir_port == 60091 ?
  2668. 1 : 0)
  2669. );
  2670. tt_assert(found_B1 == 0);
  2671. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2672. int found_A2 = 0;
  2673. SMARTLIST_FOREACH(dir_servers,
  2674. dir_server_t *,
  2675. ds,
  2676. /* increment the found counter if dir_port matches */
  2677. found_A2 +=
  2678. (ds->dir_port == 60092 ?
  2679. 1 : 0)
  2680. );
  2681. tt_assert(found_A2 == 0);
  2682. /* There's no easy way of checking that we have included all the
  2683. * default Bridge & V3 Directory authorities, so let's assume that
  2684. * if the total count above is correct, we have the right ones.
  2685. */
  2686. }
  2687. {
  2688. /* fallback_dir_servers */
  2689. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2690. /* (No D0), (No B1), Default Bridge Authorities,
  2691. * (No A2), Default v3 Directory Authorities,
  2692. * Custom Fallback Directory, (No Default Fallback Directories) */
  2693. tt_assert(smartlist_len(fallback_servers) ==
  2694. 1 + n_default_authorities);
  2695. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2696. int found_D0 = 0;
  2697. SMARTLIST_FOREACH(fallback_servers,
  2698. dir_server_t *,
  2699. ds,
  2700. /* increment the found counter if dir_port matches */
  2701. found_D0 +=
  2702. (ds->dir_port == 60090 ?
  2703. 1 : 0)
  2704. );
  2705. tt_assert(found_D0 == 0);
  2706. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2707. int found_B1 = 0;
  2708. SMARTLIST_FOREACH(fallback_servers,
  2709. dir_server_t *,
  2710. ds,
  2711. /* increment the found counter if dir_port matches */
  2712. found_B1 +=
  2713. (ds->dir_port == 60091 ?
  2714. 1 : 0)
  2715. );
  2716. tt_assert(found_B1 == 0);
  2717. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2718. int found_A2 = 0;
  2719. SMARTLIST_FOREACH(fallback_servers,
  2720. dir_server_t *,
  2721. ds,
  2722. /* increment the found counter if dir_port matches */
  2723. found_A2 +=
  2724. (ds->dir_port == 60092 ?
  2725. 1 : 0)
  2726. );
  2727. tt_assert(found_A2 == 0);
  2728. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2729. int found_non_default_fallback = 0;
  2730. SMARTLIST_FOREACH(fallback_servers,
  2731. dir_server_t *,
  2732. ds,
  2733. /* increment the found counter if dir_port matches */
  2734. found_non_default_fallback +=
  2735. (ds->dir_port == 60093 ?
  2736. 1 : 0)
  2737. );
  2738. tt_assert(found_non_default_fallback == 1);
  2739. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2740. int found_default_fallback = 0;
  2741. SMARTLIST_FOREACH(fallback_servers,
  2742. dir_server_t *,
  2743. ds,
  2744. /* increment the found counter if dir_port matches */
  2745. found_default_fallback +=
  2746. (ds->dir_port == 60099 ?
  2747. 1 : 0)
  2748. );
  2749. tt_assert(found_default_fallback == 0);
  2750. /* There's no easy way of checking that we have included all the
  2751. * default Bridge & V3 Directory authorities, so let's assume that
  2752. * if the total count above is correct, we have the right ones.
  2753. */
  2754. }
  2755. }
  2756. /*
  2757. 5. Outcome: Use All Defaults
  2758. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2759. Cases expected to yield this outcome:
  2760. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2761. and FallbackDir are all NULL)
  2762. */
  2763. /* Case 0: 0000 - All Not Set */
  2764. {
  2765. /* clear fallback dirs counter */
  2766. n_add_default_fallback_dir_servers_known_default = 0;
  2767. /* clear options*/
  2768. memset(options, 0, sizeof(or_options_t));
  2769. /* clear any previous dir servers:
  2770. consider_adding_dir_servers() should do this anyway */
  2771. clear_dir_servers();
  2772. /* assign options: 0001 */
  2773. options->DirAuthorities = NULL;
  2774. options->AlternateBridgeAuthority = NULL;
  2775. options->AlternateDirAuthority = NULL;
  2776. options->FallbackDir = NULL;
  2777. options->UseDefaultFallbackDirs = 1;
  2778. /* parse options - ensure we always update by passing NULL old_options */
  2779. consider_adding_dir_servers(options, NULL);
  2780. /* check outcome */
  2781. /* we must have added the default fallback dirs */
  2782. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2783. /* we have more fallbacks than just the authorities */
  2784. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2785. {
  2786. /* trusted_dir_servers */
  2787. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2788. /* (No D0), (No B1), Default Bridge Authorities,
  2789. * (No A2), Default v3 Directory Authorities */
  2790. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2791. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2792. int found_D0 = 0;
  2793. SMARTLIST_FOREACH(dir_servers,
  2794. dir_server_t *,
  2795. ds,
  2796. /* increment the found counter if dir_port matches */
  2797. found_D0 +=
  2798. (ds->dir_port == 60090 ?
  2799. 1 : 0)
  2800. );
  2801. tt_assert(found_D0 == 0);
  2802. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2803. int found_B1 = 0;
  2804. SMARTLIST_FOREACH(dir_servers,
  2805. dir_server_t *,
  2806. ds,
  2807. /* increment the found counter if dir_port matches */
  2808. found_B1 +=
  2809. (ds->dir_port == 60091 ?
  2810. 1 : 0)
  2811. );
  2812. tt_assert(found_B1 == 0);
  2813. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2814. int found_A2 = 0;
  2815. SMARTLIST_FOREACH(dir_servers,
  2816. dir_server_t *,
  2817. ds,
  2818. /* increment the found counter if dir_port matches */
  2819. found_A2 +=
  2820. (ds->dir_port == 60092 ?
  2821. 1 : 0)
  2822. );
  2823. tt_assert(found_A2 == 0);
  2824. /* There's no easy way of checking that we have included all the
  2825. * default Bridge & V3 Directory authorities, so let's assume that
  2826. * if the total count above is correct, we have the right ones.
  2827. */
  2828. }
  2829. {
  2830. /* fallback_dir_servers */
  2831. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2832. /* (No D0), (No B1), Default Bridge Authorities,
  2833. * (No A2), Default v3 Directory Authorities,
  2834. * (No Custom Fallback Directory), Default Fallback Directories */
  2835. tt_assert(smartlist_len(fallback_servers) ==
  2836. n_default_authorities + n_default_fallback_dir);
  2837. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2838. int found_D0 = 0;
  2839. SMARTLIST_FOREACH(fallback_servers,
  2840. dir_server_t *,
  2841. ds,
  2842. /* increment the found counter if dir_port matches */
  2843. found_D0 +=
  2844. (ds->dir_port == 60090 ?
  2845. 1 : 0)
  2846. );
  2847. tt_assert(found_D0 == 0);
  2848. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2849. int found_B1 = 0;
  2850. SMARTLIST_FOREACH(fallback_servers,
  2851. dir_server_t *,
  2852. ds,
  2853. /* increment the found counter if dir_port matches */
  2854. found_B1 +=
  2855. (ds->dir_port == 60091 ?
  2856. 1 : 0)
  2857. );
  2858. tt_assert(found_B1 == 0);
  2859. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2860. int found_A2 = 0;
  2861. SMARTLIST_FOREACH(fallback_servers,
  2862. dir_server_t *,
  2863. ds,
  2864. /* increment the found counter if dir_port matches */
  2865. found_A2 +=
  2866. (ds->dir_port == 60092 ?
  2867. 1 : 0)
  2868. );
  2869. tt_assert(found_A2 == 0);
  2870. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2871. int found_non_default_fallback = 0;
  2872. SMARTLIST_FOREACH(fallback_servers,
  2873. dir_server_t *,
  2874. ds,
  2875. /* increment the found counter if dir_port matches */
  2876. found_non_default_fallback +=
  2877. (ds->dir_port == 60093 ?
  2878. 1 : 0)
  2879. );
  2880. tt_assert(found_non_default_fallback == 0);
  2881. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2882. int found_default_fallback = 0;
  2883. SMARTLIST_FOREACH(fallback_servers,
  2884. dir_server_t *,
  2885. ds,
  2886. /* increment the found counter if dir_port matches */
  2887. found_default_fallback +=
  2888. (ds->dir_port == 60099 ?
  2889. 1 : 0)
  2890. );
  2891. tt_assert(found_default_fallback == 1);
  2892. /* There's no easy way of checking that we have included all the
  2893. * default Bridge & V3 Directory authorities, and the default
  2894. * Fallback Directories, so let's assume that if the total count
  2895. * above is correct, we have the right ones.
  2896. */
  2897. }
  2898. }
  2899. done:
  2900. clear_dir_servers();
  2901. tor_free(test_dir_authority->key);
  2902. tor_free(test_dir_authority->value);
  2903. tor_free(test_dir_authority);
  2904. tor_free(test_alt_dir_authority->key);
  2905. tor_free(test_alt_dir_authority->value);
  2906. tor_free(test_alt_dir_authority);
  2907. tor_free(test_alt_bridge_authority->key);
  2908. tor_free(test_alt_bridge_authority->value);
  2909. tor_free(test_alt_bridge_authority);
  2910. tor_free(test_fallback_directory->key);
  2911. tor_free(test_fallback_directory->value);
  2912. tor_free(test_fallback_directory);
  2913. options->DirAuthorities = NULL;
  2914. options->AlternateBridgeAuthority = NULL;
  2915. options->AlternateDirAuthority = NULL;
  2916. options->FallbackDir = NULL;
  2917. or_options_free(options);
  2918. UNMOCK(add_default_fallback_dir_servers);
  2919. }
  2920. static void
  2921. test_config_default_dir_servers(void *arg)
  2922. {
  2923. or_options_t *opts = NULL;
  2924. (void)arg;
  2925. int trusted_count = 0;
  2926. int fallback_count = 0;
  2927. /* new set of options should stop fallback parsing */
  2928. opts = tor_malloc_zero(sizeof(or_options_t));
  2929. opts->UseDefaultFallbackDirs = 0;
  2930. /* set old_options to NULL to force dir update */
  2931. consider_adding_dir_servers(opts, NULL);
  2932. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2933. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2934. or_options_free(opts);
  2935. opts = NULL;
  2936. /* assume a release will never go out with less than 7 authorities */
  2937. tt_assert(trusted_count >= 7);
  2938. /* if we disable the default fallbacks, there must not be any extra */
  2939. tt_assert(fallback_count == trusted_count);
  2940. opts = tor_malloc_zero(sizeof(or_options_t));
  2941. opts->UseDefaultFallbackDirs = 1;
  2942. consider_adding_dir_servers(opts, opts);
  2943. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  2944. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  2945. or_options_free(opts);
  2946. opts = NULL;
  2947. /* assume a release will never go out with less than 7 authorities */
  2948. tt_assert(trusted_count >= 7);
  2949. /* XX/teor - allow for default fallbacks to be added without breaking
  2950. * the unit tests. Set a minimum fallback count once the list is stable. */
  2951. tt_assert(fallback_count >= trusted_count);
  2952. done:
  2953. or_options_free(opts);
  2954. }
  2955. static int mock_router_pick_published_address_result = 0;
  2956. static int
  2957. mock_router_pick_published_address(const or_options_t *options, uint32_t *addr)
  2958. {
  2959. (void)options;
  2960. (void)addr;
  2961. return mock_router_pick_published_address_result;
  2962. }
  2963. static int mock_router_my_exit_policy_is_reject_star_result = 0;
  2964. static int
  2965. mock_router_my_exit_policy_is_reject_star(void)
  2966. {
  2967. return mock_router_my_exit_policy_is_reject_star_result;
  2968. }
  2969. static int mock_advertised_server_mode_result = 0;
  2970. static int
  2971. mock_advertised_server_mode(void)
  2972. {
  2973. return mock_advertised_server_mode_result;
  2974. }
  2975. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  2976. static const routerinfo_t *
  2977. mock_router_get_my_routerinfo(void)
  2978. {
  2979. return mock_router_get_my_routerinfo_result;
  2980. }
  2981. static void
  2982. test_config_directory_fetch(void *arg)
  2983. {
  2984. (void)arg;
  2985. /* Test Setup */
  2986. or_options_t *options = tor_malloc_zero(sizeof(or_options_t));
  2987. routerinfo_t routerinfo;
  2988. memset(&routerinfo, 0, sizeof(routerinfo));
  2989. mock_router_pick_published_address_result = -1;
  2990. mock_router_my_exit_policy_is_reject_star_result = 1;
  2991. mock_advertised_server_mode_result = 0;
  2992. mock_router_get_my_routerinfo_result = NULL;
  2993. MOCK(router_pick_published_address, mock_router_pick_published_address);
  2994. MOCK(router_my_exit_policy_is_reject_star,
  2995. mock_router_my_exit_policy_is_reject_star);
  2996. MOCK(advertised_server_mode, mock_advertised_server_mode);
  2997. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  2998. /* Clients can use multiple directory mirrors for bootstrap */
  2999. memset(options, 0, sizeof(or_options_t));
  3000. options->ClientOnly = 1;
  3001. tt_assert(server_mode(options) == 0);
  3002. tt_assert(public_server_mode(options) == 0);
  3003. tt_assert(directory_fetches_from_authorities(options) == 0);
  3004. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3005. == 1);
  3006. /* Bridge Clients can use multiple directory mirrors for bootstrap */
  3007. memset(options, 0, sizeof(or_options_t));
  3008. options->UseBridges = 1;
  3009. tt_assert(server_mode(options) == 0);
  3010. tt_assert(public_server_mode(options) == 0);
  3011. tt_assert(directory_fetches_from_authorities(options) == 0);
  3012. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3013. == 1);
  3014. /* Bridge Relays (Bridges) must act like clients, and use multiple
  3015. * directory mirrors for bootstrap */
  3016. memset(options, 0, sizeof(or_options_t));
  3017. options->BridgeRelay = 1;
  3018. options->ORPort_set = 1;
  3019. tt_assert(server_mode(options) == 1);
  3020. tt_assert(public_server_mode(options) == 0);
  3021. tt_assert(directory_fetches_from_authorities(options) == 0);
  3022. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3023. == 1);
  3024. /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
  3025. * but can use multiple authorities for bootstrap */
  3026. memset(options, 0, sizeof(or_options_t));
  3027. options->FetchDirInfoEarly = 1;
  3028. tt_assert(server_mode(options) == 0);
  3029. tt_assert(public_server_mode(options) == 0);
  3030. tt_assert(directory_fetches_from_authorities(options) == 1);
  3031. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3032. == 1);
  3033. /* OR servers only fetch the consensus from the authorities when they don't
  3034. * know their own address, but never use multiple directories for bootstrap
  3035. */
  3036. memset(options, 0, sizeof(or_options_t));
  3037. options->ORPort_set = 1;
  3038. mock_router_pick_published_address_result = -1;
  3039. tt_assert(server_mode(options) == 1);
  3040. tt_assert(public_server_mode(options) == 1);
  3041. tt_assert(directory_fetches_from_authorities(options) == 1);
  3042. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3043. == 0);
  3044. mock_router_pick_published_address_result = 0;
  3045. tt_assert(server_mode(options) == 1);
  3046. tt_assert(public_server_mode(options) == 1);
  3047. tt_assert(directory_fetches_from_authorities(options) == 0);
  3048. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3049. == 0);
  3050. /* Exit OR servers only fetch the consensus from the authorities when they
  3051. * refuse unknown exits, but never use multiple directories for bootstrap
  3052. */
  3053. memset(options, 0, sizeof(or_options_t));
  3054. options->ORPort_set = 1;
  3055. options->ExitRelay = 1;
  3056. mock_router_pick_published_address_result = 0;
  3057. mock_router_my_exit_policy_is_reject_star_result = 0;
  3058. mock_advertised_server_mode_result = 1;
  3059. mock_router_get_my_routerinfo_result = &routerinfo;
  3060. routerinfo.supports_tunnelled_dir_requests = 1;
  3061. options->RefuseUnknownExits = 1;
  3062. tt_assert(server_mode(options) == 1);
  3063. tt_assert(public_server_mode(options) == 1);
  3064. tt_assert(directory_fetches_from_authorities(options) == 1);
  3065. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3066. == 0);
  3067. options->RefuseUnknownExits = 0;
  3068. mock_router_pick_published_address_result = 0;
  3069. tt_assert(server_mode(options) == 1);
  3070. tt_assert(public_server_mode(options) == 1);
  3071. tt_assert(directory_fetches_from_authorities(options) == 0);
  3072. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3073. == 0);
  3074. /* Dir servers fetch the consensus from the authorities, unless they are not
  3075. * advertising themselves (hibernating) or have no routerinfo or are not
  3076. * advertising their dirport, and never use multiple directories for
  3077. * bootstrap. This only applies if they are also OR servers.
  3078. * (We don't care much about the behaviour of non-OR directory servers.) */
  3079. memset(options, 0, sizeof(or_options_t));
  3080. options->DirPort_set = 1;
  3081. options->ORPort_set = 1;
  3082. options->DirCache = 1;
  3083. mock_router_pick_published_address_result = 0;
  3084. mock_router_my_exit_policy_is_reject_star_result = 1;
  3085. mock_advertised_server_mode_result = 1;
  3086. routerinfo.dir_port = 1;
  3087. mock_router_get_my_routerinfo_result = &routerinfo;
  3088. tt_assert(server_mode(options) == 1);
  3089. tt_assert(public_server_mode(options) == 1);
  3090. tt_assert(directory_fetches_from_authorities(options) == 1);
  3091. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3092. == 0);
  3093. mock_advertised_server_mode_result = 0;
  3094. routerinfo.dir_port = 1;
  3095. mock_router_get_my_routerinfo_result = &routerinfo;
  3096. tt_assert(server_mode(options) == 1);
  3097. tt_assert(public_server_mode(options) == 1);
  3098. tt_assert(directory_fetches_from_authorities(options) == 0);
  3099. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3100. == 0);
  3101. mock_advertised_server_mode_result = 1;
  3102. mock_router_get_my_routerinfo_result = NULL;
  3103. tt_assert(server_mode(options) == 1);
  3104. tt_assert(public_server_mode(options) == 1);
  3105. tt_assert(directory_fetches_from_authorities(options) == 0);
  3106. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3107. == 0);
  3108. mock_advertised_server_mode_result = 1;
  3109. routerinfo.dir_port = 0;
  3110. routerinfo.supports_tunnelled_dir_requests = 0;
  3111. mock_router_get_my_routerinfo_result = &routerinfo;
  3112. tt_assert(server_mode(options) == 1);
  3113. tt_assert(public_server_mode(options) == 1);
  3114. tt_assert(directory_fetches_from_authorities(options) == 0);
  3115. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3116. == 0);
  3117. mock_advertised_server_mode_result = 1;
  3118. routerinfo.dir_port = 1;
  3119. routerinfo.supports_tunnelled_dir_requests = 1;
  3120. mock_router_get_my_routerinfo_result = &routerinfo;
  3121. tt_assert(server_mode(options) == 1);
  3122. tt_assert(public_server_mode(options) == 1);
  3123. tt_assert(directory_fetches_from_authorities(options) == 1);
  3124. tt_assert(networkstatus_consensus_can_use_multiple_directories(options)
  3125. == 0);
  3126. done:
  3127. tor_free(options);
  3128. UNMOCK(router_pick_published_address);
  3129. UNMOCK(router_get_my_routerinfo);
  3130. UNMOCK(advertised_server_mode);
  3131. UNMOCK(router_my_exit_policy_is_reject_star);
  3132. }
  3133. static void
  3134. test_config_default_fallback_dirs(void *arg)
  3135. {
  3136. const char *fallback[] = {
  3137. #include "../or/fallback_dirs.inc"
  3138. NULL
  3139. };
  3140. int n_included_fallback_dirs = 0;
  3141. int n_added_fallback_dirs = 0;
  3142. (void)arg;
  3143. clear_dir_servers();
  3144. while (fallback[n_included_fallback_dirs])
  3145. n_included_fallback_dirs++;
  3146. add_default_fallback_dir_servers();
  3147. n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
  3148. tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
  3149. done:
  3150. clear_dir_servers();
  3151. }
  3152. static config_line_t *
  3153. mock_config_line(const char *key, const char *val)
  3154. {
  3155. config_line_t *config_line = tor_malloc(sizeof(config_line_t));
  3156. memset(config_line, 0, sizeof(config_line_t));
  3157. config_line->key = tor_strdup(key);
  3158. config_line->value = tor_strdup(val);
  3159. return config_line;
  3160. }
  3161. static void
  3162. test_config_parse_port_config__listenaddress(void *data)
  3163. {
  3164. (void)data;
  3165. int ret;
  3166. config_line_t *config_listen_address = NULL, *config_listen_address2 = NULL,
  3167. *config_listen_address3 = NULL;
  3168. config_line_t *config_port1 = NULL, *config_port2 = NULL,
  3169. *config_port3 = NULL, *config_port4 = NULL, *config_port5 = NULL;
  3170. smartlist_t *slout = NULL;
  3171. port_cfg_t *port_cfg = NULL;
  3172. // Test basic invocation with no arguments
  3173. ret = parse_port_config(NULL, NULL, NULL, NULL, 0, NULL, 0, 0);
  3174. tt_int_op(ret, OP_EQ, 0);
  3175. // Setup some test data
  3176. config_listen_address = mock_config_line("DNSListenAddress", "127.0.0.1");
  3177. config_listen_address2 = mock_config_line("DNSListenAddress", "x$$$:::345");
  3178. config_listen_address3 = mock_config_line("DNSListenAddress",
  3179. "127.0.0.1:1442");
  3180. config_port1 = mock_config_line("DNSPort", "42");
  3181. config_port2 = mock_config_line("DNSPort", "43");
  3182. config_port1->next = config_port2;
  3183. config_port3 = mock_config_line("DNSPort", "auto");
  3184. config_port4 = mock_config_line("DNSPort", "55542");
  3185. config_port5 = mock_config_line("DNSPort", "666777");
  3186. // Test failure when we have a ListenAddress line and several
  3187. // Port lines for the same portname
  3188. ret = parse_port_config(NULL, config_port1, config_listen_address, "DNS", 0,
  3189. NULL, 0, 0);
  3190. tt_int_op(ret, OP_EQ, -1);
  3191. // Test case when we have a listen address, no default port and allow
  3192. // spurious listen address lines
  3193. ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
  3194. 0, CL_PORT_ALLOW_EXTRA_LISTENADDR);
  3195. tt_int_op(ret, OP_EQ, 1);
  3196. // Test case when we have a listen address, no default port but doesn't
  3197. // allow spurious listen address lines
  3198. ret = parse_port_config(NULL, NULL, config_listen_address, "DNS", 0, NULL,
  3199. 0, 0);
  3200. tt_int_op(ret, OP_EQ, -1);
  3201. // Test case when we have a listen address, and a port that points to auto,
  3202. // should use the AUTO port
  3203. slout = smartlist_new();
  3204. ret = parse_port_config(slout, config_port3, config_listen_address, "DNS",
  3205. 0, NULL, 0, 0);
  3206. tt_int_op(ret, OP_EQ, 0);
  3207. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3208. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3209. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  3210. // Test when we have a listen address and a custom port
  3211. ret = parse_port_config(slout, config_port4, config_listen_address, "DNS",
  3212. 0, NULL, 0, 0);
  3213. tt_int_op(ret, OP_EQ, 0);
  3214. tt_int_op(smartlist_len(slout), OP_EQ, 2);
  3215. port_cfg = (port_cfg_t *)smartlist_get(slout, 1);
  3216. tt_int_op(port_cfg->port, OP_EQ, 55542);
  3217. // Test when we have a listen address and an invalid custom port
  3218. ret = parse_port_config(slout, config_port5, config_listen_address, "DNS",
  3219. 0, NULL, 0, 0);
  3220. tt_int_op(ret, OP_EQ, -1);
  3221. // Test we get a server port configuration when asked for it
  3222. ret = parse_port_config(slout, NULL, config_listen_address, "DNS", 0, NULL,
  3223. 123, CL_PORT_SERVER_OPTIONS);
  3224. tt_int_op(ret, OP_EQ, 0);
  3225. tt_int_op(smartlist_len(slout), OP_EQ, 4);
  3226. port_cfg = (port_cfg_t *)smartlist_get(slout, 2);
  3227. tt_int_op(port_cfg->port, OP_EQ, 123);
  3228. tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
  3229. tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
  3230. // Test an invalid ListenAddress configuration
  3231. ret = parse_port_config(NULL, NULL, config_listen_address2, "DNS", 0, NULL,
  3232. 222, 0);
  3233. tt_int_op(ret, OP_EQ, -1);
  3234. // Test default to the port in the listen address if available
  3235. ret = parse_port_config(slout, config_port2, config_listen_address3, "DNS",
  3236. 0, NULL, 0, 0);
  3237. tt_int_op(ret, OP_EQ, 0);
  3238. tt_int_op(smartlist_len(slout), OP_EQ, 5);
  3239. port_cfg = (port_cfg_t *)smartlist_get(slout, 4);
  3240. tt_int_op(port_cfg->port, OP_EQ, 1442);
  3241. // Test we work correctly without an out, but with a listen address
  3242. // and a port
  3243. ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
  3244. 0, NULL, 0, 0);
  3245. tt_int_op(ret, OP_EQ, 0);
  3246. // Test warning nonlocal control
  3247. ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
  3248. CONN_TYPE_CONTROL_LISTENER, NULL, 0,
  3249. CL_PORT_WARN_NONLOCAL);
  3250. tt_int_op(ret, OP_EQ, 0);
  3251. // Test warning nonlocal ext or listener
  3252. ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
  3253. CONN_TYPE_EXT_OR_LISTENER, NULL, 0,
  3254. CL_PORT_WARN_NONLOCAL);
  3255. tt_int_op(ret, OP_EQ, 0);
  3256. // Test warning nonlocal other
  3257. ret = parse_port_config(slout, config_port2, config_listen_address, "DNS",
  3258. 0, NULL, 0, CL_PORT_WARN_NONLOCAL);
  3259. tt_int_op(ret, OP_EQ, 0);
  3260. // Test warning nonlocal control without an out
  3261. ret = parse_port_config(NULL, config_port2, config_listen_address, "DNS",
  3262. CONN_TYPE_CONTROL_LISTENER, NULL, 0,
  3263. CL_PORT_WARN_NONLOCAL);
  3264. tt_int_op(ret, OP_EQ, 0);
  3265. done:
  3266. tor_free(config_listen_address);
  3267. tor_free(config_listen_address2);
  3268. tor_free(config_listen_address3);
  3269. tor_free(config_port1);
  3270. tor_free(config_port2);
  3271. tor_free(config_port3);
  3272. tor_free(config_port4);
  3273. tor_free(config_port5);
  3274. smartlist_free(slout);
  3275. }
  3276. static void
  3277. test_config_parse_port_config__ports__no_ports_given(void *data)
  3278. {
  3279. (void)data;
  3280. int ret;
  3281. smartlist_t *slout = NULL;
  3282. port_cfg_t *port_cfg = NULL;
  3283. config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
  3284. slout = smartlist_new();
  3285. // Test no defaultport, no defaultaddress and no out
  3286. ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 0, 0);
  3287. tt_int_op(ret, OP_EQ, 0);
  3288. // Test with defaultport, no defaultaddress and no out
  3289. ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, NULL, 42, 0);
  3290. tt_int_op(ret, OP_EQ, 0);
  3291. // Test no defaultport, with defaultaddress and no out
  3292. ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
  3293. tt_int_op(ret, OP_EQ, 0);
  3294. // Test with defaultport, with defaultaddress and no out
  3295. ret = parse_port_config(NULL, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
  3296. tt_int_op(ret, OP_EQ, 0);
  3297. // Test no defaultport, no defaultaddress and with out
  3298. ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 0, 0);
  3299. tt_int_op(ret, OP_EQ, 0);
  3300. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3301. // Test with defaultport, no defaultaddress and with out
  3302. ret = parse_port_config(slout, NULL, NULL, "DNS", 0, NULL, 42, 0);
  3303. tt_int_op(ret, OP_EQ, 0);
  3304. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3305. // Test no defaultport, with defaultaddress and with out
  3306. ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
  3307. tt_int_op(ret, OP_EQ, 0);
  3308. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3309. // Test with defaultport, with defaultaddress and out, adds a new port cfg
  3310. smartlist_clear(slout);
  3311. ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
  3312. tt_int_op(ret, OP_EQ, 0);
  3313. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3314. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3315. tt_int_op(port_cfg->port, OP_EQ, 42);
  3316. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
  3317. // Test with defaultport, with defaultaddress and out, adds a new port cfg
  3318. // for a unix address
  3319. smartlist_clear(slout);
  3320. ret = parse_port_config(slout, NULL, NULL, "DNS", 0, "/foo/bar/unixdomain",
  3321. 42, CL_PORT_IS_UNIXSOCKET);
  3322. tt_int_op(ret, OP_EQ, 0);
  3323. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3324. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3325. tt_int_op(port_cfg->port, OP_EQ, 0);
  3326. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  3327. tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
  3328. done:
  3329. smartlist_free(slout);
  3330. tor_free(config_port_invalid);
  3331. tor_free(config_port_valid);
  3332. }
  3333. static void
  3334. test_config_parse_port_config__ports__ports_given(void *data)
  3335. {
  3336. (void)data;
  3337. int ret;
  3338. smartlist_t *slout = NULL;
  3339. port_cfg_t *port_cfg = NULL;
  3340. config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
  3341. tor_addr_t addr;
  3342. slout = smartlist_new();
  3343. // Test error when encounters an invalid Port specification
  3344. config_port_invalid = mock_config_line("DNSPort", "");
  3345. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
  3346. 0, 0);
  3347. tt_int_op(ret, OP_EQ, -1);
  3348. // Test error when encounters an empty unix domain specification
  3349. tor_free(config_port_invalid);
  3350. config_port_invalid = mock_config_line("DNSPort", "unix:");
  3351. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0, NULL,
  3352. 0, 0);
  3353. tt_int_op(ret, OP_EQ, -1);
  3354. // Test error when encounters a unix domain specification but the listener
  3355. // doesnt support domain sockets
  3356. config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
  3357. ret = parse_port_config(NULL, config_port_valid, NULL, "DNS",
  3358. CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
  3359. tt_int_op(ret, OP_EQ, -1);
  3360. // Test valid unix domain
  3361. smartlist_clear(slout);
  3362. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3363. CONN_TYPE_AP_LISTENER, NULL, 0, 0);
  3364. tt_int_op(ret, OP_EQ, 0);
  3365. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3366. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3367. tt_int_op(port_cfg->port, OP_EQ, 0);
  3368. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  3369. tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
  3370. // Test failure if we have no ipv4 and no ipv6 (for unix domain sockets,
  3371. // this makes no sense - it should be fixed)
  3372. tor_free(config_port_invalid);
  3373. config_port_invalid = mock_config_line("DNSPort",
  3374. "unix:/tmp/foo/bar NoIPv4Traffic");
  3375. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS",
  3376. CONN_TYPE_AP_LISTENER, NULL, 0,
  3377. CL_PORT_TAKES_HOSTNAMES);
  3378. tt_int_op(ret, OP_EQ, -1);
  3379. // Test success with no ipv4 but take ipv6 (for unix domain sockets, this
  3380. // makes no sense - it should be fixed)
  3381. tor_free(config_port_valid);
  3382. smartlist_clear(slout);
  3383. config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar "
  3384. "NoIPv4Traffic IPv6Traffic");
  3385. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3386. CONN_TYPE_AP_LISTENER, NULL, 0,
  3387. CL_PORT_TAKES_HOSTNAMES);
  3388. tt_int_op(ret, OP_EQ, 0);
  3389. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3390. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3391. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3392. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
  3393. // Test success with both ipv4 and ipv6 (for unix domain sockets,
  3394. // this makes no sense - it should be fixed)
  3395. tor_free(config_port_valid);
  3396. smartlist_clear(slout);
  3397. config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar "
  3398. "IPv4Traffic IPv6Traffic");
  3399. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3400. CONN_TYPE_AP_LISTENER, NULL, 0,
  3401. CL_PORT_TAKES_HOSTNAMES);
  3402. tt_int_op(ret, OP_EQ, 0);
  3403. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3404. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3405. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
  3406. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
  3407. // Test failure if we specify world writable for an IP Port
  3408. tor_free(config_port_invalid);
  3409. config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
  3410. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
  3411. "127.0.0.3", 0, 0);
  3412. tt_int_op(ret, OP_EQ, -1);
  3413. // Test failure if we specify group writable for an IP Port
  3414. tor_free(config_port_invalid);
  3415. config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
  3416. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
  3417. "127.0.0.3", 0, 0);
  3418. tt_int_op(ret, OP_EQ, -1);
  3419. // Test success with only a port (this will fail without a default address)
  3420. tor_free(config_port_valid);
  3421. config_port_valid = mock_config_line("DNSPort", "42");
  3422. ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
  3423. "127.0.0.3", 0, 0);
  3424. tt_int_op(ret, OP_EQ, 0);
  3425. // Test success with only a port and isolate destination port
  3426. tor_free(config_port_valid);
  3427. smartlist_clear(slout);
  3428. config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
  3429. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3430. "127.0.0.3", 0, 0);
  3431. tt_int_op(ret, OP_EQ, 0);
  3432. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3433. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3434. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3435. ISO_DEFAULT | ISO_DESTPORT);
  3436. // Test success with a negative isolate destination port, and plural
  3437. tor_free(config_port_valid);
  3438. smartlist_clear(slout);
  3439. config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
  3440. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3441. "127.0.0.3", 0, 0);
  3442. tt_int_op(ret, OP_EQ, 0);
  3443. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3444. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3445. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3446. ISO_DEFAULT & ~ISO_DESTPORT);
  3447. // Test success with isolate destination address
  3448. tor_free(config_port_valid);
  3449. smartlist_clear(slout);
  3450. config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
  3451. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3452. "127.0.0.3", 0, 0);
  3453. tt_int_op(ret, OP_EQ, 0);
  3454. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3455. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3456. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3457. ISO_DEFAULT | ISO_DESTADDR);
  3458. // Test success with isolate socks AUTH
  3459. tor_free(config_port_valid);
  3460. smartlist_clear(slout);
  3461. config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
  3462. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3463. "127.0.0.3", 0, 0);
  3464. tt_int_op(ret, OP_EQ, 0);
  3465. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3466. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3467. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3468. ISO_DEFAULT | ISO_SOCKSAUTH);
  3469. // Test success with isolate client protocol
  3470. tor_free(config_port_valid);
  3471. smartlist_clear(slout);
  3472. config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
  3473. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3474. "127.0.0.3", 0, 0);
  3475. tt_int_op(ret, OP_EQ, 0);
  3476. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3477. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3478. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3479. ISO_DEFAULT | ISO_CLIENTPROTO);
  3480. // Test success with isolate client address
  3481. tor_free(config_port_valid);
  3482. smartlist_clear(slout);
  3483. config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
  3484. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3485. "127.0.0.3", 0, 0);
  3486. tt_int_op(ret, OP_EQ, 0);
  3487. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3488. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3489. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3490. ISO_DEFAULT | ISO_CLIENTADDR);
  3491. // Test success with ignored unknown options
  3492. tor_free(config_port_valid);
  3493. config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
  3494. ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
  3495. "127.0.0.3", 0, 0);
  3496. tt_int_op(ret, OP_EQ, 0);
  3497. // Test success with no isolate socks AUTH
  3498. tor_free(config_port_valid);
  3499. smartlist_clear(slout);
  3500. config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
  3501. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3502. "127.0.0.3", 0, 0);
  3503. tt_int_op(ret, OP_EQ, 0);
  3504. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3505. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3506. tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
  3507. // Test success with prefer ipv6
  3508. tor_free(config_port_valid);
  3509. smartlist_clear(slout);
  3510. config_port_valid = mock_config_line("DNSPort", "42 IPv6Traffic PreferIPv6");
  3511. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3512. CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
  3513. CL_PORT_TAKES_HOSTNAMES);
  3514. tt_int_op(ret, OP_EQ, 0);
  3515. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3516. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3517. tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
  3518. // Test success with cache ipv4 DNS
  3519. tor_free(config_port_valid);
  3520. smartlist_clear(slout);
  3521. config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
  3522. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3523. "127.0.0.42", 0, 0);
  3524. tt_int_op(ret, OP_EQ, 0);
  3525. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3526. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3527. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
  3528. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
  3529. // Test success with cache ipv6 DNS
  3530. tor_free(config_port_valid);
  3531. smartlist_clear(slout);
  3532. config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
  3533. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3534. "127.0.0.42", 0, 0);
  3535. tt_int_op(ret, OP_EQ, 0);
  3536. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3537. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3538. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
  3539. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
  3540. // Test success with no cache ipv4 DNS
  3541. tor_free(config_port_valid);
  3542. smartlist_clear(slout);
  3543. config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
  3544. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3545. "127.0.0.42", 0, 0);
  3546. tt_int_op(ret, OP_EQ, 0);
  3547. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3548. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3549. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
  3550. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
  3551. // Test success with cache DNS
  3552. tor_free(config_port_valid);
  3553. smartlist_clear(slout);
  3554. config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
  3555. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3556. "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
  3557. tt_int_op(ret, OP_EQ, 0);
  3558. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3559. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3560. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
  3561. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
  3562. // Test success with use cached ipv4 DNS
  3563. tor_free(config_port_valid);
  3564. smartlist_clear(slout);
  3565. config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
  3566. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3567. "127.0.0.42", 0, 0);
  3568. tt_int_op(ret, OP_EQ, 0);
  3569. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3570. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3571. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
  3572. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
  3573. // Test success with use cached ipv6 DNS
  3574. tor_free(config_port_valid);
  3575. smartlist_clear(slout);
  3576. config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
  3577. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3578. "127.0.0.42", 0, 0);
  3579. tt_int_op(ret, OP_EQ, 0);
  3580. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3581. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3582. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
  3583. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
  3584. // Test success with use cached DNS
  3585. tor_free(config_port_valid);
  3586. smartlist_clear(slout);
  3587. config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
  3588. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3589. "127.0.0.42", 0, 0);
  3590. tt_int_op(ret, OP_EQ, 0);
  3591. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3592. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3593. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
  3594. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
  3595. // Test success with not preferring ipv6 automap
  3596. tor_free(config_port_valid);
  3597. smartlist_clear(slout);
  3598. config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
  3599. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3600. "127.0.0.42", 0, 0);
  3601. tt_int_op(ret, OP_EQ, 0);
  3602. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3603. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3604. tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
  3605. // Test success with prefer SOCKS no auth
  3606. tor_free(config_port_valid);
  3607. smartlist_clear(slout);
  3608. config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
  3609. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3610. "127.0.0.42", 0, 0);
  3611. tt_int_op(ret, OP_EQ, 0);
  3612. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3613. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3614. tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
  3615. // Test failure with both a zero port and a non-zero port
  3616. tor_free(config_port_invalid);
  3617. tor_free(config_port_valid);
  3618. smartlist_clear(slout);
  3619. config_port_invalid = mock_config_line("DNSPort", "0");
  3620. config_port_valid = mock_config_line("DNSPort", "42");
  3621. config_port_invalid->next = config_port_valid;
  3622. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3623. "127.0.0.42", 0, 0);
  3624. tt_int_op(ret, OP_EQ, -1);
  3625. // Test success with warn non-local control
  3626. smartlist_clear(slout);
  3627. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3628. CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
  3629. CL_PORT_WARN_NONLOCAL);
  3630. tt_int_op(ret, OP_EQ, 0);
  3631. // Test success with warn non-local listener
  3632. smartlist_clear(slout);
  3633. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3634. CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
  3635. CL_PORT_WARN_NONLOCAL);
  3636. tt_int_op(ret, OP_EQ, 0);
  3637. // Test success with warn non-local other
  3638. smartlist_clear(slout);
  3639. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3640. "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
  3641. tt_int_op(ret, OP_EQ, 0);
  3642. // Test success with warn non-local other without out
  3643. ret = parse_port_config(NULL, config_port_valid, NULL, "DNS", 0,
  3644. "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
  3645. tt_int_op(ret, OP_EQ, 0);
  3646. // Test success with both ipv4 and ipv6 but without stream options
  3647. tor_free(config_port_valid);
  3648. smartlist_clear(slout);
  3649. config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
  3650. "IPv6Traffic");
  3651. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3652. "127.0.0.44", 0,
  3653. CL_PORT_TAKES_HOSTNAMES |
  3654. CL_PORT_NO_STREAM_OPTIONS);
  3655. tt_int_op(ret, OP_EQ, 0);
  3656. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3657. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3658. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
  3659. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
  3660. // Test failure for a SessionGroup argument with invalid value
  3661. tor_free(config_port_invalid);
  3662. smartlist_clear(slout);
  3663. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
  3664. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3665. "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
  3666. tt_int_op(ret, OP_EQ, -1);
  3667. // TODO: this seems wrong. Shouldn't it be the other way around?
  3668. // Potential bug.
  3669. // Test failure for a SessionGroup argument with valid value but with stream
  3670. // options allowed
  3671. tor_free(config_port_invalid);
  3672. smartlist_clear(slout);
  3673. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
  3674. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3675. "127.0.0.44", 0, 0);
  3676. tt_int_op(ret, OP_EQ, -1);
  3677. // Test failure for more than one SessionGroup argument
  3678. tor_free(config_port_invalid);
  3679. smartlist_clear(slout);
  3680. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
  3681. "SessionGroup=321");
  3682. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3683. "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
  3684. tt_int_op(ret, OP_EQ, -1);
  3685. // Test success with a sessiongroup options
  3686. tor_free(config_port_valid);
  3687. smartlist_clear(slout);
  3688. config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
  3689. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3690. "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
  3691. tt_int_op(ret, OP_EQ, 0);
  3692. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3693. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3694. tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
  3695. // Test success with a zero unix domain socket, and doesnt add it to out
  3696. tor_free(config_port_valid);
  3697. smartlist_clear(slout);
  3698. config_port_valid = mock_config_line("DNSPort", "0");
  3699. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3700. "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
  3701. tt_int_op(ret, OP_EQ, 0);
  3702. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3703. // Test success with a one unix domain socket, and doesnt add it to out
  3704. tor_free(config_port_valid);
  3705. smartlist_clear(slout);
  3706. config_port_valid = mock_config_line("DNSPort", "something");
  3707. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3708. "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
  3709. tt_int_op(ret, OP_EQ, 0);
  3710. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3711. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3712. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  3713. tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
  3714. // Test success with a port of auto - it uses the default address
  3715. tor_free(config_port_valid);
  3716. smartlist_clear(slout);
  3717. config_port_valid = mock_config_line("DNSPort", "auto");
  3718. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3719. "127.0.0.46", 0, 0);
  3720. tt_int_op(ret, OP_EQ, 0);
  3721. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3722. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3723. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  3724. tor_addr_parse(&addr, "127.0.0.46");
  3725. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  3726. // Test success with parsing both an address and an auto port
  3727. tor_free(config_port_valid);
  3728. smartlist_clear(slout);
  3729. config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
  3730. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3731. "127.0.0.46", 0, 0);
  3732. tt_int_op(ret, OP_EQ, 0);
  3733. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3734. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3735. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  3736. tor_addr_parse(&addr, "127.0.0.122");
  3737. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  3738. // Test failure when asked to parse an invalid address followed by auto
  3739. tor_free(config_port_invalid);
  3740. config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
  3741. ret = parse_port_config(NULL, config_port_invalid, NULL, "DNS", 0,
  3742. "127.0.0.46", 0, 0);
  3743. tt_int_op(ret, OP_EQ, -1);
  3744. // Test success with parsing both an address and a real port
  3745. tor_free(config_port_valid);
  3746. smartlist_clear(slout);
  3747. config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
  3748. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0,
  3749. "127.0.0.46", 0, 0);
  3750. tt_int_op(ret, OP_EQ, 0);
  3751. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3752. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3753. tt_int_op(port_cfg->port, OP_EQ, 656);
  3754. tor_addr_parse(&addr, "127.0.0.123");
  3755. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  3756. // Test failure if we can't parse anything at all
  3757. tor_free(config_port_invalid);
  3758. smartlist_clear(slout);
  3759. config_port_invalid = mock_config_line("DNSPort", "something wrong");
  3760. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3761. "127.0.0.46", 0, 0);
  3762. tt_int_op(ret, OP_EQ, -1);
  3763. // Test failure if we find both an address, a port and an auto
  3764. tor_free(config_port_invalid);
  3765. smartlist_clear(slout);
  3766. config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
  3767. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0,
  3768. "127.0.0.46", 0, 0);
  3769. tt_int_op(ret, OP_EQ, -1);
  3770. // Test that default to group writeable default sets group writeable for
  3771. // domain socket
  3772. tor_free(config_port_valid);
  3773. smartlist_clear(slout);
  3774. config_port_valid = mock_config_line("DNSPort", "unix:/tmp/somewhere");
  3775. ret = parse_port_config(slout, config_port_valid, NULL, "DNS",
  3776. CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
  3777. CL_PORT_DFLT_GROUP_WRITABLE);
  3778. tt_int_op(ret, OP_EQ, 0);
  3779. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3780. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3781. tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
  3782. done:
  3783. smartlist_free(slout);
  3784. tor_free(config_port_invalid);
  3785. tor_free(config_port_valid);
  3786. }
  3787. static void
  3788. test_config_parse_port_config__ports__server_options(void *data)
  3789. {
  3790. (void)data;
  3791. int ret;
  3792. smartlist_t *slout = NULL;
  3793. port_cfg_t *port_cfg = NULL;
  3794. config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
  3795. slout = smartlist_new();
  3796. // Test success with NoAdvertise option
  3797. tor_free(config_port_valid);
  3798. config_port_valid = mock_config_line("DNSPort",
  3799. "127.0.0.124:656 NoAdvertise");
  3800. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
  3801. CL_PORT_SERVER_OPTIONS);
  3802. tt_int_op(ret, OP_EQ, 0);
  3803. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3804. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3805. tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
  3806. tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
  3807. // Test success with NoListen option
  3808. tor_free(config_port_valid);
  3809. smartlist_clear(slout);
  3810. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
  3811. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
  3812. CL_PORT_SERVER_OPTIONS);
  3813. tt_int_op(ret, OP_EQ, 0);
  3814. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3815. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3816. tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
  3817. tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
  3818. // Test failure with both NoAdvertise and NoListen option
  3819. tor_free(config_port_invalid);
  3820. smartlist_clear(slout);
  3821. config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
  3822. "NoAdvertise");
  3823. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
  3824. 0, CL_PORT_SERVER_OPTIONS);
  3825. tt_int_op(ret, OP_EQ, -1);
  3826. // Test success with IPv4Only
  3827. tor_free(config_port_valid);
  3828. smartlist_clear(slout);
  3829. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
  3830. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
  3831. CL_PORT_SERVER_OPTIONS);
  3832. tt_int_op(ret, OP_EQ, 0);
  3833. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3834. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3835. tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
  3836. tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
  3837. // Test success with IPv6Only
  3838. tor_free(config_port_valid);
  3839. smartlist_clear(slout);
  3840. config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
  3841. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
  3842. CL_PORT_SERVER_OPTIONS);
  3843. tt_int_op(ret, OP_EQ, 0);
  3844. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3845. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3846. tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
  3847. tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
  3848. // Test failure with both IPv4Only and IPv6Only
  3849. tor_free(config_port_invalid);
  3850. smartlist_clear(slout);
  3851. config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
  3852. "IPv4Only");
  3853. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
  3854. 0, CL_PORT_SERVER_OPTIONS);
  3855. tt_int_op(ret, OP_EQ, -1);
  3856. // Test success with invalid parameter
  3857. tor_free(config_port_valid);
  3858. smartlist_clear(slout);
  3859. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
  3860. ret = parse_port_config(slout, config_port_valid, NULL, "DNS", 0, NULL, 0,
  3861. CL_PORT_SERVER_OPTIONS);
  3862. tt_int_op(ret, OP_EQ, 0);
  3863. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3864. // Test failure when asked to bind only to ipv6 but gets an ipv4 address
  3865. tor_free(config_port_invalid);
  3866. smartlist_clear(slout);
  3867. config_port_invalid = mock_config_line("DNSPort",
  3868. "127.0.0.124:656 IPv6Only");
  3869. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
  3870. 0, CL_PORT_SERVER_OPTIONS);
  3871. tt_int_op(ret, OP_EQ, -1);
  3872. // Test failure when asked to bind only to ipv4 but gets an ipv6 address
  3873. tor_free(config_port_invalid);
  3874. smartlist_clear(slout);
  3875. config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
  3876. ret = parse_port_config(slout, config_port_invalid, NULL, "DNS", 0, NULL,
  3877. 0, CL_PORT_SERVER_OPTIONS);
  3878. tt_int_op(ret, OP_EQ, -1);
  3879. done:
  3880. smartlist_free(slout);
  3881. tor_free(config_port_invalid);
  3882. tor_free(config_port_valid);
  3883. }
  3884. #define CONFIG_TEST(name, flags) \
  3885. { #name, test_config_ ## name, flags, NULL, NULL }
  3886. struct testcase_t config_tests[] = {
  3887. CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
  3888. CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
  3889. CONFIG_TEST(parsing_trusted_dir_server, 0),
  3890. CONFIG_TEST(parsing_fallback_dir_server, 0),
  3891. CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
  3892. CONFIG_TEST(adding_dir_servers, TT_FORK),
  3893. CONFIG_TEST(default_dir_servers, TT_FORK),
  3894. CONFIG_TEST(default_fallback_dirs, 0),
  3895. CONFIG_TEST(resolve_my_address, TT_FORK),
  3896. CONFIG_TEST(addressmap, 0),
  3897. CONFIG_TEST(parse_bridge_line, 0),
  3898. CONFIG_TEST(parse_transport_options_line, 0),
  3899. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  3900. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  3901. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  3902. CONFIG_TEST(fix_my_family, 0),
  3903. CONFIG_TEST(directory_fetch, 0),
  3904. CONFIG_TEST(parse_port_config__listenaddress, 0),
  3905. CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
  3906. CONFIG_TEST(parse_port_config__ports__server_options, 0),
  3907. CONFIG_TEST(parse_port_config__ports__ports_given, 0),
  3908. END_OF_TESTCASES
  3909. };