test_dir_handle_get.c 77 KB

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