test_dir_handle_get.c 77 KB

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