test_dir_handle_get.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #define RENDCOMMON_PRIVATE
  6. #define GEOIP_PRIVATE
  7. #define CONNECTION_PRIVATE
  8. #define CONFIG_PRIVATE
  9. #define RENDCACHE_PRIVATE
  10. #include "or.h"
  11. #include "config.h"
  12. #include "connection.h"
  13. #include "directory.h"
  14. #include "test.h"
  15. #include "connection.h"
  16. #include "rendcommon.h"
  17. #include "rendcache.h"
  18. #include "router.h"
  19. #include "routerlist.h"
  20. #include "rend_test_helpers.h"
  21. #include "microdesc.h"
  22. #include "test_helpers.h"
  23. #include "nodelist.h"
  24. #include "entrynodes.h"
  25. #include "routerparse.h"
  26. #include "networkstatus.h"
  27. #include "geoip.h"
  28. #include "dirserv.h"
  29. #include "torgzip.h"
  30. #include "dirvote.h"
  31. #ifdef _WIN32
  32. /* For mkdir() */
  33. #include <direct.h>
  34. #else
  35. #include <dirent.h>
  36. #endif
  37. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  38. DISABLE_GCC_WARNING(overlength-strings)
  39. /* We allow huge string constants in the unit tests, but not in the code
  40. * at large. */
  41. #endif
  42. #include "vote_descriptors.inc"
  43. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  44. ENABLE_GCC_WARNING(overlength-strings)
  45. #endif
  46. #define NS_MODULE dir_handle_get
  47. static void
  48. connection_write_to_buf_mock(const char *string, size_t len,
  49. connection_t *conn, int zlib)
  50. {
  51. (void) zlib;
  52. tor_assert(string);
  53. tor_assert(conn);
  54. write_to_buf(string, len, conn->outbuf);
  55. }
  56. #define GET(path) "GET " path " HTTP/1.0\r\n\r\n"
  57. #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
  58. #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
  59. #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
  60. #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
  61. "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
  62. static tor_addr_t MOCK_TOR_ADDR;
  63. static void
  64. test_dir_handle_get_bad_request(void *data)
  65. {
  66. dir_connection_t *conn = NULL;
  67. char *header = NULL;
  68. (void) data;
  69. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  70. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  71. tt_int_op(directory_handle_command_get(conn, "", NULL, 0), OP_EQ, 0);
  72. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  73. NULL, NULL, 1, 0);
  74. tt_str_op(header, OP_EQ, BAD_REQUEST);
  75. done:
  76. UNMOCK(connection_write_to_buf_impl_);
  77. connection_free_(TO_CONN(conn));
  78. tor_free(header);
  79. }
  80. static void
  81. test_dir_handle_get_v1_command_not_found(void *data)
  82. {
  83. dir_connection_t *conn = NULL;
  84. char *header = NULL;
  85. (void) data;
  86. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  87. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  88. // no frontpage configured
  89. tt_ptr_op(get_dirportfrontpage(), OP_EQ, NULL);
  90. /* V1 path */
  91. tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
  92. OP_EQ, 0);
  93. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  94. NULL, NULL, 1, 0);
  95. tt_str_op(NOT_FOUND, OP_EQ, header);
  96. done:
  97. UNMOCK(connection_write_to_buf_impl_);
  98. connection_free_(TO_CONN(conn));
  99. tor_free(header);
  100. }
  101. static const char*
  102. mock_get_dirportfrontpage(void)
  103. {
  104. return "HELLO FROM FRONTPAGE";
  105. }
  106. static void
  107. test_dir_handle_get_v1_command(void *data)
  108. {
  109. dir_connection_t *conn = NULL;
  110. char *header = NULL;
  111. char *body = NULL;
  112. size_t body_used = 0, body_len = 0;
  113. const char *exp_body = NULL;
  114. (void) data;
  115. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  116. MOCK(get_dirportfrontpage, mock_get_dirportfrontpage);
  117. exp_body = get_dirportfrontpage();
  118. body_len = strlen(exp_body);
  119. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  120. tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
  121. OP_EQ, 0);
  122. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  123. &body, &body_used, body_len+1, 0);
  124. tt_assert(header);
  125. tt_assert(body);
  126. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  127. tt_assert(strstr(header, "Content-Type: text/html\r\n"));
  128. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  129. tt_assert(strstr(header, "Content-Length: 20\r\n"));
  130. tt_int_op(body_used, OP_EQ, strlen(body));
  131. tt_str_op(body, OP_EQ, exp_body);
  132. done:
  133. UNMOCK(connection_write_to_buf_impl_);
  134. UNMOCK(get_dirportfrontpage);
  135. connection_free_(TO_CONN(conn));
  136. tor_free(header);
  137. tor_free(body);
  138. }
  139. static void
  140. test_dir_handle_get_not_found(void *data)
  141. {
  142. dir_connection_t *conn = NULL;
  143. char *header = NULL;
  144. (void) data;
  145. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  146. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  147. /* Unrecognized path */
  148. tt_int_op(directory_handle_command_get(conn, GET("/anything"), NULL, 0),
  149. OP_EQ, 0);
  150. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  151. NULL, NULL, 1, 0);
  152. tt_str_op(NOT_FOUND, OP_EQ, header);
  153. done:
  154. UNMOCK(connection_write_to_buf_impl_);
  155. connection_free_(TO_CONN(conn));
  156. tor_free(header);
  157. }
  158. static void
  159. test_dir_handle_get_robots_txt(void *data)
  160. {
  161. dir_connection_t *conn = NULL;
  162. char *header = NULL;
  163. char *body = NULL;
  164. size_t body_used = 0;
  165. (void) data;
  166. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  167. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  168. tt_int_op(directory_handle_command_get(conn, GET("/tor/robots.txt"),
  169. NULL, 0), OP_EQ, 0);
  170. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  171. &body, &body_used, 29, 0);
  172. tt_assert(header);
  173. tt_assert(body);
  174. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  175. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  176. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  177. tt_assert(strstr(header, "Content-Length: 28\r\n"));
  178. tt_int_op(body_used, OP_EQ, strlen(body));
  179. tt_str_op(body, OP_EQ, "User-agent: *\r\nDisallow: /\r\n");
  180. done:
  181. UNMOCK(connection_write_to_buf_impl_);
  182. connection_free_(TO_CONN(conn));
  183. tor_free(header);
  184. tor_free(body);
  185. }
  186. static void
  187. test_dir_handle_get_bytes_txt(void *data)
  188. {
  189. dir_connection_t *conn = NULL;
  190. char *header = NULL;
  191. char *body = NULL;
  192. size_t body_used = 0, body_len = 0;
  193. char buff[30];
  194. char *exp_body = NULL;
  195. (void) data;
  196. exp_body = directory_dump_request_log();
  197. body_len = strlen(exp_body);
  198. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  199. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  200. tt_int_op(directory_handle_command_get(conn, GET("/tor/bytes.txt"), NULL, 0),
  201. OP_EQ, 0);
  202. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  203. &body, &body_used, body_len+1, 0);
  204. tt_assert(header);
  205. tt_assert(body);
  206. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  207. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  208. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  209. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  210. tor_snprintf(buff, sizeof(buff), "Content-Length: %ld\r\n", (long) body_len);
  211. tt_assert(strstr(header, buff));
  212. tt_int_op(body_used, OP_EQ, strlen(body));
  213. tt_str_op(body, OP_EQ, exp_body);
  214. done:
  215. UNMOCK(connection_write_to_buf_impl_);
  216. connection_free_(TO_CONN(conn));
  217. tor_free(header);
  218. tor_free(body);
  219. tor_free(exp_body);
  220. }
  221. #define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
  222. static void
  223. test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
  224. {
  225. dir_connection_t *conn = NULL;
  226. char *header = NULL;
  227. (void) data;
  228. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  229. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  230. // connection is not encrypted
  231. tt_assert(!connection_dir_is_encrypted(conn))
  232. tt_int_op(directory_handle_command_get(conn, RENDEZVOUS2_GET(), NULL, 0),
  233. OP_EQ, 0);
  234. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  235. NULL, NULL, 1, 0);
  236. tt_str_op(NOT_FOUND, OP_EQ, header);
  237. done:
  238. UNMOCK(connection_write_to_buf_impl_);
  239. connection_free_(TO_CONN(conn));
  240. tor_free(header);
  241. }
  242. static void
  243. test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
  244. void *data)
  245. {
  246. dir_connection_t *conn = NULL;
  247. char *header = NULL;
  248. (void) data;
  249. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  250. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  251. // connection is encrypted
  252. TO_CONN(conn)->linked = 1;
  253. tt_assert(connection_dir_is_encrypted(conn));
  254. tt_int_op(directory_handle_command_get(conn,
  255. RENDEZVOUS2_GET("invalid-desc-id"), NULL, 0), OP_EQ, 0);
  256. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  257. NULL, NULL, 1, 0);
  258. tt_str_op(header, OP_EQ, BAD_REQUEST);
  259. done:
  260. UNMOCK(connection_write_to_buf_impl_);
  261. connection_free_(TO_CONN(conn));
  262. tor_free(header);
  263. }
  264. static void
  265. test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
  266. {
  267. dir_connection_t *conn = NULL;
  268. char *header = NULL;
  269. (void) data;
  270. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  271. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  272. // connection is encrypted
  273. TO_CONN(conn)->linked = 1;
  274. tt_assert(connection_dir_is_encrypted(conn));
  275. //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
  276. //case to happen. This test is the same as
  277. //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
  278. //We should refactor to remove the case from the switch.
  279. const char *req = RENDEZVOUS2_GET("1bababababababababababababababab");
  280. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  281. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  282. NULL, NULL, 1, 0);
  283. tt_str_op(header, OP_EQ, BAD_REQUEST);
  284. done:
  285. UNMOCK(connection_write_to_buf_impl_);
  286. connection_free_(TO_CONN(conn));
  287. tor_free(header);
  288. }
  289. static void
  290. test_dir_handle_get_rendezvous2_not_found(void *data)
  291. {
  292. dir_connection_t *conn = NULL;
  293. char *header = NULL;
  294. (void) data;
  295. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  296. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  297. rend_cache_init();
  298. // connection is encrypted
  299. TO_CONN(conn)->linked = 1;
  300. tt_assert(connection_dir_is_encrypted(conn));
  301. const char *req = RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
  302. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  303. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  304. NULL, NULL, 1, 0);
  305. tt_str_op(NOT_FOUND, OP_EQ, header);
  306. done:
  307. UNMOCK(connection_write_to_buf_impl_);
  308. connection_free_(TO_CONN(conn));
  309. tor_free(header);
  310. rend_cache_free_all();
  311. }
  312. NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
  313. static routerinfo_t *mock_routerinfo;
  314. static const routerinfo_t *
  315. NS(router_get_my_routerinfo)(void)
  316. {
  317. if (!mock_routerinfo) {
  318. mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
  319. }
  320. return mock_routerinfo;
  321. }
  322. static void
  323. test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
  324. {
  325. dir_connection_t *conn = NULL;
  326. char *header = NULL;
  327. char *body = NULL;
  328. size_t body_used = 0;
  329. char buff[30];
  330. char req[70];
  331. rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
  332. char *service_id = NULL;
  333. char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
  334. size_t body_len = 0;
  335. (void) data;
  336. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  337. NS_MOCK(router_get_my_routerinfo);
  338. rend_cache_init();
  339. /* create a valid rend service descriptor */
  340. #define RECENT_TIME -10
  341. generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
  342. tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str),
  343. OP_EQ, 0);
  344. base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
  345. DIGEST_LEN);
  346. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  347. // connection is encrypted
  348. TO_CONN(conn)->linked = 1;
  349. tt_assert(connection_dir_is_encrypted(conn));
  350. sprintf(req, RENDEZVOUS2_GET("%s"), desc_id_base32);
  351. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  352. body_len = strlen(desc_holder->desc_str);
  353. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  354. &body, &body_used, body_len+1, 0);
  355. tt_assert(header);
  356. tt_assert(body);
  357. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  358. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  359. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  360. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  361. sprintf(buff, "Content-Length: %ld\r\n", (long) body_len);
  362. tt_assert(strstr(header, buff));
  363. tt_int_op(body_used, OP_EQ, strlen(body));
  364. tt_str_op(body, OP_EQ, desc_holder->desc_str);
  365. done:
  366. UNMOCK(connection_write_to_buf_impl_);
  367. NS_UNMOCK(router_get_my_routerinfo);
  368. connection_free_(TO_CONN(conn));
  369. tor_free(header);
  370. tor_free(body);
  371. rend_encoded_v2_service_descriptor_free(desc_holder);
  372. tor_free(service_id);
  373. rend_cache_free_all();
  374. }
  375. #define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
  376. static void
  377. test_dir_handle_get_micro_d_not_found(void *data)
  378. {
  379. dir_connection_t *conn = NULL;
  380. char *header = NULL;
  381. (void) data;
  382. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  383. #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
  384. #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
  385. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  386. const char *req = MICRODESC_GET(B64_256_1 "-" B64_256_2);
  387. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  388. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  389. NULL, NULL, 1, 0);
  390. tt_str_op(NOT_FOUND, OP_EQ, header);
  391. done:
  392. UNMOCK(connection_write_to_buf_impl_);
  393. connection_free_(TO_CONN(conn));
  394. tor_free(header);
  395. }
  396. static or_options_t *mock_options = NULL;
  397. static void
  398. init_mock_options(void)
  399. {
  400. mock_options = malloc(sizeof(or_options_t));
  401. memset(mock_options, 0, sizeof(or_options_t));
  402. mock_options->TestingTorNetwork = 1;
  403. }
  404. static const or_options_t *
  405. mock_get_options(void)
  406. {
  407. tor_assert(mock_options);
  408. return mock_options;
  409. }
  410. static const char microdesc[] =
  411. "onion-key\n"
  412. "-----BEGIN RSA PUBLIC KEY-----\n"
  413. "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
  414. "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
  415. "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
  416. "-----END RSA PUBLIC KEY-----\n";
  417. static void
  418. test_dir_handle_get_micro_d(void *data)
  419. {
  420. dir_connection_t *conn = NULL;
  421. microdesc_cache_t *mc = NULL ;
  422. smartlist_t *list = NULL;
  423. char digest[DIGEST256_LEN];
  424. char digest_base64[128];
  425. char path[80];
  426. char *header = NULL;
  427. char *body = NULL;
  428. size_t body_used = 0;
  429. (void) data;
  430. MOCK(get_options, mock_get_options);
  431. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  432. /* SETUP */
  433. init_mock_options();
  434. const char *fn = get_fname("dir_handle_datadir_test1");
  435. mock_options->DataDirectory = tor_strdup(fn);
  436. #ifdef _WIN32
  437. tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory));
  438. #else
  439. tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory, 0700));
  440. #endif
  441. /* Add microdesc to cache */
  442. crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  443. base64_encode_nopad(digest_base64, sizeof(digest_base64),
  444. (uint8_t *) digest, DIGEST256_LEN);
  445. mc = get_microdesc_cache();
  446. list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
  447. time(NULL), NULL);
  448. tt_int_op(1, OP_EQ, smartlist_len(list));
  449. /* Make the request */
  450. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  451. sprintf(path, MICRODESC_GET("%s"), digest_base64);
  452. tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
  453. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  454. &body, &body_used, strlen(microdesc)+1, 0);
  455. tt_assert(header);
  456. tt_assert(body);
  457. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  458. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  459. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  460. tt_int_op(body_used, OP_EQ, strlen(body));
  461. tt_str_op(body, OP_EQ, microdesc);
  462. done:
  463. UNMOCK(get_options);
  464. UNMOCK(connection_write_to_buf_impl_);
  465. or_options_free(mock_options); mock_options = NULL;
  466. connection_free_(TO_CONN(conn));
  467. tor_free(header);
  468. tor_free(body);
  469. smartlist_free(list);
  470. microdesc_free_all();
  471. }
  472. static void
  473. test_dir_handle_get_micro_d_server_busy(void *data)
  474. {
  475. dir_connection_t *conn = NULL;
  476. microdesc_cache_t *mc = NULL ;
  477. smartlist_t *list = NULL;
  478. char digest[DIGEST256_LEN];
  479. char digest_base64[128];
  480. char path[80];
  481. char *header = NULL;
  482. (void) data;
  483. MOCK(get_options, mock_get_options);
  484. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  485. /* SETUP */
  486. init_mock_options();
  487. const char *fn = get_fname("dir_handle_datadir_test2");
  488. mock_options->DataDirectory = tor_strdup(fn);
  489. #ifdef _WIN32
  490. tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory));
  491. #else
  492. tt_int_op(0, OP_EQ, mkdir(mock_options->DataDirectory, 0700));
  493. #endif
  494. /* Add microdesc to cache */
  495. crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  496. base64_encode_nopad(digest_base64, sizeof(digest_base64),
  497. (uint8_t *) digest, DIGEST256_LEN);
  498. mc = get_microdesc_cache();
  499. list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
  500. time(NULL), NULL);
  501. tt_int_op(1, OP_EQ, smartlist_len(list));
  502. //Make it busy
  503. mock_options->CountPrivateBandwidth = 1;
  504. /* Make the request */
  505. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  506. sprintf(path, MICRODESC_GET("%s"), digest_base64);
  507. tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
  508. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  509. NULL, NULL, 1, 0);
  510. tt_str_op(SERVER_BUSY, OP_EQ, header);
  511. done:
  512. UNMOCK(get_options);
  513. UNMOCK(connection_write_to_buf_impl_);
  514. or_options_free(mock_options); mock_options = NULL;
  515. connection_free_(TO_CONN(conn));
  516. tor_free(header);
  517. smartlist_free(list);
  518. microdesc_free_all();
  519. }
  520. #define BRIDGES_PATH "/tor/networkstatus-bridges"
  521. static void
  522. test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
  523. {
  524. dir_connection_t *conn = NULL;
  525. char *header = NULL;
  526. (void) data;
  527. MOCK(get_options, mock_get_options);
  528. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  529. /* SETUP */
  530. init_mock_options();
  531. mock_options->BridgeAuthoritativeDir = 1;
  532. mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
  533. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  534. TO_CONN(conn)->linked = 1;
  535. const char *req = GET(BRIDGES_PATH);
  536. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  537. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  538. NULL, NULL, 1, 0);
  539. tt_str_op(NOT_FOUND, OP_EQ, header);
  540. done:
  541. UNMOCK(get_options);
  542. UNMOCK(connection_write_to_buf_impl_);
  543. or_options_free(mock_options); mock_options = NULL;
  544. connection_free_(TO_CONN(conn));
  545. tor_free(header);
  546. }
  547. static void
  548. test_dir_handle_get_networkstatus_bridges(void *data)
  549. {
  550. dir_connection_t *conn = NULL;
  551. char *header = NULL;
  552. (void) data;
  553. MOCK(get_options, mock_get_options);
  554. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  555. /* SETUP */
  556. init_mock_options();
  557. mock_options->BridgeAuthoritativeDir = 1;
  558. mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  559. crypto_digest256(mock_options->BridgePassword_AuthDigest_,
  560. "abcdefghijklm12345", 18, DIGEST_SHA256);
  561. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  562. TO_CONN(conn)->linked = 1;
  563. const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
  564. "Authorization: Basic abcdefghijklm12345\r\n\r\n";
  565. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  566. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  567. NULL, NULL, 1, 0);
  568. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  569. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  570. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  571. tt_assert(strstr(header, "Content-Length: 0\r\n"));
  572. done:
  573. UNMOCK(get_options);
  574. UNMOCK(connection_write_to_buf_impl_);
  575. or_options_free(mock_options); mock_options = NULL;
  576. connection_free_(TO_CONN(conn));
  577. tor_free(header);
  578. }
  579. static void
  580. test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
  581. {
  582. dir_connection_t *conn = NULL;
  583. char *header = NULL;
  584. (void) data;
  585. MOCK(get_options, mock_get_options);
  586. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  587. /* SETUP */
  588. init_mock_options();
  589. mock_options->BridgeAuthoritativeDir = 1;
  590. mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  591. crypto_digest256(mock_options->BridgePassword_AuthDigest_,
  592. "abcdefghijklm12345", 18, DIGEST_SHA256);
  593. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  594. TO_CONN(conn)->linked = 1;
  595. const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
  596. "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
  597. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  598. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  599. NULL, NULL, 1, 0);
  600. tt_str_op(NOT_FOUND, OP_EQ, header);
  601. done:
  602. UNMOCK(get_options);
  603. UNMOCK(connection_write_to_buf_impl_);
  604. or_options_free(mock_options); mock_options = NULL;
  605. connection_free_(TO_CONN(conn));
  606. tor_free(header);
  607. }
  608. #define SERVER_DESC_GET(id) GET("/tor/server/" id)
  609. static void
  610. test_dir_handle_get_server_descriptors_not_found(void* data)
  611. {
  612. dir_connection_t *conn = NULL;
  613. char *header = NULL;
  614. (void) data;
  615. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  616. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  617. const char *req = SERVER_DESC_GET("invalid");
  618. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  619. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  620. NULL, NULL, 1, 0);
  621. tt_str_op(NOT_FOUND, OP_EQ, header);
  622. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_SERVER_BY_FP);
  623. done:
  624. UNMOCK(connection_write_to_buf_impl_);
  625. or_options_free(mock_options); mock_options = NULL;
  626. connection_free_(TO_CONN(conn));
  627. tor_free(header);
  628. }
  629. static void
  630. test_dir_handle_get_server_descriptors_all(void* data)
  631. {
  632. dir_connection_t *conn = NULL;
  633. char *header = NULL;
  634. char *body = NULL;
  635. size_t body_used = 0;
  636. (void) data;
  637. /* Setup fake routerlist. */
  638. helper_setup_fake_routerlist();
  639. //TODO: change to router_get_my_extrainfo when testing "extra" path
  640. NS_MOCK(router_get_my_routerinfo);
  641. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  642. // We are one of the routers
  643. routerlist_t *our_routerlist = router_get_routerlist();
  644. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  645. mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
  646. set_server_identity_key(mock_routerinfo->identity_pkey);
  647. /* Treat "all" requests as if they were unencrypted */
  648. mock_routerinfo->cache_info.send_unencrypted = 1;
  649. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  650. const char *req = SERVER_DESC_GET("all");
  651. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  652. //TODO: Is this a BUG?
  653. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  654. //which is smaller than that by annotation_len bytes
  655. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  656. &body, &body_used,
  657. mock_routerinfo->cache_info.signed_descriptor_len+1, 0);
  658. tt_assert(header);
  659. tt_assert(body);
  660. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  661. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  662. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  663. //TODO: Is this a BUG?
  664. //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
  665. tt_int_op(body_used, OP_EQ,
  666. mock_routerinfo->cache_info.signed_descriptor_len);
  667. tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
  668. mock_routerinfo->cache_info.annotations_len);
  669. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  670. done:
  671. NS_UNMOCK(router_get_my_routerinfo);
  672. UNMOCK(connection_write_to_buf_impl_);
  673. connection_free_(TO_CONN(conn));
  674. tor_free(header);
  675. tor_free(body);
  676. routerlist_free_all();
  677. nodelist_free_all();
  678. entry_guards_free_all();
  679. }
  680. static char
  681. TEST_DESCRIPTOR[] =
  682. "@uploaded-at 2014-06-08 19:20:11\n"
  683. "@source \"127.0.0.1\"\n"
  684. "router test000a 127.0.0.1 5000 0 7000\n"
  685. "platform Tor 0.2.5.3-alpha-dev on Linux\n"
  686. "protocols Link 1 2 Circuit 1\n"
  687. "published 2014-06-08 19:20:11\n"
  688. "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
  689. "uptime 0\n"
  690. "bandwidth 1073741824 1073741824 0\n"
  691. "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
  692. "caches-extra-info\n"
  693. "onion-key\n"
  694. "-----BEGIN RSA PUBLIC KEY-----\n"
  695. "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
  696. "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
  697. "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
  698. "-----END RSA PUBLIC KEY-----\n"
  699. "signing-key\n"
  700. "-----BEGIN RSA PUBLIC KEY-----\n"
  701. "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
  702. "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
  703. "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
  704. "-----END RSA PUBLIC KEY-----\n"
  705. "hidden-service-dir\n"
  706. "contact auth0@test.test\n"
  707. "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
  708. "reject *:*\n"
  709. "router-signature\n"
  710. "-----BEGIN SIGNATURE-----\n"
  711. "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
  712. "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
  713. "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
  714. "-----END SIGNATURE-----\n";
  715. static void
  716. test_dir_handle_get_server_descriptors_authority(void* data)
  717. {
  718. dir_connection_t *conn = NULL;
  719. char *header = NULL;
  720. char *body = NULL;
  721. size_t body_used = 0;
  722. crypto_pk_t *identity_pkey = pk_generate(0);
  723. (void) data;
  724. NS_MOCK(router_get_my_routerinfo);
  725. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  726. /* init mock */
  727. router_get_my_routerinfo();
  728. crypto_pk_get_digest(identity_pkey,
  729. mock_routerinfo->cache_info.identity_digest);
  730. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  731. set_server_identity_key(identity_pkey);
  732. mock_routerinfo->cache_info.send_unencrypted = 1;
  733. /* Setup descriptor */
  734. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  735. mock_routerinfo->cache_info.signed_descriptor_body =
  736. tor_strdup(TEST_DESCRIPTOR);
  737. mock_routerinfo->cache_info.signed_descriptor_len =
  738. strlen(TEST_DESCRIPTOR) - annotation_len;;
  739. mock_routerinfo->cache_info.annotations_len = annotation_len;
  740. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  741. const char *req = SERVER_DESC_GET("authority");
  742. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  743. //TODO: Is this a BUG?
  744. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  745. //is smaller than that by annotation_len bytes
  746. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  747. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  748. tt_assert(header);
  749. tt_assert(body);
  750. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  751. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  752. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  753. tt_int_op(body_used, OP_EQ, strlen(body));
  754. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  755. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  756. done:
  757. NS_UNMOCK(router_get_my_routerinfo);
  758. UNMOCK(connection_write_to_buf_impl_);
  759. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  760. tor_free(mock_routerinfo);
  761. connection_free_(TO_CONN(conn));
  762. tor_free(header);
  763. tor_free(body);
  764. crypto_pk_free(identity_pkey);
  765. }
  766. static void
  767. test_dir_handle_get_server_descriptors_fp(void* data)
  768. {
  769. dir_connection_t *conn = NULL;
  770. char *header = NULL;
  771. char *body = NULL;
  772. size_t body_used = 0;
  773. crypto_pk_t *identity_pkey = pk_generate(0);
  774. (void) data;
  775. NS_MOCK(router_get_my_routerinfo);
  776. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  777. /* init mock */
  778. router_get_my_routerinfo();
  779. crypto_pk_get_digest(identity_pkey,
  780. mock_routerinfo->cache_info.identity_digest);
  781. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  782. set_server_identity_key(identity_pkey);
  783. mock_routerinfo->cache_info.send_unencrypted = 1;
  784. /* Setup descriptor */
  785. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  786. mock_routerinfo->cache_info.signed_descriptor_body =
  787. tor_strdup(TEST_DESCRIPTOR);
  788. mock_routerinfo->cache_info.signed_descriptor_len =
  789. strlen(TEST_DESCRIPTOR) - annotation_len;
  790. mock_routerinfo->cache_info.annotations_len = annotation_len;
  791. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  792. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  793. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  794. const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
  795. DIGEST_LEN);
  796. char req[155];
  797. sprintf(req, SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2), hex_digest);
  798. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  799. //TODO: Is this a BUG?
  800. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  801. //is smaller than that by annotation_len bytes
  802. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  803. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  804. tt_assert(header);
  805. tt_assert(body);
  806. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  807. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  808. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  809. tt_int_op(body_used, OP_EQ, strlen(body));
  810. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  811. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  812. done:
  813. NS_UNMOCK(router_get_my_routerinfo);
  814. UNMOCK(connection_write_to_buf_impl_);
  815. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  816. tor_free(mock_routerinfo);
  817. connection_free_(TO_CONN(conn));
  818. tor_free(header);
  819. tor_free(body);
  820. crypto_pk_free(identity_pkey);
  821. }
  822. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  823. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  824. static void
  825. test_dir_handle_get_server_descriptors_d(void* data)
  826. {
  827. dir_connection_t *conn = NULL;
  828. char *header = NULL;
  829. char *body = NULL;
  830. size_t body_used = 0;
  831. crypto_pk_t *identity_pkey = pk_generate(0);
  832. (void) data;
  833. /* Setup fake routerlist. */
  834. helper_setup_fake_routerlist();
  835. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  836. /* Get one router's signed_descriptor_digest */
  837. routerlist_t *our_routerlist = router_get_routerlist();
  838. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  839. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  840. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  841. DIGEST_LEN);
  842. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  843. char req_header[155];
  844. sprintf(req_header, SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  845. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  846. //TODO: Is this a BUG?
  847. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  848. //which is smaller than that by annotation_len bytes
  849. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  850. &body, &body_used,
  851. router->cache_info.signed_descriptor_len+1, 0);
  852. tt_assert(header);
  853. tt_assert(body);
  854. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  855. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  856. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  857. //TODO: Is this a BUG?
  858. //This is what should be expected:
  859. //tt_int_op(body_used, OP_EQ, strlen(body));
  860. tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
  861. tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
  862. router->cache_info.annotations_len);
  863. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  864. done:
  865. UNMOCK(connection_write_to_buf_impl_);
  866. tor_free(mock_routerinfo);
  867. connection_free_(TO_CONN(conn));
  868. tor_free(header);
  869. tor_free(body);
  870. crypto_pk_free(identity_pkey);
  871. routerlist_free_all();
  872. nodelist_free_all();
  873. entry_guards_free_all();
  874. }
  875. static void
  876. test_dir_handle_get_server_descriptors_busy(void* data)
  877. {
  878. dir_connection_t *conn = NULL;
  879. char *header = NULL;
  880. crypto_pk_t *identity_pkey = pk_generate(0);
  881. (void) data;
  882. /* Setup fake routerlist. */
  883. helper_setup_fake_routerlist();
  884. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  885. //Make it busy
  886. MOCK(get_options, mock_get_options);
  887. init_mock_options();
  888. mock_options->CountPrivateBandwidth = 1;
  889. /* Get one router's signed_descriptor_digest */
  890. routerlist_t *our_routerlist = router_get_routerlist();
  891. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  892. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  893. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  894. DIGEST_LEN);
  895. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  896. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  897. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  898. char req_header[155];
  899. sprintf(req_header, SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  900. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  901. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  902. NULL, NULL, 1, 0);
  903. tt_assert(header);
  904. tt_str_op(SERVER_BUSY, OP_EQ, header);
  905. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  906. done:
  907. UNMOCK(get_options);
  908. UNMOCK(connection_write_to_buf_impl_);
  909. tor_free(mock_routerinfo);
  910. connection_free_(TO_CONN(conn));
  911. tor_free(header);
  912. crypto_pk_free(identity_pkey);
  913. routerlist_free_all();
  914. nodelist_free_all();
  915. entry_guards_free_all();
  916. }
  917. static void
  918. test_dir_handle_get_server_keys_bad_req(void* data)
  919. {
  920. dir_connection_t *conn = NULL;
  921. char *header = NULL;
  922. (void) data;
  923. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  924. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  925. const char *req = GET("/tor/keys/");
  926. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  927. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  928. NULL, NULL, 1, 0);
  929. tt_assert(header);
  930. tt_str_op(BAD_REQUEST, OP_EQ, header);
  931. done:
  932. UNMOCK(connection_write_to_buf_impl_);
  933. connection_free_(TO_CONN(conn));
  934. tor_free(header);
  935. }
  936. static void
  937. test_dir_handle_get_server_keys_all_not_found(void* data)
  938. {
  939. dir_connection_t *conn = NULL;
  940. char *header = NULL;
  941. (void) data;
  942. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  943. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  944. const char *req = GET("/tor/keys/all");
  945. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  946. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  947. NULL, NULL, 1, 0);
  948. tt_assert(header);
  949. tt_str_op(NOT_FOUND, OP_EQ, header);
  950. done:
  951. UNMOCK(connection_write_to_buf_impl_);
  952. connection_free_(TO_CONN(conn));
  953. tor_free(header);
  954. }
  955. #define TEST_CERTIFICATE AUTHORITY_CERT_3
  956. #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
  957. static const char TEST_CERT_IDENT_KEY[] =
  958. "D867ACF56A9D229B35C25F0090BC9867E906BE69";
  959. static void
  960. test_dir_handle_get_server_keys_all(void* data)
  961. {
  962. dir_connection_t *conn = NULL;
  963. char *header = NULL;
  964. char *body = NULL;
  965. size_t body_used = 0;
  966. const char digest[DIGEST_LEN] = "";
  967. dir_server_t *ds = NULL;
  968. (void) data;
  969. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  970. clear_dir_servers();
  971. routerlist_free_all();
  972. /* create a trusted ds */
  973. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  974. NULL, V3_DIRINFO, 1.0);
  975. tt_assert(ds);
  976. dir_server_add(ds);
  977. /* ds v3_identity_digest is the certificate's identity_key */
  978. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  979. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  980. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  981. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  982. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  983. const char *req = GET("/tor/keys/all");
  984. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  985. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  986. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  987. tt_assert(header);
  988. tt_assert(body);
  989. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  990. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  991. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  992. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  993. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  994. done:
  995. UNMOCK(connection_write_to_buf_impl_);
  996. connection_free_(TO_CONN(conn));
  997. tor_free(header);
  998. tor_free(body);
  999. clear_dir_servers();
  1000. routerlist_free_all();
  1001. }
  1002. static void
  1003. test_dir_handle_get_server_keys_authority_not_found(void* data)
  1004. {
  1005. dir_connection_t *conn = NULL;
  1006. char *header = NULL;
  1007. (void) data;
  1008. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1009. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1010. const char *req = GET("/tor/keys/authority");
  1011. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1012. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1013. NULL, NULL, 1, 0);
  1014. tt_assert(header);
  1015. tt_str_op(NOT_FOUND, OP_EQ, header);
  1016. done:
  1017. UNMOCK(connection_write_to_buf_impl_);
  1018. connection_free_(TO_CONN(conn));
  1019. tor_free(header);
  1020. }
  1021. static authority_cert_t * mock_cert = NULL;
  1022. static authority_cert_t *
  1023. get_my_v3_authority_cert_m(void)
  1024. {
  1025. tor_assert(mock_cert);
  1026. return mock_cert;
  1027. }
  1028. static void
  1029. test_dir_handle_get_server_keys_authority(void* data)
  1030. {
  1031. dir_connection_t *conn = NULL;
  1032. char *header = NULL;
  1033. char *body = NULL;
  1034. size_t body_used = 0;
  1035. (void) data;
  1036. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1037. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1038. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1039. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1040. const char *req = GET("/tor/keys/authority");
  1041. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1042. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1043. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1044. tt_assert(header);
  1045. tt_assert(body);
  1046. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1047. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1048. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1049. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1050. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1051. done:
  1052. UNMOCK(get_my_v3_authority_cert);
  1053. UNMOCK(connection_write_to_buf_impl_);
  1054. connection_free_(TO_CONN(conn));
  1055. tor_free(header);
  1056. tor_free(body);
  1057. authority_cert_free(mock_cert); mock_cert = NULL;
  1058. }
  1059. static void
  1060. test_dir_handle_get_server_keys_fp_not_found(void* data)
  1061. {
  1062. dir_connection_t *conn = NULL;
  1063. char *header = NULL;
  1064. (void) data;
  1065. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1066. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1067. const char *req = GET("/tor/keys/fp/somehex");
  1068. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1069. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1070. NULL, NULL, 1, 0);
  1071. tt_assert(header);
  1072. tt_str_op(NOT_FOUND, OP_EQ, header);
  1073. done:
  1074. UNMOCK(connection_write_to_buf_impl_);
  1075. connection_free_(TO_CONN(conn));
  1076. tor_free(header);
  1077. }
  1078. static void
  1079. test_dir_handle_get_server_keys_fp(void* data)
  1080. {
  1081. dir_connection_t *conn = NULL;
  1082. char *header = NULL;
  1083. char *body = NULL;
  1084. size_t body_used = 0;
  1085. dir_server_t *ds = NULL;
  1086. const char digest[DIGEST_LEN] = "";
  1087. (void) data;
  1088. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1089. clear_dir_servers();
  1090. routerlist_free_all();
  1091. /* create a trusted ds */
  1092. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1093. NULL, V3_DIRINFO, 1.0);
  1094. tt_assert(ds);
  1095. dir_server_add(ds);
  1096. /* ds v3_identity_digest is the certificate's identity_key */
  1097. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1098. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1099. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1100. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1101. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1102. char req[71];
  1103. sprintf(req, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1104. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1105. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1106. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1107. tt_assert(header);
  1108. tt_assert(body);
  1109. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1110. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1111. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1112. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1113. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1114. done:
  1115. UNMOCK(connection_write_to_buf_impl_);
  1116. connection_free_(TO_CONN(conn));
  1117. tor_free(header);
  1118. tor_free(body);
  1119. clear_dir_servers();
  1120. routerlist_free_all();
  1121. }
  1122. static void
  1123. test_dir_handle_get_server_keys_sk_not_found(void* data)
  1124. {
  1125. dir_connection_t *conn = NULL;
  1126. char *header = NULL;
  1127. (void) data;
  1128. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1129. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1130. const char *req = GET("/tor/keys/sk/somehex");
  1131. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1132. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1133. NULL, NULL, 1, 0);
  1134. tt_assert(header);
  1135. tt_str_op(NOT_FOUND, OP_EQ, header);
  1136. done:
  1137. UNMOCK(connection_write_to_buf_impl_);
  1138. connection_free_(TO_CONN(conn));
  1139. tor_free(header);
  1140. }
  1141. static void
  1142. test_dir_handle_get_server_keys_sk(void* data)
  1143. {
  1144. dir_connection_t *conn = NULL;
  1145. char *header = NULL;
  1146. char *body = NULL;
  1147. size_t body_used = 0;
  1148. (void) data;
  1149. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1150. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1151. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1152. clear_dir_servers();
  1153. routerlist_free_all();
  1154. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1155. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1156. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1157. char req[71];
  1158. sprintf(req, GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
  1159. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1160. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1161. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1162. tt_assert(header);
  1163. tt_assert(body);
  1164. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1165. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1166. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1167. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1168. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1169. done:
  1170. UNMOCK(get_my_v3_authority_cert);
  1171. UNMOCK(connection_write_to_buf_impl_);
  1172. connection_free_(TO_CONN(conn));
  1173. authority_cert_free(mock_cert); mock_cert = NULL;
  1174. tor_free(header);
  1175. tor_free(body);
  1176. }
  1177. static void
  1178. test_dir_handle_get_server_keys_fpsk_not_found(void* data)
  1179. {
  1180. dir_connection_t *conn = NULL;
  1181. char *header = NULL;
  1182. (void) data;
  1183. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1184. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1185. const char *req = GET("/tor/keys/fp-sk/somehex");
  1186. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1187. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1188. NULL, NULL, 1, 0);
  1189. tt_assert(header);
  1190. tt_str_op(NOT_FOUND, OP_EQ, header);
  1191. done:
  1192. UNMOCK(connection_write_to_buf_impl_);
  1193. connection_free_(TO_CONN(conn));
  1194. tor_free(header);
  1195. }
  1196. static void
  1197. test_dir_handle_get_server_keys_fpsk(void* data)
  1198. {
  1199. dir_connection_t *conn = NULL;
  1200. char *header = NULL;
  1201. char *body = NULL;
  1202. size_t body_used = 0;
  1203. dir_server_t *ds = NULL;
  1204. const char digest[DIGEST_LEN] = "";
  1205. (void) data;
  1206. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1207. clear_dir_servers();
  1208. routerlist_free_all();
  1209. /* create a trusted ds */
  1210. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1211. NULL, V3_DIRINFO, 1.0);
  1212. tt_assert(ds);
  1213. /* ds v3_identity_digest is the certificate's identity_key */
  1214. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1215. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1216. dir_server_add(ds);
  1217. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1218. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1219. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1220. char req[115];
  1221. sprintf(req, GET("/tor/keys/fp-sk/%s-%s"),
  1222. TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
  1223. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1224. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1225. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1226. tt_assert(header);
  1227. tt_assert(body);
  1228. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1229. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1230. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1231. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1232. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1233. done:
  1234. UNMOCK(connection_write_to_buf_impl_);
  1235. connection_free_(TO_CONN(conn));
  1236. tor_free(header);
  1237. tor_free(body);
  1238. clear_dir_servers();
  1239. routerlist_free_all();
  1240. }
  1241. static void
  1242. test_dir_handle_get_server_keys_busy(void* data)
  1243. {
  1244. dir_connection_t *conn = NULL;
  1245. char *header = NULL;
  1246. dir_server_t *ds = NULL;
  1247. const char digest[DIGEST_LEN] = "";
  1248. (void) data;
  1249. clear_dir_servers();
  1250. routerlist_free_all();
  1251. /* create a trusted ds */
  1252. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1253. NULL, V3_DIRINFO, 1.0);
  1254. tt_assert(ds);
  1255. /* ds v3_identity_digest is the certificate's identity_key */
  1256. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1257. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1258. dir_server_add(ds);
  1259. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1260. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1261. MOCK(get_options, mock_get_options);
  1262. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1263. /* setup busy server */
  1264. init_mock_options();
  1265. mock_options->CountPrivateBandwidth = 1;
  1266. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1267. char req[71];
  1268. sprintf(req, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1269. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1270. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1271. NULL, NULL, 1, 0);
  1272. tt_assert(header);
  1273. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1274. done:
  1275. UNMOCK(get_options);
  1276. UNMOCK(connection_write_to_buf_impl_);
  1277. connection_free_(TO_CONN(conn));
  1278. tor_free(header);
  1279. or_options_free(mock_options); mock_options = NULL;
  1280. clear_dir_servers();
  1281. routerlist_free_all();
  1282. }
  1283. static networkstatus_t *mock_ns_val = NULL;
  1284. static networkstatus_t *
  1285. mock_ns_get_by_flavor(consensus_flavor_t f)
  1286. {
  1287. (void)f;
  1288. return mock_ns_val;
  1289. }
  1290. static void
  1291. test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
  1292. {
  1293. dir_connection_t *conn = NULL;
  1294. char *header = NULL;
  1295. char *stats = NULL;
  1296. (void) d;
  1297. /* init mock */
  1298. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1299. mock_ns_val->flavor = FLAV_NS;
  1300. mock_ns_val->voters = smartlist_new();
  1301. /* init mock */
  1302. init_mock_options();
  1303. MOCK(get_options, mock_get_options);
  1304. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1305. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1306. /* start gathering stats */
  1307. mock_options->DirReqStatistics = 1;
  1308. geoip_dirreq_stats_init(time(NULL));
  1309. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1310. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1311. GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
  1312. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1313. NULL, NULL, 1, 0);
  1314. tt_assert(header);
  1315. tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
  1316. stats = geoip_format_dirreq_stats(time(NULL));
  1317. tt_assert(stats);
  1318. tt_assert(strstr(stats, "not-enough-sigs=8"));
  1319. done:
  1320. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1321. UNMOCK(connection_write_to_buf_impl_);
  1322. UNMOCK(get_options);
  1323. connection_free_(TO_CONN(conn));
  1324. tor_free(header);
  1325. tor_free(stats);
  1326. smartlist_free(mock_ns_val->voters);
  1327. tor_free(mock_ns_val);
  1328. or_options_free(mock_options); mock_options = NULL;
  1329. }
  1330. static void
  1331. test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
  1332. {
  1333. dir_connection_t *conn = NULL;
  1334. char *header = NULL;
  1335. char *stats = NULL;
  1336. (void) data;
  1337. init_mock_options();
  1338. MOCK(get_options, mock_get_options);
  1339. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1340. /* start gathering stats */
  1341. mock_options->DirReqStatistics = 1;
  1342. geoip_dirreq_stats_init(time(NULL));
  1343. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1344. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1345. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1346. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1347. NULL, NULL, 1, 0);
  1348. tt_assert(header);
  1349. tt_str_op(NOT_FOUND, OP_EQ, header);
  1350. stats = geoip_format_dirreq_stats(time(NULL));
  1351. tt_assert(stats);
  1352. tt_assert(strstr(stats, "not-found=8"));
  1353. done:
  1354. UNMOCK(connection_write_to_buf_impl_);
  1355. UNMOCK(get_options);
  1356. connection_free_(TO_CONN(conn));
  1357. tor_free(header);
  1358. tor_free(stats);
  1359. or_options_free(mock_options); mock_options = NULL;
  1360. }
  1361. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  1362. int
  1363. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  1364. {
  1365. (void)addr;
  1366. CALLED(geoip_get_country_by_addr)++;
  1367. return 1;
  1368. }
  1369. static void
  1370. status_vote_current_consensus_ns_test(char **header, char **body,
  1371. size_t *body_len)
  1372. {
  1373. common_digests_t digests;
  1374. dir_connection_t *conn = NULL;
  1375. #define NETWORK_STATUS "some network status string"
  1376. dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
  1377. time(NULL));
  1378. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1379. tt_assert(mock_options);
  1380. mock_options->DirReqStatistics = 1;
  1381. geoip_dirreq_stats_init(time(NULL));
  1382. /* init geoip database */
  1383. geoip_parse_entry("10,50,AB", AF_INET);
  1384. tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
  1385. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1386. TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  1387. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1388. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1389. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1390. body, body_len, strlen(NETWORK_STATUS)+7, 0);
  1391. done:
  1392. UNMOCK(connection_write_to_buf_impl_);
  1393. connection_free_(TO_CONN(conn));
  1394. }
  1395. static void
  1396. test_dir_handle_get_status_vote_current_consensus_ns(void* data)
  1397. {
  1398. char *header = NULL;
  1399. char *body = NULL, *comp_body = NULL;
  1400. size_t body_used = 0, comp_body_used = 0;
  1401. char *stats = NULL, *hist = NULL;
  1402. (void) data;
  1403. dirserv_free_all();
  1404. clear_geoip_db();
  1405. NS_MOCK(geoip_get_country_by_addr);
  1406. MOCK(get_options, mock_get_options);
  1407. init_mock_options();
  1408. status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
  1409. tt_assert(header);
  1410. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1411. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1412. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1413. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  1414. compress_method_t compression = detect_compression_method(comp_body,
  1415. comp_body_used);
  1416. tt_int_op(ZLIB_METHOD, OP_EQ, compression);
  1417. tor_gzip_uncompress(&body, &body_used, comp_body, comp_body_used,
  1418. compression, 0, LOG_PROTOCOL_WARN);
  1419. tt_str_op(NETWORK_STATUS, OP_EQ, body);
  1420. tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
  1421. stats = geoip_format_dirreq_stats(time(NULL));
  1422. tt_assert(stats);
  1423. tt_assert(strstr(stats, "ok=8"));
  1424. tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
  1425. tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
  1426. tt_assert(strstr(stats, "dirreq-v3-direct-dl"
  1427. " complete=0,timeout=0,running=4"));
  1428. hist = geoip_get_request_history();
  1429. tt_assert(hist);
  1430. tt_str_op("ab=8", OP_EQ, hist);
  1431. done:
  1432. NS_UNMOCK(geoip_get_country_by_addr);
  1433. UNMOCK(get_options);
  1434. tor_free(header);
  1435. tor_free(comp_body);
  1436. tor_free(body);
  1437. tor_free(stats);
  1438. tor_free(hist);
  1439. or_options_free(mock_options); mock_options = NULL;
  1440. dirserv_free_all();
  1441. clear_geoip_db();
  1442. }
  1443. static void
  1444. test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
  1445. {
  1446. char *header = NULL;
  1447. char *body = NULL;
  1448. size_t body_used = 0;
  1449. char *stats = NULL;
  1450. (void) data;
  1451. dirserv_free_all();
  1452. clear_geoip_db();
  1453. MOCK(get_options, mock_get_options);
  1454. // Make it busy
  1455. init_mock_options();
  1456. mock_options->CountPrivateBandwidth = 1;
  1457. status_vote_current_consensus_ns_test(&header, &body, &body_used);
  1458. tt_assert(header);
  1459. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1460. stats = geoip_format_dirreq_stats(time(NULL));
  1461. tt_assert(stats);
  1462. tt_assert(strstr(stats, "busy=8"));
  1463. done:
  1464. UNMOCK(get_options);
  1465. tor_free(header);
  1466. tor_free(body);
  1467. or_options_free(mock_options); mock_options = NULL;
  1468. tor_free(stats);
  1469. dirserv_free_all();
  1470. clear_geoip_db();
  1471. }
  1472. static void
  1473. test_dir_handle_get_status_vote_current_not_found(void* data)
  1474. {
  1475. dir_connection_t *conn = NULL;
  1476. char *header = NULL;
  1477. (void) data;
  1478. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1479. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1480. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1481. GET("/tor/status-vote/current/" HEX1), NULL, 0));
  1482. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1483. NULL, NULL, 1, 0);
  1484. tt_assert(header);
  1485. tt_str_op(NOT_FOUND, OP_EQ, header);
  1486. done:
  1487. UNMOCK(connection_write_to_buf_impl_);
  1488. connection_free_(TO_CONN(conn));
  1489. tor_free(header);
  1490. }
  1491. #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
  1492. static void
  1493. status_vote_current_d_test(char **header, char **body, size_t *body_l)
  1494. {
  1495. dir_connection_t *conn = NULL;
  1496. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1497. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1498. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1499. GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
  1500. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1501. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1502. tt_assert(header);
  1503. done:
  1504. UNMOCK(connection_write_to_buf_impl_);
  1505. connection_free_(TO_CONN(conn));
  1506. }
  1507. static void
  1508. status_vote_next_d_test(char **header, char **body, size_t *body_l)
  1509. {
  1510. dir_connection_t *conn = NULL;
  1511. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1512. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1513. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1514. GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
  1515. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1516. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1517. tt_assert(header);
  1518. done:
  1519. UNMOCK(connection_write_to_buf_impl_);
  1520. connection_free_(TO_CONN(conn));
  1521. }
  1522. static void
  1523. test_dir_handle_get_status_vote_current_d_not_found(void* data)
  1524. {
  1525. char *header = NULL;
  1526. (void) data;
  1527. status_vote_current_d_test(&header, NULL, NULL);
  1528. tt_assert(header);
  1529. tt_str_op(NOT_FOUND, OP_EQ, header);
  1530. done:
  1531. tor_free(header);
  1532. }
  1533. static void
  1534. test_dir_handle_get_status_vote_next_d_not_found(void* data)
  1535. {
  1536. char *header = NULL;
  1537. (void) data;
  1538. status_vote_next_d_test(&header, NULL, NULL);
  1539. tt_assert(header);
  1540. tt_str_op(NOT_FOUND, OP_EQ, header);
  1541. done:
  1542. UNMOCK(connection_write_to_buf_impl_);
  1543. tor_free(header);
  1544. }
  1545. static void
  1546. test_dir_handle_get_status_vote_d(void* data)
  1547. {
  1548. char *header = NULL, *body = NULL;
  1549. size_t body_used = 0;
  1550. dir_server_t *ds = NULL;
  1551. const char digest[DIGEST_LEN] = "";
  1552. (void) data;
  1553. clear_dir_servers();
  1554. dirvote_free_all();
  1555. /* create a trusted ds */
  1556. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1557. NULL, V3_DIRINFO, 1.0);
  1558. tt_assert(ds);
  1559. dir_server_add(ds);
  1560. /* ds v3_identity_digest is the certificate's identity_key */
  1561. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1562. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1563. init_mock_options();
  1564. mock_options->AuthoritativeDir = 1;
  1565. mock_options->V3AuthoritativeDir = 1;
  1566. mock_options->TestingV3AuthVotingStartOffset = 0;
  1567. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1568. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1569. mock_options->TestingV3AuthInitialDistDelay = 1;
  1570. time_t now = 1441223455 -1;
  1571. dirvote_recalculate_timing(mock_options, now);
  1572. const char *msg_out = NULL;
  1573. int status_out = 0;
  1574. struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1575. &status_out);
  1576. tt_assert(pv);
  1577. status_vote_current_d_test(&header, &body, &body_used);
  1578. tt_assert(header);
  1579. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1580. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1581. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1582. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1583. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1584. tor_free(header);
  1585. tor_free(body);
  1586. status_vote_next_d_test(&header, &body, &body_used);
  1587. tt_assert(header);
  1588. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1589. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1590. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1591. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1592. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1593. done:
  1594. tor_free(header);
  1595. tor_free(body);
  1596. or_options_free(mock_options); mock_options = NULL;
  1597. clear_dir_servers();
  1598. dirvote_free_all();
  1599. }
  1600. static void
  1601. test_dir_handle_get_status_vote_next_not_found(void* data)
  1602. {
  1603. dir_connection_t *conn = NULL;
  1604. char *header = NULL;
  1605. (void) data;
  1606. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1607. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1608. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1609. GET("/tor/status-vote/next/" HEX1), NULL, 0));
  1610. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1611. NULL, NULL, 1, 0);
  1612. tt_assert(header);
  1613. tt_str_op(NOT_FOUND, OP_EQ, header);
  1614. done:
  1615. UNMOCK(connection_write_to_buf_impl_);
  1616. connection_free_(TO_CONN(conn));
  1617. tor_free(header);
  1618. }
  1619. static void
  1620. status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
  1621. {
  1622. dir_connection_t *conn = NULL;
  1623. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1624. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1625. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1626. GET("/tor/status-vote/next/consensus"), NULL, 0));
  1627. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1628. body, body_used, 18, 0);
  1629. done:
  1630. UNMOCK(connection_write_to_buf_impl_);
  1631. connection_free_(TO_CONN(conn));
  1632. }
  1633. static void
  1634. test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
  1635. {
  1636. char *header = NULL, *body = NULL;
  1637. size_t body_used;
  1638. (void) data;
  1639. status_vote_next_consensus_test(&header, &body, &body_used);
  1640. tt_assert(header);
  1641. tt_str_op(NOT_FOUND, OP_EQ, header);
  1642. done:
  1643. tor_free(header);
  1644. tor_free(body);
  1645. }
  1646. static void
  1647. test_dir_handle_get_status_vote_current_authority_not_found(void* data)
  1648. {
  1649. dir_connection_t *conn = NULL;
  1650. char *header = NULL;
  1651. (void) data;
  1652. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1653. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1654. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1655. GET("/tor/status-vote/current/authority"), NULL, 0));
  1656. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1657. NULL, NULL, 1, 0);
  1658. tt_assert(header);
  1659. tt_str_op(NOT_FOUND, OP_EQ, header);
  1660. done:
  1661. UNMOCK(connection_write_to_buf_impl_);
  1662. connection_free_(TO_CONN(conn));
  1663. tor_free(header);
  1664. }
  1665. static void
  1666. test_dir_handle_get_status_vote_next_authority_not_found(void* data)
  1667. {
  1668. dir_connection_t *conn = NULL;
  1669. char *header = NULL;
  1670. (void) data;
  1671. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1672. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1673. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1674. GET("/tor/status-vote/next/authority"), NULL, 0));
  1675. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1676. NULL, NULL, 1, 0);
  1677. tt_assert(header);
  1678. tt_str_op(NOT_FOUND, OP_EQ, header);
  1679. done:
  1680. UNMOCK(connection_write_to_buf_impl_);
  1681. connection_free_(TO_CONN(conn));
  1682. tor_free(header);
  1683. }
  1684. NS_DECL(const char*,
  1685. dirvote_get_pending_consensus, (consensus_flavor_t flav));
  1686. const char*
  1687. NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
  1688. {
  1689. (void)flav;
  1690. return "pending consensus";
  1691. }
  1692. static void
  1693. test_dir_handle_get_status_vote_next_consensus(void* data)
  1694. {
  1695. char *header = NULL, *body = NULL;
  1696. size_t body_used = 0;
  1697. (void) data;
  1698. NS_MOCK(dirvote_get_pending_consensus);
  1699. status_vote_next_consensus_test(&header, &body, &body_used);
  1700. tt_assert(header);
  1701. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1702. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1703. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1704. tt_assert(strstr(header, "Content-Length: 17\r\n"));
  1705. tt_str_op("pending consensus", OP_EQ, body);
  1706. done:
  1707. NS_UNMOCK(dirvote_get_pending_consensus);
  1708. tor_free(header);
  1709. tor_free(body);
  1710. }
  1711. static void
  1712. test_dir_handle_get_status_vote_next_consensus_busy(void* data)
  1713. {
  1714. char *header = NULL, *body = NULL;
  1715. size_t body_used = 0;
  1716. (void) data;
  1717. MOCK(get_options, mock_get_options);
  1718. NS_MOCK(dirvote_get_pending_consensus);
  1719. //Make it busy
  1720. init_mock_options();
  1721. mock_options->CountPrivateBandwidth = 1;
  1722. status_vote_next_consensus_test(&header, &body, &body_used);
  1723. tt_assert(header);
  1724. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1725. done:
  1726. NS_UNMOCK(dirvote_get_pending_consensus);
  1727. UNMOCK(get_options);
  1728. tor_free(header);
  1729. tor_free(body);
  1730. or_options_free(mock_options); mock_options = NULL;
  1731. }
  1732. static void
  1733. status_vote_next_consensus_signatures_test(char **header, char **body,
  1734. size_t *body_used)
  1735. {
  1736. dir_connection_t *conn = NULL;
  1737. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1738. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1739. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1740. GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
  1741. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1742. body, body_used, 22, 0);
  1743. done:
  1744. connection_free_(TO_CONN(conn));
  1745. UNMOCK(connection_write_to_buf_impl_);
  1746. }
  1747. static void
  1748. test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
  1749. {
  1750. char *header = NULL, *body = NULL;
  1751. size_t body_used;
  1752. (void) data;
  1753. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1754. tt_assert(header);
  1755. tt_str_op(NOT_FOUND, OP_EQ, header);
  1756. done:
  1757. tor_free(header);
  1758. tor_free(body);
  1759. }
  1760. NS_DECL(const char*,
  1761. dirvote_get_pending_detached_signatures, (void));
  1762. const char*
  1763. NS(dirvote_get_pending_detached_signatures)(void)
  1764. {
  1765. return "pending detached sigs";
  1766. }
  1767. static void
  1768. test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
  1769. {
  1770. char *header = NULL, *body = NULL;
  1771. size_t body_used = 0;
  1772. (void) data;
  1773. NS_MOCK(dirvote_get_pending_detached_signatures);
  1774. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1775. tt_assert(header);
  1776. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1777. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1778. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1779. tt_assert(strstr(header, "Content-Length: 21\r\n"));
  1780. tt_str_op("pending detached sigs", OP_EQ, body);
  1781. done:
  1782. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1783. tor_free(header);
  1784. tor_free(body);
  1785. }
  1786. static void
  1787. test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
  1788. {
  1789. char *header = NULL, *body = NULL;
  1790. size_t body_used;
  1791. (void) data;
  1792. NS_MOCK(dirvote_get_pending_detached_signatures);
  1793. MOCK(get_options, mock_get_options);
  1794. //Make it busy
  1795. init_mock_options();
  1796. mock_options->CountPrivateBandwidth = 1;
  1797. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1798. tt_assert(header);
  1799. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1800. done:
  1801. UNMOCK(get_options);
  1802. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1803. tor_free(header);
  1804. tor_free(body);
  1805. or_options_free(mock_options); mock_options = NULL;
  1806. }
  1807. static void
  1808. test_dir_handle_get_status_vote_next_authority(void* data)
  1809. {
  1810. dir_connection_t *conn = NULL;
  1811. char *header = NULL, *body = NULL;
  1812. const char *msg_out = NULL;
  1813. int status_out = 0;
  1814. size_t body_used = 0;
  1815. dir_server_t *ds = NULL;
  1816. const char digest[DIGEST_LEN] = "";
  1817. (void) data;
  1818. clear_dir_servers();
  1819. routerlist_free_all();
  1820. dirvote_free_all();
  1821. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1822. /* create a trusted ds */
  1823. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1824. NULL, V3_DIRINFO, 1.0);
  1825. tt_assert(ds);
  1826. dir_server_add(ds);
  1827. /* ds v3_identity_digest is the certificate's identity_key */
  1828. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1829. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1830. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1831. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1832. init_mock_options();
  1833. mock_options->AuthoritativeDir = 1;
  1834. mock_options->V3AuthoritativeDir = 1;
  1835. mock_options->TestingV3AuthVotingStartOffset = 0;
  1836. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1837. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1838. mock_options->TestingV3AuthInitialDistDelay = 1;
  1839. time_t now = 1441223455 -1;
  1840. dirvote_recalculate_timing(mock_options, now);
  1841. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1842. &status_out);
  1843. tt_assert(vote);
  1844. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1845. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1846. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1847. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1848. GET("/tor/status-vote/next/authority"), NULL, 0));
  1849. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1850. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1851. tt_assert(header);
  1852. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1853. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1854. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1855. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1856. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1857. done:
  1858. UNMOCK(connection_write_to_buf_impl_);
  1859. UNMOCK(get_my_v3_authority_cert);
  1860. connection_free_(TO_CONN(conn));
  1861. tor_free(header);
  1862. tor_free(body);
  1863. authority_cert_free(mock_cert); mock_cert = NULL;
  1864. or_options_free(mock_options); mock_options = NULL;
  1865. clear_dir_servers();
  1866. routerlist_free_all();
  1867. dirvote_free_all();
  1868. }
  1869. static void
  1870. test_dir_handle_get_status_vote_current_authority(void* data)
  1871. {
  1872. dir_connection_t *conn = NULL;
  1873. char *header = NULL, *body = NULL;
  1874. const char *msg_out = NULL;
  1875. int status_out = 0;
  1876. size_t body_used = 0;
  1877. const char digest[DIGEST_LEN] = "";
  1878. dir_server_t *ds = NULL;
  1879. (void) data;
  1880. clear_dir_servers();
  1881. routerlist_free_all();
  1882. dirvote_free_all();
  1883. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1884. /* create a trusted ds */
  1885. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1886. NULL, V3_DIRINFO, 1.0);
  1887. tt_assert(ds);
  1888. dir_server_add(ds);
  1889. /* ds v3_identity_digest is the certificate's identity_key */
  1890. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1891. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1892. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1893. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1894. init_mock_options();
  1895. mock_options->AuthoritativeDir = 1;
  1896. mock_options->V3AuthoritativeDir = 1;
  1897. mock_options->TestingV3AuthVotingStartOffset = 0;
  1898. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1899. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1900. mock_options->TestingV3AuthInitialDistDelay = 1;
  1901. time_t now = 1441223455;
  1902. dirvote_recalculate_timing(mock_options, now-1);
  1903. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1904. &status_out);
  1905. tt_assert(vote);
  1906. // move the pending vote to previous vote
  1907. dirvote_act(mock_options, now+1);
  1908. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1909. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1910. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1911. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1912. GET("/tor/status-vote/current/authority"), NULL, 0));
  1913. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1914. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1915. tt_assert(header);
  1916. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1917. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1918. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1919. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1920. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1921. done:
  1922. UNMOCK(connection_write_to_buf_impl_);
  1923. UNMOCK(get_my_v3_authority_cert);
  1924. connection_free_(TO_CONN(conn));
  1925. tor_free(header);
  1926. tor_free(body);
  1927. authority_cert_free(mock_cert); mock_cert = NULL;
  1928. or_options_free(mock_options); mock_options = NULL;
  1929. clear_dir_servers();
  1930. routerlist_free_all();
  1931. dirvote_free_all();
  1932. }
  1933. #define DIR_HANDLE_CMD(name,flags) \
  1934. { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
  1935. struct testcase_t dir_handle_get_tests[] = {
  1936. DIR_HANDLE_CMD(not_found, 0),
  1937. DIR_HANDLE_CMD(bad_request, 0),
  1938. DIR_HANDLE_CMD(v1_command_not_found, 0),
  1939. DIR_HANDLE_CMD(v1_command, 0),
  1940. DIR_HANDLE_CMD(robots_txt, 0),
  1941. DIR_HANDLE_CMD(bytes_txt, 0),
  1942. DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
  1943. DIR_HANDLE_CMD(rendezvous2_not_found, 0),
  1944. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
  1945. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
  1946. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
  1947. DIR_HANDLE_CMD(micro_d_not_found, 0),
  1948. DIR_HANDLE_CMD(micro_d_server_busy, 0),
  1949. DIR_HANDLE_CMD(micro_d, 0),
  1950. DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
  1951. DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
  1952. DIR_HANDLE_CMD(networkstatus_bridges, 0),
  1953. DIR_HANDLE_CMD(server_descriptors_not_found, 0),
  1954. DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
  1955. DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
  1956. DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
  1957. DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
  1958. DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
  1959. DIR_HANDLE_CMD(server_keys_bad_req, 0),
  1960. DIR_HANDLE_CMD(server_keys_busy, 0),
  1961. DIR_HANDLE_CMD(server_keys_all_not_found, 0),
  1962. DIR_HANDLE_CMD(server_keys_all, 0),
  1963. DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
  1964. DIR_HANDLE_CMD(server_keys_authority, 0),
  1965. DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
  1966. DIR_HANDLE_CMD(server_keys_fp, 0),
  1967. DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
  1968. DIR_HANDLE_CMD(server_keys_sk, 0),
  1969. DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
  1970. DIR_HANDLE_CMD(server_keys_fpsk, 0),
  1971. DIR_HANDLE_CMD(status_vote_current_not_found, 0),
  1972. DIR_HANDLE_CMD(status_vote_next_not_found, 0),
  1973. DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
  1974. DIR_HANDLE_CMD(status_vote_current_authority, 0),
  1975. DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
  1976. DIR_HANDLE_CMD(status_vote_next_authority, 0),
  1977. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, 0),
  1978. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, 0),
  1979. DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, 0),
  1980. DIR_HANDLE_CMD(status_vote_current_consensus_ns, 0),
  1981. DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
  1982. DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
  1983. DIR_HANDLE_CMD(status_vote_d, 0),
  1984. DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
  1985. DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
  1986. DIR_HANDLE_CMD(status_vote_next_consensus, 0),
  1987. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
  1988. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
  1989. DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
  1990. END_OF_TESTCASES
  1991. };