test_dir_handle_get.c 77 KB

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