test_config.c 197 KB

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