test_dir_handle_get.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761
  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 = options_new();
  379. mock_options->TestingTorNetwork = 1;
  380. mock_options->DataDirectory = tor_strdup(get_fname_rnd("datadir_tmp"));
  381. mock_options->CacheDirectory = tor_strdup(mock_options->DataDirectory);
  382. check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
  383. }
  384. static const or_options_t *
  385. mock_get_options(void)
  386. {
  387. tor_assert(mock_options);
  388. return mock_options;
  389. }
  390. static const char microdesc[] =
  391. "onion-key\n"
  392. "-----BEGIN RSA PUBLIC KEY-----\n"
  393. "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
  394. "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
  395. "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
  396. "-----END RSA PUBLIC KEY-----\n";
  397. static void
  398. test_dir_handle_get_micro_d(void *data)
  399. {
  400. dir_connection_t *conn = NULL;
  401. microdesc_cache_t *mc = NULL ;
  402. smartlist_t *list = NULL;
  403. char digest[DIGEST256_LEN];
  404. char digest_base64[128];
  405. char path[80];
  406. char *header = NULL;
  407. char *body = NULL;
  408. size_t body_used = 0;
  409. (void) data;
  410. MOCK(get_options, mock_get_options);
  411. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  412. /* SETUP */
  413. init_mock_options();
  414. /* Add microdesc to cache */
  415. crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  416. base64_encode_nopad(digest_base64, sizeof(digest_base64),
  417. (uint8_t *) digest, DIGEST256_LEN);
  418. mc = get_microdesc_cache();
  419. list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
  420. time(NULL), NULL);
  421. tt_int_op(1, OP_EQ, smartlist_len(list));
  422. /* Make the request */
  423. conn = new_dir_conn();
  424. tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
  425. tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
  426. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  427. &body, &body_used, strlen(microdesc)+1, 0);
  428. tt_assert(header);
  429. tt_assert(body);
  430. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  431. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  432. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  433. tt_int_op(body_used, OP_EQ, strlen(body));
  434. tt_str_op(body, OP_EQ, microdesc);
  435. done:
  436. UNMOCK(get_options);
  437. UNMOCK(connection_write_to_buf_impl_);
  438. or_options_free(mock_options); mock_options = NULL;
  439. connection_free_minimal(TO_CONN(conn));
  440. tor_free(header);
  441. tor_free(body);
  442. smartlist_free(list);
  443. microdesc_free_all();
  444. }
  445. static void
  446. test_dir_handle_get_micro_d_server_busy(void *data)
  447. {
  448. dir_connection_t *conn = NULL;
  449. microdesc_cache_t *mc = NULL ;
  450. smartlist_t *list = NULL;
  451. char digest[DIGEST256_LEN];
  452. char digest_base64[128];
  453. char path[80];
  454. char *header = NULL;
  455. (void) data;
  456. MOCK(get_options, mock_get_options);
  457. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  458. /* SETUP */
  459. init_mock_options();
  460. /* Add microdesc to cache */
  461. crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
  462. base64_encode_nopad(digest_base64, sizeof(digest_base64),
  463. (uint8_t *) digest, DIGEST256_LEN);
  464. mc = get_microdesc_cache();
  465. list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
  466. time(NULL), NULL);
  467. tt_int_op(1, OP_EQ, smartlist_len(list));
  468. //Make it busy
  469. mock_options->CountPrivateBandwidth = 1;
  470. /* Make the request */
  471. conn = new_dir_conn();
  472. tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
  473. tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
  474. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  475. NULL, NULL, 1, 0);
  476. tt_str_op(SERVER_BUSY, OP_EQ, header);
  477. done:
  478. UNMOCK(get_options);
  479. UNMOCK(connection_write_to_buf_impl_);
  480. or_options_free(mock_options); mock_options = NULL;
  481. connection_free_minimal(TO_CONN(conn));
  482. tor_free(header);
  483. smartlist_free(list);
  484. microdesc_free_all();
  485. }
  486. #define BRIDGES_PATH "/tor/networkstatus-bridges"
  487. static void
  488. test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
  489. {
  490. dir_connection_t *conn = NULL;
  491. char *header = NULL;
  492. (void) data;
  493. MOCK(get_options, mock_get_options);
  494. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  495. /* SETUP */
  496. init_mock_options();
  497. mock_options->BridgeAuthoritativeDir = 1;
  498. mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
  499. conn = new_dir_conn();
  500. TO_CONN(conn)->linked = 1;
  501. const char *req = GET(BRIDGES_PATH);
  502. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  503. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  504. NULL, NULL, 1, 0);
  505. tt_str_op(NOT_FOUND, OP_EQ, header);
  506. done:
  507. UNMOCK(get_options);
  508. UNMOCK(connection_write_to_buf_impl_);
  509. or_options_free(mock_options); mock_options = NULL;
  510. connection_free_minimal(TO_CONN(conn));
  511. tor_free(header);
  512. }
  513. static void
  514. test_dir_handle_get_networkstatus_bridges(void *data)
  515. {
  516. dir_connection_t *conn = NULL;
  517. char *header = NULL;
  518. (void) data;
  519. MOCK(get_options, mock_get_options);
  520. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  521. /* SETUP */
  522. init_mock_options();
  523. mock_options->BridgeAuthoritativeDir = 1;
  524. mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  525. crypto_digest256(mock_options->BridgePassword_AuthDigest_,
  526. "abcdefghijklm12345", 18, DIGEST_SHA256);
  527. conn = new_dir_conn();
  528. TO_CONN(conn)->linked = 1;
  529. const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
  530. "Authorization: Basic abcdefghijklm12345\r\n\r\n";
  531. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  532. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  533. NULL, NULL, 1, 0);
  534. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  535. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  536. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  537. tt_assert(strstr(header, "Content-Length: 0\r\n"));
  538. done:
  539. UNMOCK(get_options);
  540. UNMOCK(connection_write_to_buf_impl_);
  541. or_options_free(mock_options); mock_options = NULL;
  542. connection_free_minimal(TO_CONN(conn));
  543. tor_free(header);
  544. }
  545. static void
  546. test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
  547. {
  548. dir_connection_t *conn = NULL;
  549. char *header = NULL;
  550. (void) data;
  551. MOCK(get_options, mock_get_options);
  552. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  553. /* SETUP */
  554. init_mock_options();
  555. mock_options->BridgeAuthoritativeDir = 1;
  556. mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
  557. crypto_digest256(mock_options->BridgePassword_AuthDigest_,
  558. "abcdefghijklm12345", 18, DIGEST_SHA256);
  559. conn = new_dir_conn();
  560. TO_CONN(conn)->linked = 1;
  561. const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
  562. "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
  563. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  564. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  565. NULL, NULL, 1, 0);
  566. tt_str_op(NOT_FOUND, OP_EQ, header);
  567. done:
  568. UNMOCK(get_options);
  569. UNMOCK(connection_write_to_buf_impl_);
  570. or_options_free(mock_options); mock_options = NULL;
  571. connection_free_minimal(TO_CONN(conn));
  572. tor_free(header);
  573. }
  574. #define SERVER_DESC_GET(id) GET("/tor/server/" id)
  575. static void
  576. test_dir_handle_get_server_descriptors_not_found(void* data)
  577. {
  578. dir_connection_t *conn = NULL;
  579. char *header = NULL;
  580. (void) data;
  581. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  582. conn = new_dir_conn();
  583. const char *req = SERVER_DESC_GET("invalid");
  584. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  585. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  586. NULL, NULL, 1, 0);
  587. tt_str_op(NOT_FOUND, OP_EQ, header);
  588. tt_ptr_op(conn->spool, OP_EQ, NULL);
  589. done:
  590. UNMOCK(connection_write_to_buf_impl_);
  591. or_options_free(mock_options); mock_options = NULL;
  592. connection_free_minimal(TO_CONN(conn));
  593. tor_free(header);
  594. }
  595. static void
  596. test_dir_handle_get_server_descriptors_all(void* data)
  597. {
  598. dir_connection_t *conn = NULL;
  599. char *header = NULL;
  600. char *body = NULL;
  601. size_t body_used = 0;
  602. (void) data;
  603. /* Setup fake routerlist. */
  604. helper_setup_fake_routerlist();
  605. //TODO: change to router_get_my_extrainfo when testing "extra" path
  606. NS_MOCK(router_get_my_routerinfo);
  607. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  608. // We are one of the routers
  609. routerlist_t *our_routerlist = router_get_routerlist();
  610. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  611. mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
  612. set_server_identity_key(mock_routerinfo->identity_pkey);
  613. mock_routerinfo->cache_info.published_on = time(NULL);
  614. /* Treat "all" requests as if they were unencrypted */
  615. mock_routerinfo->cache_info.send_unencrypted = 1;
  616. conn = new_dir_conn();
  617. const char *req = SERVER_DESC_GET("all");
  618. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  619. //TODO: Is this a BUG?
  620. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  621. //which is smaller than that by annotation_len bytes
  622. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  623. &body, &body_used,
  624. 1024*1024, 0);
  625. tt_assert(header);
  626. tt_assert(body);
  627. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  628. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  629. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  630. //TODO: Is this a BUG?
  631. //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
  632. tt_int_op(body_used, OP_EQ,
  633. mock_routerinfo->cache_info.signed_descriptor_len);
  634. tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
  635. mock_routerinfo->cache_info.annotations_len);
  636. tt_ptr_op(conn->spool, OP_EQ, NULL);
  637. done:
  638. NS_UNMOCK(router_get_my_routerinfo);
  639. UNMOCK(connection_write_to_buf_impl_);
  640. connection_free_minimal(TO_CONN(conn));
  641. tor_free(header);
  642. tor_free(body);
  643. routerlist_free_all();
  644. nodelist_free_all();
  645. entry_guards_free_all();
  646. }
  647. static char
  648. TEST_DESCRIPTOR[] =
  649. "@uploaded-at 2014-06-08 19:20:11\n"
  650. "@source \"127.0.0.1\"\n"
  651. "router test000a 127.0.0.1 5000 0 7000\n"
  652. "platform Tor 0.2.5.3-alpha-dev on Linux\n"
  653. "protocols Link 1 2 Circuit 1\n"
  654. "published 2014-06-08 19:20:11\n"
  655. "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
  656. "uptime 0\n"
  657. "bandwidth 1073741824 1073741824 0\n"
  658. "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
  659. "caches-extra-info\n"
  660. "onion-key\n"
  661. "-----BEGIN RSA PUBLIC KEY-----\n"
  662. "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
  663. "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
  664. "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
  665. "-----END RSA PUBLIC KEY-----\n"
  666. "signing-key\n"
  667. "-----BEGIN RSA PUBLIC KEY-----\n"
  668. "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
  669. "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
  670. "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
  671. "-----END RSA PUBLIC KEY-----\n"
  672. "hidden-service-dir\n"
  673. "contact auth0@test.test\n"
  674. "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
  675. "reject *:*\n"
  676. "router-signature\n"
  677. "-----BEGIN SIGNATURE-----\n"
  678. "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
  679. "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
  680. "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
  681. "-----END SIGNATURE-----\n";
  682. static void
  683. test_dir_handle_get_server_descriptors_authority(void* data)
  684. {
  685. dir_connection_t *conn = NULL;
  686. char *header = NULL;
  687. char *body = NULL;
  688. size_t body_used = 0;
  689. crypto_pk_t *identity_pkey = pk_generate(0);
  690. (void) data;
  691. NS_MOCK(router_get_my_routerinfo);
  692. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  693. /* init mock */
  694. router_get_my_routerinfo();
  695. crypto_pk_get_digest(identity_pkey,
  696. mock_routerinfo->cache_info.identity_digest);
  697. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  698. set_server_identity_key(identity_pkey);
  699. mock_routerinfo->cache_info.send_unencrypted = 1;
  700. /* Setup descriptor */
  701. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  702. mock_routerinfo->cache_info.signed_descriptor_body =
  703. tor_strdup(TEST_DESCRIPTOR);
  704. mock_routerinfo->cache_info.signed_descriptor_len =
  705. strlen(TEST_DESCRIPTOR) - annotation_len;
  706. mock_routerinfo->cache_info.annotations_len = annotation_len;
  707. mock_routerinfo->cache_info.published_on = time(NULL);
  708. conn = new_dir_conn();
  709. const char *req = SERVER_DESC_GET("authority");
  710. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  711. //TODO: Is this a BUG?
  712. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  713. //is smaller than that by annotation_len bytes
  714. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  715. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  716. tt_assert(header);
  717. tt_assert(body);
  718. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  719. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  720. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  721. tt_int_op(body_used, OP_EQ, strlen(body));
  722. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  723. tt_ptr_op(conn->spool, OP_EQ, NULL);
  724. done:
  725. NS_UNMOCK(router_get_my_routerinfo);
  726. UNMOCK(connection_write_to_buf_impl_);
  727. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  728. tor_free(mock_routerinfo);
  729. connection_free_minimal(TO_CONN(conn));
  730. tor_free(header);
  731. tor_free(body);
  732. crypto_pk_free(identity_pkey);
  733. }
  734. static void
  735. test_dir_handle_get_server_descriptors_fp(void* data)
  736. {
  737. dir_connection_t *conn = NULL;
  738. char *header = NULL;
  739. char *body = NULL;
  740. size_t body_used = 0;
  741. crypto_pk_t *identity_pkey = pk_generate(0);
  742. (void) data;
  743. NS_MOCK(router_get_my_routerinfo);
  744. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  745. /* init mock */
  746. router_get_my_routerinfo();
  747. crypto_pk_get_digest(identity_pkey,
  748. mock_routerinfo->cache_info.identity_digest);
  749. // the digest is mine (the channel is unnecrypted, so we must allow sending)
  750. set_server_identity_key(identity_pkey);
  751. mock_routerinfo->cache_info.send_unencrypted = 1;
  752. /* Setup descriptor */
  753. long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
  754. mock_routerinfo->cache_info.signed_descriptor_body =
  755. tor_strdup(TEST_DESCRIPTOR);
  756. mock_routerinfo->cache_info.signed_descriptor_len =
  757. strlen(TEST_DESCRIPTOR) - annotation_len;
  758. mock_routerinfo->cache_info.annotations_len = annotation_len;
  759. mock_routerinfo->cache_info.published_on = time(NULL);
  760. conn = new_dir_conn();
  761. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  762. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  763. const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
  764. DIGEST_LEN);
  765. char req[155];
  766. tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
  767. hex_digest);
  768. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  769. //TODO: Is this a BUG?
  770. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  771. //is smaller than that by annotation_len bytes
  772. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  773. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  774. tt_assert(header);
  775. tt_assert(body);
  776. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  777. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  778. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  779. tt_int_op(body_used, OP_EQ, strlen(body));
  780. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  781. tt_ptr_op(conn->spool, OP_EQ, NULL);
  782. done:
  783. NS_UNMOCK(router_get_my_routerinfo);
  784. UNMOCK(connection_write_to_buf_impl_);
  785. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  786. tor_free(mock_routerinfo);
  787. connection_free_minimal(TO_CONN(conn));
  788. tor_free(header);
  789. tor_free(body);
  790. crypto_pk_free(identity_pkey);
  791. }
  792. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  793. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  794. static void
  795. test_dir_handle_get_server_descriptors_d(void* data)
  796. {
  797. dir_connection_t *conn = NULL;
  798. char *header = NULL;
  799. char *body = NULL;
  800. size_t body_used = 0;
  801. crypto_pk_t *identity_pkey = pk_generate(0);
  802. (void) data;
  803. /* Setup fake routerlist. */
  804. helper_setup_fake_routerlist();
  805. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  806. /* Get one router's signed_descriptor_digest */
  807. routerlist_t *our_routerlist = router_get_routerlist();
  808. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  809. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  810. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  811. DIGEST_LEN);
  812. conn = new_dir_conn();
  813. char req_header[155]; /* XXX Why 155? What kind of number is that?? */
  814. tor_snprintf(req_header, sizeof(req_header),
  815. SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  816. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  817. //TODO: Is this a BUG?
  818. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  819. //which is smaller than that by annotation_len bytes
  820. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  821. &body, &body_used,
  822. router->cache_info.signed_descriptor_len+1, 0);
  823. tt_assert(header);
  824. tt_assert(body);
  825. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  826. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  827. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  828. //TODO: Is this a BUG?
  829. //This is what should be expected:
  830. //tt_int_op(body_used, OP_EQ, strlen(body));
  831. tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
  832. tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
  833. router->cache_info.annotations_len);
  834. tt_ptr_op(conn->spool, OP_EQ, NULL);
  835. done:
  836. UNMOCK(connection_write_to_buf_impl_);
  837. tor_free(mock_routerinfo);
  838. connection_free_minimal(TO_CONN(conn));
  839. tor_free(header);
  840. tor_free(body);
  841. crypto_pk_free(identity_pkey);
  842. routerlist_free_all();
  843. nodelist_free_all();
  844. entry_guards_free_all();
  845. }
  846. static void
  847. test_dir_handle_get_server_descriptors_busy(void* data)
  848. {
  849. dir_connection_t *conn = NULL;
  850. char *header = NULL;
  851. crypto_pk_t *identity_pkey = pk_generate(0);
  852. (void) data;
  853. /* Setup fake routerlist. */
  854. helper_setup_fake_routerlist();
  855. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  856. //Make it busy
  857. MOCK(get_options, mock_get_options);
  858. init_mock_options();
  859. mock_options->CountPrivateBandwidth = 1;
  860. /* Get one router's signed_descriptor_digest */
  861. routerlist_t *our_routerlist = router_get_routerlist();
  862. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  863. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  864. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  865. DIGEST_LEN);
  866. conn = new_dir_conn();
  867. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  868. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  869. char req_header[155]; /* XXX 155? Why 155? */
  870. tor_snprintf(req_header, sizeof(req_header),
  871. SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  872. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  873. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  874. NULL, NULL, 1, 0);
  875. tt_assert(header);
  876. tt_str_op(SERVER_BUSY, OP_EQ, header);
  877. tt_ptr_op(conn->spool, OP_EQ, NULL);
  878. done:
  879. UNMOCK(get_options);
  880. UNMOCK(connection_write_to_buf_impl_);
  881. tor_free(mock_routerinfo);
  882. connection_free_minimal(TO_CONN(conn));
  883. tor_free(header);
  884. crypto_pk_free(identity_pkey);
  885. routerlist_free_all();
  886. nodelist_free_all();
  887. entry_guards_free_all();
  888. }
  889. static void
  890. test_dir_handle_get_server_keys_bad_req(void* data)
  891. {
  892. dir_connection_t *conn = NULL;
  893. char *header = NULL;
  894. (void) data;
  895. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  896. conn = new_dir_conn();
  897. const char *req = GET("/tor/keys/");
  898. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  899. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  900. NULL, NULL, 1, 0);
  901. tt_assert(header);
  902. tt_str_op(BAD_REQUEST, OP_EQ, header);
  903. done:
  904. UNMOCK(connection_write_to_buf_impl_);
  905. connection_free_minimal(TO_CONN(conn));
  906. tor_free(header);
  907. }
  908. static void
  909. test_dir_handle_get_server_keys_all_not_found(void* data)
  910. {
  911. dir_connection_t *conn = NULL;
  912. char *header = NULL;
  913. (void) data;
  914. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  915. conn = new_dir_conn();
  916. const char *req = GET("/tor/keys/all");
  917. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  918. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  919. NULL, NULL, 1, 0);
  920. tt_assert(header);
  921. tt_str_op(NOT_FOUND, OP_EQ, header);
  922. done:
  923. UNMOCK(connection_write_to_buf_impl_);
  924. connection_free_minimal(TO_CONN(conn));
  925. tor_free(header);
  926. }
  927. #define TEST_CERTIFICATE AUTHORITY_CERT_3
  928. #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
  929. static const char TEST_CERT_IDENT_KEY[] =
  930. "D867ACF56A9D229B35C25F0090BC9867E906BE69";
  931. static void
  932. test_dir_handle_get_server_keys_all(void* data)
  933. {
  934. dir_connection_t *conn = NULL;
  935. char *header = NULL;
  936. char *body = NULL;
  937. size_t body_used = 0;
  938. const char digest[DIGEST_LEN] = "";
  939. dir_server_t *ds = NULL;
  940. (void) data;
  941. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  942. clear_dir_servers();
  943. routerlist_free_all();
  944. /* create a trusted ds */
  945. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  946. NULL, V3_DIRINFO, 1.0);
  947. tt_assert(ds);
  948. dir_server_add(ds);
  949. /* ds v3_identity_digest is the certificate's identity_key */
  950. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  951. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  952. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  953. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  954. conn = new_dir_conn();
  955. const char *req = GET("/tor/keys/all");
  956. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  957. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  958. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  959. tt_assert(header);
  960. tt_assert(body);
  961. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  962. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  963. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  964. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  965. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  966. done:
  967. UNMOCK(connection_write_to_buf_impl_);
  968. connection_free_minimal(TO_CONN(conn));
  969. tor_free(header);
  970. tor_free(body);
  971. clear_dir_servers();
  972. routerlist_free_all();
  973. }
  974. static void
  975. test_dir_handle_get_server_keys_authority_not_found(void* data)
  976. {
  977. dir_connection_t *conn = NULL;
  978. char *header = NULL;
  979. (void) data;
  980. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  981. conn = new_dir_conn();
  982. const char *req = GET("/tor/keys/authority");
  983. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  984. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  985. NULL, NULL, 1, 0);
  986. tt_assert(header);
  987. tt_str_op(NOT_FOUND, OP_EQ, header);
  988. done:
  989. UNMOCK(connection_write_to_buf_impl_);
  990. connection_free_minimal(TO_CONN(conn));
  991. tor_free(header);
  992. }
  993. static authority_cert_t * mock_cert = NULL;
  994. static authority_cert_t *
  995. get_my_v3_authority_cert_m(void)
  996. {
  997. tor_assert(mock_cert);
  998. return mock_cert;
  999. }
  1000. static void
  1001. test_dir_handle_get_server_keys_authority(void* data)
  1002. {
  1003. dir_connection_t *conn = NULL;
  1004. char *header = NULL;
  1005. char *body = NULL;
  1006. size_t body_used = 0;
  1007. (void) data;
  1008. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1009. strlen(TEST_CERTIFICATE),
  1010. NULL);
  1011. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1012. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1013. conn = new_dir_conn();
  1014. const char *req = GET("/tor/keys/authority");
  1015. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1016. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1017. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1018. tt_assert(header);
  1019. tt_assert(body);
  1020. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1021. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1022. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1023. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1024. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1025. done:
  1026. UNMOCK(get_my_v3_authority_cert);
  1027. UNMOCK(connection_write_to_buf_impl_);
  1028. connection_free_minimal(TO_CONN(conn));
  1029. tor_free(header);
  1030. tor_free(body);
  1031. authority_cert_free(mock_cert); mock_cert = NULL;
  1032. }
  1033. static void
  1034. test_dir_handle_get_server_keys_fp_not_found(void* data)
  1035. {
  1036. dir_connection_t *conn = NULL;
  1037. char *header = NULL;
  1038. (void) data;
  1039. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1040. conn = new_dir_conn();
  1041. const char *req = GET("/tor/keys/fp/somehex");
  1042. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1043. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1044. NULL, NULL, 1, 0);
  1045. tt_assert(header);
  1046. tt_str_op(NOT_FOUND, OP_EQ, header);
  1047. done:
  1048. UNMOCK(connection_write_to_buf_impl_);
  1049. connection_free_minimal(TO_CONN(conn));
  1050. tor_free(header);
  1051. }
  1052. static void
  1053. test_dir_handle_get_server_keys_fp(void* data)
  1054. {
  1055. dir_connection_t *conn = NULL;
  1056. char *header = NULL;
  1057. char *body = NULL;
  1058. size_t body_used = 0;
  1059. dir_server_t *ds = NULL;
  1060. const char digest[DIGEST_LEN] = "";
  1061. (void) data;
  1062. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1063. clear_dir_servers();
  1064. routerlist_free_all();
  1065. /* create a trusted ds */
  1066. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1067. NULL, V3_DIRINFO, 1.0);
  1068. tt_assert(ds);
  1069. dir_server_add(ds);
  1070. /* ds v3_identity_digest is the certificate's identity_key */
  1071. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1072. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1073. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1074. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1075. conn = new_dir_conn();
  1076. char req[71];
  1077. tor_snprintf(req, sizeof(req),
  1078. GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1079. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1080. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1081. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1082. tt_assert(header);
  1083. tt_assert(body);
  1084. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1085. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1086. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1087. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1088. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1089. done:
  1090. UNMOCK(connection_write_to_buf_impl_);
  1091. connection_free_minimal(TO_CONN(conn));
  1092. tor_free(header);
  1093. tor_free(body);
  1094. clear_dir_servers();
  1095. routerlist_free_all();
  1096. }
  1097. static void
  1098. test_dir_handle_get_server_keys_sk_not_found(void* data)
  1099. {
  1100. dir_connection_t *conn = NULL;
  1101. char *header = NULL;
  1102. (void) data;
  1103. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1104. conn = new_dir_conn();
  1105. const char *req = GET("/tor/keys/sk/somehex");
  1106. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1107. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1108. NULL, NULL, 1, 0);
  1109. tt_assert(header);
  1110. tt_str_op(NOT_FOUND, OP_EQ, header);
  1111. done:
  1112. UNMOCK(connection_write_to_buf_impl_);
  1113. connection_free_minimal(TO_CONN(conn));
  1114. tor_free(header);
  1115. }
  1116. static void
  1117. test_dir_handle_get_server_keys_sk(void* data)
  1118. {
  1119. dir_connection_t *conn = NULL;
  1120. char *header = NULL;
  1121. char *body = NULL;
  1122. size_t body_used = 0;
  1123. (void) data;
  1124. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1125. strlen(TEST_CERTIFICATE),
  1126. NULL);
  1127. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1128. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1129. clear_dir_servers();
  1130. routerlist_free_all();
  1131. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1132. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1133. conn = new_dir_conn();
  1134. char req[71];
  1135. tor_snprintf(req, sizeof(req),
  1136. GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
  1137. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1138. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1139. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1140. tt_assert(header);
  1141. tt_assert(body);
  1142. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1143. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1144. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1145. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1146. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1147. done:
  1148. UNMOCK(get_my_v3_authority_cert);
  1149. UNMOCK(connection_write_to_buf_impl_);
  1150. connection_free_minimal(TO_CONN(conn));
  1151. authority_cert_free(mock_cert); mock_cert = NULL;
  1152. tor_free(header);
  1153. tor_free(body);
  1154. }
  1155. static void
  1156. test_dir_handle_get_server_keys_fpsk_not_found(void* data)
  1157. {
  1158. dir_connection_t *conn = NULL;
  1159. char *header = NULL;
  1160. (void) data;
  1161. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1162. conn = new_dir_conn();
  1163. const char *req = GET("/tor/keys/fp-sk/somehex");
  1164. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1165. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1166. NULL, NULL, 1, 0);
  1167. tt_assert(header);
  1168. tt_str_op(NOT_FOUND, OP_EQ, header);
  1169. done:
  1170. UNMOCK(connection_write_to_buf_impl_);
  1171. connection_free_minimal(TO_CONN(conn));
  1172. tor_free(header);
  1173. }
  1174. static void
  1175. test_dir_handle_get_server_keys_fpsk(void* data)
  1176. {
  1177. dir_connection_t *conn = NULL;
  1178. char *header = NULL;
  1179. char *body = NULL;
  1180. size_t body_used = 0;
  1181. dir_server_t *ds = NULL;
  1182. const char digest[DIGEST_LEN] = "";
  1183. (void) data;
  1184. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1185. clear_dir_servers();
  1186. routerlist_free_all();
  1187. /* create a trusted ds */
  1188. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1189. NULL, V3_DIRINFO, 1.0);
  1190. tt_assert(ds);
  1191. /* ds v3_identity_digest is the certificate's identity_key */
  1192. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1193. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1194. dir_server_add(ds);
  1195. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1196. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1197. conn = new_dir_conn();
  1198. char req[115];
  1199. tor_snprintf(req, sizeof(req),
  1200. GET("/tor/keys/fp-sk/%s-%s"),
  1201. TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
  1202. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1203. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1204. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1205. tt_assert(header);
  1206. tt_assert(body);
  1207. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1208. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1209. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1210. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1211. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1212. done:
  1213. UNMOCK(connection_write_to_buf_impl_);
  1214. connection_free_minimal(TO_CONN(conn));
  1215. tor_free(header);
  1216. tor_free(body);
  1217. clear_dir_servers();
  1218. routerlist_free_all();
  1219. }
  1220. static void
  1221. test_dir_handle_get_server_keys_busy(void* data)
  1222. {
  1223. dir_connection_t *conn = NULL;
  1224. char *header = NULL;
  1225. dir_server_t *ds = NULL;
  1226. const char digest[DIGEST_LEN] = "";
  1227. (void) data;
  1228. clear_dir_servers();
  1229. routerlist_free_all();
  1230. /* create a trusted ds */
  1231. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1232. NULL, V3_DIRINFO, 1.0);
  1233. tt_assert(ds);
  1234. /* ds v3_identity_digest is the certificate's identity_key */
  1235. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1236. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1237. dir_server_add(ds);
  1238. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1239. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1240. MOCK(get_options, mock_get_options);
  1241. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1242. /* setup busy server */
  1243. init_mock_options();
  1244. mock_options->CountPrivateBandwidth = 1;
  1245. conn = new_dir_conn();
  1246. char req[71];
  1247. tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1248. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1249. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1250. NULL, NULL, 1, 0);
  1251. tt_assert(header);
  1252. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1253. done:
  1254. UNMOCK(get_options);
  1255. UNMOCK(connection_write_to_buf_impl_);
  1256. connection_free_minimal(TO_CONN(conn));
  1257. tor_free(header);
  1258. or_options_free(mock_options); mock_options = NULL;
  1259. clear_dir_servers();
  1260. routerlist_free_all();
  1261. }
  1262. static networkstatus_t *mock_ns_val = NULL;
  1263. static networkstatus_t *
  1264. mock_ns_get_by_flavor(consensus_flavor_t f)
  1265. {
  1266. (void)f;
  1267. return mock_ns_val;
  1268. }
  1269. static void
  1270. test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
  1271. {
  1272. dir_connection_t *conn = NULL;
  1273. char *header = NULL;
  1274. char *stats = NULL;
  1275. (void) d;
  1276. /* init mock */
  1277. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1278. mock_ns_val->flavor = FLAV_NS;
  1279. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1280. mock_ns_val->voters = smartlist_new();
  1281. mock_ns_val->valid_after = time(NULL) - 1800;
  1282. mock_ns_val->valid_until = time(NULL) - 60;
  1283. #define NETWORK_STATUS "some network status string"
  1284. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1285. /* init mock */
  1286. init_mock_options();
  1287. MOCK(get_options, mock_get_options);
  1288. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1289. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1290. /* start gathering stats */
  1291. mock_options->DirReqStatistics = 1;
  1292. geoip_dirreq_stats_init(time(NULL));
  1293. conn = new_dir_conn();
  1294. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1295. GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
  1296. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1297. NULL, NULL, 1, 0);
  1298. tt_assert(header);
  1299. tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
  1300. stats = geoip_format_dirreq_stats(time(NULL));
  1301. tt_assert(stats);
  1302. tt_assert(strstr(stats, "not-enough-sigs=8"));
  1303. done:
  1304. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1305. UNMOCK(connection_write_to_buf_impl_);
  1306. UNMOCK(get_options);
  1307. connection_free_minimal(TO_CONN(conn));
  1308. tor_free(header);
  1309. tor_free(stats);
  1310. smartlist_free(mock_ns_val->voters);
  1311. tor_free(mock_ns_val);
  1312. or_options_free(mock_options); mock_options = NULL;
  1313. }
  1314. static void
  1315. test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
  1316. {
  1317. dir_connection_t *conn = NULL;
  1318. char *header = NULL;
  1319. char *stats = NULL;
  1320. (void) data;
  1321. init_mock_options();
  1322. MOCK(get_options, mock_get_options);
  1323. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1324. /* start gathering stats */
  1325. mock_options->DirReqStatistics = 1;
  1326. geoip_dirreq_stats_init(time(NULL));
  1327. conn = new_dir_conn();
  1328. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1329. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1330. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1331. NULL, NULL, 1, 0);
  1332. tt_assert(header);
  1333. tt_str_op(NOT_FOUND, OP_EQ, header);
  1334. stats = geoip_format_dirreq_stats(time(NULL));
  1335. tt_assert(stats);
  1336. tt_assert(strstr(stats, "not-found=8"));
  1337. done:
  1338. UNMOCK(connection_write_to_buf_impl_);
  1339. UNMOCK(get_options);
  1340. connection_free_minimal(TO_CONN(conn));
  1341. tor_free(header);
  1342. tor_free(stats);
  1343. or_options_free(mock_options); mock_options = NULL;
  1344. }
  1345. static void
  1346. test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
  1347. {
  1348. dir_connection_t *conn = NULL;
  1349. char *header = NULL;
  1350. (void)data;
  1351. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1352. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1353. mock_ns_val->flavor = FLAV_MICRODESC;
  1354. mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
  1355. mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
  1356. mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
  1357. #define NETWORK_STATUS "some network status string"
  1358. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1359. init_mock_options();
  1360. MOCK(get_options, mock_get_options);
  1361. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1362. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1363. conn = new_dir_conn();
  1364. setup_capture_of_logs(LOG_WARN);
  1365. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1366. GET("/tor/status-vote/current/consensus-microdesc"), NULL, 0));
  1367. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1368. NULL, NULL, 1, 0);
  1369. tt_assert(header);
  1370. tt_str_op(TOO_OLD, OP_EQ, header);
  1371. expect_log_msg_containing("too old");
  1372. tor_free(header);
  1373. teardown_capture_of_logs();
  1374. tor_free(mock_ns_val);
  1375. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1376. mock_ns_val->type = NS_TYPE_CONSENSUS;
  1377. mock_ns_val->flavor = FLAV_NS;
  1378. mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
  1379. mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
  1380. mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
  1381. #define NETWORK_STATUS "some network status string"
  1382. consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
  1383. setup_capture_of_logs(LOG_WARN);
  1384. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1385. GET("/tor/status-vote/current/consensus"), NULL, 0));
  1386. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1387. NULL, NULL, 1, 0);
  1388. tt_assert(header);
  1389. tt_str_op(TOO_OLD, OP_EQ, header);
  1390. expect_no_log_entry();
  1391. done:
  1392. teardown_capture_of_logs();
  1393. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1394. UNMOCK(connection_write_to_buf_impl_);
  1395. UNMOCK(get_options);
  1396. connection_free_minimal(TO_CONN(conn));
  1397. tor_free(header);
  1398. tor_free(mock_ns_val);
  1399. or_options_free(mock_options); mock_options = NULL;
  1400. }
  1401. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  1402. int
  1403. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  1404. {
  1405. (void)addr;
  1406. CALLED(geoip_get_country_by_addr)++;
  1407. return 1;
  1408. }
  1409. static void
  1410. status_vote_current_consensus_ns_test(char **header, char **body,
  1411. size_t *body_len)
  1412. {
  1413. dir_connection_t *conn = NULL;
  1414. #define NETWORK_STATUS "some network status string"
  1415. #if 0
  1416. common_digests_t digests;
  1417. uint8_t sha3[DIGEST256_LEN];
  1418. memset(&digests, 0x60, sizeof(digests));
  1419. memset(sha3, 0x06, sizeof(sha3));
  1420. dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
  1421. sha3,
  1422. time(NULL));
  1423. #endif /* 0 */
  1424. networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
  1425. ns->type = NS_TYPE_CONSENSUS;
  1426. ns->flavor = FLAV_NS;
  1427. ns->valid_after = time(NULL) - 1800;
  1428. ns->fresh_until = time(NULL) - 900;
  1429. ns->valid_until = time(NULL) - 60;
  1430. consdiffmgr_add_consensus(NETWORK_STATUS, ns);
  1431. networkstatus_vote_free(ns);
  1432. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1433. tt_assert(mock_options);
  1434. mock_options->DirReqStatistics = 1;
  1435. geoip_dirreq_stats_init(time(NULL));
  1436. /* init geoip database */
  1437. geoip_parse_entry("10,50,AB", AF_INET);
  1438. tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
  1439. conn = new_dir_conn();
  1440. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1441. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1442. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1443. body, body_len, strlen(NETWORK_STATUS)+7, 0);
  1444. done:
  1445. UNMOCK(connection_write_to_buf_impl_);
  1446. connection_free_minimal(TO_CONN(conn));
  1447. }
  1448. static void
  1449. test_dir_handle_get_status_vote_current_consensus_ns(void* data)
  1450. {
  1451. char *header = NULL;
  1452. char *body = NULL, *comp_body = NULL;
  1453. size_t body_used = 0, comp_body_used = 0;
  1454. char *stats = NULL, *hist = NULL;
  1455. (void) data;
  1456. dirserv_free_all();
  1457. clear_geoip_db();
  1458. NS_MOCK(geoip_get_country_by_addr);
  1459. MOCK(get_options, mock_get_options);
  1460. init_mock_options();
  1461. status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
  1462. tt_assert(header);
  1463. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1464. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1465. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1466. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  1467. compress_method_t compression = detect_compression_method(comp_body,
  1468. comp_body_used);
  1469. tt_int_op(ZLIB_METHOD, OP_EQ, compression);
  1470. tor_uncompress(&body, &body_used, comp_body, comp_body_used,
  1471. compression, 0, LOG_PROTOCOL_WARN);
  1472. tt_str_op(NETWORK_STATUS, OP_EQ, body);
  1473. tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
  1474. stats = geoip_format_dirreq_stats(time(NULL));
  1475. tt_assert(stats);
  1476. tt_assert(strstr(stats, "ok=8"));
  1477. tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
  1478. tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
  1479. tt_assert(strstr(stats, "dirreq-v3-direct-dl"
  1480. " complete=0,timeout=0,running=4"));
  1481. hist = geoip_get_request_history();
  1482. tt_assert(hist);
  1483. tt_str_op("ab=8", OP_EQ, hist);
  1484. done:
  1485. NS_UNMOCK(geoip_get_country_by_addr);
  1486. UNMOCK(get_options);
  1487. tor_free(header);
  1488. tor_free(comp_body);
  1489. tor_free(body);
  1490. tor_free(stats);
  1491. tor_free(hist);
  1492. or_options_free(mock_options); mock_options = NULL;
  1493. dirserv_free_all();
  1494. clear_geoip_db();
  1495. }
  1496. static void
  1497. test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
  1498. {
  1499. char *header = NULL;
  1500. char *body = NULL;
  1501. size_t body_used = 0;
  1502. char *stats = NULL;
  1503. (void) data;
  1504. dirserv_free_all();
  1505. clear_geoip_db();
  1506. MOCK(get_options, mock_get_options);
  1507. // Make it busy
  1508. init_mock_options();
  1509. mock_options->CountPrivateBandwidth = 1;
  1510. status_vote_current_consensus_ns_test(&header, &body, &body_used);
  1511. tt_assert(header);
  1512. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1513. stats = geoip_format_dirreq_stats(time(NULL));
  1514. tt_assert(stats);
  1515. tt_assert(strstr(stats, "busy=8"));
  1516. done:
  1517. UNMOCK(get_options);
  1518. tor_free(header);
  1519. tor_free(body);
  1520. or_options_free(mock_options); mock_options = NULL;
  1521. tor_free(stats);
  1522. dirserv_free_all();
  1523. clear_geoip_db();
  1524. }
  1525. static void
  1526. test_dir_handle_get_status_vote_current_not_found(void* data)
  1527. {
  1528. dir_connection_t *conn = NULL;
  1529. char *header = NULL;
  1530. (void) data;
  1531. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1532. conn = new_dir_conn();
  1533. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1534. GET("/tor/status-vote/current/" HEX1), NULL, 0));
  1535. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1536. NULL, NULL, 1, 0);
  1537. tt_assert(header);
  1538. tt_str_op(NOT_FOUND, OP_EQ, header);
  1539. done:
  1540. UNMOCK(connection_write_to_buf_impl_);
  1541. connection_free_minimal(TO_CONN(conn));
  1542. tor_free(header);
  1543. }
  1544. #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
  1545. static void
  1546. status_vote_current_d_test(char **header, char **body, size_t *body_l)
  1547. {
  1548. dir_connection_t *conn = NULL;
  1549. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1550. conn = new_dir_conn();
  1551. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1552. GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
  1553. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1554. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1555. tt_assert(header);
  1556. done:
  1557. UNMOCK(connection_write_to_buf_impl_);
  1558. connection_free_minimal(TO_CONN(conn));
  1559. }
  1560. static void
  1561. status_vote_next_d_test(char **header, char **body, size_t *body_l)
  1562. {
  1563. dir_connection_t *conn = NULL;
  1564. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1565. conn = new_dir_conn();
  1566. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1567. GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
  1568. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1569. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1570. tt_assert(header);
  1571. done:
  1572. UNMOCK(connection_write_to_buf_impl_);
  1573. connection_free_minimal(TO_CONN(conn));
  1574. }
  1575. static void
  1576. test_dir_handle_get_status_vote_current_d_not_found(void* data)
  1577. {
  1578. char *header = NULL;
  1579. (void) data;
  1580. status_vote_current_d_test(&header, NULL, NULL);
  1581. tt_assert(header);
  1582. tt_str_op(NOT_FOUND, OP_EQ, header);
  1583. done:
  1584. tor_free(header);
  1585. }
  1586. static void
  1587. test_dir_handle_get_status_vote_next_d_not_found(void* data)
  1588. {
  1589. char *header = NULL;
  1590. (void) data;
  1591. status_vote_next_d_test(&header, NULL, NULL);
  1592. tt_assert(header);
  1593. tt_str_op(NOT_FOUND, OP_EQ, header);
  1594. done:
  1595. UNMOCK(connection_write_to_buf_impl_);
  1596. tor_free(header);
  1597. }
  1598. static void
  1599. test_dir_handle_get_status_vote_d(void* data)
  1600. {
  1601. char *header = NULL, *body = NULL;
  1602. size_t body_used = 0;
  1603. dir_server_t *ds = NULL;
  1604. const char digest[DIGEST_LEN] = "";
  1605. (void) data;
  1606. clear_dir_servers();
  1607. dirvote_free_all();
  1608. /* create a trusted ds */
  1609. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1610. NULL, V3_DIRINFO, 1.0);
  1611. tt_assert(ds);
  1612. dir_server_add(ds);
  1613. /* ds v3_identity_digest is the certificate's identity_key */
  1614. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1615. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1616. init_mock_options();
  1617. mock_options->AuthoritativeDir = 1;
  1618. mock_options->V3AuthoritativeDir = 1;
  1619. mock_options->TestingV3AuthVotingStartOffset = 0;
  1620. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1621. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1622. mock_options->TestingV3AuthInitialDistDelay = 1;
  1623. time_t now = 1441223455 -1;
  1624. voting_schedule_recalculate_timing(mock_options, now);
  1625. const char *msg_out = NULL;
  1626. int status_out = 0;
  1627. struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1628. &status_out);
  1629. tt_assert(pv);
  1630. status_vote_current_d_test(&header, &body, &body_used);
  1631. tt_assert(header);
  1632. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1633. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1634. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1635. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1636. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1637. tor_free(header);
  1638. tor_free(body);
  1639. status_vote_next_d_test(&header, &body, &body_used);
  1640. tt_assert(header);
  1641. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1642. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1643. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1644. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1645. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1646. done:
  1647. tor_free(header);
  1648. tor_free(body);
  1649. or_options_free(mock_options); mock_options = NULL;
  1650. clear_dir_servers();
  1651. dirvote_free_all();
  1652. routerlist_free_all();
  1653. }
  1654. static void
  1655. test_dir_handle_get_status_vote_next_not_found(void* data)
  1656. {
  1657. dir_connection_t *conn = NULL;
  1658. char *header = NULL;
  1659. (void) data;
  1660. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1661. conn = new_dir_conn();
  1662. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1663. GET("/tor/status-vote/next/" HEX1), NULL, 0));
  1664. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1665. NULL, NULL, 1, 0);
  1666. tt_assert(header);
  1667. tt_str_op(NOT_FOUND, OP_EQ, header);
  1668. done:
  1669. UNMOCK(connection_write_to_buf_impl_);
  1670. connection_free_minimal(TO_CONN(conn));
  1671. tor_free(header);
  1672. }
  1673. static void
  1674. status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
  1675. {
  1676. dir_connection_t *conn = NULL;
  1677. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1678. conn = new_dir_conn();
  1679. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1680. GET("/tor/status-vote/next/consensus"), NULL, 0));
  1681. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1682. body, body_used, 18, 0);
  1683. done:
  1684. UNMOCK(connection_write_to_buf_impl_);
  1685. connection_free_minimal(TO_CONN(conn));
  1686. }
  1687. static void
  1688. test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
  1689. {
  1690. char *header = NULL, *body = NULL;
  1691. size_t body_used;
  1692. (void) data;
  1693. status_vote_next_consensus_test(&header, &body, &body_used);
  1694. tt_assert(header);
  1695. tt_str_op(NOT_FOUND, OP_EQ, header);
  1696. done:
  1697. tor_free(header);
  1698. tor_free(body);
  1699. }
  1700. static void
  1701. test_dir_handle_get_status_vote_current_authority_not_found(void* data)
  1702. {
  1703. dir_connection_t *conn = NULL;
  1704. char *header = NULL;
  1705. (void) data;
  1706. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1707. conn = new_dir_conn();
  1708. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1709. GET("/tor/status-vote/current/authority"), NULL, 0));
  1710. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1711. NULL, NULL, 1, 0);
  1712. tt_assert(header);
  1713. tt_str_op(NOT_FOUND, OP_EQ, header);
  1714. done:
  1715. UNMOCK(connection_write_to_buf_impl_);
  1716. connection_free_minimal(TO_CONN(conn));
  1717. tor_free(header);
  1718. }
  1719. static void
  1720. test_dir_handle_get_status_vote_next_authority_not_found(void* data)
  1721. {
  1722. dir_connection_t *conn = NULL;
  1723. char *header = NULL;
  1724. (void) data;
  1725. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1726. conn = new_dir_conn();
  1727. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1728. GET("/tor/status-vote/next/authority"), NULL, 0));
  1729. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1730. NULL, NULL, 1, 0);
  1731. tt_assert(header);
  1732. tt_str_op(NOT_FOUND, OP_EQ, header);
  1733. done:
  1734. UNMOCK(connection_write_to_buf_impl_);
  1735. connection_free_minimal(TO_CONN(conn));
  1736. tor_free(header);
  1737. }
  1738. static void
  1739. test_dir_handle_get_status_vote_next_bandwidth_not_found(void* data)
  1740. {
  1741. dir_connection_t *conn = NULL;
  1742. char *header = NULL;
  1743. (void) data;
  1744. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1745. conn = new_dir_conn();
  1746. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1747. GET("/tor/status-vote/next/bandwdith"), NULL, 0));
  1748. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1749. NULL, NULL, 1, 0);
  1750. tt_assert(header);
  1751. tt_str_op(NOT_FOUND, OP_EQ, header);
  1752. done:
  1753. UNMOCK(connection_write_to_buf_impl_);
  1754. connection_free_minimal(TO_CONN(conn));
  1755. tor_free(header);
  1756. }
  1757. NS_DECL(const char*,
  1758. dirvote_get_pending_consensus, (consensus_flavor_t flav));
  1759. const char*
  1760. NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
  1761. {
  1762. (void)flav;
  1763. return "pending consensus";
  1764. }
  1765. static void
  1766. test_dir_handle_get_status_vote_next_consensus(void* data)
  1767. {
  1768. char *header = NULL, *body = NULL;
  1769. size_t body_used = 0;
  1770. (void) data;
  1771. NS_MOCK(dirvote_get_pending_consensus);
  1772. status_vote_next_consensus_test(&header, &body, &body_used);
  1773. tt_assert(header);
  1774. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1775. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1776. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1777. tt_assert(strstr(header, "Content-Length: 17\r\n"));
  1778. tt_str_op("pending consensus", OP_EQ, body);
  1779. done:
  1780. NS_UNMOCK(dirvote_get_pending_consensus);
  1781. tor_free(header);
  1782. tor_free(body);
  1783. }
  1784. static void
  1785. test_dir_handle_get_status_vote_next_consensus_busy(void* data)
  1786. {
  1787. char *header = NULL, *body = NULL;
  1788. size_t body_used = 0;
  1789. (void) data;
  1790. MOCK(get_options, mock_get_options);
  1791. NS_MOCK(dirvote_get_pending_consensus);
  1792. //Make it busy
  1793. init_mock_options();
  1794. mock_options->CountPrivateBandwidth = 1;
  1795. status_vote_next_consensus_test(&header, &body, &body_used);
  1796. tt_assert(header);
  1797. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1798. done:
  1799. NS_UNMOCK(dirvote_get_pending_consensus);
  1800. UNMOCK(get_options);
  1801. tor_free(header);
  1802. tor_free(body);
  1803. or_options_free(mock_options); mock_options = NULL;
  1804. }
  1805. static void
  1806. status_vote_next_consensus_signatures_test(char **header, char **body,
  1807. size_t *body_used)
  1808. {
  1809. dir_connection_t *conn = NULL;
  1810. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1811. conn = new_dir_conn();
  1812. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1813. GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
  1814. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1815. body, body_used, 22, 0);
  1816. done:
  1817. connection_free_minimal(TO_CONN(conn));
  1818. UNMOCK(connection_write_to_buf_impl_);
  1819. }
  1820. static void
  1821. test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
  1822. {
  1823. char *header = NULL, *body = NULL;
  1824. size_t body_used;
  1825. (void) data;
  1826. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1827. tt_assert(header);
  1828. tt_str_op(NOT_FOUND, OP_EQ, header);
  1829. done:
  1830. tor_free(header);
  1831. tor_free(body);
  1832. }
  1833. NS_DECL(const char*,
  1834. dirvote_get_pending_detached_signatures, (void));
  1835. const char*
  1836. NS(dirvote_get_pending_detached_signatures)(void)
  1837. {
  1838. return "pending detached sigs";
  1839. }
  1840. static void
  1841. test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
  1842. {
  1843. char *header = NULL, *body = NULL;
  1844. size_t body_used = 0;
  1845. (void) data;
  1846. NS_MOCK(dirvote_get_pending_detached_signatures);
  1847. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1848. tt_assert(header);
  1849. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1850. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1851. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1852. tt_assert(strstr(header, "Content-Length: 21\r\n"));
  1853. tt_str_op("pending detached sigs", OP_EQ, body);
  1854. done:
  1855. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1856. tor_free(header);
  1857. tor_free(body);
  1858. }
  1859. static void
  1860. test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
  1861. {
  1862. char *header = NULL, *body = NULL;
  1863. size_t body_used;
  1864. (void) data;
  1865. NS_MOCK(dirvote_get_pending_detached_signatures);
  1866. MOCK(get_options, mock_get_options);
  1867. //Make it busy
  1868. init_mock_options();
  1869. mock_options->CountPrivateBandwidth = 1;
  1870. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1871. tt_assert(header);
  1872. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1873. done:
  1874. UNMOCK(get_options);
  1875. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1876. tor_free(header);
  1877. tor_free(body);
  1878. or_options_free(mock_options); mock_options = NULL;
  1879. }
  1880. static void
  1881. test_dir_handle_get_status_vote_next_authority(void* data)
  1882. {
  1883. dir_connection_t *conn = NULL;
  1884. char *header = NULL, *body = NULL;
  1885. const char *msg_out = NULL;
  1886. int status_out = 0;
  1887. size_t body_used = 0;
  1888. dir_server_t *ds = NULL;
  1889. const char digest[DIGEST_LEN] = "";
  1890. (void) data;
  1891. clear_dir_servers();
  1892. routerlist_free_all();
  1893. dirvote_free_all();
  1894. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  1895. strlen(TEST_CERTIFICATE),
  1896. NULL);
  1897. /* create a trusted ds */
  1898. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1899. NULL, V3_DIRINFO, 1.0);
  1900. tt_assert(ds);
  1901. dir_server_add(ds);
  1902. /* ds v3_identity_digest is the certificate's identity_key */
  1903. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1904. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1905. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1906. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  1907. init_mock_options();
  1908. mock_options->AuthoritativeDir = 1;
  1909. mock_options->V3AuthoritativeDir = 1;
  1910. mock_options->TestingV3AuthVotingStartOffset = 0;
  1911. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1912. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1913. mock_options->TestingV3AuthInitialDistDelay = 1;
  1914. time_t now = 1441223455 -1;
  1915. voting_schedule_recalculate_timing(mock_options, now);
  1916. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1917. &status_out);
  1918. tt_assert(vote);
  1919. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1920. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1921. conn = new_dir_conn();
  1922. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1923. GET("/tor/status-vote/next/authority"), NULL, 0));
  1924. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1925. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1926. tt_assert(header);
  1927. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1928. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1929. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1930. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1931. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1932. done:
  1933. UNMOCK(connection_write_to_buf_impl_);
  1934. UNMOCK(get_my_v3_authority_cert);
  1935. connection_free_minimal(TO_CONN(conn));
  1936. tor_free(header);
  1937. tor_free(body);
  1938. authority_cert_free(mock_cert); mock_cert = NULL;
  1939. or_options_free(mock_options); mock_options = NULL;
  1940. clear_dir_servers();
  1941. routerlist_free_all();
  1942. dirvote_free_all();
  1943. }
  1944. static void
  1945. test_dir_handle_get_status_vote_next_bandwidth(void* data)
  1946. {
  1947. dir_connection_t *conn = NULL;
  1948. char *header = NULL, *body = NULL;
  1949. size_t body_used = 0;
  1950. (void) data;
  1951. const char *content =
  1952. "1541171221\n"
  1953. "node_id=$68A483E05A2ABDCA6DA5A3EF8DB5177638A27F80 "
  1954. "master_key_ed25519=YaqV4vbvPYKucElk297eVdNArDz9HtIwUoIeo0+cVIpQ "
  1955. "bw=760 nick=Test time=2018-05-08T16:13:26\n";
  1956. init_mock_options();
  1957. MOCK(get_options, mock_get_options);
  1958. mock_options->V3BandwidthsFile = tor_strdup(
  1959. get_fname_rnd("V3BandwidthsFile")
  1960. );
  1961. write_str_to_file(mock_options->V3BandwidthsFile, content, 0);
  1962. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1963. conn = new_dir_conn();
  1964. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1965. GET("/tor/status-vote/next/bandwidth"), NULL, 0));
  1966. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1967. &body, &body_used, strlen(content)+1, 0);
  1968. tt_assert(header);
  1969. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1970. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1971. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1972. tt_assert(strstr(header, "Content-Length: 167\r\n"));
  1973. /* Check cache lifetime */
  1974. char expbuf[RFC1123_TIME_LEN+1];
  1975. time_t now = approx_time();
  1976. /* BANDWIDTH_CACHE_LIFETIME is defined in dircache.c. */
  1977. format_rfc1123_time(expbuf, (time_t)(now + 30*60));
  1978. char *expires = NULL;
  1979. /* Change to 'Cache-control: max-age=%d' if using http/1.1. */
  1980. tor_asprintf(&expires, "Expires: %s\r\n", expbuf);
  1981. tt_assert(strstr(header, expires));
  1982. tt_int_op(body_used, OP_EQ, strlen(body));
  1983. tt_str_op(content, OP_EQ, body);
  1984. tor_free(header);
  1985. tor_free(body);
  1986. /* Request the file using compression, the result should be the same. */
  1987. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1988. GET("/tor/status-vote/next/bandwidth.z"), NULL, 0));
  1989. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1990. &body, &body_used, strlen(content)+1, 0);
  1991. tt_assert(header);
  1992. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1993. tt_assert(strstr(header, "Content-Encoding: deflate\r\n"));
  1994. /* Since using connection_write_to_buf_mock instead of mocking
  1995. * connection_buf_add_compress, the content is not actually compressed.
  1996. * If it would, the size and content would be different than the original.
  1997. */
  1998. done:
  1999. UNMOCK(get_options);
  2000. UNMOCK(connection_write_to_buf_impl_);
  2001. connection_free_minimal(TO_CONN(conn));
  2002. tor_free(header);
  2003. tor_free(body);
  2004. tor_free(expires);
  2005. or_options_free(mock_options);
  2006. }
  2007. static void
  2008. test_dir_handle_get_status_vote_current_authority(void* data)
  2009. {
  2010. dir_connection_t *conn = NULL;
  2011. char *header = NULL, *body = NULL;
  2012. const char *msg_out = NULL;
  2013. int status_out = 0;
  2014. size_t body_used = 0;
  2015. const char digest[DIGEST_LEN] = "";
  2016. dir_server_t *ds = NULL;
  2017. (void) data;
  2018. clear_dir_servers();
  2019. routerlist_free_all();
  2020. dirvote_free_all();
  2021. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE,
  2022. strlen(TEST_CERTIFICATE),
  2023. NULL);
  2024. /* create a trusted ds */
  2025. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  2026. NULL, V3_DIRINFO, 1.0);
  2027. tt_assert(ds);
  2028. dir_server_add(ds);
  2029. /* ds v3_identity_digest is the certificate's identity_key */
  2030. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  2031. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  2032. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  2033. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
  2034. init_mock_options();
  2035. mock_options->AuthoritativeDir = 1;
  2036. mock_options->V3AuthoritativeDir = 1;
  2037. mock_options->TestingV3AuthVotingStartOffset = 0;
  2038. mock_options->TestingV3AuthInitialVotingInterval = 1;
  2039. mock_options->TestingV3AuthInitialVoteDelay = 1;
  2040. mock_options->TestingV3AuthInitialDistDelay = 1;
  2041. time_t now = 1441223455;
  2042. voting_schedule_recalculate_timing(mock_options, now-1);
  2043. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  2044. &status_out);
  2045. tt_assert(vote);
  2046. // move the pending vote to previous vote
  2047. dirvote_act(mock_options, now+1);
  2048. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  2049. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  2050. conn = new_dir_conn();
  2051. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  2052. GET("/tor/status-vote/current/authority"), NULL, 0));
  2053. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  2054. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  2055. tt_assert(header);
  2056. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  2057. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  2058. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  2059. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  2060. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  2061. done:
  2062. UNMOCK(connection_write_to_buf_impl_);
  2063. UNMOCK(get_my_v3_authority_cert);
  2064. connection_free_minimal(TO_CONN(conn));
  2065. tor_free(header);
  2066. tor_free(body);
  2067. authority_cert_free(mock_cert); mock_cert = NULL;
  2068. or_options_free(mock_options); mock_options = NULL;
  2069. clear_dir_servers();
  2070. routerlist_free_all();
  2071. dirvote_free_all();
  2072. }
  2073. static void
  2074. test_dir_handle_get_parse_accept_encoding(void *arg)
  2075. {
  2076. (void)arg;
  2077. const unsigned B_NONE = 1u << NO_METHOD;
  2078. const unsigned B_ZLIB = 1u << ZLIB_METHOD;
  2079. const unsigned B_GZIP = 1u << GZIP_METHOD;
  2080. const unsigned B_LZMA = 1u << LZMA_METHOD;
  2081. const unsigned B_ZSTD = 1u << ZSTD_METHOD;
  2082. unsigned encodings;
  2083. encodings = parse_accept_encoding_header("");
  2084. tt_uint_op(B_NONE, OP_EQ, encodings);
  2085. encodings = parse_accept_encoding_header(" ");
  2086. tt_uint_op(B_NONE, OP_EQ, encodings);
  2087. encodings = parse_accept_encoding_header("dewey, cheatham, and howe ");
  2088. tt_uint_op(B_NONE, OP_EQ, encodings);
  2089. encodings = parse_accept_encoding_header("dewey, cheatham, and gzip");
  2090. tt_uint_op(B_NONE, OP_EQ, encodings);
  2091. encodings = parse_accept_encoding_header("dewey, cheatham, and, gzip");
  2092. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2093. encodings = parse_accept_encoding_header(" gzip");
  2094. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2095. encodings = parse_accept_encoding_header("gzip");
  2096. tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
  2097. encodings = parse_accept_encoding_header("x-zstd, deflate, x-tor-lzma");
  2098. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA, OP_EQ, encodings);
  2099. encodings = parse_accept_encoding_header(
  2100. "x-zstd, deflate, x-tor-lzma, gzip");
  2101. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
  2102. encodings = parse_accept_encoding_header("x-zstd,deflate,x-tor-lzma,gzip");
  2103. tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
  2104. done:
  2105. ;
  2106. }
  2107. #define DIR_HANDLE_CMD(name,flags) \
  2108. { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
  2109. struct testcase_t dir_handle_get_tests[] = {
  2110. DIR_HANDLE_CMD(not_found, 0),
  2111. DIR_HANDLE_CMD(bad_request, 0),
  2112. DIR_HANDLE_CMD(v1_command_not_found, 0),
  2113. DIR_HANDLE_CMD(v1_command, 0),
  2114. DIR_HANDLE_CMD(robots_txt, 0),
  2115. DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
  2116. DIR_HANDLE_CMD(rendezvous2_not_found, 0),
  2117. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
  2118. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
  2119. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
  2120. DIR_HANDLE_CMD(micro_d_not_found, 0),
  2121. DIR_HANDLE_CMD(micro_d_server_busy, 0),
  2122. DIR_HANDLE_CMD(micro_d, 0),
  2123. DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
  2124. DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
  2125. DIR_HANDLE_CMD(networkstatus_bridges, 0),
  2126. DIR_HANDLE_CMD(server_descriptors_not_found, 0),
  2127. DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
  2128. DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
  2129. DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
  2130. DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
  2131. DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
  2132. DIR_HANDLE_CMD(server_keys_bad_req, 0),
  2133. DIR_HANDLE_CMD(server_keys_busy, 0),
  2134. DIR_HANDLE_CMD(server_keys_all_not_found, 0),
  2135. DIR_HANDLE_CMD(server_keys_all, 0),
  2136. DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
  2137. DIR_HANDLE_CMD(server_keys_authority, 0),
  2138. DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
  2139. DIR_HANDLE_CMD(server_keys_fp, 0),
  2140. DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
  2141. DIR_HANDLE_CMD(server_keys_sk, 0),
  2142. DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
  2143. DIR_HANDLE_CMD(server_keys_fpsk, 0),
  2144. DIR_HANDLE_CMD(status_vote_current_not_found, 0),
  2145. DIR_HANDLE_CMD(status_vote_next_not_found, 0),
  2146. DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
  2147. DIR_HANDLE_CMD(status_vote_current_authority, 0),
  2148. DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
  2149. DIR_HANDLE_CMD(status_vote_next_authority, 0),
  2150. DIR_HANDLE_CMD(status_vote_next_bandwidth_not_found, 0),
  2151. DIR_HANDLE_CMD(status_vote_next_bandwidth, 0),
  2152. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, TT_FORK),
  2153. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, TT_FORK),
  2154. DIR_HANDLE_CMD(status_vote_current_consensus_too_old, TT_FORK),
  2155. DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, TT_FORK),
  2156. DIR_HANDLE_CMD(status_vote_current_consensus_ns, TT_FORK),
  2157. DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
  2158. DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
  2159. DIR_HANDLE_CMD(status_vote_d, 0),
  2160. DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
  2161. DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
  2162. DIR_HANDLE_CMD(status_vote_next_consensus, 0),
  2163. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
  2164. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
  2165. DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
  2166. DIR_HANDLE_CMD(parse_accept_encoding, 0),
  2167. END_OF_TESTCASES
  2168. };