test_config.c 211 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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 "core/or/or.h"
  10. #include "lib/net/address.h"
  11. #include "lib/net/resolve.h"
  12. #include "feature/client/addressmap.h"
  13. #include "feature/client/bridges.h"
  14. #include "core/or/circuitmux_ewma.h"
  15. #include "core/or/circuitbuild.h"
  16. #include "app/config/config.h"
  17. #include "lib/confmgt/confparse.h"
  18. #include "core/mainloop/connection.h"
  19. #include "core/or/connection_edge.h"
  20. #include "test/test.h"
  21. #include "core/or/connection_or.h"
  22. #include "feature/control/control.h"
  23. #include "core/mainloop/cpuworker.h"
  24. #include "feature/dircache/dirserv.h"
  25. #include "feature/dirauth/dirvote.h"
  26. #include "feature/relay/dns.h"
  27. #include "feature/client/entrynodes.h"
  28. #include "feature/client/transports.h"
  29. #include "feature/relay/ext_orport.h"
  30. #include "lib/geoip/geoip.h"
  31. #include "feature/hibernate/hibernate.h"
  32. #include "core/mainloop/mainloop.h"
  33. #include "feature/nodelist/networkstatus.h"
  34. #include "feature/nodelist/nodelist.h"
  35. #include "core/or/policies.h"
  36. #include "feature/rend/rendclient.h"
  37. #include "feature/rend/rendservice.h"
  38. #include "feature/relay/router.h"
  39. #include "feature/relay/routermode.h"
  40. #include "feature/nodelist/dirlist.h"
  41. #include "feature/nodelist/routerlist.h"
  42. #include "feature/nodelist/routerset.h"
  43. #include "app/config/statefile.h"
  44. #include "test/test_helpers.h"
  45. #include "test/resolve_test_helpers.h"
  46. #include "feature/dirclient/dir_server_st.h"
  47. #include "core/or/port_cfg_st.h"
  48. #include "feature/nodelist/routerinfo_st.h"
  49. #include "lib/fs/conffile.h"
  50. #include "lib/meminfo/meminfo.h"
  51. #include "lib/net/gethostname.h"
  52. #include "lib/encoding/confline.h"
  53. #include "lib/encoding/kvline.h"
  54. #ifdef HAVE_UNISTD_H
  55. #include <unistd.h>
  56. #endif
  57. #ifdef HAVE_SYS_STAT_H
  58. #include <sys/stat.h>
  59. #endif
  60. static void
  61. test_config_addressmap(void *arg)
  62. {
  63. char buf[1024];
  64. char address[256];
  65. time_t expires = TIME_MAX;
  66. (void)arg;
  67. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  68. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  69. "MapAddress *.google.com *.torserver.exit\n"
  70. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  71. "MapAddress *.cn.com www.cnn.com\n"
  72. "MapAddress *.cnn.com www.cnn.com\n"
  73. "MapAddress ex.com www.cnn.com\n"
  74. "MapAddress ey.com *.cnn.com\n"
  75. "MapAddress www.torproject.org 1.1.1.1\n"
  76. "MapAddress other.torproject.org "
  77. "this.torproject.org.otherserver.exit\n"
  78. "MapAddress test.torproject.org 2.2.2.2\n"
  79. "MapAddress www.google.com 3.3.3.3\n"
  80. "MapAddress www.example.org 4.4.4.4\n"
  81. "MapAddress 4.4.4.4 7.7.7.7\n"
  82. "MapAddress 4.4.4.4 5.5.5.5\n"
  83. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  84. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  85. , sizeof(buf));
  86. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  87. config_register_addressmaps(get_options());
  88. /* Use old interface for now, so we don't need to rewrite the unit tests */
  89. #define addressmap_rewrite(a,s,eo,ao) \
  90. addressmap_rewrite((a),(s), ~0, (eo),(ao))
  91. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  92. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  93. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  94. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  95. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  96. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  97. /* Where no mapping for FQDN match on top-level domain */
  98. /* MapAddress .google.com .torserver.exit */
  99. strlcpy(address, "reader.google.com", sizeof(address));
  100. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  101. tt_str_op(address,OP_EQ, "reader.torserver.exit");
  102. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  103. strlcpy(address, "reader.yahoo.com", sizeof(address));
  104. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  105. tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
  106. /*MapAddress *.cnn.com www.cnn.com */
  107. strlcpy(address, "cnn.com", sizeof(address));
  108. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  109. tt_str_op(address,OP_EQ, "www.cnn.com");
  110. /* MapAddress .cn.com www.cnn.com */
  111. strlcpy(address, "www.cn.com", sizeof(address));
  112. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  113. tt_str_op(address,OP_EQ, "www.cnn.com");
  114. /* MapAddress ex.com www.cnn.com - no match */
  115. strlcpy(address, "www.ex.com", sizeof(address));
  116. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  117. /* MapAddress ey.com *.cnn.com - invalid expression */
  118. strlcpy(address, "ey.com", sizeof(address));
  119. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  120. /* Where mapping for FQDN match on FQDN */
  121. strlcpy(address, "www.google.com", sizeof(address));
  122. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  123. tt_str_op(address,OP_EQ, "3.3.3.3");
  124. strlcpy(address, "www.torproject.org", sizeof(address));
  125. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  126. tt_str_op(address,OP_EQ, "1.1.1.1");
  127. strlcpy(address, "other.torproject.org", sizeof(address));
  128. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  129. tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
  130. strlcpy(address, "test.torproject.org", sizeof(address));
  131. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  132. tt_str_op(address,OP_EQ, "2.2.2.2");
  133. /* Test a chain of address mappings and the order in which they were added:
  134. "MapAddress www.example.org 4.4.4.4"
  135. "MapAddress 4.4.4.4 7.7.7.7"
  136. "MapAddress 4.4.4.4 5.5.5.5"
  137. */
  138. strlcpy(address, "www.example.org", sizeof(address));
  139. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  140. tt_str_op(address,OP_EQ, "5.5.5.5");
  141. /* Test infinite address mapping results in no change */
  142. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  143. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  144. tt_str_op(address,OP_EQ, "www.infiniteloop.org");
  145. /* Test we don't find false positives */
  146. strlcpy(address, "www.example.com", sizeof(address));
  147. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  148. /* Test top-level-domain matching a bit harder */
  149. config_free_lines(get_options_mutable()->AddressMap);
  150. addressmap_clear_configured();
  151. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  152. "MapAddress *.torproject.org 1.1.1.1\n"
  153. "MapAddress *.net 2.2.2.2\n"
  154. , sizeof(buf));
  155. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  156. config_register_addressmaps(get_options());
  157. strlcpy(address, "www.abc.com", sizeof(address));
  158. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  159. tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
  160. strlcpy(address, "www.def.com", sizeof(address));
  161. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  162. tt_str_op(address,OP_EQ, "www.def.torserver.exit");
  163. strlcpy(address, "www.torproject.org", sizeof(address));
  164. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  165. tt_str_op(address,OP_EQ, "1.1.1.1");
  166. strlcpy(address, "test.torproject.org", sizeof(address));
  167. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  168. tt_str_op(address,OP_EQ, "1.1.1.1");
  169. strlcpy(address, "torproject.net", sizeof(address));
  170. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  171. tt_str_op(address,OP_EQ, "2.2.2.2");
  172. /* We don't support '*' as a mapping directive */
  173. config_free_lines(get_options_mutable()->AddressMap);
  174. addressmap_clear_configured();
  175. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  176. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  177. config_register_addressmaps(get_options());
  178. strlcpy(address, "www.abc.com", sizeof(address));
  179. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  180. strlcpy(address, "www.def.net", sizeof(address));
  181. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  182. strlcpy(address, "www.torproject.org", sizeof(address));
  183. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  184. #undef addressmap_rewrite
  185. done:
  186. config_free_lines(get_options_mutable()->AddressMap);
  187. get_options_mutable()->AddressMap = NULL;
  188. addressmap_free_all();
  189. }
  190. static int
  191. is_private_dir(const char* path)
  192. {
  193. struct stat st;
  194. int r = stat(path, &st);
  195. if (r) {
  196. return 0;
  197. }
  198. #if !defined (_WIN32)
  199. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  200. return 0;
  201. }
  202. #endif
  203. return 1;
  204. }
  205. static void
  206. test_config_check_or_create_data_subdir(void *arg)
  207. {
  208. or_options_t *options = get_options_mutable();
  209. char *datadir;
  210. const char *subdir = "test_stats";
  211. char *subpath;
  212. struct stat st;
  213. int r;
  214. #if !defined (_WIN32)
  215. unsigned group_permission;
  216. #endif
  217. (void)arg;
  218. tor_free(options->DataDirectory);
  219. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  220. subpath = get_datadir_fname(subdir);
  221. #if defined (_WIN32)
  222. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  223. #else
  224. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  225. #endif
  226. r = stat(subpath, &st);
  227. // The subdirectory shouldn't exist yet,
  228. // but should be created by the call to check_or_create_data_subdir.
  229. tt_assert(r && (errno == ENOENT));
  230. tt_assert(!check_or_create_data_subdir(subdir));
  231. tt_assert(is_private_dir(subpath));
  232. // The check should return 0, if the directory already exists
  233. // and is private to the user.
  234. tt_assert(!check_or_create_data_subdir(subdir));
  235. r = stat(subpath, &st);
  236. if (r) {
  237. tt_abort_perror("stat");
  238. }
  239. #if !defined (_WIN32)
  240. group_permission = st.st_mode | 0070;
  241. r = chmod(subpath, group_permission);
  242. if (r) {
  243. tt_abort_perror("chmod");
  244. }
  245. // If the directory exists, but its mode is too permissive
  246. // a call to check_or_create_data_subdir should reset the mode.
  247. tt_assert(!is_private_dir(subpath));
  248. tt_assert(!check_or_create_data_subdir(subdir));
  249. tt_assert(is_private_dir(subpath));
  250. #endif /* !defined (_WIN32) */
  251. done:
  252. rmdir(subpath);
  253. tor_free(datadir);
  254. tor_free(subpath);
  255. }
  256. static void
  257. test_config_write_to_data_subdir(void *arg)
  258. {
  259. or_options_t* options = get_options_mutable();
  260. char *datadir;
  261. char *cp = NULL;
  262. const char* subdir = "test_stats";
  263. const char* fname = "test_file";
  264. const char* str =
  265. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  266. "elitr, sed diam nonumy eirmod\n"
  267. "tempor invidunt ut labore et dolore magna aliquyam\n"
  268. "erat, sed diam voluptua.\n"
  269. "At vero eos et accusam et justo duo dolores et ea\n"
  270. "rebum. Stet clita kasd gubergren,\n"
  271. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  272. "Lorem ipsum dolor sit amet,\n"
  273. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  274. "tempor invidunt ut labore et dolore\n"
  275. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  276. "accusam et justo duo dolores et\n"
  277. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  278. "sanctus est Lorem ipsum dolor sit amet.";
  279. char* filepath = NULL;
  280. (void)arg;
  281. tor_free(options->DataDirectory);
  282. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  283. filepath = get_datadir_fname2(subdir, fname);
  284. #if defined (_WIN32)
  285. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  286. #else
  287. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  288. #endif
  289. // Write attempt should fail, if subdirectory doesn't exist.
  290. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  291. tt_assert(! check_or_create_data_subdir(subdir));
  292. // Content of file after write attempt should be
  293. // equal to the original string.
  294. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  295. cp = read_file_to_str(filepath, 0, NULL);
  296. tt_str_op(cp,OP_EQ, str);
  297. tor_free(cp);
  298. // A second write operation should overwrite the old content.
  299. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  300. cp = read_file_to_str(filepath, 0, NULL);
  301. tt_str_op(cp,OP_EQ, str);
  302. tor_free(cp);
  303. done:
  304. (void) unlink(filepath);
  305. rmdir(options->DataDirectory);
  306. tor_free(datadir);
  307. tor_free(filepath);
  308. tor_free(cp);
  309. }
  310. /* Test helper function: Make sure that a bridge line gets parsed
  311. * properly. Also make sure that the resulting bridge_line_t structure
  312. * has its fields set correctly. */
  313. static void
  314. good_bridge_line_test(const char *string, const char *test_addrport,
  315. const char *test_digest, const char *test_transport,
  316. const smartlist_t *test_socks_args)
  317. {
  318. char *tmp = NULL;
  319. bridge_line_t *bridge_line = parse_bridge_line(string);
  320. tt_assert(bridge_line);
  321. /* test addrport */
  322. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  323. tt_str_op(test_addrport,OP_EQ, tmp);
  324. tor_free(tmp);
  325. /* If we were asked to validate a digest, but we did not get a
  326. digest after parsing, we failed. */
  327. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  328. tt_abort();
  329. /* If we were not asked to validate a digest, and we got a digest
  330. after parsing, we failed again. */
  331. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  332. tt_abort();
  333. /* If we were asked to validate a digest, and we got a digest after
  334. parsing, make sure it's correct. */
  335. if (test_digest) {
  336. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  337. tor_strlower(tmp);
  338. tt_str_op(test_digest,OP_EQ, tmp);
  339. tor_free(tmp);
  340. }
  341. /* If we were asked to validate a transport name, make sure tha it
  342. matches with the transport name that was parsed. */
  343. if (test_transport && !bridge_line->transport_name)
  344. tt_abort();
  345. if (!test_transport && bridge_line->transport_name)
  346. tt_abort();
  347. if (test_transport)
  348. tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
  349. /* Validate the SOCKS argument smartlist. */
  350. if (test_socks_args && !bridge_line->socks_args)
  351. tt_abort();
  352. if (!test_socks_args && bridge_line->socks_args)
  353. tt_abort();
  354. if (test_socks_args)
  355. tt_assert(smartlist_strings_eq(test_socks_args,
  356. bridge_line->socks_args));
  357. done:
  358. tor_free(tmp);
  359. bridge_line_free(bridge_line);
  360. }
  361. /* Test helper function: Make sure that a bridge line is
  362. * unparseable. */
  363. static void
  364. bad_bridge_line_test(const char *string)
  365. {
  366. bridge_line_t *bridge_line = parse_bridge_line(string);
  367. if (bridge_line)
  368. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  369. tt_ptr_op(bridge_line, OP_EQ, NULL);
  370. done:
  371. bridge_line_free(bridge_line);
  372. }
  373. static void
  374. test_config_parse_bridge_line(void *arg)
  375. {
  376. (void) arg;
  377. good_bridge_line_test("192.0.2.1:4123",
  378. "192.0.2.1:4123", NULL, NULL, NULL);
  379. good_bridge_line_test("192.0.2.1",
  380. "192.0.2.1:443", NULL, NULL, NULL);
  381. good_bridge_line_test("transport [::1]",
  382. "[::1]:443", NULL, "transport", NULL);
  383. good_bridge_line_test("transport 192.0.2.1:12 "
  384. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  385. "192.0.2.1:12",
  386. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  387. "transport", NULL);
  388. {
  389. smartlist_t *sl_tmp = smartlist_new();
  390. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  391. good_bridge_line_test("transport 192.0.2.1:12 "
  392. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  393. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  394. "transport", sl_tmp);
  395. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  396. smartlist_free(sl_tmp);
  397. }
  398. {
  399. smartlist_t *sl_tmp = smartlist_new();
  400. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  401. smartlist_add_asprintf(sl_tmp, "z=z");
  402. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  403. "192.0.2.1:12", NULL, "transport", sl_tmp);
  404. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  405. smartlist_free(sl_tmp);
  406. }
  407. {
  408. smartlist_t *sl_tmp = smartlist_new();
  409. smartlist_add_asprintf(sl_tmp, "dub=come");
  410. smartlist_add_asprintf(sl_tmp, "save=me");
  411. good_bridge_line_test("transport 192.0.2.1:12 "
  412. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  413. "dub=come save=me",
  414. "192.0.2.1:12",
  415. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  416. "transport", sl_tmp);
  417. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  418. smartlist_free(sl_tmp);
  419. }
  420. good_bridge_line_test("192.0.2.1:1231 "
  421. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  422. "192.0.2.1:1231",
  423. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  424. NULL, NULL);
  425. /* Empty line */
  426. bad_bridge_line_test("");
  427. /* bad transport name */
  428. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  429. /* weird ip address */
  430. bad_bridge_line_test("a.b.c.d");
  431. /* invalid fpr */
  432. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  433. /* no k=v in the end */
  434. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  435. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  436. /* no addrport */
  437. bad_bridge_line_test("asdw");
  438. /* huge k=v value that can't fit in SOCKS fields */
  439. bad_bridge_line_test(
  440. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  441. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  442. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  443. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  444. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  445. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  446. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  447. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  448. "aa=b");
  449. }
  450. static void
  451. test_config_parse_transport_options_line(void *arg)
  452. {
  453. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  454. (void) arg;
  455. { /* too small line */
  456. options_sl = get_options_from_transport_options_line("valley", NULL);
  457. tt_ptr_op(options_sl, OP_EQ, NULL);
  458. }
  459. { /* no k=v values */
  460. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  461. tt_ptr_op(options_sl, OP_EQ, NULL);
  462. }
  463. { /* correct line, but wrong transport specified */
  464. options_sl =
  465. get_options_from_transport_options_line("trebuchet k=v", "rook");
  466. tt_ptr_op(options_sl, OP_EQ, NULL);
  467. }
  468. { /* correct -- no transport specified */
  469. sl_tmp = smartlist_new();
  470. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  471. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  472. options_sl =
  473. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  474. NULL);
  475. tt_assert(options_sl);
  476. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  477. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  478. smartlist_free(sl_tmp);
  479. sl_tmp = NULL;
  480. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  481. smartlist_free(options_sl);
  482. options_sl = NULL;
  483. }
  484. { /* correct -- correct transport specified */
  485. sl_tmp = smartlist_new();
  486. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  487. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  488. options_sl =
  489. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  490. "rook");
  491. tt_assert(options_sl);
  492. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  493. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  494. smartlist_free(sl_tmp);
  495. sl_tmp = NULL;
  496. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  497. smartlist_free(options_sl);
  498. options_sl = NULL;
  499. }
  500. done:
  501. if (options_sl) {
  502. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  503. smartlist_free(options_sl);
  504. }
  505. if (sl_tmp) {
  506. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  507. smartlist_free(sl_tmp);
  508. }
  509. }
  510. /* Mocks needed for the compute_max_mem_in_queues test */
  511. static int get_total_system_memory_mock(size_t *mem_out);
  512. static size_t total_system_memory_output = 0;
  513. static int total_system_memory_return = 0;
  514. static int
  515. get_total_system_memory_mock(size_t *mem_out)
  516. {
  517. if (! mem_out)
  518. return -1;
  519. *mem_out = total_system_memory_output;
  520. return total_system_memory_return;
  521. }
  522. /* Mocks needed for the transport plugin line test */
  523. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  524. char **proxy_argv, int is_server);
  525. static int transport_add_from_config_mock(const tor_addr_t *addr,
  526. uint16_t port, const char *name,
  527. int socks_ver);
  528. static int transport_is_needed_mock(const char *transport_name);
  529. static int pt_kickstart_proxy_mock_call_count = 0;
  530. static int transport_add_from_config_mock_call_count = 0;
  531. static int transport_is_needed_mock_call_count = 0;
  532. static int transport_is_needed_mock_return = 0;
  533. static void
  534. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  535. char **proxy_argv, int is_server)
  536. {
  537. (void) transport_list;
  538. (void) proxy_argv;
  539. (void) is_server;
  540. /* XXXX check that args are as expected. */
  541. ++pt_kickstart_proxy_mock_call_count;
  542. free_execve_args(proxy_argv);
  543. }
  544. static int
  545. transport_add_from_config_mock(const tor_addr_t *addr,
  546. uint16_t port, const char *name,
  547. int socks_ver)
  548. {
  549. (void) addr;
  550. (void) port;
  551. (void) name;
  552. (void) socks_ver;
  553. /* XXXX check that args are as expected. */
  554. ++transport_add_from_config_mock_call_count;
  555. return 0;
  556. }
  557. static int
  558. transport_is_needed_mock(const char *transport_name)
  559. {
  560. (void) transport_name;
  561. /* XXXX check that arg is as expected. */
  562. ++transport_is_needed_mock_call_count;
  563. return transport_is_needed_mock_return;
  564. }
  565. /**
  566. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  567. * options.
  568. */
  569. static void
  570. test_config_parse_transport_plugin_line(void *arg)
  571. {
  572. (void)arg;
  573. or_options_t *options = get_options_mutable();
  574. int r, tmp;
  575. int old_pt_kickstart_proxy_mock_call_count;
  576. int old_transport_add_from_config_mock_call_count;
  577. int old_transport_is_needed_mock_call_count;
  578. /* Bad transport lines - too short */
  579. r = parse_transport_line(options, "bad", 1, 0);
  580. tt_int_op(r, OP_LT, 0);
  581. r = parse_transport_line(options, "bad", 1, 1);
  582. tt_int_op(r, OP_LT, 0);
  583. r = parse_transport_line(options, "bad bad", 1, 0);
  584. tt_int_op(r, OP_LT, 0);
  585. r = parse_transport_line(options, "bad bad", 1, 1);
  586. tt_int_op(r, OP_LT, 0);
  587. /* Test transport list parsing */
  588. r = parse_transport_line(options,
  589. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  590. tt_int_op(r, OP_EQ, 0);
  591. r = parse_transport_line(options,
  592. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  593. tt_int_op(r, OP_EQ, 0);
  594. r = parse_transport_line(options,
  595. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  596. tt_int_op(r, OP_EQ, 0);
  597. r = parse_transport_line(options,
  598. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  599. tt_int_op(r, OP_EQ, 0);
  600. /* Bad transport identifiers */
  601. r = parse_transport_line(options,
  602. "transport_* exec /usr/bin/fake-transport", 1, 0);
  603. tt_int_op(r, OP_LT, 0);
  604. r = parse_transport_line(options,
  605. "transport_* exec /usr/bin/fake-transport", 1, 1);
  606. tt_int_op(r, OP_LT, 0);
  607. /* Check SOCKS cases for client transport */
  608. r = parse_transport_line(options,
  609. "transport_1 socks4 1.2.3.4:567", 1, 0);
  610. tt_int_op(r, OP_EQ, 0);
  611. r = parse_transport_line(options,
  612. "transport_1 socks5 1.2.3.4:567", 1, 0);
  613. tt_int_op(r, OP_EQ, 0);
  614. /* Proxy case for server transport */
  615. r = parse_transport_line(options,
  616. "transport_1 proxy 1.2.3.4:567", 1, 1);
  617. tt_int_op(r, OP_EQ, 0);
  618. /* Multiple-transport error exit */
  619. r = parse_transport_line(options,
  620. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  621. tt_int_op(r, OP_LT, 0);
  622. r = parse_transport_line(options,
  623. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  624. tt_int_op(r, OP_LT, 0);
  625. /* No port error exit */
  626. r = parse_transport_line(options,
  627. "transport_1 socks5 1.2.3.4", 1, 0);
  628. tt_int_op(r, OP_LT, 0);
  629. r = parse_transport_line(options,
  630. "transport_1 proxy 1.2.3.4", 1, 1);
  631. tt_int_op(r, OP_LT, 0);
  632. /* Unparsable address error exit */
  633. r = parse_transport_line(options,
  634. "transport_1 socks5 1.2.3:6x7", 1, 0);
  635. tt_int_op(r, OP_LT, 0);
  636. r = parse_transport_line(options,
  637. "transport_1 proxy 1.2.3:6x7", 1, 1);
  638. tt_int_op(r, OP_LT, 0);
  639. /* "Strange {Client|Server}TransportPlugin field" error exit */
  640. r = parse_transport_line(options,
  641. "transport_1 foo bar", 1, 0);
  642. tt_int_op(r, OP_LT, 0);
  643. r = parse_transport_line(options,
  644. "transport_1 foo bar", 1, 1);
  645. tt_int_op(r, OP_LT, 0);
  646. /* No sandbox mode error exit */
  647. tmp = options->Sandbox;
  648. options->Sandbox = 1;
  649. r = parse_transport_line(options,
  650. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  651. tt_int_op(r, OP_LT, 0);
  652. r = parse_transport_line(options,
  653. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  654. tt_int_op(r, OP_LT, 0);
  655. options->Sandbox = tmp;
  656. /*
  657. * These final test cases cover code paths that only activate without
  658. * validate_only, so they need mocks in place.
  659. */
  660. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  661. old_pt_kickstart_proxy_mock_call_count =
  662. pt_kickstart_proxy_mock_call_count;
  663. r = parse_transport_line(options,
  664. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  665. tt_int_op(r, OP_EQ, 0);
  666. tt_assert(pt_kickstart_proxy_mock_call_count ==
  667. old_pt_kickstart_proxy_mock_call_count + 1);
  668. UNMOCK(pt_kickstart_proxy);
  669. /* This one hits a log line in the !validate_only case only */
  670. r = parse_transport_line(options,
  671. "transport_1 proxy 1.2.3.4:567", 0, 1);
  672. tt_int_op(r, OP_EQ, 0);
  673. /* Check mocked client transport cases */
  674. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  675. MOCK(transport_add_from_config, transport_add_from_config_mock);
  676. MOCK(transport_is_needed, transport_is_needed_mock);
  677. /* Unnecessary transport case */
  678. transport_is_needed_mock_return = 0;
  679. old_pt_kickstart_proxy_mock_call_count =
  680. pt_kickstart_proxy_mock_call_count;
  681. old_transport_add_from_config_mock_call_count =
  682. transport_add_from_config_mock_call_count;
  683. old_transport_is_needed_mock_call_count =
  684. transport_is_needed_mock_call_count;
  685. r = parse_transport_line(options,
  686. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  687. /* Should have succeeded */
  688. tt_int_op(r, OP_EQ, 0);
  689. /* transport_is_needed() should have been called */
  690. tt_assert(transport_is_needed_mock_call_count ==
  691. old_transport_is_needed_mock_call_count + 1);
  692. /*
  693. * pt_kickstart_proxy() and transport_add_from_config() should
  694. * not have been called.
  695. */
  696. tt_assert(pt_kickstart_proxy_mock_call_count ==
  697. old_pt_kickstart_proxy_mock_call_count);
  698. tt_assert(transport_add_from_config_mock_call_count ==
  699. old_transport_add_from_config_mock_call_count);
  700. /* Necessary transport case */
  701. transport_is_needed_mock_return = 1;
  702. old_pt_kickstart_proxy_mock_call_count =
  703. pt_kickstart_proxy_mock_call_count;
  704. old_transport_add_from_config_mock_call_count =
  705. transport_add_from_config_mock_call_count;
  706. old_transport_is_needed_mock_call_count =
  707. transport_is_needed_mock_call_count;
  708. r = parse_transport_line(options,
  709. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  710. /* Should have succeeded */
  711. tt_int_op(r, OP_EQ, 0);
  712. /*
  713. * transport_is_needed() and pt_kickstart_proxy() should have been
  714. * called.
  715. */
  716. tt_assert(pt_kickstart_proxy_mock_call_count ==
  717. old_pt_kickstart_proxy_mock_call_count + 1);
  718. tt_assert(transport_is_needed_mock_call_count ==
  719. old_transport_is_needed_mock_call_count + 1);
  720. /* transport_add_from_config() should not have been called. */
  721. tt_assert(transport_add_from_config_mock_call_count ==
  722. old_transport_add_from_config_mock_call_count);
  723. /* proxy case */
  724. transport_is_needed_mock_return = 1;
  725. old_pt_kickstart_proxy_mock_call_count =
  726. pt_kickstart_proxy_mock_call_count;
  727. old_transport_add_from_config_mock_call_count =
  728. transport_add_from_config_mock_call_count;
  729. old_transport_is_needed_mock_call_count =
  730. transport_is_needed_mock_call_count;
  731. r = parse_transport_line(options,
  732. "transport_1 socks5 1.2.3.4:567", 0, 0);
  733. /* Should have succeeded */
  734. tt_int_op(r, OP_EQ, 0);
  735. /*
  736. * transport_is_needed() and transport_add_from_config() should have
  737. * been called.
  738. */
  739. tt_assert(transport_add_from_config_mock_call_count ==
  740. old_transport_add_from_config_mock_call_count + 1);
  741. tt_assert(transport_is_needed_mock_call_count ==
  742. old_transport_is_needed_mock_call_count + 1);
  743. /* pt_kickstart_proxy() should not have been called. */
  744. tt_assert(pt_kickstart_proxy_mock_call_count ==
  745. old_pt_kickstart_proxy_mock_call_count);
  746. /* Done with mocked client transport cases */
  747. UNMOCK(transport_is_needed);
  748. UNMOCK(transport_add_from_config);
  749. UNMOCK(pt_kickstart_proxy);
  750. done:
  751. /* Make sure we undo all mocks */
  752. UNMOCK(pt_kickstart_proxy);
  753. UNMOCK(transport_add_from_config);
  754. UNMOCK(transport_is_needed);
  755. return;
  756. }
  757. // Tests if an options with MyFamily fingerprints missing '$' normalises
  758. // them correctly and also ensure it also works with multiple fingerprints
  759. static void
  760. test_config_fix_my_family(void *arg)
  761. {
  762. char *err = NULL;
  763. config_line_t *family = tor_malloc_zero(sizeof(config_line_t));
  764. family->key = tor_strdup("MyFamily");
  765. family->value = tor_strdup("$1111111111111111111111111111111111111111, "
  766. "1111111111111111111111111111111111111112, "
  767. "$1111111111111111111111111111111111111113");
  768. config_line_t *family2 = tor_malloc_zero(sizeof(config_line_t));
  769. family2->key = tor_strdup("MyFamily");
  770. family2->value = tor_strdup("1111111111111111111111111111111111111114");
  771. config_line_t *family3 = tor_malloc_zero(sizeof(config_line_t));
  772. family3->key = tor_strdup("MyFamily");
  773. family3->value = tor_strdup("$1111111111111111111111111111111111111115");
  774. family->next = family2;
  775. family2->next = family3;
  776. family3->next = NULL;
  777. or_options_t* options = options_new();
  778. or_options_t* defaults = options_new();
  779. (void) arg;
  780. options_init(options);
  781. options_init(defaults);
  782. options->MyFamily_lines = family;
  783. options_validate(NULL, options, defaults, 0, &err) ;
  784. if (err != NULL) {
  785. TT_FAIL(("options_validate failed: %s", err));
  786. }
  787. const char *valid[] = { "$1111111111111111111111111111111111111111",
  788. "$1111111111111111111111111111111111111112",
  789. "$1111111111111111111111111111111111111113",
  790. "$1111111111111111111111111111111111111114",
  791. "$1111111111111111111111111111111111111115" };
  792. int ret_size = 0;
  793. config_line_t *ret;
  794. for (ret = options->MyFamily; ret && ret_size < 5; ret = ret->next) {
  795. tt_str_op(ret->value, OP_EQ, valid[ret_size]);
  796. ret_size++;
  797. }
  798. tt_int_op(ret_size, OP_EQ, 5);
  799. done:
  800. tor_free(err);
  801. or_options_free(options);
  802. or_options_free(defaults);
  803. }
  804. static int n_hostname_01010101 = 0;
  805. /** This mock function is meant to replace tor_lookup_hostname().
  806. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  807. * This function increments <b>n_hostname_01010101</b> counter by one
  808. * every time it is called.
  809. */
  810. static int
  811. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  812. {
  813. n_hostname_01010101++;
  814. if (name && addr) {
  815. *addr = ntohl(0x01010101);
  816. }
  817. return 0;
  818. }
  819. static int n_hostname_localhost = 0;
  820. /** This mock function is meant to replace tor_lookup_hostname().
  821. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  822. * This function increments <b>n_hostname_localhost</b> counter by one
  823. * every time it is called.
  824. */
  825. static int
  826. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  827. {
  828. n_hostname_localhost++;
  829. if (name && addr) {
  830. *addr = 0x7f000001;
  831. }
  832. return 0;
  833. }
  834. static int n_hostname_failure = 0;
  835. /** This mock function is meant to replace tor_lookup_hostname().
  836. * It pretends to fail by returning -1 to caller. Also, this function
  837. * increments <b>n_hostname_failure</b> every time it is called.
  838. */
  839. static int
  840. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  841. {
  842. (void)name;
  843. (void)addr;
  844. n_hostname_failure++;
  845. return -1;
  846. }
  847. static int n_gethostname_replacement = 0;
  848. /** This mock function is meant to replace tor_gethostname(). It
  849. * responds with string "onionrouter!" as hostname. This function
  850. * increments <b>n_gethostname_replacement</b> by one every time
  851. * it is called.
  852. */
  853. static int
  854. tor_gethostname_replacement(char *name, size_t namelen)
  855. {
  856. n_gethostname_replacement++;
  857. if (name && namelen) {
  858. strlcpy(name,"onionrouter!",namelen);
  859. }
  860. return 0;
  861. }
  862. static int n_gethostname_localhost = 0;
  863. /** This mock function is meant to replace tor_gethostname(). It
  864. * responds with string "127.0.0.1" as hostname. This function
  865. * increments <b>n_gethostname_localhost</b> by one every time
  866. * it is called.
  867. */
  868. static int
  869. tor_gethostname_localhost(char *name, size_t namelen)
  870. {
  871. n_gethostname_localhost++;
  872. if (name && namelen) {
  873. strlcpy(name,"127.0.0.1",namelen);
  874. }
  875. return 0;
  876. }
  877. static int n_gethostname_failure = 0;
  878. /** This mock function is meant to replace tor_gethostname.
  879. * It pretends to fail by returning -1. This function increments
  880. * <b>n_gethostname_failure</b> by one every time it is called.
  881. */
  882. static int
  883. tor_gethostname_failure(char *name, size_t namelen)
  884. {
  885. (void)name;
  886. (void)namelen;
  887. n_gethostname_failure++;
  888. return -1;
  889. }
  890. static int n_get_interface_address = 0;
  891. /** This mock function is meant to replace get_interface_address().
  892. * It answers with address 8.8.8.8. This function increments
  893. * <b>n_get_interface_address</b> by one every time it is called.
  894. */
  895. static int
  896. get_interface_address_08080808(int severity, uint32_t *addr)
  897. {
  898. (void)severity;
  899. n_get_interface_address++;
  900. if (addr) {
  901. *addr = ntohl(0x08080808);
  902. }
  903. return 0;
  904. }
  905. static int n_get_interface_address6 = 0;
  906. static sa_family_t last_address6_family;
  907. /** This mock function is meant to replace get_interface_address6().
  908. * It answers with IP address 9.9.9.9 iff both of the following are true:
  909. * - <b>family</b> is AF_INET
  910. * - <b>addr</b> pointer is not NULL.
  911. * This function increments <b>n_get_interface_address6</b> by one every
  912. * time it is called.
  913. */
  914. static int
  915. get_interface_address6_replacement(int severity, sa_family_t family,
  916. tor_addr_t *addr)
  917. {
  918. (void)severity;
  919. last_address6_family = family;
  920. n_get_interface_address6++;
  921. if ((family != AF_INET) || !addr) {
  922. return -1;
  923. }
  924. tor_addr_from_ipv4h(addr,0x09090909);
  925. return 0;
  926. }
  927. static int n_get_interface_address_failure = 0;
  928. /**
  929. * This mock function is meant to replace get_interface_address().
  930. * It pretends to fail getting interface address by returning -1.
  931. * <b>n_get_interface_address_failure</b> is incremented by one
  932. * every time this function is called.
  933. */
  934. static int
  935. get_interface_address_failure(int severity, uint32_t *addr)
  936. {
  937. (void)severity;
  938. (void)addr;
  939. n_get_interface_address_failure++;
  940. return -1;
  941. }
  942. static int n_get_interface_address6_failure = 0;
  943. /**
  944. * This mock function is meant to replace get_interface_addres6().
  945. * It will pretend to fail by return -1.
  946. * <b>n_get_interface_address6_failure</b> is incremented by one
  947. * every time this function is called and <b>last_address6_family</b>
  948. * is assigned the value of <b>family</b> argument.
  949. */
  950. static int
  951. get_interface_address6_failure(int severity, sa_family_t family,
  952. tor_addr_t *addr)
  953. {
  954. (void)severity;
  955. (void)addr;
  956. n_get_interface_address6_failure++;
  957. last_address6_family = family;
  958. return -1;
  959. }
  960. static void
  961. test_config_resolve_my_address(void *arg)
  962. {
  963. or_options_t *options;
  964. uint32_t resolved_addr;
  965. const char *method_used;
  966. char *hostname_out = NULL;
  967. int retval;
  968. int prev_n_hostname_01010101;
  969. int prev_n_hostname_localhost;
  970. int prev_n_hostname_failure;
  971. int prev_n_gethostname_replacement;
  972. int prev_n_gethostname_failure;
  973. int prev_n_gethostname_localhost;
  974. int prev_n_get_interface_address;
  975. int prev_n_get_interface_address_failure;
  976. int prev_n_get_interface_address6;
  977. int prev_n_get_interface_address6_failure;
  978. (void)arg;
  979. options = options_new();
  980. options_init(options);
  981. /*
  982. * CASE 1:
  983. * If options->Address is a valid IPv4 address string, we want
  984. * the corresponding address to be parsed and returned.
  985. */
  986. options->Address = tor_strdup("128.52.128.105");
  987. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  988. &method_used,&hostname_out);
  989. tt_want(retval == 0);
  990. tt_want_str_op(method_used,OP_EQ,"CONFIGURED");
  991. tt_want(hostname_out == NULL);
  992. tt_assert(resolved_addr == 0x80348069);
  993. tor_free(options->Address);
  994. /*
  995. * CASE 2:
  996. * If options->Address is a valid DNS address, we want resolve_my_address()
  997. * function to ask tor_lookup_hostname() for help with resolving it
  998. * and return the address that was resolved (in host order).
  999. */
  1000. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  1001. tor_free(options->Address);
  1002. options->Address = tor_strdup("www.torproject.org");
  1003. prev_n_hostname_01010101 = n_hostname_01010101;
  1004. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1005. &method_used,&hostname_out);
  1006. tt_want(retval == 0);
  1007. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  1008. tt_want_str_op(method_used,OP_EQ,"RESOLVED");
  1009. tt_want_str_op(hostname_out,OP_EQ,"www.torproject.org");
  1010. tt_assert(resolved_addr == 0x01010101);
  1011. UNMOCK(tor_lookup_hostname);
  1012. tor_free(options->Address);
  1013. tor_free(hostname_out);
  1014. /*
  1015. * CASE 3:
  1016. * Given that options->Address is NULL, we want resolve_my_address()
  1017. * to try and use tor_gethostname() to get hostname AND use
  1018. * tor_lookup_hostname() to get IP address.
  1019. */
  1020. resolved_addr = 0;
  1021. tor_free(options->Address);
  1022. options->Address = NULL;
  1023. MOCK(tor_gethostname,tor_gethostname_replacement);
  1024. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  1025. prev_n_gethostname_replacement = n_gethostname_replacement;
  1026. prev_n_hostname_01010101 = n_hostname_01010101;
  1027. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1028. &method_used,&hostname_out);
  1029. tt_want(retval == 0);
  1030. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1031. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  1032. tt_want_str_op(method_used,OP_EQ,"GETHOSTNAME");
  1033. tt_want_str_op(hostname_out,OP_EQ,"onionrouter!");
  1034. tt_assert(resolved_addr == 0x01010101);
  1035. UNMOCK(tor_gethostname);
  1036. UNMOCK(tor_lookup_hostname);
  1037. tor_free(hostname_out);
  1038. /*
  1039. * CASE 4:
  1040. * Given that options->Address is a local host address, we want
  1041. * resolve_my_address() function to fail.
  1042. */
  1043. resolved_addr = 0;
  1044. tor_free(options->Address);
  1045. options->Address = tor_strdup("127.0.0.1");
  1046. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1047. &method_used,&hostname_out);
  1048. tt_want(resolved_addr == 0);
  1049. tt_int_op(retval, OP_EQ, -1);
  1050. tor_free(options->Address);
  1051. tor_free(hostname_out);
  1052. /*
  1053. * CASE 5:
  1054. * We want resolve_my_address() to fail if DNS address in options->Address
  1055. * cannot be resolved.
  1056. */
  1057. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1058. prev_n_hostname_failure = n_hostname_failure;
  1059. tor_free(options->Address);
  1060. options->Address = tor_strdup("www.tor-project.org");
  1061. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1062. &method_used,&hostname_out);
  1063. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1064. tt_int_op(retval, OP_EQ, -1);
  1065. UNMOCK(tor_lookup_hostname);
  1066. tor_free(options->Address);
  1067. tor_free(hostname_out);
  1068. /*
  1069. * CASE 6:
  1070. * If options->Address is NULL AND gettting local hostname fails, we want
  1071. * resolve_my_address() to fail as well.
  1072. */
  1073. MOCK(tor_gethostname,tor_gethostname_failure);
  1074. prev_n_gethostname_failure = n_gethostname_failure;
  1075. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1076. &method_used,&hostname_out);
  1077. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1078. tt_int_op(retval, OP_EQ, -1);
  1079. UNMOCK(tor_gethostname);
  1080. tor_free(hostname_out);
  1081. /*
  1082. * CASE 7:
  1083. * We want resolve_my_address() to try and get network interface address via
  1084. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1085. * resolved into IP address.
  1086. */
  1087. MOCK(tor_gethostname,tor_gethostname_replacement);
  1088. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1089. MOCK(get_interface_address,get_interface_address_08080808);
  1090. prev_n_gethostname_replacement = n_gethostname_replacement;
  1091. prev_n_get_interface_address = n_get_interface_address;
  1092. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1093. &method_used,&hostname_out);
  1094. tt_want(retval == 0);
  1095. tt_want_int_op(n_gethostname_replacement, OP_EQ,
  1096. prev_n_gethostname_replacement + 1);
  1097. tt_want_int_op(n_get_interface_address, OP_EQ,
  1098. prev_n_get_interface_address + 1);
  1099. tt_want_str_op(method_used,OP_EQ,"INTERFACE");
  1100. tt_want(hostname_out == NULL);
  1101. tt_assert(resolved_addr == 0x08080808);
  1102. UNMOCK(get_interface_address);
  1103. tor_free(hostname_out);
  1104. /*
  1105. * CASE 8:
  1106. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1107. * is unresolvable. We want resolve_my_address to fail if
  1108. * get_interface_address() fails.
  1109. */
  1110. MOCK(get_interface_address,get_interface_address_failure);
  1111. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1112. prev_n_gethostname_replacement = n_gethostname_replacement;
  1113. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1114. &method_used,&hostname_out);
  1115. tt_want(n_get_interface_address_failure ==
  1116. prev_n_get_interface_address_failure + 1);
  1117. tt_want(n_gethostname_replacement ==
  1118. prev_n_gethostname_replacement + 1);
  1119. tt_int_op(retval, OP_EQ, -1);
  1120. UNMOCK(get_interface_address);
  1121. tor_free(hostname_out);
  1122. /*
  1123. * CASE 9:
  1124. * Given that options->Address is NULL AND tor_lookup_hostname()
  1125. * fails AND hostname returned by gethostname() resolves
  1126. * to local IP address, we want resolve_my_address() function to
  1127. * call get_interface_address6(.,AF_INET,.) and return IP address
  1128. * the latter function has found.
  1129. */
  1130. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1131. MOCK(tor_gethostname,tor_gethostname_replacement);
  1132. MOCK(get_interface_address6,get_interface_address6_replacement);
  1133. prev_n_gethostname_replacement = n_gethostname_replacement;
  1134. prev_n_hostname_failure = n_hostname_failure;
  1135. prev_n_get_interface_address6 = n_get_interface_address6;
  1136. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1137. &method_used,&hostname_out);
  1138. tt_want(last_address6_family == AF_INET);
  1139. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1140. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1141. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1142. tt_want(retval == 0);
  1143. tt_want_str_op(method_used,OP_EQ,"INTERFACE");
  1144. tt_assert(resolved_addr == 0x09090909);
  1145. UNMOCK(tor_lookup_hostname);
  1146. UNMOCK(tor_gethostname);
  1147. UNMOCK(get_interface_address6);
  1148. tor_free(hostname_out);
  1149. /*
  1150. * CASE 10: We want resolve_my_address() to fail if all of the following
  1151. * are true:
  1152. * 1. options->Address is not NULL
  1153. * 2. ... but it cannot be converted to struct in_addr by
  1154. * tor_inet_aton()
  1155. * 3. ... and tor_lookup_hostname() fails to resolve the
  1156. * options->Address
  1157. */
  1158. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1159. prev_n_hostname_failure = n_hostname_failure;
  1160. tor_free(options->Address);
  1161. options->Address = tor_strdup("some_hostname");
  1162. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1163. &method_used,&hostname_out);
  1164. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1165. tt_int_op(retval, OP_EQ, -1);
  1166. UNMOCK(tor_gethostname);
  1167. UNMOCK(tor_lookup_hostname);
  1168. tor_free(hostname_out);
  1169. /*
  1170. * CASE 11:
  1171. * Suppose the following sequence of events:
  1172. * 1. options->Address is NULL
  1173. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1174. * if running on.
  1175. * 3. Hostname from previous step cannot be converted to
  1176. * address by using tor_inet_aton() function.
  1177. * 4. However, tor_lookup_hostname() succeeds in resolving the
  1178. * hostname from step 2.
  1179. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1180. * to be internal.
  1181. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1182. * IPv4
  1183. *
  1184. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1185. * and address from step 6.
  1186. */
  1187. tor_free(options->Address);
  1188. options->Address = NULL;
  1189. MOCK(tor_gethostname,tor_gethostname_replacement);
  1190. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1191. MOCK(get_interface_address6,get_interface_address6_replacement);
  1192. prev_n_gethostname_replacement = n_gethostname_replacement;
  1193. prev_n_hostname_localhost = n_hostname_localhost;
  1194. prev_n_get_interface_address6 = n_get_interface_address6;
  1195. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1196. &method_used,&hostname_out);
  1197. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1198. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1199. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1200. tt_str_op(method_used,OP_EQ,"INTERFACE");
  1201. tt_ptr_op(hostname_out, OP_EQ, NULL);
  1202. tt_int_op(retval, OP_EQ, 0);
  1203. /*
  1204. * CASE 11b:
  1205. * 1-5 as above.
  1206. * 6. get_interface_address6() fails.
  1207. *
  1208. * In this subcase, we want resolve_my_address() to fail.
  1209. */
  1210. UNMOCK(get_interface_address6);
  1211. MOCK(get_interface_address6,get_interface_address6_failure);
  1212. prev_n_gethostname_replacement = n_gethostname_replacement;
  1213. prev_n_hostname_localhost = n_hostname_localhost;
  1214. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1215. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1216. &method_used,&hostname_out);
  1217. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1218. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1219. tt_want(n_get_interface_address6_failure ==
  1220. prev_n_get_interface_address6_failure + 1);
  1221. tt_int_op(retval, OP_EQ, -1);
  1222. UNMOCK(tor_gethostname);
  1223. UNMOCK(tor_lookup_hostname);
  1224. UNMOCK(get_interface_address6);
  1225. /* CASE 12:
  1226. * Suppose the following happens:
  1227. * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
  1228. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1229. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1230. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1231. * internal.
  1232. *
  1233. * In this case, we want resolve_my_address() to fail.
  1234. */
  1235. tor_free(options->Address);
  1236. options->Address = NULL;
  1237. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1238. MOCK(tor_gethostname,tor_gethostname_localhost);
  1239. prev_n_gethostname_localhost = n_gethostname_localhost;
  1240. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1241. &method_used,&hostname_out);
  1242. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1243. tt_int_op(retval, OP_EQ, -1);
  1244. UNMOCK(tor_gethostname);
  1245. done:
  1246. tor_free(options->Address);
  1247. tor_free(options->DirAuthorities);
  1248. or_options_free(options);
  1249. tor_free(hostname_out);
  1250. UNMOCK(tor_gethostname);
  1251. UNMOCK(tor_lookup_hostname);
  1252. UNMOCK(get_interface_address);
  1253. UNMOCK(get_interface_address6);
  1254. UNMOCK(tor_gethostname);
  1255. }
  1256. static void
  1257. test_config_adding_trusted_dir_server(void *arg)
  1258. {
  1259. (void)arg;
  1260. const char digest[DIGEST_LEN] = "";
  1261. dir_server_t *ds = NULL;
  1262. tor_addr_port_t ipv6;
  1263. int rv = -1;
  1264. clear_dir_servers();
  1265. routerlist_free_all();
  1266. /* create a trusted ds without an IPv6 address and port */
  1267. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1268. NULL, V3_DIRINFO, 1.0);
  1269. tt_assert(ds);
  1270. dir_server_add(ds);
  1271. tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 1);
  1272. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
  1273. /* create a trusted ds with an IPv6 address and port */
  1274. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1275. tt_int_op(rv, OP_EQ, 0);
  1276. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, &ipv6, digest,
  1277. NULL, V3_DIRINFO, 1.0);
  1278. tt_assert(ds);
  1279. dir_server_add(ds);
  1280. tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 2);
  1281. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
  1282. done:
  1283. clear_dir_servers();
  1284. routerlist_free_all();
  1285. }
  1286. static void
  1287. test_config_adding_fallback_dir_server(void *arg)
  1288. {
  1289. (void)arg;
  1290. const char digest[DIGEST_LEN] = "";
  1291. dir_server_t *ds = NULL;
  1292. tor_addr_t ipv4;
  1293. tor_addr_port_t ipv6;
  1294. int rv = -1;
  1295. clear_dir_servers();
  1296. routerlist_free_all();
  1297. rv = tor_addr_parse(&ipv4, "127.0.0.1");
  1298. tt_int_op(rv, OP_EQ, AF_INET);
  1299. /* create a trusted ds without an IPv6 address and port */
  1300. ds = fallback_dir_server_new(&ipv4, 9059, 9060, NULL, digest, 1.0);
  1301. tt_assert(ds);
  1302. dir_server_add(ds);
  1303. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
  1304. /* create a trusted ds with an IPv6 address and port */
  1305. rv = tor_addr_port_parse(LOG_WARN, "[::1]:9061", &ipv6.addr, &ipv6.port, -1);
  1306. tt_int_op(rv, OP_EQ, 0);
  1307. ds = fallback_dir_server_new(&ipv4, 9059, 9060, &ipv6, digest, 1.0);
  1308. tt_assert(ds);
  1309. dir_server_add(ds);
  1310. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 2);
  1311. done:
  1312. clear_dir_servers();
  1313. routerlist_free_all();
  1314. }
  1315. /* No secrets here:
  1316. * v3ident is `echo "onion" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1317. * fingerprint is `echo "unionem" | shasum | cut -d" " -f1 | tr "a-f" "A-F"`
  1318. * with added spaces
  1319. */
  1320. #define TEST_DIR_AUTH_LINE_START \
  1321. "foobar orport=12345 " \
  1322. "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
  1323. #define TEST_DIR_AUTH_LINE_END \
  1324. "1.2.3.4:54321 " \
  1325. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1326. #define TEST_DIR_AUTH_IPV6_FLAG \
  1327. "ipv6=[feed::beef]:9 "
  1328. static void
  1329. test_config_parsing_trusted_dir_server(void *arg)
  1330. {
  1331. (void)arg;
  1332. int rv = -1;
  1333. /* parse a trusted dir server without an IPv6 address and port */
  1334. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1335. TEST_DIR_AUTH_LINE_END,
  1336. V3_DIRINFO, 1);
  1337. tt_int_op(rv, OP_EQ, 0);
  1338. /* parse a trusted dir server with an IPv6 address and port */
  1339. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1340. TEST_DIR_AUTH_IPV6_FLAG
  1341. TEST_DIR_AUTH_LINE_END,
  1342. V3_DIRINFO, 1);
  1343. tt_int_op(rv, OP_EQ, 0);
  1344. /* Since we are only validating, there is no cleanup. */
  1345. done:
  1346. ;
  1347. }
  1348. #undef TEST_DIR_AUTH_LINE_START
  1349. #undef TEST_DIR_AUTH_LINE_END
  1350. #undef TEST_DIR_AUTH_IPV6_FLAG
  1351. #define TEST_DIR_AUTH_LINE_START \
  1352. "foobar orport=12345 " \
  1353. "v3ident=14C131DFC5C6F93646BE72FA1401C02A8DF2E8B4 "
  1354. #define TEST_DIR_AUTH_LINE_END_BAD_IP \
  1355. "0.256.3.4:54321 " \
  1356. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1357. #define TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR \
  1358. "torproject.org:54321 " \
  1359. "FDB2 FBD2 AAA5 25FA 2999 E617 5091 5A32 C777 3B17"
  1360. static void
  1361. test_config_parsing_invalid_dir_address(void *arg)
  1362. {
  1363. (void)arg;
  1364. int rv;
  1365. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1366. TEST_DIR_AUTH_LINE_END_BAD_IP,
  1367. V3_DIRINFO, 1);
  1368. tt_int_op(rv, OP_EQ, -1);
  1369. rv = parse_dir_authority_line(TEST_DIR_AUTH_LINE_START
  1370. TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR,
  1371. V3_DIRINFO, 1);
  1372. tt_int_op(rv, OP_EQ, -1);
  1373. done:
  1374. return;
  1375. }
  1376. #undef TEST_DIR_AUTH_LINE_START
  1377. #undef TEST_DIR_AUTH_LINE_END_BAD_IP
  1378. #undef TEST_DIR_AUTH_LINE_END_WITH_DNS_ADDR
  1379. /* No secrets here:
  1380. * id is `echo "syn-propanethial-S-oxide" | shasum | cut -d" " -f1`
  1381. */
  1382. #define TEST_DIR_FALLBACK_LINE \
  1383. "1.2.3.4:54321 orport=12345 " \
  1384. "id=50e643986f31ea1235bcc1af17a1c5c5cfc0ee54 "
  1385. #define TEST_DIR_FALLBACK_IPV6_FLAG \
  1386. "ipv6=[2015:c0de::deed]:9"
  1387. static void
  1388. test_config_parsing_fallback_dir_server(void *arg)
  1389. {
  1390. (void)arg;
  1391. int rv = -1;
  1392. /* parse a trusted dir server without an IPv6 address and port */
  1393. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE, 1);
  1394. tt_int_op(rv, OP_EQ, 0);
  1395. /* parse a trusted dir server with an IPv6 address and port */
  1396. rv = parse_dir_fallback_line(TEST_DIR_FALLBACK_LINE
  1397. TEST_DIR_FALLBACK_IPV6_FLAG,
  1398. 1);
  1399. tt_int_op(rv, OP_EQ, 0);
  1400. /* Since we are only validating, there is no cleanup. */
  1401. done:
  1402. ;
  1403. }
  1404. #undef TEST_DIR_FALLBACK_LINE
  1405. #undef TEST_DIR_FALLBACK_IPV6_FLAG
  1406. static void
  1407. test_config_adding_default_trusted_dir_servers(void *arg)
  1408. {
  1409. (void)arg;
  1410. clear_dir_servers();
  1411. routerlist_free_all();
  1412. /* Assume we only have one bridge authority */
  1413. add_default_trusted_dir_authorities(BRIDGE_DIRINFO);
  1414. tt_int_op(get_n_authorities(BRIDGE_DIRINFO), OP_EQ, 1);
  1415. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 1);
  1416. /* Assume we have eight V3 authorities */
  1417. add_default_trusted_dir_authorities(V3_DIRINFO);
  1418. tt_int_op(get_n_authorities(V3_DIRINFO), OP_EQ, 9);
  1419. tt_int_op(smartlist_len(router_get_fallback_dir_servers()), OP_EQ, 10);
  1420. done:
  1421. clear_dir_servers();
  1422. routerlist_free_all();
  1423. }
  1424. static int n_add_default_fallback_dir_servers_known_default = 0;
  1425. /**
  1426. * This mock function is meant to replace add_default_fallback_dir_servers().
  1427. * It will parse and add one known default fallback dir server,
  1428. * which has a dir_port of 99.
  1429. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1430. * one every time this function is called.
  1431. */
  1432. static void
  1433. add_default_fallback_dir_servers_known_default(void)
  1434. {
  1435. int i;
  1436. const char *fallback[] = {
  1437. "127.0.0.1:60099 orport=9009 "
  1438. "id=0923456789012345678901234567890123456789",
  1439. NULL
  1440. };
  1441. for (i=0; fallback[i]; i++) {
  1442. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1443. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1444. fallback[i]);
  1445. }
  1446. }
  1447. n_add_default_fallback_dir_servers_known_default++;
  1448. }
  1449. /* Helper for test_config_adding_dir_servers(), which should be
  1450. * refactored: clear the fields in the options which the options object
  1451. * does not really own. */
  1452. static void
  1453. ads_clear_helper(or_options_t *options)
  1454. {
  1455. options->DirAuthorities = NULL;
  1456. options->AlternateBridgeAuthority = NULL;
  1457. options->AlternateDirAuthority = NULL;
  1458. options->FallbackDir = NULL;
  1459. }
  1460. /* Test all the different combinations of adding dir servers */
  1461. static void
  1462. test_config_adding_dir_servers(void *arg)
  1463. {
  1464. (void)arg;
  1465. /* allocate options */
  1466. or_options_t *options = options_new();
  1467. /* Allocate and populate configuration lines:
  1468. *
  1469. * Use the same format as the hard-coded directories in
  1470. * add_default_trusted_dir_authorities().
  1471. * Zeroing the structure has the same effect as initialising to:
  1472. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1473. */
  1474. config_line_t *test_dir_authority = tor_malloc_zero(sizeof(config_line_t));
  1475. test_dir_authority->key = tor_strdup("DirAuthority");
  1476. test_dir_authority->value = tor_strdup(
  1477. "D0 orport=9000 "
  1478. "v3ident=0023456789012345678901234567890123456789 "
  1479. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1480. );
  1481. config_line_t *test_alt_bridge_authority = tor_malloc_zero(
  1482. sizeof(config_line_t));
  1483. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1484. test_alt_bridge_authority->value = tor_strdup(
  1485. "B1 orport=9001 bridge "
  1486. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1487. );
  1488. config_line_t *test_alt_dir_authority = tor_malloc_zero(
  1489. sizeof(config_line_t));
  1490. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1491. test_alt_dir_authority->value = tor_strdup(
  1492. "A2 orport=9002 "
  1493. "v3ident=0223456789012345678901234567890123456789 "
  1494. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1495. );
  1496. /* Use the format specified in the manual page */
  1497. config_line_t *test_fallback_directory = tor_malloc_zero(
  1498. sizeof(config_line_t));
  1499. test_fallback_directory->key = tor_strdup("FallbackDir");
  1500. test_fallback_directory->value = tor_strdup(
  1501. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1502. );
  1503. /* We need to know if add_default_fallback_dir_servers is called,
  1504. * whatever the size of the list in fallback_dirs.inc,
  1505. * so we use a version of add_default_fallback_dir_servers that adds
  1506. * one known default fallback directory. */
  1507. MOCK(add_default_fallback_dir_servers,
  1508. add_default_fallback_dir_servers_known_default);
  1509. /* There are 16 different cases, covering each combination of set/NULL for:
  1510. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1511. * FallbackDir. (We always set UseDefaultFallbackDirs to 1.)
  1512. * But validate_dir_servers() ensures that:
  1513. * "You cannot set both DirAuthority and Alternate*Authority."
  1514. * This reduces the number of cases to 10.
  1515. *
  1516. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1517. * So 1001 or case 9 is:
  1518. * DirAuthorities set,
  1519. * AlternateBridgeAuthority NULL,
  1520. * AlternateDirAuthority NULL
  1521. * FallbackDir set
  1522. * The valid cases are cases 0-9 counting using this method, as every case
  1523. * greater than or equal to 10 = 1010 is invalid.
  1524. *
  1525. * 1. Outcome: Use Set Directory Authorities
  1526. * - No Default Authorities
  1527. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1528. * if they are set
  1529. * Cases expected to yield this outcome:
  1530. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1531. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1532. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1533. *
  1534. * 2. Outcome: Use Set Bridge Authority
  1535. * - Use Default Non-Bridge Directory Authorities
  1536. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1537. * Cases expected to yield this outcome:
  1538. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1539. * AlternateBridgeAuthority is set, and
  1540. * AlternateDirAuthority is NULL)
  1541. *
  1542. * 3. Outcome: Use Set Alternate Directory Authority
  1543. * - Use Default Bridge Authorities
  1544. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1545. * Cases expected to yield this outcome:
  1546. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1547. * are both NULL, but AlternateDirAuthority is set)
  1548. *
  1549. * 4. Outcome: Use Set Custom Fallback Directory
  1550. * - Use Default Bridge & Directory Authorities
  1551. * Cases expected to yield this outcome:
  1552. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1553. * are all NULL, but FallbackDir is set)
  1554. *
  1555. * 5. Outcome: Use All Defaults
  1556. * - Use Default Bridge & Directory Authorities, and
  1557. * Default Fallback Directories
  1558. * Cases expected to yield this outcome:
  1559. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1560. * and FallbackDir are all NULL)
  1561. */
  1562. /*
  1563. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1564. * are hard-coded into this build.
  1565. * This code makes some assumptions about the implementation.
  1566. * If they are wrong, one or more of cases 0-5 could fail.
  1567. */
  1568. int n_default_alt_bridge_authority = 0;
  1569. int n_default_alt_dir_authority = 0;
  1570. int n_default_fallback_dir = 0;
  1571. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1572. + (n_default_alt_dir_authority))
  1573. /* Pre-Count Number of Authorities of Each Type
  1574. * Use 0000: No Directory Authorities or Fallback Directories Set
  1575. */
  1576. {
  1577. /* clear fallback dirs counter */
  1578. n_add_default_fallback_dir_servers_known_default = 0;
  1579. /* clear options*/
  1580. ads_clear_helper(options);
  1581. or_options_free(options);
  1582. options = options_new();
  1583. /* clear any previous dir servers:
  1584. consider_adding_dir_servers() should do this anyway */
  1585. clear_dir_servers();
  1586. /* assign options: 0000 */
  1587. options->DirAuthorities = NULL;
  1588. options->AlternateBridgeAuthority = NULL;
  1589. options->AlternateDirAuthority = NULL;
  1590. options->FallbackDir = NULL;
  1591. options->UseDefaultFallbackDirs = 1;
  1592. /* parse options - ensure we always update by passing NULL old_options */
  1593. consider_adding_dir_servers(options, NULL);
  1594. /* check outcome */
  1595. /* we must have added the default fallback dirs */
  1596. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
  1597. /* we have more fallbacks than just the authorities */
  1598. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1599. {
  1600. /* fallback_dir_servers */
  1601. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1602. /* Count Bridge Authorities */
  1603. SMARTLIST_FOREACH(fallback_servers,
  1604. dir_server_t *,
  1605. ds,
  1606. /* increment the found counter if it's a bridge auth */
  1607. n_default_alt_bridge_authority +=
  1608. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1609. 1 : 0)
  1610. );
  1611. /* If we have no default bridge authority, something has gone wrong */
  1612. tt_int_op(n_default_alt_bridge_authority, OP_GE, 1);
  1613. /* Count v3 Authorities */
  1614. SMARTLIST_FOREACH(fallback_servers,
  1615. dir_server_t *,
  1616. ds,
  1617. /* increment found counter if it's a v3 auth */
  1618. n_default_alt_dir_authority +=
  1619. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1620. 1 : 0)
  1621. );
  1622. /* If we have no default authorities, something has gone really wrong */
  1623. tt_int_op(n_default_alt_dir_authority, OP_GE, 1);
  1624. /* Calculate Fallback Directory Count */
  1625. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1626. n_default_alt_bridge_authority -
  1627. n_default_alt_dir_authority);
  1628. /* If we have a negative count, something has gone really wrong,
  1629. * or some authorities aren't being added as fallback directories.
  1630. * (networkstatus_consensus_can_use_extra_fallbacks depends on all
  1631. * authorities being fallback directories.) */
  1632. tt_int_op(n_default_fallback_dir, OP_GE, 0);
  1633. }
  1634. }
  1635. /*
  1636. * 1. Outcome: Use Set Directory Authorities
  1637. * - No Default Authorities
  1638. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1639. * if they are set
  1640. * Cases expected to yield this outcome:
  1641. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1642. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1643. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1644. */
  1645. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1646. AlternateDirAuthority Not Set, FallbackDir Set */
  1647. {
  1648. /* clear fallback dirs counter */
  1649. n_add_default_fallback_dir_servers_known_default = 0;
  1650. /* clear options*/
  1651. ads_clear_helper(options);
  1652. or_options_free(options);
  1653. options = options_new();
  1654. /* clear any previous dir servers:
  1655. consider_adding_dir_servers() should do this anyway */
  1656. clear_dir_servers();
  1657. /* assign options: 1001 */
  1658. options->DirAuthorities = test_dir_authority;
  1659. options->AlternateBridgeAuthority = NULL;
  1660. options->AlternateDirAuthority = NULL;
  1661. options->FallbackDir = test_fallback_directory;
  1662. options->UseDefaultFallbackDirs = 1;
  1663. /* parse options - ensure we always update by passing NULL old_options */
  1664. consider_adding_dir_servers(options, NULL);
  1665. /* check outcome */
  1666. /* we must not have added the default fallback dirs */
  1667. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  1668. /* we have more fallbacks than just the authorities */
  1669. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1670. {
  1671. /* trusted_dir_servers */
  1672. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1673. /* D0, (No B1), (No A2) */
  1674. tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
  1675. /* DirAuthority - D0 - dir_port: 60090 */
  1676. int found_D0 = 0;
  1677. SMARTLIST_FOREACH(dir_servers,
  1678. dir_server_t *,
  1679. ds,
  1680. /* increment the found counter if dir_port matches */
  1681. found_D0 +=
  1682. (ds->dir_port == 60090 ?
  1683. 1 : 0)
  1684. );
  1685. tt_int_op(found_D0, OP_EQ, 1);
  1686. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1687. int found_B1 = 0;
  1688. SMARTLIST_FOREACH(dir_servers,
  1689. dir_server_t *,
  1690. ds,
  1691. /* increment the found counter if dir_port matches */
  1692. found_B1 +=
  1693. (ds->dir_port == 60091 ?
  1694. 1 : 0)
  1695. );
  1696. tt_int_op(found_B1, OP_EQ, 0);
  1697. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1698. int found_A2 = 0;
  1699. SMARTLIST_FOREACH(dir_servers,
  1700. dir_server_t *,
  1701. ds,
  1702. /* increment the found counter if dir_port matches */
  1703. found_A2 +=
  1704. (ds->dir_port == 60092 ?
  1705. 1 : 0)
  1706. );
  1707. tt_int_op(found_A2, OP_EQ, 0);
  1708. }
  1709. {
  1710. /* fallback_dir_servers */
  1711. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1712. /* D0, (No B1), (No A2), Custom Fallback */
  1713. tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
  1714. /* DirAuthority - D0 - dir_port: 60090 */
  1715. int found_D0 = 0;
  1716. SMARTLIST_FOREACH(fallback_servers,
  1717. dir_server_t *,
  1718. ds,
  1719. /* increment the found counter if dir_port matches */
  1720. found_D0 +=
  1721. (ds->dir_port == 60090 ?
  1722. 1 : 0)
  1723. );
  1724. tt_int_op(found_D0, OP_EQ, 1);
  1725. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1726. int found_B1 = 0;
  1727. SMARTLIST_FOREACH(fallback_servers,
  1728. dir_server_t *,
  1729. ds,
  1730. /* increment the found counter if dir_port matches */
  1731. found_B1 +=
  1732. (ds->dir_port == 60091 ?
  1733. 1 : 0)
  1734. );
  1735. tt_int_op(found_B1, OP_EQ, 0);
  1736. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1737. int found_A2 = 0;
  1738. SMARTLIST_FOREACH(fallback_servers,
  1739. dir_server_t *,
  1740. ds,
  1741. /* increment the found counter if dir_port matches */
  1742. found_A2 +=
  1743. (ds->dir_port == 60092 ?
  1744. 1 : 0)
  1745. );
  1746. tt_int_op(found_A2, OP_EQ, 0);
  1747. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1748. int found_non_default_fallback = 0;
  1749. SMARTLIST_FOREACH(fallback_servers,
  1750. dir_server_t *,
  1751. ds,
  1752. /* increment the found counter if dir_port matches */
  1753. found_non_default_fallback +=
  1754. (ds->dir_port == 60093 ?
  1755. 1 : 0)
  1756. );
  1757. tt_int_op(found_non_default_fallback, OP_EQ, 1);
  1758. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1759. int found_default_fallback = 0;
  1760. SMARTLIST_FOREACH(fallback_servers,
  1761. dir_server_t *,
  1762. ds,
  1763. /* increment the found counter if dir_port matches */
  1764. found_default_fallback +=
  1765. (ds->dir_port == 60099 ?
  1766. 1 : 0)
  1767. );
  1768. tt_int_op(found_default_fallback, OP_EQ, 0);
  1769. }
  1770. }
  1771. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1772. {
  1773. /* clear fallback dirs counter */
  1774. n_add_default_fallback_dir_servers_known_default = 0;
  1775. /* clear options*/
  1776. ads_clear_helper(options);
  1777. or_options_free(options);
  1778. options = options_new();
  1779. /* clear any previous dir servers:
  1780. consider_adding_dir_servers() should do this anyway */
  1781. clear_dir_servers();
  1782. /* assign options: 1000 */
  1783. options->DirAuthorities = test_dir_authority;
  1784. options->AlternateBridgeAuthority = NULL;
  1785. options->AlternateDirAuthority = NULL;
  1786. options->FallbackDir = NULL;
  1787. options->UseDefaultFallbackDirs = 1;
  1788. /* parse options - ensure we always update by passing NULL old_options */
  1789. consider_adding_dir_servers(options, NULL);
  1790. /* check outcome */
  1791. /* we must not have added the default fallback dirs */
  1792. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  1793. /* we just have the authorities */
  1794. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  1795. {
  1796. /* trusted_dir_servers */
  1797. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1798. /* D0, (No B1), (No A2) */
  1799. tt_int_op(smartlist_len(dir_servers), OP_EQ, 1);
  1800. /* DirAuthority - D0 - dir_port: 60090 */
  1801. int found_D0 = 0;
  1802. SMARTLIST_FOREACH(dir_servers,
  1803. dir_server_t *,
  1804. ds,
  1805. /* increment the found counter if dir_port matches */
  1806. found_D0 +=
  1807. (ds->dir_port == 60090 ?
  1808. 1 : 0)
  1809. );
  1810. tt_int_op(found_D0, OP_EQ, 1);
  1811. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1812. int found_B1 = 0;
  1813. SMARTLIST_FOREACH(dir_servers,
  1814. dir_server_t *,
  1815. ds,
  1816. /* increment the found counter if dir_port matches */
  1817. found_B1 +=
  1818. (ds->dir_port == 60091 ?
  1819. 1 : 0)
  1820. );
  1821. tt_int_op(found_B1, OP_EQ, 0);
  1822. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1823. int found_A2 = 0;
  1824. SMARTLIST_FOREACH(dir_servers,
  1825. dir_server_t *,
  1826. ds,
  1827. /* increment the found counter if dir_port matches */
  1828. found_A2 +=
  1829. (ds->dir_port == 60092 ?
  1830. 1 : 0)
  1831. );
  1832. tt_int_op(found_A2, OP_EQ, 0);
  1833. }
  1834. {
  1835. /* fallback_dir_servers */
  1836. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1837. /* D0, (No B1), (No A2), (No Fallback) */
  1838. tt_int_op(smartlist_len(fallback_servers), OP_EQ, 1);
  1839. /* DirAuthority - D0 - dir_port: 60090 */
  1840. int found_D0 = 0;
  1841. SMARTLIST_FOREACH(fallback_servers,
  1842. dir_server_t *,
  1843. ds,
  1844. /* increment the found counter if dir_port matches */
  1845. found_D0 +=
  1846. (ds->dir_port == 60090 ?
  1847. 1 : 0)
  1848. );
  1849. tt_int_op(found_D0, OP_EQ, 1);
  1850. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1851. int found_B1 = 0;
  1852. SMARTLIST_FOREACH(fallback_servers,
  1853. dir_server_t *,
  1854. ds,
  1855. /* increment the found counter if dir_port matches */
  1856. found_B1 +=
  1857. (ds->dir_port == 60091 ?
  1858. 1 : 0)
  1859. );
  1860. tt_int_op(found_B1, OP_EQ, 0);
  1861. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1862. int found_A2 = 0;
  1863. SMARTLIST_FOREACH(fallback_servers,
  1864. dir_server_t *,
  1865. ds,
  1866. /* increment the found counter if dir_port matches */
  1867. found_A2 +=
  1868. (ds->dir_port == 60092 ?
  1869. 1 : 0)
  1870. );
  1871. tt_int_op(found_A2, OP_EQ, 0);
  1872. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1873. int found_non_default_fallback = 0;
  1874. SMARTLIST_FOREACH(fallback_servers,
  1875. dir_server_t *,
  1876. ds,
  1877. /* increment the found counter if dir_port matches */
  1878. found_non_default_fallback +=
  1879. (ds->dir_port == 60093 ?
  1880. 1 : 0)
  1881. );
  1882. tt_int_op(found_non_default_fallback, OP_EQ, 0);
  1883. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1884. int found_default_fallback = 0;
  1885. SMARTLIST_FOREACH(fallback_servers,
  1886. dir_server_t *,
  1887. ds,
  1888. /* increment the found counter if dir_port matches */
  1889. found_default_fallback +=
  1890. (ds->dir_port == 60099 ?
  1891. 1 : 0)
  1892. );
  1893. tt_int_op(found_default_fallback, OP_EQ, 0);
  1894. }
  1895. }
  1896. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1897. {
  1898. /* clear fallback dirs counter */
  1899. n_add_default_fallback_dir_servers_known_default = 0;
  1900. /* clear options*/
  1901. ads_clear_helper(options);
  1902. or_options_free(options);
  1903. options = options_new();
  1904. /* clear any previous dir servers:
  1905. consider_adding_dir_servers() should do this anyway */
  1906. clear_dir_servers();
  1907. /* assign options: 0111 */
  1908. options->DirAuthorities = NULL;
  1909. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1910. options->AlternateDirAuthority = test_alt_dir_authority;
  1911. options->FallbackDir = test_fallback_directory;
  1912. options->UseDefaultFallbackDirs = 1;
  1913. /* parse options - ensure we always update by passing NULL old_options */
  1914. consider_adding_dir_servers(options, NULL);
  1915. /* check outcome */
  1916. /* we must not have added the default fallback dirs */
  1917. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  1918. /* we have more fallbacks than just the authorities */
  1919. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  1920. {
  1921. /* trusted_dir_servers */
  1922. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1923. /* (No D0), B1, A2 */
  1924. tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
  1925. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1926. int found_D0 = 0;
  1927. SMARTLIST_FOREACH(dir_servers,
  1928. dir_server_t *,
  1929. ds,
  1930. /* increment the found counter if dir_port matches */
  1931. found_D0 +=
  1932. (ds->dir_port == 60090 ?
  1933. 1 : 0)
  1934. );
  1935. tt_int_op(found_D0, OP_EQ, 0);
  1936. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1937. int found_B1 = 0;
  1938. SMARTLIST_FOREACH(dir_servers,
  1939. dir_server_t *,
  1940. ds,
  1941. /* increment the found counter if dir_port matches */
  1942. found_B1 +=
  1943. (ds->dir_port == 60091 ?
  1944. 1 : 0)
  1945. );
  1946. tt_int_op(found_B1, OP_EQ, 1);
  1947. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1948. int found_A2 = 0;
  1949. SMARTLIST_FOREACH(dir_servers,
  1950. dir_server_t *,
  1951. ds,
  1952. /* increment the found counter if dir_port matches */
  1953. found_A2 +=
  1954. (ds->dir_port == 60092 ?
  1955. 1 : 0)
  1956. );
  1957. tt_int_op(found_A2, OP_EQ, 1);
  1958. }
  1959. {
  1960. /* fallback_dir_servers */
  1961. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1962. /* (No D0), B1, A2, Custom Fallback */
  1963. tt_int_op(smartlist_len(fallback_servers), OP_EQ, 3);
  1964. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1965. int found_D0 = 0;
  1966. SMARTLIST_FOREACH(fallback_servers,
  1967. dir_server_t *,
  1968. ds,
  1969. /* increment the found counter if dir_port matches */
  1970. found_D0 +=
  1971. (ds->dir_port == 60090 ?
  1972. 1 : 0)
  1973. );
  1974. tt_int_op(found_D0, OP_EQ, 0);
  1975. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1976. int found_B1 = 0;
  1977. SMARTLIST_FOREACH(fallback_servers,
  1978. dir_server_t *,
  1979. ds,
  1980. /* increment the found counter if dir_port matches */
  1981. found_B1 +=
  1982. (ds->dir_port == 60091 ?
  1983. 1 : 0)
  1984. );
  1985. tt_int_op(found_B1, OP_EQ, 1);
  1986. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1987. int found_A2 = 0;
  1988. SMARTLIST_FOREACH(fallback_servers,
  1989. dir_server_t *,
  1990. ds,
  1991. /* increment the found counter if dir_port matches */
  1992. found_A2 +=
  1993. (ds->dir_port == 60092 ?
  1994. 1 : 0)
  1995. );
  1996. tt_int_op(found_A2, OP_EQ, 1);
  1997. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1998. int found_non_default_fallback = 0;
  1999. SMARTLIST_FOREACH(fallback_servers,
  2000. dir_server_t *,
  2001. ds,
  2002. /* increment the found counter if dir_port matches */
  2003. found_non_default_fallback +=
  2004. (ds->dir_port == 60093 ?
  2005. 1 : 0)
  2006. );
  2007. tt_int_op(found_non_default_fallback, OP_EQ, 1);
  2008. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2009. int found_default_fallback = 0;
  2010. SMARTLIST_FOREACH(fallback_servers,
  2011. dir_server_t *,
  2012. ds,
  2013. /* increment the found counter if dir_port matches */
  2014. found_default_fallback +=
  2015. (ds->dir_port == 60099 ?
  2016. 1 : 0)
  2017. );
  2018. tt_int_op(found_default_fallback, OP_EQ, 0);
  2019. }
  2020. }
  2021. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  2022. AlternateDirAuthority Set, FallbackDir Not Set */
  2023. {
  2024. /* clear fallback dirs counter */
  2025. n_add_default_fallback_dir_servers_known_default = 0;
  2026. /* clear options*/
  2027. ads_clear_helper(options);
  2028. or_options_free(options);
  2029. options = options_new();
  2030. /* clear any previous dir servers:
  2031. consider_adding_dir_servers() should do this anyway */
  2032. clear_dir_servers();
  2033. /* assign options: 0110 */
  2034. options->DirAuthorities = NULL;
  2035. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2036. options->AlternateDirAuthority = test_alt_dir_authority;
  2037. options->FallbackDir = NULL;
  2038. options->UseDefaultFallbackDirs = 1;
  2039. /* parse options - ensure we always update by passing NULL old_options */
  2040. consider_adding_dir_servers(options, NULL);
  2041. /* check outcome */
  2042. /* we must not have added the default fallback dirs */
  2043. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  2044. /* we have more fallbacks than just the authorities */
  2045. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2046. {
  2047. /* trusted_dir_servers */
  2048. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2049. /* (No D0), B1, A2 */
  2050. tt_int_op(smartlist_len(dir_servers), OP_EQ, 2);
  2051. /* (No DirAuthority) - D0 - dir_port: 60090 */
  2052. int found_D0 = 0;
  2053. SMARTLIST_FOREACH(dir_servers,
  2054. dir_server_t *,
  2055. ds,
  2056. /* increment the found counter if dir_port matches */
  2057. found_D0 +=
  2058. (ds->dir_port == 60090 ?
  2059. 1 : 0)
  2060. );
  2061. tt_int_op(found_D0, OP_EQ, 0);
  2062. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2063. int found_B1 = 0;
  2064. SMARTLIST_FOREACH(dir_servers,
  2065. dir_server_t *,
  2066. ds,
  2067. /* increment the found counter if dir_port matches */
  2068. found_B1 +=
  2069. (ds->dir_port == 60091 ?
  2070. 1 : 0)
  2071. );
  2072. tt_int_op(found_B1, OP_EQ, 1);
  2073. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2074. int found_A2 = 0;
  2075. SMARTLIST_FOREACH(dir_servers,
  2076. dir_server_t *,
  2077. ds,
  2078. /* increment the found counter if dir_port matches */
  2079. found_A2 +=
  2080. (ds->dir_port == 60092 ?
  2081. 1 : 0)
  2082. );
  2083. tt_int_op(found_A2, OP_EQ, 1);
  2084. }
  2085. {
  2086. /* fallback_dir_servers */
  2087. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2088. /* (No D0), B1, A2, (No Fallback) */
  2089. tt_int_op(smartlist_len(fallback_servers), OP_EQ, 2);
  2090. /* (No DirAuthority) - D0 - dir_port: 60090 */
  2091. int found_D0 = 0;
  2092. SMARTLIST_FOREACH(fallback_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_int_op(found_D0, OP_EQ, 0);
  2101. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2102. int found_B1 = 0;
  2103. SMARTLIST_FOREACH(fallback_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_int_op(found_B1, OP_EQ, 1);
  2112. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2113. int found_A2 = 0;
  2114. SMARTLIST_FOREACH(fallback_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_int_op(found_A2, OP_EQ, 1);
  2123. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2124. int found_non_default_fallback = 0;
  2125. SMARTLIST_FOREACH(fallback_servers,
  2126. dir_server_t *,
  2127. ds,
  2128. /* increment the found counter if dir_port matches */
  2129. found_non_default_fallback +=
  2130. (ds->dir_port == 60093 ?
  2131. 1 : 0)
  2132. );
  2133. tt_int_op(found_non_default_fallback, OP_EQ, 0);
  2134. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2135. int found_default_fallback = 0;
  2136. SMARTLIST_FOREACH(fallback_servers,
  2137. dir_server_t *,
  2138. ds,
  2139. /* increment the found counter if dir_port matches */
  2140. found_default_fallback +=
  2141. (ds->dir_port == 60099 ?
  2142. 1 : 0)
  2143. );
  2144. tt_int_op(found_default_fallback, OP_EQ, 0);
  2145. }
  2146. }
  2147. /*
  2148. 2. Outcome: Use Set Bridge Authority
  2149. - Use Default Non-Bridge Directory Authorities
  2150. - Use FallbackDir if it is set, otherwise use default FallbackDir
  2151. Cases expected to yield this outcome:
  2152. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  2153. AlternateBridgeAuthority is set, and
  2154. AlternateDirAuthority is NULL)
  2155. */
  2156. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2157. AlternateDirAuthority Not Set, FallbackDir Set */
  2158. {
  2159. /* clear fallback dirs counter */
  2160. n_add_default_fallback_dir_servers_known_default = 0;
  2161. /* clear options*/
  2162. ads_clear_helper(options);
  2163. or_options_free(options);
  2164. options = options_new();
  2165. /* clear any previous dir servers:
  2166. consider_adding_dir_servers() should do this anyway */
  2167. clear_dir_servers();
  2168. /* assign options: 0101 */
  2169. options->DirAuthorities = NULL;
  2170. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2171. options->AlternateDirAuthority = NULL;
  2172. options->FallbackDir = test_fallback_directory;
  2173. options->UseDefaultFallbackDirs = 1;
  2174. /* parse options - ensure we always update by passing NULL old_options */
  2175. consider_adding_dir_servers(options, NULL);
  2176. /* check outcome */
  2177. /* we must not have added the default fallback dirs */
  2178. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  2179. /* we have more fallbacks than just the authorities */
  2180. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2181. {
  2182. /* trusted_dir_servers */
  2183. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2184. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2185. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2186. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2187. int found_D0 = 0;
  2188. SMARTLIST_FOREACH(dir_servers,
  2189. dir_server_t *,
  2190. ds,
  2191. /* increment the found counter if dir_port matches */
  2192. found_D0 +=
  2193. (ds->dir_port == 60090 ?
  2194. 1 : 0)
  2195. );
  2196. tt_int_op(found_D0, OP_EQ, 0);
  2197. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2198. int found_B1 = 0;
  2199. SMARTLIST_FOREACH(dir_servers,
  2200. dir_server_t *,
  2201. ds,
  2202. /* increment the found counter if dir_port matches */
  2203. found_B1 +=
  2204. (ds->dir_port == 60091 ?
  2205. 1 : 0)
  2206. );
  2207. tt_int_op(found_B1, OP_EQ, 1);
  2208. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2209. int found_A2 = 0;
  2210. SMARTLIST_FOREACH(dir_servers,
  2211. dir_server_t *,
  2212. ds,
  2213. /* increment the found counter if dir_port matches */
  2214. found_A2 +=
  2215. (ds->dir_port == 60092 ?
  2216. 1 : 0)
  2217. );
  2218. tt_int_op(found_A2, OP_EQ, 0);
  2219. /* There's no easy way of checking that we have included all the
  2220. * default v3 non-Bridge directory authorities, so let's assume that
  2221. * if the total count above is correct, we have the right ones.
  2222. */
  2223. }
  2224. {
  2225. /* fallback_dir_servers */
  2226. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2227. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2228. * Custom Fallback */
  2229. tt_assert(smartlist_len(fallback_servers) ==
  2230. 2 + n_default_alt_dir_authority);
  2231. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2232. int found_D0 = 0;
  2233. SMARTLIST_FOREACH(fallback_servers,
  2234. dir_server_t *,
  2235. ds,
  2236. /* increment the found counter if dir_port matches */
  2237. found_D0 +=
  2238. (ds->dir_port == 60090 ?
  2239. 1 : 0)
  2240. );
  2241. tt_int_op(found_D0, OP_EQ, 0);
  2242. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2243. int found_B1 = 0;
  2244. SMARTLIST_FOREACH(fallback_servers,
  2245. dir_server_t *,
  2246. ds,
  2247. /* increment the found counter if dir_port matches */
  2248. found_B1 +=
  2249. (ds->dir_port == 60091 ?
  2250. 1 : 0)
  2251. );
  2252. tt_int_op(found_B1, OP_EQ, 1);
  2253. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2254. int found_A2 = 0;
  2255. SMARTLIST_FOREACH(fallback_servers,
  2256. dir_server_t *,
  2257. ds,
  2258. /* increment the found counter if dir_port matches */
  2259. found_A2 +=
  2260. (ds->dir_port == 60092 ?
  2261. 1 : 0)
  2262. );
  2263. tt_int_op(found_A2, OP_EQ, 0);
  2264. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2265. int found_non_default_fallback = 0;
  2266. SMARTLIST_FOREACH(fallback_servers,
  2267. dir_server_t *,
  2268. ds,
  2269. /* increment the found counter if dir_port matches */
  2270. found_non_default_fallback +=
  2271. (ds->dir_port == 60093 ?
  2272. 1 : 0)
  2273. );
  2274. tt_int_op(found_non_default_fallback, OP_EQ, 1);
  2275. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2276. int found_default_fallback = 0;
  2277. SMARTLIST_FOREACH(fallback_servers,
  2278. dir_server_t *,
  2279. ds,
  2280. /* increment the found counter if dir_port matches */
  2281. found_default_fallback +=
  2282. (ds->dir_port == 60099 ?
  2283. 1 : 0)
  2284. );
  2285. tt_int_op(found_default_fallback, OP_EQ, 0);
  2286. /* There's no easy way of checking that we have included all the
  2287. * default v3 non-Bridge directory authorities, so let's assume that
  2288. * if the total count above is correct, we have the right ones.
  2289. */
  2290. }
  2291. }
  2292. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2293. AlternateDirAuthority & FallbackDir Not Set */
  2294. {
  2295. /* clear fallback dirs counter */
  2296. n_add_default_fallback_dir_servers_known_default = 0;
  2297. /* clear options*/
  2298. ads_clear_helper(options);
  2299. or_options_free(options);
  2300. options = options_new();
  2301. /* clear any previous dir servers:
  2302. consider_adding_dir_servers() should do this anyway */
  2303. clear_dir_servers();
  2304. /* assign options: 0100 */
  2305. options->DirAuthorities = NULL;
  2306. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2307. options->AlternateDirAuthority = NULL;
  2308. options->FallbackDir = NULL;
  2309. options->UseDefaultFallbackDirs = 1;
  2310. /* parse options - ensure we always update by passing NULL old_options */
  2311. consider_adding_dir_servers(options, NULL);
  2312. /* check outcome */
  2313. /* we must have added the default fallback dirs */
  2314. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
  2315. /* we have more fallbacks than just the authorities */
  2316. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2317. {
  2318. /* trusted_dir_servers */
  2319. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2320. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2321. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2322. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2323. int found_D0 = 0;
  2324. SMARTLIST_FOREACH(dir_servers,
  2325. dir_server_t *,
  2326. ds,
  2327. /* increment the found counter if dir_port matches */
  2328. found_D0 +=
  2329. (ds->dir_port == 60090 ?
  2330. 1 : 0)
  2331. );
  2332. tt_int_op(found_D0, OP_EQ, 0);
  2333. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2334. int found_B1 = 0;
  2335. SMARTLIST_FOREACH(dir_servers,
  2336. dir_server_t *,
  2337. ds,
  2338. /* increment the found counter if dir_port matches */
  2339. found_B1 +=
  2340. (ds->dir_port == 60091 ?
  2341. 1 : 0)
  2342. );
  2343. tt_int_op(found_B1, OP_EQ, 1);
  2344. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2345. int found_A2 = 0;
  2346. SMARTLIST_FOREACH(dir_servers,
  2347. dir_server_t *,
  2348. ds,
  2349. /* increment the found counter if dir_port matches */
  2350. found_A2 +=
  2351. (ds->dir_port == 60092 ?
  2352. 1 : 0)
  2353. );
  2354. tt_int_op(found_A2, OP_EQ, 0);
  2355. /* There's no easy way of checking that we have included all the
  2356. * default v3 non-Bridge directory authorities, so let's assume that
  2357. * if the total count above is correct, we have the right ones.
  2358. */
  2359. }
  2360. {
  2361. /* fallback_dir_servers */
  2362. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2363. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2364. * Default Fallback */
  2365. tt_assert(smartlist_len(fallback_servers) ==
  2366. 2 + n_default_alt_dir_authority);
  2367. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2368. int found_D0 = 0;
  2369. SMARTLIST_FOREACH(fallback_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_int_op(found_D0, OP_EQ, 0);
  2378. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2379. int found_B1 = 0;
  2380. SMARTLIST_FOREACH(fallback_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_int_op(found_B1, OP_EQ, 1);
  2389. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2390. int found_A2 = 0;
  2391. SMARTLIST_FOREACH(fallback_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_int_op(found_A2, OP_EQ, 0);
  2400. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2401. int found_non_default_fallback = 0;
  2402. SMARTLIST_FOREACH(fallback_servers,
  2403. dir_server_t *,
  2404. ds,
  2405. /* increment the found counter if dir_port matches */
  2406. found_non_default_fallback +=
  2407. (ds->dir_port == 60093 ?
  2408. 1 : 0)
  2409. );
  2410. tt_int_op(found_non_default_fallback, OP_EQ, 0);
  2411. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2412. int found_default_fallback = 0;
  2413. SMARTLIST_FOREACH(fallback_servers,
  2414. dir_server_t *,
  2415. ds,
  2416. /* increment the found counter if dir_port matches */
  2417. found_default_fallback +=
  2418. (ds->dir_port == 60099 ?
  2419. 1 : 0)
  2420. );
  2421. tt_int_op(found_default_fallback, OP_EQ, 1);
  2422. /* There's no easy way of checking that we have included all the
  2423. * default v3 non-Bridge directory authorities, so let's assume that
  2424. * if the total count above is correct, we have the right ones.
  2425. */
  2426. }
  2427. }
  2428. /*
  2429. 3. Outcome: Use Set Alternate Directory Authority
  2430. - Use Default Bridge Authorities
  2431. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2432. Cases expected to yield this outcome:
  2433. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2434. are both NULL, but AlternateDirAuthority is set)
  2435. */
  2436. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2437. AlternateDirAuthority & FallbackDir Set */
  2438. {
  2439. /* clear fallback dirs counter */
  2440. n_add_default_fallback_dir_servers_known_default = 0;
  2441. /* clear options*/
  2442. ads_clear_helper(options);
  2443. or_options_free(options);
  2444. options = options_new();
  2445. /* clear any previous dir servers:
  2446. consider_adding_dir_servers() should do this anyway */
  2447. clear_dir_servers();
  2448. /* assign options: 0011 */
  2449. options->DirAuthorities = NULL;
  2450. options->AlternateBridgeAuthority = NULL;
  2451. options->AlternateDirAuthority = test_alt_dir_authority;
  2452. options->FallbackDir = test_fallback_directory;
  2453. options->UseDefaultFallbackDirs = 1;
  2454. /* parse options - ensure we always update by passing NULL old_options */
  2455. consider_adding_dir_servers(options, NULL);
  2456. /* check outcome */
  2457. /* we must not have added the default fallback dirs */
  2458. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  2459. /* we have more fallbacks than just the authorities */
  2460. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2461. {
  2462. /* trusted_dir_servers */
  2463. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2464. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2465. tt_assert(smartlist_len(dir_servers) ==
  2466. 1 + n_default_alt_bridge_authority);
  2467. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2468. int found_D0 = 0;
  2469. SMARTLIST_FOREACH(dir_servers,
  2470. dir_server_t *,
  2471. ds,
  2472. /* increment the found counter if dir_port matches */
  2473. found_D0 +=
  2474. (ds->dir_port == 60090 ?
  2475. 1 : 0)
  2476. );
  2477. tt_int_op(found_D0, OP_EQ, 0);
  2478. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2479. int found_B1 = 0;
  2480. SMARTLIST_FOREACH(dir_servers,
  2481. dir_server_t *,
  2482. ds,
  2483. /* increment the found counter if dir_port matches */
  2484. found_B1 +=
  2485. (ds->dir_port == 60091 ?
  2486. 1 : 0)
  2487. );
  2488. tt_int_op(found_B1, OP_EQ, 0);
  2489. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2490. int found_A2 = 0;
  2491. SMARTLIST_FOREACH(dir_servers,
  2492. dir_server_t *,
  2493. ds,
  2494. /* increment the found counter if dir_port matches */
  2495. found_A2 +=
  2496. (ds->dir_port == 60092 ?
  2497. 1 : 0)
  2498. );
  2499. tt_int_op(found_A2, OP_EQ, 1);
  2500. /* There's no easy way of checking that we have included all the
  2501. * default Bridge authorities (except for hard-coding tonga's details),
  2502. * so let's assume that if the total count above is correct,
  2503. * we have the right ones.
  2504. */
  2505. }
  2506. {
  2507. /* fallback_dir_servers */
  2508. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2509. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2510. * Custom Fallback Directory, (No Default Fallback Directories) */
  2511. tt_assert(smartlist_len(fallback_servers) ==
  2512. 2 + n_default_alt_bridge_authority);
  2513. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2514. int found_D0 = 0;
  2515. SMARTLIST_FOREACH(fallback_servers,
  2516. dir_server_t *,
  2517. ds,
  2518. /* increment the found counter if dir_port matches */
  2519. found_D0 +=
  2520. (ds->dir_port == 60090 ?
  2521. 1 : 0)
  2522. );
  2523. tt_int_op(found_D0, OP_EQ, 0);
  2524. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2525. int found_B1 = 0;
  2526. SMARTLIST_FOREACH(fallback_servers,
  2527. dir_server_t *,
  2528. ds,
  2529. /* increment the found counter if dir_port matches */
  2530. found_B1 +=
  2531. (ds->dir_port == 60091 ?
  2532. 1 : 0)
  2533. );
  2534. tt_int_op(found_B1, OP_EQ, 0);
  2535. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2536. int found_A2 = 0;
  2537. SMARTLIST_FOREACH(fallback_servers,
  2538. dir_server_t *,
  2539. ds,
  2540. /* increment the found counter if dir_port matches */
  2541. found_A2 +=
  2542. (ds->dir_port == 60092 ?
  2543. 1 : 0)
  2544. );
  2545. tt_int_op(found_A2, OP_EQ, 1);
  2546. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2547. int found_non_default_fallback = 0;
  2548. SMARTLIST_FOREACH(fallback_servers,
  2549. dir_server_t *,
  2550. ds,
  2551. /* increment the found counter if dir_port matches */
  2552. found_non_default_fallback +=
  2553. (ds->dir_port == 60093 ?
  2554. 1 : 0)
  2555. );
  2556. tt_int_op(found_non_default_fallback, OP_EQ, 1);
  2557. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2558. int found_default_fallback = 0;
  2559. SMARTLIST_FOREACH(fallback_servers,
  2560. dir_server_t *,
  2561. ds,
  2562. /* increment the found counter if dir_port matches */
  2563. found_default_fallback +=
  2564. (ds->dir_port == 60099 ?
  2565. 1 : 0)
  2566. );
  2567. tt_int_op(found_default_fallback, OP_EQ, 0);
  2568. /* There's no easy way of checking that we have included all the
  2569. * default Bridge authorities (except for hard-coding tonga's details),
  2570. * so let's assume that if the total count above is correct,
  2571. * we have the right ones.
  2572. */
  2573. }
  2574. }
  2575. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2576. AlternateDirAuthority Set, FallbackDir Not Set */
  2577. {
  2578. /* clear fallback dirs counter */
  2579. n_add_default_fallback_dir_servers_known_default = 0;
  2580. /* clear options*/
  2581. ads_clear_helper(options);
  2582. or_options_free(options);
  2583. options = options_new();
  2584. /* clear any previous dir servers:
  2585. consider_adding_dir_servers() should do this anyway */
  2586. clear_dir_servers();
  2587. /* assign options: 0010 */
  2588. options->DirAuthorities = NULL;
  2589. options->AlternateBridgeAuthority = NULL;
  2590. options->AlternateDirAuthority = test_alt_dir_authority;
  2591. options->FallbackDir = NULL;
  2592. options->UseDefaultFallbackDirs = 1;
  2593. /* parse options - ensure we always update by passing NULL old_options */
  2594. consider_adding_dir_servers(options, NULL);
  2595. /* check outcome */
  2596. /* we must not have added the default fallback dirs */
  2597. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  2598. /* we just have the authorities */
  2599. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 0);
  2600. {
  2601. /* trusted_dir_servers */
  2602. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2603. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2604. * No Default or Custom Fallback Directories */
  2605. tt_assert(smartlist_len(dir_servers) ==
  2606. 1 + n_default_alt_bridge_authority);
  2607. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2608. int found_D0 = 0;
  2609. SMARTLIST_FOREACH(dir_servers,
  2610. dir_server_t *,
  2611. ds,
  2612. /* increment the found counter if dir_port matches */
  2613. found_D0 +=
  2614. (ds->dir_port == 60090 ?
  2615. 1 : 0)
  2616. );
  2617. tt_int_op(found_D0, OP_EQ, 0);
  2618. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2619. int found_B1 = 0;
  2620. SMARTLIST_FOREACH(dir_servers,
  2621. dir_server_t *,
  2622. ds,
  2623. /* increment the found counter if dir_port matches */
  2624. found_B1 +=
  2625. (ds->dir_port == 60091 ?
  2626. 1 : 0)
  2627. );
  2628. tt_int_op(found_B1, OP_EQ, 0);
  2629. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2630. int found_A2 = 0;
  2631. SMARTLIST_FOREACH(dir_servers,
  2632. dir_server_t *,
  2633. ds,
  2634. /* increment the found counter if dir_port matches */
  2635. found_A2 +=
  2636. (ds->dir_port == 60092 ?
  2637. 1 : 0)
  2638. );
  2639. tt_int_op(found_A2, OP_EQ, 1);
  2640. /* There's no easy way of checking that we have included all the
  2641. * default Bridge authorities (except for hard-coding tonga's details),
  2642. * so let's assume that if the total count above is correct,
  2643. * we have the right ones.
  2644. */
  2645. }
  2646. {
  2647. /* fallback_dir_servers */
  2648. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2649. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2650. * No Custom or Default Fallback Directories */
  2651. tt_assert(smartlist_len(fallback_servers) ==
  2652. 1 + n_default_alt_bridge_authority);
  2653. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2654. int found_D0 = 0;
  2655. SMARTLIST_FOREACH(fallback_servers,
  2656. dir_server_t *,
  2657. ds,
  2658. /* increment the found counter if dir_port matches */
  2659. found_D0 +=
  2660. (ds->dir_port == 60090 ?
  2661. 1 : 0)
  2662. );
  2663. tt_int_op(found_D0, OP_EQ, 0);
  2664. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2665. int found_B1 = 0;
  2666. SMARTLIST_FOREACH(fallback_servers,
  2667. dir_server_t *,
  2668. ds,
  2669. /* increment the found counter if dir_port matches */
  2670. found_B1 +=
  2671. (ds->dir_port == 60091 ?
  2672. 1 : 0)
  2673. );
  2674. tt_int_op(found_B1, OP_EQ, 0);
  2675. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2676. int found_A2 = 0;
  2677. SMARTLIST_FOREACH(fallback_servers,
  2678. dir_server_t *,
  2679. ds,
  2680. /* increment the found counter if dir_port matches */
  2681. found_A2 +=
  2682. (ds->dir_port == 60092 ?
  2683. 1 : 0)
  2684. );
  2685. tt_int_op(found_A2, OP_EQ, 1);
  2686. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2687. int found_non_default_fallback = 0;
  2688. SMARTLIST_FOREACH(fallback_servers,
  2689. dir_server_t *,
  2690. ds,
  2691. /* increment the found counter if dir_port matches */
  2692. found_non_default_fallback +=
  2693. (ds->dir_port == 60093 ?
  2694. 1 : 0)
  2695. );
  2696. tt_int_op(found_non_default_fallback, OP_EQ, 0);
  2697. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2698. int found_default_fallback = 0;
  2699. SMARTLIST_FOREACH(fallback_servers,
  2700. dir_server_t *,
  2701. ds,
  2702. /* increment the found counter if dir_port matches */
  2703. found_default_fallback +=
  2704. (ds->dir_port == 60099 ?
  2705. 1 : 0)
  2706. );
  2707. tt_int_op(found_default_fallback, OP_EQ, 0);
  2708. /* There's no easy way of checking that we have included all the
  2709. * default Bridge authorities (except for hard-coding tonga's details),
  2710. * so let's assume that if the total count above is correct,
  2711. * we have the right ones.
  2712. */
  2713. }
  2714. }
  2715. /*
  2716. 4. Outcome: Use Set Custom Fallback Directory
  2717. - Use Default Bridge & Directory Authorities
  2718. Cases expected to yield this outcome:
  2719. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2720. are all NULL, but FallbackDir is set)
  2721. */
  2722. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2723. & AlternateDirAuthority Not Set, FallbackDir Set */
  2724. {
  2725. /* clear fallback dirs counter */
  2726. n_add_default_fallback_dir_servers_known_default = 0;
  2727. /* clear options*/
  2728. ads_clear_helper(options);
  2729. or_options_free(options);
  2730. options = options_new();
  2731. /* clear any previous dir servers:
  2732. consider_adding_dir_servers() should do this anyway */
  2733. clear_dir_servers();
  2734. /* assign options: 0001 */
  2735. options->DirAuthorities = NULL;
  2736. options->AlternateBridgeAuthority = NULL;
  2737. options->AlternateDirAuthority = NULL;
  2738. options->FallbackDir = test_fallback_directory;
  2739. options->UseDefaultFallbackDirs = 1;
  2740. /* parse options - ensure we always update by passing NULL old_options */
  2741. consider_adding_dir_servers(options, NULL);
  2742. /* check outcome */
  2743. /* we must not have added the default fallback dirs */
  2744. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 0);
  2745. /* we have more fallbacks than just the authorities */
  2746. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2747. {
  2748. /* trusted_dir_servers */
  2749. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2750. /* (No D0), (No B1), Default Bridge Authorities,
  2751. * (No A2), Default v3 Directory Authorities */
  2752. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2753. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2754. int found_D0 = 0;
  2755. SMARTLIST_FOREACH(dir_servers,
  2756. dir_server_t *,
  2757. ds,
  2758. /* increment the found counter if dir_port matches */
  2759. found_D0 +=
  2760. (ds->dir_port == 60090 ?
  2761. 1 : 0)
  2762. );
  2763. tt_int_op(found_D0, OP_EQ, 0);
  2764. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2765. int found_B1 = 0;
  2766. SMARTLIST_FOREACH(dir_servers,
  2767. dir_server_t *,
  2768. ds,
  2769. /* increment the found counter if dir_port matches */
  2770. found_B1 +=
  2771. (ds->dir_port == 60091 ?
  2772. 1 : 0)
  2773. );
  2774. tt_int_op(found_B1, OP_EQ, 0);
  2775. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2776. int found_A2 = 0;
  2777. SMARTLIST_FOREACH(dir_servers,
  2778. dir_server_t *,
  2779. ds,
  2780. /* increment the found counter if dir_port matches */
  2781. found_A2 +=
  2782. (ds->dir_port == 60092 ?
  2783. 1 : 0)
  2784. );
  2785. tt_int_op(found_A2, OP_EQ, 0);
  2786. /* There's no easy way of checking that we have included all the
  2787. * default Bridge & V3 Directory authorities, so let's assume that
  2788. * if the total count above is correct, we have the right ones.
  2789. */
  2790. }
  2791. {
  2792. /* fallback_dir_servers */
  2793. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2794. /* (No D0), (No B1), Default Bridge Authorities,
  2795. * (No A2), Default v3 Directory Authorities,
  2796. * Custom Fallback Directory, (No Default Fallback Directories) */
  2797. tt_assert(smartlist_len(fallback_servers) ==
  2798. 1 + n_default_authorities);
  2799. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2800. int found_D0 = 0;
  2801. SMARTLIST_FOREACH(fallback_servers,
  2802. dir_server_t *,
  2803. ds,
  2804. /* increment the found counter if dir_port matches */
  2805. found_D0 +=
  2806. (ds->dir_port == 60090 ?
  2807. 1 : 0)
  2808. );
  2809. tt_int_op(found_D0, OP_EQ, 0);
  2810. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2811. int found_B1 = 0;
  2812. SMARTLIST_FOREACH(fallback_servers,
  2813. dir_server_t *,
  2814. ds,
  2815. /* increment the found counter if dir_port matches */
  2816. found_B1 +=
  2817. (ds->dir_port == 60091 ?
  2818. 1 : 0)
  2819. );
  2820. tt_int_op(found_B1, OP_EQ, 0);
  2821. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2822. int found_A2 = 0;
  2823. SMARTLIST_FOREACH(fallback_servers,
  2824. dir_server_t *,
  2825. ds,
  2826. /* increment the found counter if dir_port matches */
  2827. found_A2 +=
  2828. (ds->dir_port == 60092 ?
  2829. 1 : 0)
  2830. );
  2831. tt_int_op(found_A2, OP_EQ, 0);
  2832. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2833. int found_non_default_fallback = 0;
  2834. SMARTLIST_FOREACH(fallback_servers,
  2835. dir_server_t *,
  2836. ds,
  2837. /* increment the found counter if dir_port matches */
  2838. found_non_default_fallback +=
  2839. (ds->dir_port == 60093 ?
  2840. 1 : 0)
  2841. );
  2842. tt_int_op(found_non_default_fallback, OP_EQ, 1);
  2843. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2844. int found_default_fallback = 0;
  2845. SMARTLIST_FOREACH(fallback_servers,
  2846. dir_server_t *,
  2847. ds,
  2848. /* increment the found counter if dir_port matches */
  2849. found_default_fallback +=
  2850. (ds->dir_port == 60099 ?
  2851. 1 : 0)
  2852. );
  2853. tt_int_op(found_default_fallback, OP_EQ, 0);
  2854. /* There's no easy way of checking that we have included all the
  2855. * default Bridge & V3 Directory authorities, so let's assume that
  2856. * if the total count above is correct, we have the right ones.
  2857. */
  2858. }
  2859. }
  2860. /*
  2861. 5. Outcome: Use All Defaults
  2862. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2863. Cases expected to yield this outcome:
  2864. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2865. and FallbackDir are all NULL)
  2866. */
  2867. /* Case 0: 0000 - All Not Set */
  2868. {
  2869. /* clear fallback dirs counter */
  2870. n_add_default_fallback_dir_servers_known_default = 0;
  2871. /* clear options*/
  2872. ads_clear_helper(options);
  2873. or_options_free(options);
  2874. options = options_new();
  2875. /* clear any previous dir servers:
  2876. consider_adding_dir_servers() should do this anyway */
  2877. clear_dir_servers();
  2878. /* assign options: 0001 */
  2879. options->DirAuthorities = NULL;
  2880. options->AlternateBridgeAuthority = NULL;
  2881. options->AlternateDirAuthority = NULL;
  2882. options->FallbackDir = NULL;
  2883. options->UseDefaultFallbackDirs = 1;
  2884. /* parse options - ensure we always update by passing NULL old_options */
  2885. consider_adding_dir_servers(options, NULL);
  2886. /* check outcome */
  2887. /* we must have added the default fallback dirs */
  2888. tt_int_op(n_add_default_fallback_dir_servers_known_default, OP_EQ, 1);
  2889. /* we have more fallbacks than just the authorities */
  2890. tt_assert(networkstatus_consensus_can_use_extra_fallbacks(options) == 1);
  2891. {
  2892. /* trusted_dir_servers */
  2893. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2894. /* (No D0), (No B1), Default Bridge Authorities,
  2895. * (No A2), Default v3 Directory Authorities */
  2896. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2897. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2898. int found_D0 = 0;
  2899. SMARTLIST_FOREACH(dir_servers,
  2900. dir_server_t *,
  2901. ds,
  2902. /* increment the found counter if dir_port matches */
  2903. found_D0 +=
  2904. (ds->dir_port == 60090 ?
  2905. 1 : 0)
  2906. );
  2907. tt_int_op(found_D0, OP_EQ, 0);
  2908. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2909. int found_B1 = 0;
  2910. SMARTLIST_FOREACH(dir_servers,
  2911. dir_server_t *,
  2912. ds,
  2913. /* increment the found counter if dir_port matches */
  2914. found_B1 +=
  2915. (ds->dir_port == 60091 ?
  2916. 1 : 0)
  2917. );
  2918. tt_int_op(found_B1, OP_EQ, 0);
  2919. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2920. int found_A2 = 0;
  2921. SMARTLIST_FOREACH(dir_servers,
  2922. dir_server_t *,
  2923. ds,
  2924. /* increment the found counter if dir_port matches */
  2925. found_A2 +=
  2926. (ds->dir_port == 60092 ?
  2927. 1 : 0)
  2928. );
  2929. tt_int_op(found_A2, OP_EQ, 0);
  2930. /* There's no easy way of checking that we have included all the
  2931. * default Bridge & V3 Directory authorities, so let's assume that
  2932. * if the total count above is correct, we have the right ones.
  2933. */
  2934. }
  2935. {
  2936. /* fallback_dir_servers */
  2937. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2938. /* (No D0), (No B1), Default Bridge Authorities,
  2939. * (No A2), Default v3 Directory Authorities,
  2940. * (No Custom Fallback Directory), Default Fallback Directories */
  2941. tt_assert(smartlist_len(fallback_servers) ==
  2942. n_default_authorities + n_default_fallback_dir);
  2943. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2944. int found_D0 = 0;
  2945. SMARTLIST_FOREACH(fallback_servers,
  2946. dir_server_t *,
  2947. ds,
  2948. /* increment the found counter if dir_port matches */
  2949. found_D0 +=
  2950. (ds->dir_port == 60090 ?
  2951. 1 : 0)
  2952. );
  2953. tt_int_op(found_D0, OP_EQ, 0);
  2954. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2955. int found_B1 = 0;
  2956. SMARTLIST_FOREACH(fallback_servers,
  2957. dir_server_t *,
  2958. ds,
  2959. /* increment the found counter if dir_port matches */
  2960. found_B1 +=
  2961. (ds->dir_port == 60091 ?
  2962. 1 : 0)
  2963. );
  2964. tt_int_op(found_B1, OP_EQ, 0);
  2965. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2966. int found_A2 = 0;
  2967. SMARTLIST_FOREACH(fallback_servers,
  2968. dir_server_t *,
  2969. ds,
  2970. /* increment the found counter if dir_port matches */
  2971. found_A2 +=
  2972. (ds->dir_port == 60092 ?
  2973. 1 : 0)
  2974. );
  2975. tt_int_op(found_A2, OP_EQ, 0);
  2976. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2977. int found_non_default_fallback = 0;
  2978. SMARTLIST_FOREACH(fallback_servers,
  2979. dir_server_t *,
  2980. ds,
  2981. /* increment the found counter if dir_port matches */
  2982. found_non_default_fallback +=
  2983. (ds->dir_port == 60093 ?
  2984. 1 : 0)
  2985. );
  2986. tt_int_op(found_non_default_fallback, OP_EQ, 0);
  2987. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2988. int found_default_fallback = 0;
  2989. SMARTLIST_FOREACH(fallback_servers,
  2990. dir_server_t *,
  2991. ds,
  2992. /* increment the found counter if dir_port matches */
  2993. found_default_fallback +=
  2994. (ds->dir_port == 60099 ?
  2995. 1 : 0)
  2996. );
  2997. tt_int_op(found_default_fallback, OP_EQ, 1);
  2998. /* There's no easy way of checking that we have included all the
  2999. * default Bridge & V3 Directory authorities, and the default
  3000. * Fallback Directories, so let's assume that if the total count
  3001. * above is correct, we have the right ones.
  3002. */
  3003. }
  3004. }
  3005. done:
  3006. clear_dir_servers();
  3007. tor_free(test_dir_authority->key);
  3008. tor_free(test_dir_authority->value);
  3009. tor_free(test_dir_authority);
  3010. tor_free(test_alt_dir_authority->key);
  3011. tor_free(test_alt_dir_authority->value);
  3012. tor_free(test_alt_dir_authority);
  3013. tor_free(test_alt_bridge_authority->key);
  3014. tor_free(test_alt_bridge_authority->value);
  3015. tor_free(test_alt_bridge_authority);
  3016. tor_free(test_fallback_directory->key);
  3017. tor_free(test_fallback_directory->value);
  3018. tor_free(test_fallback_directory);
  3019. ads_clear_helper(options);
  3020. or_options_free(options);
  3021. UNMOCK(add_default_fallback_dir_servers);
  3022. }
  3023. static void
  3024. test_config_default_dir_servers(void *arg)
  3025. {
  3026. or_options_t *opts = NULL;
  3027. (void)arg;
  3028. int trusted_count = 0;
  3029. int fallback_count = 0;
  3030. /* new set of options should stop fallback parsing */
  3031. opts = options_new();
  3032. opts->UseDefaultFallbackDirs = 0;
  3033. /* set old_options to NULL to force dir update */
  3034. consider_adding_dir_servers(opts, NULL);
  3035. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  3036. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  3037. or_options_free(opts);
  3038. opts = NULL;
  3039. /* assume a release will never go out with less than 7 authorities */
  3040. tt_int_op(trusted_count, OP_GE, 7);
  3041. /* if we disable the default fallbacks, there must not be any extra */
  3042. tt_assert(fallback_count == trusted_count);
  3043. opts = options_new();
  3044. opts->UseDefaultFallbackDirs = 1;
  3045. consider_adding_dir_servers(opts, opts);
  3046. trusted_count = smartlist_len(router_get_trusted_dir_servers());
  3047. fallback_count = smartlist_len(router_get_fallback_dir_servers());
  3048. or_options_free(opts);
  3049. opts = NULL;
  3050. /* assume a release will never go out with less than 7 authorities */
  3051. tt_int_op(trusted_count, OP_GE, 7);
  3052. /* XX/teor - allow for default fallbacks to be added without breaking
  3053. * the unit tests. Set a minimum fallback count once the list is stable. */
  3054. tt_assert(fallback_count >= trusted_count);
  3055. done:
  3056. or_options_free(opts);
  3057. }
  3058. static int mock_router_pick_published_address_result = 0;
  3059. static int
  3060. mock_router_pick_published_address(const or_options_t *options,
  3061. uint32_t *addr, int cache_only)
  3062. {
  3063. (void)options;
  3064. (void)addr;
  3065. (void)cache_only;
  3066. return mock_router_pick_published_address_result;
  3067. }
  3068. static int mock_router_my_exit_policy_is_reject_star_result = 0;
  3069. static int
  3070. mock_router_my_exit_policy_is_reject_star(void)
  3071. {
  3072. return mock_router_my_exit_policy_is_reject_star_result;
  3073. }
  3074. static int mock_advertised_server_mode_result = 0;
  3075. static int
  3076. mock_advertised_server_mode(void)
  3077. {
  3078. return mock_advertised_server_mode_result;
  3079. }
  3080. static routerinfo_t *mock_router_get_my_routerinfo_result = NULL;
  3081. static const routerinfo_t *
  3082. mock_router_get_my_routerinfo(void)
  3083. {
  3084. return mock_router_get_my_routerinfo_result;
  3085. }
  3086. static void
  3087. test_config_directory_fetch(void *arg)
  3088. {
  3089. (void)arg;
  3090. /* Test Setup */
  3091. or_options_t *options = options_new();
  3092. routerinfo_t routerinfo;
  3093. memset(&routerinfo, 0, sizeof(routerinfo));
  3094. mock_router_pick_published_address_result = -1;
  3095. mock_router_my_exit_policy_is_reject_star_result = 1;
  3096. mock_advertised_server_mode_result = 0;
  3097. mock_router_get_my_routerinfo_result = NULL;
  3098. MOCK(router_pick_published_address, mock_router_pick_published_address);
  3099. MOCK(router_my_exit_policy_is_reject_star,
  3100. mock_router_my_exit_policy_is_reject_star);
  3101. MOCK(advertised_server_mode, mock_advertised_server_mode);
  3102. MOCK(router_get_my_routerinfo, mock_router_get_my_routerinfo);
  3103. or_options_free(options);
  3104. options = options_new();
  3105. /* Clients can use multiple directory mirrors for bootstrap */
  3106. options->ClientOnly = 1;
  3107. tt_assert(server_mode(options) == 0);
  3108. tt_assert(public_server_mode(options) == 0);
  3109. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3110. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3111. OP_EQ, 1);
  3112. /* Bridge Clients can use multiple directory mirrors for bootstrap */
  3113. or_options_free(options);
  3114. options = options_new();
  3115. options->UseBridges = 1;
  3116. tt_assert(server_mode(options) == 0);
  3117. tt_assert(public_server_mode(options) == 0);
  3118. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3119. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3120. OP_EQ, 1);
  3121. /* Bridge Relays (Bridges) must act like clients, and use multiple
  3122. * directory mirrors for bootstrap */
  3123. or_options_free(options);
  3124. options = options_new();
  3125. options->BridgeRelay = 1;
  3126. options->ORPort_set = 1;
  3127. tt_assert(server_mode(options) == 1);
  3128. tt_assert(public_server_mode(options) == 0);
  3129. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3130. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3131. OP_EQ, 1);
  3132. /* Clients set to FetchDirInfoEarly must fetch it from the authorities,
  3133. * but can use multiple authorities for bootstrap */
  3134. or_options_free(options);
  3135. options = options_new();
  3136. options->FetchDirInfoEarly = 1;
  3137. tt_assert(server_mode(options) == 0);
  3138. tt_assert(public_server_mode(options) == 0);
  3139. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
  3140. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3141. OP_EQ, 1);
  3142. /* OR servers only fetch the consensus from the authorities when they don't
  3143. * know their own address, but never use multiple directories for bootstrap
  3144. */
  3145. or_options_free(options);
  3146. options = options_new();
  3147. options->ORPort_set = 1;
  3148. mock_router_pick_published_address_result = -1;
  3149. tt_assert(server_mode(options) == 1);
  3150. tt_assert(public_server_mode(options) == 1);
  3151. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
  3152. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3153. OP_EQ, 0);
  3154. mock_router_pick_published_address_result = 0;
  3155. tt_assert(server_mode(options) == 1);
  3156. tt_assert(public_server_mode(options) == 1);
  3157. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3158. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3159. OP_EQ, 0);
  3160. /* Exit OR servers only fetch the consensus from the authorities when they
  3161. * refuse unknown exits, but never use multiple directories for bootstrap
  3162. */
  3163. or_options_free(options);
  3164. options = options_new();
  3165. options->ORPort_set = 1;
  3166. options->ExitRelay = 1;
  3167. mock_router_pick_published_address_result = 0;
  3168. mock_router_my_exit_policy_is_reject_star_result = 0;
  3169. mock_advertised_server_mode_result = 1;
  3170. mock_router_get_my_routerinfo_result = &routerinfo;
  3171. routerinfo.supports_tunnelled_dir_requests = 1;
  3172. options->RefuseUnknownExits = 1;
  3173. tt_assert(server_mode(options) == 1);
  3174. tt_assert(public_server_mode(options) == 1);
  3175. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
  3176. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3177. OP_EQ, 0);
  3178. options->RefuseUnknownExits = 0;
  3179. mock_router_pick_published_address_result = 0;
  3180. tt_assert(server_mode(options) == 1);
  3181. tt_assert(public_server_mode(options) == 1);
  3182. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3183. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3184. OP_EQ, 0);
  3185. /* Dir servers fetch the consensus from the authorities, unless they are not
  3186. * advertising themselves (hibernating) or have no routerinfo or are not
  3187. * advertising their dirport, and never use multiple directories for
  3188. * bootstrap. This only applies if they are also OR servers.
  3189. * (We don't care much about the behaviour of non-OR directory servers.) */
  3190. or_options_free(options);
  3191. options = options_new();
  3192. options->DirPort_set = 1;
  3193. options->ORPort_set = 1;
  3194. options->DirCache = 1;
  3195. mock_router_pick_published_address_result = 0;
  3196. mock_router_my_exit_policy_is_reject_star_result = 1;
  3197. mock_advertised_server_mode_result = 1;
  3198. routerinfo.dir_port = 1;
  3199. mock_router_get_my_routerinfo_result = &routerinfo;
  3200. tt_assert(server_mode(options) == 1);
  3201. tt_assert(public_server_mode(options) == 1);
  3202. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
  3203. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3204. OP_EQ, 0);
  3205. mock_advertised_server_mode_result = 0;
  3206. routerinfo.dir_port = 1;
  3207. mock_router_get_my_routerinfo_result = &routerinfo;
  3208. tt_assert(server_mode(options) == 1);
  3209. tt_assert(public_server_mode(options) == 1);
  3210. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3211. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3212. OP_EQ, 0);
  3213. mock_advertised_server_mode_result = 1;
  3214. mock_router_get_my_routerinfo_result = NULL;
  3215. tt_assert(server_mode(options) == 1);
  3216. tt_assert(public_server_mode(options) == 1);
  3217. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3218. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3219. OP_EQ, 0);
  3220. mock_advertised_server_mode_result = 1;
  3221. routerinfo.dir_port = 0;
  3222. routerinfo.supports_tunnelled_dir_requests = 0;
  3223. mock_router_get_my_routerinfo_result = &routerinfo;
  3224. tt_assert(server_mode(options) == 1);
  3225. tt_assert(public_server_mode(options) == 1);
  3226. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 0);
  3227. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3228. OP_EQ, 0);
  3229. mock_advertised_server_mode_result = 1;
  3230. routerinfo.dir_port = 1;
  3231. routerinfo.supports_tunnelled_dir_requests = 1;
  3232. mock_router_get_my_routerinfo_result = &routerinfo;
  3233. tt_assert(server_mode(options) == 1);
  3234. tt_assert(public_server_mode(options) == 1);
  3235. tt_int_op(directory_fetches_from_authorities(options), OP_EQ, 1);
  3236. tt_int_op(networkstatus_consensus_can_use_multiple_directories(options),
  3237. OP_EQ, 0);
  3238. done:
  3239. or_options_free(options);
  3240. UNMOCK(router_pick_published_address);
  3241. UNMOCK(router_get_my_routerinfo);
  3242. UNMOCK(advertised_server_mode);
  3243. UNMOCK(router_my_exit_policy_is_reject_star);
  3244. }
  3245. static void
  3246. test_config_default_fallback_dirs(void *arg)
  3247. {
  3248. const char *fallback[] = {
  3249. #include "app/config/fallback_dirs.inc"
  3250. NULL
  3251. };
  3252. int n_included_fallback_dirs = 0;
  3253. int n_added_fallback_dirs = 0;
  3254. (void)arg;
  3255. clear_dir_servers();
  3256. while (fallback[n_included_fallback_dirs])
  3257. n_included_fallback_dirs++;
  3258. add_default_fallback_dir_servers();
  3259. n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
  3260. tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
  3261. done:
  3262. clear_dir_servers();
  3263. }
  3264. static void
  3265. test_config_port_cfg_line_extract_addrport(void *arg)
  3266. {
  3267. (void)arg;
  3268. int unixy = 0;
  3269. const char *rest = NULL;
  3270. char *a = NULL;
  3271. tt_int_op(port_cfg_line_extract_addrport("", &a, &unixy, &rest), OP_EQ, 0);
  3272. tt_int_op(unixy, OP_EQ, 0);
  3273. tt_str_op(a, OP_EQ, "");
  3274. tt_str_op(rest, OP_EQ, "");
  3275. tor_free(a);
  3276. tt_int_op(port_cfg_line_extract_addrport("hello", &a, &unixy, &rest),
  3277. OP_EQ, 0);
  3278. tt_int_op(unixy, OP_EQ, 0);
  3279. tt_str_op(a, OP_EQ, "hello");
  3280. tt_str_op(rest, OP_EQ, "");
  3281. tor_free(a);
  3282. tt_int_op(port_cfg_line_extract_addrport(" flipperwalt gersplut",
  3283. &a, &unixy, &rest), OP_EQ, 0);
  3284. tt_int_op(unixy, OP_EQ, 0);
  3285. tt_str_op(a, OP_EQ, "flipperwalt");
  3286. tt_str_op(rest, OP_EQ, "gersplut");
  3287. tor_free(a);
  3288. tt_int_op(port_cfg_line_extract_addrport(" flipperwalt \t gersplut",
  3289. &a, &unixy, &rest), OP_EQ, 0);
  3290. tt_int_op(unixy, OP_EQ, 0);
  3291. tt_str_op(a, OP_EQ, "flipperwalt");
  3292. tt_str_op(rest, OP_EQ, "gersplut");
  3293. tor_free(a);
  3294. tt_int_op(port_cfg_line_extract_addrport("flipperwalt \t gersplut",
  3295. &a, &unixy, &rest), OP_EQ, 0);
  3296. tt_int_op(unixy, OP_EQ, 0);
  3297. tt_str_op(a, OP_EQ, "flipperwalt");
  3298. tt_str_op(rest, OP_EQ, "gersplut");
  3299. tor_free(a);
  3300. tt_int_op(port_cfg_line_extract_addrport("unix:flipperwalt \t gersplut",
  3301. &a, &unixy, &rest), OP_EQ, 0);
  3302. tt_int_op(unixy, OP_EQ, 1);
  3303. tt_str_op(a, OP_EQ, "flipperwalt");
  3304. tt_str_op(rest, OP_EQ, "gersplut");
  3305. tor_free(a);
  3306. tt_int_op(port_cfg_line_extract_addrport("lolol",
  3307. &a, &unixy, &rest), OP_EQ, 0);
  3308. tt_int_op(unixy, OP_EQ, 0);
  3309. tt_str_op(a, OP_EQ, "lolol");
  3310. tt_str_op(rest, OP_EQ, "");
  3311. tor_free(a);
  3312. tt_int_op(port_cfg_line_extract_addrport("unix:lolol",
  3313. &a, &unixy, &rest), OP_EQ, 0);
  3314. tt_int_op(unixy, OP_EQ, 1);
  3315. tt_str_op(a, OP_EQ, "lolol");
  3316. tt_str_op(rest, OP_EQ, "");
  3317. tor_free(a);
  3318. tt_int_op(port_cfg_line_extract_addrport("unix:lolol ",
  3319. &a, &unixy, &rest), OP_EQ, 0);
  3320. tt_int_op(unixy, OP_EQ, 1);
  3321. tt_str_op(a, OP_EQ, "lolol");
  3322. tt_str_op(rest, OP_EQ, "");
  3323. tor_free(a);
  3324. tt_int_op(port_cfg_line_extract_addrport(" unix:lolol",
  3325. &a, &unixy, &rest), OP_EQ, 0);
  3326. tt_int_op(unixy, OP_EQ, 1);
  3327. tt_str_op(a, OP_EQ, "lolol");
  3328. tt_str_op(rest, OP_EQ, "");
  3329. tor_free(a);
  3330. tt_int_op(port_cfg_line_extract_addrport("foobar:lolol",
  3331. &a, &unixy, &rest), OP_EQ, 0);
  3332. tt_int_op(unixy, OP_EQ, 0);
  3333. tt_str_op(a, OP_EQ, "foobar:lolol");
  3334. tt_str_op(rest, OP_EQ, "");
  3335. tor_free(a);
  3336. tt_int_op(port_cfg_line_extract_addrport(":lolol",
  3337. &a, &unixy, &rest), OP_EQ, 0);
  3338. tt_int_op(unixy, OP_EQ, 0);
  3339. tt_str_op(a, OP_EQ, ":lolol");
  3340. tt_str_op(rest, OP_EQ, "");
  3341. tor_free(a);
  3342. tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\"",
  3343. &a, &unixy, &rest), OP_EQ, 0);
  3344. tt_int_op(unixy, OP_EQ, 1);
  3345. tt_str_op(a, OP_EQ, "lolol");
  3346. tt_str_op(rest, OP_EQ, "");
  3347. tor_free(a);
  3348. tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" ",
  3349. &a, &unixy, &rest), OP_EQ, 0);
  3350. tt_int_op(unixy, OP_EQ, 1);
  3351. tt_str_op(a, OP_EQ, "lolol");
  3352. tt_str_op(rest, OP_EQ, "");
  3353. tor_free(a);
  3354. tt_int_op(port_cfg_line_extract_addrport("unix:\"lolol\" foo ",
  3355. &a, &unixy, &rest), OP_EQ, 0);
  3356. tt_int_op(unixy, OP_EQ, 1);
  3357. tt_str_op(a, OP_EQ, "lolol");
  3358. tt_str_op(rest, OP_EQ, "foo ");
  3359. tor_free(a);
  3360. tt_int_op(port_cfg_line_extract_addrport("unix:\"lol ol\" foo ",
  3361. &a, &unixy, &rest), OP_EQ, 0);
  3362. tt_int_op(unixy, OP_EQ, 1);
  3363. tt_str_op(a, OP_EQ, "lol ol");
  3364. tt_str_op(rest, OP_EQ, "foo ");
  3365. tor_free(a);
  3366. tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol\" foo ",
  3367. &a, &unixy, &rest), OP_EQ, 0);
  3368. tt_int_op(unixy, OP_EQ, 1);
  3369. tt_str_op(a, OP_EQ, "lol\" ol");
  3370. tt_str_op(rest, OP_EQ, "foo ");
  3371. tor_free(a);
  3372. tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\\" ol foo ",
  3373. &a, &unixy, &rest), OP_EQ, -1);
  3374. tor_free(a);
  3375. tt_int_op(port_cfg_line_extract_addrport("unix:\"lol\\0\" ol foo ",
  3376. &a, &unixy, &rest), OP_EQ, -1);
  3377. tor_free(a);
  3378. done:
  3379. tor_free(a);
  3380. }
  3381. static config_line_t *
  3382. mock_config_line(const char *key, const char *val)
  3383. {
  3384. config_line_t *config_line = tor_malloc(sizeof(config_line_t));
  3385. memset(config_line, 0, sizeof(config_line_t));
  3386. config_line->key = tor_strdup(key);
  3387. config_line->value = tor_strdup(val);
  3388. return config_line;
  3389. }
  3390. static void
  3391. test_config_parse_port_config__ports__no_ports_given(void *data)
  3392. {
  3393. (void)data;
  3394. int ret;
  3395. smartlist_t *slout = NULL;
  3396. port_cfg_t *port_cfg = NULL;
  3397. slout = smartlist_new();
  3398. // Test no defaultport, no defaultaddress and no out
  3399. ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 0, 0);
  3400. tt_int_op(ret, OP_EQ, 0);
  3401. // Test with defaultport, no defaultaddress and no out
  3402. ret = parse_port_config(NULL, NULL, "DNS", 0, NULL, 42, 0);
  3403. tt_int_op(ret, OP_EQ, 0);
  3404. // Test no defaultport, with defaultaddress and no out
  3405. ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 0, 0);
  3406. tt_int_op(ret, OP_EQ, 0);
  3407. // Test with defaultport, with defaultaddress and no out
  3408. ret = parse_port_config(NULL, NULL, "DNS", 0, "127.0.0.2", 42, 0);
  3409. tt_int_op(ret, OP_EQ, 0);
  3410. // Test no defaultport, no defaultaddress and with out
  3411. ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 0, 0);
  3412. tt_int_op(ret, OP_EQ, 0);
  3413. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3414. // Test with defaultport, no defaultaddress and with out
  3415. ret = parse_port_config(slout, NULL, "DNS", 0, NULL, 42, 0);
  3416. tt_int_op(ret, OP_EQ, 0);
  3417. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3418. // Test no defaultport, with defaultaddress and with out
  3419. ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 0, 0);
  3420. tt_int_op(ret, OP_EQ, 0);
  3421. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3422. // Test with defaultport, with defaultaddress and out, adds a new port cfg
  3423. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3424. smartlist_clear(slout);
  3425. ret = parse_port_config(slout, NULL, "DNS", 0, "127.0.0.2", 42, 0);
  3426. tt_int_op(ret, OP_EQ, 0);
  3427. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3428. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3429. tt_int_op(port_cfg->port, OP_EQ, 42);
  3430. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 0);
  3431. // Test with defaultport, with defaultaddress and out, adds a new port cfg
  3432. // for a unix address
  3433. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3434. smartlist_clear(slout);
  3435. ret = parse_port_config(slout, NULL, "DNS", 0, "/foo/bar/unixdomain",
  3436. 42, CL_PORT_IS_UNIXSOCKET);
  3437. tt_int_op(ret, OP_EQ, 0);
  3438. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3439. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3440. tt_int_op(port_cfg->port, OP_EQ, 0);
  3441. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  3442. tt_str_op(port_cfg->unix_addr, OP_EQ, "/foo/bar/unixdomain");
  3443. done:
  3444. if (slout)
  3445. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3446. smartlist_free(slout);
  3447. }
  3448. static void
  3449. test_config_parse_port_config__ports__ports_given(void *data)
  3450. {
  3451. (void)data;
  3452. int ret;
  3453. smartlist_t *slout = NULL;
  3454. port_cfg_t *port_cfg = NULL;
  3455. config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
  3456. tor_addr_t addr;
  3457. slout = smartlist_new();
  3458. mock_hostname_resolver();
  3459. // Test error when encounters an invalid Port specification
  3460. config_port_invalid = mock_config_line("DNSPort", "");
  3461. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
  3462. 0, 0);
  3463. tt_int_op(ret, OP_EQ, -1);
  3464. // Test error when encounters an empty unix domain specification
  3465. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3466. config_port_invalid = mock_config_line("DNSPort", "unix:");
  3467. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0, NULL,
  3468. 0, 0);
  3469. tt_int_op(ret, OP_EQ, -1);
  3470. // Test error when encounters a unix domain specification but the listener
  3471. // doesn't support domain sockets
  3472. config_port_valid = mock_config_line("DNSPort", "unix:/tmp/foo/bar");
  3473. ret = parse_port_config(NULL, config_port_valid, "DNS",
  3474. CONN_TYPE_AP_DNS_LISTENER, NULL, 0, 0);
  3475. tt_int_op(ret, OP_EQ, -1);
  3476. // Test valid unix domain
  3477. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3478. smartlist_clear(slout);
  3479. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3480. CONN_TYPE_AP_LISTENER, NULL, 0, 0);
  3481. #ifdef _WIN32
  3482. tt_int_op(ret, OP_EQ, -1);
  3483. #else
  3484. tt_int_op(ret, OP_EQ, 0);
  3485. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3486. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3487. tt_int_op(port_cfg->port, OP_EQ, 0);
  3488. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  3489. tt_str_op(port_cfg->unix_addr, OP_EQ, "/tmp/foo/bar");
  3490. /* Test entry port defaults as initialised in parse_port_config */
  3491. tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
  3492. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
  3493. tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
  3494. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
  3495. tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 1);
  3496. #endif /* defined(_WIN32) */
  3497. // Test failure if we have no ipv4 and no ipv6 and no onion (DNS only)
  3498. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3499. config_port_invalid = mock_config_line("SOCKSPort",
  3500. "unix:/tmp/foo/bar NoIPv4Traffic "
  3501. "NoIPv6Traffic "
  3502. "NoOnionTraffic");
  3503. ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
  3504. CONN_TYPE_AP_LISTENER, NULL, 0,
  3505. CL_PORT_TAKES_HOSTNAMES);
  3506. tt_int_op(ret, OP_EQ, -1);
  3507. // Test failure if we have no DNS and we're a DNSPort
  3508. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3509. config_port_invalid = mock_config_line("DNSPort",
  3510. "127.0.0.1:80 NoDNSRequest");
  3511. ret = parse_port_config(NULL, config_port_invalid, "DNS",
  3512. CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
  3513. CL_PORT_TAKES_HOSTNAMES);
  3514. tt_int_op(ret, OP_EQ, -1);
  3515. // If we're a DNSPort, DNS only is ok
  3516. // Use a port because DNSPort doesn't support sockets
  3517. config_free_lines(config_port_valid); config_port_valid = NULL;
  3518. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3519. smartlist_clear(slout);
  3520. config_port_valid = mock_config_line("DNSPort", "127.0.0.1:80 "
  3521. "NoIPv6Traffic "
  3522. "NoIPv4Traffic NoOnionTraffic");
  3523. ret = parse_port_config(slout, config_port_valid, "DNS",
  3524. CONN_TYPE_AP_DNS_LISTENER, NULL, 0,
  3525. CL_PORT_TAKES_HOSTNAMES);
  3526. tt_int_op(ret, OP_EQ, 0);
  3527. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3528. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3529. tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 1);
  3530. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3531. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
  3532. tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 0);
  3533. // Test failure if we have DNS but no ipv4 and no ipv6
  3534. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3535. config_port_invalid = mock_config_line("SOCKSPort",
  3536. "NoIPv6Traffic "
  3537. "unix:/tmp/foo/bar NoIPv4Traffic");
  3538. ret = parse_port_config(NULL, config_port_invalid, "SOCKS",
  3539. CONN_TYPE_AP_LISTENER, NULL, 0,
  3540. CL_PORT_TAKES_HOSTNAMES);
  3541. tt_int_op(ret, OP_EQ, -1);
  3542. // Test success with no DNS, no ipv4, no ipv6 (only onion, using separate
  3543. // options)
  3544. config_free_lines(config_port_valid); config_port_valid = NULL;
  3545. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3546. smartlist_clear(slout);
  3547. config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
  3548. "NoIPv6Traffic "
  3549. "NoDNSRequest NoIPv4Traffic");
  3550. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3551. CONN_TYPE_AP_LISTENER, NULL, 0,
  3552. CL_PORT_TAKES_HOSTNAMES);
  3553. #ifdef _WIN32
  3554. tt_int_op(ret, OP_EQ, -1);
  3555. #else
  3556. tt_int_op(ret, OP_EQ, 0);
  3557. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3558. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3559. tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
  3560. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3561. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
  3562. tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
  3563. #endif /* defined(_WIN32) */
  3564. // Test success with quoted unix: address.
  3565. config_free_lines(config_port_valid); config_port_valid = NULL;
  3566. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3567. smartlist_clear(slout);
  3568. config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar\" "
  3569. "NoIPv6Traffic "
  3570. "NoDNSRequest NoIPv4Traffic");
  3571. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3572. CONN_TYPE_AP_LISTENER, NULL, 0,
  3573. CL_PORT_TAKES_HOSTNAMES);
  3574. #ifdef _WIN32
  3575. tt_int_op(ret, OP_EQ, -1);
  3576. #else
  3577. tt_int_op(ret, OP_EQ, 0);
  3578. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3579. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3580. tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
  3581. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3582. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
  3583. tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
  3584. #endif /* defined(_WIN32) */
  3585. // Test failure with broken quoted unix: address.
  3586. config_free_lines(config_port_valid); config_port_valid = NULL;
  3587. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3588. smartlist_clear(slout);
  3589. config_port_valid = mock_config_line("SOCKSPort", "unix:\"/tmp/foo/ bar "
  3590. "NoIPv6Traffic "
  3591. "NoDNSRequest NoIPv4Traffic");
  3592. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3593. CONN_TYPE_AP_LISTENER, NULL, 0,
  3594. CL_PORT_TAKES_HOSTNAMES);
  3595. tt_int_op(ret, OP_EQ, -1);
  3596. // Test failure with empty quoted unix: address.
  3597. config_free_lines(config_port_valid); config_port_valid = NULL;
  3598. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3599. smartlist_clear(slout);
  3600. config_port_valid = mock_config_line("SOCKSPort", "unix:\"\" "
  3601. "NoIPv6Traffic "
  3602. "NoDNSRequest NoIPv4Traffic");
  3603. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3604. CONN_TYPE_AP_LISTENER, NULL, 0,
  3605. CL_PORT_TAKES_HOSTNAMES);
  3606. tt_int_op(ret, OP_EQ, -1);
  3607. // Test success with OnionTrafficOnly (no DNS, no ipv4, no ipv6)
  3608. config_free_lines(config_port_valid); config_port_valid = NULL;
  3609. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3610. smartlist_clear(slout);
  3611. config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
  3612. "OnionTrafficOnly");
  3613. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3614. CONN_TYPE_AP_LISTENER, NULL, 0,
  3615. CL_PORT_TAKES_HOSTNAMES);
  3616. #ifdef _WIN32
  3617. tt_int_op(ret, OP_EQ, -1);
  3618. #else
  3619. tt_int_op(ret, OP_EQ, 0);
  3620. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3621. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3622. tt_int_op(port_cfg->entry_cfg.dns_request, OP_EQ, 0);
  3623. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3624. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 0);
  3625. tt_int_op(port_cfg->entry_cfg.onion_traffic, OP_EQ, 1);
  3626. #endif /* defined(_WIN32) */
  3627. // Test success with no ipv4 but take ipv6
  3628. config_free_lines(config_port_valid); config_port_valid = NULL;
  3629. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3630. smartlist_clear(slout);
  3631. config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
  3632. "NoIPv4Traffic IPv6Traffic");
  3633. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3634. CONN_TYPE_AP_LISTENER, NULL, 0,
  3635. CL_PORT_TAKES_HOSTNAMES);
  3636. #ifdef _WIN32
  3637. tt_int_op(ret, OP_EQ, -1);
  3638. #else
  3639. tt_int_op(ret, OP_EQ, 0);
  3640. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3641. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3642. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 0);
  3643. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
  3644. #endif /* defined(_WIN32) */
  3645. // Test success with both ipv4 and ipv6
  3646. config_free_lines(config_port_valid); config_port_valid = NULL;
  3647. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3648. smartlist_clear(slout);
  3649. config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/foo/bar "
  3650. "IPv4Traffic IPv6Traffic");
  3651. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3652. CONN_TYPE_AP_LISTENER, NULL, 0,
  3653. CL_PORT_TAKES_HOSTNAMES);
  3654. #ifdef _WIN32
  3655. tt_int_op(ret, OP_EQ, -1);
  3656. #else
  3657. tt_int_op(ret, OP_EQ, 0);
  3658. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3659. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3660. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
  3661. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
  3662. #endif /* defined(_WIN32) */
  3663. // Test failure if we specify world writable for an IP Port
  3664. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3665. config_port_invalid = mock_config_line("DNSPort", "42 WorldWritable");
  3666. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
  3667. "127.0.0.3", 0, 0);
  3668. tt_int_op(ret, OP_EQ, -1);
  3669. // Test failure if we specify group writable for an IP Port
  3670. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3671. config_port_invalid = mock_config_line("DNSPort", "42 GroupWritable");
  3672. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
  3673. "127.0.0.3", 0, 0);
  3674. tt_int_op(ret, OP_EQ, -1);
  3675. // Test failure if we specify group writable for an IP Port
  3676. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3677. config_port_invalid = mock_config_line("DNSPort", "42 RelaxDirModeCheck");
  3678. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
  3679. "127.0.0.3", 0, 0);
  3680. tt_int_op(ret, OP_EQ, -1);
  3681. // Test success with only a port (this will fail without a default address)
  3682. config_free_lines(config_port_valid); config_port_valid = NULL;
  3683. config_port_valid = mock_config_line("DNSPort", "42");
  3684. ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
  3685. "127.0.0.3", 0, 0);
  3686. tt_int_op(ret, OP_EQ, 0);
  3687. // Test success with only a port and isolate destination port
  3688. config_free_lines(config_port_valid); config_port_valid = NULL;
  3689. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3690. smartlist_clear(slout);
  3691. config_port_valid = mock_config_line("DNSPort", "42 IsolateDestPort");
  3692. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3693. "127.0.0.3", 0, 0);
  3694. tt_int_op(ret, OP_EQ, 0);
  3695. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3696. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3697. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3698. ISO_DEFAULT | ISO_DESTPORT);
  3699. // Test success with a negative isolate destination port, and plural
  3700. config_free_lines(config_port_valid); config_port_valid = NULL;
  3701. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3702. smartlist_clear(slout);
  3703. config_port_valid = mock_config_line("DNSPort", "42 NoIsolateDestPorts");
  3704. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3705. "127.0.0.3", 0, 0);
  3706. tt_int_op(ret, OP_EQ, 0);
  3707. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3708. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3709. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3710. ISO_DEFAULT & ~ISO_DESTPORT);
  3711. // Test success with isolate destination address
  3712. config_free_lines(config_port_valid); config_port_valid = NULL;
  3713. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3714. smartlist_clear(slout);
  3715. config_port_valid = mock_config_line("DNSPort", "42 IsolateDestAddr");
  3716. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3717. "127.0.0.3", 0, 0);
  3718. tt_int_op(ret, OP_EQ, 0);
  3719. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3720. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3721. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3722. ISO_DEFAULT | ISO_DESTADDR);
  3723. // Test success with isolate socks AUTH
  3724. config_free_lines(config_port_valid); config_port_valid = NULL;
  3725. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3726. smartlist_clear(slout);
  3727. config_port_valid = mock_config_line("DNSPort", "42 IsolateSOCKSAuth");
  3728. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3729. "127.0.0.3", 0, 0);
  3730. tt_int_op(ret, OP_EQ, 0);
  3731. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3732. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3733. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3734. ISO_DEFAULT | ISO_SOCKSAUTH);
  3735. // Test success with isolate client protocol
  3736. config_free_lines(config_port_valid); config_port_valid = NULL;
  3737. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3738. smartlist_clear(slout);
  3739. config_port_valid = mock_config_line("DNSPort", "42 IsolateClientProtocol");
  3740. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3741. "127.0.0.3", 0, 0);
  3742. tt_int_op(ret, OP_EQ, 0);
  3743. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3744. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3745. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3746. ISO_DEFAULT | ISO_CLIENTPROTO);
  3747. // Test success with isolate client address
  3748. config_free_lines(config_port_valid); config_port_valid = NULL;
  3749. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3750. smartlist_clear(slout);
  3751. config_port_valid = mock_config_line("DNSPort", "42 IsolateClientAddr");
  3752. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3753. "127.0.0.3", 0, 0);
  3754. tt_int_op(ret, OP_EQ, 0);
  3755. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3756. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3757. tt_int_op(port_cfg->entry_cfg.isolation_flags, OP_EQ,
  3758. ISO_DEFAULT | ISO_CLIENTADDR);
  3759. // Test success with ignored unknown options
  3760. config_free_lines(config_port_valid); config_port_valid = NULL;
  3761. config_port_valid = mock_config_line("DNSPort", "42 ThisOptionDoesntExist");
  3762. ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
  3763. "127.0.0.3", 0, 0);
  3764. tt_int_op(ret, OP_EQ, 0);
  3765. // Test success with no isolate socks AUTH
  3766. config_free_lines(config_port_valid); config_port_valid = NULL;
  3767. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3768. smartlist_clear(slout);
  3769. config_port_valid = mock_config_line("DNSPort", "42 NoIsolateSOCKSAuth");
  3770. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3771. "127.0.0.3", 0, 0);
  3772. tt_int_op(ret, OP_EQ, 0);
  3773. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3774. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3775. tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
  3776. // Test success with prefer ipv6
  3777. config_free_lines(config_port_valid); config_port_valid = NULL;
  3778. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3779. smartlist_clear(slout);
  3780. config_port_valid = mock_config_line("SOCKSPort",
  3781. "42 IPv6Traffic PreferIPv6");
  3782. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  3783. CONN_TYPE_AP_LISTENER, "127.0.0.42", 0,
  3784. CL_PORT_TAKES_HOSTNAMES);
  3785. tt_int_op(ret, OP_EQ, 0);
  3786. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3787. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3788. tt_int_op(port_cfg->entry_cfg.prefer_ipv6, OP_EQ, 1);
  3789. // Test success with cache ipv4 DNS
  3790. config_free_lines(config_port_valid); config_port_valid = NULL;
  3791. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3792. smartlist_clear(slout);
  3793. config_port_valid = mock_config_line("DNSPort", "42 CacheIPv4DNS");
  3794. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3795. "127.0.0.42", 0, 0);
  3796. tt_int_op(ret, OP_EQ, 0);
  3797. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3798. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3799. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
  3800. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
  3801. // Test success with cache ipv6 DNS
  3802. config_free_lines(config_port_valid); config_port_valid = NULL;
  3803. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3804. smartlist_clear(slout);
  3805. config_port_valid = mock_config_line("DNSPort", "42 CacheIPv6DNS");
  3806. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3807. "127.0.0.42", 0, 0);
  3808. tt_int_op(ret, OP_EQ, 0);
  3809. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3810. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3811. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
  3812. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
  3813. // Test success with no cache ipv4 DNS
  3814. config_free_lines(config_port_valid); config_port_valid = NULL;
  3815. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3816. smartlist_clear(slout);
  3817. config_port_valid = mock_config_line("DNSPort", "42 NoCacheIPv4DNS");
  3818. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3819. "127.0.0.42", 0, 0);
  3820. tt_int_op(ret, OP_EQ, 0);
  3821. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3822. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3823. tt_int_op(port_cfg->entry_cfg.cache_ipv4_answers, OP_EQ, 0);
  3824. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 0);
  3825. // Test success with cache DNS
  3826. config_free_lines(config_port_valid); config_port_valid = NULL;
  3827. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3828. smartlist_clear(slout);
  3829. config_port_valid = mock_config_line("DNSPort", "42 CacheDNS");
  3830. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3831. "127.0.0.42", 0, CL_PORT_TAKES_HOSTNAMES);
  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->entry_cfg.cache_ipv4_answers, OP_EQ, 1);
  3836. tt_int_op(port_cfg->entry_cfg.cache_ipv6_answers, OP_EQ, 1);
  3837. // Test success with use cached ipv4 DNS
  3838. config_free_lines(config_port_valid); config_port_valid = NULL;
  3839. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3840. smartlist_clear(slout);
  3841. config_port_valid = mock_config_line("DNSPort", "42 UseIPv4Cache");
  3842. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3843. "127.0.0.42", 0, 0);
  3844. tt_int_op(ret, OP_EQ, 0);
  3845. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3846. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3847. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
  3848. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 0);
  3849. // Test success with use cached ipv6 DNS
  3850. config_free_lines(config_port_valid); config_port_valid = NULL;
  3851. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3852. smartlist_clear(slout);
  3853. config_port_valid = mock_config_line("DNSPort", "42 UseIPv6Cache");
  3854. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3855. "127.0.0.42", 0, 0);
  3856. tt_int_op(ret, OP_EQ, 0);
  3857. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3858. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3859. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 0);
  3860. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
  3861. // Test success with use cached DNS
  3862. config_free_lines(config_port_valid); config_port_valid = NULL;
  3863. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3864. smartlist_clear(slout);
  3865. config_port_valid = mock_config_line("DNSPort", "42 UseDNSCache");
  3866. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3867. "127.0.0.42", 0, 0);
  3868. tt_int_op(ret, OP_EQ, 0);
  3869. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3870. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3871. tt_int_op(port_cfg->entry_cfg.use_cached_ipv4_answers, OP_EQ, 1);
  3872. tt_int_op(port_cfg->entry_cfg.use_cached_ipv6_answers, OP_EQ, 1);
  3873. // Test success with not preferring ipv6 automap
  3874. config_free_lines(config_port_valid); config_port_valid = NULL;
  3875. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3876. smartlist_clear(slout);
  3877. config_port_valid = mock_config_line("DNSPort", "42 NoPreferIPv6Automap");
  3878. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3879. "127.0.0.42", 0, 0);
  3880. tt_int_op(ret, OP_EQ, 0);
  3881. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3882. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3883. tt_int_op(port_cfg->entry_cfg.prefer_ipv6_virtaddr, OP_EQ, 0);
  3884. // Test success with prefer SOCKS no auth
  3885. config_free_lines(config_port_valid); config_port_valid = NULL;
  3886. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3887. smartlist_clear(slout);
  3888. config_port_valid = mock_config_line("DNSPort", "42 PreferSOCKSNoAuth");
  3889. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3890. "127.0.0.42", 0, 0);
  3891. tt_int_op(ret, OP_EQ, 0);
  3892. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3893. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3894. tt_int_op(port_cfg->entry_cfg.socks_prefer_no_auth, OP_EQ, 1);
  3895. // Test failure with both a zero port and a non-zero port
  3896. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3897. config_free_lines(config_port_valid); config_port_valid = NULL;
  3898. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3899. smartlist_clear(slout);
  3900. config_port_invalid = mock_config_line("DNSPort", "0");
  3901. config_port_valid = mock_config_line("DNSPort", "42");
  3902. config_port_invalid->next = config_port_valid;
  3903. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  3904. "127.0.0.42", 0, 0);
  3905. tt_int_op(ret, OP_EQ, -1);
  3906. // Test success with warn non-local control
  3907. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3908. smartlist_clear(slout);
  3909. ret = parse_port_config(slout, config_port_valid, "Control",
  3910. CONN_TYPE_CONTROL_LISTENER, "127.0.0.42", 0,
  3911. CL_PORT_WARN_NONLOCAL);
  3912. tt_int_op(ret, OP_EQ, 0);
  3913. // Test success with warn non-local listener
  3914. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3915. smartlist_clear(slout);
  3916. ret = parse_port_config(slout, config_port_valid, "ExtOR",
  3917. CONN_TYPE_EXT_OR_LISTENER, "127.0.0.42", 0,
  3918. CL_PORT_WARN_NONLOCAL);
  3919. tt_int_op(ret, OP_EQ, 0);
  3920. // Test success with warn non-local other
  3921. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3922. smartlist_clear(slout);
  3923. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3924. "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
  3925. tt_int_op(ret, OP_EQ, 0);
  3926. // Test success with warn non-local other without out
  3927. ret = parse_port_config(NULL, config_port_valid, "DNS", 0,
  3928. "127.0.0.42", 0, CL_PORT_WARN_NONLOCAL);
  3929. tt_int_op(ret, OP_EQ, 0);
  3930. // Test success with both ipv4 and ipv6 but without stream options
  3931. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3932. config_port_valid = NULL;
  3933. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3934. smartlist_clear(slout);
  3935. config_port_valid = mock_config_line("DNSPort", "42 IPv4Traffic "
  3936. "IPv6Traffic");
  3937. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3938. "127.0.0.44", 0,
  3939. CL_PORT_TAKES_HOSTNAMES |
  3940. CL_PORT_NO_STREAM_OPTIONS);
  3941. tt_int_op(ret, OP_EQ, 0);
  3942. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3943. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3944. tt_int_op(port_cfg->entry_cfg.ipv4_traffic, OP_EQ, 1);
  3945. tt_int_op(port_cfg->entry_cfg.ipv6_traffic, OP_EQ, 1);
  3946. // Test failure for a SessionGroup argument with invalid value
  3947. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3948. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3949. smartlist_clear(slout);
  3950. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=invalid");
  3951. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  3952. "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
  3953. tt_int_op(ret, OP_EQ, -1);
  3954. // Test failure for a SessionGroup argument with valid value but with no
  3955. // stream options allowed
  3956. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3957. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3958. smartlist_clear(slout);
  3959. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123");
  3960. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  3961. "127.0.0.44", 0, CL_PORT_NO_STREAM_OPTIONS);
  3962. tt_int_op(ret, OP_EQ, -1);
  3963. // Test failure for more than one SessionGroup argument
  3964. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  3965. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3966. smartlist_clear(slout);
  3967. config_port_invalid = mock_config_line("DNSPort", "42 SessionGroup=123 "
  3968. "SessionGroup=321");
  3969. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  3970. "127.0.0.44", 0, 0);
  3971. tt_int_op(ret, OP_EQ, -1);
  3972. // Test success with a sessiongroup options
  3973. config_free_lines(config_port_valid); config_port_valid = NULL;
  3974. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3975. smartlist_clear(slout);
  3976. config_port_valid = mock_config_line("DNSPort", "42 SessionGroup=1111122");
  3977. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3978. "127.0.0.44", 0, 0);
  3979. tt_int_op(ret, OP_EQ, 0);
  3980. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  3981. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  3982. tt_int_op(port_cfg->entry_cfg.session_group, OP_EQ, 1111122);
  3983. // Test success with a zero unix domain socket, and doesnt add it to out
  3984. config_free_lines(config_port_valid); config_port_valid = NULL;
  3985. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3986. smartlist_clear(slout);
  3987. config_port_valid = mock_config_line("DNSPort", "0");
  3988. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3989. "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
  3990. tt_int_op(ret, OP_EQ, 0);
  3991. tt_int_op(smartlist_len(slout), OP_EQ, 0);
  3992. // Test success with a one unix domain socket, and doesnt add it to out
  3993. config_free_lines(config_port_valid); config_port_valid = NULL;
  3994. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  3995. smartlist_clear(slout);
  3996. config_port_valid = mock_config_line("DNSPort", "something");
  3997. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  3998. "127.0.0.45", 0, CL_PORT_IS_UNIXSOCKET);
  3999. tt_int_op(ret, OP_EQ, 0);
  4000. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4001. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4002. tt_int_op(port_cfg->is_unix_addr, OP_EQ, 1);
  4003. tt_str_op(port_cfg->unix_addr, OP_EQ, "something");
  4004. // Test success with a port of auto - it uses the default address
  4005. config_free_lines(config_port_valid); config_port_valid = NULL;
  4006. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4007. smartlist_clear(slout);
  4008. config_port_valid = mock_config_line("DNSPort", "auto");
  4009. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  4010. "127.0.0.46", 0, 0);
  4011. tt_int_op(ret, OP_EQ, 0);
  4012. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4013. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4014. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  4015. tor_addr_parse(&addr, "127.0.0.46");
  4016. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  4017. // Test success with a port of auto in mixed case
  4018. config_free_lines(config_port_valid); config_port_valid = NULL;
  4019. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4020. smartlist_clear(slout);
  4021. config_port_valid = mock_config_line("DNSPort", "AuTo");
  4022. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  4023. "127.0.0.46", 0, 0);
  4024. tt_int_op(ret, OP_EQ, 0);
  4025. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4026. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4027. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  4028. tor_addr_parse(&addr, "127.0.0.46");
  4029. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  4030. // Test success with parsing both an address and an auto port
  4031. config_free_lines(config_port_valid); config_port_valid = NULL;
  4032. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4033. smartlist_clear(slout);
  4034. config_port_valid = mock_config_line("DNSPort", "127.0.0.122:auto");
  4035. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  4036. "127.0.0.46", 0, 0);
  4037. tt_int_op(ret, OP_EQ, 0);
  4038. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4039. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4040. tt_int_op(port_cfg->port, OP_EQ, CFG_AUTO_PORT);
  4041. tor_addr_parse(&addr, "127.0.0.122");
  4042. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  4043. // Test failure when asked to parse an invalid address followed by auto
  4044. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4045. config_port_invalid = mock_config_line("DNSPort", "invalidstuff!!:auto");
  4046. MOCK(tor_addr_lookup, mock_tor_addr_lookup__fail_on_bad_addrs);
  4047. ret = parse_port_config(NULL, config_port_invalid, "DNS", 0,
  4048. "127.0.0.46", 0, 0);
  4049. UNMOCK(tor_addr_lookup);
  4050. tt_int_op(ret, OP_EQ, -1);
  4051. // Test success with parsing both an address and a real port
  4052. config_free_lines(config_port_valid); config_port_valid = NULL;
  4053. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4054. smartlist_clear(slout);
  4055. config_port_valid = mock_config_line("DNSPort", "127.0.0.123:656");
  4056. ret = parse_port_config(slout, config_port_valid, "DNS", 0,
  4057. "127.0.0.46", 0, 0);
  4058. tt_int_op(ret, OP_EQ, 0);
  4059. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4060. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4061. tt_int_op(port_cfg->port, OP_EQ, 656);
  4062. tor_addr_parse(&addr, "127.0.0.123");
  4063. tt_assert(tor_addr_eq(&port_cfg->addr, &addr))
  4064. // Test failure if we can't parse anything at all
  4065. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4066. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4067. smartlist_clear(slout);
  4068. config_port_invalid = mock_config_line("DNSPort", "something wrong");
  4069. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  4070. "127.0.0.46", 0, 0);
  4071. tt_int_op(ret, OP_EQ, -1);
  4072. // Test failure if we find both an address, a port and an auto
  4073. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4074. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4075. smartlist_clear(slout);
  4076. config_port_invalid = mock_config_line("DNSPort", "127.0.1.0:123:auto");
  4077. ret = parse_port_config(slout, config_port_invalid, "DNS", 0,
  4078. "127.0.0.46", 0, 0);
  4079. tt_int_op(ret, OP_EQ, -1);
  4080. // Test that default to group writeable default sets group writeable for
  4081. // domain socket
  4082. config_free_lines(config_port_valid); config_port_valid = NULL;
  4083. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4084. smartlist_clear(slout);
  4085. config_port_valid = mock_config_line("SOCKSPort", "unix:/tmp/somewhere");
  4086. ret = parse_port_config(slout, config_port_valid, "SOCKS",
  4087. CONN_TYPE_AP_LISTENER, "127.0.0.46", 0,
  4088. CL_PORT_DFLT_GROUP_WRITABLE);
  4089. #ifdef _WIN32
  4090. tt_int_op(ret, OP_EQ, -1);
  4091. #else
  4092. tt_int_op(ret, OP_EQ, 0);
  4093. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4094. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4095. tt_int_op(port_cfg->is_group_writable, OP_EQ, 1);
  4096. #endif /* defined(_WIN32) */
  4097. done:
  4098. unmock_hostname_resolver();
  4099. if (slout)
  4100. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4101. smartlist_free(slout);
  4102. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4103. config_free_lines(config_port_valid); config_port_valid = NULL;
  4104. }
  4105. static void
  4106. test_config_parse_port_config__ports__server_options(void *data)
  4107. {
  4108. (void)data;
  4109. int ret;
  4110. smartlist_t *slout = NULL;
  4111. port_cfg_t *port_cfg = NULL;
  4112. config_line_t *config_port_invalid = NULL, *config_port_valid = NULL;
  4113. slout = smartlist_new();
  4114. // Test success with NoAdvertise option
  4115. config_free_lines(config_port_valid); config_port_valid = NULL;
  4116. config_port_valid = mock_config_line("DNSPort",
  4117. "127.0.0.124:656 NoAdvertise");
  4118. ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
  4119. CL_PORT_SERVER_OPTIONS);
  4120. tt_int_op(ret, OP_EQ, 0);
  4121. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4122. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4123. tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 1);
  4124. tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 0);
  4125. // Test success with NoListen option
  4126. config_free_lines(config_port_valid); config_port_valid = NULL;
  4127. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4128. smartlist_clear(slout);
  4129. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen");
  4130. ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
  4131. CL_PORT_SERVER_OPTIONS);
  4132. tt_int_op(ret, OP_EQ, 0);
  4133. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4134. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4135. tt_int_op(port_cfg->server_cfg.no_advertise, OP_EQ, 0);
  4136. tt_int_op(port_cfg->server_cfg.no_listen, OP_EQ, 1);
  4137. // Test failure with both NoAdvertise and NoListen option
  4138. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4139. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4140. smartlist_clear(slout);
  4141. config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 NoListen "
  4142. "NoAdvertise");
  4143. ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
  4144. 0, CL_PORT_SERVER_OPTIONS);
  4145. tt_int_op(ret, OP_EQ, -1);
  4146. // Test success with IPv4Only
  4147. config_free_lines(config_port_valid); config_port_valid = NULL;
  4148. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4149. smartlist_clear(slout);
  4150. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 IPv4Only");
  4151. ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
  4152. CL_PORT_SERVER_OPTIONS);
  4153. tt_int_op(ret, OP_EQ, 0);
  4154. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4155. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4156. tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 1);
  4157. tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 0);
  4158. // Test success with IPv6Only
  4159. config_free_lines(config_port_valid); config_port_valid = NULL;
  4160. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4161. smartlist_clear(slout);
  4162. config_port_valid = mock_config_line("DNSPort", "[::1]:656 IPv6Only");
  4163. ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
  4164. CL_PORT_SERVER_OPTIONS);
  4165. tt_int_op(ret, OP_EQ, 0);
  4166. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4167. port_cfg = (port_cfg_t *)smartlist_get(slout, 0);
  4168. tt_int_op(port_cfg->server_cfg.bind_ipv4_only, OP_EQ, 0);
  4169. tt_int_op(port_cfg->server_cfg.bind_ipv6_only, OP_EQ, 1);
  4170. // Test failure with both IPv4Only and IPv6Only
  4171. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4172. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4173. smartlist_clear(slout);
  4174. config_port_invalid = mock_config_line("DNSPort", "127.0.0.124:656 IPv6Only "
  4175. "IPv4Only");
  4176. ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
  4177. 0, CL_PORT_SERVER_OPTIONS);
  4178. tt_int_op(ret, OP_EQ, -1);
  4179. // Test success with invalid parameter
  4180. config_free_lines(config_port_valid); config_port_valid = NULL;
  4181. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4182. smartlist_clear(slout);
  4183. config_port_valid = mock_config_line("DNSPort", "127.0.0.124:656 unknown");
  4184. ret = parse_port_config(slout, config_port_valid, "DNS", 0, NULL, 0,
  4185. CL_PORT_SERVER_OPTIONS);
  4186. tt_int_op(ret, OP_EQ, 0);
  4187. tt_int_op(smartlist_len(slout), OP_EQ, 1);
  4188. // Test failure when asked to bind only to ipv6 but gets an ipv4 address
  4189. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4190. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4191. smartlist_clear(slout);
  4192. config_port_invalid = mock_config_line("DNSPort",
  4193. "127.0.0.124:656 IPv6Only");
  4194. ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
  4195. 0, CL_PORT_SERVER_OPTIONS);
  4196. tt_int_op(ret, OP_EQ, -1);
  4197. // Test failure when asked to bind only to ipv4 but gets an ipv6 address
  4198. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4199. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4200. smartlist_clear(slout);
  4201. config_port_invalid = mock_config_line("DNSPort", "[::1]:656 IPv4Only");
  4202. ret = parse_port_config(slout, config_port_invalid, "DNS", 0, NULL,
  4203. 0, CL_PORT_SERVER_OPTIONS);
  4204. tt_int_op(ret, OP_EQ, -1);
  4205. // Check for failure with empty unix: address.
  4206. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4207. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4208. smartlist_clear(slout);
  4209. config_port_invalid = mock_config_line("ORPort", "unix:\"\"");
  4210. ret = parse_port_config(slout, config_port_invalid, "ORPort", 0, NULL,
  4211. 0, CL_PORT_SERVER_OPTIONS);
  4212. tt_int_op(ret, OP_EQ, -1);
  4213. done:
  4214. if (slout)
  4215. SMARTLIST_FOREACH(slout,port_cfg_t *,pf,port_cfg_free(pf));
  4216. smartlist_free(slout);
  4217. config_free_lines(config_port_invalid); config_port_invalid = NULL;
  4218. config_free_lines(config_port_valid); config_port_valid = NULL;
  4219. }
  4220. static void
  4221. test_config_parse_log_severity(void *data)
  4222. {
  4223. int ret;
  4224. const char *severity_log_lines[] = {
  4225. "debug file /tmp/debug.log",
  4226. "debug\tfile /tmp/debug.log",
  4227. "[handshake]debug [~net,~mm]info notice stdout",
  4228. "[handshake]debug\t[~net,~mm]info\tnotice\tstdout",
  4229. NULL
  4230. };
  4231. int i;
  4232. log_severity_list_t *severity;
  4233. (void) data;
  4234. severity = tor_malloc(sizeof(log_severity_list_t));
  4235. for (i = 0; severity_log_lines[i]; i++) {
  4236. memset(severity, 0, sizeof(log_severity_list_t));
  4237. ret = parse_log_severity_config(&severity_log_lines[i], severity);
  4238. tt_int_op(ret, OP_EQ, 0);
  4239. }
  4240. done:
  4241. tor_free(severity);
  4242. }
  4243. static void
  4244. test_config_include_limit(void *data)
  4245. {
  4246. (void)data;
  4247. config_line_t *result = NULL;
  4248. char *torrc_path = NULL;
  4249. char *dir = tor_strdup(get_fname("test_include_limit"));
  4250. tt_ptr_op(dir, OP_NE, NULL);
  4251. #ifdef _WIN32
  4252. tt_int_op(mkdir(dir), OP_EQ, 0);
  4253. #else
  4254. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4255. #endif
  4256. tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
  4257. char torrc_contents[1000];
  4258. tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
  4259. torrc_path);
  4260. tt_int_op(write_str_to_file(torrc_path, torrc_contents, 0), OP_EQ, 0);
  4261. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
  4262. OP_EQ, -1);
  4263. done:
  4264. config_free_lines(result);
  4265. tor_free(torrc_path);
  4266. tor_free(dir);
  4267. }
  4268. static void
  4269. test_config_include_does_not_exist(void *data)
  4270. {
  4271. (void)data;
  4272. config_line_t *result = NULL;
  4273. char *dir = tor_strdup(get_fname("test_include_does_not_exist"));
  4274. char *missing_path = NULL;
  4275. tt_ptr_op(dir, OP_NE, NULL);
  4276. #ifdef _WIN32
  4277. tt_int_op(mkdir(dir), OP_EQ, 0);
  4278. #else
  4279. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4280. #endif
  4281. tor_asprintf(&missing_path, "%s"PATH_SEPARATOR"missing", dir);
  4282. char torrc_contents[1000];
  4283. tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
  4284. missing_path);
  4285. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
  4286. OP_EQ, -1);
  4287. done:
  4288. config_free_lines(result);
  4289. tor_free(dir);
  4290. tor_free(missing_path);
  4291. }
  4292. static void
  4293. test_config_include_error_in_included_file(void *data)
  4294. {
  4295. (void)data;
  4296. config_line_t *result = NULL;
  4297. char *dir = tor_strdup(get_fname("test_error_in_included_file"));
  4298. char *invalid_path = NULL;
  4299. tt_ptr_op(dir, OP_NE, NULL);
  4300. #ifdef _WIN32
  4301. tt_int_op(mkdir(dir), OP_EQ, 0);
  4302. #else
  4303. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4304. #endif
  4305. tor_asprintf(&invalid_path, "%s"PATH_SEPARATOR"invalid", dir);
  4306. tt_int_op(write_str_to_file(invalid_path, "unclosed \"", 0), OP_EQ, 0);
  4307. char torrc_contents[1000];
  4308. tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s",
  4309. invalid_path);
  4310. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, NULL, NULL),
  4311. OP_EQ, -1);
  4312. done:
  4313. config_free_lines(result);
  4314. tor_free(dir);
  4315. tor_free(invalid_path);
  4316. }
  4317. static void
  4318. test_config_include_empty_file_folder(void *data)
  4319. {
  4320. (void)data;
  4321. config_line_t *result = NULL;
  4322. char *folder_path = NULL;
  4323. char *file_path = NULL;
  4324. char *dir = tor_strdup(get_fname("test_include_empty_file_folder"));
  4325. tt_ptr_op(dir, OP_NE, NULL);
  4326. #ifdef _WIN32
  4327. tt_int_op(mkdir(dir), OP_EQ, 0);
  4328. #else
  4329. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4330. #endif
  4331. tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"empty_dir", dir);
  4332. #ifdef _WIN32
  4333. tt_int_op(mkdir(folder_path), OP_EQ, 0);
  4334. #else
  4335. tt_int_op(mkdir(folder_path, 0700), OP_EQ, 0);
  4336. #endif
  4337. tor_asprintf(&file_path, "%s"PATH_SEPARATOR"empty_file", dir);
  4338. tt_int_op(write_str_to_file(file_path, "", 0), OP_EQ, 0);
  4339. char torrc_contents[1000];
  4340. tor_snprintf(torrc_contents, sizeof(torrc_contents),
  4341. "%%include %s\n"
  4342. "%%include %s\n",
  4343. folder_path, file_path);
  4344. int include_used;
  4345. tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
  4346. NULL), OP_EQ, 0);
  4347. tt_ptr_op(result, OP_EQ, NULL);
  4348. tt_int_op(include_used, OP_EQ, 1);
  4349. done:
  4350. config_free_lines(result);
  4351. tor_free(folder_path);
  4352. tor_free(file_path);
  4353. tor_free(dir);
  4354. }
  4355. #ifndef _WIN32
  4356. static void
  4357. test_config_include_no_permission(void *data)
  4358. {
  4359. (void)data;
  4360. config_line_t *result = NULL;
  4361. char *folder_path = NULL;
  4362. char *dir = NULL;
  4363. if (geteuid() == 0)
  4364. tt_skip();
  4365. dir = tor_strdup(get_fname("test_include_forbidden_folder"));
  4366. tt_ptr_op(dir, OP_NE, NULL);
  4367. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4368. tor_asprintf(&folder_path, "%s"PATH_SEPARATOR"forbidden_dir", dir);
  4369. tt_int_op(mkdir(folder_path, 0100), OP_EQ, 0);
  4370. char torrc_contents[1000];
  4371. tor_snprintf(torrc_contents, sizeof(torrc_contents),
  4372. "%%include %s\n",
  4373. folder_path);
  4374. int include_used;
  4375. tt_int_op(config_get_lines_include(torrc_contents, &result, 0,
  4376. &include_used, NULL),
  4377. OP_EQ, -1);
  4378. tt_ptr_op(result, OP_EQ, NULL);
  4379. done:
  4380. config_free_lines(result);
  4381. tor_free(folder_path);
  4382. if (dir)
  4383. chmod(dir, 0700);
  4384. tor_free(dir);
  4385. }
  4386. #endif /* !defined(_WIN32) */
  4387. static void
  4388. test_config_include_recursion_before_after(void *data)
  4389. {
  4390. (void)data;
  4391. config_line_t *result = NULL;
  4392. char *torrc_path = NULL;
  4393. char *dir = tor_strdup(get_fname("test_include_recursion_before_after"));
  4394. tt_ptr_op(dir, OP_NE, NULL);
  4395. #ifdef _WIN32
  4396. tt_int_op(mkdir(dir), OP_EQ, 0);
  4397. #else
  4398. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4399. #endif
  4400. tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
  4401. char file_contents[1000];
  4402. const int limit = MAX_INCLUDE_RECURSION_LEVEL;
  4403. int i;
  4404. // Loop backwards so file_contents has the contents of the first file by the
  4405. // end of the loop
  4406. for (i = limit; i > 0; i--) {
  4407. if (i < limit) {
  4408. tor_snprintf(file_contents, sizeof(file_contents),
  4409. "Test %d\n"
  4410. "%%include %s%d\n"
  4411. "Test %d\n",
  4412. i, torrc_path, i + 1, 2 * limit - i);
  4413. } else {
  4414. tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", i);
  4415. }
  4416. if (i > 1) {
  4417. char *file_path = NULL;
  4418. tor_asprintf(&file_path, "%s%d", torrc_path, i);
  4419. tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
  4420. tor_free(file_path);
  4421. }
  4422. }
  4423. int include_used;
  4424. tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
  4425. NULL), OP_EQ, 0);
  4426. tt_ptr_op(result, OP_NE, NULL);
  4427. tt_int_op(include_used, OP_EQ, 1);
  4428. int len = 0;
  4429. config_line_t *next;
  4430. for (next = result; next != NULL; next = next->next) {
  4431. char expected[10];
  4432. tor_snprintf(expected, sizeof(expected), "%d", len + 1);
  4433. tt_str_op(next->key, OP_EQ, "Test");
  4434. tt_str_op(next->value, OP_EQ, expected);
  4435. len++;
  4436. }
  4437. tt_int_op(len, OP_EQ, 2 * limit - 1);
  4438. done:
  4439. config_free_lines(result);
  4440. tor_free(dir);
  4441. tor_free(torrc_path);
  4442. }
  4443. static void
  4444. test_config_include_recursion_after_only(void *data)
  4445. {
  4446. (void)data;
  4447. config_line_t *result = NULL;
  4448. char *torrc_path = NULL;
  4449. char *dir = tor_strdup(get_fname("test_include_recursion_after_only"));
  4450. tt_ptr_op(dir, OP_NE, NULL);
  4451. #ifdef _WIN32
  4452. tt_int_op(mkdir(dir), OP_EQ, 0);
  4453. #else
  4454. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4455. #endif
  4456. tor_asprintf(&torrc_path, "%s"PATH_SEPARATOR"torrc", dir);
  4457. char file_contents[1000];
  4458. const int limit = MAX_INCLUDE_RECURSION_LEVEL;
  4459. int i;
  4460. // Loop backwards so file_contents has the contents of the first file by the
  4461. // end of the loop
  4462. for (i = limit; i > 0; i--) {
  4463. int n = (i - limit - 1) * -1;
  4464. if (i < limit) {
  4465. tor_snprintf(file_contents, sizeof(file_contents),
  4466. "%%include %s%d\n"
  4467. "Test %d\n",
  4468. torrc_path, i + 1, n);
  4469. } else {
  4470. tor_snprintf(file_contents, sizeof(file_contents), "Test %d\n", n);
  4471. }
  4472. if (i > 1) {
  4473. char *file_path = NULL;
  4474. tor_asprintf(&file_path, "%s%d", torrc_path, i);
  4475. tt_int_op(write_str_to_file(file_path, file_contents, 0), OP_EQ, 0);
  4476. tor_free(file_path);
  4477. }
  4478. }
  4479. int include_used;
  4480. tt_int_op(config_get_lines_include(file_contents, &result, 0, &include_used,
  4481. NULL), OP_EQ, 0);
  4482. tt_ptr_op(result, OP_NE, NULL);
  4483. tt_int_op(include_used, OP_EQ, 1);
  4484. int len = 0;
  4485. config_line_t *next;
  4486. for (next = result; next != NULL; next = next->next) {
  4487. char expected[10];
  4488. tor_snprintf(expected, sizeof(expected), "%d", len + 1);
  4489. tt_str_op(next->key, OP_EQ, "Test");
  4490. tt_str_op(next->value, OP_EQ, expected);
  4491. len++;
  4492. }
  4493. tt_int_op(len, OP_EQ, limit);
  4494. done:
  4495. config_free_lines(result);
  4496. tor_free(dir);
  4497. tor_free(torrc_path);
  4498. }
  4499. static void
  4500. test_config_include_folder_order(void *data)
  4501. {
  4502. (void)data;
  4503. config_line_t *result = NULL;
  4504. char *torrcd = NULL;
  4505. char *path = NULL;
  4506. char *path2 = NULL;
  4507. char *dir = tor_strdup(get_fname("test_include_folder_order"));
  4508. tt_ptr_op(dir, OP_NE, NULL);
  4509. #ifdef _WIN32
  4510. tt_int_op(mkdir(dir), OP_EQ, 0);
  4511. #else
  4512. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4513. #endif
  4514. tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
  4515. #ifdef _WIN32
  4516. tt_int_op(mkdir(torrcd), OP_EQ, 0);
  4517. #else
  4518. tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
  4519. #endif
  4520. // test that files in subfolders are ignored
  4521. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
  4522. #ifdef _WIN32
  4523. tt_int_op(mkdir(path), OP_EQ, 0);
  4524. #else
  4525. tt_int_op(mkdir(path, 0700), OP_EQ, 0);
  4526. #endif
  4527. tor_asprintf(&path2, "%s"PATH_SEPARATOR"%s", path, "01_ignore");
  4528. tt_int_op(write_str_to_file(path2, "ShouldNotSee 1\n", 0), OP_EQ, 0);
  4529. tor_free(path);
  4530. // test that files starting with . are ignored
  4531. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
  4532. tt_int_op(write_str_to_file(path, "ShouldNotSee 2\n", 0), OP_EQ, 0);
  4533. tor_free(path);
  4534. // test file order
  4535. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "01_1st");
  4536. tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
  4537. tor_free(path);
  4538. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "02_2nd");
  4539. tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
  4540. tor_free(path);
  4541. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_3rd");
  4542. tt_int_op(write_str_to_file(path, "Test 3\n", 0), OP_EQ, 0);
  4543. tor_free(path);
  4544. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "ab_4th");
  4545. tt_int_op(write_str_to_file(path, "Test 4\n", 0), OP_EQ, 0);
  4546. tor_free(path);
  4547. char torrc_contents[1000];
  4548. tor_snprintf(torrc_contents, sizeof(torrc_contents),
  4549. "%%include %s\n",
  4550. torrcd);
  4551. int include_used;
  4552. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
  4553. NULL), OP_EQ, 0);
  4554. tt_ptr_op(result, OP_NE, NULL);
  4555. tt_int_op(include_used, OP_EQ, 1);
  4556. int len = 0;
  4557. config_line_t *next;
  4558. for (next = result; next != NULL; next = next->next) {
  4559. char expected[10];
  4560. tor_snprintf(expected, sizeof(expected), "%d", len + 1);
  4561. tt_str_op(next->key, OP_EQ, "Test");
  4562. tt_str_op(next->value, OP_EQ, expected);
  4563. len++;
  4564. }
  4565. tt_int_op(len, OP_EQ, 4);
  4566. done:
  4567. config_free_lines(result);
  4568. tor_free(torrcd);
  4569. tor_free(path);
  4570. tor_free(path2);
  4571. tor_free(dir);
  4572. }
  4573. static void
  4574. test_config_include_blank_file_last(void *data)
  4575. {
  4576. (void)data;
  4577. config_line_t *result = NULL;
  4578. char *torrcd = NULL;
  4579. char *path = NULL;
  4580. char *dir = tor_strdup(get_fname("test_include_blank_file_last"));
  4581. tt_ptr_op(dir, OP_NE, NULL);
  4582. #ifdef _WIN32
  4583. tt_int_op(mkdir(dir), OP_EQ, 0);
  4584. #else
  4585. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4586. #endif
  4587. tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
  4588. #ifdef _WIN32
  4589. tt_int_op(mkdir(torrcd), OP_EQ, 0);
  4590. #else
  4591. tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
  4592. #endif
  4593. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "aa_1st");
  4594. tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
  4595. tor_free(path);
  4596. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "bb_2nd");
  4597. tt_int_op(write_str_to_file(path, "Test 2\n", 0), OP_EQ, 0);
  4598. tor_free(path);
  4599. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", torrcd, "cc_comment");
  4600. tt_int_op(write_str_to_file(path, "# comment only\n", 0), OP_EQ, 0);
  4601. tor_free(path);
  4602. char torrc_contents[1000];
  4603. tor_snprintf(torrc_contents, sizeof(torrc_contents),
  4604. "%%include %s\n"
  4605. "Test 3\n",
  4606. torrcd);
  4607. int include_used;
  4608. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
  4609. NULL), OP_EQ, 0);
  4610. tt_ptr_op(result, OP_NE, NULL);
  4611. tt_int_op(include_used, OP_EQ, 1);
  4612. int len = 0;
  4613. config_line_t *next;
  4614. for (next = result; next != NULL; next = next->next) {
  4615. char expected[10];
  4616. tor_snprintf(expected, sizeof(expected), "%d", len + 1);
  4617. tt_str_op(next->key, OP_EQ, "Test");
  4618. tt_str_op(next->value, OP_EQ, expected);
  4619. len++;
  4620. }
  4621. tt_int_op(len, OP_EQ, 3);
  4622. done:
  4623. config_free_lines(result);
  4624. tor_free(torrcd);
  4625. tor_free(path);
  4626. tor_free(dir);
  4627. }
  4628. static void
  4629. test_config_include_path_syntax(void *data)
  4630. {
  4631. (void)data;
  4632. config_line_t *result = NULL;
  4633. char *dir = tor_strdup(get_fname("test_include_path_syntax"));
  4634. char *esc_dir = NULL, *dir_with_pathsep = NULL,
  4635. *esc_dir_with_pathsep = NULL, *torrc_contents = NULL;
  4636. tt_ptr_op(dir, OP_NE, NULL);
  4637. #ifdef _WIN32
  4638. tt_int_op(mkdir(dir), OP_EQ, 0);
  4639. #else
  4640. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4641. #endif
  4642. esc_dir = esc_for_log(dir);
  4643. tor_asprintf(&dir_with_pathsep, "%s%s", dir, PATH_SEPARATOR);
  4644. esc_dir_with_pathsep = esc_for_log(dir_with_pathsep);
  4645. tor_asprintf(&torrc_contents,
  4646. "%%include %s\n"
  4647. "%%include %s%s \n" // space to avoid suppressing newline
  4648. "%%include %s\n",
  4649. esc_dir,
  4650. dir, PATH_SEPARATOR,
  4651. esc_dir_with_pathsep);
  4652. int include_used;
  4653. tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
  4654. NULL), OP_EQ, 0);
  4655. tt_ptr_op(result, OP_EQ, NULL);
  4656. tt_int_op(include_used, OP_EQ, 1);
  4657. done:
  4658. config_free_lines(result);
  4659. tor_free(dir);
  4660. tor_free(torrc_contents);
  4661. tor_free(esc_dir);
  4662. tor_free(dir_with_pathsep);
  4663. tor_free(esc_dir_with_pathsep);
  4664. }
  4665. static void
  4666. test_config_include_not_processed(void *data)
  4667. {
  4668. (void)data;
  4669. char torrc_contents[1000] = "%include does_not_exist\n";
  4670. config_line_t *result = NULL;
  4671. tt_int_op(config_get_lines(torrc_contents, &result, 0),OP_EQ, 0);
  4672. tt_ptr_op(result, OP_NE, NULL);
  4673. int len = 0;
  4674. config_line_t *next;
  4675. for (next = result; next != NULL; next = next->next) {
  4676. tt_str_op(next->key, OP_EQ, "%include");
  4677. tt_str_op(next->value, OP_EQ, "does_not_exist");
  4678. len++;
  4679. }
  4680. tt_int_op(len, OP_EQ, 1);
  4681. done:
  4682. config_free_lines(result);
  4683. }
  4684. static void
  4685. test_config_include_has_include(void *data)
  4686. {
  4687. (void)data;
  4688. config_line_t *result = NULL;
  4689. char *dir = tor_strdup(get_fname("test_include_has_include"));
  4690. tt_ptr_op(dir, OP_NE, NULL);
  4691. #ifdef _WIN32
  4692. tt_int_op(mkdir(dir), OP_EQ, 0);
  4693. #else
  4694. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4695. #endif
  4696. char torrc_contents[1000] = "Test 1\n";
  4697. int include_used;
  4698. tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
  4699. NULL), OP_EQ, 0);
  4700. tt_int_op(include_used, OP_EQ, 0);
  4701. config_free_lines(result);
  4702. tor_snprintf(torrc_contents, sizeof(torrc_contents), "%%include %s\n", dir);
  4703. tt_int_op(config_get_lines_include(torrc_contents, &result, 0,&include_used,
  4704. NULL), OP_EQ, 0);
  4705. tt_int_op(include_used, OP_EQ, 1);
  4706. done:
  4707. config_free_lines(result);
  4708. tor_free(dir);
  4709. }
  4710. static void
  4711. test_config_include_flag_both_without(void *data)
  4712. {
  4713. (void)data;
  4714. char *errmsg = NULL;
  4715. char conf_empty[1000];
  4716. tor_snprintf(conf_empty, sizeof(conf_empty),
  4717. "DataDirectory %s\n",
  4718. get_fname(NULL));
  4719. // test with defaults-torrc and torrc without include
  4720. int ret = options_init_from_string(conf_empty, conf_empty, CMD_RUN_UNITTESTS,
  4721. NULL, &errmsg);
  4722. tt_int_op(ret, OP_EQ, 0);
  4723. const or_options_t *options = get_options();
  4724. tt_int_op(options->IncludeUsed, OP_EQ, 0);
  4725. done:
  4726. tor_free(errmsg);
  4727. }
  4728. static void
  4729. test_config_include_flag_torrc_only(void *data)
  4730. {
  4731. (void)data;
  4732. char *errmsg = NULL;
  4733. char *path = NULL;
  4734. char *dir = tor_strdup(get_fname("test_include_flag_torrc_only"));
  4735. tt_ptr_op(dir, OP_NE, NULL);
  4736. #ifdef _WIN32
  4737. tt_int_op(mkdir(dir), OP_EQ, 0);
  4738. #else
  4739. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4740. #endif
  4741. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
  4742. tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
  4743. char conf_empty[1000];
  4744. tor_snprintf(conf_empty, sizeof(conf_empty),
  4745. "DataDirectory %s\n",
  4746. get_fname(NULL));
  4747. char conf_include[1000];
  4748. tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
  4749. // test with defaults-torrc without include and torrc with include
  4750. int ret = options_init_from_string(conf_empty, conf_include,
  4751. CMD_RUN_UNITTESTS, NULL, &errmsg);
  4752. tt_int_op(ret, OP_EQ, 0);
  4753. const or_options_t *options = get_options();
  4754. tt_int_op(options->IncludeUsed, OP_EQ, 1);
  4755. done:
  4756. tor_free(errmsg);
  4757. tor_free(path);
  4758. tor_free(dir);
  4759. }
  4760. static void
  4761. test_config_include_flag_defaults_only(void *data)
  4762. {
  4763. (void)data;
  4764. char *errmsg = NULL;
  4765. char *path = NULL;
  4766. char *dir = tor_strdup(get_fname("test_include_flag_defaults_only"));
  4767. tt_ptr_op(dir, OP_NE, NULL);
  4768. #ifdef _WIN32
  4769. tt_int_op(mkdir(dir), OP_EQ, 0);
  4770. #else
  4771. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4772. #endif
  4773. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", dir, "dummy");
  4774. tt_int_op(write_str_to_file(path, "\n", 0), OP_EQ, 0);
  4775. char conf_empty[1000];
  4776. tor_snprintf(conf_empty, sizeof(conf_empty),
  4777. "DataDirectory %s\n",
  4778. get_fname(NULL));
  4779. char conf_include[1000];
  4780. tor_snprintf(conf_include, sizeof(conf_include), "%%include %s", path);
  4781. // test with defaults-torrc with include and torrc without include
  4782. int ret = options_init_from_string(conf_include, conf_empty,
  4783. CMD_RUN_UNITTESTS, NULL, &errmsg);
  4784. tt_int_op(ret, OP_EQ, 0);
  4785. const or_options_t *options = get_options();
  4786. tt_int_op(options->IncludeUsed, OP_EQ, 0);
  4787. done:
  4788. tor_free(errmsg);
  4789. tor_free(path);
  4790. tor_free(dir);
  4791. }
  4792. static void
  4793. test_config_dup_and_filter(void *arg)
  4794. {
  4795. (void)arg;
  4796. /* Test normal input. */
  4797. config_line_t *line = NULL;
  4798. config_line_append(&line, "abc", "def");
  4799. config_line_append(&line, "ghi", "jkl");
  4800. config_line_append(&line, "ABCD", "mno");
  4801. config_line_t *line_dup = config_lines_dup_and_filter(line, "aBc");
  4802. tt_ptr_op(line_dup, OP_NE, NULL);
  4803. tt_ptr_op(line_dup->next, OP_NE, NULL);
  4804. tt_ptr_op(line_dup->next->next, OP_EQ, NULL);
  4805. tt_str_op(line_dup->key, OP_EQ, "abc");
  4806. tt_str_op(line_dup->value, OP_EQ, "def");
  4807. tt_str_op(line_dup->next->key, OP_EQ, "ABCD");
  4808. tt_str_op(line_dup->next->value, OP_EQ, "mno");
  4809. /* empty output */
  4810. config_free_lines(line_dup);
  4811. line_dup = config_lines_dup_and_filter(line, "skdjfsdkljf");
  4812. tt_ptr_op(line_dup, OP_EQ, NULL);
  4813. /* empty input */
  4814. config_free_lines(line_dup);
  4815. line_dup = config_lines_dup_and_filter(NULL, "abc");
  4816. tt_ptr_op(line_dup, OP_EQ, NULL);
  4817. done:
  4818. config_free_lines(line);
  4819. config_free_lines(line_dup);
  4820. }
  4821. /* If we're not configured to be a bridge, but we set
  4822. * BridgeDistribution, then options_validate () should return -1. */
  4823. static void
  4824. test_config_check_bridge_distribution_setting_not_a_bridge(void *arg)
  4825. {
  4826. or_options_t* options = get_options_mutable();
  4827. or_options_t* old_options = options;
  4828. or_options_t* default_options = options;
  4829. char* message = NULL;
  4830. int ret;
  4831. (void)arg;
  4832. options->BridgeRelay = 0;
  4833. options->BridgeDistribution = (char*)("https");
  4834. ret = options_validate(old_options, options, default_options, 0, &message);
  4835. tt_int_op(ret, OP_EQ, -1);
  4836. tt_str_op(message, OP_EQ, "You set BridgeDistribution, but you "
  4837. "didn't set BridgeRelay!");
  4838. done:
  4839. tor_free(message);
  4840. options->BridgeDistribution = NULL;
  4841. }
  4842. /* If the BridgeDistribution setting was valid, 0 should be returned. */
  4843. static void
  4844. test_config_check_bridge_distribution_setting_valid(void *arg)
  4845. {
  4846. int ret = check_bridge_distribution_setting("https");
  4847. (void)arg;
  4848. tt_int_op(ret, OP_EQ, 0);
  4849. done:
  4850. return;
  4851. }
  4852. /* If the BridgeDistribution setting was invalid, -1 should be returned. */
  4853. static void
  4854. test_config_check_bridge_distribution_setting_invalid(void *arg)
  4855. {
  4856. int ret = check_bridge_distribution_setting("hyphens-are-allowed");
  4857. (void)arg;
  4858. tt_int_op(ret, OP_EQ, 0);
  4859. ret = check_bridge_distribution_setting("asterisks*are*forbidden");
  4860. tt_int_op(ret, OP_EQ, -1);
  4861. done:
  4862. return;
  4863. }
  4864. /* If the BridgeDistribution setting was unrecognised, a warning should be
  4865. * logged and 0 should be returned. */
  4866. static void
  4867. test_config_check_bridge_distribution_setting_unrecognised(void *arg)
  4868. {
  4869. int ret = check_bridge_distribution_setting("unicorn");
  4870. (void)arg;
  4871. tt_int_op(ret, OP_EQ, 0);
  4872. done:
  4873. return;
  4874. }
  4875. static void
  4876. test_config_include_opened_file_list(void *data)
  4877. {
  4878. (void)data;
  4879. config_line_t *result = NULL;
  4880. smartlist_t *opened_files = smartlist_new();
  4881. char *torrcd = NULL;
  4882. char *subfolder = NULL;
  4883. char *path = NULL;
  4884. char *empty = NULL;
  4885. char *file = NULL;
  4886. char *dot = NULL;
  4887. char *dir = tor_strdup(get_fname("test_include_opened_file_list"));
  4888. tt_ptr_op(dir, OP_NE, NULL);
  4889. #ifdef _WIN32
  4890. tt_int_op(mkdir(dir), OP_EQ, 0);
  4891. #else
  4892. tt_int_op(mkdir(dir, 0700), OP_EQ, 0);
  4893. #endif
  4894. tor_asprintf(&torrcd, "%s"PATH_SEPARATOR"%s", dir, "torrc.d");
  4895. #ifdef _WIN32
  4896. tt_int_op(mkdir(torrcd), OP_EQ, 0);
  4897. #else
  4898. tt_int_op(mkdir(torrcd, 0700), OP_EQ, 0);
  4899. #endif
  4900. tor_asprintf(&subfolder, "%s"PATH_SEPARATOR"%s", torrcd, "subfolder");
  4901. #ifdef _WIN32
  4902. tt_int_op(mkdir(subfolder), OP_EQ, 0);
  4903. #else
  4904. tt_int_op(mkdir(subfolder, 0700), OP_EQ, 0);
  4905. #endif
  4906. tor_asprintf(&path, "%s"PATH_SEPARATOR"%s", subfolder,
  4907. "01_file_in_subfolder");
  4908. tt_int_op(write_str_to_file(path, "Test 1\n", 0), OP_EQ, 0);
  4909. tor_asprintf(&empty, "%s"PATH_SEPARATOR"%s", torrcd, "empty");
  4910. tt_int_op(write_str_to_file(empty, "", 0), OP_EQ, 0);
  4911. tor_asprintf(&file, "%s"PATH_SEPARATOR"%s", torrcd, "file");
  4912. tt_int_op(write_str_to_file(file, "Test 2\n", 0), OP_EQ, 0);
  4913. tor_asprintf(&dot, "%s"PATH_SEPARATOR"%s", torrcd, ".dot");
  4914. tt_int_op(write_str_to_file(dot, "Test 3\n", 0), OP_EQ, 0);
  4915. char torrc_contents[1000];
  4916. tor_snprintf(torrc_contents, sizeof(torrc_contents),
  4917. "%%include %s\n",
  4918. torrcd);
  4919. int include_used;
  4920. tt_int_op(config_get_lines_include(torrc_contents, &result, 0, &include_used,
  4921. opened_files), OP_EQ, 0);
  4922. tt_ptr_op(result, OP_NE, NULL);
  4923. tt_int_op(include_used, OP_EQ, 1);
  4924. tt_int_op(smartlist_len(opened_files), OP_EQ, 4);
  4925. tt_int_op(smartlist_contains_string(opened_files, torrcd), OP_EQ, 1);
  4926. tt_int_op(smartlist_contains_string(opened_files, subfolder), OP_EQ, 1);
  4927. // files inside subfolders are not opended, only the subfolder is opened
  4928. tt_int_op(smartlist_contains_string(opened_files, empty), OP_EQ, 1);
  4929. tt_int_op(smartlist_contains_string(opened_files, file), OP_EQ, 1);
  4930. // dot files are not opened as we ignore them when we get their name from
  4931. // their parent folder
  4932. done:
  4933. SMARTLIST_FOREACH(opened_files, char *, f, tor_free(f));
  4934. smartlist_free(opened_files);
  4935. config_free_lines(result);
  4936. tor_free(torrcd);
  4937. tor_free(subfolder);
  4938. tor_free(path);
  4939. tor_free(empty);
  4940. tor_free(file);
  4941. tor_free(dot);
  4942. tor_free(dir);
  4943. }
  4944. static void
  4945. test_config_compute_max_mem_in_queues(void *data)
  4946. {
  4947. #define GIGABYTE(x) (UINT64_C(x) << 30)
  4948. #define MEGABYTE(x) (UINT64_C(x) << 20)
  4949. (void)data;
  4950. MOCK(get_total_system_memory, get_total_system_memory_mock);
  4951. /* We are unable to detect the amount of memory on the system. Tor will try
  4952. * to use some sensible default values for 64-bit and 32-bit systems. */
  4953. total_system_memory_return = -1;
  4954. #if SIZEOF_VOID_P >= 8
  4955. /* We are on a 64-bit system. */
  4956. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(8));
  4957. #else
  4958. /* We are on a 32-bit system. */
  4959. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, GIGABYTE(1));
  4960. #endif /* SIZEOF_VOID_P >= 8 */
  4961. /* We are able to detect the amount of RAM on the system. */
  4962. total_system_memory_return = 0;
  4963. /* We are running on a system with one gigabyte of RAM. */
  4964. total_system_memory_output = GIGABYTE(1);
  4965. /* We have 0.75 * RAM available. */
  4966. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
  4967. 3 * (GIGABYTE(1) / 4));
  4968. /* We are running on a tiny machine with 256 MB of RAM. */
  4969. total_system_memory_output = MEGABYTE(256);
  4970. /* We will now enforce a minimum of 256 MB of RAM available for the
  4971. * MaxMemInQueues here, even though we should only have had 0.75 * 256 = 192
  4972. * MB available. */
  4973. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ, MEGABYTE(256));
  4974. #if SIZEOF_SIZE_T > 4
  4975. /* We are running on a machine with 8 GB of RAM. */
  4976. total_system_memory_output = GIGABYTE(8);
  4977. /* We will have 0.4 * RAM available. */
  4978. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
  4979. 2 * (GIGABYTE(8) / 5));
  4980. /* We are running on a machine with 16 GB of RAM. */
  4981. total_system_memory_output = GIGABYTE(16);
  4982. /* We will have 0.4 * RAM available. */
  4983. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
  4984. 2 * (GIGABYTE(16) / 5));
  4985. /* We are running on a machine with 32 GB of RAM. */
  4986. total_system_memory_output = GIGABYTE(32);
  4987. /* We will at maximum get MAX_DEFAULT_MEMORY_QUEUE_SIZE here. */
  4988. tt_u64_op(compute_real_max_mem_in_queues(0, 0), OP_EQ,
  4989. MAX_DEFAULT_MEMORY_QUEUE_SIZE);
  4990. #endif /* SIZEOF_SIZE_T > 4 */
  4991. done:
  4992. UNMOCK(get_total_system_memory);
  4993. #undef GIGABYTE
  4994. #undef MEGABYTE
  4995. }
  4996. static void
  4997. test_config_extended_fmt(void *arg)
  4998. {
  4999. (void)arg;
  5000. config_line_t *lines = NULL, *lp;
  5001. const char string1[] =
  5002. "thing1 is here\n"
  5003. "+thing2 is over here\n"
  5004. "/thing3\n"
  5005. "/thing4 is back here\n";
  5006. /* Try with the "extended" flag disabled. */
  5007. int r = config_get_lines(string1, &lines, 0);
  5008. tt_int_op(r, OP_EQ, 0);
  5009. lp = lines;
  5010. tt_ptr_op(lp, OP_NE, NULL);
  5011. tt_str_op(lp->key, OP_EQ, "thing1");
  5012. tt_str_op(lp->value, OP_EQ, "is here");
  5013. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
  5014. lp = lp->next;
  5015. tt_ptr_op(lp, OP_NE, NULL);
  5016. tt_str_op(lp->key, OP_EQ, "+thing2");
  5017. tt_str_op(lp->value, OP_EQ, "is over here");
  5018. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
  5019. lp = lp->next;
  5020. tt_ptr_op(lp, OP_NE, NULL);
  5021. tt_str_op(lp->key, OP_EQ, "/thing3");
  5022. tt_str_op(lp->value, OP_EQ, "");
  5023. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
  5024. lp = lp->next;
  5025. tt_ptr_op(lp, OP_NE, NULL);
  5026. tt_str_op(lp->key, OP_EQ, "/thing4");
  5027. tt_str_op(lp->value, OP_EQ, "is back here");
  5028. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
  5029. lp = lp->next;
  5030. tt_assert(!lp);
  5031. config_free_lines(lines);
  5032. /* Try with the "extended" flag enabled. */
  5033. r = config_get_lines(string1, &lines, 1);
  5034. tt_int_op(r, OP_EQ, 0);
  5035. lp = lines;
  5036. tt_ptr_op(lp, OP_NE, NULL);
  5037. tt_str_op(lp->key, OP_EQ, "thing1");
  5038. tt_str_op(lp->value, OP_EQ, "is here");
  5039. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_NORMAL);
  5040. lp = lp->next;
  5041. tt_ptr_op(lp, OP_NE, NULL);
  5042. tt_str_op(lp->key, OP_EQ, "thing2");
  5043. tt_str_op(lp->value, OP_EQ, "is over here");
  5044. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_APPEND);
  5045. lp = lp->next;
  5046. tt_ptr_op(lp, OP_NE, NULL);
  5047. tt_str_op(lp->key, OP_EQ, "thing3");
  5048. tt_str_op(lp->value, OP_EQ, "");
  5049. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
  5050. lp = lp->next;
  5051. tt_ptr_op(lp, OP_NE, NULL);
  5052. tt_str_op(lp->key, OP_EQ, "thing4");
  5053. tt_str_op(lp->value, OP_EQ, "");
  5054. tt_int_op(lp->command, OP_EQ, CONFIG_LINE_CLEAR);
  5055. lp = lp->next;
  5056. tt_assert(!lp);
  5057. done:
  5058. config_free_lines(lines);
  5059. }
  5060. static void
  5061. test_config_kvline_parse(void *arg)
  5062. {
  5063. (void)arg;
  5064. config_line_t *lines = NULL;
  5065. char *enc = NULL;
  5066. lines = kvline_parse("A=B CD=EF", 0);
  5067. tt_assert(lines);
  5068. tt_str_op(lines->key, OP_EQ, "A");
  5069. tt_str_op(lines->value, OP_EQ, "B");
  5070. tt_str_op(lines->next->key, OP_EQ, "CD");
  5071. tt_str_op(lines->next->value, OP_EQ, "EF");
  5072. enc = kvline_encode(lines, 0);
  5073. tt_str_op(enc, OP_EQ, "A=B CD=EF");
  5074. tor_free(enc);
  5075. enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
  5076. tt_str_op(enc, OP_EQ, "A=B CD=EF");
  5077. tor_free(enc);
  5078. config_free_lines(lines);
  5079. lines = kvline_parse("AB CDE=F", 0);
  5080. tt_assert(! lines);
  5081. lines = kvline_parse("AB CDE=F", KV_OMIT_KEYS);
  5082. tt_assert(lines);
  5083. tt_str_op(lines->key, OP_EQ, "");
  5084. tt_str_op(lines->value, OP_EQ, "AB");
  5085. tt_str_op(lines->next->key, OP_EQ, "CDE");
  5086. tt_str_op(lines->next->value, OP_EQ, "F");
  5087. tt_assert(lines);
  5088. enc = kvline_encode(lines, 0);
  5089. tt_assert(!enc);
  5090. enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
  5091. tt_str_op(enc, OP_EQ, "AB CDE=F");
  5092. tor_free(enc);
  5093. config_free_lines(lines);
  5094. lines = kvline_parse("AB=C CDE=\"F G\"", 0);
  5095. tt_assert(!lines);
  5096. lines = kvline_parse("AB=C CDE=\"F G\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
  5097. tt_assert(lines);
  5098. tt_str_op(lines->key, OP_EQ, "AB");
  5099. tt_str_op(lines->value, OP_EQ, "C");
  5100. tt_str_op(lines->next->key, OP_EQ, "CDE");
  5101. tt_str_op(lines->next->value, OP_EQ, "F G");
  5102. tt_str_op(lines->next->next->key, OP_EQ, "");
  5103. tt_str_op(lines->next->next->value, OP_EQ, "GHI");
  5104. enc = kvline_encode(lines, 0);
  5105. tt_assert(!enc);
  5106. enc = kvline_encode(lines, KV_QUOTED|KV_OMIT_KEYS);
  5107. tt_str_op(enc, OP_EQ, "AB=C CDE=\"F G\" GHI");
  5108. tor_free(enc);
  5109. config_free_lines(lines);
  5110. lines = kvline_parse("A\"B=C CDE=\"F\" \"GHI\" ", KV_QUOTED|KV_OMIT_KEYS);
  5111. tt_assert(! lines);
  5112. lines = kvline_parse("AB=", KV_QUOTED);
  5113. tt_assert(lines);
  5114. tt_str_op(lines->key, OP_EQ, "AB");
  5115. tt_str_op(lines->value, OP_EQ, "");
  5116. config_free_lines(lines);
  5117. lines = kvline_parse("AB=", 0);
  5118. tt_assert(lines);
  5119. tt_str_op(lines->key, OP_EQ, "AB");
  5120. tt_str_op(lines->value, OP_EQ, "");
  5121. config_free_lines(lines);
  5122. lines = kvline_parse("AB=", KV_OMIT_VALS);
  5123. tt_assert(lines);
  5124. tt_str_op(lines->key, OP_EQ, "AB");
  5125. tt_str_op(lines->value, OP_EQ, "");
  5126. config_free_lines(lines);
  5127. lines = kvline_parse(" AB ", KV_OMIT_VALS);
  5128. tt_assert(lines);
  5129. tt_str_op(lines->key, OP_EQ, "AB");
  5130. tt_str_op(lines->value, OP_EQ, "");
  5131. config_free_lines(lines);
  5132. lines = kvline_parse("AB", KV_OMIT_VALS);
  5133. tt_assert(lines);
  5134. tt_str_op(lines->key, OP_EQ, "AB");
  5135. tt_str_op(lines->value, OP_EQ, "");
  5136. enc = kvline_encode(lines, KV_OMIT_VALS);
  5137. tt_str_op(enc, OP_EQ, "AB");
  5138. tor_free(enc);
  5139. config_free_lines(lines);
  5140. lines = kvline_parse("AB=CD", KV_OMIT_VALS);
  5141. tt_assert(lines);
  5142. tt_str_op(lines->key, OP_EQ, "AB");
  5143. tt_str_op(lines->value, OP_EQ, "CD");
  5144. enc = kvline_encode(lines, KV_OMIT_VALS);
  5145. tt_str_op(enc, OP_EQ, "AB=CD");
  5146. tor_free(enc);
  5147. config_free_lines(lines);
  5148. lines = kvline_parse("AB=CD DE FGH=I", KV_OMIT_VALS);
  5149. tt_assert(lines);
  5150. tt_str_op(lines->key, OP_EQ, "AB");
  5151. tt_str_op(lines->value, OP_EQ, "CD");
  5152. tt_str_op(lines->next->key, OP_EQ, "DE");
  5153. tt_str_op(lines->next->value, OP_EQ, "");
  5154. tt_str_op(lines->next->next->key, OP_EQ, "FGH");
  5155. tt_str_op(lines->next->next->value, OP_EQ, "I");
  5156. enc = kvline_encode(lines, KV_OMIT_VALS);
  5157. tt_str_op(enc, OP_EQ, "AB=CD DE FGH=I");
  5158. tor_free(enc);
  5159. config_free_lines(lines);
  5160. lines = kvline_parse("AB=\"CD E\" DE FGH=\"I\"", KV_OMIT_VALS|KV_QUOTED);
  5161. tt_assert(lines);
  5162. tt_str_op(lines->key, OP_EQ, "AB");
  5163. tt_str_op(lines->value, OP_EQ, "CD E");
  5164. tt_str_op(lines->next->key, OP_EQ, "DE");
  5165. tt_str_op(lines->next->value, OP_EQ, "");
  5166. tt_str_op(lines->next->next->key, OP_EQ, "FGH");
  5167. tt_str_op(lines->next->next->value, OP_EQ, "I");
  5168. enc = kvline_encode(lines, KV_OMIT_VALS|KV_QUOTED);
  5169. tt_str_op(enc, OP_EQ, "AB=\"CD E\" DE FGH=I");
  5170. done:
  5171. config_free_lines(lines);
  5172. tor_free(enc);
  5173. }
  5174. static void
  5175. test_config_getinfo_config_names(void *arg)
  5176. {
  5177. (void)arg;
  5178. char *answer = NULL;
  5179. const char *error = NULL;
  5180. int rv;
  5181. rv = getinfo_helper_config(NULL, "config/names", &answer, &error);
  5182. tt_int_op(rv, OP_EQ, 0);
  5183. tt_ptr_op(error, OP_EQ, NULL);
  5184. // ContactInfo should be listed.
  5185. tt_assert(strstr(answer, "\nContactInfo String\n"));
  5186. // V1AuthoritativeDirectory should not be listed, since it is obsolete.
  5187. tt_assert(! strstr(answer, "V1AuthoritativeDirectory"));
  5188. // ___UsingTestNetworkDefaults should not be listed, since it is invisible.
  5189. tt_assert(! strstr(answer, "UsingTestNetworkDefaults"));
  5190. done:
  5191. tor_free(answer);
  5192. }
  5193. #define CONFIG_TEST(name, flags) \
  5194. { #name, test_config_ ## name, flags, NULL, NULL }
  5195. struct testcase_t config_tests[] = {
  5196. CONFIG_TEST(adding_trusted_dir_server, TT_FORK),
  5197. CONFIG_TEST(adding_fallback_dir_server, TT_FORK),
  5198. CONFIG_TEST(parsing_trusted_dir_server, 0),
  5199. CONFIG_TEST(parsing_invalid_dir_address, 0),
  5200. CONFIG_TEST(parsing_fallback_dir_server, 0),
  5201. CONFIG_TEST(adding_default_trusted_dir_servers, TT_FORK),
  5202. CONFIG_TEST(adding_dir_servers, TT_FORK),
  5203. CONFIG_TEST(default_dir_servers, TT_FORK),
  5204. CONFIG_TEST(default_fallback_dirs, 0),
  5205. CONFIG_TEST(resolve_my_address, TT_FORK),
  5206. CONFIG_TEST(addressmap, 0),
  5207. CONFIG_TEST(parse_bridge_line, 0),
  5208. CONFIG_TEST(parse_transport_options_line, 0),
  5209. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  5210. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  5211. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  5212. CONFIG_TEST(fix_my_family, 0),
  5213. CONFIG_TEST(directory_fetch, 0),
  5214. CONFIG_TEST(port_cfg_line_extract_addrport, 0),
  5215. CONFIG_TEST(parse_port_config__ports__no_ports_given, 0),
  5216. CONFIG_TEST(parse_port_config__ports__server_options, 0),
  5217. CONFIG_TEST(parse_port_config__ports__ports_given, 0),
  5218. CONFIG_TEST(parse_log_severity, 0),
  5219. CONFIG_TEST(include_limit, 0),
  5220. CONFIG_TEST(include_does_not_exist, 0),
  5221. CONFIG_TEST(include_error_in_included_file, 0),
  5222. CONFIG_TEST(include_empty_file_folder, 0),
  5223. #ifndef _WIN32
  5224. CONFIG_TEST(include_no_permission, 0),
  5225. #endif
  5226. CONFIG_TEST(include_recursion_before_after, 0),
  5227. CONFIG_TEST(include_recursion_after_only, 0),
  5228. CONFIG_TEST(include_folder_order, 0),
  5229. CONFIG_TEST(include_blank_file_last, 0),
  5230. CONFIG_TEST(include_path_syntax, 0),
  5231. CONFIG_TEST(include_not_processed, 0),
  5232. CONFIG_TEST(include_has_include, 0),
  5233. CONFIG_TEST(include_flag_both_without, TT_FORK),
  5234. CONFIG_TEST(include_flag_torrc_only, TT_FORK),
  5235. CONFIG_TEST(include_flag_defaults_only, TT_FORK),
  5236. CONFIG_TEST(dup_and_filter, 0),
  5237. CONFIG_TEST(check_bridge_distribution_setting_not_a_bridge, TT_FORK),
  5238. CONFIG_TEST(check_bridge_distribution_setting_valid, 0),
  5239. CONFIG_TEST(check_bridge_distribution_setting_invalid, 0),
  5240. CONFIG_TEST(check_bridge_distribution_setting_unrecognised, 0),
  5241. CONFIG_TEST(include_opened_file_list, 0),
  5242. CONFIG_TEST(compute_max_mem_in_queues, 0),
  5243. CONFIG_TEST(extended_fmt, 0),
  5244. CONFIG_TEST(kvline_parse, 0),
  5245. CONFIG_TEST(getinfo_config_names, 0),
  5246. END_OF_TESTCASES
  5247. };