test_dir_handle_get.c 76 KB

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