test_dir_handle_get.c 72 KB

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