test_dir_handle_get.c 77 KB

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