test_dir_handle_get.c 72 KB

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