test_dir_handle_get.c 74 KB

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