test_dir_handle_get.c 78 KB

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