test_options.c 175 KB

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