test_dir_handle_get.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2019, 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/nodelist/networkstatus.h"
  32. #include "core/proto/proto_http.h"
  33. #include "lib/geoip/geoip.h"
  34. #include "feature/stats/geoip_stats.h"
  35. #include "feature/dircache/dirserv.h"
  36. #include "feature/dirauth/dirvote.h"
  37. #include "test/log_test_helpers.h"
  38. #include "feature/dircommon/voting_schedule.h"
  39. #include "feature/dircommon/dir_connection_st.h"
  40. #include "feature/dirclient/dir_server_st.h"
  41. #include "feature/nodelist/networkstatus_st.h"
  42. #include "feature/rend/rend_encoded_v2_service_descriptor_st.h"
  43. #include "feature/nodelist/routerinfo_st.h"
  44. #include "feature/nodelist/routerlist_st.h"
  45. #ifdef _WIN32
  46. /* For mkdir() */
  47. #include <direct.h>
  48. #else
  49. #include <dirent.h>
  50. #endif /* defined(_WIN32) */
  51. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  52. DISABLE_GCC_WARNING(overlength-strings)
  53. /* We allow huge string constants in the unit tests, but not in the code
  54. * at large. */
  55. #endif
  56. #include "vote_descriptors.inc"
  57. #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
  58. ENABLE_GCC_WARNING(overlength-strings)
  59. #endif
  60. #define NS_MODULE dir_handle_get
  61. #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
  62. #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
  63. #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
  64. #define TOO_OLD "HTTP/1.0 404 Consensus is too old\r\n\r\n"
  65. #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
  66. "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
  67. #define consdiffmgr_add_consensus consdiffmgr_add_consensus_nulterm
  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,
  1010. strlen(TEST_CERTIFICATE),
  1011. NULL);
  1012. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1013. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1014. conn = new_dir_conn();
  1015. const char *req = GET("/tor/keys/authority");
  1016. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1017. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1018. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1019. tt_assert(header);
  1020. tt_assert(body);
  1021. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1022. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1023. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1024. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1025. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1026. done:
  1027. UNMOCK(get_my_v3_authority_cert);
  1028. UNMOCK(connection_write_to_buf_impl_);
  1029. connection_free_minimal(TO_CONN(conn));
  1030. tor_free(header);
  1031. tor_free(body);
  1032. authority_cert_free(mock_cert); mock_cert = NULL;
  1033. }
  1034. static void
  1035. test_dir_handle_get_server_keys_fp_not_found(void* data)
  1036. {
  1037. dir_connection_t *conn = NULL;
  1038. char *header = NULL;
  1039. (void) data;
  1040. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1041. conn = new_dir_conn();
  1042. const char *req = GET("/tor/keys/fp/somehex");
  1043. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1044. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1045. NULL, NULL, 1, 0);
  1046. tt_assert(header);
  1047. tt_str_op(NOT_FOUND, OP_EQ, header);
  1048. done:
  1049. UNMOCK(connection_write_to_buf_impl_);
  1050. connection_free_minimal(TO_CONN(conn));
  1051. tor_free(header);
  1052. }
  1053. static void
  1054. test_dir_handle_get_server_keys_fp(void* data)
  1055. {
  1056. dir_connection_t *conn = NULL;
  1057. char *header = NULL;
  1058. char *body = NULL;
  1059. size_t body_used = 0;
  1060. dir_server_t *ds = NULL;
  1061. const char digest[DIGEST_LEN] = "";
  1062. (void) data;
  1063. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1064. clear_dir_servers();
  1065. routerlist_free_all();
  1066. /* create a trusted ds */
  1067. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1068. NULL, V3_DIRINFO, 1.0);
  1069. tt_assert(ds);
  1070. dir_server_add(ds);
  1071. /* ds v3_identity_digest is the certificate's identity_key */
  1072. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1073. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1074. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1075. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1076. conn = new_dir_conn();
  1077. char req[71];
  1078. tor_snprintf(req, sizeof(req),
  1079. GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1080. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1081. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1082. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1083. tt_assert(header);
  1084. tt_assert(body);
  1085. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1086. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1087. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1088. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1089. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1090. done:
  1091. UNMOCK(connection_write_to_buf_impl_);
  1092. connection_free_minimal(TO_CONN(conn));
  1093. tor_free(header);
  1094. tor_free(body);
  1095. clear_dir_servers();
  1096. routerlist_free_all();
  1097. }
  1098. static void
  1099. test_dir_handle_get_server_keys_sk_not_found(void* data)
  1100. {
  1101. dir_connection_t *conn = NULL;
  1102. char *header = NULL;
  1103. (void) data;
  1104. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1105. conn = new_dir_conn();
  1106. const char *req = GET("/tor/keys/sk/somehex");
  1107. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1108. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1109. NULL, NULL, 1, 0);
  1110. tt_assert(header);
  1111. tt_str_op(NOT_FOUND, OP_EQ, header);
  1112. done:
  1113. UNMOCK(connection_write_to_buf_impl_);
  1114. connection_free_minimal(TO_CONN(conn));
  1115. tor_free(header);
  1116. }
  1117. static void
  1118. test_dir_handle_get_server_keys_sk(void* data)
  1119. {
  1120. dir_connection_t *conn = NULL;
  1121. char *header = NULL;
  1122. char *body = NULL;
  1123. size_t body_used = 0;
  1124. (void) data;
  1125. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1126. strlen(TEST_CERTIFICATE),
  1127. NULL);
  1128. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1129. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1130. clear_dir_servers();
  1131. routerlist_free_all();
  1132. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1133. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1134. conn = new_dir_conn();
  1135. char req[71];
  1136. tor_snprintf(req, sizeof(req),
  1137. GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
  1138. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1139. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1140. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1141. tt_assert(header);
  1142. tt_assert(body);
  1143. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1144. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1145. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1146. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1147. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1148. done:
  1149. UNMOCK(get_my_v3_authority_cert);
  1150. UNMOCK(connection_write_to_buf_impl_);
  1151. connection_free_minimal(TO_CONN(conn));
  1152. authority_cert_free(mock_cert); mock_cert = NULL;
  1153. tor_free(header);
  1154. tor_free(body);
  1155. }
  1156. static void
  1157. test_dir_handle_get_server_keys_fpsk_not_found(void* data)
  1158. {
  1159. dir_connection_t *conn = NULL;
  1160. char *header = NULL;
  1161. (void) data;
  1162. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1163. conn = new_dir_conn();
  1164. const char *req = GET("/tor/keys/fp-sk/somehex");
  1165. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1166. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1167. NULL, NULL, 1, 0);
  1168. tt_assert(header);
  1169. tt_str_op(NOT_FOUND, OP_EQ, header);
  1170. done:
  1171. UNMOCK(connection_write_to_buf_impl_);
  1172. connection_free_minimal(TO_CONN(conn));
  1173. tor_free(header);
  1174. }
  1175. static void
  1176. test_dir_handle_get_server_keys_fpsk(void* data)
  1177. {
  1178. dir_connection_t *conn = NULL;
  1179. char *header = NULL;
  1180. char *body = NULL;
  1181. size_t body_used = 0;
  1182. dir_server_t *ds = NULL;
  1183. const char digest[DIGEST_LEN] = "";
  1184. (void) data;
  1185. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1186. clear_dir_servers();
  1187. routerlist_free_all();
  1188. /* create a trusted ds */
  1189. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1190. NULL, V3_DIRINFO, 1.0);
  1191. tt_assert(ds);
  1192. /* ds v3_identity_digest is the certificate's identity_key */
  1193. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1194. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1195. dir_server_add(ds);
  1196. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1197. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1198. conn = new_dir_conn();
  1199. char req[115];
  1200. tor_snprintf(req, sizeof(req),
  1201. GET("/tor/keys/fp-sk/%s-%s"),
  1202. TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
  1203. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1204. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1205. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1206. tt_assert(header);
  1207. tt_assert(body);
  1208. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1209. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1210. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1211. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1212. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1213. done:
  1214. UNMOCK(connection_write_to_buf_impl_);
  1215. connection_free_minimal(TO_CONN(conn));
  1216. tor_free(header);
  1217. tor_free(body);
  1218. clear_dir_servers();
  1219. routerlist_free_all();
  1220. }
  1221. static void
  1222. test_dir_handle_get_server_keys_busy(void* data)
  1223. {
  1224. dir_connection_t *conn = NULL;
  1225. char *header = NULL;
  1226. dir_server_t *ds = NULL;
  1227. const char digest[DIGEST_LEN] = "";
  1228. (void) data;
  1229. clear_dir_servers();
  1230. routerlist_free_all();
  1231. /* create a trusted ds */
  1232. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1233. NULL, V3_DIRINFO, 1.0);
  1234. tt_assert(ds);
  1235. /* ds v3_identity_digest is the certificate's identity_key */
  1236. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1237. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1238. dir_server_add(ds);
  1239. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1240. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1241. MOCK(get_options, mock_get_options);
  1242. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1243. /* setup busy server */
  1244. init_mock_options();
  1245. mock_options->CountPrivateBandwidth = 1;
  1246. conn = new_dir_conn();
  1247. char req[71];
  1248. tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1249. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1250. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1251. NULL, NULL, 1, 0);
  1252. tt_assert(header);
  1253. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1254. done:
  1255. UNMOCK(get_options);
  1256. UNMOCK(connection_write_to_buf_impl_);
  1257. connection_free_minimal(TO_CONN(conn));
  1258. tor_free(header);
  1259. or_options_free(mock_options); mock_options = NULL;
  1260. clear_dir_servers();
  1261. routerlist_free_all();
  1262. }
  1263. static networkstatus_t *mock_ns_val = NULL;
  1264. static networkstatus_t *
  1265. mock_ns_get_by_flavor(consensus_flavor_t f)
  1266. {
  1267. (void)f;
  1268. return mock_ns_val;
  1269. }
  1270. static void
  1271. test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
  1272. {
  1273. dir_connection_t *conn = NULL;
  1274. char *header = NULL;
  1275. char *stats = NULL;
  1276. (void) d;
  1277. /* init mock */
  1278. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1279. mock_ns_val->flavor = FLAV_NS;
  1280. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1281. mock_ns_val->voters = smartlist_new();
  1282. mock_ns_val->valid_after = time(NULL) - 1800;
  1283. mock_ns_val->valid_until = time(NULL) - 60;
  1284. #define NETWORK_STATUS "some network status string"
  1285. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1286. /* init mock */
  1287. init_mock_options();
  1288. MOCK(get_options, mock_get_options);
  1289. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1290. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1291. /* start gathering stats */
  1292. mock_options->DirReqStatistics = 1;
  1293. geoip_dirreq_stats_init(time(NULL));
  1294. conn = new_dir_conn();
  1295. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1296. GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
  1297. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1298. NULL, NULL, 1, 0);
  1299. tt_assert(header);
  1300. tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
  1301. stats = geoip_format_dirreq_stats(time(NULL));
  1302. tt_assert(stats);
  1303. tt_assert(strstr(stats, "not-enough-sigs=8"));
  1304. done:
  1305. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1306. UNMOCK(connection_write_to_buf_impl_);
  1307. UNMOCK(get_options);
  1308. connection_free_minimal(TO_CONN(conn));
  1309. tor_free(header);
  1310. tor_free(stats);
  1311. smartlist_free(mock_ns_val->voters);
  1312. tor_free(mock_ns_val);
  1313. or_options_free(mock_options); mock_options = NULL;
  1314. }
  1315. static void
  1316. test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
  1317. {
  1318. dir_connection_t *conn = NULL;
  1319. char *header = NULL;
  1320. char *stats = NULL;
  1321. (void) data;
  1322. init_mock_options();
  1323. MOCK(get_options, mock_get_options);
  1324. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1325. /* start gathering stats */
  1326. mock_options->DirReqStatistics = 1;
  1327. geoip_dirreq_stats_init(time(NULL));
  1328. conn = new_dir_conn();
  1329. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1330. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1331. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1332. NULL, NULL, 1, 0);
  1333. tt_assert(header);
  1334. tt_str_op(NOT_FOUND, OP_EQ, header);
  1335. stats = geoip_format_dirreq_stats(time(NULL));
  1336. tt_assert(stats);
  1337. tt_assert(strstr(stats, "not-found=8"));
  1338. done:
  1339. UNMOCK(connection_write_to_buf_impl_);
  1340. UNMOCK(get_options);
  1341. connection_free_minimal(TO_CONN(conn));
  1342. tor_free(header);
  1343. tor_free(stats);
  1344. or_options_free(mock_options); mock_options = NULL;
  1345. }
  1346. static void
  1347. test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
  1348. {
  1349. dir_connection_t *conn = NULL;
  1350. char *header = NULL;
  1351. (void)data;
  1352. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1353. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1354. mock_ns_val->flavor = FLAV_MICRODESC;
  1355. mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
  1356. mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
  1357. mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
  1358. #define NETWORK_STATUS "some network status string"
  1359. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1360. init_mock_options();
  1361. MOCK(get_options, mock_get_options);
  1362. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1363. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1364. conn = new_dir_conn();
  1365. setup_capture_of_logs(LOG_WARN);
  1366. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1367. GET("/tor/status-vote/current/consensus-microdesc"), NULL, 0));
  1368. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1369. NULL, NULL, 1, 0);
  1370. tt_assert(header);
  1371. tt_str_op(TOO_OLD, OP_EQ, header);
  1372. expect_log_msg_containing("too old");
  1373. tor_free(header);
  1374. teardown_capture_of_logs();
  1375. tor_free(mock_ns_val);
  1376. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1377. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1378. mock_ns_val->flavor = FLAV_NS;
  1379. mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
  1380. mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
  1381. mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
  1382. #define NETWORK_STATUS "some network status string"
  1383. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1384. setup_capture_of_logs(LOG_WARN);
  1385. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1386. GET("/tor/status-vote/current/consensus"), NULL, 0));
  1387. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1388. NULL, NULL, 1, 0);
  1389. tt_assert(header);
  1390. tt_str_op(TOO_OLD, OP_EQ, header);
  1391. expect_no_log_entry();
  1392. done:
  1393. teardown_capture_of_logs();
  1394. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1395. UNMOCK(connection_write_to_buf_impl_);
  1396. UNMOCK(get_options);
  1397. connection_free_minimal(TO_CONN(conn));
  1398. tor_free(header);
  1399. tor_free(mock_ns_val);
  1400. or_options_free(mock_options); mock_options = NULL;
  1401. }
  1402. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  1403. int
  1404. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  1405. {
  1406. (void)addr;
  1407. CALLED(geoip_get_country_by_addr)++;
  1408. return 1;
  1409. }
  1410. static void
  1411. status_vote_current_consensus_ns_test(char **header, char **body,
  1412. size_t *body_len)
  1413. {
  1414. dir_connection_t *conn = NULL;
  1415. #define NETWORK_STATUS "some network status string"
  1416. #if 0
  1417. common_digests_t digests;
  1418. uint8_t sha3[DIGEST256_LEN];
  1419. memset(&digests, 0x60, sizeof(digests));
  1420. memset(sha3, 0x06, sizeof(sha3));
  1421. dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
  1422. sha3,
  1423. time(NULL));
  1424. #endif /* 0 */
  1425. networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
  1426. ns->type = NS_TYPE_CONSENSUS;
  1427. ns->flavor = FLAV_NS;
  1428. ns->valid_after = time(NULL) - 1800;
  1429. ns->fresh_until = time(NULL) - 900;
  1430. ns->valid_until = time(NULL) - 60;
  1431. consdiffmgr_add_consensus(NETWORK_STATUS, ns);
  1432. networkstatus_vote_free(ns);
  1433. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1434. tt_assert(mock_options);
  1435. mock_options->DirReqStatistics = 1;
  1436. geoip_dirreq_stats_init(time(NULL));
  1437. /* init geoip database */
  1438. geoip_parse_entry("10,50,AB", AF_INET);
  1439. tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
  1440. conn = new_dir_conn();
  1441. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1442. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1443. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1444. body, body_len, strlen(NETWORK_STATUS)+7, 0);
  1445. done:
  1446. UNMOCK(connection_write_to_buf_impl_);
  1447. connection_free_minimal(TO_CONN(conn));
  1448. }
  1449. static void
  1450. test_dir_handle_get_status_vote_current_consensus_ns(void* data)
  1451. {
  1452. char *header = NULL;
  1453. char *body = NULL, *comp_body = NULL;
  1454. size_t body_used = 0, comp_body_used = 0;
  1455. char *stats = NULL, *hist = NULL;
  1456. (void) data;
  1457. dirserv_free_all();
  1458. clear_geoip_db();
  1459. NS_MOCK(geoip_get_country_by_addr);
  1460. MOCK(get_options, mock_get_options);
  1461. init_mock_options();
  1462. status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
  1463. tt_assert(header);
  1464. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1465. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1466. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1467. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  1468. compress_method_t compression = detect_compression_method(comp_body,
  1469. comp_body_used);
  1470. tt_int_op(ZLIB_METHOD, OP_EQ, compression);
  1471. tor_uncompress(&body, &body_used, comp_body, comp_body_used,
  1472. compression, 0, LOG_PROTOCOL_WARN);
  1473. tt_str_op(NETWORK_STATUS, OP_EQ, body);
  1474. tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
  1475. stats = geoip_format_dirreq_stats(time(NULL));
  1476. tt_assert(stats);
  1477. tt_assert(strstr(stats, "ok=8"));
  1478. tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
  1479. tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
  1480. tt_assert(strstr(stats, "dirreq-v3-direct-dl"
  1481. " complete=0,timeout=0,running=4"));
  1482. hist = geoip_get_request_history();
  1483. tt_assert(hist);
  1484. tt_str_op("ab=8", OP_EQ, hist);
  1485. done:
  1486. NS_UNMOCK(geoip_get_country_by_addr);
  1487. UNMOCK(get_options);
  1488. tor_free(header);
  1489. tor_free(comp_body);
  1490. tor_free(body);
  1491. tor_free(stats);
  1492. tor_free(hist);
  1493. or_options_free(mock_options); mock_options = NULL;
  1494. dirserv_free_all();
  1495. clear_geoip_db();
  1496. }
  1497. static void
  1498. test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
  1499. {
  1500. char *header = NULL;
  1501. char *body = NULL;
  1502. size_t body_used = 0;
  1503. char *stats = NULL;
  1504. (void) data;
  1505. dirserv_free_all();
  1506. clear_geoip_db();
  1507. MOCK(get_options, mock_get_options);
  1508. // Make it busy
  1509. init_mock_options();
  1510. mock_options->CountPrivateBandwidth = 1;
  1511. status_vote_current_consensus_ns_test(&header, &body, &body_used);
  1512. tt_assert(header);
  1513. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1514. stats = geoip_format_dirreq_stats(time(NULL));
  1515. tt_assert(stats);
  1516. tt_assert(strstr(stats, "busy=8"));
  1517. done:
  1518. UNMOCK(get_options);
  1519. tor_free(header);
  1520. tor_free(body);
  1521. or_options_free(mock_options); mock_options = NULL;
  1522. tor_free(stats);
  1523. dirserv_free_all();
  1524. clear_geoip_db();
  1525. }
  1526. static void
  1527. test_dir_handle_get_status_vote_current_not_found(void* data)
  1528. {
  1529. dir_connection_t *conn = NULL;
  1530. char *header = NULL;
  1531. (void) data;
  1532. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1533. conn = new_dir_conn();
  1534. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1535. GET("/tor/status-vote/current/" HEX1), NULL, 0));
  1536. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1537. NULL, NULL, 1, 0);
  1538. tt_assert(header);
  1539. tt_str_op(NOT_FOUND, OP_EQ, header);
  1540. done:
  1541. UNMOCK(connection_write_to_buf_impl_);
  1542. connection_free_minimal(TO_CONN(conn));
  1543. tor_free(header);
  1544. }
  1545. #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
  1546. static void
  1547. status_vote_current_d_test(char **header, char **body, size_t *body_l)
  1548. {
  1549. dir_connection_t *conn = NULL;
  1550. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1551. conn = new_dir_conn();
  1552. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1553. GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
  1554. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1555. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1556. tt_assert(header);
  1557. done:
  1558. UNMOCK(connection_write_to_buf_impl_);
  1559. connection_free_minimal(TO_CONN(conn));
  1560. }
  1561. static void
  1562. status_vote_next_d_test(char **header, char **body, size_t *body_l)
  1563. {
  1564. dir_connection_t *conn = NULL;
  1565. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1566. conn = new_dir_conn();
  1567. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1568. GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
  1569. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1570. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1571. tt_assert(header);
  1572. done:
  1573. UNMOCK(connection_write_to_buf_impl_);
  1574. connection_free_minimal(TO_CONN(conn));
  1575. }
  1576. static void
  1577. test_dir_handle_get_status_vote_current_d_not_found(void* data)
  1578. {
  1579. char *header = NULL;
  1580. (void) data;
  1581. status_vote_current_d_test(&header, NULL, NULL);
  1582. tt_assert(header);
  1583. tt_str_op(NOT_FOUND, OP_EQ, header);
  1584. done:
  1585. tor_free(header);
  1586. }
  1587. static void
  1588. test_dir_handle_get_status_vote_next_d_not_found(void* data)
  1589. {
  1590. char *header = NULL;
  1591. (void) data;
  1592. status_vote_next_d_test(&header, NULL, NULL);
  1593. tt_assert(header);
  1594. tt_str_op(NOT_FOUND, OP_EQ, header);
  1595. done:
  1596. UNMOCK(connection_write_to_buf_impl_);
  1597. tor_free(header);
  1598. }
  1599. static void
  1600. test_dir_handle_get_status_vote_d(void* data)
  1601. {
  1602. char *header = NULL, *body = NULL;
  1603. size_t body_used = 0;
  1604. dir_server_t *ds = NULL;
  1605. const char digest[DIGEST_LEN] = "";
  1606. (void) data;
  1607. clear_dir_servers();
  1608. dirvote_free_all();
  1609. /* create a trusted ds */
  1610. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1611. NULL, V3_DIRINFO, 1.0);
  1612. tt_assert(ds);
  1613. dir_server_add(ds);
  1614. /* ds v3_identity_digest is the certificate's identity_key */
  1615. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1616. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1617. init_mock_options();
  1618. mock_options->AuthoritativeDir = 1;
  1619. mock_options->V3AuthoritativeDir = 1;
  1620. mock_options->TestingV3AuthVotingStartOffset = 0;
  1621. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1622. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1623. mock_options->TestingV3AuthInitialDistDelay = 1;
  1624. time_t now = 1441223455 -1;
  1625. voting_schedule_recalculate_timing(mock_options, now);
  1626. const char *msg_out = NULL;
  1627. int status_out = 0;
  1628. struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1629. &status_out);
  1630. tt_assert(pv);
  1631. status_vote_current_d_test(&header, &body, &body_used);
  1632. tt_assert(header);
  1633. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1634. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1635. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1636. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1637. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1638. tor_free(header);
  1639. tor_free(body);
  1640. status_vote_next_d_test(&header, &body, &body_used);
  1641. tt_assert(header);
  1642. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1643. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1644. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1645. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1646. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1647. done:
  1648. tor_free(header);
  1649. tor_free(body);
  1650. or_options_free(mock_options); mock_options = NULL;
  1651. clear_dir_servers();
  1652. dirvote_free_all();
  1653. routerlist_free_all();
  1654. }
  1655. static void
  1656. test_dir_handle_get_status_vote_next_not_found(void* data)
  1657. {
  1658. dir_connection_t *conn = NULL;
  1659. char *header = NULL;
  1660. (void) data;
  1661. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1662. conn = new_dir_conn();
  1663. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1664. GET("/tor/status-vote/next/" HEX1), NULL, 0));
  1665. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1666. NULL, NULL, 1, 0);
  1667. tt_assert(header);
  1668. tt_str_op(NOT_FOUND, OP_EQ, header);
  1669. done:
  1670. UNMOCK(connection_write_to_buf_impl_);
  1671. connection_free_minimal(TO_CONN(conn));
  1672. tor_free(header);
  1673. }
  1674. static void
  1675. status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
  1676. {
  1677. dir_connection_t *conn = NULL;
  1678. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1679. conn = new_dir_conn();
  1680. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1681. GET("/tor/status-vote/next/consensus"), NULL, 0));
  1682. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1683. body, body_used, 18, 0);
  1684. done:
  1685. UNMOCK(connection_write_to_buf_impl_);
  1686. connection_free_minimal(TO_CONN(conn));
  1687. }
  1688. static void
  1689. test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
  1690. {
  1691. char *header = NULL, *body = NULL;
  1692. size_t body_used;
  1693. (void) data;
  1694. status_vote_next_consensus_test(&header, &body, &body_used);
  1695. tt_assert(header);
  1696. tt_str_op(NOT_FOUND, OP_EQ, header);
  1697. done:
  1698. tor_free(header);
  1699. tor_free(body);
  1700. }
  1701. static void
  1702. test_dir_handle_get_status_vote_current_authority_not_found(void* data)
  1703. {
  1704. dir_connection_t *conn = NULL;
  1705. char *header = NULL;
  1706. (void) data;
  1707. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1708. conn = new_dir_conn();
  1709. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1710. GET("/tor/status-vote/current/authority"), NULL, 0));
  1711. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1712. NULL, NULL, 1, 0);
  1713. tt_assert(header);
  1714. tt_str_op(NOT_FOUND, OP_EQ, header);
  1715. done:
  1716. UNMOCK(connection_write_to_buf_impl_);
  1717. connection_free_minimal(TO_CONN(conn));
  1718. tor_free(header);
  1719. }
  1720. static void
  1721. test_dir_handle_get_status_vote_next_authority_not_found(void* data)
  1722. {
  1723. dir_connection_t *conn = NULL;
  1724. char *header = NULL;
  1725. (void) data;
  1726. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1727. conn = new_dir_conn();
  1728. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1729. GET("/tor/status-vote/next/authority"), NULL, 0));
  1730. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1731. NULL, NULL, 1, 0);
  1732. tt_assert(header);
  1733. tt_str_op(NOT_FOUND, OP_EQ, header);
  1734. done:
  1735. UNMOCK(connection_write_to_buf_impl_);
  1736. connection_free_minimal(TO_CONN(conn));
  1737. tor_free(header);
  1738. }
  1739. NS_DECL(const char*,
  1740. dirvote_get_pending_consensus, (consensus_flavor_t flav));
  1741. const char*
  1742. NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
  1743. {
  1744. (void)flav;
  1745. return "pending consensus";
  1746. }
  1747. static void
  1748. test_dir_handle_get_status_vote_next_consensus(void* data)
  1749. {
  1750. char *header = NULL, *body = NULL;
  1751. size_t body_used = 0;
  1752. (void) data;
  1753. NS_MOCK(dirvote_get_pending_consensus);
  1754. status_vote_next_consensus_test(&header, &body, &body_used);
  1755. tt_assert(header);
  1756. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1757. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1758. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1759. tt_assert(strstr(header, "Content-Length: 17\r\n"));
  1760. tt_str_op("pending consensus", OP_EQ, body);
  1761. done:
  1762. NS_UNMOCK(dirvote_get_pending_consensus);
  1763. tor_free(header);
  1764. tor_free(body);
  1765. }
  1766. static void
  1767. test_dir_handle_get_status_vote_next_consensus_busy(void* data)
  1768. {
  1769. char *header = NULL, *body = NULL;
  1770. size_t body_used = 0;
  1771. (void) data;
  1772. MOCK(get_options, mock_get_options);
  1773. NS_MOCK(dirvote_get_pending_consensus);
  1774. //Make it busy
  1775. init_mock_options();
  1776. mock_options->CountPrivateBandwidth = 1;
  1777. status_vote_next_consensus_test(&header, &body, &body_used);
  1778. tt_assert(header);
  1779. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1780. done:
  1781. NS_UNMOCK(dirvote_get_pending_consensus);
  1782. UNMOCK(get_options);
  1783. tor_free(header);
  1784. tor_free(body);
  1785. or_options_free(mock_options); mock_options = NULL;
  1786. }
  1787. static void
  1788. status_vote_next_consensus_signatures_test(char **header, char **body,
  1789. size_t *body_used)
  1790. {
  1791. dir_connection_t *conn = NULL;
  1792. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1793. conn = new_dir_conn();
  1794. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1795. GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
  1796. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1797. body, body_used, 22, 0);
  1798. done:
  1799. connection_free_minimal(TO_CONN(conn));
  1800. UNMOCK(connection_write_to_buf_impl_);
  1801. }
  1802. static void
  1803. test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
  1804. {
  1805. char *header = NULL, *body = NULL;
  1806. size_t body_used;
  1807. (void) data;
  1808. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1809. tt_assert(header);
  1810. tt_str_op(NOT_FOUND, OP_EQ, header);
  1811. done:
  1812. tor_free(header);
  1813. tor_free(body);
  1814. }
  1815. NS_DECL(const char*,
  1816. dirvote_get_pending_detached_signatures, (void));
  1817. const char*
  1818. NS(dirvote_get_pending_detached_signatures)(void)
  1819. {
  1820. return "pending detached sigs";
  1821. }
  1822. static void
  1823. test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
  1824. {
  1825. char *header = NULL, *body = NULL;
  1826. size_t body_used = 0;
  1827. (void) data;
  1828. NS_MOCK(dirvote_get_pending_detached_signatures);
  1829. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1830. tt_assert(header);
  1831. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1832. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1833. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1834. tt_assert(strstr(header, "Content-Length: 21\r\n"));
  1835. tt_str_op("pending detached sigs", OP_EQ, body);
  1836. done:
  1837. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1838. tor_free(header);
  1839. tor_free(body);
  1840. }
  1841. static void
  1842. test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
  1843. {
  1844. char *header = NULL, *body = NULL;
  1845. size_t body_used;
  1846. (void) data;
  1847. NS_MOCK(dirvote_get_pending_detached_signatures);
  1848. MOCK(get_options, mock_get_options);
  1849. //Make it busy
  1850. init_mock_options();
  1851. mock_options->CountPrivateBandwidth = 1;
  1852. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1853. tt_assert(header);
  1854. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1855. done:
  1856. UNMOCK(get_options);
  1857. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1858. tor_free(header);
  1859. tor_free(body);
  1860. or_options_free(mock_options); mock_options = NULL;
  1861. }
  1862. static void
  1863. test_dir_handle_get_status_vote_next_authority(void* data)
  1864. {
  1865. dir_connection_t *conn = NULL;
  1866. char *header = NULL, *body = NULL;
  1867. const char *msg_out = NULL;
  1868. int status_out = 0;
  1869. size_t body_used = 0;
  1870. dir_server_t *ds = NULL;
  1871. const char digest[DIGEST_LEN] = "";
  1872. (void) data;
  1873. clear_dir_servers();
  1874. routerlist_free_all();
  1875. dirvote_free_all();
  1876. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1877. strlen(TEST_CERTIFICATE),
  1878. NULL);
  1879. /* create a trusted ds */
  1880. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1881. NULL, V3_DIRINFO, 1.0);
  1882. tt_assert(ds);
  1883. dir_server_add(ds);
  1884. /* ds v3_identity_digest is the certificate's identity_key */
  1885. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1886. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1887. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1888. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1889. init_mock_options();
  1890. mock_options->AuthoritativeDir = 1;
  1891. mock_options->V3AuthoritativeDir = 1;
  1892. mock_options->TestingV3AuthVotingStartOffset = 0;
  1893. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1894. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1895. mock_options->TestingV3AuthInitialDistDelay = 1;
  1896. time_t now = 1441223455 -1;
  1897. voting_schedule_recalculate_timing(mock_options, now);
  1898. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1899. &status_out);
  1900. tt_assert(vote);
  1901. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1902. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1903. conn = new_dir_conn();
  1904. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1905. GET("/tor/status-vote/next/authority"), NULL, 0));
  1906. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1907. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1908. tt_assert(header);
  1909. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1910. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1911. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1912. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1913. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1914. done:
  1915. UNMOCK(connection_write_to_buf_impl_);
  1916. UNMOCK(get_my_v3_authority_cert);
  1917. connection_free_minimal(TO_CONN(conn));
  1918. tor_free(header);
  1919. tor_free(body);
  1920. authority_cert_free(mock_cert); mock_cert = NULL;
  1921. or_options_free(mock_options); mock_options = NULL;
  1922. clear_dir_servers();
  1923. routerlist_free_all();
  1924. dirvote_free_all();
  1925. }
  1926. static void
  1927. test_dir_handle_get_status_vote_current_authority(void* data)
  1928. {
  1929. dir_connection_t *conn = NULL;
  1930. char *header = NULL, *body = NULL;
  1931. const char *msg_out = NULL;
  1932. int status_out = 0;
  1933. size_t body_used = 0;
  1934. const char digest[DIGEST_LEN] = "";
  1935. dir_server_t *ds = NULL;
  1936. (void) data;
  1937. clear_dir_servers();
  1938. routerlist_free_all();
  1939. dirvote_free_all();
  1940. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1941. strlen(TEST_CERTIFICATE),
  1942. NULL);
  1943. /* create a trusted ds */
  1944. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1945. NULL, V3_DIRINFO, 1.0);
  1946. tt_assert(ds);
  1947. dir_server_add(ds);
  1948. /* ds v3_identity_digest is the certificate's identity_key */
  1949. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1950. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1951. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1952. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1953. init_mock_options();
  1954. mock_options->AuthoritativeDir = 1;
  1955. mock_options->V3AuthoritativeDir = 1;
  1956. mock_options->TestingV3AuthVotingStartOffset = 0;
  1957. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1958. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1959. mock_options->TestingV3AuthInitialDistDelay = 1;
  1960. time_t now = 1441223455;
  1961. voting_schedule_recalculate_timing(mock_options, now-1);
  1962. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1963. &status_out);
  1964. tt_assert(vote);
  1965. // move the pending vote to previous vote
  1966. dirvote_act(mock_options, now+1);
  1967. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1968. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1969. conn = new_dir_conn();
  1970. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1971. GET("/tor/status-vote/current/authority"), NULL, 0));
  1972. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1973. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1974. tt_assert(header);
  1975. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1976. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1977. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1978. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1979. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1980. done:
  1981. UNMOCK(connection_write_to_buf_impl_);
  1982. UNMOCK(get_my_v3_authority_cert);
  1983. connection_free_minimal(TO_CONN(conn));
  1984. tor_free(header);
  1985. tor_free(body);
  1986. authority_cert_free(mock_cert); mock_cert = NULL;
  1987. or_options_free(mock_options); mock_options = NULL;
  1988. clear_dir_servers();
  1989. routerlist_free_all();
  1990. dirvote_free_all();
  1991. }
  1992. static void
  1993. test_dir_handle_get_parse_accept_encoding(void *arg)
  1994. {
  1995. (void)arg;
  1996. const unsigned B_NONE = 1u << NO_METHOD;
  1997. const unsigned B_ZLIB = 1u << ZLIB_METHOD;
  1998. const unsigned B_GZIP = 1u << GZIP_METHOD;
  1999. const unsigned B_LZMA = 1u << LZMA_METHOD;
  2000. const unsigned B_ZSTD = 1u << ZSTD_METHOD;
  2001. unsigned encodings;
  2002. encodings = parse_accept_encoding_header("");
  2003. tt_uint_op(B_NONE, OP_EQ, encodings);
  2004. encodings = parse_accept_encoding_header(" ");
  2005. tt_uint_op(B_NONE, OP_EQ, encodings);
  2006. encodings = parse_accept_encoding_header("dewey, cheatham, and howe ");
  2007. tt_uint_op(B_NONE, OP_EQ, encodings);
  2008. encodings = parse_accept_encoding_header("dewey, cheatham, and gzip");
  2009. tt_uint_op(B_NONE, OP_EQ, encodings);
  2010. encodings = parse_accept_encoding_header("dewey, cheatham, and, gzip");
  2011. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2012. encodings = parse_accept_encoding_header(" gzip");
  2013. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2014. encodings = parse_accept_encoding_header("gzip");
  2015. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2016. encodings = parse_accept_encoding_header("x-zstd, deflate, x-tor-lzma");
  2017. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA, OP_EQ, encodings);
  2018. encodings = parse_accept_encoding_header(
  2019. "x-zstd, deflate, x-tor-lzma, gzip");
  2020. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
  2021. encodings = parse_accept_encoding_header("x-zstd,deflate,x-tor-lzma,gzip");
  2022. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
  2023. done:
  2024. ;
  2025. }
  2026. #define DIR_HANDLE_CMD(name,flags) \
  2027. { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
  2028. struct testcase_t dir_handle_get_tests[] = {
  2029. DIR_HANDLE_CMD(not_found, 0),
  2030. DIR_HANDLE_CMD(bad_request, 0),
  2031. DIR_HANDLE_CMD(v1_command_not_found, 0),
  2032. DIR_HANDLE_CMD(v1_command, 0),
  2033. DIR_HANDLE_CMD(robots_txt, 0),
  2034. DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
  2035. DIR_HANDLE_CMD(rendezvous2_not_found, 0),
  2036. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
  2037. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
  2038. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
  2039. DIR_HANDLE_CMD(micro_d_not_found, 0),
  2040. DIR_HANDLE_CMD(micro_d_server_busy, 0),
  2041. DIR_HANDLE_CMD(micro_d, 0),
  2042. DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
  2043. DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
  2044. DIR_HANDLE_CMD(networkstatus_bridges, 0),
  2045. DIR_HANDLE_CMD(server_descriptors_not_found, 0),
  2046. DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
  2047. DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
  2048. DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
  2049. DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
  2050. DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
  2051. DIR_HANDLE_CMD(server_keys_bad_req, 0),
  2052. DIR_HANDLE_CMD(server_keys_busy, 0),
  2053. DIR_HANDLE_CMD(server_keys_all_not_found, 0),
  2054. DIR_HANDLE_CMD(server_keys_all, 0),
  2055. DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
  2056. DIR_HANDLE_CMD(server_keys_authority, 0),
  2057. DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
  2058. DIR_HANDLE_CMD(server_keys_fp, 0),
  2059. DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
  2060. DIR_HANDLE_CMD(server_keys_sk, 0),
  2061. DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
  2062. DIR_HANDLE_CMD(server_keys_fpsk, 0),
  2063. DIR_HANDLE_CMD(status_vote_current_not_found, 0),
  2064. DIR_HANDLE_CMD(status_vote_next_not_found, 0),
  2065. DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
  2066. DIR_HANDLE_CMD(status_vote_current_authority, 0),
  2067. DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
  2068. DIR_HANDLE_CMD(status_vote_next_authority, 0),
  2069. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, TT_FORK),
  2070. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, TT_FORK),
  2071. DIR_HANDLE_CMD(status_vote_current_consensus_too_old, TT_FORK),
  2072. DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, TT_FORK),
  2073. DIR_HANDLE_CMD(status_vote_current_consensus_ns, TT_FORK),
  2074. DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
  2075. DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
  2076. DIR_HANDLE_CMD(status_vote_d, 0),
  2077. DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
  2078. DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
  2079. DIR_HANDLE_CMD(status_vote_next_consensus, 0),
  2080. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
  2081. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
  2082. DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
  2083. DIR_HANDLE_CMD(parse_accept_encoding, 0),
  2084. END_OF_TESTCASES
  2085. };