test_config.c 200 KB

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