test_config.c 110 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2015, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #define CONFIG_PRIVATE
  7. #define PT_PRIVATE
  8. #include "or.h"
  9. #include "addressmap.h"
  10. #include "config.h"
  11. #include "confparse.h"
  12. #include "connection_edge.h"
  13. #include "test.h"
  14. #include "util.h"
  15. #include "address.h"
  16. #include "entrynodes.h"
  17. #include "transports.h"
  18. #include "routerlist.h"
  19. static void
  20. test_config_addressmap(void *arg)
  21. {
  22. char buf[1024];
  23. char address[256];
  24. time_t expires = TIME_MAX;
  25. (void)arg;
  26. strlcpy(buf, "MapAddress .invalidwildcard.com *.torserver.exit\n" // invalid
  27. "MapAddress *invalidasterisk.com *.torserver.exit\n" // invalid
  28. "MapAddress *.google.com *.torserver.exit\n"
  29. "MapAddress *.yahoo.com *.google.com.torserver.exit\n"
  30. "MapAddress *.cn.com www.cnn.com\n"
  31. "MapAddress *.cnn.com www.cnn.com\n"
  32. "MapAddress ex.com www.cnn.com\n"
  33. "MapAddress ey.com *.cnn.com\n"
  34. "MapAddress www.torproject.org 1.1.1.1\n"
  35. "MapAddress other.torproject.org "
  36. "this.torproject.org.otherserver.exit\n"
  37. "MapAddress test.torproject.org 2.2.2.2\n"
  38. "MapAddress www.google.com 3.3.3.3\n"
  39. "MapAddress www.example.org 4.4.4.4\n"
  40. "MapAddress 4.4.4.4 7.7.7.7\n"
  41. "MapAddress 4.4.4.4 5.5.5.5\n"
  42. "MapAddress www.infiniteloop.org 6.6.6.6\n"
  43. "MapAddress 6.6.6.6 www.infiniteloop.org\n"
  44. , sizeof(buf));
  45. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  46. config_register_addressmaps(get_options());
  47. /* Use old interface for now, so we don't need to rewrite the unit tests */
  48. #define addressmap_rewrite(a,s,eo,ao) \
  49. addressmap_rewrite((a),(s), ~0, (eo),(ao))
  50. /* MapAddress .invalidwildcard.com .torserver.exit - no match */
  51. strlcpy(address, "www.invalidwildcard.com", sizeof(address));
  52. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  53. /* MapAddress *invalidasterisk.com .torserver.exit - no match */
  54. strlcpy(address, "www.invalidasterisk.com", sizeof(address));
  55. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  56. /* Where no mapping for FQDN match on top-level domain */
  57. /* MapAddress .google.com .torserver.exit */
  58. strlcpy(address, "reader.google.com", sizeof(address));
  59. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  60. tt_str_op(address,OP_EQ, "reader.torserver.exit");
  61. /* MapAddress *.yahoo.com *.google.com.torserver.exit */
  62. strlcpy(address, "reader.yahoo.com", sizeof(address));
  63. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  64. tt_str_op(address,OP_EQ, "reader.google.com.torserver.exit");
  65. /*MapAddress *.cnn.com www.cnn.com */
  66. strlcpy(address, "cnn.com", sizeof(address));
  67. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  68. tt_str_op(address,OP_EQ, "www.cnn.com");
  69. /* MapAddress .cn.com www.cnn.com */
  70. strlcpy(address, "www.cn.com", sizeof(address));
  71. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  72. tt_str_op(address,OP_EQ, "www.cnn.com");
  73. /* MapAddress ex.com www.cnn.com - no match */
  74. strlcpy(address, "www.ex.com", sizeof(address));
  75. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  76. /* MapAddress ey.com *.cnn.com - invalid expression */
  77. strlcpy(address, "ey.com", sizeof(address));
  78. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  79. /* Where mapping for FQDN match on FQDN */
  80. strlcpy(address, "www.google.com", sizeof(address));
  81. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  82. tt_str_op(address,OP_EQ, "3.3.3.3");
  83. strlcpy(address, "www.torproject.org", sizeof(address));
  84. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  85. tt_str_op(address,OP_EQ, "1.1.1.1");
  86. strlcpy(address, "other.torproject.org", sizeof(address));
  87. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  88. tt_str_op(address,OP_EQ, "this.torproject.org.otherserver.exit");
  89. strlcpy(address, "test.torproject.org", sizeof(address));
  90. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  91. tt_str_op(address,OP_EQ, "2.2.2.2");
  92. /* Test a chain of address mappings and the order in which they were added:
  93. "MapAddress www.example.org 4.4.4.4"
  94. "MapAddress 4.4.4.4 7.7.7.7"
  95. "MapAddress 4.4.4.4 5.5.5.5"
  96. */
  97. strlcpy(address, "www.example.org", sizeof(address));
  98. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  99. tt_str_op(address,OP_EQ, "5.5.5.5");
  100. /* Test infinite address mapping results in no change */
  101. strlcpy(address, "www.infiniteloop.org", sizeof(address));
  102. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  103. tt_str_op(address,OP_EQ, "www.infiniteloop.org");
  104. /* Test we don't find false positives */
  105. strlcpy(address, "www.example.com", sizeof(address));
  106. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  107. /* Test top-level-domain matching a bit harder */
  108. config_free_lines(get_options_mutable()->AddressMap);
  109. addressmap_clear_configured();
  110. strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
  111. "MapAddress *.torproject.org 1.1.1.1\n"
  112. "MapAddress *.net 2.2.2.2\n"
  113. , sizeof(buf));
  114. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  115. config_register_addressmaps(get_options());
  116. strlcpy(address, "www.abc.com", sizeof(address));
  117. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  118. tt_str_op(address,OP_EQ, "www.abc.torserver.exit");
  119. strlcpy(address, "www.def.com", sizeof(address));
  120. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  121. tt_str_op(address,OP_EQ, "www.def.torserver.exit");
  122. strlcpy(address, "www.torproject.org", sizeof(address));
  123. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  124. tt_str_op(address,OP_EQ, "1.1.1.1");
  125. strlcpy(address, "test.torproject.org", sizeof(address));
  126. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  127. tt_str_op(address,OP_EQ, "1.1.1.1");
  128. strlcpy(address, "torproject.net", sizeof(address));
  129. tt_assert(addressmap_rewrite(address, sizeof(address), &expires, NULL));
  130. tt_str_op(address,OP_EQ, "2.2.2.2");
  131. /* We don't support '*' as a mapping directive */
  132. config_free_lines(get_options_mutable()->AddressMap);
  133. addressmap_clear_configured();
  134. strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
  135. config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
  136. config_register_addressmaps(get_options());
  137. strlcpy(address, "www.abc.com", sizeof(address));
  138. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  139. strlcpy(address, "www.def.net", sizeof(address));
  140. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  141. strlcpy(address, "www.torproject.org", sizeof(address));
  142. tt_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
  143. #undef addressmap_rewrite
  144. done:
  145. config_free_lines(get_options_mutable()->AddressMap);
  146. get_options_mutable()->AddressMap = NULL;
  147. addressmap_free_all();
  148. }
  149. static int
  150. is_private_dir(const char* path)
  151. {
  152. struct stat st;
  153. int r = stat(path, &st);
  154. if (r) {
  155. return 0;
  156. }
  157. #if !defined (_WIN32)
  158. if ((st.st_mode & (S_IFDIR | 0777)) != (S_IFDIR | 0700)) {
  159. return 0;
  160. }
  161. #endif
  162. return 1;
  163. }
  164. static void
  165. test_config_check_or_create_data_subdir(void *arg)
  166. {
  167. or_options_t *options = get_options_mutable();
  168. char *datadir;
  169. const char *subdir = "test_stats";
  170. char *subpath;
  171. struct stat st;
  172. int r;
  173. #if !defined (_WIN32)
  174. unsigned group_permission;
  175. #endif
  176. (void)arg;
  177. tor_free(options->DataDirectory);
  178. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
  179. subpath = get_datadir_fname(subdir);
  180. #if defined (_WIN32)
  181. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  182. #else
  183. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  184. #endif
  185. r = stat(subpath, &st);
  186. // The subdirectory shouldn't exist yet,
  187. // but should be created by the call to check_or_create_data_subdir.
  188. tt_assert(r && (errno == ENOENT));
  189. tt_assert(!check_or_create_data_subdir(subdir));
  190. tt_assert(is_private_dir(subpath));
  191. // The check should return 0, if the directory already exists
  192. // and is private to the user.
  193. tt_assert(!check_or_create_data_subdir(subdir));
  194. r = stat(subpath, &st);
  195. if (r) {
  196. tt_abort_perror("stat");
  197. }
  198. #if !defined (_WIN32)
  199. group_permission = st.st_mode | 0070;
  200. r = chmod(subpath, group_permission);
  201. if (r) {
  202. tt_abort_perror("chmod");
  203. }
  204. // If the directory exists, but its mode is too permissive
  205. // a call to check_or_create_data_subdir should reset the mode.
  206. tt_assert(!is_private_dir(subpath));
  207. tt_assert(!check_or_create_data_subdir(subdir));
  208. tt_assert(is_private_dir(subpath));
  209. #endif
  210. done:
  211. rmdir(subpath);
  212. tor_free(datadir);
  213. tor_free(subpath);
  214. }
  215. static void
  216. test_config_write_to_data_subdir(void *arg)
  217. {
  218. or_options_t* options = get_options_mutable();
  219. char *datadir;
  220. char *cp = NULL;
  221. const char* subdir = "test_stats";
  222. const char* fname = "test_file";
  223. const char* str =
  224. "Lorem ipsum dolor sit amet, consetetur sadipscing\n"
  225. "elitr, sed diam nonumy eirmod\n"
  226. "tempor invidunt ut labore et dolore magna aliquyam\n"
  227. "erat, sed diam voluptua.\n"
  228. "At vero eos et accusam et justo duo dolores et ea\n"
  229. "rebum. Stet clita kasd gubergren,\n"
  230. "no sea takimata sanctus est Lorem ipsum dolor sit amet.\n"
  231. "Lorem ipsum dolor sit amet,\n"
  232. "consetetur sadipscing elitr, sed diam nonumy eirmod\n"
  233. "tempor invidunt ut labore et dolore\n"
  234. "magna aliquyam erat, sed diam voluptua. At vero eos et\n"
  235. "accusam et justo duo dolores et\n"
  236. "ea rebum. Stet clita kasd gubergren, no sea takimata\n"
  237. "sanctus est Lorem ipsum dolor sit amet.";
  238. char* filepath = NULL;
  239. (void)arg;
  240. tor_free(options->DataDirectory);
  241. datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
  242. filepath = get_datadir_fname2(subdir, fname);
  243. #if defined (_WIN32)
  244. tt_int_op(mkdir(options->DataDirectory), OP_EQ, 0);
  245. #else
  246. tt_int_op(mkdir(options->DataDirectory, 0700), OP_EQ, 0);
  247. #endif
  248. // Write attempt shoudl fail, if subdirectory doesn't exist.
  249. tt_assert(write_to_data_subdir(subdir, fname, str, NULL));
  250. tt_assert(! check_or_create_data_subdir(subdir));
  251. // Content of file after write attempt should be
  252. // equal to the original string.
  253. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  254. cp = read_file_to_str(filepath, 0, NULL);
  255. tt_str_op(cp,OP_EQ, str);
  256. tor_free(cp);
  257. // A second write operation should overwrite the old content.
  258. tt_assert(!write_to_data_subdir(subdir, fname, str, NULL));
  259. cp = read_file_to_str(filepath, 0, NULL);
  260. tt_str_op(cp,OP_EQ, str);
  261. tor_free(cp);
  262. done:
  263. (void) unlink(filepath);
  264. rmdir(options->DataDirectory);
  265. tor_free(datadir);
  266. tor_free(filepath);
  267. tor_free(cp);
  268. }
  269. /* Test helper function: Make sure that a bridge line gets parsed
  270. * properly. Also make sure that the resulting bridge_line_t structure
  271. * has its fields set correctly. */
  272. static void
  273. good_bridge_line_test(const char *string, const char *test_addrport,
  274. const char *test_digest, const char *test_transport,
  275. const smartlist_t *test_socks_args)
  276. {
  277. char *tmp = NULL;
  278. bridge_line_t *bridge_line = parse_bridge_line(string);
  279. tt_assert(bridge_line);
  280. /* test addrport */
  281. tmp = tor_strdup(fmt_addrport(&bridge_line->addr, bridge_line->port));
  282. tt_str_op(test_addrport,OP_EQ, tmp);
  283. tor_free(tmp);
  284. /* If we were asked to validate a digest, but we did not get a
  285. digest after parsing, we failed. */
  286. if (test_digest && tor_digest_is_zero(bridge_line->digest))
  287. tt_assert(0);
  288. /* If we were not asked to validate a digest, and we got a digest
  289. after parsing, we failed again. */
  290. if (!test_digest && !tor_digest_is_zero(bridge_line->digest))
  291. tt_assert(0);
  292. /* If we were asked to validate a digest, and we got a digest after
  293. parsing, make sure it's correct. */
  294. if (test_digest) {
  295. tmp = tor_strdup(hex_str(bridge_line->digest, DIGEST_LEN));
  296. tor_strlower(tmp);
  297. tt_str_op(test_digest,OP_EQ, tmp);
  298. tor_free(tmp);
  299. }
  300. /* If we were asked to validate a transport name, make sure tha it
  301. matches with the transport name that was parsed. */
  302. if (test_transport && !bridge_line->transport_name)
  303. tt_assert(0);
  304. if (!test_transport && bridge_line->transport_name)
  305. tt_assert(0);
  306. if (test_transport)
  307. tt_str_op(test_transport,OP_EQ, bridge_line->transport_name);
  308. /* Validate the SOCKS argument smartlist. */
  309. if (test_socks_args && !bridge_line->socks_args)
  310. tt_assert(0);
  311. if (!test_socks_args && bridge_line->socks_args)
  312. tt_assert(0);
  313. if (test_socks_args)
  314. tt_assert(smartlist_strings_eq(test_socks_args,
  315. bridge_line->socks_args));
  316. done:
  317. tor_free(tmp);
  318. bridge_line_free(bridge_line);
  319. }
  320. /* Test helper function: Make sure that a bridge line is
  321. * unparseable. */
  322. static void
  323. bad_bridge_line_test(const char *string)
  324. {
  325. bridge_line_t *bridge_line = parse_bridge_line(string);
  326. if (bridge_line)
  327. TT_FAIL(("%s was supposed to fail, but it didn't.", string));
  328. tt_assert(!bridge_line);
  329. done:
  330. bridge_line_free(bridge_line);
  331. }
  332. static void
  333. test_config_parse_bridge_line(void *arg)
  334. {
  335. (void) arg;
  336. good_bridge_line_test("192.0.2.1:4123",
  337. "192.0.2.1:4123", NULL, NULL, NULL);
  338. good_bridge_line_test("192.0.2.1",
  339. "192.0.2.1:443", NULL, NULL, NULL);
  340. good_bridge_line_test("transport [::1]",
  341. "[::1]:443", NULL, "transport", NULL);
  342. good_bridge_line_test("transport 192.0.2.1:12 "
  343. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  344. "192.0.2.1:12",
  345. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  346. "transport", NULL);
  347. {
  348. smartlist_t *sl_tmp = smartlist_new();
  349. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  350. good_bridge_line_test("transport 192.0.2.1:12 "
  351. "4352e58420e68f5e40bf7c74faddccd9d1349413 twoandtwo=five",
  352. "192.0.2.1:12", "4352e58420e68f5e40bf7c74faddccd9d1349413",
  353. "transport", sl_tmp);
  354. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  355. smartlist_free(sl_tmp);
  356. }
  357. {
  358. smartlist_t *sl_tmp = smartlist_new();
  359. smartlist_add_asprintf(sl_tmp, "twoandtwo=five");
  360. smartlist_add_asprintf(sl_tmp, "z=z");
  361. good_bridge_line_test("transport 192.0.2.1:12 twoandtwo=five z=z",
  362. "192.0.2.1:12", NULL, "transport", sl_tmp);
  363. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  364. smartlist_free(sl_tmp);
  365. }
  366. {
  367. smartlist_t *sl_tmp = smartlist_new();
  368. smartlist_add_asprintf(sl_tmp, "dub=come");
  369. smartlist_add_asprintf(sl_tmp, "save=me");
  370. good_bridge_line_test("transport 192.0.2.1:12 "
  371. "4352e58420e68f5e40bf7c74faddccd9d1349666 "
  372. "dub=come save=me",
  373. "192.0.2.1:12",
  374. "4352e58420e68f5e40bf7c74faddccd9d1349666",
  375. "transport", sl_tmp);
  376. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  377. smartlist_free(sl_tmp);
  378. }
  379. good_bridge_line_test("192.0.2.1:1231 "
  380. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  381. "192.0.2.1:1231",
  382. "4352e58420e68f5e40bf7c74faddccd9d1349413",
  383. NULL, NULL);
  384. /* Empty line */
  385. bad_bridge_line_test("");
  386. /* bad transport name */
  387. bad_bridge_line_test("tr$n_sp0r7 190.20.2.2");
  388. /* weird ip address */
  389. bad_bridge_line_test("a.b.c.d");
  390. /* invalid fpr */
  391. bad_bridge_line_test("2.2.2.2:1231 4352e58420e68f5e40bf7c74faddccd9d1349");
  392. /* no k=v in the end */
  393. bad_bridge_line_test("obfs2 2.2.2.2:1231 "
  394. "4352e58420e68f5e40bf7c74faddccd9d1349413 what");
  395. /* no addrport */
  396. bad_bridge_line_test("asdw");
  397. /* huge k=v value that can't fit in SOCKS fields */
  398. bad_bridge_line_test(
  399. "obfs2 2.2.2.2:1231 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  400. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  401. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  402. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  403. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  404. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  405. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  406. "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  407. "aa=b");
  408. }
  409. static void
  410. test_config_parse_transport_options_line(void *arg)
  411. {
  412. smartlist_t *options_sl = NULL, *sl_tmp = NULL;
  413. (void) arg;
  414. { /* too small line */
  415. options_sl = get_options_from_transport_options_line("valley", NULL);
  416. tt_assert(!options_sl);
  417. }
  418. { /* no k=v values */
  419. options_sl = get_options_from_transport_options_line("hit it!", NULL);
  420. tt_assert(!options_sl);
  421. }
  422. { /* correct line, but wrong transport specified */
  423. options_sl =
  424. get_options_from_transport_options_line("trebuchet k=v", "rook");
  425. tt_assert(!options_sl);
  426. }
  427. { /* correct -- no transport specified */
  428. sl_tmp = smartlist_new();
  429. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  430. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  431. options_sl =
  432. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  433. NULL);
  434. tt_assert(options_sl);
  435. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  436. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  437. smartlist_free(sl_tmp);
  438. sl_tmp = NULL;
  439. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  440. smartlist_free(options_sl);
  441. options_sl = NULL;
  442. }
  443. { /* correct -- correct transport specified */
  444. sl_tmp = smartlist_new();
  445. smartlist_add_asprintf(sl_tmp, "ladi=dadi");
  446. smartlist_add_asprintf(sl_tmp, "weliketo=party");
  447. options_sl =
  448. get_options_from_transport_options_line("rook ladi=dadi weliketo=party",
  449. "rook");
  450. tt_assert(options_sl);
  451. tt_assert(smartlist_strings_eq(options_sl, sl_tmp));
  452. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  453. smartlist_free(sl_tmp);
  454. sl_tmp = NULL;
  455. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  456. smartlist_free(options_sl);
  457. options_sl = NULL;
  458. }
  459. done:
  460. if (options_sl) {
  461. SMARTLIST_FOREACH(options_sl, char *, s, tor_free(s));
  462. smartlist_free(options_sl);
  463. }
  464. if (sl_tmp) {
  465. SMARTLIST_FOREACH(sl_tmp, char *, s, tor_free(s));
  466. smartlist_free(sl_tmp);
  467. }
  468. }
  469. /* Mocks needed for the transport plugin line test */
  470. static void pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  471. char **proxy_argv, int is_server);
  472. static int transport_add_from_config_mock(const tor_addr_t *addr,
  473. uint16_t port, const char *name,
  474. int socks_ver);
  475. static int transport_is_needed_mock(const char *transport_name);
  476. static int pt_kickstart_proxy_mock_call_count = 0;
  477. static int transport_add_from_config_mock_call_count = 0;
  478. static int transport_is_needed_mock_call_count = 0;
  479. static int transport_is_needed_mock_return = 0;
  480. static void
  481. pt_kickstart_proxy_mock(const smartlist_t *transport_list,
  482. char **proxy_argv, int is_server)
  483. {
  484. (void) transport_list;
  485. (void) proxy_argv;
  486. (void) is_server;
  487. /* XXXX check that args are as expected. */
  488. ++pt_kickstart_proxy_mock_call_count;
  489. free_execve_args(proxy_argv);
  490. }
  491. static int
  492. transport_add_from_config_mock(const tor_addr_t *addr,
  493. uint16_t port, const char *name,
  494. int socks_ver)
  495. {
  496. (void) addr;
  497. (void) port;
  498. (void) name;
  499. (void) socks_ver;
  500. /* XXXX check that args are as expected. */
  501. ++transport_add_from_config_mock_call_count;
  502. return 0;
  503. }
  504. static int
  505. transport_is_needed_mock(const char *transport_name)
  506. {
  507. (void) transport_name;
  508. /* XXXX check that arg is as expected. */
  509. ++transport_is_needed_mock_call_count;
  510. return transport_is_needed_mock_return;
  511. }
  512. /**
  513. * Test parsing for the ClientTransportPlugin and ServerTransportPlugin config
  514. * options.
  515. */
  516. static void
  517. test_config_parse_transport_plugin_line(void *arg)
  518. {
  519. (void)arg;
  520. or_options_t *options = get_options_mutable();
  521. int r, tmp;
  522. int old_pt_kickstart_proxy_mock_call_count;
  523. int old_transport_add_from_config_mock_call_count;
  524. int old_transport_is_needed_mock_call_count;
  525. /* Bad transport lines - too short */
  526. r = parse_transport_line(options, "bad", 1, 0);
  527. tt_assert(r < 0);
  528. r = parse_transport_line(options, "bad", 1, 1);
  529. tt_assert(r < 0);
  530. r = parse_transport_line(options, "bad bad", 1, 0);
  531. tt_assert(r < 0);
  532. r = parse_transport_line(options, "bad bad", 1, 1);
  533. tt_assert(r < 0);
  534. /* Test transport list parsing */
  535. r = parse_transport_line(options,
  536. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  537. tt_assert(r == 0);
  538. r = parse_transport_line(options,
  539. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  540. tt_assert(r == 0);
  541. r = parse_transport_line(options,
  542. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 0);
  543. tt_assert(r == 0);
  544. r = parse_transport_line(options,
  545. "transport_1,transport_2 exec /usr/bin/fake-transport", 1, 1);
  546. tt_assert(r == 0);
  547. /* Bad transport identifiers */
  548. r = parse_transport_line(options,
  549. "transport_* exec /usr/bin/fake-transport", 1, 0);
  550. tt_assert(r < 0);
  551. r = parse_transport_line(options,
  552. "transport_* exec /usr/bin/fake-transport", 1, 1);
  553. tt_assert(r < 0);
  554. /* Check SOCKS cases for client transport */
  555. r = parse_transport_line(options,
  556. "transport_1 socks4 1.2.3.4:567", 1, 0);
  557. tt_assert(r == 0);
  558. r = parse_transport_line(options,
  559. "transport_1 socks5 1.2.3.4:567", 1, 0);
  560. tt_assert(r == 0);
  561. /* Proxy case for server transport */
  562. r = parse_transport_line(options,
  563. "transport_1 proxy 1.2.3.4:567", 1, 1);
  564. tt_assert(r == 0);
  565. /* Multiple-transport error exit */
  566. r = parse_transport_line(options,
  567. "transport_1,transport_2 socks5 1.2.3.4:567", 1, 0);
  568. tt_assert(r < 0);
  569. r = parse_transport_line(options,
  570. "transport_1,transport_2 proxy 1.2.3.4:567", 1, 1);
  571. /* No port error exit */
  572. r = parse_transport_line(options,
  573. "transport_1 socks5 1.2.3.4", 1, 0);
  574. tt_assert(r < 0);
  575. r = parse_transport_line(options,
  576. "transport_1 proxy 1.2.3.4", 1, 1);
  577. tt_assert(r < 0);
  578. /* Unparsable address error exit */
  579. r = parse_transport_line(options,
  580. "transport_1 socks5 1.2.3:6x7", 1, 0);
  581. tt_assert(r < 0);
  582. r = parse_transport_line(options,
  583. "transport_1 proxy 1.2.3:6x7", 1, 1);
  584. tt_assert(r < 0);
  585. /* "Strange {Client|Server}TransportPlugin field" error exit */
  586. r = parse_transport_line(options,
  587. "transport_1 foo bar", 1, 0);
  588. tt_assert(r < 0);
  589. r = parse_transport_line(options,
  590. "transport_1 foo bar", 1, 1);
  591. tt_assert(r < 0);
  592. /* No sandbox mode error exit */
  593. tmp = options->Sandbox;
  594. options->Sandbox = 1;
  595. r = parse_transport_line(options,
  596. "transport_1 exec /usr/bin/fake-transport", 1, 0);
  597. tt_assert(r < 0);
  598. r = parse_transport_line(options,
  599. "transport_1 exec /usr/bin/fake-transport", 1, 1);
  600. tt_assert(r < 0);
  601. options->Sandbox = tmp;
  602. /*
  603. * These final test cases cover code paths that only activate without
  604. * validate_only, so they need mocks in place.
  605. */
  606. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  607. old_pt_kickstart_proxy_mock_call_count =
  608. pt_kickstart_proxy_mock_call_count;
  609. r = parse_transport_line(options,
  610. "transport_1 exec /usr/bin/fake-transport", 0, 1);
  611. tt_assert(r == 0);
  612. tt_assert(pt_kickstart_proxy_mock_call_count ==
  613. old_pt_kickstart_proxy_mock_call_count + 1);
  614. UNMOCK(pt_kickstart_proxy);
  615. /* This one hits a log line in the !validate_only case only */
  616. r = parse_transport_line(options,
  617. "transport_1 proxy 1.2.3.4:567", 0, 1);
  618. tt_assert(r == 0);
  619. /* Check mocked client transport cases */
  620. MOCK(pt_kickstart_proxy, pt_kickstart_proxy_mock);
  621. MOCK(transport_add_from_config, transport_add_from_config_mock);
  622. MOCK(transport_is_needed, transport_is_needed_mock);
  623. /* Unnecessary transport case */
  624. transport_is_needed_mock_return = 0;
  625. old_pt_kickstart_proxy_mock_call_count =
  626. pt_kickstart_proxy_mock_call_count;
  627. old_transport_add_from_config_mock_call_count =
  628. transport_add_from_config_mock_call_count;
  629. old_transport_is_needed_mock_call_count =
  630. transport_is_needed_mock_call_count;
  631. r = parse_transport_line(options,
  632. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  633. /* Should have succeeded */
  634. tt_assert(r == 0);
  635. /* transport_is_needed() should have been called */
  636. tt_assert(transport_is_needed_mock_call_count ==
  637. old_transport_is_needed_mock_call_count + 1);
  638. /*
  639. * pt_kickstart_proxy() and transport_add_from_config() should
  640. * not have been called.
  641. */
  642. tt_assert(pt_kickstart_proxy_mock_call_count ==
  643. old_pt_kickstart_proxy_mock_call_count);
  644. tt_assert(transport_add_from_config_mock_call_count ==
  645. old_transport_add_from_config_mock_call_count);
  646. /* Necessary transport case */
  647. transport_is_needed_mock_return = 1;
  648. old_pt_kickstart_proxy_mock_call_count =
  649. pt_kickstart_proxy_mock_call_count;
  650. old_transport_add_from_config_mock_call_count =
  651. transport_add_from_config_mock_call_count;
  652. old_transport_is_needed_mock_call_count =
  653. transport_is_needed_mock_call_count;
  654. r = parse_transport_line(options,
  655. "transport_1 exec /usr/bin/fake-transport", 0, 0);
  656. /* Should have succeeded */
  657. tt_assert(r == 0);
  658. /*
  659. * transport_is_needed() and pt_kickstart_proxy() should have been
  660. * called.
  661. */
  662. tt_assert(pt_kickstart_proxy_mock_call_count ==
  663. old_pt_kickstart_proxy_mock_call_count + 1);
  664. tt_assert(transport_is_needed_mock_call_count ==
  665. old_transport_is_needed_mock_call_count + 1);
  666. /* transport_add_from_config() should not have been called. */
  667. tt_assert(transport_add_from_config_mock_call_count ==
  668. old_transport_add_from_config_mock_call_count);
  669. /* proxy case */
  670. transport_is_needed_mock_return = 1;
  671. old_pt_kickstart_proxy_mock_call_count =
  672. pt_kickstart_proxy_mock_call_count;
  673. old_transport_add_from_config_mock_call_count =
  674. transport_add_from_config_mock_call_count;
  675. old_transport_is_needed_mock_call_count =
  676. transport_is_needed_mock_call_count;
  677. r = parse_transport_line(options,
  678. "transport_1 socks5 1.2.3.4:567", 0, 0);
  679. /* Should have succeeded */
  680. tt_assert(r == 0);
  681. /*
  682. * transport_is_needed() and transport_add_from_config() should have
  683. * been called.
  684. */
  685. tt_assert(transport_add_from_config_mock_call_count ==
  686. old_transport_add_from_config_mock_call_count + 1);
  687. tt_assert(transport_is_needed_mock_call_count ==
  688. old_transport_is_needed_mock_call_count + 1);
  689. /* pt_kickstart_proxy() should not have been called. */
  690. tt_assert(pt_kickstart_proxy_mock_call_count ==
  691. old_pt_kickstart_proxy_mock_call_count);
  692. /* Done with mocked client transport cases */
  693. UNMOCK(transport_is_needed);
  694. UNMOCK(transport_add_from_config);
  695. UNMOCK(pt_kickstart_proxy);
  696. done:
  697. /* Make sure we undo all mocks */
  698. UNMOCK(pt_kickstart_proxy);
  699. UNMOCK(transport_add_from_config);
  700. UNMOCK(transport_is_needed);
  701. return;
  702. }
  703. // Tests if an options with MyFamily fingerprints missing '$' normalises
  704. // them correctly and also ensure it also works with multiple fingerprints
  705. static void
  706. test_config_fix_my_family(void *arg)
  707. {
  708. char *err = NULL;
  709. const char *family = "$1111111111111111111111111111111111111111, "
  710. "1111111111111111111111111111111111111112, "
  711. "$1111111111111111111111111111111111111113";
  712. or_options_t* options = options_new();
  713. or_options_t* defaults = options_new();
  714. (void) arg;
  715. options_init(options);
  716. options_init(defaults);
  717. options->MyFamily = tor_strdup(family);
  718. options_validate(NULL, options, defaults, 0, &err) ;
  719. if (err != NULL) {
  720. TT_FAIL(("options_validate failed: %s", err));
  721. }
  722. tt_str_op(options->MyFamily,OP_EQ,
  723. "$1111111111111111111111111111111111111111, "
  724. "$1111111111111111111111111111111111111112, "
  725. "$1111111111111111111111111111111111111113");
  726. done:
  727. if (err != NULL) {
  728. tor_free(err);
  729. }
  730. or_options_free(options);
  731. or_options_free(defaults);
  732. }
  733. static int n_hostname_01010101 = 0;
  734. /** This mock function is meant to replace tor_lookup_hostname().
  735. * It answers with 1.1.1.1 as IP adddress that resulted from lookup.
  736. * This function increments <b>n_hostname_01010101</b> counter by one
  737. * every time it is called.
  738. */
  739. static int
  740. tor_lookup_hostname_01010101(const char *name, uint32_t *addr)
  741. {
  742. n_hostname_01010101++;
  743. if (name && addr) {
  744. *addr = ntohl(0x01010101);
  745. }
  746. return 0;
  747. }
  748. static int n_hostname_localhost = 0;
  749. /** This mock function is meant to replace tor_lookup_hostname().
  750. * It answers with 127.0.0.1 as IP adddress that resulted from lookup.
  751. * This function increments <b>n_hostname_localhost</b> counter by one
  752. * every time it is called.
  753. */
  754. static int
  755. tor_lookup_hostname_localhost(const char *name, uint32_t *addr)
  756. {
  757. n_hostname_localhost++;
  758. if (name && addr) {
  759. *addr = 0x7f000001;
  760. }
  761. return 0;
  762. }
  763. static int n_hostname_failure = 0;
  764. /** This mock function is meant to replace tor_lookup_hostname().
  765. * It pretends to fail by returning -1 to caller. Also, this function
  766. * increments <b>n_hostname_failure</b> every time it is called.
  767. */
  768. static int
  769. tor_lookup_hostname_failure(const char *name, uint32_t *addr)
  770. {
  771. (void)name;
  772. (void)addr;
  773. n_hostname_failure++;
  774. return -1;
  775. }
  776. static int n_gethostname_replacement = 0;
  777. /** This mock function is meant to replace tor_gethostname(). It
  778. * responds with string "onionrouter!" as hostname. This function
  779. * increments <b>n_gethostname_replacement</b> by one every time
  780. * it is called.
  781. */
  782. static int
  783. tor_gethostname_replacement(char *name, size_t namelen)
  784. {
  785. n_gethostname_replacement++;
  786. if (name && namelen) {
  787. strlcpy(name,"onionrouter!",namelen);
  788. }
  789. return 0;
  790. }
  791. static int n_gethostname_localhost = 0;
  792. /** This mock function is meant to replace tor_gethostname(). It
  793. * responds with string "127.0.0.1" as hostname. This function
  794. * increments <b>n_gethostname_localhost</b> by one every time
  795. * it is called.
  796. */
  797. static int
  798. tor_gethostname_localhost(char *name, size_t namelen)
  799. {
  800. n_gethostname_localhost++;
  801. if (name && namelen) {
  802. strlcpy(name,"127.0.0.1",namelen);
  803. }
  804. return 0;
  805. }
  806. static int n_gethostname_failure = 0;
  807. /** This mock function is meant to replace tor_gethostname.
  808. * It pretends to fail by returning -1. This function increments
  809. * <b>n_gethostname_failure</b> by one every time it is called.
  810. */
  811. static int
  812. tor_gethostname_failure(char *name, size_t namelen)
  813. {
  814. (void)name;
  815. (void)namelen;
  816. n_gethostname_failure++;
  817. return -1;
  818. }
  819. static int n_get_interface_address = 0;
  820. /** This mock function is meant to replace get_interface_address().
  821. * It answers with address 8.8.8.8. This function increments
  822. * <b>n_get_interface_address</b> by one every time it is called.
  823. */
  824. static int
  825. get_interface_address_08080808(int severity, uint32_t *addr)
  826. {
  827. (void)severity;
  828. n_get_interface_address++;
  829. if (addr) {
  830. *addr = ntohl(0x08080808);
  831. }
  832. return 0;
  833. }
  834. static int n_get_interface_address6 = 0;
  835. static sa_family_t last_address6_family;
  836. /** This mock function is meant to replace get_interface_address6().
  837. * It answers with IP address 9.9.9.9 iff both of the following are true:
  838. * - <b>family</b> is AF_INET
  839. * - <b>addr</b> pointer is not NULL.
  840. * This function increments <b>n_get_interface_address6</b> by one every
  841. * time it is called.
  842. */
  843. static int
  844. get_interface_address6_replacement(int severity, sa_family_t family,
  845. tor_addr_t *addr)
  846. {
  847. (void)severity;
  848. last_address6_family = family;
  849. n_get_interface_address6++;
  850. if ((family != AF_INET) || !addr) {
  851. return -1;
  852. }
  853. tor_addr_from_ipv4h(addr,0x09090909);
  854. return 0;
  855. }
  856. static int n_get_interface_address_failure = 0;
  857. /**
  858. * This mock function is meant to replace get_interface_address().
  859. * It pretends to fail getting interface address by returning -1.
  860. * <b>n_get_interface_address_failure</b> is incremented by one
  861. * every time this function is called.
  862. */
  863. static int
  864. get_interface_address_failure(int severity, uint32_t *addr)
  865. {
  866. (void)severity;
  867. (void)addr;
  868. n_get_interface_address_failure++;
  869. return -1;
  870. }
  871. static int n_get_interface_address6_failure = 0;
  872. /**
  873. * This mock function is meant to replace get_interface_addres6().
  874. * It will pretend to fail by return -1.
  875. * <b>n_get_interface_address6_failure</b> is incremented by one
  876. * every time this function is called and <b>last_address6_family</b>
  877. * is assigned the value of <b>family</b> argument.
  878. */
  879. static int
  880. get_interface_address6_failure(int severity, sa_family_t family,
  881. tor_addr_t *addr)
  882. {
  883. (void)severity;
  884. (void)addr;
  885. n_get_interface_address6_failure++;
  886. last_address6_family = family;
  887. return -1;
  888. }
  889. static void
  890. test_config_resolve_my_address(void *arg)
  891. {
  892. or_options_t *options;
  893. uint32_t resolved_addr;
  894. const char *method_used;
  895. char *hostname_out = NULL;
  896. int retval;
  897. int prev_n_hostname_01010101;
  898. int prev_n_hostname_localhost;
  899. int prev_n_hostname_failure;
  900. int prev_n_gethostname_replacement;
  901. int prev_n_gethostname_failure;
  902. int prev_n_gethostname_localhost;
  903. int prev_n_get_interface_address;
  904. int prev_n_get_interface_address_failure;
  905. int prev_n_get_interface_address6;
  906. int prev_n_get_interface_address6_failure;
  907. (void)arg;
  908. options = options_new();
  909. options_init(options);
  910. /*
  911. * CASE 1:
  912. * If options->Address is a valid IPv4 address string, we want
  913. * the corresponding address to be parsed and returned.
  914. */
  915. options->Address = tor_strdup("128.52.128.105");
  916. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  917. &method_used,&hostname_out);
  918. tt_want(retval == 0);
  919. tt_want_str_op(method_used,==,"CONFIGURED");
  920. tt_want(hostname_out == NULL);
  921. tt_assert(resolved_addr == 0x80348069);
  922. tor_free(options->Address);
  923. /*
  924. * CASE 2:
  925. * If options->Address is a valid DNS address, we want resolve_my_address()
  926. * function to ask tor_lookup_hostname() for help with resolving it
  927. * and return the address that was resolved (in host order).
  928. */
  929. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  930. tor_free(options->Address);
  931. options->Address = tor_strdup("www.torproject.org");
  932. prev_n_hostname_01010101 = n_hostname_01010101;
  933. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  934. &method_used,&hostname_out);
  935. tt_want(retval == 0);
  936. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  937. tt_want_str_op(method_used,==,"RESOLVED");
  938. tt_want_str_op(hostname_out,==,"www.torproject.org");
  939. tt_assert(resolved_addr == 0x01010101);
  940. UNMOCK(tor_lookup_hostname);
  941. tor_free(options->Address);
  942. tor_free(hostname_out);
  943. /*
  944. * CASE 3:
  945. * Given that options->Address is NULL, we want resolve_my_address()
  946. * to try and use tor_gethostname() to get hostname AND use
  947. * tor_lookup_hostname() to get IP address.
  948. */
  949. resolved_addr = 0;
  950. tor_free(options->Address);
  951. options->Address = NULL;
  952. MOCK(tor_gethostname,tor_gethostname_replacement);
  953. MOCK(tor_lookup_hostname,tor_lookup_hostname_01010101);
  954. prev_n_gethostname_replacement = n_gethostname_replacement;
  955. prev_n_hostname_01010101 = n_hostname_01010101;
  956. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  957. &method_used,&hostname_out);
  958. tt_want(retval == 0);
  959. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  960. tt_want(n_hostname_01010101 == prev_n_hostname_01010101 + 1);
  961. tt_want_str_op(method_used,==,"GETHOSTNAME");
  962. tt_want_str_op(hostname_out,==,"onionrouter!");
  963. tt_assert(resolved_addr == 0x01010101);
  964. UNMOCK(tor_gethostname);
  965. UNMOCK(tor_lookup_hostname);
  966. tor_free(hostname_out);
  967. /*
  968. * CASE 4:
  969. * Given that options->Address is a local host address, we want
  970. * resolve_my_address() function to fail.
  971. */
  972. resolved_addr = 0;
  973. tor_free(options->Address);
  974. options->Address = tor_strdup("127.0.0.1");
  975. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  976. &method_used,&hostname_out);
  977. tt_want(resolved_addr == 0);
  978. tt_assert(retval == -1);
  979. tor_free(options->Address);
  980. tor_free(hostname_out);
  981. /*
  982. * CASE 5:
  983. * We want resolve_my_address() to fail if DNS address in options->Address
  984. * cannot be resolved.
  985. */
  986. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  987. prev_n_hostname_failure = n_hostname_failure;
  988. tor_free(options->Address);
  989. options->Address = tor_strdup("www.tor-project.org");
  990. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  991. &method_used,&hostname_out);
  992. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  993. tt_assert(retval == -1);
  994. UNMOCK(tor_lookup_hostname);
  995. tor_free(options->Address);
  996. tor_free(hostname_out);
  997. /*
  998. * CASE 6:
  999. * If options->Address is NULL AND gettting local hostname fails, we want
  1000. * resolve_my_address() to fail as well.
  1001. */
  1002. MOCK(tor_gethostname,tor_gethostname_failure);
  1003. prev_n_gethostname_failure = n_gethostname_failure;
  1004. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1005. &method_used,&hostname_out);
  1006. tt_want(n_gethostname_failure == prev_n_gethostname_failure + 1);
  1007. tt_assert(retval == -1);
  1008. UNMOCK(tor_gethostname);
  1009. tor_free(hostname_out);
  1010. /*
  1011. * CASE 7:
  1012. * We want resolve_my_address() to try and get network interface address via
  1013. * get_interface_address() if hostname returned by tor_gethostname() cannot be
  1014. * resolved into IP address.
  1015. */
  1016. MOCK(tor_gethostname,tor_gethostname_replacement);
  1017. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1018. MOCK(get_interface_address,get_interface_address_08080808);
  1019. prev_n_gethostname_replacement = n_gethostname_replacement;
  1020. prev_n_get_interface_address = n_get_interface_address;
  1021. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1022. &method_used,&hostname_out);
  1023. tt_want(retval == 0);
  1024. tt_want_int_op(n_gethostname_replacement, ==,
  1025. prev_n_gethostname_replacement + 1);
  1026. tt_want_int_op(n_get_interface_address, ==,
  1027. prev_n_get_interface_address + 1);
  1028. tt_want_str_op(method_used,==,"INTERFACE");
  1029. tt_want(hostname_out == NULL);
  1030. tt_assert(resolved_addr == 0x08080808);
  1031. UNMOCK(get_interface_address);
  1032. tor_free(hostname_out);
  1033. /*
  1034. * CASE 8:
  1035. * Suppose options->Address is NULL AND hostname returned by tor_gethostname()
  1036. * is unresolvable. We want resolve_my_address to fail if
  1037. * get_interface_address() fails.
  1038. */
  1039. MOCK(get_interface_address,get_interface_address_failure);
  1040. prev_n_get_interface_address_failure = n_get_interface_address_failure;
  1041. prev_n_gethostname_replacement = n_gethostname_replacement;
  1042. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1043. &method_used,&hostname_out);
  1044. tt_want(n_get_interface_address_failure ==
  1045. prev_n_get_interface_address_failure + 1);
  1046. tt_want(n_gethostname_replacement ==
  1047. prev_n_gethostname_replacement + 1);
  1048. tt_assert(retval == -1);
  1049. UNMOCK(get_interface_address);
  1050. tor_free(hostname_out);
  1051. /*
  1052. * CASE 9:
  1053. * Given that options->Address is NULL AND tor_lookup_hostname()
  1054. * fails AND hostname returned by gethostname() resolves
  1055. * to local IP address, we want resolve_my_address() function to
  1056. * call get_interface_address6(.,AF_INET,.) and return IP address
  1057. * the latter function has found.
  1058. */
  1059. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1060. MOCK(tor_gethostname,tor_gethostname_replacement);
  1061. MOCK(get_interface_address6,get_interface_address6_replacement);
  1062. prev_n_gethostname_replacement = n_gethostname_replacement;
  1063. prev_n_hostname_failure = n_hostname_failure;
  1064. prev_n_get_interface_address6 = n_get_interface_address6;
  1065. retval = resolve_my_address(LOG_NOTICE,options,&resolved_addr,
  1066. &method_used,&hostname_out);
  1067. tt_want(last_address6_family == AF_INET);
  1068. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1069. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1070. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1071. tt_want(retval == 0);
  1072. tt_want_str_op(method_used,==,"INTERFACE");
  1073. tt_assert(resolved_addr == 0x09090909);
  1074. UNMOCK(tor_lookup_hostname);
  1075. UNMOCK(tor_gethostname);
  1076. UNMOCK(get_interface_address6);
  1077. tor_free(hostname_out);
  1078. /*
  1079. * CASE 10: We want resolve_my_address() to fail if all of the following
  1080. * are true:
  1081. * 1. options->Address is not NULL
  1082. * 2. ... but it cannot be converted to struct in_addr by
  1083. * tor_inet_aton()
  1084. * 3. ... and tor_lookup_hostname() fails to resolve the
  1085. * options->Address
  1086. */
  1087. MOCK(tor_lookup_hostname,tor_lookup_hostname_failure);
  1088. prev_n_hostname_failure = n_hostname_failure;
  1089. tor_free(options->Address);
  1090. options->Address = tor_strdup("some_hostname");
  1091. retval = resolve_my_address(LOG_NOTICE, options, &resolved_addr,
  1092. &method_used,&hostname_out);
  1093. tt_want(n_hostname_failure == prev_n_hostname_failure + 1);
  1094. tt_assert(retval == -1);
  1095. UNMOCK(tor_gethostname);
  1096. UNMOCK(tor_lookup_hostname);
  1097. tor_free(hostname_out);
  1098. /*
  1099. * CASE 11:
  1100. * Suppose the following sequence of events:
  1101. * 1. options->Address is NULL
  1102. * 2. tor_gethostname() succeeds to get hostname of machine Tor
  1103. * if running on.
  1104. * 3. Hostname from previous step cannot be converted to
  1105. * address by using tor_inet_aton() function.
  1106. * 4. However, tor_lookup_hostname() succeds in resolving the
  1107. * hostname from step 2.
  1108. * 5. Unfortunately, tor_addr_is_internal() deems this address
  1109. * to be internal.
  1110. * 6. get_interface_address6(.,AF_INET,.) returns non-internal
  1111. * IPv4
  1112. *
  1113. * We want resolve_my_addr() to succeed with method "INTERFACE"
  1114. * and address from step 6.
  1115. */
  1116. tor_free(options->Address);
  1117. options->Address = NULL;
  1118. MOCK(tor_gethostname,tor_gethostname_replacement);
  1119. MOCK(tor_lookup_hostname,tor_lookup_hostname_localhost);
  1120. MOCK(get_interface_address6,get_interface_address6_replacement);
  1121. prev_n_gethostname_replacement = n_gethostname_replacement;
  1122. prev_n_hostname_localhost = n_hostname_localhost;
  1123. prev_n_get_interface_address6 = n_get_interface_address6;
  1124. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1125. &method_used,&hostname_out);
  1126. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1127. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1128. tt_want(n_get_interface_address6 == prev_n_get_interface_address6 + 1);
  1129. tt_str_op(method_used,==,"INTERFACE");
  1130. tt_assert(!hostname_out);
  1131. tt_assert(retval == 0);
  1132. /*
  1133. * CASE 11b:
  1134. * 1-5 as above.
  1135. * 6. get_interface_address6() fails.
  1136. *
  1137. * In this subcase, we want resolve_my_address() to fail.
  1138. */
  1139. UNMOCK(get_interface_address6);
  1140. MOCK(get_interface_address6,get_interface_address6_failure);
  1141. prev_n_gethostname_replacement = n_gethostname_replacement;
  1142. prev_n_hostname_localhost = n_hostname_localhost;
  1143. prev_n_get_interface_address6_failure = n_get_interface_address6_failure;
  1144. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1145. &method_used,&hostname_out);
  1146. tt_want(n_gethostname_replacement == prev_n_gethostname_replacement + 1);
  1147. tt_want(n_hostname_localhost == prev_n_hostname_localhost + 1);
  1148. tt_want(n_get_interface_address6_failure ==
  1149. prev_n_get_interface_address6_failure + 1);
  1150. tt_assert(retval == -1);
  1151. UNMOCK(tor_gethostname);
  1152. UNMOCK(tor_lookup_hostname);
  1153. UNMOCK(get_interface_address6);
  1154. /* CASE 12:
  1155. * Suppose the following happens:
  1156. * 1. options->Address is NULL AND options->DirAuthorities is non-NULL
  1157. * 2. tor_gethostname() succeeds in getting hostname of a machine ...
  1158. * 3. ... which is successfully parsed by tor_inet_aton() ...
  1159. * 4. into IPv4 address that tor_addr_is_inernal() considers to be
  1160. * internal.
  1161. *
  1162. * In this case, we want resolve_my_address() to fail.
  1163. */
  1164. tor_free(options->Address);
  1165. options->Address = NULL;
  1166. options->DirAuthorities = tor_malloc_zero(sizeof(config_line_t));
  1167. MOCK(tor_gethostname,tor_gethostname_localhost);
  1168. prev_n_gethostname_localhost = n_gethostname_localhost;
  1169. retval = resolve_my_address(LOG_DEBUG, options, &resolved_addr,
  1170. &method_used,&hostname_out);
  1171. tt_want(n_gethostname_localhost == prev_n_gethostname_localhost + 1);
  1172. tt_assert(retval == -1);
  1173. UNMOCK(tor_gethostname);
  1174. done:
  1175. tor_free(options->Address);
  1176. tor_free(options->DirAuthorities);
  1177. or_options_free(options);
  1178. tor_free(hostname_out);
  1179. UNMOCK(tor_gethostname);
  1180. UNMOCK(tor_lookup_hostname);
  1181. UNMOCK(get_interface_address);
  1182. UNMOCK(get_interface_address6);
  1183. UNMOCK(tor_gethostname);
  1184. }
  1185. static int n_add_default_fallback_dir_servers_known_default = 0;
  1186. /**
  1187. * This mock function is meant to replace add_default_fallback_dir_servers().
  1188. * It will parse and add one known default fallback dir server,
  1189. * which has a dir_port of 99.
  1190. * <b>n_add_default_fallback_dir_servers_known_default</b> is incremented by
  1191. * one every time this function is called.
  1192. */
  1193. static void
  1194. add_default_fallback_dir_servers_known_default(void)
  1195. {
  1196. int i;
  1197. const char *fallback[] = {
  1198. "127.0.0.1:60099 orport=9009 "
  1199. "id=0923456789012345678901234567890123456789",
  1200. NULL
  1201. };
  1202. for (i=0; fallback[i]; i++) {
  1203. if (parse_dir_fallback_line(fallback[i], 0)<0) {
  1204. log_err(LD_BUG, "Couldn't parse internal FallbackDir line %s",
  1205. fallback[i]);
  1206. }
  1207. }
  1208. n_add_default_fallback_dir_servers_known_default++;
  1209. }
  1210. static void
  1211. test_config_adding_dir_servers(void *arg)
  1212. {
  1213. (void)arg;
  1214. /* allocate options */
  1215. or_options_t *options = tor_malloc(sizeof(or_options_t));
  1216. /* Allocate and populate configuration lines:
  1217. *
  1218. * Use the same format as the hard-coded directories in
  1219. * add_default_trusted_dir_authorities().
  1220. * Zeroing the structure has the same effect as initialising to:
  1221. * { NULL, NULL, NULL, CONFIG_LINE_NORMAL, 0};
  1222. */
  1223. config_line_t *test_dir_authority = tor_malloc(sizeof(config_line_t));
  1224. memset(test_dir_authority, 0, sizeof(config_line_t));
  1225. test_dir_authority->key = tor_strdup("DirAuthority");
  1226. test_dir_authority->value = tor_strdup(
  1227. "D0 orport=9000 "
  1228. "v3ident=0023456789012345678901234567890123456789 "
  1229. "127.0.0.1:60090 0123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1230. );
  1231. config_line_t *test_alt_bridge_authority = tor_malloc(sizeof(config_line_t));
  1232. memset(test_alt_bridge_authority, 0, sizeof(config_line_t));
  1233. test_alt_bridge_authority->key = tor_strdup("AlternateBridgeAuthority");
  1234. test_alt_bridge_authority->value = tor_strdup(
  1235. "B1 orport=9001 bridge "
  1236. "127.0.0.1:60091 1123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1237. );
  1238. config_line_t *test_alt_dir_authority = tor_malloc(sizeof(config_line_t));
  1239. memset(test_alt_dir_authority, 0, sizeof(config_line_t));
  1240. test_alt_dir_authority->key = tor_strdup("AlternateDirAuthority");
  1241. test_alt_dir_authority->value = tor_strdup(
  1242. "A2 orport=9002 "
  1243. "v3ident=0223456789012345678901234567890123456789 "
  1244. "127.0.0.1:60092 2123 4567 8901 2345 6789 0123 4567 8901 2345 6789"
  1245. );
  1246. /* Use the format specified in the manual page */
  1247. config_line_t *test_fallback_directory = tor_malloc(sizeof(config_line_t));
  1248. memset(test_fallback_directory, 0, sizeof(config_line_t));
  1249. test_fallback_directory->key = tor_strdup("FallbackDir");
  1250. test_fallback_directory->value = tor_strdup(
  1251. "127.0.0.1:60093 orport=9003 id=0323456789012345678901234567890123456789"
  1252. );
  1253. /* We need to know if add_default_fallback_dir_servers is called,
  1254. * whatever the size of the list in fallback_dirs.inc,
  1255. * so we use a version of add_default_fallback_dir_servers that adds
  1256. * one known default fallback directory. */
  1257. MOCK(add_default_fallback_dir_servers,
  1258. add_default_fallback_dir_servers_known_default);
  1259. /* There are 16 different cases, covering each combination of set/NULL for:
  1260. * DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority &
  1261. * FallbackDir.
  1262. * But validate_dir_servers() ensures that:
  1263. * "You cannot set both DirAuthority and Alternate*Authority."
  1264. * This reduces the number of cases to 10.
  1265. *
  1266. * Let's count these cases using binary, with 1 meaning set & 0 meaning NULL
  1267. * So 1001 or case 9 is:
  1268. * DirAuthorities set,
  1269. * AlternateBridgeAuthority NULL,
  1270. * AlternateDirAuthority NULL
  1271. * FallbackDir set
  1272. * The valid cases are cases 0-9 counting using this method, as every case
  1273. * greater than or equal to 10 = 1010 is invalid.
  1274. *
  1275. * After #15642 - Disable default fallback dirs when any custom dirs set
  1276. *
  1277. * 1. Outcome: Use Set Directory Authorities
  1278. * - No Default Authorities
  1279. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1280. * if they are set
  1281. * Cases expected to yield this outcome:
  1282. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1283. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1284. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1285. *
  1286. * 2. Outcome: Use Set Bridge Authority
  1287. * - Use Default Non-Bridge Directory Authorities
  1288. * - Use FallbackDir if it is set, otherwise use default FallbackDir
  1289. * Cases expected to yield this outcome:
  1290. * 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1291. * AlternateBridgeAuthority is set, and
  1292. * AlternateDirAuthority is NULL)
  1293. *
  1294. * 3. Outcome: Use Set Alternate Directory Authority
  1295. * - Use Default Bridge Authorities
  1296. * - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  1297. * Cases expected to yield this outcome:
  1298. * 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  1299. * are both NULL, but AlternateDirAuthority is set)
  1300. *
  1301. * 4. Outcome: Use Set Custom Fallback Directory
  1302. * - Use Default Bridge & Directory Authorities
  1303. * Cases expected to yield this outcome:
  1304. * 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  1305. * are all NULL, but FallbackDir is set)
  1306. *
  1307. * 5. Outcome: Use All Defaults
  1308. * - Use Default Bridge & Directory Authorities, and
  1309. * Default Fallback Directories
  1310. * Cases expected to yield this outcome:
  1311. * 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  1312. * and FallbackDir are all NULL)
  1313. */
  1314. /*
  1315. * Find out how many default Bridge, Non-Bridge and Fallback Directories
  1316. * are hard-coded into this build.
  1317. * This code makes some assumptions about the implementation.
  1318. * If they are wrong, one or more of cases 0-5 could fail.
  1319. */
  1320. int n_default_alt_bridge_authority = 0;
  1321. int n_default_alt_dir_authority = 0;
  1322. int n_default_fallback_dir = 0;
  1323. #define n_default_authorities ((n_default_alt_bridge_authority) \
  1324. + (n_default_alt_dir_authority))
  1325. /* Pre-Count Number of Authorities of Each Type
  1326. * Use 0000: No Directory Authorities or Fallback Directories Set
  1327. */
  1328. {
  1329. /* clear fallback dirs counter */
  1330. n_add_default_fallback_dir_servers_known_default = 0;
  1331. /* clear options*/
  1332. memset(options, 0, sizeof(or_options_t));
  1333. /* clear any previous dir servers:
  1334. consider_adding_dir_servers() should do this anyway */
  1335. clear_dir_servers();
  1336. /* assign options: 0000 */
  1337. options->DirAuthorities = NULL;
  1338. options->AlternateBridgeAuthority = NULL;
  1339. options->AlternateDirAuthority = NULL;
  1340. options->FallbackDir = NULL;
  1341. /* parse options - ensure we always update by passing NULL old_options */
  1342. consider_adding_dir_servers(options, NULL);
  1343. /* check outcome */
  1344. /* we must have added the default fallback dirs */
  1345. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  1346. {
  1347. /* fallback_dir_servers */
  1348. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1349. /* Count Bridge Authorities */
  1350. SMARTLIST_FOREACH(fallback_servers,
  1351. dir_server_t *,
  1352. ds,
  1353. /* increment the found counter if it's a bridge auth */
  1354. n_default_alt_bridge_authority +=
  1355. ((ds->is_authority && (ds->type & BRIDGE_DIRINFO)) ?
  1356. 1 : 0)
  1357. );
  1358. /* If we have no default bridge authority, something has gone wrong */
  1359. tt_assert(n_default_alt_bridge_authority >= 1);
  1360. /* Count v3 Authorities */
  1361. SMARTLIST_FOREACH(fallback_servers,
  1362. dir_server_t *,
  1363. ds,
  1364. /* increment found counter if it's a v3 auth */
  1365. n_default_alt_dir_authority +=
  1366. ((ds->is_authority && (ds->type & V3_DIRINFO)) ?
  1367. 1 : 0)
  1368. );
  1369. /* If we have no default authorities, something has gone really wrong */
  1370. tt_assert(n_default_alt_dir_authority >= 1);
  1371. /* Calculate Fallback Directory Count */
  1372. n_default_fallback_dir = (smartlist_len(fallback_servers) -
  1373. n_default_alt_bridge_authority -
  1374. n_default_alt_dir_authority);
  1375. /* If we have a negative count, something has gone really wrong */
  1376. tt_assert(n_default_fallback_dir >= 0);
  1377. }
  1378. }
  1379. /*
  1380. * 1. Outcome: Use Set Directory Authorities
  1381. * - No Default Authorities
  1382. * - Use AlternateBridgeAuthority, AlternateDirAuthority, and FallbackDir
  1383. * if they are set
  1384. * Cases expected to yield this outcome:
  1385. * 8 & 9 (the 2 valid cases where DirAuthorities is set)
  1386. * 6 & 7 (the 2 cases where DirAuthorities is NULL, and
  1387. * AlternateBridgeAuthority and AlternateDirAuthority are both set)
  1388. */
  1389. /* Case 9: 1001 - DirAuthorities Set, AlternateBridgeAuthority Not Set,
  1390. AlternateDirAuthority Not Set, FallbackDir Set */
  1391. {
  1392. /* clear fallback dirs counter */
  1393. n_add_default_fallback_dir_servers_known_default = 0;
  1394. /* clear options*/
  1395. memset(options, 0, sizeof(or_options_t));
  1396. /* clear any previous dir servers:
  1397. consider_adding_dir_servers() should do this anyway */
  1398. clear_dir_servers();
  1399. /* assign options: 1001 */
  1400. options->DirAuthorities = test_dir_authority;
  1401. options->AlternateBridgeAuthority = NULL;
  1402. options->AlternateDirAuthority = NULL;
  1403. options->FallbackDir = test_fallback_directory;
  1404. /* parse options - ensure we always update by passing NULL old_options */
  1405. consider_adding_dir_servers(options, NULL);
  1406. /* check outcome */
  1407. /* we must not have added the default fallback dirs */
  1408. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1409. {
  1410. /* trusted_dir_servers */
  1411. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1412. /* D0, (No B1), (No A2) */
  1413. tt_assert(smartlist_len(dir_servers) == 1);
  1414. /* DirAuthority - D0 - dir_port: 60090 */
  1415. int found_D0 = 0;
  1416. SMARTLIST_FOREACH(dir_servers,
  1417. dir_server_t *,
  1418. ds,
  1419. /* increment the found counter if dir_port matches */
  1420. found_D0 +=
  1421. (ds->dir_port == 60090 ?
  1422. 1 : 0)
  1423. );
  1424. tt_assert(found_D0 == 1);
  1425. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1426. int found_B1 = 0;
  1427. SMARTLIST_FOREACH(dir_servers,
  1428. dir_server_t *,
  1429. ds,
  1430. /* increment the found counter if dir_port matches */
  1431. found_B1 +=
  1432. (ds->dir_port == 60091 ?
  1433. 1 : 0)
  1434. );
  1435. tt_assert(found_B1 == 0);
  1436. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1437. int found_A2 = 0;
  1438. SMARTLIST_FOREACH(dir_servers,
  1439. dir_server_t *,
  1440. ds,
  1441. /* increment the found counter if dir_port matches */
  1442. found_A2 +=
  1443. (ds->dir_port == 60092 ?
  1444. 1 : 0)
  1445. );
  1446. tt_assert(found_A2 == 0);
  1447. }
  1448. {
  1449. /* fallback_dir_servers */
  1450. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1451. /* D0, (No B1), (No A2), Custom Fallback */
  1452. tt_assert(smartlist_len(fallback_servers) == 2);
  1453. /* DirAuthority - D0 - dir_port: 60090 */
  1454. int found_D0 = 0;
  1455. SMARTLIST_FOREACH(fallback_servers,
  1456. dir_server_t *,
  1457. ds,
  1458. /* increment the found counter if dir_port matches */
  1459. found_D0 +=
  1460. (ds->dir_port == 60090 ?
  1461. 1 : 0)
  1462. );
  1463. tt_assert(found_D0 == 1);
  1464. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1465. int found_B1 = 0;
  1466. SMARTLIST_FOREACH(fallback_servers,
  1467. dir_server_t *,
  1468. ds,
  1469. /* increment the found counter if dir_port matches */
  1470. found_B1 +=
  1471. (ds->dir_port == 60091 ?
  1472. 1 : 0)
  1473. );
  1474. tt_assert(found_B1 == 0);
  1475. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1476. int found_A2 = 0;
  1477. SMARTLIST_FOREACH(fallback_servers,
  1478. dir_server_t *,
  1479. ds,
  1480. /* increment the found counter if dir_port matches */
  1481. found_A2 +=
  1482. (ds->dir_port == 60092 ?
  1483. 1 : 0)
  1484. );
  1485. tt_assert(found_A2 == 0);
  1486. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1487. int found_non_default_fallback = 0;
  1488. SMARTLIST_FOREACH(fallback_servers,
  1489. dir_server_t *,
  1490. ds,
  1491. /* increment the found counter if dir_port matches */
  1492. found_non_default_fallback +=
  1493. (ds->dir_port == 60093 ?
  1494. 1 : 0)
  1495. );
  1496. tt_assert(found_non_default_fallback == 1);
  1497. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1498. int found_default_fallback = 0;
  1499. SMARTLIST_FOREACH(fallback_servers,
  1500. dir_server_t *,
  1501. ds,
  1502. /* increment the found counter if dir_port matches */
  1503. found_default_fallback +=
  1504. (ds->dir_port == 60099 ?
  1505. 1 : 0)
  1506. );
  1507. tt_assert(found_default_fallback == 0);
  1508. }
  1509. }
  1510. /* Case 8: 1000 - DirAuthorities Set, Others Not Set */
  1511. {
  1512. /* clear fallback dirs counter */
  1513. n_add_default_fallback_dir_servers_known_default = 0;
  1514. /* clear options*/
  1515. memset(options, 0, sizeof(or_options_t));
  1516. /* clear any previous dir servers:
  1517. consider_adding_dir_servers() should do this anyway */
  1518. clear_dir_servers();
  1519. /* assign options: 1000 */
  1520. options->DirAuthorities = test_dir_authority;
  1521. options->AlternateBridgeAuthority = NULL;
  1522. options->AlternateDirAuthority = NULL;
  1523. options->FallbackDir = NULL;
  1524. /* parse options - ensure we always update by passing NULL old_options */
  1525. consider_adding_dir_servers(options, NULL);
  1526. /* check outcome */
  1527. /* we must not have added the default fallback dirs */
  1528. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1529. {
  1530. /* trusted_dir_servers */
  1531. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1532. /* D0, (No B1), (No A2) */
  1533. tt_assert(smartlist_len(dir_servers) == 1);
  1534. /* DirAuthority - D0 - dir_port: 60090 */
  1535. int found_D0 = 0;
  1536. SMARTLIST_FOREACH(dir_servers,
  1537. dir_server_t *,
  1538. ds,
  1539. /* increment the found counter if dir_port matches */
  1540. found_D0 +=
  1541. (ds->dir_port == 60090 ?
  1542. 1 : 0)
  1543. );
  1544. tt_assert(found_D0 == 1);
  1545. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1546. int found_B1 = 0;
  1547. SMARTLIST_FOREACH(dir_servers,
  1548. dir_server_t *,
  1549. ds,
  1550. /* increment the found counter if dir_port matches */
  1551. found_B1 +=
  1552. (ds->dir_port == 60091 ?
  1553. 1 : 0)
  1554. );
  1555. tt_assert(found_B1 == 0);
  1556. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1557. int found_A2 = 0;
  1558. SMARTLIST_FOREACH(dir_servers,
  1559. dir_server_t *,
  1560. ds,
  1561. /* increment the found counter if dir_port matches */
  1562. found_A2 +=
  1563. (ds->dir_port == 60092 ?
  1564. 1 : 0)
  1565. );
  1566. tt_assert(found_A2 == 0);
  1567. }
  1568. {
  1569. /* fallback_dir_servers */
  1570. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1571. /* D0, (No B1), (No A2), (No Fallback) */
  1572. tt_assert(smartlist_len(fallback_servers) == 1);
  1573. /* DirAuthority - D0 - dir_port: 60090 */
  1574. int found_D0 = 0;
  1575. SMARTLIST_FOREACH(fallback_servers,
  1576. dir_server_t *,
  1577. ds,
  1578. /* increment the found counter if dir_port matches */
  1579. found_D0 +=
  1580. (ds->dir_port == 60090 ?
  1581. 1 : 0)
  1582. );
  1583. tt_assert(found_D0 == 1);
  1584. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  1585. int found_B1 = 0;
  1586. SMARTLIST_FOREACH(fallback_servers,
  1587. dir_server_t *,
  1588. ds,
  1589. /* increment the found counter if dir_port matches */
  1590. found_B1 +=
  1591. (ds->dir_port == 60091 ?
  1592. 1 : 0)
  1593. );
  1594. tt_assert(found_B1 == 0);
  1595. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1596. int found_A2 = 0;
  1597. SMARTLIST_FOREACH(fallback_servers,
  1598. dir_server_t *,
  1599. ds,
  1600. /* increment the found counter if dir_port matches */
  1601. found_A2 +=
  1602. (ds->dir_port == 60092 ?
  1603. 1 : 0)
  1604. );
  1605. tt_assert(found_A2 == 0);
  1606. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1607. int found_non_default_fallback = 0;
  1608. SMARTLIST_FOREACH(fallback_servers,
  1609. dir_server_t *,
  1610. ds,
  1611. /* increment the found counter if dir_port matches */
  1612. found_non_default_fallback +=
  1613. (ds->dir_port == 60093 ?
  1614. 1 : 0)
  1615. );
  1616. tt_assert(found_non_default_fallback == 0);
  1617. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1618. int found_default_fallback = 0;
  1619. SMARTLIST_FOREACH(fallback_servers,
  1620. dir_server_t *,
  1621. ds,
  1622. /* increment the found counter if dir_port matches */
  1623. found_default_fallback +=
  1624. (ds->dir_port == 60099 ?
  1625. 1 : 0)
  1626. );
  1627. tt_assert(found_default_fallback == 0);
  1628. }
  1629. }
  1630. /* Case 7: 0111 - DirAuthorities Not Set, Others Set */
  1631. {
  1632. /* clear fallback dirs counter */
  1633. n_add_default_fallback_dir_servers_known_default = 0;
  1634. /* clear options*/
  1635. memset(options, 0, sizeof(or_options_t));
  1636. /* clear any previous dir servers:
  1637. consider_adding_dir_servers() should do this anyway */
  1638. clear_dir_servers();
  1639. /* assign options: 0111 */
  1640. options->DirAuthorities = NULL;
  1641. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1642. options->AlternateDirAuthority = test_alt_dir_authority;
  1643. options->FallbackDir = test_fallback_directory;
  1644. /* parse options - ensure we always update by passing NULL old_options */
  1645. consider_adding_dir_servers(options, NULL);
  1646. /* check outcome */
  1647. /* we must not have added the default fallback dirs */
  1648. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1649. {
  1650. /* trusted_dir_servers */
  1651. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1652. /* (No D0), B1, A2 */
  1653. tt_assert(smartlist_len(dir_servers) == 2);
  1654. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1655. int found_D0 = 0;
  1656. SMARTLIST_FOREACH(dir_servers,
  1657. dir_server_t *,
  1658. ds,
  1659. /* increment the found counter if dir_port matches */
  1660. found_D0 +=
  1661. (ds->dir_port == 60090 ?
  1662. 1 : 0)
  1663. );
  1664. tt_assert(found_D0 == 0);
  1665. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1666. int found_B1 = 0;
  1667. SMARTLIST_FOREACH(dir_servers,
  1668. dir_server_t *,
  1669. ds,
  1670. /* increment the found counter if dir_port matches */
  1671. found_B1 +=
  1672. (ds->dir_port == 60091 ?
  1673. 1 : 0)
  1674. );
  1675. tt_assert(found_B1 == 1);
  1676. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1677. int found_A2 = 0;
  1678. SMARTLIST_FOREACH(dir_servers,
  1679. dir_server_t *,
  1680. ds,
  1681. /* increment the found counter if dir_port matches */
  1682. found_A2 +=
  1683. (ds->dir_port == 60092 ?
  1684. 1 : 0)
  1685. );
  1686. tt_assert(found_A2 == 1);
  1687. }
  1688. {
  1689. /* fallback_dir_servers */
  1690. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1691. /* (No D0), B1, A2, Custom Fallback */
  1692. tt_assert(smartlist_len(fallback_servers) == 3);
  1693. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1694. int found_D0 = 0;
  1695. SMARTLIST_FOREACH(fallback_servers,
  1696. dir_server_t *,
  1697. ds,
  1698. /* increment the found counter if dir_port matches */
  1699. found_D0 +=
  1700. (ds->dir_port == 60090 ?
  1701. 1 : 0)
  1702. );
  1703. tt_assert(found_D0 == 0);
  1704. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1705. int found_B1 = 0;
  1706. SMARTLIST_FOREACH(fallback_servers,
  1707. dir_server_t *,
  1708. ds,
  1709. /* increment the found counter if dir_port matches */
  1710. found_B1 +=
  1711. (ds->dir_port == 60091 ?
  1712. 1 : 0)
  1713. );
  1714. tt_assert(found_B1 == 1);
  1715. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1716. int found_A2 = 0;
  1717. SMARTLIST_FOREACH(fallback_servers,
  1718. dir_server_t *,
  1719. ds,
  1720. /* increment the found counter if dir_port matches */
  1721. found_A2 +=
  1722. (ds->dir_port == 60092 ?
  1723. 1 : 0)
  1724. );
  1725. tt_assert(found_A2 == 1);
  1726. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1727. int found_non_default_fallback = 0;
  1728. SMARTLIST_FOREACH(fallback_servers,
  1729. dir_server_t *,
  1730. ds,
  1731. /* increment the found counter if dir_port matches */
  1732. found_non_default_fallback +=
  1733. (ds->dir_port == 60093 ?
  1734. 1 : 0)
  1735. );
  1736. tt_assert(found_non_default_fallback == 1);
  1737. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1738. int found_default_fallback = 0;
  1739. SMARTLIST_FOREACH(fallback_servers,
  1740. dir_server_t *,
  1741. ds,
  1742. /* increment the found counter if dir_port matches */
  1743. found_default_fallback +=
  1744. (ds->dir_port == 60099 ?
  1745. 1 : 0)
  1746. );
  1747. tt_assert(found_default_fallback == 0);
  1748. }
  1749. }
  1750. /* Case 6: 0110 - DirAuthorities Not Set, AlternateBridgeAuthority &
  1751. AlternateDirAuthority Set, FallbackDir Not Set */
  1752. {
  1753. /* clear fallback dirs counter */
  1754. n_add_default_fallback_dir_servers_known_default = 0;
  1755. /* clear options*/
  1756. memset(options, 0, sizeof(or_options_t));
  1757. /* clear any previous dir servers:
  1758. consider_adding_dir_servers() should do this anyway */
  1759. clear_dir_servers();
  1760. /* assign options: 0110 */
  1761. options->DirAuthorities = NULL;
  1762. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1763. options->AlternateDirAuthority = test_alt_dir_authority;
  1764. options->FallbackDir = NULL;
  1765. /* parse options - ensure we always update by passing NULL old_options */
  1766. consider_adding_dir_servers(options, NULL);
  1767. /* check outcome */
  1768. /* we must not have added the default fallback dirs */
  1769. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1770. {
  1771. /* trusted_dir_servers */
  1772. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1773. /* (No D0), B1, A2 */
  1774. tt_assert(smartlist_len(dir_servers) == 2);
  1775. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1776. int found_D0 = 0;
  1777. SMARTLIST_FOREACH(dir_servers,
  1778. dir_server_t *,
  1779. ds,
  1780. /* increment the found counter if dir_port matches */
  1781. found_D0 +=
  1782. (ds->dir_port == 60090 ?
  1783. 1 : 0)
  1784. );
  1785. tt_assert(found_D0 == 0);
  1786. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1787. int found_B1 = 0;
  1788. SMARTLIST_FOREACH(dir_servers,
  1789. dir_server_t *,
  1790. ds,
  1791. /* increment the found counter if dir_port matches */
  1792. found_B1 +=
  1793. (ds->dir_port == 60091 ?
  1794. 1 : 0)
  1795. );
  1796. tt_assert(found_B1 == 1);
  1797. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1798. int found_A2 = 0;
  1799. SMARTLIST_FOREACH(dir_servers,
  1800. dir_server_t *,
  1801. ds,
  1802. /* increment the found counter if dir_port matches */
  1803. found_A2 +=
  1804. (ds->dir_port == 60092 ?
  1805. 1 : 0)
  1806. );
  1807. tt_assert(found_A2 == 1);
  1808. }
  1809. {
  1810. /* fallback_dir_servers */
  1811. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1812. /* (No D0), B1, A2, (No Fallback) */
  1813. tt_assert(smartlist_len(fallback_servers) == 2);
  1814. /* (No DirAuthority) - D0 - dir_port: 60090 */
  1815. int found_D0 = 0;
  1816. SMARTLIST_FOREACH(fallback_servers,
  1817. dir_server_t *,
  1818. ds,
  1819. /* increment the found counter if dir_port matches */
  1820. found_D0 +=
  1821. (ds->dir_port == 60090 ?
  1822. 1 : 0)
  1823. );
  1824. tt_assert(found_D0 == 0);
  1825. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1826. int found_B1 = 0;
  1827. SMARTLIST_FOREACH(fallback_servers,
  1828. dir_server_t *,
  1829. ds,
  1830. /* increment the found counter if dir_port matches */
  1831. found_B1 +=
  1832. (ds->dir_port == 60091 ?
  1833. 1 : 0)
  1834. );
  1835. tt_assert(found_B1 == 1);
  1836. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  1837. int found_A2 = 0;
  1838. SMARTLIST_FOREACH(fallback_servers,
  1839. dir_server_t *,
  1840. ds,
  1841. /* increment the found counter if dir_port matches */
  1842. found_A2 +=
  1843. (ds->dir_port == 60092 ?
  1844. 1 : 0)
  1845. );
  1846. tt_assert(found_A2 == 1);
  1847. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  1848. int found_non_default_fallback = 0;
  1849. SMARTLIST_FOREACH(fallback_servers,
  1850. dir_server_t *,
  1851. ds,
  1852. /* increment the found counter if dir_port matches */
  1853. found_non_default_fallback +=
  1854. (ds->dir_port == 60093 ?
  1855. 1 : 0)
  1856. );
  1857. tt_assert(found_non_default_fallback == 0);
  1858. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1859. int found_default_fallback = 0;
  1860. SMARTLIST_FOREACH(fallback_servers,
  1861. dir_server_t *,
  1862. ds,
  1863. /* increment the found counter if dir_port matches */
  1864. found_default_fallback +=
  1865. (ds->dir_port == 60099 ?
  1866. 1 : 0)
  1867. );
  1868. tt_assert(found_default_fallback == 0);
  1869. }
  1870. }
  1871. /*
  1872. 2. Outcome: Use Set Bridge Authority
  1873. - Use Default Non-Bridge Directory Authorities
  1874. - Use FallbackDir if it is set, otherwise use default FallbackDir
  1875. Cases expected to yield this outcome:
  1876. 4 & 5 (the 2 cases where DirAuthorities is NULL,
  1877. AlternateBridgeAuthority is set, and
  1878. AlternateDirAuthority is NULL)
  1879. */
  1880. /* Case 5: 0101 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  1881. AlternateDirAuthority Not Set, FallbackDir Set */
  1882. {
  1883. /* clear fallback dirs counter */
  1884. n_add_default_fallback_dir_servers_known_default = 0;
  1885. /* clear options*/
  1886. memset(options, 0, sizeof(or_options_t));
  1887. /* clear any previous dir servers:
  1888. consider_adding_dir_servers() should do this anyway */
  1889. clear_dir_servers();
  1890. /* assign options: 0101 */
  1891. options->DirAuthorities = NULL;
  1892. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  1893. options->AlternateDirAuthority = NULL;
  1894. options->FallbackDir = test_fallback_directory;
  1895. /* parse options - ensure we always update by passing NULL old_options */
  1896. consider_adding_dir_servers(options, NULL);
  1897. /* check outcome */
  1898. /* we must not have added the default fallback dirs */
  1899. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  1900. {
  1901. /* trusted_dir_servers */
  1902. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  1903. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  1904. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  1905. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  1906. int found_D0 = 0;
  1907. SMARTLIST_FOREACH(dir_servers,
  1908. dir_server_t *,
  1909. ds,
  1910. /* increment the found counter if dir_port matches */
  1911. found_D0 +=
  1912. (ds->dir_port == 60090 ?
  1913. 1 : 0)
  1914. );
  1915. tt_assert(found_D0 == 0);
  1916. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1917. int found_B1 = 0;
  1918. SMARTLIST_FOREACH(dir_servers,
  1919. dir_server_t *,
  1920. ds,
  1921. /* increment the found counter if dir_port matches */
  1922. found_B1 +=
  1923. (ds->dir_port == 60091 ?
  1924. 1 : 0)
  1925. );
  1926. tt_assert(found_B1 == 1);
  1927. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1928. int found_A2 = 0;
  1929. SMARTLIST_FOREACH(dir_servers,
  1930. dir_server_t *,
  1931. ds,
  1932. /* increment the found counter if dir_port matches */
  1933. found_A2 +=
  1934. (ds->dir_port == 60092 ?
  1935. 1 : 0)
  1936. );
  1937. tt_assert(found_A2 == 0);
  1938. /* There's no easy way of checking that we have included all the
  1939. * default v3 non-Bridge directory authorities, so let's assume that
  1940. * if the total count above is correct, we have the right ones.
  1941. */
  1942. }
  1943. {
  1944. /* fallback_dir_servers */
  1945. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  1946. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  1947. * Custom Fallback */
  1948. tt_assert(smartlist_len(fallback_servers) ==
  1949. 2 + n_default_alt_dir_authority);
  1950. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  1951. int found_D0 = 0;
  1952. SMARTLIST_FOREACH(fallback_servers,
  1953. dir_server_t *,
  1954. ds,
  1955. /* increment the found counter if dir_port matches */
  1956. found_D0 +=
  1957. (ds->dir_port == 60090 ?
  1958. 1 : 0)
  1959. );
  1960. tt_assert(found_D0 == 0);
  1961. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  1962. int found_B1 = 0;
  1963. SMARTLIST_FOREACH(fallback_servers,
  1964. dir_server_t *,
  1965. ds,
  1966. /* increment the found counter if dir_port matches */
  1967. found_B1 +=
  1968. (ds->dir_port == 60091 ?
  1969. 1 : 0)
  1970. );
  1971. tt_assert(found_B1 == 1);
  1972. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  1973. int found_A2 = 0;
  1974. SMARTLIST_FOREACH(fallback_servers,
  1975. dir_server_t *,
  1976. ds,
  1977. /* increment the found counter if dir_port matches */
  1978. found_A2 +=
  1979. (ds->dir_port == 60092 ?
  1980. 1 : 0)
  1981. );
  1982. tt_assert(found_A2 == 0);
  1983. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  1984. int found_non_default_fallback = 0;
  1985. SMARTLIST_FOREACH(fallback_servers,
  1986. dir_server_t *,
  1987. ds,
  1988. /* increment the found counter if dir_port matches */
  1989. found_non_default_fallback +=
  1990. (ds->dir_port == 60093 ?
  1991. 1 : 0)
  1992. );
  1993. tt_assert(found_non_default_fallback == 1);
  1994. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  1995. int found_default_fallback = 0;
  1996. SMARTLIST_FOREACH(fallback_servers,
  1997. dir_server_t *,
  1998. ds,
  1999. /* increment the found counter if dir_port matches */
  2000. found_default_fallback +=
  2001. (ds->dir_port == 60099 ?
  2002. 1 : 0)
  2003. );
  2004. tt_assert(found_default_fallback == 0);
  2005. /* There's no easy way of checking that we have included all the
  2006. * default v3 non-Bridge directory authorities, so let's assume that
  2007. * if the total count above is correct, we have the right ones.
  2008. */
  2009. }
  2010. }
  2011. /* Case 4: 0100 - DirAuthorities Not Set, AlternateBridgeAuthority Set,
  2012. AlternateDirAuthority & FallbackDir Not Set */
  2013. {
  2014. /* clear fallback dirs counter */
  2015. n_add_default_fallback_dir_servers_known_default = 0;
  2016. /* clear options*/
  2017. memset(options, 0, sizeof(or_options_t));
  2018. /* clear any previous dir servers:
  2019. consider_adding_dir_servers() should do this anyway */
  2020. clear_dir_servers();
  2021. /* assign options: 0100 */
  2022. options->DirAuthorities = NULL;
  2023. options->AlternateBridgeAuthority = test_alt_bridge_authority;
  2024. options->AlternateDirAuthority = NULL;
  2025. options->FallbackDir = NULL;
  2026. /* parse options - ensure we always update by passing NULL old_options */
  2027. consider_adding_dir_servers(options, NULL);
  2028. /* check outcome */
  2029. /* we must have added the default fallback dirs */
  2030. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2031. {
  2032. /* trusted_dir_servers */
  2033. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2034. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities */
  2035. tt_assert(smartlist_len(dir_servers) == 1 + n_default_alt_dir_authority);
  2036. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2037. int found_D0 = 0;
  2038. SMARTLIST_FOREACH(dir_servers,
  2039. dir_server_t *,
  2040. ds,
  2041. /* increment the found counter if dir_port matches */
  2042. found_D0 +=
  2043. (ds->dir_port == 60090 ?
  2044. 1 : 0)
  2045. );
  2046. tt_assert(found_D0 == 0);
  2047. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2048. int found_B1 = 0;
  2049. SMARTLIST_FOREACH(dir_servers,
  2050. dir_server_t *,
  2051. ds,
  2052. /* increment the found counter if dir_port matches */
  2053. found_B1 +=
  2054. (ds->dir_port == 60091 ?
  2055. 1 : 0)
  2056. );
  2057. tt_assert(found_B1 == 1);
  2058. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2059. int found_A2 = 0;
  2060. SMARTLIST_FOREACH(dir_servers,
  2061. dir_server_t *,
  2062. ds,
  2063. /* increment the found counter if dir_port matches */
  2064. found_A2 +=
  2065. (ds->dir_port == 60092 ?
  2066. 1 : 0)
  2067. );
  2068. tt_assert(found_A2 == 0);
  2069. /* There's no easy way of checking that we have included all the
  2070. * default v3 non-Bridge directory authorities, so let's assume that
  2071. * if the total count above is correct, we have the right ones.
  2072. */
  2073. }
  2074. {
  2075. /* fallback_dir_servers */
  2076. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2077. /* (No D0), B1, (No A2), Default v3 Non-Bridge Authorities,
  2078. * Default Fallback */
  2079. tt_assert(smartlist_len(fallback_servers) ==
  2080. 2 + n_default_alt_dir_authority);
  2081. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2082. int found_D0 = 0;
  2083. SMARTLIST_FOREACH(fallback_servers,
  2084. dir_server_t *,
  2085. ds,
  2086. /* increment the found counter if dir_port matches */
  2087. found_D0 +=
  2088. (ds->dir_port == 60090 ?
  2089. 1 : 0)
  2090. );
  2091. tt_assert(found_D0 == 0);
  2092. /* AlternateBridgeAuthority - B1 - dir_port: 60091 */
  2093. int found_B1 = 0;
  2094. SMARTLIST_FOREACH(fallback_servers,
  2095. dir_server_t *,
  2096. ds,
  2097. /* increment the found counter if dir_port matches */
  2098. found_B1 +=
  2099. (ds->dir_port == 60091 ?
  2100. 1 : 0)
  2101. );
  2102. tt_assert(found_B1 == 1);
  2103. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2104. int found_A2 = 0;
  2105. SMARTLIST_FOREACH(fallback_servers,
  2106. dir_server_t *,
  2107. ds,
  2108. /* increment the found counter if dir_port matches */
  2109. found_A2 +=
  2110. (ds->dir_port == 60092 ?
  2111. 1 : 0)
  2112. );
  2113. tt_assert(found_A2 == 0);
  2114. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2115. int found_non_default_fallback = 0;
  2116. SMARTLIST_FOREACH(fallback_servers,
  2117. dir_server_t *,
  2118. ds,
  2119. /* increment the found counter if dir_port matches */
  2120. found_non_default_fallback +=
  2121. (ds->dir_port == 60093 ?
  2122. 1 : 0)
  2123. );
  2124. tt_assert(found_non_default_fallback == 0);
  2125. /* Default FallbackDir - No Nickname - dir_port: 60099 */
  2126. int found_default_fallback = 0;
  2127. SMARTLIST_FOREACH(fallback_servers,
  2128. dir_server_t *,
  2129. ds,
  2130. /* increment the found counter if dir_port matches */
  2131. found_default_fallback +=
  2132. (ds->dir_port == 60099 ?
  2133. 1 : 0)
  2134. );
  2135. tt_assert(found_default_fallback == 1);
  2136. /* There's no easy way of checking that we have included all the
  2137. * default v3 non-Bridge directory authorities, so let's assume that
  2138. * if the total count above is correct, we have the right ones.
  2139. */
  2140. }
  2141. }
  2142. /*
  2143. 3. Outcome: Use Set Alternate Directory Authority
  2144. - Use Default Bridge Authorities
  2145. - Use FallbackDir if it is set, otherwise No Default Fallback Directories
  2146. Cases expected to yield this outcome:
  2147. 2 & 3 (the 2 cases where DirAuthorities and AlternateBridgeAuthority
  2148. are both NULL, but AlternateDirAuthority is set)
  2149. */
  2150. /* Case 3: 0011 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2151. AlternateDirAuthority & FallbackDir Set */
  2152. {
  2153. /* clear fallback dirs counter */
  2154. n_add_default_fallback_dir_servers_known_default = 0;
  2155. /* clear options*/
  2156. memset(options, 0, sizeof(or_options_t));
  2157. /* clear any previous dir servers:
  2158. consider_adding_dir_servers() should do this anyway */
  2159. clear_dir_servers();
  2160. /* assign options: 0011 */
  2161. options->DirAuthorities = NULL;
  2162. options->AlternateBridgeAuthority = NULL;
  2163. options->AlternateDirAuthority = test_alt_dir_authority;
  2164. options->FallbackDir = test_fallback_directory;
  2165. /* parse options - ensure we always update by passing NULL old_options */
  2166. consider_adding_dir_servers(options, NULL);
  2167. /* check outcome */
  2168. /* we must not have added the default fallback dirs */
  2169. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2170. {
  2171. /* trusted_dir_servers */
  2172. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2173. /* (No D0), (No B1), Default Bridge Authorities, A2 */
  2174. tt_assert(smartlist_len(dir_servers) ==
  2175. 1 + n_default_alt_bridge_authority);
  2176. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2177. int found_D0 = 0;
  2178. SMARTLIST_FOREACH(dir_servers,
  2179. dir_server_t *,
  2180. ds,
  2181. /* increment the found counter if dir_port matches */
  2182. found_D0 +=
  2183. (ds->dir_port == 60090 ?
  2184. 1 : 0)
  2185. );
  2186. tt_assert(found_D0 == 0);
  2187. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2188. int found_B1 = 0;
  2189. SMARTLIST_FOREACH(dir_servers,
  2190. dir_server_t *,
  2191. ds,
  2192. /* increment the found counter if dir_port matches */
  2193. found_B1 +=
  2194. (ds->dir_port == 60091 ?
  2195. 1 : 0)
  2196. );
  2197. tt_assert(found_B1 == 0);
  2198. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2199. int found_A2 = 0;
  2200. SMARTLIST_FOREACH(dir_servers,
  2201. dir_server_t *,
  2202. ds,
  2203. /* increment the found counter if dir_port matches */
  2204. found_A2 +=
  2205. (ds->dir_port == 60092 ?
  2206. 1 : 0)
  2207. );
  2208. tt_assert(found_A2 == 1);
  2209. /* There's no easy way of checking that we have included all the
  2210. * default Bridge authorities (except for hard-coding tonga's details),
  2211. * so let's assume that if the total count above is correct,
  2212. * we have the right ones.
  2213. */
  2214. }
  2215. {
  2216. /* fallback_dir_servers */
  2217. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2218. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2219. * Custom Fallback Directory, (No Default Fallback Directories) */
  2220. tt_assert(smartlist_len(fallback_servers) ==
  2221. 2 + n_default_alt_bridge_authority);
  2222. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2223. int found_D0 = 0;
  2224. SMARTLIST_FOREACH(fallback_servers,
  2225. dir_server_t *,
  2226. ds,
  2227. /* increment the found counter if dir_port matches */
  2228. found_D0 +=
  2229. (ds->dir_port == 60090 ?
  2230. 1 : 0)
  2231. );
  2232. tt_assert(found_D0 == 0);
  2233. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2234. int found_B1 = 0;
  2235. SMARTLIST_FOREACH(fallback_servers,
  2236. dir_server_t *,
  2237. ds,
  2238. /* increment the found counter if dir_port matches */
  2239. found_B1 +=
  2240. (ds->dir_port == 60091 ?
  2241. 1 : 0)
  2242. );
  2243. tt_assert(found_B1 == 0);
  2244. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2245. int found_A2 = 0;
  2246. SMARTLIST_FOREACH(fallback_servers,
  2247. dir_server_t *,
  2248. ds,
  2249. /* increment the found counter if dir_port matches */
  2250. found_A2 +=
  2251. (ds->dir_port == 60092 ?
  2252. 1 : 0)
  2253. );
  2254. tt_assert(found_A2 == 1);
  2255. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2256. int found_non_default_fallback = 0;
  2257. SMARTLIST_FOREACH(fallback_servers,
  2258. dir_server_t *,
  2259. ds,
  2260. /* increment the found counter if dir_port matches */
  2261. found_non_default_fallback +=
  2262. (ds->dir_port == 60093 ?
  2263. 1 : 0)
  2264. );
  2265. tt_assert(found_non_default_fallback == 1);
  2266. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2267. int found_default_fallback = 0;
  2268. SMARTLIST_FOREACH(fallback_servers,
  2269. dir_server_t *,
  2270. ds,
  2271. /* increment the found counter if dir_port matches */
  2272. found_default_fallback +=
  2273. (ds->dir_port == 60099 ?
  2274. 1 : 0)
  2275. );
  2276. tt_assert(found_default_fallback == 0);
  2277. /* There's no easy way of checking that we have included all the
  2278. * default Bridge authorities (except for hard-coding tonga's details),
  2279. * so let's assume that if the total count above is correct,
  2280. * we have the right ones.
  2281. */
  2282. }
  2283. }
  2284. /* Case 2: 0010 - DirAuthorities & AlternateBridgeAuthority Not Set,
  2285. AlternateDirAuthority Set, FallbackDir Not Set */
  2286. {
  2287. /* clear fallback dirs counter */
  2288. n_add_default_fallback_dir_servers_known_default = 0;
  2289. /* clear options*/
  2290. memset(options, 0, sizeof(or_options_t));
  2291. /* clear any previous dir servers:
  2292. consider_adding_dir_servers() should do this anyway */
  2293. clear_dir_servers();
  2294. /* assign options: 0010 */
  2295. options->DirAuthorities = NULL;
  2296. options->AlternateBridgeAuthority = NULL;
  2297. options->AlternateDirAuthority = test_alt_dir_authority;
  2298. options->FallbackDir = NULL;
  2299. /* parse options - ensure we always update by passing NULL old_options */
  2300. consider_adding_dir_servers(options, NULL);
  2301. /* check outcome */
  2302. /* we must not have added the default fallback dirs */
  2303. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2304. {
  2305. /* trusted_dir_servers */
  2306. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2307. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2308. * No Default or Custom Fallback Directories */
  2309. tt_assert(smartlist_len(dir_servers) ==
  2310. 1 + n_default_alt_bridge_authority);
  2311. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2312. int found_D0 = 0;
  2313. SMARTLIST_FOREACH(dir_servers,
  2314. dir_server_t *,
  2315. ds,
  2316. /* increment the found counter if dir_port matches */
  2317. found_D0 +=
  2318. (ds->dir_port == 60090 ?
  2319. 1 : 0)
  2320. );
  2321. tt_assert(found_D0 == 0);
  2322. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2323. int found_B1 = 0;
  2324. SMARTLIST_FOREACH(dir_servers,
  2325. dir_server_t *,
  2326. ds,
  2327. /* increment the found counter if dir_port matches */
  2328. found_B1 +=
  2329. (ds->dir_port == 60091 ?
  2330. 1 : 0)
  2331. );
  2332. tt_assert(found_B1 == 0);
  2333. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2334. int found_A2 = 0;
  2335. SMARTLIST_FOREACH(dir_servers,
  2336. dir_server_t *,
  2337. ds,
  2338. /* increment the found counter if dir_port matches */
  2339. found_A2 +=
  2340. (ds->dir_port == 60092 ?
  2341. 1 : 0)
  2342. );
  2343. tt_assert(found_A2 == 1);
  2344. /* There's no easy way of checking that we have included all the
  2345. * default Bridge authorities (except for hard-coding tonga's details),
  2346. * so let's assume that if the total count above is correct,
  2347. * we have the right ones.
  2348. */
  2349. }
  2350. {
  2351. /* fallback_dir_servers */
  2352. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2353. /* (No D0), (No B1), Default Bridge Authorities, A2,
  2354. * No Custom or Default Fallback Directories */
  2355. tt_assert(smartlist_len(fallback_servers) ==
  2356. 1 + n_default_alt_bridge_authority);
  2357. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2358. int found_D0 = 0;
  2359. SMARTLIST_FOREACH(fallback_servers,
  2360. dir_server_t *,
  2361. ds,
  2362. /* increment the found counter if dir_port matches */
  2363. found_D0 +=
  2364. (ds->dir_port == 60090 ?
  2365. 1 : 0)
  2366. );
  2367. tt_assert(found_D0 == 0);
  2368. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2369. int found_B1 = 0;
  2370. SMARTLIST_FOREACH(fallback_servers,
  2371. dir_server_t *,
  2372. ds,
  2373. /* increment the found counter if dir_port matches */
  2374. found_B1 +=
  2375. (ds->dir_port == 60091 ?
  2376. 1 : 0)
  2377. );
  2378. tt_assert(found_B1 == 0);
  2379. /* AlternateDirAuthority - A2 - dir_port: 60092 */
  2380. int found_A2 = 0;
  2381. SMARTLIST_FOREACH(fallback_servers,
  2382. dir_server_t *,
  2383. ds,
  2384. /* increment the found counter if dir_port matches */
  2385. found_A2 +=
  2386. (ds->dir_port == 60092 ?
  2387. 1 : 0)
  2388. );
  2389. tt_assert(found_A2 == 1);
  2390. /* (No Custom FallbackDir) - No Nickname - dir_port: 60093 */
  2391. int found_non_default_fallback = 0;
  2392. SMARTLIST_FOREACH(fallback_servers,
  2393. dir_server_t *,
  2394. ds,
  2395. /* increment the found counter if dir_port matches */
  2396. found_non_default_fallback +=
  2397. (ds->dir_port == 60093 ?
  2398. 1 : 0)
  2399. );
  2400. tt_assert(found_non_default_fallback == 0);
  2401. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2402. int found_default_fallback = 0;
  2403. SMARTLIST_FOREACH(fallback_servers,
  2404. dir_server_t *,
  2405. ds,
  2406. /* increment the found counter if dir_port matches */
  2407. found_default_fallback +=
  2408. (ds->dir_port == 60099 ?
  2409. 1 : 0)
  2410. );
  2411. tt_assert(found_default_fallback == 0);
  2412. /* There's no easy way of checking that we have included all the
  2413. * default Bridge authorities (except for hard-coding tonga's details),
  2414. * so let's assume that if the total count above is correct,
  2415. * we have the right ones.
  2416. */
  2417. }
  2418. }
  2419. /*
  2420. 4. Outcome: Use Set Custom Fallback Directory
  2421. - Use Default Bridge & Directory Authorities
  2422. Cases expected to yield this outcome:
  2423. 1 (DirAuthorities, AlternateBridgeAuthority and AlternateDirAuthority
  2424. are all NULL, but FallbackDir is set)
  2425. */
  2426. /* Case 1: 0001 - DirAuthorities, AlternateBridgeAuthority
  2427. & AlternateDirAuthority Not Set, FallbackDir Set */
  2428. {
  2429. /* clear fallback dirs counter */
  2430. n_add_default_fallback_dir_servers_known_default = 0;
  2431. /* clear options*/
  2432. memset(options, 0, sizeof(or_options_t));
  2433. /* clear any previous dir servers:
  2434. consider_adding_dir_servers() should do this anyway */
  2435. clear_dir_servers();
  2436. /* assign options: 0001 */
  2437. options->DirAuthorities = NULL;
  2438. options->AlternateBridgeAuthority = NULL;
  2439. options->AlternateDirAuthority = NULL;
  2440. options->FallbackDir = test_fallback_directory;
  2441. /* parse options - ensure we always update by passing NULL old_options */
  2442. consider_adding_dir_servers(options, NULL);
  2443. /* check outcome */
  2444. /* we must not have added the default fallback dirs */
  2445. tt_assert(n_add_default_fallback_dir_servers_known_default == 0);
  2446. {
  2447. /* trusted_dir_servers */
  2448. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2449. /* (No D0), (No B1), Default Bridge Authorities,
  2450. * (No A2), Default v3 Directory Authorities */
  2451. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2452. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2453. int found_D0 = 0;
  2454. SMARTLIST_FOREACH(dir_servers,
  2455. dir_server_t *,
  2456. ds,
  2457. /* increment the found counter if dir_port matches */
  2458. found_D0 +=
  2459. (ds->dir_port == 60090 ?
  2460. 1 : 0)
  2461. );
  2462. tt_assert(found_D0 == 0);
  2463. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2464. int found_B1 = 0;
  2465. SMARTLIST_FOREACH(dir_servers,
  2466. dir_server_t *,
  2467. ds,
  2468. /* increment the found counter if dir_port matches */
  2469. found_B1 +=
  2470. (ds->dir_port == 60091 ?
  2471. 1 : 0)
  2472. );
  2473. tt_assert(found_B1 == 0);
  2474. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2475. int found_A2 = 0;
  2476. SMARTLIST_FOREACH(dir_servers,
  2477. dir_server_t *,
  2478. ds,
  2479. /* increment the found counter if dir_port matches */
  2480. found_A2 +=
  2481. (ds->dir_port == 60092 ?
  2482. 1 : 0)
  2483. );
  2484. tt_assert(found_A2 == 0);
  2485. /* There's no easy way of checking that we have included all the
  2486. * default Bridge & V3 Directory authorities, so let's assume that
  2487. * if the total count above is correct, we have the right ones.
  2488. */
  2489. }
  2490. {
  2491. /* fallback_dir_servers */
  2492. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2493. /* (No D0), (No B1), Default Bridge Authorities,
  2494. * (No A2), Default v3 Directory Authorities,
  2495. * Custom Fallback Directory, (No Default Fallback Directories) */
  2496. tt_assert(smartlist_len(fallback_servers) ==
  2497. 1 + n_default_authorities);
  2498. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2499. int found_D0 = 0;
  2500. SMARTLIST_FOREACH(fallback_servers,
  2501. dir_server_t *,
  2502. ds,
  2503. /* increment the found counter if dir_port matches */
  2504. found_D0 +=
  2505. (ds->dir_port == 60090 ?
  2506. 1 : 0)
  2507. );
  2508. tt_assert(found_D0 == 0);
  2509. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2510. int found_B1 = 0;
  2511. SMARTLIST_FOREACH(fallback_servers,
  2512. dir_server_t *,
  2513. ds,
  2514. /* increment the found counter if dir_port matches */
  2515. found_B1 +=
  2516. (ds->dir_port == 60091 ?
  2517. 1 : 0)
  2518. );
  2519. tt_assert(found_B1 == 0);
  2520. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2521. int found_A2 = 0;
  2522. SMARTLIST_FOREACH(fallback_servers,
  2523. dir_server_t *,
  2524. ds,
  2525. /* increment the found counter if dir_port matches */
  2526. found_A2 +=
  2527. (ds->dir_port == 60092 ?
  2528. 1 : 0)
  2529. );
  2530. tt_assert(found_A2 == 0);
  2531. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2532. int found_non_default_fallback = 0;
  2533. SMARTLIST_FOREACH(fallback_servers,
  2534. dir_server_t *,
  2535. ds,
  2536. /* increment the found counter if dir_port matches */
  2537. found_non_default_fallback +=
  2538. (ds->dir_port == 60093 ?
  2539. 1 : 0)
  2540. );
  2541. tt_assert(found_non_default_fallback == 1);
  2542. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2543. int found_default_fallback = 0;
  2544. SMARTLIST_FOREACH(fallback_servers,
  2545. dir_server_t *,
  2546. ds,
  2547. /* increment the found counter if dir_port matches */
  2548. found_default_fallback +=
  2549. (ds->dir_port == 60099 ?
  2550. 1 : 0)
  2551. );
  2552. tt_assert(found_default_fallback == 0);
  2553. /* There's no easy way of checking that we have included all the
  2554. * default Bridge & V3 Directory authorities, so let's assume that
  2555. * if the total count above is correct, we have the right ones.
  2556. */
  2557. }
  2558. }
  2559. /*
  2560. 5. Outcome: Use All Defaults
  2561. - Use Default Bridge & Directory Authorities, Default Fallback Directories
  2562. Cases expected to yield this outcome:
  2563. 0 (DirAuthorities, AlternateBridgeAuthority, AlternateDirAuthority
  2564. and FallbackDir are all NULL)
  2565. */
  2566. /* Case 0: 0000 - All Not Set */
  2567. {
  2568. /* clear fallback dirs counter */
  2569. n_add_default_fallback_dir_servers_known_default = 0;
  2570. /* clear options*/
  2571. memset(options, 0, sizeof(or_options_t));
  2572. /* clear any previous dir servers:
  2573. consider_adding_dir_servers() should do this anyway */
  2574. clear_dir_servers();
  2575. /* assign options: 0001 */
  2576. options->DirAuthorities = NULL;
  2577. options->AlternateBridgeAuthority = NULL;
  2578. options->AlternateDirAuthority = NULL;
  2579. options->FallbackDir = NULL;
  2580. /* parse options - ensure we always update by passing NULL old_options */
  2581. consider_adding_dir_servers(options, NULL);
  2582. /* check outcome */
  2583. /* we must have added the default fallback dirs */
  2584. tt_assert(n_add_default_fallback_dir_servers_known_default == 1);
  2585. {
  2586. /* trusted_dir_servers */
  2587. const smartlist_t *dir_servers = router_get_trusted_dir_servers();
  2588. /* (No D0), (No B1), Default Bridge Authorities,
  2589. * (No A2), Default v3 Directory Authorities */
  2590. tt_assert(smartlist_len(dir_servers) == n_default_authorities);
  2591. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2592. int found_D0 = 0;
  2593. SMARTLIST_FOREACH(dir_servers,
  2594. dir_server_t *,
  2595. ds,
  2596. /* increment the found counter if dir_port matches */
  2597. found_D0 +=
  2598. (ds->dir_port == 60090 ?
  2599. 1 : 0)
  2600. );
  2601. tt_assert(found_D0 == 0);
  2602. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2603. int found_B1 = 0;
  2604. SMARTLIST_FOREACH(dir_servers,
  2605. dir_server_t *,
  2606. ds,
  2607. /* increment the found counter if dir_port matches */
  2608. found_B1 +=
  2609. (ds->dir_port == 60091 ?
  2610. 1 : 0)
  2611. );
  2612. tt_assert(found_B1 == 0);
  2613. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2614. int found_A2 = 0;
  2615. SMARTLIST_FOREACH(dir_servers,
  2616. dir_server_t *,
  2617. ds,
  2618. /* increment the found counter if dir_port matches */
  2619. found_A2 +=
  2620. (ds->dir_port == 60092 ?
  2621. 1 : 0)
  2622. );
  2623. tt_assert(found_A2 == 0);
  2624. /* There's no easy way of checking that we have included all the
  2625. * default Bridge & V3 Directory authorities, so let's assume that
  2626. * if the total count above is correct, we have the right ones.
  2627. */
  2628. }
  2629. {
  2630. /* fallback_dir_servers */
  2631. const smartlist_t *fallback_servers = router_get_fallback_dir_servers();
  2632. /* (No D0), (No B1), Default Bridge Authorities,
  2633. * (No A2), Default v3 Directory Authorities,
  2634. * (No Custom Fallback Directory), Default Fallback Directories */
  2635. tt_assert(smartlist_len(fallback_servers) ==
  2636. n_default_authorities + n_default_fallback_dir);
  2637. /* (No DirAuthorities) - D0 - dir_port: 60090 */
  2638. int found_D0 = 0;
  2639. SMARTLIST_FOREACH(fallback_servers,
  2640. dir_server_t *,
  2641. ds,
  2642. /* increment the found counter if dir_port matches */
  2643. found_D0 +=
  2644. (ds->dir_port == 60090 ?
  2645. 1 : 0)
  2646. );
  2647. tt_assert(found_D0 == 0);
  2648. /* (No AlternateBridgeAuthority) - B1 - dir_port: 60091 */
  2649. int found_B1 = 0;
  2650. SMARTLIST_FOREACH(fallback_servers,
  2651. dir_server_t *,
  2652. ds,
  2653. /* increment the found counter if dir_port matches */
  2654. found_B1 +=
  2655. (ds->dir_port == 60091 ?
  2656. 1 : 0)
  2657. );
  2658. tt_assert(found_B1 == 0);
  2659. /* (No AlternateDirAuthority) - A2 - dir_port: 60092 */
  2660. int found_A2 = 0;
  2661. SMARTLIST_FOREACH(fallback_servers,
  2662. dir_server_t *,
  2663. ds,
  2664. /* increment the found counter if dir_port matches */
  2665. found_A2 +=
  2666. (ds->dir_port == 60092 ?
  2667. 1 : 0)
  2668. );
  2669. tt_assert(found_A2 == 0);
  2670. /* Custom FallbackDir - No Nickname - dir_port: 60093 */
  2671. int found_non_default_fallback = 0;
  2672. SMARTLIST_FOREACH(fallback_servers,
  2673. dir_server_t *,
  2674. ds,
  2675. /* increment the found counter if dir_port matches */
  2676. found_non_default_fallback +=
  2677. (ds->dir_port == 60093 ?
  2678. 1 : 0)
  2679. );
  2680. tt_assert(found_non_default_fallback == 0);
  2681. /* (No Default FallbackDir) - No Nickname - dir_port: 60099 */
  2682. int found_default_fallback = 0;
  2683. SMARTLIST_FOREACH(fallback_servers,
  2684. dir_server_t *,
  2685. ds,
  2686. /* increment the found counter if dir_port matches */
  2687. found_default_fallback +=
  2688. (ds->dir_port == 60099 ?
  2689. 1 : 0)
  2690. );
  2691. tt_assert(found_default_fallback == 1);
  2692. /* There's no easy way of checking that we have included all the
  2693. * default Bridge & V3 Directory authorities, and the default
  2694. * Fallback Directories, so let's assume that if the total count
  2695. * above is correct, we have the right ones.
  2696. */
  2697. }
  2698. }
  2699. done:
  2700. clear_dir_servers();
  2701. tor_free(test_dir_authority->key);
  2702. tor_free(test_dir_authority->value);
  2703. tor_free(test_dir_authority);
  2704. tor_free(test_alt_dir_authority->key);
  2705. tor_free(test_alt_dir_authority->value);
  2706. tor_free(test_alt_dir_authority);
  2707. tor_free(test_alt_bridge_authority->key);
  2708. tor_free(test_alt_bridge_authority->value);
  2709. tor_free(test_alt_bridge_authority);
  2710. tor_free(test_fallback_directory->key);
  2711. tor_free(test_fallback_directory->value);
  2712. tor_free(test_fallback_directory);
  2713. options->DirAuthorities = NULL;
  2714. options->AlternateBridgeAuthority = NULL;
  2715. options->AlternateDirAuthority = NULL;
  2716. options->FallbackDir = NULL;
  2717. or_options_free(options);
  2718. UNMOCK(add_default_fallback_dir_servers);
  2719. }
  2720. static void
  2721. test_config_default_fallback_dirs(void *arg)
  2722. {
  2723. const char *fallback[] = {
  2724. #include "../or/fallback_dirs.inc"
  2725. NULL
  2726. };
  2727. int n_included_fallback_dirs = 0;
  2728. int n_added_fallback_dirs = 0;
  2729. (void)arg;
  2730. clear_dir_servers();
  2731. while (fallback[n_included_fallback_dirs])
  2732. n_included_fallback_dirs++;
  2733. add_default_fallback_dir_servers();
  2734. n_added_fallback_dirs = smartlist_len(router_get_fallback_dir_servers());
  2735. tt_assert(n_included_fallback_dirs == n_added_fallback_dirs);
  2736. done:
  2737. clear_dir_servers();
  2738. }
  2739. #define CONFIG_TEST(name, flags) \
  2740. { #name, test_config_ ## name, flags, NULL, NULL }
  2741. struct testcase_t config_tests[] = {
  2742. CONFIG_TEST(adding_dir_servers, 0),
  2743. CONFIG_TEST(default_fallback_dirs, 0),
  2744. CONFIG_TEST(resolve_my_address, TT_FORK),
  2745. CONFIG_TEST(addressmap, 0),
  2746. CONFIG_TEST(parse_bridge_line, 0),
  2747. CONFIG_TEST(parse_transport_options_line, 0),
  2748. CONFIG_TEST(parse_transport_plugin_line, TT_FORK),
  2749. CONFIG_TEST(check_or_create_data_subdir, TT_FORK),
  2750. CONFIG_TEST(write_to_data_subdir, TT_FORK),
  2751. CONFIG_TEST(fix_my_family, 0),
  2752. END_OF_TESTCASES
  2753. };