test_config.c 201 KB

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