test_dir_handle_get.c 74 KB

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