test_options.c 160 KB

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