test_options.c 171 KB

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