test_dir_handle_get.c 74 KB

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