test_dir_handle_get.c 73 KB

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