test_config.c 199 KB

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