test_dir_handle_get.c 77 KB

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