test_dir_handle_get.c 72 KB

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