test_dir_handle_get.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2017, 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_ptr_op(conn->spool, OP_EQ, NULL);
  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. mock_routerinfo->cache_info.published_on = time(NULL);
  611. /* Treat "all" requests as if they were unencrypted */
  612. mock_routerinfo->cache_info.send_unencrypted = 1;
  613. conn = new_dir_conn();
  614. const char *req = SERVER_DESC_GET("all");
  615. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  616. //TODO: Is this a BUG?
  617. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  618. //which is smaller than that by annotation_len bytes
  619. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  620. &body, &body_used,
  621. 1024*1024, 0);
  622. tt_assert(header);
  623. tt_assert(body);
  624. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  625. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  626. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  627. //TODO: Is this a BUG?
  628. //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
  629. tt_int_op(body_used, OP_EQ,
  630. mock_routerinfo->cache_info.signed_descriptor_len);
  631. tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
  632. mock_routerinfo->cache_info.annotations_len);
  633. tt_ptr_op(conn->spool, OP_EQ, NULL);
  634. done:
  635. NS_UNMOCK(router_get_my_routerinfo);
  636. UNMOCK(connection_write_to_buf_impl_);
  637. connection_free_(TO_CONN(conn));
  638. tor_free(header);
  639. tor_free(body);
  640. routerlist_free_all();
  641. nodelist_free_all();
  642. entry_guards_free_all();
  643. }
  644. static char
  645. TEST_DESCRIPTOR[] =
  646. "@uploaded-at 2014-06-08 19:20:11\n"
  647. "@source \"127.0.0.1\"\n"
  648. "router test000a 127.0.0.1 5000 0 7000\n"
  649. "platform Tor 0.2.5.3-alpha-dev on Linux\n"
  650. "protocols Link 1 2 Circuit 1\n"
  651. "published 2014-06-08 19:20:11\n"
  652. "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
  653. "uptime 0\n"
  654. "bandwidth 1073741824 1073741824 0\n"
  655. "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
  656. "caches-extra-info\n"
  657. "onion-key\n"
  658. "-----BEGIN RSA PUBLIC KEY-----\n"
  659. "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
  660. "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
  661. "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
  662. "-----END RSA PUBLIC KEY-----\n"
  663. "signing-key\n"
  664. "-----BEGIN RSA PUBLIC KEY-----\n"
  665. "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
  666. "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
  667. "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
  668. "-----END RSA PUBLIC KEY-----\n"
  669. "hidden-service-dir\n"
  670. "contact auth0@test.test\n"
  671. "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
  672. "reject *:*\n"
  673. "router-signature\n"
  674. "-----BEGIN SIGNATURE-----\n"
  675. "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
  676. "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
  677. "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
  678. "-----END SIGNATURE-----\n";
  679. static void
  680. test_dir_handle_get_server_descriptors_authority(void* data)
  681. {
  682. dir_connection_t *conn = NULL;
  683. char *header = NULL;
  684. char *body = NULL;
  685. size_t body_used = 0;
  686. crypto_pk_t *identity_pkey = pk_generate(0);
  687. (void) data;
  688. NS_MOCK(router_get_my_routerinfo);
  689. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  690. /* init mock */
  691. router_get_my_routerinfo();
  692. crypto_pk_get_digest(identity_pkey,
  693. mock_routerinfo->cache_info.identity_digest);
  694. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  695. set_server_identity_key(identity_pkey);
  696. mock_routerinfo->cache_info.send_unencrypted = 1;
  697. /* Setup descriptor */
  698. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  699. mock_routerinfo->cache_info.signed_descriptor_body =
  700. tor_strdup(TEST_DESCRIPTOR);
  701. mock_routerinfo->cache_info.signed_descriptor_len =
  702. strlen(TEST_DESCRIPTOR) - annotation_len;;
  703. mock_routerinfo->cache_info.annotations_len = annotation_len;
  704. mock_routerinfo->cache_info.published_on = time(NULL);
  705. conn = new_dir_conn();
  706. const char *req = SERVER_DESC_GET("authority");
  707. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  708. //TODO: Is this a BUG?
  709. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  710. //is smaller than that by annotation_len bytes
  711. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  712. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  713. tt_assert(header);
  714. tt_assert(body);
  715. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  716. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  717. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  718. tt_int_op(body_used, OP_EQ, strlen(body));
  719. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  720. tt_ptr_op(conn->spool, OP_EQ, NULL);
  721. done:
  722. NS_UNMOCK(router_get_my_routerinfo);
  723. UNMOCK(connection_write_to_buf_impl_);
  724. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  725. tor_free(mock_routerinfo);
  726. connection_free_(TO_CONN(conn));
  727. tor_free(header);
  728. tor_free(body);
  729. crypto_pk_free(identity_pkey);
  730. }
  731. static void
  732. test_dir_handle_get_server_descriptors_fp(void* data)
  733. {
  734. dir_connection_t *conn = NULL;
  735. char *header = NULL;
  736. char *body = NULL;
  737. size_t body_used = 0;
  738. crypto_pk_t *identity_pkey = pk_generate(0);
  739. (void) data;
  740. NS_MOCK(router_get_my_routerinfo);
  741. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  742. /* init mock */
  743. router_get_my_routerinfo();
  744. crypto_pk_get_digest(identity_pkey,
  745. mock_routerinfo->cache_info.identity_digest);
  746. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  747. set_server_identity_key(identity_pkey);
  748. mock_routerinfo->cache_info.send_unencrypted = 1;
  749. /* Setup descriptor */
  750. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  751. mock_routerinfo->cache_info.signed_descriptor_body =
  752. tor_strdup(TEST_DESCRIPTOR);
  753. mock_routerinfo->cache_info.signed_descriptor_len =
  754. strlen(TEST_DESCRIPTOR) - annotation_len;
  755. mock_routerinfo->cache_info.annotations_len = annotation_len;
  756. mock_routerinfo->cache_info.published_on = time(NULL);
  757. conn = new_dir_conn();
  758. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  759. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  760. const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
  761. DIGEST_LEN);
  762. char req[155];
  763. tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
  764. hex_digest);
  765. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  766. //TODO: Is this a BUG?
  767. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  768. //is smaller than that by annotation_len bytes
  769. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  770. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  771. tt_assert(header);
  772. tt_assert(body);
  773. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  774. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  775. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  776. tt_int_op(body_used, OP_EQ, strlen(body));
  777. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  778. tt_ptr_op(conn->spool, OP_EQ, NULL);
  779. done:
  780. NS_UNMOCK(router_get_my_routerinfo);
  781. UNMOCK(connection_write_to_buf_impl_);
  782. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  783. tor_free(mock_routerinfo);
  784. connection_free_(TO_CONN(conn));
  785. tor_free(header);
  786. tor_free(body);
  787. crypto_pk_free(identity_pkey);
  788. }
  789. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  790. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  791. static void
  792. test_dir_handle_get_server_descriptors_d(void* data)
  793. {
  794. dir_connection_t *conn = NULL;
  795. char *header = NULL;
  796. char *body = NULL;
  797. size_t body_used = 0;
  798. crypto_pk_t *identity_pkey = pk_generate(0);
  799. (void) data;
  800. /* Setup fake routerlist. */
  801. helper_setup_fake_routerlist();
  802. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  803. /* Get one router's signed_descriptor_digest */
  804. routerlist_t *our_routerlist = router_get_routerlist();
  805. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  806. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  807. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  808. DIGEST_LEN);
  809. conn = new_dir_conn();
  810. char req_header[155]; /* XXX Why 155? What kind of number is that?? */
  811. tor_snprintf(req_header, sizeof(req_header),
  812. SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  813. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  814. //TODO: Is this a BUG?
  815. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  816. //which is smaller than that by annotation_len bytes
  817. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  818. &body, &body_used,
  819. router->cache_info.signed_descriptor_len+1, 0);
  820. tt_assert(header);
  821. tt_assert(body);
  822. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  823. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  824. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  825. //TODO: Is this a BUG?
  826. //This is what should be expected:
  827. //tt_int_op(body_used, OP_EQ, strlen(body));
  828. tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
  829. tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
  830. router->cache_info.annotations_len);
  831. tt_ptr_op(conn->spool, OP_EQ, NULL);
  832. done:
  833. UNMOCK(connection_write_to_buf_impl_);
  834. tor_free(mock_routerinfo);
  835. connection_free_(TO_CONN(conn));
  836. tor_free(header);
  837. tor_free(body);
  838. crypto_pk_free(identity_pkey);
  839. routerlist_free_all();
  840. nodelist_free_all();
  841. entry_guards_free_all();
  842. }
  843. static void
  844. test_dir_handle_get_server_descriptors_busy(void* data)
  845. {
  846. dir_connection_t *conn = NULL;
  847. char *header = NULL;
  848. crypto_pk_t *identity_pkey = pk_generate(0);
  849. (void) data;
  850. /* Setup fake routerlist. */
  851. helper_setup_fake_routerlist();
  852. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  853. //Make it busy
  854. MOCK(get_options, mock_get_options);
  855. init_mock_options();
  856. mock_options->CountPrivateBandwidth = 1;
  857. /* Get one router's signed_descriptor_digest */
  858. routerlist_t *our_routerlist = router_get_routerlist();
  859. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  860. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  861. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  862. DIGEST_LEN);
  863. conn = new_dir_conn();
  864. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  865. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  866. char req_header[155]; /* XXX 155? Why 155? */
  867. tor_snprintf(req_header, sizeof(req_header),
  868. SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  869. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  870. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  871. NULL, NULL, 1, 0);
  872. tt_assert(header);
  873. tt_str_op(SERVER_BUSY, OP_EQ, header);
  874. tt_ptr_op(conn->spool, OP_EQ, NULL);
  875. done:
  876. UNMOCK(get_options);
  877. UNMOCK(connection_write_to_buf_impl_);
  878. tor_free(mock_routerinfo);
  879. connection_free_(TO_CONN(conn));
  880. tor_free(header);
  881. crypto_pk_free(identity_pkey);
  882. routerlist_free_all();
  883. nodelist_free_all();
  884. entry_guards_free_all();
  885. }
  886. static void
  887. test_dir_handle_get_server_keys_bad_req(void* data)
  888. {
  889. dir_connection_t *conn = NULL;
  890. char *header = NULL;
  891. (void) data;
  892. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  893. conn = new_dir_conn();
  894. const char *req = GET("/tor/keys/");
  895. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  896. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  897. NULL, NULL, 1, 0);
  898. tt_assert(header);
  899. tt_str_op(BAD_REQUEST, OP_EQ, header);
  900. done:
  901. UNMOCK(connection_write_to_buf_impl_);
  902. connection_free_(TO_CONN(conn));
  903. tor_free(header);
  904. }
  905. static void
  906. test_dir_handle_get_server_keys_all_not_found(void* data)
  907. {
  908. dir_connection_t *conn = NULL;
  909. char *header = NULL;
  910. (void) data;
  911. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  912. conn = new_dir_conn();
  913. const char *req = GET("/tor/keys/all");
  914. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  915. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  916. NULL, NULL, 1, 0);
  917. tt_assert(header);
  918. tt_str_op(NOT_FOUND, OP_EQ, header);
  919. done:
  920. UNMOCK(connection_write_to_buf_impl_);
  921. connection_free_(TO_CONN(conn));
  922. tor_free(header);
  923. }
  924. #define TEST_CERTIFICATE AUTHORITY_CERT_3
  925. #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
  926. static const char TEST_CERT_IDENT_KEY[] =
  927. "D867ACF56A9D229B35C25F0090BC9867E906BE69";
  928. static void
  929. test_dir_handle_get_server_keys_all(void* data)
  930. {
  931. dir_connection_t *conn = NULL;
  932. char *header = NULL;
  933. char *body = NULL;
  934. size_t body_used = 0;
  935. const char digest[DIGEST_LEN] = "";
  936. dir_server_t *ds = NULL;
  937. (void) data;
  938. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  939. clear_dir_servers();
  940. routerlist_free_all();
  941. /* create a trusted ds */
  942. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  943. NULL, V3_DIRINFO, 1.0);
  944. tt_assert(ds);
  945. dir_server_add(ds);
  946. /* ds v3_identity_digest is the certificate's identity_key */
  947. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  948. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  949. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  950. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  951. conn = new_dir_conn();
  952. const char *req = GET("/tor/keys/all");
  953. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  954. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  955. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  956. tt_assert(header);
  957. tt_assert(body);
  958. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  959. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  960. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  961. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  962. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  963. done:
  964. UNMOCK(connection_write_to_buf_impl_);
  965. connection_free_(TO_CONN(conn));
  966. tor_free(header);
  967. tor_free(body);
  968. clear_dir_servers();
  969. routerlist_free_all();
  970. }
  971. static void
  972. test_dir_handle_get_server_keys_authority_not_found(void* data)
  973. {
  974. dir_connection_t *conn = NULL;
  975. char *header = NULL;
  976. (void) data;
  977. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  978. conn = new_dir_conn();
  979. const char *req = GET("/tor/keys/authority");
  980. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  981. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  982. NULL, NULL, 1, 0);
  983. tt_assert(header);
  984. tt_str_op(NOT_FOUND, OP_EQ, header);
  985. done:
  986. UNMOCK(connection_write_to_buf_impl_);
  987. connection_free_(TO_CONN(conn));
  988. tor_free(header);
  989. }
  990. static authority_cert_t * mock_cert = NULL;
  991. static authority_cert_t *
  992. get_my_v3_authority_cert_m(void)
  993. {
  994. tor_assert(mock_cert);
  995. return mock_cert;
  996. }
  997. static void
  998. test_dir_handle_get_server_keys_authority(void* data)
  999. {
  1000. dir_connection_t *conn = NULL;
  1001. char *header = NULL;
  1002. char *body = NULL;
  1003. size_t body_used = 0;
  1004. (void) data;
  1005. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1006. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1007. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1008. conn = new_dir_conn();
  1009. const char *req = GET("/tor/keys/authority");
  1010. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1011. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1012. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1013. tt_assert(header);
  1014. tt_assert(body);
  1015. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1016. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1017. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1018. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1019. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1020. done:
  1021. UNMOCK(get_my_v3_authority_cert);
  1022. UNMOCK(connection_write_to_buf_impl_);
  1023. connection_free_(TO_CONN(conn));
  1024. tor_free(header);
  1025. tor_free(body);
  1026. authority_cert_free(mock_cert); mock_cert = NULL;
  1027. }
  1028. static void
  1029. test_dir_handle_get_server_keys_fp_not_found(void* data)
  1030. {
  1031. dir_connection_t *conn = NULL;
  1032. char *header = NULL;
  1033. (void) data;
  1034. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1035. conn = new_dir_conn();
  1036. const char *req = GET("/tor/keys/fp/somehex");
  1037. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1038. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1039. NULL, NULL, 1, 0);
  1040. tt_assert(header);
  1041. tt_str_op(NOT_FOUND, OP_EQ, header);
  1042. done:
  1043. UNMOCK(connection_write_to_buf_impl_);
  1044. connection_free_(TO_CONN(conn));
  1045. tor_free(header);
  1046. }
  1047. static void
  1048. test_dir_handle_get_server_keys_fp(void* data)
  1049. {
  1050. dir_connection_t *conn = NULL;
  1051. char *header = NULL;
  1052. char *body = NULL;
  1053. size_t body_used = 0;
  1054. dir_server_t *ds = NULL;
  1055. const char digest[DIGEST_LEN] = "";
  1056. (void) data;
  1057. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1058. clear_dir_servers();
  1059. routerlist_free_all();
  1060. /* create a trusted ds */
  1061. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1062. NULL, V3_DIRINFO, 1.0);
  1063. tt_assert(ds);
  1064. dir_server_add(ds);
  1065. /* ds v3_identity_digest is the certificate's identity_key */
  1066. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1067. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1068. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1069. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1070. conn = new_dir_conn();
  1071. char req[71];
  1072. tor_snprintf(req, sizeof(req),
  1073. GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1074. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1075. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1076. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1077. tt_assert(header);
  1078. tt_assert(body);
  1079. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1080. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1081. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1082. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1083. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1084. done:
  1085. UNMOCK(connection_write_to_buf_impl_);
  1086. connection_free_(TO_CONN(conn));
  1087. tor_free(header);
  1088. tor_free(body);
  1089. clear_dir_servers();
  1090. routerlist_free_all();
  1091. }
  1092. static void
  1093. test_dir_handle_get_server_keys_sk_not_found(void* data)
  1094. {
  1095. dir_connection_t *conn = NULL;
  1096. char *header = NULL;
  1097. (void) data;
  1098. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1099. conn = new_dir_conn();
  1100. const char *req = GET("/tor/keys/sk/somehex");
  1101. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1102. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1103. NULL, NULL, 1, 0);
  1104. tt_assert(header);
  1105. tt_str_op(NOT_FOUND, OP_EQ, header);
  1106. done:
  1107. UNMOCK(connection_write_to_buf_impl_);
  1108. connection_free_(TO_CONN(conn));
  1109. tor_free(header);
  1110. }
  1111. static void
  1112. test_dir_handle_get_server_keys_sk(void* data)
  1113. {
  1114. dir_connection_t *conn = NULL;
  1115. char *header = NULL;
  1116. char *body = NULL;
  1117. size_t body_used = 0;
  1118. (void) data;
  1119. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1120. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1121. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1122. clear_dir_servers();
  1123. routerlist_free_all();
  1124. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1125. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1126. conn = new_dir_conn();
  1127. char req[71];
  1128. tor_snprintf(req, sizeof(req),
  1129. GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
  1130. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1131. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1132. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1133. tt_assert(header);
  1134. tt_assert(body);
  1135. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1136. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1137. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1138. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1139. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1140. done:
  1141. UNMOCK(get_my_v3_authority_cert);
  1142. UNMOCK(connection_write_to_buf_impl_);
  1143. connection_free_(TO_CONN(conn));
  1144. authority_cert_free(mock_cert); mock_cert = NULL;
  1145. tor_free(header);
  1146. tor_free(body);
  1147. }
  1148. static void
  1149. test_dir_handle_get_server_keys_fpsk_not_found(void* data)
  1150. {
  1151. dir_connection_t *conn = NULL;
  1152. char *header = NULL;
  1153. (void) data;
  1154. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1155. conn = new_dir_conn();
  1156. const char *req = GET("/tor/keys/fp-sk/somehex");
  1157. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1158. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1159. NULL, NULL, 1, 0);
  1160. tt_assert(header);
  1161. tt_str_op(NOT_FOUND, OP_EQ, header);
  1162. done:
  1163. UNMOCK(connection_write_to_buf_impl_);
  1164. connection_free_(TO_CONN(conn));
  1165. tor_free(header);
  1166. }
  1167. static void
  1168. test_dir_handle_get_server_keys_fpsk(void* data)
  1169. {
  1170. dir_connection_t *conn = NULL;
  1171. char *header = NULL;
  1172. char *body = NULL;
  1173. size_t body_used = 0;
  1174. dir_server_t *ds = NULL;
  1175. const char digest[DIGEST_LEN] = "";
  1176. (void) data;
  1177. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1178. clear_dir_servers();
  1179. routerlist_free_all();
  1180. /* create a trusted ds */
  1181. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1182. NULL, V3_DIRINFO, 1.0);
  1183. tt_assert(ds);
  1184. /* ds v3_identity_digest is the certificate's identity_key */
  1185. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1186. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1187. dir_server_add(ds);
  1188. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1189. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1190. conn = new_dir_conn();
  1191. char req[115];
  1192. tor_snprintf(req, sizeof(req),
  1193. GET("/tor/keys/fp-sk/%s-%s"),
  1194. TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
  1195. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1196. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1197. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1198. tt_assert(header);
  1199. tt_assert(body);
  1200. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1201. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1202. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1203. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1204. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1205. done:
  1206. UNMOCK(connection_write_to_buf_impl_);
  1207. connection_free_(TO_CONN(conn));
  1208. tor_free(header);
  1209. tor_free(body);
  1210. clear_dir_servers();
  1211. routerlist_free_all();
  1212. }
  1213. static void
  1214. test_dir_handle_get_server_keys_busy(void* data)
  1215. {
  1216. dir_connection_t *conn = NULL;
  1217. char *header = NULL;
  1218. dir_server_t *ds = NULL;
  1219. const char digest[DIGEST_LEN] = "";
  1220. (void) data;
  1221. clear_dir_servers();
  1222. routerlist_free_all();
  1223. /* create a trusted ds */
  1224. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1225. NULL, V3_DIRINFO, 1.0);
  1226. tt_assert(ds);
  1227. /* ds v3_identity_digest is the certificate's identity_key */
  1228. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1229. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1230. dir_server_add(ds);
  1231. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1232. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1233. MOCK(get_options, mock_get_options);
  1234. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1235. /* setup busy server */
  1236. init_mock_options();
  1237. mock_options->CountPrivateBandwidth = 1;
  1238. conn = new_dir_conn();
  1239. char req[71];
  1240. tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1241. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1242. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1243. NULL, NULL, 1, 0);
  1244. tt_assert(header);
  1245. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1246. done:
  1247. UNMOCK(get_options);
  1248. UNMOCK(connection_write_to_buf_impl_);
  1249. connection_free_(TO_CONN(conn));
  1250. tor_free(header);
  1251. or_options_free(mock_options); mock_options = NULL;
  1252. clear_dir_servers();
  1253. routerlist_free_all();
  1254. }
  1255. static networkstatus_t *mock_ns_val = NULL;
  1256. static networkstatus_t *
  1257. mock_ns_get_by_flavor(consensus_flavor_t f)
  1258. {
  1259. (void)f;
  1260. return mock_ns_val;
  1261. }
  1262. static void
  1263. test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
  1264. {
  1265. dir_connection_t *conn = NULL;
  1266. char *header = NULL;
  1267. char *stats = NULL;
  1268. (void) d;
  1269. /* init mock */
  1270. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1271. mock_ns_val->flavor = FLAV_NS;
  1272. mock_ns_val->voters = smartlist_new();
  1273. mock_ns_val->valid_until = time(NULL);
  1274. /* init mock */
  1275. init_mock_options();
  1276. MOCK(get_options, mock_get_options);
  1277. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1278. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1279. /* start gathering stats */
  1280. mock_options->DirReqStatistics = 1;
  1281. geoip_dirreq_stats_init(time(NULL));
  1282. conn = new_dir_conn();
  1283. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1284. GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
  1285. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1286. NULL, NULL, 1, 0);
  1287. tt_assert(header);
  1288. tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
  1289. stats = geoip_format_dirreq_stats(time(NULL));
  1290. tt_assert(stats);
  1291. tt_assert(strstr(stats, "not-enough-sigs=8"));
  1292. done:
  1293. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1294. UNMOCK(connection_write_to_buf_impl_);
  1295. UNMOCK(get_options);
  1296. connection_free_(TO_CONN(conn));
  1297. tor_free(header);
  1298. tor_free(stats);
  1299. smartlist_free(mock_ns_val->voters);
  1300. tor_free(mock_ns_val);
  1301. or_options_free(mock_options); mock_options = NULL;
  1302. }
  1303. static void
  1304. test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
  1305. {
  1306. dir_connection_t *conn = NULL;
  1307. char *header = NULL;
  1308. char *stats = NULL;
  1309. (void) data;
  1310. init_mock_options();
  1311. MOCK(get_options, mock_get_options);
  1312. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1313. /* start gathering stats */
  1314. mock_options->DirReqStatistics = 1;
  1315. geoip_dirreq_stats_init(time(NULL));
  1316. conn = new_dir_conn();
  1317. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1318. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1319. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1320. NULL, NULL, 1, 0);
  1321. tt_assert(header);
  1322. tt_str_op(NOT_FOUND, OP_EQ, header);
  1323. stats = geoip_format_dirreq_stats(time(NULL));
  1324. tt_assert(stats);
  1325. tt_assert(strstr(stats, "not-found=8"));
  1326. done:
  1327. UNMOCK(connection_write_to_buf_impl_);
  1328. UNMOCK(get_options);
  1329. connection_free_(TO_CONN(conn));
  1330. tor_free(header);
  1331. tor_free(stats);
  1332. or_options_free(mock_options); mock_options = NULL;
  1333. }
  1334. static void
  1335. test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
  1336. {
  1337. dir_connection_t *conn = NULL;
  1338. char *header = NULL;
  1339. (void)data;
  1340. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1341. mock_ns_val->flavor = FLAV_MICRODESC;
  1342. mock_ns_val->valid_until = time(NULL) - (60 * 60 * 24) - 1;
  1343. init_mock_options();
  1344. MOCK(get_options, mock_get_options);
  1345. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1346. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1347. conn = new_dir_conn();
  1348. setup_capture_of_logs(LOG_WARN);
  1349. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1350. GET("/tor/status-vote/current/consensus-microdesc"), NULL, 0));
  1351. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1352. NULL, NULL, 1, 0);
  1353. tt_assert(header);
  1354. tt_str_op(TOO_OLD, OP_EQ, header);
  1355. expect_log_msg_containing("too old");
  1356. tor_free(header);
  1357. teardown_capture_of_logs();
  1358. setup_capture_of_logs(LOG_WARN);
  1359. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1360. GET("/tor/status-vote/current/consensus"), NULL, 0));
  1361. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1362. NULL, NULL, 1, 0);
  1363. tt_assert(header);
  1364. tt_str_op(TOO_OLD, OP_EQ, header);
  1365. expect_no_log_entry();
  1366. done:
  1367. teardown_capture_of_logs();
  1368. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1369. UNMOCK(connection_write_to_buf_impl_);
  1370. UNMOCK(get_options);
  1371. connection_free_(TO_CONN(conn));
  1372. tor_free(header);
  1373. tor_free(mock_ns_val);
  1374. or_options_free(mock_options); mock_options = NULL;
  1375. }
  1376. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  1377. int
  1378. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  1379. {
  1380. (void)addr;
  1381. CALLED(geoip_get_country_by_addr)++;
  1382. return 1;
  1383. }
  1384. static void
  1385. status_vote_current_consensus_ns_test(char **header, char **body,
  1386. size_t *body_len)
  1387. {
  1388. common_digests_t digests;
  1389. dir_connection_t *conn = NULL;
  1390. #define NETWORK_STATUS "some network status string"
  1391. dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
  1392. time(NULL));
  1393. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1394. tt_assert(mock_options);
  1395. mock_options->DirReqStatistics = 1;
  1396. geoip_dirreq_stats_init(time(NULL));
  1397. /* init geoip database */
  1398. geoip_parse_entry("10,50,AB", AF_INET);
  1399. tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
  1400. conn = new_dir_conn();
  1401. TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  1402. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1403. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1404. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1405. body, body_len, strlen(NETWORK_STATUS)+7, 0);
  1406. done:
  1407. UNMOCK(connection_write_to_buf_impl_);
  1408. connection_free_(TO_CONN(conn));
  1409. }
  1410. static void
  1411. test_dir_handle_get_status_vote_current_consensus_ns(void* data)
  1412. {
  1413. char *header = NULL;
  1414. char *body = NULL, *comp_body = NULL;
  1415. size_t body_used = 0, comp_body_used = 0;
  1416. char *stats = NULL, *hist = NULL;
  1417. (void) data;
  1418. dirserv_free_all();
  1419. clear_geoip_db();
  1420. NS_MOCK(geoip_get_country_by_addr);
  1421. MOCK(get_options, mock_get_options);
  1422. init_mock_options();
  1423. status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
  1424. tt_assert(header);
  1425. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1426. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1427. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1428. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  1429. compress_method_t compression = detect_compression_method(comp_body,
  1430. comp_body_used);
  1431. tt_int_op(ZLIB_METHOD, OP_EQ, compression);
  1432. tor_uncompress(&body, &body_used, comp_body, comp_body_used,
  1433. compression, 0, LOG_PROTOCOL_WARN);
  1434. tt_str_op(NETWORK_STATUS, OP_EQ, body);
  1435. tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
  1436. stats = geoip_format_dirreq_stats(time(NULL));
  1437. tt_assert(stats);
  1438. tt_assert(strstr(stats, "ok=8"));
  1439. tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
  1440. tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
  1441. tt_assert(strstr(stats, "dirreq-v3-direct-dl"
  1442. " complete=0,timeout=0,running=4"));
  1443. hist = geoip_get_request_history();
  1444. tt_assert(hist);
  1445. tt_str_op("ab=8", OP_EQ, hist);
  1446. done:
  1447. NS_UNMOCK(geoip_get_country_by_addr);
  1448. UNMOCK(get_options);
  1449. tor_free(header);
  1450. tor_free(comp_body);
  1451. tor_free(body);
  1452. tor_free(stats);
  1453. tor_free(hist);
  1454. or_options_free(mock_options); mock_options = NULL;
  1455. dirserv_free_all();
  1456. clear_geoip_db();
  1457. }
  1458. static void
  1459. test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
  1460. {
  1461. char *header = NULL;
  1462. char *body = NULL;
  1463. size_t body_used = 0;
  1464. char *stats = NULL;
  1465. (void) data;
  1466. dirserv_free_all();
  1467. clear_geoip_db();
  1468. MOCK(get_options, mock_get_options);
  1469. // Make it busy
  1470. init_mock_options();
  1471. mock_options->CountPrivateBandwidth = 1;
  1472. status_vote_current_consensus_ns_test(&header, &body, &body_used);
  1473. tt_assert(header);
  1474. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1475. stats = geoip_format_dirreq_stats(time(NULL));
  1476. tt_assert(stats);
  1477. tt_assert(strstr(stats, "busy=8"));
  1478. done:
  1479. UNMOCK(get_options);
  1480. tor_free(header);
  1481. tor_free(body);
  1482. or_options_free(mock_options); mock_options = NULL;
  1483. tor_free(stats);
  1484. dirserv_free_all();
  1485. clear_geoip_db();
  1486. }
  1487. static void
  1488. test_dir_handle_get_status_vote_current_not_found(void* data)
  1489. {
  1490. dir_connection_t *conn = NULL;
  1491. char *header = NULL;
  1492. (void) data;
  1493. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1494. conn = new_dir_conn();
  1495. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1496. GET("/tor/status-vote/current/" HEX1), NULL, 0));
  1497. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1498. NULL, NULL, 1, 0);
  1499. tt_assert(header);
  1500. tt_str_op(NOT_FOUND, OP_EQ, header);
  1501. done:
  1502. UNMOCK(connection_write_to_buf_impl_);
  1503. connection_free_(TO_CONN(conn));
  1504. tor_free(header);
  1505. }
  1506. #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
  1507. static void
  1508. status_vote_current_d_test(char **header, char **body, size_t *body_l)
  1509. {
  1510. dir_connection_t *conn = NULL;
  1511. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1512. conn = new_dir_conn();
  1513. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1514. GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
  1515. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1516. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1517. tt_assert(header);
  1518. done:
  1519. UNMOCK(connection_write_to_buf_impl_);
  1520. connection_free_(TO_CONN(conn));
  1521. }
  1522. static void
  1523. status_vote_next_d_test(char **header, char **body, size_t *body_l)
  1524. {
  1525. dir_connection_t *conn = NULL;
  1526. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1527. conn = new_dir_conn();
  1528. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1529. GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
  1530. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1531. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1532. tt_assert(header);
  1533. done:
  1534. UNMOCK(connection_write_to_buf_impl_);
  1535. connection_free_(TO_CONN(conn));
  1536. }
  1537. static void
  1538. test_dir_handle_get_status_vote_current_d_not_found(void* data)
  1539. {
  1540. char *header = NULL;
  1541. (void) data;
  1542. status_vote_current_d_test(&header, NULL, NULL);
  1543. tt_assert(header);
  1544. tt_str_op(NOT_FOUND, OP_EQ, header);
  1545. done:
  1546. tor_free(header);
  1547. }
  1548. static void
  1549. test_dir_handle_get_status_vote_next_d_not_found(void* data)
  1550. {
  1551. char *header = NULL;
  1552. (void) data;
  1553. status_vote_next_d_test(&header, NULL, NULL);
  1554. tt_assert(header);
  1555. tt_str_op(NOT_FOUND, OP_EQ, header);
  1556. done:
  1557. UNMOCK(connection_write_to_buf_impl_);
  1558. tor_free(header);
  1559. }
  1560. static void
  1561. test_dir_handle_get_status_vote_d(void* data)
  1562. {
  1563. char *header = NULL, *body = NULL;
  1564. size_t body_used = 0;
  1565. dir_server_t *ds = NULL;
  1566. const char digest[DIGEST_LEN] = "";
  1567. (void) data;
  1568. clear_dir_servers();
  1569. dirvote_free_all();
  1570. /* create a trusted ds */
  1571. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1572. NULL, V3_DIRINFO, 1.0);
  1573. tt_assert(ds);
  1574. dir_server_add(ds);
  1575. /* ds v3_identity_digest is the certificate's identity_key */
  1576. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1577. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1578. init_mock_options();
  1579. mock_options->AuthoritativeDir = 1;
  1580. mock_options->V3AuthoritativeDir = 1;
  1581. mock_options->TestingV3AuthVotingStartOffset = 0;
  1582. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1583. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1584. mock_options->TestingV3AuthInitialDistDelay = 1;
  1585. time_t now = 1441223455 -1;
  1586. dirvote_recalculate_timing(mock_options, now);
  1587. const char *msg_out = NULL;
  1588. int status_out = 0;
  1589. struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1590. &status_out);
  1591. tt_assert(pv);
  1592. status_vote_current_d_test(&header, &body, &body_used);
  1593. tt_assert(header);
  1594. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1595. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1596. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1597. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1598. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1599. tor_free(header);
  1600. tor_free(body);
  1601. status_vote_next_d_test(&header, &body, &body_used);
  1602. tt_assert(header);
  1603. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1604. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1605. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1606. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1607. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1608. done:
  1609. tor_free(header);
  1610. tor_free(body);
  1611. or_options_free(mock_options); mock_options = NULL;
  1612. clear_dir_servers();
  1613. dirvote_free_all();
  1614. }
  1615. static void
  1616. test_dir_handle_get_status_vote_next_not_found(void* data)
  1617. {
  1618. dir_connection_t *conn = NULL;
  1619. char *header = NULL;
  1620. (void) data;
  1621. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1622. conn = new_dir_conn();
  1623. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1624. GET("/tor/status-vote/next/" HEX1), NULL, 0));
  1625. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1626. NULL, NULL, 1, 0);
  1627. tt_assert(header);
  1628. tt_str_op(NOT_FOUND, OP_EQ, header);
  1629. done:
  1630. UNMOCK(connection_write_to_buf_impl_);
  1631. connection_free_(TO_CONN(conn));
  1632. tor_free(header);
  1633. }
  1634. static void
  1635. status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
  1636. {
  1637. dir_connection_t *conn = NULL;
  1638. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1639. conn = new_dir_conn();
  1640. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1641. GET("/tor/status-vote/next/consensus"), NULL, 0));
  1642. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1643. body, body_used, 18, 0);
  1644. done:
  1645. UNMOCK(connection_write_to_buf_impl_);
  1646. connection_free_(TO_CONN(conn));
  1647. }
  1648. static void
  1649. test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
  1650. {
  1651. char *header = NULL, *body = NULL;
  1652. size_t body_used;
  1653. (void) data;
  1654. status_vote_next_consensus_test(&header, &body, &body_used);
  1655. tt_assert(header);
  1656. tt_str_op(NOT_FOUND, OP_EQ, header);
  1657. done:
  1658. tor_free(header);
  1659. tor_free(body);
  1660. }
  1661. static void
  1662. test_dir_handle_get_status_vote_current_authority_not_found(void* data)
  1663. {
  1664. dir_connection_t *conn = NULL;
  1665. char *header = NULL;
  1666. (void) data;
  1667. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1668. conn = new_dir_conn();
  1669. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1670. GET("/tor/status-vote/current/authority"), NULL, 0));
  1671. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1672. NULL, NULL, 1, 0);
  1673. tt_assert(header);
  1674. tt_str_op(NOT_FOUND, OP_EQ, header);
  1675. done:
  1676. UNMOCK(connection_write_to_buf_impl_);
  1677. connection_free_(TO_CONN(conn));
  1678. tor_free(header);
  1679. }
  1680. static void
  1681. test_dir_handle_get_status_vote_next_authority_not_found(void* data)
  1682. {
  1683. dir_connection_t *conn = NULL;
  1684. char *header = NULL;
  1685. (void) data;
  1686. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1687. conn = new_dir_conn();
  1688. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1689. GET("/tor/status-vote/next/authority"), NULL, 0));
  1690. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1691. NULL, NULL, 1, 0);
  1692. tt_assert(header);
  1693. tt_str_op(NOT_FOUND, OP_EQ, header);
  1694. done:
  1695. UNMOCK(connection_write_to_buf_impl_);
  1696. connection_free_(TO_CONN(conn));
  1697. tor_free(header);
  1698. }
  1699. NS_DECL(const char*,
  1700. dirvote_get_pending_consensus, (consensus_flavor_t flav));
  1701. const char*
  1702. NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
  1703. {
  1704. (void)flav;
  1705. return "pending consensus";
  1706. }
  1707. static void
  1708. test_dir_handle_get_status_vote_next_consensus(void* data)
  1709. {
  1710. char *header = NULL, *body = NULL;
  1711. size_t body_used = 0;
  1712. (void) data;
  1713. NS_MOCK(dirvote_get_pending_consensus);
  1714. status_vote_next_consensus_test(&header, &body, &body_used);
  1715. tt_assert(header);
  1716. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1717. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1718. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1719. tt_assert(strstr(header, "Content-Length: 17\r\n"));
  1720. tt_str_op("pending consensus", OP_EQ, body);
  1721. done:
  1722. NS_UNMOCK(dirvote_get_pending_consensus);
  1723. tor_free(header);
  1724. tor_free(body);
  1725. }
  1726. static void
  1727. test_dir_handle_get_status_vote_next_consensus_busy(void* data)
  1728. {
  1729. char *header = NULL, *body = NULL;
  1730. size_t body_used = 0;
  1731. (void) data;
  1732. MOCK(get_options, mock_get_options);
  1733. NS_MOCK(dirvote_get_pending_consensus);
  1734. //Make it busy
  1735. init_mock_options();
  1736. mock_options->CountPrivateBandwidth = 1;
  1737. status_vote_next_consensus_test(&header, &body, &body_used);
  1738. tt_assert(header);
  1739. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1740. done:
  1741. NS_UNMOCK(dirvote_get_pending_consensus);
  1742. UNMOCK(get_options);
  1743. tor_free(header);
  1744. tor_free(body);
  1745. or_options_free(mock_options); mock_options = NULL;
  1746. }
  1747. static void
  1748. status_vote_next_consensus_signatures_test(char **header, char **body,
  1749. size_t *body_used)
  1750. {
  1751. dir_connection_t *conn = NULL;
  1752. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1753. conn = new_dir_conn();
  1754. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1755. GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
  1756. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1757. body, body_used, 22, 0);
  1758. done:
  1759. connection_free_(TO_CONN(conn));
  1760. UNMOCK(connection_write_to_buf_impl_);
  1761. }
  1762. static void
  1763. test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
  1764. {
  1765. char *header = NULL, *body = NULL;
  1766. size_t body_used;
  1767. (void) data;
  1768. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1769. tt_assert(header);
  1770. tt_str_op(NOT_FOUND, OP_EQ, header);
  1771. done:
  1772. tor_free(header);
  1773. tor_free(body);
  1774. }
  1775. NS_DECL(const char*,
  1776. dirvote_get_pending_detached_signatures, (void));
  1777. const char*
  1778. NS(dirvote_get_pending_detached_signatures)(void)
  1779. {
  1780. return "pending detached sigs";
  1781. }
  1782. static void
  1783. test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
  1784. {
  1785. char *header = NULL, *body = NULL;
  1786. size_t body_used = 0;
  1787. (void) data;
  1788. NS_MOCK(dirvote_get_pending_detached_signatures);
  1789. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1790. tt_assert(header);
  1791. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1792. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1793. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1794. tt_assert(strstr(header, "Content-Length: 21\r\n"));
  1795. tt_str_op("pending detached sigs", OP_EQ, body);
  1796. done:
  1797. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1798. tor_free(header);
  1799. tor_free(body);
  1800. }
  1801. static void
  1802. test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
  1803. {
  1804. char *header = NULL, *body = NULL;
  1805. size_t body_used;
  1806. (void) data;
  1807. NS_MOCK(dirvote_get_pending_detached_signatures);
  1808. MOCK(get_options, mock_get_options);
  1809. //Make it busy
  1810. init_mock_options();
  1811. mock_options->CountPrivateBandwidth = 1;
  1812. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1813. tt_assert(header);
  1814. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1815. done:
  1816. UNMOCK(get_options);
  1817. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1818. tor_free(header);
  1819. tor_free(body);
  1820. or_options_free(mock_options); mock_options = NULL;
  1821. }
  1822. static void
  1823. test_dir_handle_get_status_vote_next_authority(void* data)
  1824. {
  1825. dir_connection_t *conn = NULL;
  1826. char *header = NULL, *body = NULL;
  1827. const char *msg_out = NULL;
  1828. int status_out = 0;
  1829. size_t body_used = 0;
  1830. dir_server_t *ds = NULL;
  1831. const char digest[DIGEST_LEN] = "";
  1832. (void) data;
  1833. clear_dir_servers();
  1834. routerlist_free_all();
  1835. dirvote_free_all();
  1836. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1837. /* create a trusted ds */
  1838. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1839. NULL, V3_DIRINFO, 1.0);
  1840. tt_assert(ds);
  1841. dir_server_add(ds);
  1842. /* ds v3_identity_digest is the certificate's identity_key */
  1843. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1844. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1845. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1846. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1847. init_mock_options();
  1848. mock_options->AuthoritativeDir = 1;
  1849. mock_options->V3AuthoritativeDir = 1;
  1850. mock_options->TestingV3AuthVotingStartOffset = 0;
  1851. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1852. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1853. mock_options->TestingV3AuthInitialDistDelay = 1;
  1854. time_t now = 1441223455 -1;
  1855. dirvote_recalculate_timing(mock_options, now);
  1856. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1857. &status_out);
  1858. tt_assert(vote);
  1859. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1860. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1861. conn = new_dir_conn();
  1862. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1863. GET("/tor/status-vote/next/authority"), NULL, 0));
  1864. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1865. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1866. tt_assert(header);
  1867. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1868. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1869. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1870. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1871. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1872. done:
  1873. UNMOCK(connection_write_to_buf_impl_);
  1874. UNMOCK(get_my_v3_authority_cert);
  1875. connection_free_(TO_CONN(conn));
  1876. tor_free(header);
  1877. tor_free(body);
  1878. authority_cert_free(mock_cert); mock_cert = NULL;
  1879. or_options_free(mock_options); mock_options = NULL;
  1880. clear_dir_servers();
  1881. routerlist_free_all();
  1882. dirvote_free_all();
  1883. }
  1884. static void
  1885. test_dir_handle_get_status_vote_current_authority(void* data)
  1886. {
  1887. dir_connection_t *conn = NULL;
  1888. char *header = NULL, *body = NULL;
  1889. const char *msg_out = NULL;
  1890. int status_out = 0;
  1891. size_t body_used = 0;
  1892. const char digest[DIGEST_LEN] = "";
  1893. dir_server_t *ds = NULL;
  1894. (void) data;
  1895. clear_dir_servers();
  1896. routerlist_free_all();
  1897. dirvote_free_all();
  1898. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1899. /* create a trusted ds */
  1900. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1901. NULL, V3_DIRINFO, 1.0);
  1902. tt_assert(ds);
  1903. dir_server_add(ds);
  1904. /* ds v3_identity_digest is the certificate's identity_key */
  1905. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1906. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1907. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1908. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1909. init_mock_options();
  1910. mock_options->AuthoritativeDir = 1;
  1911. mock_options->V3AuthoritativeDir = 1;
  1912. mock_options->TestingV3AuthVotingStartOffset = 0;
  1913. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1914. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1915. mock_options->TestingV3AuthInitialDistDelay = 1;
  1916. time_t now = 1441223455;
  1917. dirvote_recalculate_timing(mock_options, now-1);
  1918. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1919. &status_out);
  1920. tt_assert(vote);
  1921. // move the pending vote to previous vote
  1922. dirvote_act(mock_options, now+1);
  1923. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1924. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1925. conn = new_dir_conn();
  1926. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1927. GET("/tor/status-vote/current/authority"), NULL, 0));
  1928. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1929. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1930. tt_assert(header);
  1931. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1932. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1933. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1934. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1935. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1936. done:
  1937. UNMOCK(connection_write_to_buf_impl_);
  1938. UNMOCK(get_my_v3_authority_cert);
  1939. connection_free_(TO_CONN(conn));
  1940. tor_free(header);
  1941. tor_free(body);
  1942. authority_cert_free(mock_cert); mock_cert = NULL;
  1943. or_options_free(mock_options); mock_options = NULL;
  1944. clear_dir_servers();
  1945. routerlist_free_all();
  1946. dirvote_free_all();
  1947. }
  1948. #define DIR_HANDLE_CMD(name,flags) \
  1949. { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
  1950. struct testcase_t dir_handle_get_tests[] = {
  1951. DIR_HANDLE_CMD(not_found, 0),
  1952. DIR_HANDLE_CMD(bad_request, 0),
  1953. DIR_HANDLE_CMD(v1_command_not_found, 0),
  1954. DIR_HANDLE_CMD(v1_command, 0),
  1955. DIR_HANDLE_CMD(robots_txt, 0),
  1956. DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
  1957. DIR_HANDLE_CMD(rendezvous2_not_found, 0),
  1958. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
  1959. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
  1960. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
  1961. DIR_HANDLE_CMD(micro_d_not_found, 0),
  1962. DIR_HANDLE_CMD(micro_d_server_busy, 0),
  1963. DIR_HANDLE_CMD(micro_d, 0),
  1964. DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
  1965. DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
  1966. DIR_HANDLE_CMD(networkstatus_bridges, 0),
  1967. DIR_HANDLE_CMD(server_descriptors_not_found, 0),
  1968. DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
  1969. DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
  1970. DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
  1971. DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
  1972. DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
  1973. DIR_HANDLE_CMD(server_keys_bad_req, 0),
  1974. DIR_HANDLE_CMD(server_keys_busy, 0),
  1975. DIR_HANDLE_CMD(server_keys_all_not_found, 0),
  1976. DIR_HANDLE_CMD(server_keys_all, 0),
  1977. DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
  1978. DIR_HANDLE_CMD(server_keys_authority, 0),
  1979. DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
  1980. DIR_HANDLE_CMD(server_keys_fp, 0),
  1981. DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
  1982. DIR_HANDLE_CMD(server_keys_sk, 0),
  1983. DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
  1984. DIR_HANDLE_CMD(server_keys_fpsk, 0),
  1985. DIR_HANDLE_CMD(status_vote_current_not_found, 0),
  1986. DIR_HANDLE_CMD(status_vote_next_not_found, 0),
  1987. DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
  1988. DIR_HANDLE_CMD(status_vote_current_authority, 0),
  1989. DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
  1990. DIR_HANDLE_CMD(status_vote_next_authority, 0),
  1991. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, 0),
  1992. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, 0),
  1993. DIR_HANDLE_CMD(status_vote_current_consensus_too_old, 0),
  1994. DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, 0),
  1995. DIR_HANDLE_CMD(status_vote_current_consensus_ns, 0),
  1996. DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
  1997. DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
  1998. DIR_HANDLE_CMD(status_vote_d, 0),
  1999. DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
  2000. DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
  2001. DIR_HANDLE_CMD(status_vote_next_consensus, 0),
  2002. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
  2003. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
  2004. DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
  2005. END_OF_TESTCASES
  2006. };