test_options.c 163 KB

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