test_dir_handle_get.c 78 KB

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