test_dir_handle_get.c 75 KB

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