test_config.c 209 KB

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