test_dir_handle_get.c 73 KB

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