test_dir_handle_get.c 77 KB

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