test_options.c 175 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define CONFIG_PRIVATE
  6. #include "or.h"
  7. #include "confparse.h"
  8. #include "config.h"
  9. #include "test.h"
  10. #include "geoip.h"
  11. #define ROUTERSET_PRIVATE
  12. #include "routerset.h"
  13. #include "main.h"
  14. #include "log_test_helpers.h"
  15. #include "sandbox.h"
  16. #include "memarea.h"
  17. #include "policies.h"
  18. #define NS_MODULE test_options
  19. typedef struct {
  20. int severity;
  21. uint32_t domain;
  22. char *msg;
  23. } logmsg_t;
  24. static smartlist_t *messages = NULL;
  25. static void
  26. log_cback(int severity, uint32_t domain, const char *msg)
  27. {
  28. logmsg_t *x = tor_malloc(sizeof(*x));
  29. x->severity = severity;
  30. x->domain = domain;
  31. x->msg = tor_strdup(msg);
  32. if (!messages)
  33. messages = smartlist_new();
  34. smartlist_add(messages, x);
  35. }
  36. static void
  37. setup_log_callback(void)
  38. {
  39. log_severity_list_t lst;
  40. memset(&lst, 0, sizeof(lst));
  41. lst.masks[LOG_ERR - LOG_ERR] = ~0;
  42. lst.masks[LOG_WARN - LOG_ERR] = ~0;
  43. lst.masks[LOG_NOTICE - LOG_ERR] = ~0;
  44. add_callback_log(&lst, log_cback);
  45. mark_logs_temp();
  46. }
  47. static char *
  48. dump_logs(void)
  49. {
  50. smartlist_t *msgs;
  51. char *out;
  52. if (! messages)
  53. return tor_strdup("");
  54. msgs = smartlist_new();
  55. SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, x) {
  56. smartlist_add_asprintf(msgs, "[%s] %s",
  57. log_level_to_string(x->severity), x->msg);
  58. } SMARTLIST_FOREACH_END(x);
  59. out = smartlist_join_strings(msgs, "", 0, NULL);
  60. SMARTLIST_FOREACH(msgs, char *, cp, tor_free(cp));
  61. smartlist_free(msgs);
  62. return out;
  63. }
  64. static void
  65. clear_log_messages(void)
  66. {
  67. if (!messages)
  68. return;
  69. SMARTLIST_FOREACH(messages, logmsg_t *, m,
  70. { tor_free(m->msg); tor_free(m); });
  71. smartlist_free(messages);
  72. messages = NULL;
  73. }
  74. #define setup_options(opt,dflt) \
  75. do { \
  76. opt = options_new(); \
  77. opt->command = CMD_RUN_TOR; \
  78. options_init(opt); \
  79. \
  80. dflt = config_dup(&options_format, opt); \
  81. clear_log_messages(); \
  82. } while (0)
  83. #define VALID_DIR_AUTH "DirAuthority dizum orport=443 v3ident=E8A9C45" \
  84. "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
  85. " 083C 538F 4403 8BBF A077 587D D755\n"
  86. #define VALID_ALT_BRIDGE_AUTH \
  87. "AlternateBridgeAuthority dizum orport=443 v3ident=E8A9C45" \
  88. "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
  89. " 083C 538F 4403 8BBF A077 587D D755\n"
  90. #define VALID_ALT_DIR_AUTH \
  91. "AlternateDirAuthority dizum orport=443 v3ident=E8A9C45" \
  92. "EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
  93. " 083C 538F 4403 8BBF A077 587D D755\n"
  94. static void
  95. test_options_validate_impl(const char *configuration,
  96. const char *expect_errmsg,
  97. int expect_log_severity,
  98. const char *expect_log)
  99. {
  100. or_options_t *opt=NULL;
  101. or_options_t *dflt;
  102. config_line_t *cl=NULL;
  103. char *msg=NULL;
  104. int r;
  105. setup_options(opt, dflt);
  106. r = config_get_lines(configuration, &cl, 1);
  107. tt_int_op(r, OP_EQ, 0);
  108. r = config_assign(&options_format, opt, cl, 0, &msg);
  109. tt_int_op(r, OP_EQ, 0);
  110. r = options_validate(NULL, opt, dflt, 0, &msg);
  111. if (expect_errmsg && !msg) {
  112. TT_DIE(("Expected error message <%s> from <%s>, but got none.",
  113. expect_errmsg, configuration));
  114. } else if (expect_errmsg && !strstr(msg, expect_errmsg)) {
  115. TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
  116. expect_errmsg, configuration, msg));
  117. } else if (!expect_errmsg && msg) {
  118. TT_DIE(("Expected no error message from <%s> but got <%s>.",
  119. configuration, msg));
  120. }
  121. tt_int_op((r == 0), OP_EQ, (msg == NULL));
  122. if (expect_log) {
  123. int found = 0;
  124. if (messages) {
  125. SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, m) {
  126. if (m->severity == expect_log_severity &&
  127. strstr(m->msg, expect_log)) {
  128. found = 1;
  129. break;
  130. }
  131. } SMARTLIST_FOREACH_END(m);
  132. }
  133. if (!found) {
  134. tor_free(msg);
  135. msg = dump_logs();
  136. TT_DIE(("Expected log message [%s] %s from <%s>, but got <%s>.",
  137. log_level_to_string(expect_log_severity), expect_log,
  138. configuration, msg));
  139. }
  140. }
  141. done:
  142. escaped(NULL);
  143. policies_free_all();
  144. config_free_lines(cl);
  145. or_options_free(opt);
  146. or_options_free(dflt);
  147. tor_free(msg);
  148. clear_log_messages();
  149. }
  150. #define WANT_ERR(config, msg) \
  151. test_options_validate_impl((config), (msg), 0, NULL)
  152. #define WANT_LOG(config, severity, msg) \
  153. test_options_validate_impl((config), NULL, (severity), (msg))
  154. #define WANT_ERR_LOG(config, msg, severity, logmsg) \
  155. test_options_validate_impl((config), (msg), (severity), (logmsg))
  156. #define OK(config) \
  157. test_options_validate_impl((config), NULL, 0, NULL)
  158. static void
  159. test_options_validate(void *arg)
  160. {
  161. (void)arg;
  162. setup_log_callback();
  163. sandbox_disable_getaddrinfo_cache();
  164. WANT_ERR("ExtORPort 500000", "Invalid ExtORPort");
  165. WANT_ERR_LOG("ServerTransportOptions trebuchet",
  166. "ServerTransportOptions did not parse",
  167. LOG_WARN, "Too few arguments");
  168. OK("ServerTransportOptions trebuchet sling=snappy");
  169. OK("ServerTransportOptions trebuchet sling=");
  170. WANT_ERR_LOG("ServerTransportOptions trebuchet slingsnappy",
  171. "ServerTransportOptions did not parse",
  172. LOG_WARN, "\"slingsnappy\" is not a k=v");
  173. WANT_ERR("DirPort 8080\nDirCache 0",
  174. "DirPort configured but DirCache disabled.");
  175. WANT_ERR("BridgeRelay 1\nDirCache 0",
  176. "We're a bridge but DirCache is disabled.");
  177. close_temp_logs();
  178. clear_log_messages();
  179. return;
  180. }
  181. #define MEGABYTEIFY(mb) (U64_LITERAL(mb) << 20)
  182. static void
  183. test_have_enough_mem_for_dircache(void *arg)
  184. {
  185. (void)arg;
  186. or_options_t *opt=NULL;
  187. or_options_t *dflt=NULL;
  188. config_line_t *cl=NULL;
  189. char *msg=NULL;;
  190. int r;
  191. const char *configuration = "ORPort 8080\nDirCache 1", *expect_errmsg;
  192. setup_options(opt, dflt);
  193. setup_log_callback();
  194. (void)dflt;
  195. r = config_get_lines(configuration, &cl, 1);
  196. tt_int_op(r, OP_EQ, 0);
  197. r = config_assign(&options_format, opt, cl, 0, &msg);
  198. tt_int_op(r, OP_EQ, 0);
  199. /* 300 MB RAM available, DirCache enabled */
  200. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
  201. tt_int_op(r, OP_EQ, 0);
  202. tt_assert(!msg);
  203. /* 200 MB RAM available, DirCache enabled */
  204. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
  205. tt_int_op(r, OP_EQ, -1);
  206. expect_errmsg = "Being a directory cache (default) with less than ";
  207. if (!strstr(msg, expect_errmsg)) {
  208. TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
  209. expect_errmsg, configuration, msg));
  210. }
  211. tor_free(msg);
  212. config_free_lines(cl); cl = NULL;
  213. configuration = "ORPort 8080\nDirCache 1\nBridgeRelay 1";
  214. r = config_get_lines(configuration, &cl, 1);
  215. tt_int_op(r, OP_EQ, 0);
  216. r = config_assign(&options_format, opt, cl, 0, &msg);
  217. tt_int_op(r, OP_EQ, 0);
  218. /* 300 MB RAM available, DirCache enabled, Bridge */
  219. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
  220. tt_int_op(r, OP_EQ, 0);
  221. tt_assert(!msg);
  222. /* 200 MB RAM available, DirCache enabled, Bridge */
  223. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
  224. tt_int_op(r, OP_EQ, -1);
  225. expect_errmsg = "Running a Bridge with less than ";
  226. if (!strstr(msg, expect_errmsg)) {
  227. TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
  228. expect_errmsg, configuration, msg));
  229. }
  230. tor_free(msg);
  231. config_free_lines(cl); cl = NULL;
  232. configuration = "ORPort 8080\nDirCache 0";
  233. r = config_get_lines(configuration, &cl, 1);
  234. tt_int_op(r, OP_EQ, 0);
  235. r = config_assign(&options_format, opt, cl, 0, &msg);
  236. tt_int_op(r, OP_EQ, 0);
  237. /* 200 MB RAM available, DirCache disabled */
  238. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(200), &msg);
  239. tt_int_op(r, OP_EQ, 0);
  240. tt_assert(!msg);
  241. /* 300 MB RAM available, DirCache disabled */
  242. r = have_enough_mem_for_dircache(opt, MEGABYTEIFY(300), &msg);
  243. tt_int_op(r, OP_EQ, -1);
  244. expect_errmsg = "DirCache is disabled and we are configured as a ";
  245. if (!strstr(msg, expect_errmsg)) {
  246. TT_DIE(("Expected error message <%s> from <%s>, but got <%s>.",
  247. expect_errmsg, configuration, msg));
  248. }
  249. tor_free(msg);
  250. clear_log_messages();
  251. done:
  252. if (msg)
  253. tor_free(msg);
  254. or_options_free(dflt);
  255. or_options_free(opt);
  256. config_free_lines(cl);
  257. return;
  258. }
  259. static const char *fixed_get_uname_result = NULL;
  260. static const char *
  261. fixed_get_uname(void)
  262. {
  263. return fixed_get_uname_result;
  264. }
  265. #define TEST_OPTIONS_OLD_VALUES "TestingV3AuthInitialVotingInterval 1800\n" \
  266. "ClientBootstrapConsensusMaxDownloadTries 7\n" \
  267. "ClientBootstrapConsensusAuthorityOnlyMaxDownloadTries 4\n" \
  268. "ClientBootstrapConsensusMaxInProgressTries 3\n" \
  269. "TestingV3AuthInitialVoteDelay 300\n" \
  270. "TestingV3AuthInitialDistDelay 300\n" \
  271. "TestingClientMaxIntervalWithoutRequest 600\n" \
  272. "TestingDirConnectionMaxStall 600\n" \
  273. "TestingConsensusMaxDownloadTries 8\n" \
  274. "TestingDescriptorMaxDownloadTries 8\n" \
  275. "TestingMicrodescMaxDownloadTries 8\n" \
  276. "TestingCertMaxDownloadTries 8\n"
  277. #define TEST_OPTIONS_DEFAULT_VALUES TEST_OPTIONS_OLD_VALUES \
  278. "MaxClientCircuitsPending 1\n" \
  279. "RendPostPeriod 1000\n" \
  280. "KeepAlivePeriod 1\n" \
  281. "ConnLimit 1\n" \
  282. "V3AuthVotingInterval 300\n" \
  283. "V3AuthVoteDelay 20\n" \
  284. "V3AuthDistDelay 20\n" \
  285. "V3AuthNIntervalsValid 3\n" \
  286. "ClientUseIPv4 1\n" \
  287. "VirtualAddrNetworkIPv4 127.192.0.0/10\n" \
  288. "VirtualAddrNetworkIPv6 [FE80::]/10\n" \
  289. "SchedulerHighWaterMark__ 42\n" \
  290. "SchedulerLowWaterMark__ 10\n"
  291. typedef struct {
  292. or_options_t *old_opt;
  293. or_options_t *opt;
  294. or_options_t *def_opt;
  295. } options_test_data_t;
  296. static void free_options_test_data(options_test_data_t *td);
  297. static options_test_data_t *
  298. get_options_test_data(const char *conf)
  299. {
  300. int rv = -1;
  301. char *msg = NULL;
  302. config_line_t *cl=NULL;
  303. options_test_data_t *result = tor_malloc(sizeof(options_test_data_t));
  304. result->opt = options_new();
  305. result->old_opt = options_new();
  306. result->def_opt = options_new();
  307. rv = config_get_lines(conf, &cl, 1);
  308. tt_assert(rv == 0);
  309. rv = config_assign(&options_format, result->opt, cl, 0, &msg);
  310. if (msg) {
  311. /* Display the parse error message by comparing it with an empty string */
  312. tt_str_op(msg, OP_EQ, "");
  313. }
  314. tt_assert(rv == 0);
  315. config_free_lines(cl);
  316. result->opt->LogTimeGranularity = 1;
  317. result->opt->TokenBucketRefillInterval = 1;
  318. rv = config_get_lines(TEST_OPTIONS_OLD_VALUES, &cl, 1);
  319. tt_assert(rv == 0);
  320. rv = config_assign(&options_format, result->def_opt, cl, 0, &msg);
  321. if (msg) {
  322. /* Display the parse error message by comparing it with an empty string */
  323. tt_str_op(msg, OP_EQ, "");
  324. }
  325. tt_assert(rv == 0);
  326. done:
  327. config_free_lines(cl);
  328. if (rv != 0) {
  329. free_options_test_data(result);
  330. result = NULL;
  331. /* Callers expect a non-NULL result, so just die if we can't provide one.
  332. */
  333. tor_assert(0);
  334. }
  335. return result;
  336. }
  337. static void
  338. free_options_test_data(options_test_data_t *td)
  339. {
  340. if (!td) return;
  341. or_options_free(td->old_opt);
  342. or_options_free(td->opt);
  343. or_options_free(td->def_opt);
  344. tor_free(td);
  345. }
  346. static void
  347. test_options_validate__uname_for_server(void *ignored)
  348. {
  349. (void)ignored;
  350. char *msg;
  351. options_test_data_t *tdata = get_options_test_data(
  352. "ORListenAddress 127.0.0.1:5555");
  353. setup_capture_of_logs(LOG_WARN);
  354. MOCK(get_uname, fixed_get_uname);
  355. fixed_get_uname_result = "Windows 95";
  356. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  357. expect_log_msg("Tor is running as a server, but you"
  358. " are running Windows 95; this probably won't work. See https://www"
  359. ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
  360. tor_free(msg);
  361. fixed_get_uname_result = "Windows 98";
  362. mock_clean_saved_logs();
  363. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  364. expect_log_msg("Tor is running as a server, but you"
  365. " are running Windows 98; this probably won't work. See https://www"
  366. ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
  367. tor_free(msg);
  368. fixed_get_uname_result = "Windows Me";
  369. mock_clean_saved_logs();
  370. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  371. expect_log_msg("Tor is running as a server, but you"
  372. " are running Windows Me; this probably won't work. See https://www"
  373. ".torproject.org/docs/faq.html#BestOSForRelay for details.\n");
  374. tor_free(msg);
  375. fixed_get_uname_result = "Windows 2000";
  376. mock_clean_saved_logs();
  377. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  378. expect_log_entry();
  379. tor_free(msg);
  380. done:
  381. UNMOCK(get_uname);
  382. free_options_test_data(tdata);
  383. tor_free(msg);
  384. teardown_capture_of_logs();
  385. }
  386. static void
  387. test_options_validate__outbound_addresses(void *ignored)
  388. {
  389. (void)ignored;
  390. int ret;
  391. char *msg;
  392. options_test_data_t *tdata = get_options_test_data(
  393. "OutboundBindAddress xxyy!!!sdfaf");
  394. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  395. tt_int_op(ret, OP_EQ, -1);
  396. done:
  397. free_options_test_data(tdata);
  398. tor_free(msg);
  399. }
  400. static void
  401. test_options_validate__data_directory(void *ignored)
  402. {
  403. (void)ignored;
  404. int ret;
  405. char *msg;
  406. options_test_data_t *tdata = get_options_test_data(
  407. "DataDirectory longreallyl"
  408. "ongLONGLONGlongreallylong"
  409. "LONGLONGlongreallylongLON"
  410. "GLONGlongreallylongLONGLO"
  411. "NGlongreallylongLONGLONGl"
  412. "ongreallylongLONGLONGlong"
  413. "reallylongLONGLONGlongrea"
  414. "llylongLONGLONGlongreally"
  415. "longLONGLONGlongreallylon"
  416. "gLONGLONGlongreallylongLO"
  417. "NGLONGlongreallylongLONGL"
  418. "ONGlongreallylongLONGLONG"
  419. "longreallylongLONGLONGlon"
  420. "greallylongLONGLONGlongre"
  421. "allylongLONGLONGlongreall"
  422. "ylongLONGLONGlongreallylo"
  423. "ngLONGLONGlongreallylongL"
  424. "ONGLONGlongreallylongLONG"
  425. "LONG"); // 440 characters
  426. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  427. tt_int_op(ret, OP_EQ, -1);
  428. tt_str_op(msg, OP_EQ, "Invalid DataDirectory");
  429. done:
  430. free_options_test_data(tdata);
  431. tor_free(msg);
  432. }
  433. static void
  434. test_options_validate__nickname(void *ignored)
  435. {
  436. (void)ignored;
  437. int ret;
  438. char *msg;
  439. options_test_data_t *tdata = get_options_test_data(
  440. "Nickname ThisNickNameIsABitTooLong");
  441. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  442. tt_int_op(ret, OP_EQ, -1);
  443. tt_str_op(msg, OP_EQ,
  444. "Nickname 'ThisNickNameIsABitTooLong', nicknames must be between "
  445. "1 and 19 characters inclusive, and must contain only the "
  446. "characters [a-zA-Z0-9].");
  447. tor_free(msg);
  448. free_options_test_data(tdata);
  449. tdata = get_options_test_data("Nickname AMoreValidNick");
  450. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  451. tt_int_op(ret, OP_EQ, -1);
  452. tt_assert(!msg);
  453. free_options_test_data(tdata);
  454. tdata = get_options_test_data("DataDirectory /tmp/somewhere");
  455. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  456. tt_int_op(ret, OP_EQ, -1);
  457. tt_assert(!msg);
  458. done:
  459. free_options_test_data(tdata);
  460. tor_free(msg);
  461. }
  462. static void
  463. test_options_validate__contactinfo(void *ignored)
  464. {
  465. (void)ignored;
  466. int ret;
  467. char *msg;
  468. options_test_data_t *tdata = get_options_test_data(
  469. "ORListenAddress 127.0.0.1:5555\nORPort 955");
  470. setup_capture_of_logs(LOG_DEBUG);
  471. tdata->opt->ContactInfo = NULL;
  472. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  473. tt_int_op(ret, OP_EQ, -1);
  474. expect_log_msg(
  475. "Your ContactInfo config option is not"
  476. " set. Please consider setting it, so we can contact you if your"
  477. " server is misconfigured or something else goes wrong.\n");
  478. tor_free(msg);
  479. free_options_test_data(tdata);
  480. tdata = get_options_test_data("ORListenAddress 127.0.0.1:5555\nORPort 955\n"
  481. "ContactInfo hella@example.org");
  482. mock_clean_saved_logs();
  483. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  484. tt_int_op(ret, OP_EQ, -1);
  485. expect_no_log_msg(
  486. "Your ContactInfo config option is not"
  487. " set. Please consider setting it, so we can contact you if your"
  488. " server is misconfigured or something else goes wrong.\n");
  489. tor_free(msg);
  490. done:
  491. teardown_capture_of_logs();
  492. free_options_test_data(tdata);
  493. tor_free(msg);
  494. }
  495. static void
  496. test_options_validate__logs(void *ignored)
  497. {
  498. (void)ignored;
  499. int ret;
  500. (void)ret;
  501. char *msg;
  502. int orig_quiet_level = quiet_level;
  503. options_test_data_t *tdata = get_options_test_data("");
  504. tdata->opt->Logs = NULL;
  505. tdata->opt->RunAsDaemon = 0;
  506. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  507. tt_str_op(tdata->opt->Logs->key, OP_EQ, "Log");
  508. tt_str_op(tdata->opt->Logs->value, OP_EQ, "notice stdout");
  509. tor_free(msg);
  510. free_options_test_data(tdata);
  511. tdata = get_options_test_data("");
  512. tdata->opt->Logs = NULL;
  513. tdata->opt->RunAsDaemon = 0;
  514. quiet_level = 1;
  515. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  516. tt_str_op(tdata->opt->Logs->key, OP_EQ, "Log");
  517. tt_str_op(tdata->opt->Logs->value, OP_EQ, "warn stdout");
  518. tor_free(msg);
  519. free_options_test_data(tdata);
  520. tdata = get_options_test_data("");
  521. tdata->opt->Logs = NULL;
  522. tdata->opt->RunAsDaemon = 0;
  523. quiet_level = 2;
  524. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  525. tt_assert(!tdata->opt->Logs);
  526. tor_free(msg);
  527. free_options_test_data(tdata);
  528. tdata = get_options_test_data("");
  529. tdata->opt->Logs = NULL;
  530. tdata->opt->RunAsDaemon = 0;
  531. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 1, &msg);
  532. tt_assert(!tdata->opt->Logs);
  533. tor_free(msg);
  534. free_options_test_data(tdata);
  535. tdata = get_options_test_data("");
  536. tdata->opt->Logs = NULL;
  537. tdata->opt->RunAsDaemon = 1;
  538. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  539. tt_assert(!tdata->opt->Logs);
  540. tor_free(msg);
  541. free_options_test_data(tdata);
  542. tdata = get_options_test_data("");
  543. tdata->opt->RunAsDaemon = 0;
  544. config_line_t *cl=NULL;
  545. config_get_lines("Log foo", &cl, 1);
  546. tdata->opt->Logs = cl;
  547. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  548. tt_int_op((intptr_t)tdata->opt->Logs, OP_EQ, (intptr_t)cl);
  549. done:
  550. quiet_level = orig_quiet_level;
  551. free_options_test_data(tdata);
  552. tor_free(msg);
  553. }
  554. /* static config_line_t * */
  555. /* mock_config_line(const char *key, const char *val) */
  556. /* { */
  557. /* config_line_t *config_line = tor_malloc(sizeof(config_line_t)); */
  558. /* memset(config_line, 0, sizeof(config_line_t)); */
  559. /* config_line->key = tor_strdup(key); */
  560. /* config_line->value = tor_strdup(val); */
  561. /* return config_line; */
  562. /* } */
  563. static void
  564. test_options_validate__authdir(void *ignored)
  565. {
  566. (void)ignored;
  567. int ret;
  568. char *msg;
  569. setup_capture_of_logs(LOG_INFO);
  570. options_test_data_t *tdata = get_options_test_data(
  571. "AuthoritativeDirectory 1\n"
  572. "Address this.should.not_exist.example.org");
  573. sandbox_disable_getaddrinfo_cache();
  574. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  575. tt_int_op(ret, OP_EQ, -1);
  576. tt_str_op(msg, OP_EQ, "Failed to resolve/guess local address. See logs for"
  577. " details.");
  578. expect_log_msg("Could not resolve local Address "
  579. "'this.should.not_exist.example.org'. Failing.\n");
  580. tor_free(msg);
  581. free_options_test_data(tdata);
  582. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  583. "Address 100.200.10.1");
  584. mock_clean_saved_logs();
  585. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  586. tt_int_op(ret, OP_EQ, -1);
  587. tt_assert(!msg);
  588. free_options_test_data(tdata);
  589. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  590. "Address 100.200.10.1\n"
  591. "SchedulerHighWaterMark__ 42\n"
  592. "SchedulerLowWaterMark__ 10\n");
  593. mock_clean_saved_logs();
  594. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  595. tt_int_op(ret, OP_EQ, -1);
  596. tt_str_op(msg, OP_EQ,
  597. "Authoritative directory servers must set ContactInfo");
  598. tor_free(msg);
  599. free_options_test_data(tdata);
  600. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  601. "Address 100.200.10.1\n"
  602. "TestingTorNetwork 1\n"
  603. "SchedulerHighWaterMark__ 42\n"
  604. "SchedulerLowWaterMark__ 10\n");
  605. mock_clean_saved_logs();
  606. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  607. tt_int_op(ret, OP_EQ, -1);
  608. tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
  609. "AuthoritativeDir is set.");
  610. tor_free(msg);
  611. free_options_test_data(tdata);
  612. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  613. "Address 100.200.10.1\n"
  614. "ContactInfo hello@hello.com\n"
  615. "SchedulerHighWaterMark__ 42\n"
  616. "SchedulerLowWaterMark__ 10\n");
  617. mock_clean_saved_logs();
  618. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  619. tt_int_op(ret, OP_EQ, -1);
  620. tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
  621. "AuthoritativeDir is set.");
  622. tor_free(msg);
  623. free_options_test_data(tdata);
  624. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  625. "Address 100.200.10.1\n"
  626. "RecommendedVersions 1.2, 3.14\n"
  627. "ContactInfo hello@hello.com\n"
  628. "SchedulerHighWaterMark__ 42\n"
  629. "SchedulerLowWaterMark__ 10\n");
  630. mock_clean_saved_logs();
  631. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  632. tt_str_op(tdata->opt->RecommendedClientVersions->value, OP_EQ, "1.2, 3.14");
  633. tt_str_op(tdata->opt->RecommendedServerVersions->value, OP_EQ, "1.2, 3.14");
  634. tor_free(msg);
  635. free_options_test_data(tdata);
  636. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  637. "Address 100.200.10.1\n"
  638. "RecommendedVersions 1.2, 3.14\n"
  639. "RecommendedClientVersions 25\n"
  640. "RecommendedServerVersions 4.18\n"
  641. "ContactInfo hello@hello.com\n"
  642. "SchedulerHighWaterMark__ 42\n"
  643. "SchedulerLowWaterMark__ 10\n");
  644. mock_clean_saved_logs();
  645. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  646. tt_str_op(tdata->opt->RecommendedClientVersions->value, OP_EQ, "25");
  647. tt_str_op(tdata->opt->RecommendedServerVersions->value, OP_EQ, "4.18");
  648. tor_free(msg);
  649. free_options_test_data(tdata);
  650. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  651. "Address 100.200.10.1\n"
  652. "VersioningAuthoritativeDirectory 1\n"
  653. "RecommendedVersions 1.2, 3.14\n"
  654. "RecommendedClientVersions 25\n"
  655. "RecommendedServerVersions 4.18\n"
  656. "ContactInfo hello@hello.com\n"
  657. "SchedulerHighWaterMark__ 42\n"
  658. "SchedulerLowWaterMark__ 10\n");
  659. mock_clean_saved_logs();
  660. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  661. tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
  662. "AuthoritativeDir is set.");
  663. tor_free(msg);
  664. free_options_test_data(tdata);
  665. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  666. "Address 100.200.10.1\n"
  667. "VersioningAuthoritativeDirectory 1\n"
  668. "RecommendedServerVersions 4.18\n"
  669. "ContactInfo hello@hello.com\n"
  670. "SchedulerHighWaterMark__ 42\n"
  671. "SchedulerLowWaterMark__ 10\n");
  672. mock_clean_saved_logs();
  673. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  674. tt_str_op(msg, OP_EQ, "Versioning authoritative dir servers must set "
  675. "Recommended*Versions.");
  676. tor_free(msg);
  677. free_options_test_data(tdata);
  678. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  679. "Address 100.200.10.1\n"
  680. "VersioningAuthoritativeDirectory 1\n"
  681. "RecommendedClientVersions 4.18\n"
  682. "ContactInfo hello@hello.com\n"
  683. "SchedulerHighWaterMark__ 42\n"
  684. "SchedulerLowWaterMark__ 10\n");
  685. mock_clean_saved_logs();
  686. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  687. tt_str_op(msg, OP_EQ, "Versioning authoritative dir servers must set "
  688. "Recommended*Versions.");
  689. tor_free(msg);
  690. free_options_test_data(tdata);
  691. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  692. "Address 100.200.10.1\n"
  693. "UseEntryGuards 1\n"
  694. "ContactInfo hello@hello.com\n"
  695. "SchedulerHighWaterMark__ 42\n"
  696. "SchedulerLowWaterMark__ 10\n");
  697. mock_clean_saved_logs();
  698. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  699. expect_log_msg("Authoritative directory servers "
  700. "can't set UseEntryGuards. Disabling.\n");
  701. tt_int_op(tdata->opt->UseEntryGuards, OP_EQ, 0);
  702. tor_free(msg);
  703. free_options_test_data(tdata);
  704. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  705. "Address 100.200.10.1\n"
  706. "V3AuthoritativeDir 1\n"
  707. "ContactInfo hello@hello.com\n"
  708. "SchedulerHighWaterMark__ 42\n"
  709. "SchedulerLowWaterMark__ 10\n");
  710. mock_clean_saved_logs();
  711. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  712. expect_log_msg("Authoritative directories always try"
  713. " to download extra-info documents. Setting DownloadExtraInfo.\n");
  714. tt_int_op(tdata->opt->DownloadExtraInfo, OP_EQ, 1);
  715. tor_free(msg);
  716. free_options_test_data(tdata);
  717. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  718. "Address 100.200.10.1\n"
  719. "DownloadExtraInfo 1\n"
  720. "V3AuthoritativeDir 1\n"
  721. "ContactInfo hello@hello.com\n"
  722. "SchedulerHighWaterMark__ 42\n"
  723. "SchedulerLowWaterMark__ 10\n");
  724. mock_clean_saved_logs();
  725. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  726. expect_no_log_msg("Authoritative directories always try"
  727. " to download extra-info documents. Setting DownloadExtraInfo.\n");
  728. tt_int_op(tdata->opt->DownloadExtraInfo, OP_EQ, 1);
  729. tor_free(msg);
  730. free_options_test_data(tdata);
  731. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  732. "Address 100.200.10.1\n"
  733. "ContactInfo hello@hello.com\n"
  734. "SchedulerHighWaterMark__ 42\n"
  735. "SchedulerLowWaterMark__ 10\n");
  736. mock_clean_saved_logs();
  737. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  738. tt_str_op(msg, OP_EQ, "AuthoritativeDir is set, but none of (Bridge/V3)"
  739. "AuthoritativeDir is set.");
  740. tor_free(msg);
  741. free_options_test_data(tdata);
  742. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  743. "Address 100.200.10.1\n"
  744. "BridgeAuthoritativeDir 1\n"
  745. "ContactInfo hello@hello.com\n"
  746. "V3BandwidthsFile non-existant-file\n"
  747. "SchedulerHighWaterMark__ 42\n"
  748. "SchedulerLowWaterMark__ 10\n");
  749. mock_clean_saved_logs();
  750. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  751. tt_str_op(msg, OP_EQ,
  752. "Running as authoritative directory, but no DirPort set.");
  753. tor_free(msg);
  754. free_options_test_data(tdata);
  755. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  756. "Address 100.200.10.1\n"
  757. "BridgeAuthoritativeDir 1\n"
  758. "ContactInfo hello@hello.com\n"
  759. "V3BandwidthsFile non-existant-file\n"
  760. "SchedulerHighWaterMark__ 42\n"
  761. "SchedulerLowWaterMark__ 10\n");
  762. mock_clean_saved_logs();
  763. options_validate(NULL, tdata->opt, tdata->def_opt, 0, &msg);
  764. tt_str_op(msg, OP_EQ,
  765. "Running as authoritative directory, but no DirPort set.");
  766. tor_free(msg);
  767. free_options_test_data(tdata);
  768. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  769. "Address 100.200.10.1\n"
  770. "BridgeAuthoritativeDir 1\n"
  771. "ContactInfo hello@hello.com\n"
  772. "GuardfractionFile non-existant-file\n"
  773. "SchedulerHighWaterMark__ 42\n"
  774. "SchedulerLowWaterMark__ 10\n");
  775. mock_clean_saved_logs();
  776. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  777. tt_str_op(msg, OP_EQ,
  778. "Running as authoritative directory, but no DirPort set.");
  779. tor_free(msg);
  780. free_options_test_data(tdata);
  781. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  782. "Address 100.200.10.1\n"
  783. "BridgeAuthoritativeDir 1\n"
  784. "ContactInfo hello@hello.com\n"
  785. "GuardfractionFile non-existant-file\n"
  786. "SchedulerHighWaterMark__ 42\n"
  787. "SchedulerLowWaterMark__ 10\n");
  788. mock_clean_saved_logs();
  789. options_validate(NULL, tdata->opt, tdata->def_opt, 0, &msg);
  790. tt_str_op(msg, OP_EQ,
  791. "Running as authoritative directory, but no DirPort set.");
  792. tor_free(msg);
  793. free_options_test_data(tdata);
  794. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  795. "Address 100.200.10.1\n"
  796. "BridgeAuthoritativeDir 1\n"
  797. "ContactInfo hello@hello.com\n"
  798. "SchedulerHighWaterMark__ 42\n"
  799. "SchedulerLowWaterMark__ 10\n");
  800. mock_clean_saved_logs();
  801. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  802. tt_int_op(ret, OP_EQ, -1);
  803. tt_str_op(msg, OP_EQ,
  804. "Running as authoritative directory, but no DirPort set.");
  805. tor_free(msg);
  806. free_options_test_data(tdata);
  807. tdata = get_options_test_data("AuthoritativeDirectory 1\n"
  808. "Address 100.200.10.1\n"
  809. "DirPort 999\n"
  810. "BridgeAuthoritativeDir 1\n"
  811. "ContactInfo hello@hello.com\n"
  812. "SchedulerHighWaterMark__ 42\n"
  813. "SchedulerLowWaterMark__ 10\n");
  814. mock_clean_saved_logs();
  815. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  816. tt_int_op(ret, OP_EQ, -1);
  817. tt_str_op(msg, OP_EQ,
  818. "Running as authoritative directory, but no ORPort set.");
  819. tor_free(msg);
  820. // TODO: This case can't be reached, since clientonly is used to
  821. // check when parsing port lines as well.
  822. /* free_options_test_data(tdata); */
  823. /* tdata = get_options_test_data("AuthoritativeDirectory 1\n" */
  824. /* "Address 100.200.10.1\n" */
  825. /* "DirPort 999\n" */
  826. /* "ORPort 888\n" */
  827. /* "ClientOnly 1\n" */
  828. /* "BridgeAuthoritativeDir 1\n" */
  829. /* "ContactInfo hello@hello.com\n" */
  830. /* "SchedulerHighWaterMark__ 42\n" */
  831. /* "SchedulerLowWaterMark__ 10\n"); */
  832. /* mock_clean_saved_logs(); */
  833. /* ret = options_validate(tdata->old_opt, tdata->opt, */
  834. /* tdata->def_opt, 0, &msg); */
  835. /* tt_int_op(ret, OP_EQ, -1); */
  836. /* tt_str_op(msg, OP_EQ, "Running as authoritative directory, " */
  837. /* "but ClientOnly also set."); */
  838. done:
  839. teardown_capture_of_logs();
  840. // sandbox_free_getaddrinfo_cache();
  841. free_options_test_data(tdata);
  842. tor_free(msg);
  843. }
  844. static void
  845. test_options_validate__relay_with_hidden_services(void *ignored)
  846. {
  847. (void)ignored;
  848. char *msg;
  849. setup_capture_of_logs(LOG_DEBUG);
  850. options_test_data_t *tdata = get_options_test_data(
  851. "ORListenAddress 127.0.0.1:5555\n"
  852. "ORPort 955\n"
  853. "HiddenServiceDir "
  854. "/Library/Tor/var/lib/tor/hidden_service/\n"
  855. "HiddenServicePort 80 127.0.0.1:8080\n"
  856. );
  857. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  858. expect_log_msg(
  859. "Tor is currently configured as a relay and a hidden service. "
  860. "That's not very secure: you should probably run your hidden servi"
  861. "ce in a separate Tor process, at least -- see "
  862. "https://trac.torproject.org/8742\n");
  863. done:
  864. teardown_capture_of_logs();
  865. free_options_test_data(tdata);
  866. tor_free(msg);
  867. }
  868. // TODO: it doesn't seem possible to hit the case of having no port lines at
  869. // all, since there will be a default created for SocksPort
  870. /* static void */
  871. /* test_options_validate__ports(void *ignored) */
  872. /* { */
  873. /* (void)ignored; */
  874. /* int ret; */
  875. /* char *msg; */
  876. /* setup_capture_of_logs(LOG_WARN); */
  877. /* options_test_data_t *tdata = get_options_test_data(""); */
  878. /* ret = options_validate(tdata->old_opt, tdata->opt, */
  879. /* tdata->def_opt, 0, &msg); */
  880. /* expect_log_msg("SocksPort, TransPort, NATDPort, DNSPort, and ORPort " */
  881. /* "are all undefined, and there aren't any hidden services " */
  882. /* "configured. " */
  883. /* " Tor will still run, but probably won't do anything.\n"); */
  884. /* done: */
  885. /* teardown_capture_of_logs(); */
  886. /* free_options_test_data(tdata); */
  887. /* tor_free(msg); */
  888. /* } */
  889. static void
  890. test_options_validate__transproxy(void *ignored)
  891. {
  892. (void)ignored;
  893. int ret;
  894. char *msg;
  895. options_test_data_t *tdata;
  896. #ifdef USE_TRANSPARENT
  897. // Test default trans proxy
  898. tdata = get_options_test_data("TransProxyType default\n");
  899. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  900. tt_int_op(ret, OP_EQ, -1);
  901. tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_DEFAULT);
  902. tor_free(msg);
  903. // Test pf-divert trans proxy
  904. free_options_test_data(tdata);
  905. tdata = get_options_test_data("TransProxyType pf-divert\n");
  906. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  907. tt_int_op(ret, OP_EQ, -1);
  908. #if !defined(__OpenBSD__) && !defined( DARWIN )
  909. tt_str_op(msg, OP_EQ,
  910. "pf-divert is a OpenBSD-specific and OS X/Darwin-specific feature.");
  911. #else
  912. tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_PF_DIVERT);
  913. tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without "
  914. "any valid TransPort or TransListenAddress.");
  915. #endif
  916. tor_free(msg);
  917. // Test tproxy trans proxy
  918. free_options_test_data(tdata);
  919. tdata = get_options_test_data("TransProxyType tproxy\n");
  920. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  921. tt_int_op(ret, OP_EQ, -1);
  922. #if !defined(__linux__)
  923. tt_str_op(msg, OP_EQ, "TPROXY is a Linux-specific feature.");
  924. #else
  925. tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_TPROXY);
  926. tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without any valid "
  927. "TransPort or TransListenAddress.");
  928. #endif
  929. tor_free(msg);
  930. // Test ipfw trans proxy
  931. free_options_test_data(tdata);
  932. tdata = get_options_test_data("TransProxyType ipfw\n");
  933. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  934. tt_int_op(ret, OP_EQ, -1);
  935. #ifndef KERNEL_MAY_SUPPORT_IPFW
  936. tt_str_op(msg, OP_EQ, "ipfw is a FreeBSD-specific and OS X/Darwin-specific "
  937. "feature.");
  938. #else
  939. tt_int_op(tdata->opt->TransProxyType_parsed, OP_EQ, TPT_IPFW);
  940. tt_str_op(msg, OP_EQ, "Cannot use TransProxyType without any valid "
  941. "TransPort or TransListenAddress.");
  942. #endif
  943. tor_free(msg);
  944. // Test unknown trans proxy
  945. free_options_test_data(tdata);
  946. tdata = get_options_test_data("TransProxyType non-existant\n");
  947. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  948. tt_int_op(ret, OP_EQ, -1);
  949. tt_str_op(msg, OP_EQ, "Unrecognized value for TransProxyType");
  950. tor_free(msg);
  951. // Test trans proxy success
  952. free_options_test_data(tdata);
  953. tdata = NULL;
  954. #if defined(__linux__)
  955. tdata = get_options_test_data("TransProxyType tproxy\n"
  956. "TransPort 127.0.0.1:123\n");
  957. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  958. tt_int_op(ret, OP_EQ, -1);
  959. if (msg) {
  960. TT_DIE(("Expected NULL but got '%s'", msg));
  961. }
  962. #elif defined(KERNEL_MAY_SUPPORT_IPFW)
  963. tdata = get_options_test_data("TransProxyType ipfw\n"
  964. "TransPort 127.0.0.1:123\n");
  965. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  966. tt_int_op(ret, OP_EQ, -1);
  967. if (msg) {
  968. TT_DIE(("Expected NULL but got '%s'", msg));
  969. }
  970. #elif defined(__OpenBSD__)
  971. tdata = get_options_test_data("TransProxyType pf-divert\n"
  972. "TransPort 127.0.0.1:123\n");
  973. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  974. tt_int_op(ret, OP_EQ, -1);
  975. if (msg) {
  976. TT_DIE(("Expected NULL but got '%s'", msg));
  977. }
  978. #elif defined(__NetBSD__)
  979. tdata = get_options_test_data("TransProxyType default\n"
  980. "TransPort 127.0.0.1:123\n");
  981. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  982. tt_int_op(ret, OP_EQ, -1);
  983. if (msg) {
  984. TT_DIE(("Expected NULL but got '%s'", msg));
  985. }
  986. #endif
  987. // Assert that a test has run for some TransProxyType
  988. tt_assert(tdata);
  989. #else
  990. tdata = get_options_test_data("TransPort 127.0.0.1:555\n");
  991. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  992. tt_int_op(ret, OP_EQ, -1);
  993. tt_str_op(msg, OP_EQ, "TransPort and TransListenAddress are disabled in "
  994. "this build.");
  995. tor_free(msg);
  996. #endif
  997. done:
  998. free_options_test_data(tdata);
  999. tor_free(msg);
  1000. }
  1001. NS_DECL(country_t, geoip_get_country, (const char *country));
  1002. static country_t
  1003. NS(geoip_get_country)(const char *countrycode)
  1004. {
  1005. (void)countrycode;
  1006. CALLED(geoip_get_country)++;
  1007. return 1;
  1008. }
  1009. static void
  1010. test_options_validate__exclude_nodes(void *ignored)
  1011. {
  1012. (void)ignored;
  1013. NS_MOCK(geoip_get_country);
  1014. int ret;
  1015. char *msg;
  1016. setup_capture_of_logs(LOG_WARN);
  1017. options_test_data_t *tdata = get_options_test_data(
  1018. "ExcludeExitNodes {us}\n");
  1019. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1020. tt_int_op(ret, OP_EQ, -1);
  1021. tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 1);
  1022. tt_str_op((char *)
  1023. (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
  1024. OP_EQ, "{us}");
  1025. tor_free(msg);
  1026. free_options_test_data(tdata);
  1027. tdata = get_options_test_data("ExcludeNodes {cn}\n");
  1028. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1029. tt_int_op(ret, OP_EQ, -1);
  1030. tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 1);
  1031. tt_str_op((char *)
  1032. (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
  1033. OP_EQ, "{cn}");
  1034. tor_free(msg);
  1035. free_options_test_data(tdata);
  1036. tdata = get_options_test_data("ExcludeNodes {cn}\n"
  1037. "ExcludeExitNodes {us} {cn}\n");
  1038. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1039. tt_int_op(ret, OP_EQ, -1);
  1040. tt_int_op(smartlist_len(tdata->opt->ExcludeExitNodesUnion_->list), OP_EQ, 2);
  1041. tt_str_op((char *)
  1042. (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 0)),
  1043. OP_EQ, "{us} {cn}");
  1044. tt_str_op((char *)
  1045. (smartlist_get(tdata->opt->ExcludeExitNodesUnion_->list, 1)),
  1046. OP_EQ, "{cn}");
  1047. tor_free(msg);
  1048. free_options_test_data(tdata);
  1049. tdata = get_options_test_data("ExcludeNodes {cn}\n"
  1050. "StrictNodes 1\n"
  1051. "SchedulerHighWaterMark__ 42\n"
  1052. "SchedulerLowWaterMark__ 10\n");
  1053. mock_clean_saved_logs();
  1054. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1055. tt_int_op(ret, OP_EQ, -1);
  1056. expect_log_msg(
  1057. "You have asked to exclude certain relays from all positions "
  1058. "in your circuits. Expect hidden services and other Tor "
  1059. "features to be broken in unpredictable ways.\n");
  1060. tor_free(msg);
  1061. free_options_test_data(tdata);
  1062. tdata = get_options_test_data("ExcludeNodes {cn}\n"
  1063. "SchedulerHighWaterMark__ 42\n"
  1064. "SchedulerLowWaterMark__ 10\n");
  1065. mock_clean_saved_logs();
  1066. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1067. tt_int_op(ret, OP_EQ, -1);
  1068. expect_no_log_msg(
  1069. "You have asked to exclude certain relays from all positions "
  1070. "in your circuits. Expect hidden services and other Tor "
  1071. "features to be broken in unpredictable ways.\n");
  1072. tor_free(msg);
  1073. done:
  1074. NS_UNMOCK(geoip_get_country);
  1075. teardown_capture_of_logs();
  1076. free_options_test_data(tdata);
  1077. tor_free(msg);
  1078. }
  1079. static void
  1080. test_options_validate__scheduler(void *ignored)
  1081. {
  1082. (void)ignored;
  1083. int ret;
  1084. char *msg;
  1085. setup_capture_of_logs(LOG_DEBUG);
  1086. options_test_data_t *tdata = get_options_test_data(
  1087. "SchedulerLowWaterMark__ 0\n");
  1088. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1089. tt_int_op(ret, OP_EQ, -1);
  1090. expect_log_msg("Bad SchedulerLowWaterMark__ option\n");
  1091. tor_free(msg);
  1092. // TODO: this test cannot run on platforms where UINT32_MAX == UINT64_MAX.
  1093. // I suspect it's unlikely this branch can actually happen
  1094. /* free_options_test_data(tdata); */
  1095. /* tdata = get_options_test_data( */
  1096. /* "SchedulerLowWaterMark 10000000000000000000\n"); */
  1097. /* tdata->opt->SchedulerLowWaterMark__ = (uint64_t)UINT32_MAX; */
  1098. /* tdata->opt->SchedulerLowWaterMark__++; */
  1099. /* mock_clean_saved_logs(); */
  1100. /* ret = options_validate(tdata->old_opt, tdata->opt, */
  1101. /* tdata->def_opt, 0, &msg); */
  1102. /* tt_int_op(ret, OP_EQ, -1); */
  1103. /* expect_log_msg("Bad SchedulerLowWaterMark__ option\n"); */
  1104. free_options_test_data(tdata);
  1105. tdata = get_options_test_data("SchedulerLowWaterMark__ 42\n"
  1106. "SchedulerHighWaterMark__ 42\n");
  1107. mock_clean_saved_logs();
  1108. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1109. tt_int_op(ret, OP_EQ, -1);
  1110. expect_log_msg("Bad SchedulerHighWaterMark option\n");
  1111. tor_free(msg);
  1112. done:
  1113. teardown_capture_of_logs();
  1114. free_options_test_data(tdata);
  1115. tor_free(msg);
  1116. }
  1117. static void
  1118. test_options_validate__node_families(void *ignored)
  1119. {
  1120. (void)ignored;
  1121. int ret;
  1122. char *msg;
  1123. options_test_data_t *tdata = get_options_test_data(
  1124. "NodeFamily flux, flax\n"
  1125. "NodeFamily somewhere\n"
  1126. "SchedulerHighWaterMark__ 42\n"
  1127. "SchedulerLowWaterMark__ 10\n");
  1128. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1129. tt_int_op(ret, OP_EQ, -1);
  1130. tt_assert(tdata->opt->NodeFamilySets);
  1131. tt_int_op(smartlist_len(tdata->opt->NodeFamilySets), OP_EQ, 2);
  1132. tt_str_op((char *)(smartlist_get(
  1133. ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 0))->list, 0)),
  1134. OP_EQ, "flux");
  1135. tt_str_op((char *)(smartlist_get(
  1136. ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 0))->list, 1)),
  1137. OP_EQ, "flax");
  1138. tt_str_op((char *)(smartlist_get(
  1139. ((routerset_t *)smartlist_get(tdata->opt->NodeFamilySets, 1))->list, 0)),
  1140. OP_EQ, "somewhere");
  1141. tor_free(msg);
  1142. free_options_test_data(tdata);
  1143. tdata = get_options_test_data("SchedulerHighWaterMark__ 42\n"
  1144. "SchedulerLowWaterMark__ 10\n");
  1145. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1146. tt_int_op(ret, OP_EQ, -1);
  1147. tt_assert(!tdata->opt->NodeFamilySets);
  1148. tor_free(msg);
  1149. free_options_test_data(tdata);
  1150. tdata = get_options_test_data("NodeFamily !flux\n"
  1151. "SchedulerHighWaterMark__ 42\n"
  1152. "SchedulerLowWaterMark__ 10\n");
  1153. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1154. tt_int_op(ret, OP_EQ, -1);
  1155. tt_assert(tdata->opt->NodeFamilySets);
  1156. tt_int_op(smartlist_len(tdata->opt->NodeFamilySets), OP_EQ, 0);
  1157. tor_free(msg);
  1158. done:
  1159. free_options_test_data(tdata);
  1160. tor_free(msg);
  1161. }
  1162. static void
  1163. test_options_validate__tlsec(void *ignored)
  1164. {
  1165. (void)ignored;
  1166. int ret;
  1167. char *msg;
  1168. setup_capture_of_logs(LOG_DEBUG);
  1169. options_test_data_t *tdata = get_options_test_data(
  1170. "TLSECGroup ed25519\n"
  1171. "SchedulerHighWaterMark__ 42\n"
  1172. "SchedulerLowWaterMark__ 10\n");
  1173. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1174. tt_int_op(ret, OP_EQ, -1);
  1175. expect_log_msg("Unrecognized TLSECGroup: Falling back to the default.\n");
  1176. tt_assert(!tdata->opt->TLSECGroup);
  1177. tor_free(msg);
  1178. free_options_test_data(tdata);
  1179. tdata = get_options_test_data("TLSECGroup P224\n"
  1180. "SchedulerHighWaterMark__ 42\n"
  1181. "SchedulerLowWaterMark__ 10\n");
  1182. mock_clean_saved_logs();
  1183. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1184. tt_int_op(ret, OP_EQ, -1);
  1185. expect_no_log_msg(
  1186. "Unrecognized TLSECGroup: Falling back to the default.\n");
  1187. tt_assert(tdata->opt->TLSECGroup);
  1188. tor_free(msg);
  1189. free_options_test_data(tdata);
  1190. tdata = get_options_test_data("TLSECGroup P256\n"
  1191. "SchedulerHighWaterMark__ 42\n"
  1192. "SchedulerLowWaterMark__ 10\n");
  1193. mock_clean_saved_logs();
  1194. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1195. tt_int_op(ret, OP_EQ, -1);
  1196. expect_no_log_msg(
  1197. "Unrecognized TLSECGroup: Falling back to the default.\n");
  1198. tt_assert(tdata->opt->TLSECGroup);
  1199. tor_free(msg);
  1200. done:
  1201. teardown_capture_of_logs();
  1202. free_options_test_data(tdata);
  1203. tor_free(msg);
  1204. }
  1205. static void
  1206. test_options_validate__token_bucket(void *ignored)
  1207. {
  1208. (void)ignored;
  1209. int ret;
  1210. char *msg;
  1211. options_test_data_t *tdata = get_options_test_data("");
  1212. tdata->opt->TokenBucketRefillInterval = 0;
  1213. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1214. tt_int_op(ret, OP_EQ, -1);
  1215. tt_str_op(msg, OP_EQ,
  1216. "TokenBucketRefillInterval must be between 1 and 1000 inclusive.");
  1217. tor_free(msg);
  1218. tdata->opt->TokenBucketRefillInterval = 1001;
  1219. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1220. tt_int_op(ret, OP_EQ, -1);
  1221. tt_str_op(msg, OP_EQ,
  1222. "TokenBucketRefillInterval must be between 1 and 1000 inclusive.");
  1223. tor_free(msg);
  1224. done:
  1225. free_options_test_data(tdata);
  1226. tor_free(msg);
  1227. }
  1228. static void
  1229. test_options_validate__recommended_packages(void *ignored)
  1230. {
  1231. (void)ignored;
  1232. int ret;
  1233. char *msg;
  1234. setup_capture_of_logs(LOG_WARN);
  1235. options_test_data_t *tdata = get_options_test_data(
  1236. "RecommendedPackages foo 1.2 http://foo.com sha1=123123123123\n"
  1237. "RecommendedPackages invalid-package-line\n"
  1238. "SchedulerHighWaterMark__ 42\n"
  1239. "SchedulerLowWaterMark__ 10\n");
  1240. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1241. tt_int_op(ret, OP_EQ, -1);
  1242. expect_no_log_msg("Invalid RecommendedPackage line "
  1243. "invalid-package-line will be ignored\n");
  1244. done:
  1245. escaped(NULL); // This will free the leaking memory from the previous escaped
  1246. teardown_capture_of_logs();
  1247. free_options_test_data(tdata);
  1248. tor_free(msg);
  1249. }
  1250. static void
  1251. test_options_validate__fetch_dir(void *ignored)
  1252. {
  1253. (void)ignored;
  1254. int ret;
  1255. char *msg;
  1256. options_test_data_t *tdata = get_options_test_data(
  1257. "FetchDirInfoExtraEarly 1\n"
  1258. "FetchDirInfoEarly 0\n"
  1259. "SchedulerHighWaterMark__ 42\n"
  1260. "SchedulerLowWaterMark__ 10\n");
  1261. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1262. tt_int_op(ret, OP_EQ, -1);
  1263. tt_str_op(msg, OP_EQ, "FetchDirInfoExtraEarly requires that you"
  1264. " also set FetchDirInfoEarly");
  1265. tor_free(msg);
  1266. free_options_test_data(tdata);
  1267. tdata = get_options_test_data("FetchDirInfoExtraEarly 1\n"
  1268. "FetchDirInfoEarly 1\n"
  1269. "SchedulerHighWaterMark__ 42\n"
  1270. "SchedulerLowWaterMark__ 10\n");
  1271. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1272. tt_int_op(ret, OP_EQ, -1);
  1273. tt_str_op(msg, OP_NE, "FetchDirInfoExtraEarly requires that you"
  1274. " also set FetchDirInfoEarly");
  1275. tor_free(msg);
  1276. done:
  1277. free_options_test_data(tdata);
  1278. tor_free(msg);
  1279. }
  1280. static void
  1281. test_options_validate__conn_limit(void *ignored)
  1282. {
  1283. (void)ignored;
  1284. int ret;
  1285. char *msg;
  1286. options_test_data_t *tdata = get_options_test_data(
  1287. "ConnLimit 0\n"
  1288. "SchedulerHighWaterMark__ 42\n"
  1289. "SchedulerLowWaterMark__ 10\n");
  1290. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1291. tt_int_op(ret, OP_EQ, -1);
  1292. tt_str_op(msg, OP_EQ, "ConnLimit must be greater than 0, but was set to 0");
  1293. tor_free(msg);
  1294. free_options_test_data(tdata);
  1295. tdata = get_options_test_data("ConnLimit 1\n"
  1296. "SchedulerHighWaterMark__ 42\n"
  1297. "SchedulerLowWaterMark__ 10\n");
  1298. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1299. tt_int_op(ret, OP_EQ, -1);
  1300. tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024, "
  1301. "but was set to 0");
  1302. tor_free(msg);
  1303. done:
  1304. free_options_test_data(tdata);
  1305. tor_free(msg);
  1306. }
  1307. static void
  1308. test_options_validate__paths_needed(void *ignored)
  1309. {
  1310. (void)ignored;
  1311. int ret;
  1312. char *msg;
  1313. setup_capture_of_logs(LOG_WARN);
  1314. options_test_data_t *tdata = get_options_test_data(
  1315. "PathsNeededToBuildCircuits 0.1\n"
  1316. "ConnLimit 1\n"
  1317. "SchedulerHighWaterMark__ 42\n"
  1318. "SchedulerLowWaterMark__ 10\n");
  1319. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1320. tt_int_op(ret, OP_EQ, -1);
  1321. tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.24 &&
  1322. tdata->opt->PathsNeededToBuildCircuits < 0.26);
  1323. expect_log_msg("PathsNeededToBuildCircuits is too low. "
  1324. "Increasing to 0.25\n");
  1325. tor_free(msg);
  1326. free_options_test_data(tdata);
  1327. mock_clean_saved_logs();
  1328. tdata = get_options_test_data("PathsNeededToBuildCircuits 0.99\n"
  1329. "ConnLimit 1\n"
  1330. "SchedulerHighWaterMark__ 42\n"
  1331. "SchedulerLowWaterMark__ 10\n");
  1332. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1333. tt_int_op(ret, OP_EQ, -1);
  1334. tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.94 &&
  1335. tdata->opt->PathsNeededToBuildCircuits < 0.96);
  1336. expect_log_msg("PathsNeededToBuildCircuits is "
  1337. "too high. Decreasing to 0.95\n");
  1338. tor_free(msg);
  1339. free_options_test_data(tdata);
  1340. mock_clean_saved_logs();
  1341. tdata = get_options_test_data("PathsNeededToBuildCircuits 0.91\n"
  1342. "ConnLimit 1\n"
  1343. "SchedulerHighWaterMark__ 42\n"
  1344. "SchedulerLowWaterMark__ 10\n");
  1345. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1346. tt_int_op(ret, OP_EQ, -1);
  1347. tt_assert(tdata->opt->PathsNeededToBuildCircuits > 0.90 &&
  1348. tdata->opt->PathsNeededToBuildCircuits < 0.92);
  1349. expect_no_log_entry();
  1350. tor_free(msg);
  1351. done:
  1352. teardown_capture_of_logs();
  1353. free_options_test_data(tdata);
  1354. tor_free(msg);
  1355. }
  1356. static void
  1357. test_options_validate__max_client_circuits(void *ignored)
  1358. {
  1359. (void)ignored;
  1360. int ret;
  1361. char *msg;
  1362. options_test_data_t *tdata = get_options_test_data(
  1363. "MaxClientCircuitsPending 0\n"
  1364. "ConnLimit 1\n"
  1365. "SchedulerHighWaterMark__ 42\n"
  1366. "SchedulerLowWaterMark__ 10\n");
  1367. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1368. tt_int_op(ret, OP_EQ, -1);
  1369. tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024,"
  1370. " but was set to 0");
  1371. tor_free(msg);
  1372. free_options_test_data(tdata);
  1373. tdata = get_options_test_data("MaxClientCircuitsPending 1025\n"
  1374. "ConnLimit 1\n"
  1375. "SchedulerHighWaterMark__ 42\n"
  1376. "SchedulerLowWaterMark__ 10\n");
  1377. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1378. tt_int_op(ret, OP_EQ, -1);
  1379. tt_str_op(msg, OP_EQ, "MaxClientCircuitsPending must be between 1 and 1024,"
  1380. " but was set to 1025");
  1381. tor_free(msg);
  1382. free_options_test_data(tdata);
  1383. tdata = get_options_test_data("MaxClientCircuitsPending 1\n"
  1384. "ConnLimit 1\n"
  1385. "SchedulerHighWaterMark__ 42\n"
  1386. "SchedulerLowWaterMark__ 10\n");
  1387. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1388. tt_int_op(ret, OP_EQ, -1);
  1389. tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
  1390. tor_free(msg);
  1391. done:
  1392. free_options_test_data(tdata);
  1393. tor_free(msg);
  1394. }
  1395. static void
  1396. test_options_validate__ports(void *ignored)
  1397. {
  1398. (void)ignored;
  1399. int ret;
  1400. char *msg;
  1401. options_test_data_t *tdata = get_options_test_data(
  1402. "FirewallPorts 65537\n"
  1403. "MaxClientCircuitsPending 1\n"
  1404. "ConnLimit 1\n"
  1405. "SchedulerHighWaterMark__ 42\n"
  1406. "SchedulerLowWaterMark__ 10\n");
  1407. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1408. tt_int_op(ret, OP_EQ, -1);
  1409. tt_str_op(msg, OP_EQ, "Port '65537' out of range in FirewallPorts");
  1410. tor_free(msg);
  1411. free_options_test_data(tdata);
  1412. tdata = get_options_test_data("FirewallPorts 1\n"
  1413. "LongLivedPorts 124444\n"
  1414. "MaxClientCircuitsPending 1\n"
  1415. "ConnLimit 1\n"
  1416. "SchedulerHighWaterMark__ 42\n"
  1417. "SchedulerLowWaterMark__ 10\n");
  1418. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1419. tt_int_op(ret, OP_EQ, -1);
  1420. tt_str_op(msg, OP_EQ, "Port '124444' out of range in LongLivedPorts");
  1421. tor_free(msg);
  1422. free_options_test_data(tdata);
  1423. tdata = get_options_test_data("FirewallPorts 1\n"
  1424. "LongLivedPorts 2\n"
  1425. "RejectPlaintextPorts 112233\n"
  1426. "MaxClientCircuitsPending 1\n"
  1427. "ConnLimit 1\n"
  1428. "SchedulerHighWaterMark__ 42\n"
  1429. "SchedulerLowWaterMark__ 10\n");
  1430. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1431. tt_int_op(ret, OP_EQ, -1);
  1432. tt_str_op(msg, OP_EQ, "Port '112233' out of range in RejectPlaintextPorts");
  1433. tor_free(msg);
  1434. free_options_test_data(tdata);
  1435. tdata = get_options_test_data("FirewallPorts 1\n"
  1436. "LongLivedPorts 2\n"
  1437. "RejectPlaintextPorts 3\n"
  1438. "WarnPlaintextPorts 65536\n"
  1439. "MaxClientCircuitsPending 1\n"
  1440. "ConnLimit 1\n"
  1441. "SchedulerHighWaterMark__ 42\n"
  1442. "SchedulerLowWaterMark__ 10\n");
  1443. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1444. tt_int_op(ret, OP_EQ, -1);
  1445. tt_str_op(msg, OP_EQ, "Port '65536' out of range in WarnPlaintextPorts");
  1446. tor_free(msg);
  1447. free_options_test_data(tdata);
  1448. tdata = get_options_test_data("FirewallPorts 1\n"
  1449. "LongLivedPorts 2\n"
  1450. "RejectPlaintextPorts 3\n"
  1451. "WarnPlaintextPorts 4\n"
  1452. "MaxClientCircuitsPending 1\n"
  1453. "ConnLimit 1\n"
  1454. "SchedulerHighWaterMark__ 42\n"
  1455. "SchedulerLowWaterMark__ 10\n");
  1456. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1457. tt_int_op(ret, OP_EQ, -1);
  1458. tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
  1459. tor_free(msg);
  1460. done:
  1461. free_options_test_data(tdata);
  1462. tor_free(msg);
  1463. }
  1464. static void
  1465. test_options_validate__reachable_addresses(void *ignored)
  1466. {
  1467. (void)ignored;
  1468. int ret;
  1469. char *msg;
  1470. setup_capture_of_logs(LOG_NOTICE);
  1471. options_test_data_t *tdata = get_options_test_data(
  1472. "FascistFirewall 1\n"
  1473. "MaxClientCircuitsPending 1\n"
  1474. "ConnLimit 1\n"
  1475. "SchedulerHighWaterMark__ 42\n"
  1476. "SchedulerLowWaterMark__ 10\n");
  1477. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1478. tt_int_op(ret, OP_EQ, -1);
  1479. expect_log_msg("Converting FascistFirewall config "
  1480. "option to new format: \"ReachableDirAddresses *:80\"\n");
  1481. tt_str_op(tdata->opt->ReachableDirAddresses->value, OP_EQ, "*:80");
  1482. expect_log_msg("Converting FascistFirewall config "
  1483. "option to new format: \"ReachableORAddresses *:443\"\n");
  1484. tt_str_op(tdata->opt->ReachableORAddresses->value, OP_EQ, "*:443");
  1485. tor_free(msg);
  1486. free_options_test_data(tdata);
  1487. mock_clean_saved_logs();
  1488. tdata = get_options_test_data("FascistFirewall 1\n"
  1489. "ReachableDirAddresses *:81\n"
  1490. "ReachableORAddresses *:444\n"
  1491. "MaxClientCircuitsPending 1\n"
  1492. "ConnLimit 1\n"
  1493. "SchedulerHighWaterMark__ 42\n"
  1494. "SchedulerLowWaterMark__ 10\n");
  1495. tdata->opt->FirewallPorts = smartlist_new();
  1496. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1497. tt_int_op(ret, OP_EQ, -1);
  1498. expect_log_entry();
  1499. tt_str_op(tdata->opt->ReachableDirAddresses->value, OP_EQ, "*:81");
  1500. tt_str_op(tdata->opt->ReachableORAddresses->value, OP_EQ, "*:444");
  1501. tor_free(msg);
  1502. free_options_test_data(tdata);
  1503. mock_clean_saved_logs();
  1504. tdata = get_options_test_data("FascistFirewall 1\n"
  1505. "FirewallPort 123\n"
  1506. "MaxClientCircuitsPending 1\n"
  1507. "ConnLimit 1\n"
  1508. "SchedulerHighWaterMark__ 42\n"
  1509. "SchedulerLowWaterMark__ 10\n");
  1510. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1511. tt_int_op(ret, OP_EQ, -1);
  1512. expect_log_msg("Converting FascistFirewall and "
  1513. "FirewallPorts config options to new format: "
  1514. "\"ReachableAddresses *:123\"\n");
  1515. tt_str_op(tdata->opt->ReachableAddresses->value, OP_EQ, "*:123");
  1516. tor_free(msg);
  1517. free_options_test_data(tdata);
  1518. mock_clean_saved_logs();
  1519. tdata = get_options_test_data("FascistFirewall 1\n"
  1520. "ReachableAddresses *:82\n"
  1521. "ReachableAddresses *:83\n"
  1522. "ReachableAddresses reject *:*\n"
  1523. "MaxClientCircuitsPending 1\n"
  1524. "ConnLimit 1\n"
  1525. "SchedulerHighWaterMark__ 42\n"
  1526. "SchedulerLowWaterMark__ 10\n");
  1527. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1528. tt_int_op(ret, OP_EQ, -1);
  1529. expect_log_entry();
  1530. tt_str_op(tdata->opt->ReachableAddresses->value, OP_EQ, "*:82");
  1531. tor_free(msg);
  1532. #define SERVERS_REACHABLE_MSG "Servers must be able to freely connect to" \
  1533. " the rest of the Internet, so they must not set Reachable*Addresses or" \
  1534. " FascistFirewall or FirewallPorts or ClientUseIPv4 0."
  1535. free_options_test_data(tdata);
  1536. tdata = get_options_test_data("ReachableAddresses *:82\n"
  1537. "ORListenAddress 127.0.0.1:5555\n"
  1538. "ORPort 955\n"
  1539. "MaxClientCircuitsPending 1\n"
  1540. "ConnLimit 1\n"
  1541. "SchedulerHighWaterMark__ 42\n"
  1542. "SchedulerLowWaterMark__ 10\n");
  1543. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1544. tt_int_op(ret, OP_EQ, -1);
  1545. tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
  1546. tor_free(msg);
  1547. free_options_test_data(tdata);
  1548. tdata = get_options_test_data("ReachableORAddresses *:82\n"
  1549. "ORListenAddress 127.0.0.1:5555\n"
  1550. "ORPort 955\n"
  1551. "MaxClientCircuitsPending 1\n"
  1552. "ConnLimit 1\n"
  1553. "SchedulerHighWaterMark__ 42\n"
  1554. "SchedulerLowWaterMark__ 10\n");
  1555. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1556. tt_int_op(ret, OP_EQ, -1);
  1557. tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
  1558. tor_free(msg);
  1559. free_options_test_data(tdata);
  1560. tdata = get_options_test_data("ReachableDirAddresses *:82\n"
  1561. "ORListenAddress 127.0.0.1:5555\n"
  1562. "ORPort 955\n"
  1563. "MaxClientCircuitsPending 1\n"
  1564. "ConnLimit 1\n"
  1565. "SchedulerHighWaterMark__ 42\n"
  1566. "SchedulerLowWaterMark__ 10\n");
  1567. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1568. tt_int_op(ret, OP_EQ, -1);
  1569. tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
  1570. tor_free(msg);
  1571. free_options_test_data(tdata);
  1572. tdata = get_options_test_data("ClientUseIPv4 0\n"
  1573. "ORListenAddress 127.0.0.1:5555\n"
  1574. "ORPort 955\n"
  1575. "MaxClientCircuitsPending 1\n"
  1576. "ConnLimit 1\n"
  1577. "SchedulerHighWaterMark__ 42\n"
  1578. "SchedulerLowWaterMark__ 10\n");
  1579. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1580. tt_int_op(ret, OP_EQ, -1);
  1581. tt_str_op(msg, OP_EQ, SERVERS_REACHABLE_MSG);
  1582. tor_free(msg);
  1583. /* Test IPv4-only clients setting IPv6 preferences */
  1584. #define WARN_PLEASE_USE_IPV6_OR_LOG_MSG \
  1585. "ClientPreferIPv6ORPort 1 is ignored unless tor is using IPv6. " \
  1586. "Please set ClientUseIPv6 1, ClientUseIPv4 0, or configure bridges.\n"
  1587. #define WARN_PLEASE_USE_IPV6_DIR_LOG_MSG \
  1588. "ClientPreferIPv6DirPort 1 is ignored unless tor is using IPv6. " \
  1589. "Please set ClientUseIPv6 1, ClientUseIPv4 0, or configure bridges.\n"
  1590. free_options_test_data(tdata);
  1591. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1592. "ClientUseIPv4 1\n"
  1593. "ClientUseIPv6 0\n"
  1594. "UseBridges 0\n"
  1595. "ClientPreferIPv6ORPort 1\n");
  1596. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1597. tt_int_op(ret, OP_EQ, 0);
  1598. expect_log_msg(WARN_PLEASE_USE_IPV6_OR_LOG_MSG);
  1599. tor_free(msg);
  1600. free_options_test_data(tdata);
  1601. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1602. "ClientUseIPv4 1\n"
  1603. "ClientUseIPv6 0\n"
  1604. "UseBridges 0\n"
  1605. "ClientPreferIPv6DirPort 1\n");
  1606. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1607. tt_int_op(ret, OP_EQ, 0);
  1608. expect_log_msg(WARN_PLEASE_USE_IPV6_DIR_LOG_MSG);
  1609. tor_free(msg);
  1610. /* Now test an IPv4/IPv6 client setting IPv6 preferences */
  1611. free_options_test_data(tdata);
  1612. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1613. "ClientUseIPv4 1\n"
  1614. "ClientUseIPv6 1\n"
  1615. "ClientPreferIPv6ORPort 1\n"
  1616. "ClientPreferIPv6DirPort 1\n");
  1617. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1618. tt_int_op(ret, OP_EQ, 0);
  1619. tt_ptr_op(msg, OP_EQ, NULL);
  1620. /* Now test an IPv6 client setting IPv6 preferences */
  1621. free_options_test_data(tdata);
  1622. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1623. "ClientUseIPv6 1\n"
  1624. "ClientPreferIPv6ORPort 1\n"
  1625. "ClientPreferIPv6DirPort 1\n");
  1626. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1627. tt_int_op(ret, OP_EQ, 0);
  1628. tt_ptr_op(msg, OP_EQ, NULL);
  1629. /* And an implicit (IPv4 disabled) IPv6 client setting IPv6 preferences */
  1630. free_options_test_data(tdata);
  1631. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1632. "ClientUseIPv4 0\n"
  1633. "ClientPreferIPv6ORPort 1\n"
  1634. "ClientPreferIPv6DirPort 1\n");
  1635. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1636. tt_int_op(ret, OP_EQ, 0);
  1637. tt_ptr_op(msg, OP_EQ, NULL);
  1638. /* And an implicit (bridge) client setting IPv6 preferences */
  1639. free_options_test_data(tdata);
  1640. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1641. "UseBridges 1\n"
  1642. "Bridge 127.0.0.1:12345\n"
  1643. "ClientPreferIPv6ORPort 1\n"
  1644. "ClientPreferIPv6DirPort 1\n");
  1645. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1646. tt_int_op(ret, OP_EQ, 0);
  1647. tt_ptr_op(msg, OP_EQ, NULL);
  1648. done:
  1649. teardown_capture_of_logs();
  1650. free_options_test_data(tdata);
  1651. tor_free(msg);
  1652. }
  1653. static void
  1654. test_options_validate__use_bridges(void *ignored)
  1655. {
  1656. (void)ignored;
  1657. int ret;
  1658. char *msg;
  1659. options_test_data_t *tdata = get_options_test_data(
  1660. "UseBridges 1\n"
  1661. "ClientUseIPv4 1\n"
  1662. "ORListenAddress 127.0.0.1:5555\n"
  1663. "ORPort 955\n"
  1664. "MaxClientCircuitsPending 1\n"
  1665. "ConnLimit 1\n"
  1666. "SchedulerHighWaterMark__ 42\n"
  1667. "SchedulerLowWaterMark__ 10\n");
  1668. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1669. tt_int_op(ret, OP_EQ, -1);
  1670. tt_str_op(msg, OP_EQ, "Servers must be able to freely connect to the rest of"
  1671. " the Internet, so they must not set UseBridges.");
  1672. tor_free(msg);
  1673. free_options_test_data(tdata);
  1674. tdata = get_options_test_data("UseBridges 1\n"
  1675. "MaxClientCircuitsPending 1\n"
  1676. "ConnLimit 1\n"
  1677. "SchedulerHighWaterMark__ 42\n"
  1678. "SchedulerLowWaterMark__ 10\n");
  1679. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1680. tt_int_op(ret, OP_EQ, -1);
  1681. tt_str_op(msg, OP_NE, "Servers must be able to freely connect to the rest of"
  1682. " the Internet, so they must not set UseBridges.");
  1683. tor_free(msg);
  1684. NS_MOCK(geoip_get_country);
  1685. free_options_test_data(tdata);
  1686. tdata = get_options_test_data("UseBridges 1\n"
  1687. "EntryNodes {cn}\n"
  1688. "MaxClientCircuitsPending 1\n"
  1689. "ConnLimit 1\n"
  1690. "SchedulerHighWaterMark__ 42\n"
  1691. "SchedulerLowWaterMark__ 10\n");
  1692. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1693. tt_int_op(ret, OP_EQ, -1);
  1694. tt_str_op(msg, OP_EQ, "You cannot set both UseBridges and EntryNodes.");
  1695. tor_free(msg);
  1696. free_options_test_data(tdata);
  1697. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1698. "UseBridges 1\n");
  1699. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1700. tt_int_op(ret, OP_EQ, -1);
  1701. tt_str_op(msg, OP_EQ,
  1702. "If you set UseBridges, you must specify at least one bridge.");
  1703. tor_free(msg);
  1704. free_options_test_data(tdata);
  1705. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  1706. "UseBridges 1\n"
  1707. "Bridge 10.0.0.1\n"
  1708. "Bridge !!!\n"
  1709. );
  1710. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1711. tt_int_op(ret, OP_EQ, -1);
  1712. tt_str_op(msg, OP_EQ, "Bridge line did not parse. See logs for details.");
  1713. tor_free(msg);
  1714. done:
  1715. NS_UNMOCK(geoip_get_country);
  1716. policies_free_all();
  1717. free_options_test_data(tdata);
  1718. tor_free(msg);
  1719. }
  1720. static void
  1721. test_options_validate__entry_nodes(void *ignored)
  1722. {
  1723. (void)ignored;
  1724. int ret;
  1725. char *msg;
  1726. NS_MOCK(geoip_get_country);
  1727. options_test_data_t *tdata = get_options_test_data(
  1728. "EntryNodes {cn}\n"
  1729. "UseEntryGuards 0\n"
  1730. "MaxClientCircuitsPending 1\n"
  1731. "ConnLimit 1\n"
  1732. "SchedulerHighWaterMark__ 42\n"
  1733. "SchedulerLowWaterMark__ 10\n");
  1734. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1735. tt_int_op(ret, OP_EQ, -1);
  1736. tt_str_op(msg, OP_EQ,
  1737. "If EntryNodes is set, UseEntryGuards must be enabled.");
  1738. tor_free(msg);
  1739. free_options_test_data(tdata);
  1740. tdata = get_options_test_data("EntryNodes {cn}\n"
  1741. "UseEntryGuards 1\n"
  1742. "MaxClientCircuitsPending 1\n"
  1743. "ConnLimit 1\n"
  1744. "SchedulerHighWaterMark__ 42\n"
  1745. "SchedulerLowWaterMark__ 10\n");
  1746. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1747. tt_int_op(ret, OP_EQ, -1);
  1748. tt_str_op(msg, OP_EQ, "KeepalivePeriod option must be positive.");
  1749. tor_free(msg);
  1750. done:
  1751. NS_UNMOCK(geoip_get_country);
  1752. free_options_test_data(tdata);
  1753. tor_free(msg);
  1754. }
  1755. static void
  1756. test_options_validate__invalid_nodes(void *ignored)
  1757. {
  1758. (void)ignored;
  1759. int ret;
  1760. char *msg;
  1761. options_test_data_t *tdata = get_options_test_data(
  1762. "AllowInvalidNodes something_stupid\n"
  1763. "MaxClientCircuitsPending 1\n"
  1764. "ConnLimit 1\n"
  1765. "SchedulerHighWaterMark__ 42\n"
  1766. "SchedulerLowWaterMark__ 10\n");
  1767. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1768. tt_int_op(ret, OP_EQ, -1);
  1769. tt_str_op(msg, OP_EQ,
  1770. "Unrecognized value 'something_stupid' in AllowInvalidNodes");
  1771. tor_free(msg);
  1772. free_options_test_data(tdata);
  1773. tdata = get_options_test_data("AllowInvalidNodes entry, middle, exit\n"
  1774. "MaxClientCircuitsPending 1\n"
  1775. "ConnLimit 1\n"
  1776. "SchedulerHighWaterMark__ 42\n"
  1777. "SchedulerLowWaterMark__ 10\n");
  1778. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1779. tt_int_op(ret, OP_EQ, -1);
  1780. tt_int_op(tdata->opt->AllowInvalid_, OP_EQ, ALLOW_INVALID_ENTRY |
  1781. ALLOW_INVALID_EXIT | ALLOW_INVALID_MIDDLE);
  1782. tor_free(msg);
  1783. free_options_test_data(tdata);
  1784. tdata = get_options_test_data("AllowInvalidNodes introduction, rendezvous\n"
  1785. "MaxClientCircuitsPending 1\n"
  1786. "ConnLimit 1\n"
  1787. "SchedulerHighWaterMark__ 42\n"
  1788. "SchedulerLowWaterMark__ 10\n");
  1789. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1790. tt_int_op(ret, OP_EQ, -1);
  1791. tt_int_op(tdata->opt->AllowInvalid_, OP_EQ, ALLOW_INVALID_INTRODUCTION |
  1792. ALLOW_INVALID_RENDEZVOUS);
  1793. tor_free(msg);
  1794. done:
  1795. free_options_test_data(tdata);
  1796. tor_free(msg);
  1797. }
  1798. static void
  1799. test_options_validate__safe_logging(void *ignored)
  1800. {
  1801. (void)ignored;
  1802. int ret;
  1803. char *msg;
  1804. options_test_data_t *tdata = get_options_test_data(
  1805. "MaxClientCircuitsPending 1\n"
  1806. "ConnLimit 1\n"
  1807. "SchedulerHighWaterMark__ 42\n"
  1808. "SchedulerLowWaterMark__ 10\n");
  1809. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1810. tt_int_op(ret, OP_EQ, -1);
  1811. tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_NONE);
  1812. tor_free(msg);
  1813. free_options_test_data(tdata);
  1814. tdata = get_options_test_data("SafeLogging 0\n"
  1815. "MaxClientCircuitsPending 1\n"
  1816. "ConnLimit 1\n"
  1817. "SchedulerHighWaterMark__ 42\n"
  1818. "SchedulerLowWaterMark__ 10\n");
  1819. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1820. tt_int_op(ret, OP_EQ, -1);
  1821. tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_NONE);
  1822. tor_free(msg);
  1823. free_options_test_data(tdata);
  1824. tdata = get_options_test_data("SafeLogging Relay\n"
  1825. "MaxClientCircuitsPending 1\n"
  1826. "ConnLimit 1\n"
  1827. "SchedulerHighWaterMark__ 42\n"
  1828. "SchedulerLowWaterMark__ 10\n");
  1829. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1830. tt_int_op(ret, OP_EQ, -1);
  1831. tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_RELAY);
  1832. tor_free(msg);
  1833. free_options_test_data(tdata);
  1834. tdata = get_options_test_data("SafeLogging 1\n"
  1835. "MaxClientCircuitsPending 1\n"
  1836. "ConnLimit 1\n"
  1837. "SchedulerHighWaterMark__ 42\n"
  1838. "SchedulerLowWaterMark__ 10\n");
  1839. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1840. tt_int_op(ret, OP_EQ, -1);
  1841. tt_int_op(tdata->opt->SafeLogging_, OP_EQ, SAFELOG_SCRUB_ALL);
  1842. tor_free(msg);
  1843. free_options_test_data(tdata);
  1844. tdata = get_options_test_data("SafeLogging stuffy\n"
  1845. "MaxClientCircuitsPending 1\n"
  1846. "ConnLimit 1\n"
  1847. "SchedulerHighWaterMark__ 42\n"
  1848. "SchedulerLowWaterMark__ 10\n");
  1849. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1850. tt_int_op(ret, OP_EQ, -1);
  1851. tt_str_op(msg, OP_EQ, "Unrecognized value '\"stuffy\"' in SafeLogging");
  1852. tor_free(msg);
  1853. done:
  1854. escaped(NULL); // This will free the leaking memory from the previous escaped
  1855. free_options_test_data(tdata);
  1856. tor_free(msg);
  1857. }
  1858. static void
  1859. test_options_validate__publish_server_descriptor(void *ignored)
  1860. {
  1861. (void)ignored;
  1862. int ret;
  1863. char *msg;
  1864. setup_capture_of_logs(LOG_WARN);
  1865. options_test_data_t *tdata = get_options_test_data(
  1866. "PublishServerDescriptor bridge\n" TEST_OPTIONS_DEFAULT_VALUES
  1867. );
  1868. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1869. tt_int_op(ret, OP_EQ, 0);
  1870. tt_assert(!msg);
  1871. free_options_test_data(tdata);
  1872. tdata = get_options_test_data("PublishServerDescriptor humma\n"
  1873. TEST_OPTIONS_DEFAULT_VALUES);
  1874. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1875. tt_int_op(ret, OP_EQ, -1);
  1876. tt_str_op(msg, OP_EQ, "Unrecognized value in PublishServerDescriptor");
  1877. tor_free(msg);
  1878. free_options_test_data(tdata);
  1879. tdata = get_options_test_data("PublishServerDescriptor bridge, v3\n"
  1880. TEST_OPTIONS_DEFAULT_VALUES);
  1881. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1882. tt_int_op(ret, OP_EQ, -1);
  1883. tt_str_op(msg, OP_EQ, "Bridges are not supposed to publish router "
  1884. "descriptors to the directory authorities. Please correct your "
  1885. "PublishServerDescriptor line.");
  1886. tor_free(msg);
  1887. free_options_test_data(tdata);
  1888. tdata = get_options_test_data("BridgeRelay 1\n"
  1889. "PublishServerDescriptor v3\n"
  1890. TEST_OPTIONS_DEFAULT_VALUES);
  1891. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1892. tt_int_op(ret, OP_EQ, -1);
  1893. tt_str_op(msg, OP_EQ, "Bridges are not supposed to publish router "
  1894. "descriptors to the directory authorities. Please correct your "
  1895. "PublishServerDescriptor line.");
  1896. tor_free(msg);
  1897. free_options_test_data(tdata);
  1898. tdata = get_options_test_data("BridgeRelay 1\n" TEST_OPTIONS_DEFAULT_VALUES);
  1899. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1900. tt_int_op(ret, OP_EQ, -1);
  1901. tt_str_op(msg, OP_NE, "Bridges are not supposed to publish router "
  1902. "descriptors to the directory authorities. Please correct your "
  1903. "PublishServerDescriptor line.");
  1904. tor_free(msg);
  1905. free_options_test_data(tdata);
  1906. tdata = get_options_test_data("BridgeRelay 1\n"
  1907. "DirPort 999\n" TEST_OPTIONS_DEFAULT_VALUES);
  1908. mock_clean_saved_logs();
  1909. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  1910. tt_int_op(ret, OP_EQ, -1);
  1911. expect_log_msg("Can't set a DirPort on a bridge "
  1912. "relay; disabling DirPort\n");
  1913. tt_assert(!tdata->opt->DirPort_lines);
  1914. tt_assert(!tdata->opt->DirPort_set);
  1915. done:
  1916. teardown_capture_of_logs();
  1917. policies_free_all();
  1918. free_options_test_data(tdata);
  1919. tor_free(msg);
  1920. }
  1921. static void
  1922. test_options_validate__testing(void *ignored)
  1923. {
  1924. (void)ignored;
  1925. int ret;
  1926. char *msg;
  1927. options_test_data_t *tdata = NULL;
  1928. #define ENSURE_DEFAULT(varname, varval) \
  1929. STMT_BEGIN \
  1930. free_options_test_data(tdata); \
  1931. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
  1932. #varname " " #varval "\n"); \
  1933. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  1934. tt_str_op(msg, OP_EQ, \
  1935. #varname " may only be changed in testing Tor networks!"); \
  1936. tt_int_op(ret, OP_EQ, -1); \
  1937. tor_free(msg); \
  1938. \
  1939. free_options_test_data(tdata); \
  1940. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
  1941. #varname " " #varval "\n" \
  1942. VALID_DIR_AUTH \
  1943. "TestingTorNetwork 1\n"); \
  1944. \
  1945. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  1946. if (msg) { \
  1947. tt_str_op(msg, OP_NE, \
  1948. #varname " may only be changed in testing Tor networks!"); \
  1949. tor_free(msg); \
  1950. } \
  1951. \
  1952. free_options_test_data(tdata); \
  1953. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
  1954. #varname " " #varval "\n" \
  1955. "___UsingTestNetworkDefaults 1\n"); \
  1956. \
  1957. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  1958. if (msg) { \
  1959. tt_str_op(msg, OP_NE, \
  1960. #varname " may only be changed in testing Tor networks!"); \
  1961. tor_free(msg); \
  1962. } \
  1963. STMT_END
  1964. ENSURE_DEFAULT(TestingV3AuthInitialVotingInterval, 3600);
  1965. ENSURE_DEFAULT(TestingV3AuthInitialVoteDelay, 3000);
  1966. ENSURE_DEFAULT(TestingV3AuthInitialDistDelay, 3000);
  1967. ENSURE_DEFAULT(TestingV3AuthVotingStartOffset, 3000);
  1968. ENSURE_DEFAULT(TestingAuthDirTimeToLearnReachability, 3000);
  1969. ENSURE_DEFAULT(TestingEstimatedDescriptorPropagationTime, 3000);
  1970. ENSURE_DEFAULT(TestingServerDownloadSchedule, 3000);
  1971. ENSURE_DEFAULT(TestingClientDownloadSchedule, 3000);
  1972. ENSURE_DEFAULT(TestingServerConsensusDownloadSchedule, 3000);
  1973. ENSURE_DEFAULT(TestingClientConsensusDownloadSchedule, 3000);
  1974. ENSURE_DEFAULT(TestingBridgeDownloadSchedule, 3000);
  1975. ENSURE_DEFAULT(TestingClientMaxIntervalWithoutRequest, 3000);
  1976. ENSURE_DEFAULT(TestingDirConnectionMaxStall, 3000);
  1977. ENSURE_DEFAULT(TestingConsensusMaxDownloadTries, 3000);
  1978. ENSURE_DEFAULT(TestingDescriptorMaxDownloadTries, 3000);
  1979. ENSURE_DEFAULT(TestingMicrodescMaxDownloadTries, 3000);
  1980. ENSURE_DEFAULT(TestingCertMaxDownloadTries, 3000);
  1981. ENSURE_DEFAULT(TestingAuthKeyLifetime, 3000);
  1982. ENSURE_DEFAULT(TestingLinkCertLifetime, 3000);
  1983. ENSURE_DEFAULT(TestingSigningKeySlop, 3000);
  1984. ENSURE_DEFAULT(TestingAuthKeySlop, 3000);
  1985. ENSURE_DEFAULT(TestingLinkKeySlop, 3000);
  1986. done:
  1987. escaped(NULL); // This will free the leaking memory from the previous escaped
  1988. policies_free_all();
  1989. free_options_test_data(tdata);
  1990. tor_free(msg);
  1991. }
  1992. static void
  1993. test_options_validate__hidserv(void *ignored)
  1994. {
  1995. (void)ignored;
  1996. int ret;
  1997. char *msg;
  1998. setup_capture_of_logs(LOG_WARN);
  1999. options_test_data_t *tdata = get_options_test_data(
  2000. TEST_OPTIONS_DEFAULT_VALUES);
  2001. tdata->opt->MinUptimeHidServDirectoryV2 = -1;
  2002. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2003. tt_int_op(ret, OP_EQ, 0);
  2004. expect_log_msg("MinUptimeHidServDirectoryV2 "
  2005. "option must be at least 0 seconds. Changing to 0.\n");
  2006. tt_int_op(tdata->opt->MinUptimeHidServDirectoryV2, OP_EQ, 0);
  2007. tor_free(msg);
  2008. free_options_test_data(tdata);
  2009. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2010. "RendPostPeriod 1\n" );
  2011. mock_clean_saved_logs();
  2012. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2013. tt_int_op(ret, OP_EQ, 0);
  2014. expect_log_msg("RendPostPeriod option is too short;"
  2015. " raising to 600 seconds.\n");
  2016. tt_int_op(tdata->opt->RendPostPeriod, OP_EQ, 600);
  2017. tor_free(msg);
  2018. free_options_test_data(tdata);
  2019. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2020. "RendPostPeriod 302401\n" );
  2021. mock_clean_saved_logs();
  2022. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2023. tt_int_op(ret, OP_EQ, 0);
  2024. expect_log_msg("RendPostPeriod is too large; "
  2025. "clipping to 302400s.\n");
  2026. tt_int_op(tdata->opt->RendPostPeriod, OP_EQ, 302400);
  2027. tor_free(msg);
  2028. done:
  2029. teardown_capture_of_logs();
  2030. policies_free_all();
  2031. free_options_test_data(tdata);
  2032. tor_free(msg);
  2033. }
  2034. static void
  2035. test_options_validate__predicted_ports(void *ignored)
  2036. {
  2037. (void)ignored;
  2038. int ret;
  2039. char *msg;
  2040. setup_capture_of_logs(LOG_WARN);
  2041. options_test_data_t *tdata = get_options_test_data(
  2042. "PredictedPortsRelevanceTime 100000000\n"
  2043. TEST_OPTIONS_DEFAULT_VALUES);
  2044. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2045. tt_int_op(ret, OP_EQ, 0);
  2046. expect_log_msg("PredictedPortsRelevanceTime is too "
  2047. "large; clipping to 3600s.\n");
  2048. tt_int_op(tdata->opt->PredictedPortsRelevanceTime, OP_EQ, 3600);
  2049. done:
  2050. teardown_capture_of_logs();
  2051. policies_free_all();
  2052. free_options_test_data(tdata);
  2053. tor_free(msg);
  2054. }
  2055. static void
  2056. test_options_validate__path_bias(void *ignored)
  2057. {
  2058. (void)ignored;
  2059. int ret;
  2060. char *msg;
  2061. options_test_data_t *tdata = get_options_test_data(
  2062. TEST_OPTIONS_DEFAULT_VALUES
  2063. "PathBiasNoticeRate 1.1\n");
  2064. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2065. tt_int_op(ret, OP_EQ, -1);
  2066. tt_str_op(msg, OP_EQ,
  2067. "PathBiasNoticeRate is too high. It must be between 0 and 1.0");
  2068. tor_free(msg);
  2069. free_options_test_data(tdata);
  2070. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2071. "PathBiasWarnRate 1.1\n");
  2072. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2073. tt_int_op(ret, OP_EQ, -1);
  2074. tt_str_op(msg, OP_EQ,
  2075. "PathBiasWarnRate is too high. It must be between 0 and 1.0");
  2076. tor_free(msg);
  2077. free_options_test_data(tdata);
  2078. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2079. "PathBiasExtremeRate 1.1\n");
  2080. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2081. tt_int_op(ret, OP_EQ, -1);
  2082. tt_str_op(msg, OP_EQ,
  2083. "PathBiasExtremeRate is too high. It must be between 0 and 1.0");
  2084. tor_free(msg);
  2085. free_options_test_data(tdata);
  2086. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2087. "PathBiasNoticeUseRate 1.1\n");
  2088. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2089. tt_int_op(ret, OP_EQ, -1);
  2090. tt_str_op(msg, OP_EQ,
  2091. "PathBiasNoticeUseRate is too high. It must be between 0 and 1.0");
  2092. tor_free(msg);
  2093. free_options_test_data(tdata);
  2094. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2095. "PathBiasExtremeUseRate 1.1\n");
  2096. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2097. tt_int_op(ret, OP_EQ, -1);
  2098. tt_str_op(msg, OP_EQ,
  2099. "PathBiasExtremeUseRate is too high. It must be between 0 and 1.0");
  2100. tor_free(msg);
  2101. done:
  2102. free_options_test_data(tdata);
  2103. tor_free(msg);
  2104. }
  2105. static void
  2106. test_options_validate__bandwidth(void *ignored)
  2107. {
  2108. (void)ignored;
  2109. int ret;
  2110. char *msg;
  2111. options_test_data_t *tdata = NULL;
  2112. #define ENSURE_BANDWIDTH_PARAM(p) \
  2113. STMT_BEGIN \
  2114. free_options_test_data(tdata); \
  2115. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES #p " 3Gb\n"); \
  2116. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  2117. tt_int_op(ret, OP_EQ, -1); \
  2118. tt_mem_op(msg, OP_EQ, #p " (3221225471) must be at most 2147483647", 40); \
  2119. tor_free(msg); \
  2120. STMT_END
  2121. ENSURE_BANDWIDTH_PARAM(BandwidthRate);
  2122. ENSURE_BANDWIDTH_PARAM(BandwidthBurst);
  2123. ENSURE_BANDWIDTH_PARAM(MaxAdvertisedBandwidth);
  2124. ENSURE_BANDWIDTH_PARAM(RelayBandwidthRate);
  2125. ENSURE_BANDWIDTH_PARAM(RelayBandwidthBurst);
  2126. ENSURE_BANDWIDTH_PARAM(PerConnBWRate);
  2127. ENSURE_BANDWIDTH_PARAM(PerConnBWBurst);
  2128. ENSURE_BANDWIDTH_PARAM(AuthDirFastGuarantee);
  2129. ENSURE_BANDWIDTH_PARAM(AuthDirGuardBWGuarantee);
  2130. free_options_test_data(tdata);
  2131. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2132. "RelayBandwidthRate 1000\n");
  2133. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2134. tt_int_op(ret, OP_EQ, 0);
  2135. tt_u64_op(tdata->opt->RelayBandwidthBurst, OP_EQ, 1000);
  2136. tor_free(msg);
  2137. free_options_test_data(tdata);
  2138. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2139. "RelayBandwidthBurst 1001\n");
  2140. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2141. tt_int_op(ret, OP_EQ, 0);
  2142. tt_u64_op(tdata->opt->RelayBandwidthRate, OP_EQ, 1001);
  2143. tor_free(msg);
  2144. free_options_test_data(tdata);
  2145. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2146. "RelayBandwidthRate 1001\n"
  2147. "RelayBandwidthBurst 1000\n");
  2148. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2149. tt_int_op(ret, OP_EQ, -1);
  2150. tt_str_op(msg, OP_EQ, "RelayBandwidthBurst must be at least equal to "
  2151. "RelayBandwidthRate.");
  2152. tor_free(msg);
  2153. free_options_test_data(tdata);
  2154. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2155. "BandwidthRate 1001\n"
  2156. "BandwidthBurst 1000\n");
  2157. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2158. tt_int_op(ret, OP_EQ, -1);
  2159. tt_str_op(msg, OP_EQ,
  2160. "BandwidthBurst must be at least equal to BandwidthRate.");
  2161. tor_free(msg);
  2162. free_options_test_data(tdata);
  2163. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2164. "RelayBandwidthRate 1001\n"
  2165. "BandwidthRate 1000\n"
  2166. "BandwidthBurst 1000\n"
  2167. );
  2168. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2169. tt_int_op(ret, OP_EQ, 0);
  2170. tt_u64_op(tdata->opt->BandwidthRate, OP_EQ, 1001);
  2171. tor_free(msg);
  2172. free_options_test_data(tdata);
  2173. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2174. "RelayBandwidthRate 1001\n"
  2175. "BandwidthRate 1000\n"
  2176. "RelayBandwidthBurst 1001\n"
  2177. "BandwidthBurst 1000\n"
  2178. );
  2179. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2180. tt_int_op(ret, OP_EQ, 0);
  2181. tt_u64_op(tdata->opt->BandwidthBurst, OP_EQ, 1001);
  2182. tor_free(msg);
  2183. free_options_test_data(tdata);
  2184. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2185. "ORListenAddress 127.0.0.1:5555\n"
  2186. "ORPort 955\n"
  2187. "BandwidthRate 1\n"
  2188. );
  2189. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2190. tt_int_op(ret, OP_EQ, -1);
  2191. tt_str_op(msg, OP_EQ, "BandwidthRate is set to 1 bytes/second. For servers,"
  2192. " it must be at least 76800.");
  2193. tor_free(msg);
  2194. free_options_test_data(tdata);
  2195. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2196. "ORListenAddress 127.0.0.1:5555\n"
  2197. "ORPort 955\n"
  2198. "BandwidthRate 76800\n"
  2199. "MaxAdvertisedBandwidth 30000\n"
  2200. );
  2201. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2202. tt_int_op(ret, OP_EQ, -1);
  2203. tt_str_op(msg, OP_EQ, "MaxAdvertisedBandwidth is set to 30000 bytes/second."
  2204. " For servers, it must be at least 38400.");
  2205. tor_free(msg);
  2206. free_options_test_data(tdata);
  2207. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2208. "ORListenAddress 127.0.0.1:5555\n"
  2209. "ORPort 955\n"
  2210. "BandwidthRate 76800\n"
  2211. "RelayBandwidthRate 1\n"
  2212. "MaxAdvertisedBandwidth 38400\n"
  2213. );
  2214. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2215. tt_int_op(ret, OP_EQ, -1);
  2216. tt_str_op(msg, OP_EQ, "RelayBandwidthRate is set to 1 bytes/second. For "
  2217. "servers, it must be at least 76800.");
  2218. tor_free(msg);
  2219. free_options_test_data(tdata);
  2220. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2221. "ORListenAddress 127.0.0.1:5555\n"
  2222. "ORPort 955\n"
  2223. "BandwidthRate 76800\n"
  2224. "BandwidthBurst 76800\n"
  2225. "RelayBandwidthRate 76800\n"
  2226. "MaxAdvertisedBandwidth 38400\n"
  2227. );
  2228. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2229. tt_int_op(ret, OP_EQ, 0);
  2230. tor_free(msg);
  2231. done:
  2232. policies_free_all();
  2233. free_options_test_data(tdata);
  2234. tor_free(msg);
  2235. }
  2236. static void
  2237. test_options_validate__circuits(void *ignored)
  2238. {
  2239. (void)ignored;
  2240. char *msg;
  2241. options_test_data_t *tdata = NULL;
  2242. setup_capture_of_logs(LOG_WARN);
  2243. free_options_test_data(tdata);
  2244. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2245. "MaxCircuitDirtiness 2592001\n");
  2246. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2247. expect_log_msg("MaxCircuitDirtiness option is too "
  2248. "high; setting to 30 days.\n");
  2249. tt_int_op(tdata->opt->MaxCircuitDirtiness, OP_EQ, 2592000);
  2250. tor_free(msg);
  2251. free_options_test_data(tdata);
  2252. mock_clean_saved_logs();
  2253. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2254. "CircuitStreamTimeout 1\n");
  2255. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2256. expect_log_msg("CircuitStreamTimeout option is too"
  2257. " short; raising to 10 seconds.\n");
  2258. tt_int_op(tdata->opt->CircuitStreamTimeout, OP_EQ, 10);
  2259. tor_free(msg);
  2260. free_options_test_data(tdata);
  2261. mock_clean_saved_logs();
  2262. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2263. "CircuitStreamTimeout 111\n");
  2264. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2265. expect_no_log_msg("CircuitStreamTimeout option is too"
  2266. " short; raising to 10 seconds.\n");
  2267. tt_int_op(tdata->opt->CircuitStreamTimeout, OP_EQ, 111);
  2268. tor_free(msg);
  2269. free_options_test_data(tdata);
  2270. mock_clean_saved_logs();
  2271. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2272. "HeartbeatPeriod 1\n");
  2273. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2274. expect_log_msg("HeartbeatPeriod option is too short;"
  2275. " raising to 1800 seconds.\n");
  2276. tt_int_op(tdata->opt->HeartbeatPeriod, OP_EQ, 1800);
  2277. tor_free(msg);
  2278. free_options_test_data(tdata);
  2279. mock_clean_saved_logs();
  2280. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2281. "HeartbeatPeriod 1982\n");
  2282. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2283. expect_no_log_msg("HeartbeatPeriod option is too short;"
  2284. " raising to 1800 seconds.\n");
  2285. tt_int_op(tdata->opt->HeartbeatPeriod, OP_EQ, 1982);
  2286. tor_free(msg);
  2287. free_options_test_data(tdata);
  2288. mock_clean_saved_logs();
  2289. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2290. "CircuitBuildTimeout 1\n"
  2291. );
  2292. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2293. expect_log_msg("CircuitBuildTimeout is shorter (1"
  2294. " seconds) than the recommended minimum (10 seconds), and "
  2295. "LearnCircuitBuildTimeout is disabled. If tor isn't working, "
  2296. "raise this value or enable LearnCircuitBuildTimeout.\n");
  2297. tor_free(msg);
  2298. free_options_test_data(tdata);
  2299. mock_clean_saved_logs();
  2300. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2301. "CircuitBuildTimeout 11\n"
  2302. );
  2303. options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2304. expect_no_log_msg("CircuitBuildTimeout is shorter (1 "
  2305. "seconds) than the recommended minimum (10 seconds), and "
  2306. "LearnCircuitBuildTimeout is disabled. If tor isn't working, "
  2307. "raise this value or enable LearnCircuitBuildTimeout.\n");
  2308. tor_free(msg);
  2309. done:
  2310. policies_free_all();
  2311. teardown_capture_of_logs();
  2312. free_options_test_data(tdata);
  2313. tor_free(msg);
  2314. }
  2315. static void
  2316. test_options_validate__port_forwarding(void *ignored)
  2317. {
  2318. (void)ignored;
  2319. int ret;
  2320. char *msg;
  2321. options_test_data_t *tdata = NULL;
  2322. free_options_test_data(tdata);
  2323. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2324. "PortForwarding 1\nSandbox 1\n");
  2325. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2326. tt_int_op(ret, OP_EQ, -1);
  2327. tt_str_op(msg, OP_EQ, "PortForwarding is not compatible with Sandbox;"
  2328. " at most one can be set");
  2329. tor_free(msg);
  2330. free_options_test_data(tdata);
  2331. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2332. "PortForwarding 1\nSandbox 0\n");
  2333. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2334. tt_int_op(ret, OP_EQ, 0);
  2335. tt_assert(!msg);
  2336. tor_free(msg);
  2337. done:
  2338. free_options_test_data(tdata);
  2339. policies_free_all();
  2340. tor_free(msg);
  2341. }
  2342. static void
  2343. test_options_validate__tor2web(void *ignored)
  2344. {
  2345. (void)ignored;
  2346. int ret;
  2347. char *msg;
  2348. options_test_data_t *tdata = NULL;
  2349. free_options_test_data(tdata);
  2350. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2351. "Tor2webRendezvousPoints 1\n");
  2352. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2353. tt_int_op(ret, OP_EQ, -1);
  2354. tt_str_op(msg, OP_EQ,
  2355. "Tor2webRendezvousPoints cannot be set without Tor2webMode.");
  2356. tor_free(msg);
  2357. free_options_test_data(tdata);
  2358. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2359. "Tor2webRendezvousPoints 1\nTor2webMode 1\n");
  2360. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2361. tt_int_op(ret, OP_EQ, 0);
  2362. tor_free(msg);
  2363. done:
  2364. policies_free_all();
  2365. free_options_test_data(tdata);
  2366. tor_free(msg);
  2367. }
  2368. static void
  2369. test_options_validate__rend(void *ignored)
  2370. {
  2371. (void)ignored;
  2372. int ret;
  2373. char *msg;
  2374. options_test_data_t *tdata = NULL;
  2375. setup_capture_of_logs(LOG_WARN);
  2376. free_options_test_data(tdata);
  2377. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2378. "UseEntryGuards 0\n"
  2379. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2380. "HiddenServicePort 80 127.0.0.1:8080\n"
  2381. );
  2382. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2383. tt_int_op(ret, OP_EQ, 0);
  2384. expect_log_msg("UseEntryGuards is disabled, but you"
  2385. " have configured one or more hidden services on this Tor "
  2386. "instance. Your hidden services will be very easy to locate using"
  2387. " a well-known attack -- see http://freehaven.net/anonbib/#hs-"
  2388. "attack06 for details.\n");
  2389. tor_free(msg);
  2390. free_options_test_data(tdata);
  2391. tdata = get_options_test_data(
  2392. TEST_OPTIONS_DEFAULT_VALUES
  2393. "UseEntryGuards 1\n"
  2394. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2395. "HiddenServicePort 80 127.0.0.1:8080\n"
  2396. );
  2397. mock_clean_saved_logs();
  2398. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2399. tt_int_op(ret, OP_EQ, 0);
  2400. expect_no_log_msg("UseEntryGuards is disabled, but you"
  2401. " have configured one or more hidden services on this Tor "
  2402. "instance. Your hidden services will be very easy to locate using"
  2403. " a well-known attack -- see http://freehaven.net/anonbib/#hs-"
  2404. "attack06 for details.\n");
  2405. free_options_test_data(tdata);
  2406. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2407. "HiddenServicePort 80 127.0.0.1:8080\n"
  2408. );
  2409. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2410. tt_int_op(ret, OP_EQ, -1);
  2411. tt_str_op(msg, OP_EQ,
  2412. "Failed to configure rendezvous options. See logs for details.");
  2413. tor_free(msg);
  2414. free_options_test_data(tdata);
  2415. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2416. "HidServAuth failed\n"
  2417. );
  2418. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2419. tt_int_op(ret, OP_EQ, -1);
  2420. tt_str_op(msg, OP_EQ, "Failed to configure client authorization for hidden "
  2421. "services. See logs for details.");
  2422. tor_free(msg);
  2423. done:
  2424. policies_free_all();
  2425. teardown_capture_of_logs();
  2426. free_options_test_data(tdata);
  2427. tor_free(msg);
  2428. }
  2429. static void
  2430. test_options_validate__single_onion(void *ignored)
  2431. {
  2432. (void)ignored;
  2433. int ret;
  2434. char *msg;
  2435. options_test_data_t *tdata = NULL;
  2436. setup_capture_of_logs(LOG_WARN);
  2437. /* Test that HiddenServiceSingleHopMode must come with
  2438. * HiddenServiceNonAnonymousMode */
  2439. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2440. "SOCKSPort 0\n"
  2441. "HiddenServiceSingleHopMode 1\n"
  2442. );
  2443. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2444. tt_int_op(ret, OP_EQ, -1);
  2445. tt_str_op(msg, OP_EQ, "HiddenServiceSingleHopMode does not provide any "
  2446. "server anonymity. It must be used with "
  2447. "HiddenServiceNonAnonymousMode set to 1.");
  2448. tor_free(msg);
  2449. free_options_test_data(tdata);
  2450. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2451. "SOCKSPort 0\n"
  2452. "HiddenServiceSingleHopMode 1\n"
  2453. "HiddenServiceNonAnonymousMode 0\n"
  2454. );
  2455. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2456. tt_int_op(ret, OP_EQ, -1);
  2457. tt_str_op(msg, OP_EQ, "HiddenServiceSingleHopMode does not provide any "
  2458. "server anonymity. It must be used with "
  2459. "HiddenServiceNonAnonymousMode set to 1.");
  2460. tor_free(msg);
  2461. free_options_test_data(tdata);
  2462. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2463. "SOCKSPort 0\n"
  2464. "HiddenServiceSingleHopMode 1\n"
  2465. "HiddenServiceNonAnonymousMode 1\n"
  2466. );
  2467. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2468. tt_int_op(ret, OP_EQ, 0);
  2469. tt_ptr_op(msg, OP_EQ, NULL);
  2470. free_options_test_data(tdata);
  2471. /* Test that SOCKSPort must come with Tor2webMode if
  2472. * HiddenServiceSingleHopMode is 1 */
  2473. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2474. "SOCKSPort 5000\n"
  2475. "HiddenServiceSingleHopMode 1\n"
  2476. "HiddenServiceNonAnonymousMode 1\n"
  2477. "Tor2webMode 0\n"
  2478. );
  2479. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2480. tt_int_op(ret, OP_EQ, -1);
  2481. tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode is incompatible with "
  2482. "using Tor as an anonymous client. Please set "
  2483. "Socks/Trans/NATD/DNSPort to 0, or HiddenServiceNonAnonymousMode "
  2484. "to 0, or use the non-anonymous Tor2webMode.");
  2485. tor_free(msg);
  2486. free_options_test_data(tdata);
  2487. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2488. "SOCKSPort 0\n"
  2489. "HiddenServiceSingleHopMode 1\n"
  2490. "HiddenServiceNonAnonymousMode 1\n"
  2491. "Tor2webMode 0\n"
  2492. );
  2493. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2494. tt_int_op(ret, OP_EQ, 0);
  2495. tt_ptr_op(msg, OP_EQ, NULL);
  2496. free_options_test_data(tdata);
  2497. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2498. "SOCKSPort 5000\n"
  2499. "HiddenServiceSingleHopMode 0\n"
  2500. "Tor2webMode 0\n"
  2501. );
  2502. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2503. tt_int_op(ret, OP_EQ, 0);
  2504. tt_ptr_op(msg, OP_EQ, NULL);
  2505. free_options_test_data(tdata);
  2506. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2507. "SOCKSPort 5000\n"
  2508. "HiddenServiceSingleHopMode 1\n"
  2509. "HiddenServiceNonAnonymousMode 1\n"
  2510. "Tor2webMode 1\n"
  2511. );
  2512. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2513. tt_int_op(ret, OP_EQ, 0);
  2514. tt_ptr_op(msg, OP_EQ, NULL);
  2515. free_options_test_data(tdata);
  2516. /* Test that a hidden service can't be run with Tor2web
  2517. * Use HiddenServiceNonAnonymousMode instead of Tor2webMode, because
  2518. * Tor2webMode requires a compilation #define */
  2519. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2520. "HiddenServiceNonAnonymousMode 1\n"
  2521. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2522. "HiddenServicePort 80 127.0.0.1:8080\n"
  2523. );
  2524. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2525. tt_int_op(ret, OP_EQ, -1);
  2526. tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode does not provide any "
  2527. "server anonymity. It must be used with "
  2528. "HiddenServiceSingleHopMode set to 1.");
  2529. tor_free(msg);
  2530. free_options_test_data(tdata);
  2531. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2532. "HiddenServiceNonAnonymousMode 1\n"
  2533. );
  2534. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2535. tt_int_op(ret, OP_EQ, -1);
  2536. tt_str_op(msg, OP_EQ, "HiddenServiceNonAnonymousMode does not provide any "
  2537. "server anonymity. It must be used with "
  2538. "HiddenServiceSingleHopMode set to 1.");
  2539. tor_free(msg);
  2540. free_options_test_data(tdata);
  2541. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2542. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2543. "HiddenServicePort 80 127.0.0.1:8080\n"
  2544. );
  2545. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2546. tt_int_op(ret, OP_EQ, 0);
  2547. tt_ptr_op(msg, OP_EQ, NULL);
  2548. free_options_test_data(tdata);
  2549. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2550. "HiddenServiceNonAnonymousMode 1\n"
  2551. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2552. "HiddenServicePort 80 127.0.0.1:8080\n"
  2553. "HiddenServiceSingleHopMode 1\n"
  2554. "SOCKSPort 0\n"
  2555. );
  2556. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2557. tt_int_op(ret, OP_EQ, 0);
  2558. tt_ptr_op(msg, OP_EQ, NULL);
  2559. done:
  2560. policies_free_all();
  2561. teardown_capture_of_logs();
  2562. free_options_test_data(tdata);
  2563. tor_free(msg);
  2564. }
  2565. static void
  2566. test_options_validate__accounting(void *ignored)
  2567. {
  2568. (void)ignored;
  2569. int ret;
  2570. char *msg;
  2571. options_test_data_t *tdata = NULL;
  2572. setup_capture_of_logs(LOG_WARN);
  2573. free_options_test_data(tdata);
  2574. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2575. "AccountingRule something_bad\n"
  2576. );
  2577. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2578. tt_int_op(ret, OP_EQ, -1);
  2579. tt_str_op(msg, OP_EQ, "AccountingRule must be 'sum', 'max', 'in', or 'out'");
  2580. tor_free(msg);
  2581. free_options_test_data(tdata);
  2582. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2583. "AccountingRule sum\n"
  2584. );
  2585. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2586. tt_int_op(ret, OP_EQ, 0);
  2587. tt_int_op(tdata->opt->AccountingRule, OP_EQ, ACCT_SUM);
  2588. tor_free(msg);
  2589. free_options_test_data(tdata);
  2590. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2591. "AccountingRule max\n"
  2592. );
  2593. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2594. tt_int_op(ret, OP_EQ, 0);
  2595. tt_int_op(tdata->opt->AccountingRule, OP_EQ, ACCT_MAX);
  2596. tor_free(msg);
  2597. free_options_test_data(tdata);
  2598. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2599. "AccountingStart fail\n"
  2600. );
  2601. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2602. tt_int_op(ret, OP_EQ, -1);
  2603. tt_str_op(msg, OP_EQ,
  2604. "Failed to parse accounting options. See logs for details.");
  2605. tor_free(msg);
  2606. free_options_test_data(tdata);
  2607. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2608. "AccountingMax 10\n"
  2609. );
  2610. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2611. tt_int_op(ret, OP_EQ, 0);
  2612. tor_free(msg);
  2613. free_options_test_data(tdata);
  2614. tdata = get_options_test_data(
  2615. TEST_OPTIONS_DEFAULT_VALUES
  2616. "ORListenAddress 127.0.0.1:5555\n"
  2617. "ORPort 955\n"
  2618. "BandwidthRate 76800\n"
  2619. "BandwidthBurst 76800\n"
  2620. "MaxAdvertisedBandwidth 38400\n"
  2621. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2622. "HiddenServicePort 80 127.0.0.1:8080\n"
  2623. "AccountingMax 10\n"
  2624. );
  2625. mock_clean_saved_logs();
  2626. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2627. tt_int_op(ret, OP_EQ, 0);
  2628. expect_log_msg("Using accounting with a hidden "
  2629. "service and an ORPort is risky: your hidden service(s) and "
  2630. "your public address will all turn off at the same time, "
  2631. "which may alert observers that they are being run by the "
  2632. "same party.\n");
  2633. tor_free(msg);
  2634. free_options_test_data(tdata);
  2635. tdata = get_options_test_data(
  2636. TEST_OPTIONS_DEFAULT_VALUES
  2637. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2638. "HiddenServicePort 80 127.0.0.1:8080\n"
  2639. "AccountingMax 10\n"
  2640. );
  2641. mock_clean_saved_logs();
  2642. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2643. tt_int_op(ret, OP_EQ, 0);
  2644. expect_no_log_msg("Using accounting with a hidden "
  2645. "service and an ORPort is risky: your hidden service(s) and "
  2646. "your public address will all turn off at the same time, "
  2647. "which may alert observers that they are being run by the "
  2648. "same party.\n");
  2649. tor_free(msg);
  2650. free_options_test_data(tdata);
  2651. tdata = get_options_test_data(
  2652. TEST_OPTIONS_DEFAULT_VALUES
  2653. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service/\n"
  2654. "HiddenServicePort 80 127.0.0.1:8080\n"
  2655. "HiddenServiceDir /Library/Tor/var/lib/tor/hidden_service2/\n"
  2656. "HiddenServicePort 81 127.0.0.1:8081\n"
  2657. "AccountingMax 10\n"
  2658. );
  2659. mock_clean_saved_logs();
  2660. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2661. tt_int_op(ret, OP_EQ, 0);
  2662. expect_log_msg("Using accounting with multiple "
  2663. "hidden services is risky: they will all turn off at the same"
  2664. " time, which may alert observers that they are being run by "
  2665. "the same party.\n");
  2666. tor_free(msg);
  2667. done:
  2668. teardown_capture_of_logs();
  2669. policies_free_all();
  2670. free_options_test_data(tdata);
  2671. tor_free(msg);
  2672. }
  2673. static void
  2674. test_options_validate__proxy(void *ignored)
  2675. {
  2676. (void)ignored;
  2677. int ret;
  2678. char *msg;
  2679. options_test_data_t *tdata = NULL;
  2680. sandbox_disable_getaddrinfo_cache();
  2681. setup_capture_of_logs(LOG_WARN);
  2682. free_options_test_data(tdata);
  2683. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2684. "HttpProxy 127.0.42.1\n"
  2685. );
  2686. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2687. tt_int_op(ret, OP_EQ, 0);
  2688. tt_int_op(tdata->opt->HTTPProxyPort, OP_EQ, 80);
  2689. tor_free(msg);
  2690. free_options_test_data(tdata);
  2691. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2692. "HttpProxy 127.0.42.1:444\n"
  2693. );
  2694. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2695. tt_int_op(ret, OP_EQ, 0);
  2696. tt_int_op(tdata->opt->HTTPProxyPort, OP_EQ, 444);
  2697. tor_free(msg);
  2698. free_options_test_data(tdata);
  2699. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2700. "HttpProxy not_so_valid!\n"
  2701. );
  2702. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2703. tt_int_op(ret, OP_EQ, -1);
  2704. tt_str_op(msg, OP_EQ, "HTTPProxy failed to parse or resolve. Please fix.");
  2705. tor_free(msg);
  2706. free_options_test_data(tdata);
  2707. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2708. "HttpProxyAuthenticator "
  2709. "onetwothreonetwothreonetwothreonetwothreonetw"
  2710. "othreonetwothreonetwothreonetwothreonetwothre"
  2711. "onetwothreonetwothreonetwothreonetwothreonetw"
  2712. "othreonetwothreonetwothreonetwothreonetwothre"
  2713. "onetwothreonetwothreonetwothreonetwothreonetw"
  2714. "othreonetwothreonetwothreonetwothreonetwothre"
  2715. "onetwothreonetwothreonetwothreonetwothreonetw"
  2716. "othreonetwothreonetwothreonetwothreonetwothre"
  2717. "onetwothreonetwothreonetwothreonetwothreonetw"
  2718. "othreonetwothreonetwothreonetwothreonetwothre"
  2719. "onetwothreonetwothreonetwothreonetwothreonetw"
  2720. "othreonetwothreeonetwothreeonetwothree"
  2721. );
  2722. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2723. tt_int_op(ret, OP_EQ, -1);
  2724. tt_str_op(msg, OP_EQ, "HTTPProxyAuthenticator is too long (>= 512 chars).");
  2725. tor_free(msg);
  2726. free_options_test_data(tdata);
  2727. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2728. "HttpProxyAuthenticator validauth\n"
  2729. );
  2730. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2731. tt_int_op(ret, OP_EQ, 0);
  2732. tor_free(msg);
  2733. free_options_test_data(tdata);
  2734. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2735. "HttpsProxy 127.0.42.1\n"
  2736. );
  2737. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2738. tt_int_op(ret, OP_EQ, 0);
  2739. tt_int_op(tdata->opt->HTTPSProxyPort, OP_EQ, 443);
  2740. tor_free(msg);
  2741. free_options_test_data(tdata);
  2742. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2743. "HttpsProxy 127.0.42.1:444\n"
  2744. );
  2745. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2746. tt_int_op(ret, OP_EQ, 0);
  2747. tt_int_op(tdata->opt->HTTPSProxyPort, OP_EQ, 444);
  2748. tor_free(msg);
  2749. free_options_test_data(tdata);
  2750. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2751. "HttpsProxy not_so_valid!\n"
  2752. );
  2753. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2754. tt_int_op(ret, OP_EQ, -1);
  2755. tt_str_op(msg, OP_EQ, "HTTPSProxy failed to parse or resolve. Please fix.");
  2756. tor_free(msg);
  2757. free_options_test_data(tdata);
  2758. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2759. "HttpsProxyAuthenticator "
  2760. "onetwothreonetwothreonetwothreonetwothreonetw"
  2761. "othreonetwothreonetwothreonetwothreonetwothre"
  2762. "onetwothreonetwothreonetwothreonetwothreonetw"
  2763. "othreonetwothreonetwothreonetwothreonetwothre"
  2764. "onetwothreonetwothreonetwothreonetwothreonetw"
  2765. "othreonetwothreonetwothreonetwothreonetwothre"
  2766. "onetwothreonetwothreonetwothreonetwothreonetw"
  2767. "othreonetwothreonetwothreonetwothreonetwothre"
  2768. "onetwothreonetwothreonetwothreonetwothreonetw"
  2769. "othreonetwothreonetwothreonetwothreonetwothre"
  2770. "onetwothreonetwothreonetwothreonetwothreonetw"
  2771. "othreonetwothreeonetwothreeonetwothree"
  2772. );
  2773. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2774. tt_int_op(ret, OP_EQ, -1);
  2775. tt_str_op(msg, OP_EQ, "HTTPSProxyAuthenticator is too long (>= 512 chars).");
  2776. tor_free(msg);
  2777. free_options_test_data(tdata);
  2778. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2779. "HttpsProxyAuthenticator validauth\n"
  2780. );
  2781. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2782. tt_int_op(ret, OP_EQ, 0);
  2783. tor_free(msg);
  2784. free_options_test_data(tdata);
  2785. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2786. "Socks4Proxy 127.0.42.1\n"
  2787. );
  2788. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2789. tt_int_op(ret, OP_EQ, 0);
  2790. tt_int_op(tdata->opt->Socks4ProxyPort, OP_EQ, 1080);
  2791. tor_free(msg);
  2792. free_options_test_data(tdata);
  2793. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2794. "Socks4Proxy 127.0.42.1:444\n"
  2795. );
  2796. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2797. tt_int_op(ret, OP_EQ, 0);
  2798. tt_int_op(tdata->opt->Socks4ProxyPort, OP_EQ, 444);
  2799. tor_free(msg);
  2800. free_options_test_data(tdata);
  2801. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2802. "Socks4Proxy not_so_valid!\n"
  2803. );
  2804. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2805. tt_int_op(ret, OP_EQ, -1);
  2806. tt_str_op(msg, OP_EQ, "Socks4Proxy failed to parse or resolve. Please fix.");
  2807. tor_free(msg);
  2808. free_options_test_data(tdata);
  2809. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2810. "Socks5Proxy 127.0.42.1\n"
  2811. );
  2812. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2813. tt_int_op(ret, OP_EQ, 0);
  2814. tt_int_op(tdata->opt->Socks5ProxyPort, OP_EQ, 1080);
  2815. tor_free(msg);
  2816. free_options_test_data(tdata);
  2817. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2818. "Socks5Proxy 127.0.42.1:444\n"
  2819. );
  2820. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2821. tt_int_op(ret, OP_EQ, 0);
  2822. tt_int_op(tdata->opt->Socks5ProxyPort, OP_EQ, 444);
  2823. tor_free(msg);
  2824. free_options_test_data(tdata);
  2825. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2826. "Socks5Proxy not_so_valid!\n"
  2827. );
  2828. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2829. tt_int_op(ret, OP_EQ, -1);
  2830. tt_str_op(msg, OP_EQ, "Socks5Proxy failed to parse or resolve. Please fix.");
  2831. tor_free(msg);
  2832. free_options_test_data(tdata);
  2833. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2834. "Socks4Proxy 215.1.1.1\n"
  2835. "Socks5Proxy 215.1.1.2\n"
  2836. );
  2837. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2838. tt_int_op(ret, OP_EQ, -1);
  2839. tt_str_op(msg, OP_EQ, "You have configured more than one proxy type. "
  2840. "(Socks4Proxy|Socks5Proxy|HTTPSProxy)");
  2841. tor_free(msg);
  2842. free_options_test_data(tdata);
  2843. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2844. "HttpProxy 215.1.1.1\n"
  2845. );
  2846. mock_clean_saved_logs();
  2847. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2848. tt_int_op(ret, OP_EQ, 0);
  2849. expect_log_msg("HTTPProxy configured, but no SOCKS "
  2850. "proxy or HTTPS proxy configured. Watch out: this configuration "
  2851. "will proxy unencrypted directory connections only.\n");
  2852. tor_free(msg);
  2853. free_options_test_data(tdata);
  2854. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2855. "HttpProxy 215.1.1.1\n"
  2856. "Socks4Proxy 215.1.1.1\n"
  2857. );
  2858. mock_clean_saved_logs();
  2859. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2860. tt_int_op(ret, OP_EQ, 0);
  2861. expect_no_log_msg("HTTPProxy configured, but no SOCKS "
  2862. "proxy or HTTPS proxy configured. Watch out: this configuration "
  2863. "will proxy unencrypted directory connections only.\n");
  2864. tor_free(msg);
  2865. free_options_test_data(tdata);
  2866. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2867. "HttpProxy 215.1.1.1\n"
  2868. "Socks5Proxy 215.1.1.1\n"
  2869. );
  2870. mock_clean_saved_logs();
  2871. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2872. tt_int_op(ret, OP_EQ, 0);
  2873. expect_no_log_msg("HTTPProxy configured, but no SOCKS "
  2874. "proxy or HTTPS proxy configured. Watch out: this configuration "
  2875. "will proxy unencrypted directory connections only.\n");
  2876. tor_free(msg);
  2877. free_options_test_data(tdata);
  2878. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2879. "HttpProxy 215.1.1.1\n"
  2880. "HttpsProxy 215.1.1.1\n"
  2881. );
  2882. mock_clean_saved_logs();
  2883. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2884. tt_int_op(ret, OP_EQ, 0);
  2885. expect_no_log_msg(
  2886. "HTTPProxy configured, but no SOCKS proxy or HTTPS proxy "
  2887. "configured. Watch out: this configuration will proxy "
  2888. "unencrypted directory connections only.\n");
  2889. tor_free(msg);
  2890. free_options_test_data(tdata);
  2891. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2892. );
  2893. tdata->opt->Socks5ProxyUsername = tor_strdup("");
  2894. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2895. tt_int_op(ret, OP_EQ, -1);
  2896. tt_str_op(msg, OP_EQ,
  2897. "Socks5ProxyUsername must be between 1 and 255 characters.");
  2898. tor_free(msg);
  2899. free_options_test_data(tdata);
  2900. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2901. );
  2902. tdata->opt->Socks5ProxyUsername =
  2903. tor_strdup("ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789AB"
  2904. "CDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCD"
  2905. "EABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEA"
  2906. "BCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABC"
  2907. "DE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789");
  2908. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2909. tt_int_op(ret, OP_EQ, -1);
  2910. tt_str_op(msg, OP_EQ,
  2911. "Socks5ProxyUsername must be between 1 and 255 characters.");
  2912. tor_free(msg);
  2913. free_options_test_data(tdata);
  2914. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2915. "Socks5ProxyUsername hello_world\n"
  2916. );
  2917. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2918. tt_int_op(ret, OP_EQ, -1);
  2919. tt_str_op(msg, OP_EQ, "Socks5ProxyPassword must be included with "
  2920. "Socks5ProxyUsername.");
  2921. tor_free(msg);
  2922. free_options_test_data(tdata);
  2923. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2924. "Socks5ProxyUsername hello_world\n"
  2925. );
  2926. tdata->opt->Socks5ProxyPassword = tor_strdup("");
  2927. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2928. tt_int_op(ret, OP_EQ, -1);
  2929. tt_str_op(msg, OP_EQ,
  2930. "Socks5ProxyPassword must be between 1 and 255 characters.");
  2931. tor_free(msg);
  2932. free_options_test_data(tdata);
  2933. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2934. "Socks5ProxyUsername hello_world\n"
  2935. );
  2936. tdata->opt->Socks5ProxyPassword =
  2937. tor_strdup("ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789AB"
  2938. "CDEABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCD"
  2939. "EABCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEA"
  2940. "BCDE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789ABCDEABC"
  2941. "DE0123456789ABCDEABCDE0123456789ABCDEABCDE0123456789");
  2942. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2943. tt_int_op(ret, OP_EQ, -1);
  2944. tt_str_op(msg, OP_EQ,
  2945. "Socks5ProxyPassword must be between 1 and 255 characters.");
  2946. tor_free(msg);
  2947. free_options_test_data(tdata);
  2948. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2949. "Socks5ProxyUsername hello_world\n"
  2950. "Socks5ProxyPassword world_hello\n"
  2951. );
  2952. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2953. tt_int_op(ret, OP_EQ, 0);
  2954. tor_free(msg);
  2955. free_options_test_data(tdata);
  2956. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2957. "Socks5ProxyPassword hello_world\n"
  2958. );
  2959. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2960. tt_int_op(ret, OP_EQ, -1);
  2961. tt_str_op(msg, OP_EQ, "Socks5ProxyPassword must be included with "
  2962. "Socks5ProxyUsername.");
  2963. tor_free(msg);
  2964. done:
  2965. teardown_capture_of_logs();
  2966. free_options_test_data(tdata);
  2967. policies_free_all();
  2968. // sandbox_free_getaddrinfo_cache();
  2969. tor_free(msg);
  2970. }
  2971. static void
  2972. test_options_validate__control(void *ignored)
  2973. {
  2974. (void)ignored;
  2975. int ret;
  2976. char *msg;
  2977. options_test_data_t *tdata = NULL;
  2978. setup_capture_of_logs(LOG_WARN);
  2979. free_options_test_data(tdata);
  2980. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2981. "HashedControlPassword something_incorrect\n"
  2982. );
  2983. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2984. tt_int_op(ret, OP_EQ, -1);
  2985. tt_str_op(msg, OP_EQ,
  2986. "Bad HashedControlPassword: wrong length or bad encoding");
  2987. tor_free(msg);
  2988. free_options_test_data(tdata);
  2989. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  2990. "HashedControlPassword 16:872860B76453A77D60CA"
  2991. "2BB8C1A7042072093276A3D701AD684053EC4C\n"
  2992. );
  2993. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  2994. tt_int_op(ret, OP_EQ, 0);
  2995. tor_free(msg);
  2996. free_options_test_data(tdata);
  2997. tdata = get_options_test_data(
  2998. TEST_OPTIONS_DEFAULT_VALUES
  2999. "__HashedControlSessionPassword something_incorrect\n"
  3000. );
  3001. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3002. tt_int_op(ret, OP_EQ, -1);
  3003. tt_str_op(msg, OP_EQ, "Bad HashedControlSessionPassword: wrong length or "
  3004. "bad encoding");
  3005. tor_free(msg);
  3006. free_options_test_data(tdata);
  3007. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3008. "__HashedControlSessionPassword 16:872860B7645"
  3009. "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
  3010. "4C\n"
  3011. );
  3012. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3013. tt_int_op(ret, OP_EQ, 0);
  3014. tor_free(msg);
  3015. free_options_test_data(tdata);
  3016. tdata = get_options_test_data(
  3017. TEST_OPTIONS_DEFAULT_VALUES
  3018. "__OwningControllerProcess something_incorrect\n"
  3019. );
  3020. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3021. tt_int_op(ret, OP_EQ, -1);
  3022. tt_str_op(msg, OP_EQ, "Bad OwningControllerProcess: invalid PID");
  3023. tor_free(msg);
  3024. free_options_test_data(tdata);
  3025. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3026. "__OwningControllerProcess 123\n"
  3027. );
  3028. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3029. tt_int_op(ret, OP_EQ, 0);
  3030. tor_free(msg);
  3031. free_options_test_data(tdata);
  3032. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3033. "ControlPort 127.0.0.1:1234\n"
  3034. );
  3035. mock_clean_saved_logs();
  3036. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3037. tt_int_op(ret, OP_EQ, 0);
  3038. expect_log_msg(
  3039. "ControlPort is open, but no authentication method has been "
  3040. "configured. This means that any program on your computer can "
  3041. "reconfigure your Tor. That's bad! You should upgrade your Tor"
  3042. " controller as soon as possible.\n");
  3043. tor_free(msg);
  3044. free_options_test_data(tdata);
  3045. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3046. "ControlPort 127.0.0.1:1234\n"
  3047. "HashedControlPassword 16:872860B76453A77D60CA"
  3048. "2BB8C1A7042072093276A3D701AD684053EC4C\n"
  3049. );
  3050. mock_clean_saved_logs();
  3051. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3052. tt_int_op(ret, OP_EQ, 0);
  3053. expect_no_log_msg(
  3054. "ControlPort is open, but no authentication method has been "
  3055. "configured. This means that any program on your computer can "
  3056. "reconfigure your Tor. That's bad! You should upgrade your Tor "
  3057. "controller as soon as possible.\n");
  3058. tor_free(msg);
  3059. free_options_test_data(tdata);
  3060. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3061. "ControlPort 127.0.0.1:1234\n"
  3062. "__HashedControlSessionPassword 16:872860B7645"
  3063. "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
  3064. "4C\n"
  3065. );
  3066. mock_clean_saved_logs();
  3067. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3068. tt_int_op(ret, OP_EQ, 0);
  3069. expect_no_log_msg(
  3070. "ControlPort is open, but no authentication method has been "
  3071. "configured. This means that any program on your computer can "
  3072. "reconfigure your Tor. That's bad! You should upgrade your Tor "
  3073. "controller as soon as possible.\n");
  3074. tor_free(msg);
  3075. free_options_test_data(tdata);
  3076. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3077. "ControlPort 127.0.0.1:1234\n"
  3078. "CookieAuthentication 1\n"
  3079. );
  3080. mock_clean_saved_logs();
  3081. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3082. tt_int_op(ret, OP_EQ, 0);
  3083. expect_no_log_msg(
  3084. "ControlPort is open, but no authentication method has been "
  3085. "configured. This means that any program on your computer can "
  3086. "reconfigure your Tor. That's bad! You should upgrade your Tor "
  3087. "controller as soon as possible.\n");
  3088. tor_free(msg);
  3089. #ifdef HAVE_SYS_UN_H
  3090. free_options_test_data(tdata);
  3091. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3092. "ControlSocket unix:/tmp WorldWritable\n"
  3093. );
  3094. mock_clean_saved_logs();
  3095. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3096. tt_int_op(ret, OP_EQ, 0);
  3097. expect_log_msg(
  3098. "ControlSocket is world writable, but no authentication method has"
  3099. " been configured. This means that any program on your computer "
  3100. "can reconfigure your Tor. That's bad! You should upgrade your "
  3101. "Tor controller as soon as possible.\n");
  3102. tor_free(msg);
  3103. free_options_test_data(tdata);
  3104. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3105. "ControlSocket unix:/tmp WorldWritable\n"
  3106. "HashedControlPassword 16:872860B76453A77D60CA"
  3107. "2BB8C1A7042072093276A3D701AD684053EC4C\n"
  3108. );
  3109. mock_clean_saved_logs();
  3110. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3111. tt_int_op(ret, OP_EQ, 0);
  3112. expect_no_log_msg(
  3113. "ControlSocket is world writable, but no authentication method has"
  3114. " been configured. This means that any program on your computer "
  3115. "can reconfigure your Tor. That's bad! You should upgrade your "
  3116. "Tor controller as soon as possible.\n");
  3117. tor_free(msg);
  3118. free_options_test_data(tdata);
  3119. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3120. "ControlSocket unix:/tmp WorldWritable\n"
  3121. "__HashedControlSessionPassword 16:872860B7645"
  3122. "3A77D60CA2BB8C1A7042072093276A3D701AD684053EC"
  3123. "4C\n"
  3124. );
  3125. mock_clean_saved_logs();
  3126. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3127. tt_int_op(ret, OP_EQ, 0);
  3128. expect_no_log_msg(
  3129. "ControlSocket is world writable, but no authentication method has"
  3130. " been configured. This means that any program on your computer "
  3131. "can reconfigure your Tor. That's bad! You should upgrade your "
  3132. "Tor controller as soon as possible.\n");
  3133. tor_free(msg);
  3134. free_options_test_data(tdata);
  3135. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3136. "ControlSocket unix:/tmp WorldWritable\n"
  3137. "CookieAuthentication 1\n"
  3138. );
  3139. mock_clean_saved_logs();
  3140. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3141. tt_int_op(ret, OP_EQ, 0);
  3142. expect_no_log_msg(
  3143. "ControlSocket is world writable, but no authentication method has"
  3144. " been configured. This means that any program on your computer "
  3145. "can reconfigure your Tor. That's bad! You should upgrade your "
  3146. "Tor controller as soon as possible.\n");
  3147. tor_free(msg);
  3148. #endif
  3149. free_options_test_data(tdata);
  3150. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3151. "CookieAuthFileGroupReadable 1\n"
  3152. );
  3153. mock_clean_saved_logs();
  3154. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3155. tt_int_op(ret, OP_EQ, 0);
  3156. expect_log_msg(
  3157. "CookieAuthFileGroupReadable is set, but will have no effect: you "
  3158. "must specify an explicit CookieAuthFile to have it "
  3159. "group-readable.\n");
  3160. tor_free(msg);
  3161. free_options_test_data(tdata);
  3162. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3163. "CookieAuthFileGroupReadable 1\n"
  3164. "CookieAuthFile /tmp/somewhere\n"
  3165. );
  3166. mock_clean_saved_logs();
  3167. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3168. tt_int_op(ret, OP_EQ, 0);
  3169. expect_no_log_msg(
  3170. "CookieAuthFileGroupReadable is set, but will have no effect: you "
  3171. "must specify an explicit CookieAuthFile to have it "
  3172. "group-readable.\n");
  3173. tor_free(msg);
  3174. done:
  3175. teardown_capture_of_logs();
  3176. policies_free_all();
  3177. free_options_test_data(tdata);
  3178. tor_free(msg);
  3179. }
  3180. static void
  3181. test_options_validate__families(void *ignored)
  3182. {
  3183. (void)ignored;
  3184. int ret;
  3185. char *msg;
  3186. options_test_data_t *tdata = NULL;
  3187. setup_capture_of_logs(LOG_WARN);
  3188. free_options_test_data(tdata);
  3189. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3190. "MyFamily home\n"
  3191. "BridgeRelay 1\n"
  3192. "ORListenAddress 127.0.0.1:5555\n"
  3193. "ORPort 955\n"
  3194. "BandwidthRate 51300\n"
  3195. "BandwidthBurst 51300\n"
  3196. "MaxAdvertisedBandwidth 25700\n"
  3197. "DirCache 1\n"
  3198. );
  3199. mock_clean_saved_logs();
  3200. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3201. tt_int_op(ret, OP_EQ, 0);
  3202. expect_log_msg(
  3203. "Listing a family for a bridge relay is not supported: it can "
  3204. "reveal bridge fingerprints to censors. You should also make sure "
  3205. "you aren't listing this bridge's fingerprint in any other "
  3206. "MyFamily.\n");
  3207. tor_free(msg);
  3208. free_options_test_data(tdata);
  3209. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3210. "MyFamily home\n"
  3211. );
  3212. mock_clean_saved_logs();
  3213. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3214. tt_int_op(ret, OP_EQ, 0);
  3215. expect_no_log_msg(
  3216. "Listing a family for a bridge relay is not supported: it can "
  3217. "reveal bridge fingerprints to censors. You should also make sure "
  3218. "you aren't listing this bridge's fingerprint in any other "
  3219. "MyFamily.\n");
  3220. tor_free(msg);
  3221. free_options_test_data(tdata);
  3222. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3223. "MyFamily !\n"
  3224. );
  3225. mock_clean_saved_logs();
  3226. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3227. tt_int_op(ret, OP_EQ, -1);
  3228. tt_str_op(msg, OP_EQ, "Invalid nickname '!' in MyFamily line");
  3229. tor_free(msg);
  3230. free_options_test_data(tdata);
  3231. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3232. "NodeFamily foo\n"
  3233. "NodeFamily !\n"
  3234. );
  3235. mock_clean_saved_logs();
  3236. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3237. tt_int_op(ret, OP_EQ, -1);
  3238. tt_assert(!msg);
  3239. tor_free(msg);
  3240. done:
  3241. teardown_capture_of_logs();
  3242. policies_free_all();
  3243. free_options_test_data(tdata);
  3244. tor_free(msg);
  3245. }
  3246. static void
  3247. test_options_validate__addr_policies(void *ignored)
  3248. {
  3249. (void)ignored;
  3250. int ret;
  3251. char *msg;
  3252. options_test_data_t *tdata = NULL;
  3253. free_options_test_data(tdata);
  3254. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3255. "ExitPolicy !!!\n"
  3256. "ExitRelay 1\n"
  3257. );
  3258. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3259. tt_int_op(ret, OP_EQ, -1);
  3260. tt_str_op(msg, OP_EQ, "Error in ExitPolicy entry.");
  3261. tor_free(msg);
  3262. done:
  3263. policies_free_all();
  3264. free_options_test_data(tdata);
  3265. tor_free(msg);
  3266. }
  3267. static void
  3268. test_options_validate__dir_auth(void *ignored)
  3269. {
  3270. (void)ignored;
  3271. int ret;
  3272. char *msg;
  3273. options_test_data_t *tdata = NULL;
  3274. setup_capture_of_logs(LOG_WARN);
  3275. free_options_test_data(tdata);
  3276. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3277. VALID_DIR_AUTH
  3278. VALID_ALT_DIR_AUTH
  3279. );
  3280. mock_clean_saved_logs();
  3281. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3282. tt_int_op(ret, OP_EQ, -1);
  3283. tt_str_op(msg, OP_EQ,
  3284. "Directory authority/fallback line did not parse. See logs for "
  3285. "details.");
  3286. expect_log_msg(
  3287. "You cannot set both DirAuthority and Alternate*Authority.\n");
  3288. tor_free(msg);
  3289. free_options_test_data(tdata);
  3290. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3291. "TestingTorNetwork 1\n"
  3292. );
  3293. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3294. tt_int_op(ret, OP_EQ, -1);
  3295. tt_str_op(msg, OP_EQ,
  3296. "TestingTorNetwork may only be configured in combination with a "
  3297. "non-default set of DirAuthority or both of AlternateDirAuthority "
  3298. "and AlternateBridgeAuthority configured.");
  3299. tor_free(msg);
  3300. free_options_test_data(tdata);
  3301. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3302. VALID_DIR_AUTH
  3303. "TestingTorNetwork 1\n"
  3304. );
  3305. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3306. tt_int_op(ret, OP_EQ, 0);
  3307. tor_free(msg);
  3308. free_options_test_data(tdata);
  3309. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3310. "TestingTorNetwork 1\n"
  3311. VALID_ALT_DIR_AUTH
  3312. );
  3313. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3314. tt_int_op(ret, OP_EQ, -1);
  3315. tt_str_op(msg, OP_EQ,
  3316. "TestingTorNetwork may only be configured in combination with a "
  3317. "non-default set of DirAuthority or both of AlternateDirAuthority "
  3318. "and AlternateBridgeAuthority configured.");
  3319. tor_free(msg);
  3320. free_options_test_data(tdata);
  3321. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3322. "TestingTorNetwork 1\n"
  3323. VALID_ALT_BRIDGE_AUTH
  3324. );
  3325. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3326. tt_int_op(ret, OP_EQ, -1);
  3327. tt_str_op(msg, OP_EQ, "TestingTorNetwork may only be configured in "
  3328. "combination with a non-default set of DirAuthority or both of "
  3329. "AlternateDirAuthority and AlternateBridgeAuthority configured.");
  3330. tor_free(msg);
  3331. free_options_test_data(tdata);
  3332. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3333. VALID_ALT_DIR_AUTH
  3334. VALID_ALT_BRIDGE_AUTH
  3335. "TestingTorNetwork 1\n"
  3336. );
  3337. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3338. tt_int_op(ret, OP_EQ, 0);
  3339. tor_free(msg);
  3340. done:
  3341. policies_free_all();
  3342. teardown_capture_of_logs();
  3343. free_options_test_data(tdata);
  3344. tor_free(msg);
  3345. }
  3346. static void
  3347. test_options_validate__transport(void *ignored)
  3348. {
  3349. (void)ignored;
  3350. int ret;
  3351. char *msg;
  3352. options_test_data_t *tdata = NULL;
  3353. setup_capture_of_logs(LOG_NOTICE);
  3354. free_options_test_data(tdata);
  3355. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3356. "ClientTransportPlugin !!\n"
  3357. );
  3358. mock_clean_saved_logs();
  3359. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3360. tt_int_op(ret, OP_EQ, -1);
  3361. tt_str_op(msg, OP_EQ,
  3362. "Invalid client transport line. See logs for details.");
  3363. expect_log_msg(
  3364. "Too few arguments on ClientTransportPlugin line.\n");
  3365. tor_free(msg);
  3366. free_options_test_data(tdata);
  3367. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3368. "ClientTransportPlugin foo exec bar\n"
  3369. );
  3370. mock_clean_saved_logs();
  3371. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3372. tt_int_op(ret, OP_EQ, 0);
  3373. tor_free(msg);
  3374. free_options_test_data(tdata);
  3375. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3376. "ServerTransportPlugin !!\n"
  3377. );
  3378. mock_clean_saved_logs();
  3379. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3380. tt_int_op(ret, OP_EQ, -1);
  3381. tt_str_op(msg, OP_EQ,
  3382. "Invalid server transport line. See logs for details.");
  3383. expect_log_msg(
  3384. "Too few arguments on ServerTransportPlugin line.\n");
  3385. tor_free(msg);
  3386. free_options_test_data(tdata);
  3387. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3388. "ServerTransportPlugin foo exec bar\n"
  3389. );
  3390. mock_clean_saved_logs();
  3391. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3392. tt_int_op(ret, OP_EQ, 0);
  3393. expect_log_msg(
  3394. "Tor is not configured as a relay but you specified a "
  3395. "ServerTransportPlugin line (\"foo exec bar\"). The "
  3396. "ServerTransportPlugin line will be ignored.\n");
  3397. tor_free(msg);
  3398. free_options_test_data(tdata);
  3399. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3400. "ServerTransportPlugin foo exec bar\n"
  3401. "ORListenAddress 127.0.0.1:5555\n"
  3402. "ORPort 955\n"
  3403. "BandwidthRate 76900\n"
  3404. "BandwidthBurst 76900\n"
  3405. "MaxAdvertisedBandwidth 38500\n"
  3406. );
  3407. mock_clean_saved_logs();
  3408. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3409. tt_int_op(ret, OP_EQ, 0);
  3410. expect_no_log_msg(
  3411. "Tor is not configured as a relay but you specified a "
  3412. "ServerTransportPlugin line (\"foo exec bar\"). The "
  3413. "ServerTransportPlugin line will be ignored.\n");
  3414. tor_free(msg);
  3415. free_options_test_data(tdata);
  3416. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3417. "ServerTransportListenAddr foo 127.0.0.42:55\n"
  3418. "ServerTransportListenAddr !\n"
  3419. );
  3420. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3421. tt_int_op(ret, OP_EQ, -1);
  3422. tt_str_op(msg, OP_EQ,
  3423. "ServerTransportListenAddr did not parse. See logs for details.");
  3424. tor_free(msg);
  3425. free_options_test_data(tdata);
  3426. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3427. "ServerTransportListenAddr foo 127.0.0.42:55\n"
  3428. );
  3429. mock_clean_saved_logs();
  3430. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3431. tt_int_op(ret, OP_EQ, 0);
  3432. expect_log_msg(
  3433. "You need at least a single managed-proxy to specify a transport "
  3434. "listen address. The ServerTransportListenAddr line will be "
  3435. "ignored.\n");
  3436. tor_free(msg);
  3437. free_options_test_data(tdata);
  3438. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3439. "ServerTransportListenAddr foo 127.0.0.42:55\n"
  3440. "ServerTransportPlugin foo exec bar\n"
  3441. "ORListenAddress 127.0.0.1:5555\n"
  3442. "ORPort 955\n"
  3443. "BandwidthRate 76900\n"
  3444. "BandwidthBurst 76900\n"
  3445. "MaxAdvertisedBandwidth 38500\n"
  3446. );
  3447. mock_clean_saved_logs();
  3448. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3449. tt_int_op(ret, OP_EQ, 0);
  3450. expect_no_log_msg(
  3451. "You need at least a single managed-proxy to specify a transport "
  3452. "listen address. The ServerTransportListenAddr line will be "
  3453. "ignored.\n");
  3454. done:
  3455. escaped(NULL); // This will free the leaking memory from the previous escaped
  3456. policies_free_all();
  3457. teardown_capture_of_logs();
  3458. free_options_test_data(tdata);
  3459. tor_free(msg);
  3460. }
  3461. static void
  3462. test_options_validate__constrained_sockets(void *ignored)
  3463. {
  3464. (void)ignored;
  3465. int ret;
  3466. char *msg;
  3467. options_test_data_t *tdata = NULL;
  3468. setup_capture_of_logs(LOG_WARN);
  3469. free_options_test_data(tdata);
  3470. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3471. "ConstrainedSockets 1\n"
  3472. "ConstrainedSockSize 0\n"
  3473. );
  3474. mock_clean_saved_logs();
  3475. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3476. tt_int_op(ret, OP_EQ, -1);
  3477. tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
  3478. "between 2048 and 262144 in 1024 byte increments.");
  3479. tor_free(msg);
  3480. free_options_test_data(tdata);
  3481. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3482. "ConstrainedSockets 1\n"
  3483. "ConstrainedSockSize 263168\n"
  3484. );
  3485. mock_clean_saved_logs();
  3486. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3487. tt_int_op(ret, OP_EQ, -1);
  3488. tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
  3489. "between 2048 and 262144 in 1024 byte increments.");
  3490. tor_free(msg);
  3491. free_options_test_data(tdata);
  3492. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3493. "ConstrainedSockets 1\n"
  3494. "ConstrainedSockSize 2047\n"
  3495. );
  3496. mock_clean_saved_logs();
  3497. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3498. tt_int_op(ret, OP_EQ, -1);
  3499. tt_str_op(msg, OP_EQ, "ConstrainedSockSize is invalid. Must be a value "
  3500. "between 2048 and 262144 in 1024 byte increments.");
  3501. tor_free(msg);
  3502. free_options_test_data(tdata);
  3503. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3504. "ConstrainedSockets 1\n"
  3505. "ConstrainedSockSize 2048\n"
  3506. "DirPort 999\n"
  3507. "DirCache 1\n"
  3508. );
  3509. mock_clean_saved_logs();
  3510. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3511. tt_int_op(ret, OP_EQ, 0);
  3512. expect_log_msg("You have requested constrained "
  3513. "socket buffers while also serving directory entries via DirPort."
  3514. " It is strongly suggested that you disable serving directory"
  3515. " requests when system TCP buffer resources are scarce.\n");
  3516. tor_free(msg);
  3517. free_options_test_data(tdata);
  3518. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3519. "ConstrainedSockets 1\n"
  3520. "ConstrainedSockSize 2048\n"
  3521. );
  3522. mock_clean_saved_logs();
  3523. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3524. tt_int_op(ret, OP_EQ, 0);
  3525. expect_no_log_msg(
  3526. "You have requested constrained socket buffers while also serving"
  3527. " directory entries via DirPort. It is strongly suggested that "
  3528. "you disable serving directory requests when system TCP buffer "
  3529. "resources are scarce.\n");
  3530. tor_free(msg);
  3531. done:
  3532. policies_free_all();
  3533. teardown_capture_of_logs();
  3534. free_options_test_data(tdata);
  3535. tor_free(msg);
  3536. }
  3537. static void
  3538. test_options_validate__v3_auth(void *ignored)
  3539. {
  3540. (void)ignored;
  3541. int ret;
  3542. char *msg;
  3543. options_test_data_t *tdata = NULL;
  3544. setup_capture_of_logs(LOG_WARN);
  3545. free_options_test_data(tdata);
  3546. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3547. "V3AuthVoteDelay 1000\n"
  3548. "V3AuthDistDelay 1000\n"
  3549. "V3AuthVotingInterval 1000\n"
  3550. );
  3551. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3552. tt_int_op(ret, OP_EQ, -1);
  3553. tt_str_op(msg, OP_EQ,
  3554. "V3AuthVoteDelay plus V3AuthDistDelay must be less than half "
  3555. "V3AuthVotingInterval");
  3556. tor_free(msg);
  3557. free_options_test_data(tdata);
  3558. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3559. "V3AuthVoteDelay 1\n"
  3560. );
  3561. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3562. tt_int_op(ret, OP_EQ, -1);
  3563. tt_str_op(msg, OP_EQ, "V3AuthVoteDelay is way too low.");
  3564. tor_free(msg);
  3565. free_options_test_data(tdata);
  3566. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3567. "V3AuthVoteDelay 1\n"
  3568. "TestingTorNetwork 1\n"
  3569. );
  3570. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3571. tt_int_op(ret, OP_EQ, -1);
  3572. tt_str_op(msg, OP_EQ, "V3AuthVoteDelay is way too low.");
  3573. tor_free(msg);
  3574. // TODO: we can't reach the case of v3authvotedelay lower
  3575. // than MIN_VOTE_SECONDS but not lower than MIN_VOTE_SECONDS_TESTING,
  3576. // since they are the same
  3577. free_options_test_data(tdata);
  3578. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3579. "V3AuthDistDelay 1\n"
  3580. );
  3581. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3582. tt_int_op(ret, OP_EQ, -1);
  3583. tt_str_op(msg, OP_EQ, "V3AuthDistDelay is way too low.");
  3584. tor_free(msg);
  3585. free_options_test_data(tdata);
  3586. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3587. "V3AuthDistDelay 1\n"
  3588. "TestingTorNetwork 1\n"
  3589. );
  3590. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3591. tt_int_op(ret, OP_EQ, -1);
  3592. tt_str_op(msg, OP_EQ, "V3AuthDistDelay is way too low.");
  3593. tor_free(msg);
  3594. // TODO: we can't reach the case of v3authdistdelay lower than
  3595. // MIN_DIST_SECONDS but not lower than MIN_DIST_SECONDS_TESTING,
  3596. // since they are the same
  3597. free_options_test_data(tdata);
  3598. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3599. "V3AuthNIntervalsValid 1\n"
  3600. );
  3601. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3602. tt_int_op(ret, OP_EQ, -1);
  3603. tt_str_op(msg, OP_EQ, "V3AuthNIntervalsValid must be at least 2.");
  3604. tor_free(msg);
  3605. free_options_test_data(tdata);
  3606. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3607. "V3AuthVoteDelay 49\n"
  3608. "V3AuthDistDelay 49\n"
  3609. "V3AuthVotingInterval 200\n"
  3610. );
  3611. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3612. tt_int_op(ret, OP_EQ, -1);
  3613. tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely low.");
  3614. tor_free(msg);
  3615. free_options_test_data(tdata);
  3616. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3617. "V3AuthVoteDelay 49\n"
  3618. "V3AuthDistDelay 49\n"
  3619. "V3AuthVotingInterval 200000\n"
  3620. );
  3621. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3622. tt_int_op(ret, OP_EQ, -1);
  3623. tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely high.");
  3624. tor_free(msg);
  3625. free_options_test_data(tdata);
  3626. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3627. "V3AuthVoteDelay 49\n"
  3628. "V3AuthDistDelay 49\n"
  3629. "V3AuthVotingInterval 1441\n"
  3630. );
  3631. mock_clean_saved_logs();
  3632. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3633. tt_int_op(ret, OP_EQ, 0);
  3634. expect_log_msg("V3AuthVotingInterval does not divide"
  3635. " evenly into 24 hours.\n");
  3636. tor_free(msg);
  3637. free_options_test_data(tdata);
  3638. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3639. "V3AuthVoteDelay 49\n"
  3640. "V3AuthDistDelay 49\n"
  3641. "V3AuthVotingInterval 1440\n"
  3642. );
  3643. mock_clean_saved_logs();
  3644. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3645. tt_int_op(ret, OP_EQ, 0);
  3646. expect_no_log_msg("V3AuthVotingInterval does not divide"
  3647. " evenly into 24 hours.\n");
  3648. tor_free(msg);
  3649. free_options_test_data(tdata);
  3650. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3651. "V3AuthVoteDelay 49\n"
  3652. "V3AuthDistDelay 49\n"
  3653. "V3AuthVotingInterval 299\n"
  3654. VALID_DIR_AUTH
  3655. "TestingTorNetwork 1\n"
  3656. );
  3657. mock_clean_saved_logs();
  3658. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3659. tt_int_op(ret, OP_EQ, 0);
  3660. expect_log_msg("V3AuthVotingInterval is very low. "
  3661. "This may lead to failure to synchronise for a consensus.\n");
  3662. tor_free(msg);
  3663. // TODO: It is impossible to reach the case of testingtor network, with
  3664. // v3authvotinginterval too low
  3665. /* free_options_test_data(tdata); */
  3666. /* tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES */
  3667. /* "V3AuthVoteDelay 1\n" */
  3668. /* "V3AuthDistDelay 1\n" */
  3669. /* "V3AuthVotingInterval 9\n" */
  3670. /* VALID_DIR_AUTH */
  3671. /* "TestingTorNetwork 1\n" */
  3672. /* ); */
  3673. /* ret = options_validate(tdata->old_opt, tdata->opt, */
  3674. /* tdata->def_opt, 0, &msg); */
  3675. /* tt_int_op(ret, OP_EQ, -1); */
  3676. /* tt_str_op(msg, OP_EQ, "V3AuthVotingInterval is insanely low."); */
  3677. free_options_test_data(tdata);
  3678. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3679. "TestingV3AuthInitialVoteDelay 1\n"
  3680. VALID_DIR_AUTH
  3681. "TestingTorNetwork 1\n"
  3682. );
  3683. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3684. tt_int_op(ret, OP_EQ, -1);
  3685. tt_str_op(msg, OP_EQ, "TestingV3AuthInitialVoteDelay is way too low.");
  3686. tor_free(msg);
  3687. free_options_test_data(tdata);
  3688. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3689. "TestingV3AuthInitialDistDelay 1\n"
  3690. VALID_DIR_AUTH
  3691. "TestingTorNetwork 1\n"
  3692. );
  3693. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3694. tt_int_op(ret, OP_EQ, -1);
  3695. tt_str_op(msg, OP_EQ, "TestingV3AuthInitialDistDelay is way too low.");
  3696. tor_free(msg);
  3697. free_options_test_data(tdata);
  3698. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3699. VALID_DIR_AUTH
  3700. "TestingTorNetwork 1\n"
  3701. );
  3702. tdata->opt->TestingV3AuthVotingStartOffset = 100000;
  3703. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3704. tt_int_op(ret, OP_EQ, -1);
  3705. tt_str_op(msg, OP_EQ, "TestingV3AuthVotingStartOffset is higher than the "
  3706. "voting interval.");
  3707. tor_free(msg);
  3708. free_options_test_data(tdata);
  3709. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3710. VALID_DIR_AUTH
  3711. "TestingTorNetwork 1\n"
  3712. );
  3713. tdata->opt->TestingV3AuthVotingStartOffset = -1;
  3714. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3715. tt_int_op(ret, OP_EQ, -1);
  3716. tt_str_op(msg, OP_EQ,
  3717. "TestingV3AuthVotingStartOffset must be non-negative.");
  3718. tor_free(msg);
  3719. free_options_test_data(tdata);
  3720. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3721. VALID_DIR_AUTH
  3722. "TestingTorNetwork 1\n"
  3723. "TestingV3AuthInitialVotingInterval 4\n"
  3724. );
  3725. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3726. tt_int_op(ret, OP_EQ, -1);
  3727. tt_str_op(msg, OP_EQ, "TestingV3AuthInitialVotingInterval is insanely low.");
  3728. tor_free(msg);
  3729. done:
  3730. policies_free_all();
  3731. teardown_capture_of_logs();
  3732. free_options_test_data(tdata);
  3733. tor_free(msg);
  3734. }
  3735. static void
  3736. test_options_validate__virtual_addr(void *ignored)
  3737. {
  3738. (void)ignored;
  3739. int ret;
  3740. char *msg;
  3741. options_test_data_t *tdata = NULL;
  3742. free_options_test_data(tdata);
  3743. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3744. "VirtualAddrNetworkIPv4 !!"
  3745. );
  3746. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3747. tt_int_op(ret, OP_EQ, -1);
  3748. tt_str_op(msg, OP_EQ, "Error parsing VirtualAddressNetwork !!");
  3749. tor_free(msg);
  3750. free_options_test_data(tdata);
  3751. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3752. "VirtualAddrNetworkIPv6 !!"
  3753. );
  3754. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3755. tt_int_op(ret, OP_EQ, -1);
  3756. tt_str_op(msg, OP_EQ, "Error parsing VirtualAddressNetworkIPv6 !!");
  3757. tor_free(msg);
  3758. done:
  3759. escaped(NULL); // This will free the leaking memory from the previous escaped
  3760. policies_free_all();
  3761. free_options_test_data(tdata);
  3762. tor_free(msg);
  3763. }
  3764. static void
  3765. test_options_validate__exits(void *ignored)
  3766. {
  3767. (void)ignored;
  3768. int ret;
  3769. char *msg;
  3770. options_test_data_t *tdata = NULL;
  3771. setup_capture_of_logs(LOG_WARN);
  3772. free_options_test_data(tdata);
  3773. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3774. "AllowSingleHopExits 1"
  3775. );
  3776. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3777. tt_int_op(ret, OP_EQ, 0);
  3778. expect_log_msg("You have set AllowSingleHopExits; "
  3779. "now your relay will allow others to make one-hop exits. However,"
  3780. " since by default most clients avoid relays that set this option,"
  3781. " most clients will ignore you.\n");
  3782. tor_free(msg);
  3783. free_options_test_data(tdata);
  3784. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3785. "AllowSingleHopExits 1\n"
  3786. VALID_DIR_AUTH
  3787. );
  3788. mock_clean_saved_logs();
  3789. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3790. tt_int_op(ret, OP_EQ, 0);
  3791. expect_no_log_msg("You have set AllowSingleHopExits; "
  3792. "now your relay will allow others to make one-hop exits. However,"
  3793. " since by default most clients avoid relays that set this option,"
  3794. " most clients will ignore you.\n");
  3795. tor_free(msg);
  3796. done:
  3797. policies_free_all();
  3798. teardown_capture_of_logs();
  3799. free_options_test_data(tdata);
  3800. tor_free(msg);
  3801. }
  3802. static void
  3803. test_options_validate__testing_options(void *ignored)
  3804. {
  3805. (void)ignored;
  3806. int ret;
  3807. char *msg;
  3808. options_test_data_t *tdata = NULL;
  3809. setup_capture_of_logs(LOG_WARN);
  3810. #define TEST_TESTING_OPTION(name, low_val, high_val, err_low) \
  3811. STMT_BEGIN \
  3812. free_options_test_data(tdata); \
  3813. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
  3814. VALID_DIR_AUTH \
  3815. "TestingTorNetwork 1\n" \
  3816. ); \
  3817. tdata->opt-> name = low_val; \
  3818. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  3819. tt_int_op(ret, OP_EQ, -1); \
  3820. tt_str_op(msg, OP_EQ, #name " " err_low); \
  3821. tor_free(msg); \
  3822. \
  3823. free_options_test_data(tdata); \
  3824. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES \
  3825. VALID_DIR_AUTH \
  3826. "TestingTorNetwork 1\n" \
  3827. ); \
  3828. tdata->opt-> name = high_val; \
  3829. mock_clean_saved_logs(); \
  3830. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);\
  3831. tt_int_op(ret, OP_EQ, 0); \
  3832. expect_log_msg( #name " is insanely high.\n"); \
  3833. tor_free(msg); \
  3834. STMT_END
  3835. TEST_TESTING_OPTION(TestingAuthDirTimeToLearnReachability, -1, 8000,
  3836. "must be non-negative.");
  3837. TEST_TESTING_OPTION(TestingEstimatedDescriptorPropagationTime, -1, 3601,
  3838. "must be non-negative.");
  3839. TEST_TESTING_OPTION(TestingClientMaxIntervalWithoutRequest, -1, 3601,
  3840. "is way too low.");
  3841. TEST_TESTING_OPTION(TestingDirConnectionMaxStall, 1, 3601,
  3842. "is way too low.");
  3843. // TODO: I think this points to a bug/regression in options_validate
  3844. TEST_TESTING_OPTION(TestingConsensusMaxDownloadTries, 1, 801,
  3845. "must be greater than 2.");
  3846. TEST_TESTING_OPTION(TestingDescriptorMaxDownloadTries, 1, 801,
  3847. "must be greater than 1.");
  3848. TEST_TESTING_OPTION(TestingMicrodescMaxDownloadTries, 1, 801,
  3849. "must be greater than 1.");
  3850. TEST_TESTING_OPTION(TestingCertMaxDownloadTries, 1, 801,
  3851. "must be greater than 1.");
  3852. free_options_test_data(tdata);
  3853. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3854. "TestingEnableConnBwEvent 1\n"
  3855. );
  3856. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3857. tt_int_op(ret, OP_EQ, -1);
  3858. tt_str_op(msg, OP_EQ, "TestingEnableConnBwEvent may only be changed in "
  3859. "testing Tor networks!");
  3860. tor_free(msg);
  3861. free_options_test_data(tdata);
  3862. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3863. "TestingEnableConnBwEvent 1\n"
  3864. VALID_DIR_AUTH
  3865. "TestingTorNetwork 1\n"
  3866. "___UsingTestNetworkDefaults 0\n"
  3867. );
  3868. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3869. tt_int_op(ret, OP_EQ, 0);
  3870. tt_assert(!msg);
  3871. tor_free(msg);
  3872. free_options_test_data(tdata);
  3873. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3874. "TestingEnableConnBwEvent 1\n"
  3875. VALID_DIR_AUTH
  3876. "TestingTorNetwork 0\n"
  3877. "___UsingTestNetworkDefaults 1\n"
  3878. );
  3879. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3880. tt_int_op(ret, OP_EQ, 0);
  3881. tt_assert(!msg);
  3882. tor_free(msg);
  3883. free_options_test_data(tdata);
  3884. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3885. "TestingEnableCellStatsEvent 1\n"
  3886. );
  3887. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3888. tt_int_op(ret, OP_EQ, -1);
  3889. tt_str_op(msg, OP_EQ, "TestingEnableCellStatsEvent may only be changed in "
  3890. "testing Tor networks!");
  3891. tor_free(msg);
  3892. free_options_test_data(tdata);
  3893. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3894. "TestingEnableCellStatsEvent 1\n"
  3895. VALID_DIR_AUTH
  3896. "TestingTorNetwork 1\n"
  3897. "___UsingTestNetworkDefaults 0\n"
  3898. );
  3899. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3900. tt_int_op(ret, OP_EQ, 0);
  3901. tt_assert(!msg);
  3902. tor_free(msg);
  3903. free_options_test_data(tdata);
  3904. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3905. "TestingEnableCellStatsEvent 1\n"
  3906. VALID_DIR_AUTH
  3907. "TestingTorNetwork 0\n"
  3908. "___UsingTestNetworkDefaults 1\n"
  3909. );
  3910. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3911. tt_int_op(ret, OP_EQ, 0);
  3912. tt_assert(!msg);
  3913. tor_free(msg);
  3914. free_options_test_data(tdata);
  3915. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3916. "TestingEnableTbEmptyEvent 1\n"
  3917. );
  3918. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3919. tt_int_op(ret, OP_EQ, -1);
  3920. tt_str_op(msg, OP_EQ, "TestingEnableTbEmptyEvent may only be changed "
  3921. "in testing Tor networks!");
  3922. tor_free(msg);
  3923. free_options_test_data(tdata);
  3924. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3925. "TestingEnableTbEmptyEvent 1\n"
  3926. VALID_DIR_AUTH
  3927. "TestingTorNetwork 1\n"
  3928. "___UsingTestNetworkDefaults 0\n"
  3929. );
  3930. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3931. tt_int_op(ret, OP_EQ, 0);
  3932. tt_assert(!msg);
  3933. tor_free(msg);
  3934. free_options_test_data(tdata);
  3935. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3936. "TestingEnableTbEmptyEvent 1\n"
  3937. VALID_DIR_AUTH
  3938. "TestingTorNetwork 0\n"
  3939. "___UsingTestNetworkDefaults 1\n"
  3940. );
  3941. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3942. tt_int_op(ret, OP_EQ, 0);
  3943. tt_assert(!msg);
  3944. tor_free(msg);
  3945. done:
  3946. policies_free_all();
  3947. teardown_capture_of_logs();
  3948. free_options_test_data(tdata);
  3949. tor_free(msg);
  3950. }
  3951. static void
  3952. test_options_validate__accel(void *ignored)
  3953. {
  3954. (void)ignored;
  3955. int ret;
  3956. char *msg;
  3957. options_test_data_t *tdata = NULL;
  3958. free_options_test_data(tdata);
  3959. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3960. "AccelName foo\n"
  3961. );
  3962. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3963. tt_int_op(ret, OP_EQ, 0);
  3964. tt_int_op(tdata->opt->HardwareAccel, OP_EQ, 1);
  3965. tor_free(msg);
  3966. free_options_test_data(tdata);
  3967. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3968. "AccelName foo\n"
  3969. );
  3970. tdata->opt->HardwareAccel = 2;
  3971. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3972. tt_int_op(ret, OP_EQ, 0);
  3973. tt_int_op(tdata->opt->HardwareAccel, OP_EQ, 2);
  3974. tor_free(msg);
  3975. free_options_test_data(tdata);
  3976. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3977. "AccelDir 1\n"
  3978. );
  3979. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3980. tt_int_op(ret, OP_EQ, -1);
  3981. tt_str_op(msg, OP_EQ,
  3982. "Can't use hardware crypto accelerator dir without engine name.");
  3983. tor_free(msg);
  3984. free_options_test_data(tdata);
  3985. tdata = get_options_test_data(TEST_OPTIONS_DEFAULT_VALUES
  3986. "AccelDir 1\n"
  3987. "AccelName something\n"
  3988. );
  3989. ret = options_validate(tdata->old_opt, tdata->opt, tdata->def_opt, 0, &msg);
  3990. tt_int_op(ret, OP_EQ, 0);
  3991. tor_free(msg);
  3992. done:
  3993. policies_free_all();
  3994. free_options_test_data(tdata);
  3995. tor_free(msg);
  3996. }
  3997. #define LOCAL_VALIDATE_TEST(name) \
  3998. { "validate__" #name, test_options_validate__ ## name, TT_FORK, NULL, NULL }
  3999. struct testcase_t options_tests[] = {
  4000. { "validate", test_options_validate, TT_FORK, NULL, NULL },
  4001. { "mem_dircache", test_have_enough_mem_for_dircache, TT_FORK, NULL, NULL },
  4002. LOCAL_VALIDATE_TEST(uname_for_server),
  4003. LOCAL_VALIDATE_TEST(outbound_addresses),
  4004. LOCAL_VALIDATE_TEST(data_directory),
  4005. LOCAL_VALIDATE_TEST(nickname),
  4006. LOCAL_VALIDATE_TEST(contactinfo),
  4007. LOCAL_VALIDATE_TEST(logs),
  4008. LOCAL_VALIDATE_TEST(authdir),
  4009. LOCAL_VALIDATE_TEST(relay_with_hidden_services),
  4010. LOCAL_VALIDATE_TEST(transproxy),
  4011. LOCAL_VALIDATE_TEST(exclude_nodes),
  4012. LOCAL_VALIDATE_TEST(scheduler),
  4013. LOCAL_VALIDATE_TEST(node_families),
  4014. LOCAL_VALIDATE_TEST(tlsec),
  4015. LOCAL_VALIDATE_TEST(token_bucket),
  4016. LOCAL_VALIDATE_TEST(recommended_packages),
  4017. LOCAL_VALIDATE_TEST(fetch_dir),
  4018. LOCAL_VALIDATE_TEST(conn_limit),
  4019. LOCAL_VALIDATE_TEST(paths_needed),
  4020. LOCAL_VALIDATE_TEST(max_client_circuits),
  4021. LOCAL_VALIDATE_TEST(ports),
  4022. LOCAL_VALIDATE_TEST(reachable_addresses),
  4023. LOCAL_VALIDATE_TEST(use_bridges),
  4024. LOCAL_VALIDATE_TEST(entry_nodes),
  4025. LOCAL_VALIDATE_TEST(invalid_nodes),
  4026. LOCAL_VALIDATE_TEST(safe_logging),
  4027. LOCAL_VALIDATE_TEST(publish_server_descriptor),
  4028. LOCAL_VALIDATE_TEST(testing),
  4029. LOCAL_VALIDATE_TEST(hidserv),
  4030. LOCAL_VALIDATE_TEST(predicted_ports),
  4031. LOCAL_VALIDATE_TEST(path_bias),
  4032. LOCAL_VALIDATE_TEST(bandwidth),
  4033. LOCAL_VALIDATE_TEST(circuits),
  4034. LOCAL_VALIDATE_TEST(port_forwarding),
  4035. LOCAL_VALIDATE_TEST(tor2web),
  4036. LOCAL_VALIDATE_TEST(rend),
  4037. LOCAL_VALIDATE_TEST(single_onion),
  4038. LOCAL_VALIDATE_TEST(accounting),
  4039. LOCAL_VALIDATE_TEST(proxy),
  4040. LOCAL_VALIDATE_TEST(control),
  4041. LOCAL_VALIDATE_TEST(families),
  4042. LOCAL_VALIDATE_TEST(addr_policies),
  4043. LOCAL_VALIDATE_TEST(dir_auth),
  4044. LOCAL_VALIDATE_TEST(transport),
  4045. LOCAL_VALIDATE_TEST(constrained_sockets),
  4046. LOCAL_VALIDATE_TEST(v3_auth),
  4047. LOCAL_VALIDATE_TEST(virtual_addr),
  4048. LOCAL_VALIDATE_TEST(exits),
  4049. LOCAL_VALIDATE_TEST(testing_options),
  4050. LOCAL_VALIDATE_TEST(accel),
  4051. END_OF_TESTCASES /* */
  4052. };