test_dir_handle_get.c 73 KB

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