test_options.c 160 KB

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