test_dir_handle_get.c 75 KB

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