test_dir_handle_get.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492
  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. sprintf(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. sprintf(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. sprintf(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. sprintf(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. sprintf(req, SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2), hex_digest);
  755. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  756. //TODO: Is this a BUG?
  757. //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
  758. //is smaller than that by annotation_len bytes
  759. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  760. &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
  761. tt_assert(header);
  762. tt_assert(body);
  763. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  764. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  765. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  766. tt_int_op(body_used, OP_EQ, strlen(body));
  767. tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
  768. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  769. done:
  770. NS_UNMOCK(router_get_my_routerinfo);
  771. UNMOCK(connection_write_to_buf_impl_);
  772. tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
  773. tor_free(mock_routerinfo);
  774. connection_free_(TO_CONN(conn));
  775. tor_free(header);
  776. tor_free(body);
  777. crypto_pk_free(identity_pkey);
  778. }
  779. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  780. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  781. static void
  782. test_dir_handle_get_server_descriptors_d(void* data)
  783. {
  784. dir_connection_t *conn = NULL;
  785. char *header = NULL;
  786. char *body = NULL;
  787. size_t body_used = 0;
  788. crypto_pk_t *identity_pkey = pk_generate(0);
  789. (void) data;
  790. /* Setup fake routerlist. */
  791. helper_setup_fake_routerlist();
  792. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  793. /* Get one router's signed_descriptor_digest */
  794. routerlist_t *our_routerlist = router_get_routerlist();
  795. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  796. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  797. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  798. DIGEST_LEN);
  799. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  800. char req_header[155];
  801. sprintf(req_header, SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  802. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  803. //TODO: Is this a BUG?
  804. //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
  805. //which is smaller than that by annotation_len bytes
  806. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  807. &body, &body_used,
  808. router->cache_info.signed_descriptor_len+1, 0);
  809. tt_assert(header);
  810. tt_assert(body);
  811. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  812. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  813. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  814. //TODO: Is this a BUG?
  815. //This is what should be expected:
  816. //tt_int_op(body_used, OP_EQ, strlen(body));
  817. tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
  818. tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
  819. router->cache_info.annotations_len);
  820. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  821. done:
  822. UNMOCK(connection_write_to_buf_impl_);
  823. tor_free(mock_routerinfo);
  824. connection_free_(TO_CONN(conn));
  825. tor_free(header);
  826. tor_free(body);
  827. crypto_pk_free(identity_pkey);
  828. routerlist_free_all();
  829. nodelist_free_all();
  830. entry_guards_free_all();
  831. }
  832. static void
  833. test_dir_handle_get_server_descriptors_busy(void* data)
  834. {
  835. dir_connection_t *conn = NULL;
  836. char *header = NULL;
  837. crypto_pk_t *identity_pkey = pk_generate(0);
  838. (void) data;
  839. /* Setup fake routerlist. */
  840. helper_setup_fake_routerlist();
  841. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  842. //Make it busy
  843. MOCK(get_options, mock_get_options);
  844. init_mock_options();
  845. mock_options->CountPrivateBandwidth = 1;
  846. /* Get one router's signed_descriptor_digest */
  847. routerlist_t *our_routerlist = router_get_routerlist();
  848. tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
  849. routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
  850. const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
  851. DIGEST_LEN);
  852. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  853. #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
  854. #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
  855. char req_header[155];
  856. sprintf(req_header, SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
  857. tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
  858. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  859. NULL, NULL, 1, 0);
  860. tt_assert(header);
  861. tt_str_op(SERVER_BUSY, OP_EQ, header);
  862. tt_int_op(conn->dir_spool_src, OP_EQ, DIR_SPOOL_NONE);
  863. done:
  864. UNMOCK(get_options);
  865. UNMOCK(connection_write_to_buf_impl_);
  866. tor_free(mock_routerinfo);
  867. connection_free_(TO_CONN(conn));
  868. tor_free(header);
  869. crypto_pk_free(identity_pkey);
  870. routerlist_free_all();
  871. nodelist_free_all();
  872. entry_guards_free_all();
  873. }
  874. static void
  875. test_dir_handle_get_server_keys_bad_req(void* data)
  876. {
  877. dir_connection_t *conn = NULL;
  878. char *header = NULL;
  879. (void) data;
  880. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  881. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  882. const char *req = GET("/tor/keys/");
  883. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  884. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  885. NULL, NULL, 1, 0);
  886. tt_assert(header);
  887. tt_str_op(BAD_REQUEST, OP_EQ, header);
  888. done:
  889. UNMOCK(connection_write_to_buf_impl_);
  890. connection_free_(TO_CONN(conn));
  891. tor_free(header);
  892. }
  893. static void
  894. test_dir_handle_get_server_keys_all_not_found(void* data)
  895. {
  896. dir_connection_t *conn = NULL;
  897. char *header = NULL;
  898. (void) data;
  899. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  900. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  901. const char *req = GET("/tor/keys/all");
  902. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  903. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  904. NULL, NULL, 1, 0);
  905. tt_assert(header);
  906. tt_str_op(NOT_FOUND, OP_EQ, header);
  907. done:
  908. UNMOCK(connection_write_to_buf_impl_);
  909. connection_free_(TO_CONN(conn));
  910. tor_free(header);
  911. }
  912. #define TEST_CERTIFICATE AUTHORITY_CERT_3
  913. #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
  914. extern const char AUTHORITY_CERT_3[];
  915. extern const char AUTHORITY_SIGNKEY_A_DIGEST[];
  916. static const char TEST_CERT_IDENT_KEY[] =
  917. "D867ACF56A9D229B35C25F0090BC9867E906BE69";
  918. static void
  919. test_dir_handle_get_server_keys_all(void* data)
  920. {
  921. dir_connection_t *conn = NULL;
  922. char *header = NULL;
  923. char *body = NULL;
  924. size_t body_used = 0;
  925. const char digest[DIGEST_LEN] = "";
  926. dir_server_t *ds = NULL;
  927. (void) data;
  928. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  929. clear_dir_servers();
  930. routerlist_free_all();
  931. /* create a trusted ds */
  932. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  933. NULL, V3_DIRINFO, 1.0);
  934. tt_assert(ds);
  935. dir_server_add(ds);
  936. /* ds v3_identity_digest is the certificate's identity_key */
  937. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  938. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  939. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  940. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  941. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  942. const char *req = GET("/tor/keys/all");
  943. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  944. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  945. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  946. tt_assert(header);
  947. tt_assert(body);
  948. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  949. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  950. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  951. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  952. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  953. done:
  954. UNMOCK(connection_write_to_buf_impl_);
  955. connection_free_(TO_CONN(conn));
  956. tor_free(header);
  957. tor_free(body);
  958. clear_dir_servers();
  959. routerlist_free_all();
  960. }
  961. static void
  962. test_dir_handle_get_server_keys_authority_not_found(void* data)
  963. {
  964. dir_connection_t *conn = NULL;
  965. char *header = NULL;
  966. (void) data;
  967. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  968. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  969. const char *req = GET("/tor/keys/authority");
  970. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  971. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  972. NULL, NULL, 1, 0);
  973. tt_assert(header);
  974. tt_str_op(NOT_FOUND, OP_EQ, header);
  975. done:
  976. UNMOCK(connection_write_to_buf_impl_);
  977. connection_free_(TO_CONN(conn));
  978. tor_free(header);
  979. }
  980. static authority_cert_t * mock_cert = NULL;
  981. static authority_cert_t *
  982. get_my_v3_authority_cert_m(void)
  983. {
  984. tor_assert(mock_cert);
  985. return mock_cert;
  986. }
  987. static void
  988. test_dir_handle_get_server_keys_authority(void* data)
  989. {
  990. dir_connection_t *conn = NULL;
  991. char *header = NULL;
  992. char *body = NULL;
  993. size_t body_used = 0;
  994. (void) data;
  995. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  996. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  997. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  998. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  999. const char *req = GET("/tor/keys/authority");
  1000. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1001. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1002. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1003. tt_assert(header);
  1004. tt_assert(body);
  1005. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1006. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1007. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1008. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1009. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1010. done:
  1011. UNMOCK(get_my_v3_authority_cert);
  1012. UNMOCK(connection_write_to_buf_impl_);
  1013. connection_free_(TO_CONN(conn));
  1014. tor_free(header);
  1015. tor_free(body);
  1016. authority_cert_free(mock_cert); mock_cert = NULL;
  1017. }
  1018. static void
  1019. test_dir_handle_get_server_keys_fp_not_found(void* data)
  1020. {
  1021. dir_connection_t *conn = NULL;
  1022. char *header = NULL;
  1023. (void) data;
  1024. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1025. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1026. const char *req = GET("/tor/keys/fp/somehex");
  1027. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1028. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1029. NULL, NULL, 1, 0);
  1030. tt_assert(header);
  1031. tt_str_op(NOT_FOUND, OP_EQ, header);
  1032. done:
  1033. UNMOCK(connection_write_to_buf_impl_);
  1034. connection_free_(TO_CONN(conn));
  1035. tor_free(header);
  1036. }
  1037. static void
  1038. test_dir_handle_get_server_keys_fp(void* data)
  1039. {
  1040. dir_connection_t *conn = NULL;
  1041. char *header = NULL;
  1042. char *body = NULL;
  1043. size_t body_used = 0;
  1044. dir_server_t *ds = NULL;
  1045. const char digest[DIGEST_LEN] = "";
  1046. (void) data;
  1047. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1048. clear_dir_servers();
  1049. routerlist_free_all();
  1050. /* create a trusted ds */
  1051. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1052. NULL, V3_DIRINFO, 1.0);
  1053. tt_assert(ds);
  1054. dir_server_add(ds);
  1055. /* ds v3_identity_digest is the certificate's identity_key */
  1056. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1057. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1058. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1059. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1060. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1061. char req[71];
  1062. sprintf(req, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1063. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1064. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1065. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1066. tt_assert(header);
  1067. tt_assert(body);
  1068. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1069. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1070. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1071. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1072. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1073. done:
  1074. UNMOCK(connection_write_to_buf_impl_);
  1075. connection_free_(TO_CONN(conn));
  1076. tor_free(header);
  1077. tor_free(body);
  1078. clear_dir_servers();
  1079. routerlist_free_all();
  1080. }
  1081. static void
  1082. test_dir_handle_get_server_keys_sk_not_found(void* data)
  1083. {
  1084. dir_connection_t *conn = NULL;
  1085. char *header = NULL;
  1086. (void) data;
  1087. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1088. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1089. const char *req = GET("/tor/keys/sk/somehex");
  1090. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1091. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1092. NULL, NULL, 1, 0);
  1093. tt_assert(header);
  1094. tt_str_op(NOT_FOUND, OP_EQ, header);
  1095. done:
  1096. UNMOCK(connection_write_to_buf_impl_);
  1097. connection_free_(TO_CONN(conn));
  1098. tor_free(header);
  1099. }
  1100. static void
  1101. test_dir_handle_get_server_keys_sk(void* data)
  1102. {
  1103. dir_connection_t *conn = NULL;
  1104. char *header = NULL;
  1105. char *body = NULL;
  1106. size_t body_used = 0;
  1107. (void) data;
  1108. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1109. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1110. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1111. clear_dir_servers();
  1112. routerlist_free_all();
  1113. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1114. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1115. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1116. char req[71];
  1117. sprintf(req, GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
  1118. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1119. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1120. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1121. tt_assert(header);
  1122. tt_assert(body);
  1123. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1124. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1125. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1126. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1127. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1128. done:
  1129. UNMOCK(get_my_v3_authority_cert);
  1130. UNMOCK(connection_write_to_buf_impl_);
  1131. connection_free_(TO_CONN(conn));
  1132. authority_cert_free(mock_cert); mock_cert = NULL;
  1133. tor_free(header);
  1134. tor_free(body);
  1135. }
  1136. static void
  1137. test_dir_handle_get_server_keys_fpsk_not_found(void* data)
  1138. {
  1139. dir_connection_t *conn = NULL;
  1140. char *header = NULL;
  1141. (void) data;
  1142. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1143. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1144. const char *req = GET("/tor/keys/fp-sk/somehex");
  1145. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1146. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1147. NULL, NULL, 1, 0);
  1148. tt_assert(header);
  1149. tt_str_op(NOT_FOUND, OP_EQ, header);
  1150. done:
  1151. UNMOCK(connection_write_to_buf_impl_);
  1152. connection_free_(TO_CONN(conn));
  1153. tor_free(header);
  1154. }
  1155. static void
  1156. test_dir_handle_get_server_keys_fpsk(void* data)
  1157. {
  1158. dir_connection_t *conn = NULL;
  1159. char *header = NULL;
  1160. char *body = NULL;
  1161. size_t body_used = 0;
  1162. dir_server_t *ds = NULL;
  1163. const char digest[DIGEST_LEN] = "";
  1164. (void) data;
  1165. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1166. clear_dir_servers();
  1167. routerlist_free_all();
  1168. /* create a trusted ds */
  1169. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1170. NULL, V3_DIRINFO, 1.0);
  1171. tt_assert(ds);
  1172. /* ds v3_identity_digest is the certificate's identity_key */
  1173. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1174. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1175. dir_server_add(ds);
  1176. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1177. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1178. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1179. char req[115];
  1180. sprintf(req, GET("/tor/keys/fp-sk/%s-%s"),
  1181. TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
  1182. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1183. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1184. &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
  1185. tt_assert(header);
  1186. tt_assert(body);
  1187. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1188. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1189. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1190. tt_assert(strstr(header, "Content-Length: 1883\r\n"));
  1191. tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
  1192. done:
  1193. UNMOCK(connection_write_to_buf_impl_);
  1194. connection_free_(TO_CONN(conn));
  1195. tor_free(header);
  1196. tor_free(body);
  1197. clear_dir_servers();
  1198. routerlist_free_all();
  1199. }
  1200. static void
  1201. test_dir_handle_get_server_keys_busy(void* data)
  1202. {
  1203. dir_connection_t *conn = NULL;
  1204. char *header = NULL;
  1205. dir_server_t *ds = NULL;
  1206. const char digest[DIGEST_LEN] = "";
  1207. (void) data;
  1208. clear_dir_servers();
  1209. routerlist_free_all();
  1210. /* create a trusted ds */
  1211. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1212. NULL, V3_DIRINFO, 1.0);
  1213. tt_assert(ds);
  1214. /* ds v3_identity_digest is the certificate's identity_key */
  1215. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1216. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1217. dir_server_add(ds);
  1218. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1219. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1220. MOCK(get_options, mock_get_options);
  1221. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1222. /* setup busy server */
  1223. init_mock_options();
  1224. mock_options->CountPrivateBandwidth = 1;
  1225. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1226. char req[71];
  1227. sprintf(req, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
  1228. tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
  1229. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1230. NULL, NULL, 1, 0);
  1231. tt_assert(header);
  1232. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1233. done:
  1234. UNMOCK(get_options);
  1235. UNMOCK(connection_write_to_buf_impl_);
  1236. connection_free_(TO_CONN(conn));
  1237. tor_free(header);
  1238. or_options_free(mock_options); mock_options = NULL;
  1239. clear_dir_servers();
  1240. routerlist_free_all();
  1241. }
  1242. static networkstatus_t *mock_ns_val = NULL;
  1243. static networkstatus_t *
  1244. mock_ns_get_by_flavor(consensus_flavor_t f)
  1245. {
  1246. (void)f;
  1247. return mock_ns_val;
  1248. }
  1249. static void
  1250. test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
  1251. {
  1252. dir_connection_t *conn = NULL;
  1253. char *header = NULL;
  1254. char *stats = NULL;
  1255. (void) d;
  1256. /* init mock */
  1257. mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
  1258. mock_ns_val->flavor = FLAV_NS;
  1259. mock_ns_val->voters = smartlist_new();
  1260. /* init mock */
  1261. init_mock_options();
  1262. MOCK(get_options, mock_get_options);
  1263. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1264. MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
  1265. /* start gathering stats */
  1266. mock_options->DirReqStatistics = 1;
  1267. geoip_dirreq_stats_init(time(NULL));
  1268. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1269. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1270. GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
  1271. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1272. NULL, NULL, 1, 0);
  1273. tt_assert(header);
  1274. tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
  1275. stats = geoip_format_dirreq_stats(time(NULL));
  1276. tt_assert(stats);
  1277. tt_assert(strstr(stats, "not-enough-sigs=8"));
  1278. done:
  1279. UNMOCK(networkstatus_get_latest_consensus_by_flavor);
  1280. UNMOCK(connection_write_to_buf_impl_);
  1281. UNMOCK(get_options);
  1282. connection_free_(TO_CONN(conn));
  1283. tor_free(header);
  1284. tor_free(stats);
  1285. smartlist_free(mock_ns_val->voters);
  1286. tor_free(mock_ns_val);
  1287. or_options_free(mock_options); mock_options = NULL;
  1288. }
  1289. static void
  1290. test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
  1291. {
  1292. dir_connection_t *conn = NULL;
  1293. char *header = NULL;
  1294. char *stats = NULL;
  1295. (void) data;
  1296. init_mock_options();
  1297. MOCK(get_options, mock_get_options);
  1298. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1299. /* start gathering stats */
  1300. mock_options->DirReqStatistics = 1;
  1301. geoip_dirreq_stats_init(time(NULL));
  1302. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1303. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1304. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1305. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1306. NULL, NULL, 1, 0);
  1307. tt_assert(header);
  1308. tt_str_op(NOT_FOUND, OP_EQ, header);
  1309. stats = geoip_format_dirreq_stats(time(NULL));
  1310. tt_assert(stats);
  1311. tt_assert(strstr(stats, "not-found=8"));
  1312. done:
  1313. UNMOCK(connection_write_to_buf_impl_);
  1314. UNMOCK(get_options);
  1315. connection_free_(TO_CONN(conn));
  1316. tor_free(header);
  1317. tor_free(stats);
  1318. or_options_free(mock_options); mock_options = NULL;
  1319. }
  1320. NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
  1321. int
  1322. NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
  1323. {
  1324. (void)addr;
  1325. CALLED(geoip_get_country_by_addr)++;
  1326. return 1;
  1327. }
  1328. static void
  1329. status_vote_current_consensus_ns_test(char **header, char **body,
  1330. size_t *body_len)
  1331. {
  1332. common_digests_t digests;
  1333. dir_connection_t *conn = NULL;
  1334. #define NETWORK_STATUS "some network status string"
  1335. dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
  1336. time(NULL));
  1337. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1338. tt_assert(mock_options);
  1339. mock_options->DirReqStatistics = 1;
  1340. geoip_dirreq_stats_init(time(NULL));
  1341. /* init geoip database */
  1342. geoip_parse_entry("10,50,AB", AF_INET);
  1343. tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
  1344. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1345. TO_CONN(conn)->address = tor_strdup("127.0.0.1");
  1346. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1347. GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
  1348. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1349. body, body_len, strlen(NETWORK_STATUS)+7, 0);
  1350. done:
  1351. UNMOCK(connection_write_to_buf_impl_);
  1352. connection_free_(TO_CONN(conn));
  1353. }
  1354. static void
  1355. test_dir_handle_get_status_vote_current_consensus_ns(void* data)
  1356. {
  1357. char *header = NULL;
  1358. char *body = NULL, *comp_body = NULL;
  1359. size_t body_used = 0, comp_body_used = 0;
  1360. char *stats = NULL, *hist = NULL;
  1361. (void) data;
  1362. dirserv_free_all();
  1363. clear_geoip_db();
  1364. NS_MOCK(geoip_get_country_by_addr);
  1365. MOCK(get_options, mock_get_options);
  1366. init_mock_options();
  1367. status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
  1368. tt_assert(header);
  1369. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1370. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1371. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1372. tt_assert(strstr(header, "Pragma: no-cache\r\n"));
  1373. compress_method_t compression = detect_compression_method(comp_body,
  1374. comp_body_used);
  1375. tt_int_op(ZLIB_METHOD, OP_EQ, compression);
  1376. tor_gzip_uncompress(&body, &body_used, comp_body, comp_body_used,
  1377. compression, 0, LOG_PROTOCOL_WARN);
  1378. tt_str_op(NETWORK_STATUS, OP_EQ, body);
  1379. tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
  1380. stats = geoip_format_dirreq_stats(time(NULL));
  1381. tt_assert(stats);
  1382. tt_assert(strstr(stats, "ok=8"));
  1383. tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
  1384. tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
  1385. tt_assert(strstr(stats, "dirreq-v3-direct-dl"
  1386. " complete=0,timeout=0,running=4"));
  1387. hist = geoip_get_request_history();
  1388. tt_assert(hist);
  1389. tt_str_op("ab=8", OP_EQ, hist);
  1390. done:
  1391. NS_UNMOCK(geoip_get_country_by_addr);
  1392. UNMOCK(get_options);
  1393. tor_free(header);
  1394. tor_free(comp_body);
  1395. tor_free(body);
  1396. tor_free(stats);
  1397. tor_free(hist);
  1398. or_options_free(mock_options); mock_options = NULL;
  1399. dirserv_free_all();
  1400. clear_geoip_db();
  1401. }
  1402. static void
  1403. test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
  1404. {
  1405. char *header = NULL;
  1406. char *body = NULL;
  1407. size_t body_used = 0;
  1408. char *stats = NULL;
  1409. (void) data;
  1410. dirserv_free_all();
  1411. clear_geoip_db();
  1412. MOCK(get_options, mock_get_options);
  1413. // Make it busy
  1414. init_mock_options();
  1415. mock_options->CountPrivateBandwidth = 1;
  1416. status_vote_current_consensus_ns_test(&header, &body, &body_used);
  1417. tt_assert(header);
  1418. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1419. stats = geoip_format_dirreq_stats(time(NULL));
  1420. tt_assert(stats);
  1421. tt_assert(strstr(stats, "busy=8"));
  1422. done:
  1423. UNMOCK(get_options);
  1424. tor_free(header);
  1425. tor_free(body);
  1426. or_options_free(mock_options); mock_options = NULL;
  1427. tor_free(stats);
  1428. dirserv_free_all();
  1429. clear_geoip_db();
  1430. }
  1431. static void
  1432. test_dir_handle_get_status_vote_current_not_found(void* data)
  1433. {
  1434. dir_connection_t *conn = NULL;
  1435. char *header = NULL;
  1436. (void) data;
  1437. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1438. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1439. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1440. GET("/tor/status-vote/current/" HEX1), NULL, 0));
  1441. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1442. NULL, NULL, 1, 0);
  1443. tt_assert(header);
  1444. tt_str_op(NOT_FOUND, OP_EQ, header);
  1445. done:
  1446. UNMOCK(connection_write_to_buf_impl_);
  1447. connection_free_(TO_CONN(conn));
  1448. tor_free(header);
  1449. }
  1450. #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
  1451. static void
  1452. status_vote_current_d_test(char **header, char **body, size_t *body_l)
  1453. {
  1454. dir_connection_t *conn = NULL;
  1455. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1456. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1457. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1458. GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
  1459. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1460. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1461. tt_assert(header);
  1462. done:
  1463. UNMOCK(connection_write_to_buf_impl_);
  1464. connection_free_(TO_CONN(conn));
  1465. }
  1466. static void
  1467. status_vote_next_d_test(char **header, char **body, size_t *body_l)
  1468. {
  1469. dir_connection_t *conn = NULL;
  1470. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1471. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1472. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1473. GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
  1474. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1475. body, body_l, strlen(VOTE_BODY_V3)+1, 0);
  1476. tt_assert(header);
  1477. done:
  1478. UNMOCK(connection_write_to_buf_impl_);
  1479. connection_free_(TO_CONN(conn));
  1480. }
  1481. static void
  1482. test_dir_handle_get_status_vote_current_d_not_found(void* data)
  1483. {
  1484. char *header = NULL;
  1485. (void) data;
  1486. status_vote_current_d_test(&header, NULL, NULL);
  1487. tt_assert(header);
  1488. tt_str_op(NOT_FOUND, OP_EQ, header);
  1489. done:
  1490. tor_free(header);
  1491. }
  1492. static void
  1493. test_dir_handle_get_status_vote_next_d_not_found(void* data)
  1494. {
  1495. char *header = NULL;
  1496. (void) data;
  1497. status_vote_next_d_test(&header, NULL, NULL);
  1498. tt_assert(header);
  1499. tt_str_op(NOT_FOUND, OP_EQ, header);
  1500. done:
  1501. UNMOCK(connection_write_to_buf_impl_);
  1502. tor_free(header);
  1503. }
  1504. static void
  1505. test_dir_handle_get_status_vote_d(void* data)
  1506. {
  1507. char *header = NULL, *body = NULL;
  1508. size_t body_used = 0;
  1509. dir_server_t *ds = NULL;
  1510. const char digest[DIGEST_LEN] = "";
  1511. (void) data;
  1512. clear_dir_servers();
  1513. dirvote_free_all();
  1514. /* create a trusted ds */
  1515. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1516. NULL, V3_DIRINFO, 1.0);
  1517. tt_assert(ds);
  1518. dir_server_add(ds);
  1519. /* ds v3_identity_digest is the certificate's identity_key */
  1520. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1521. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1522. init_mock_options();
  1523. mock_options->AuthoritativeDir = 1;
  1524. mock_options->V3AuthoritativeDir = 1;
  1525. mock_options->TestingV3AuthVotingStartOffset = 0;
  1526. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1527. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1528. mock_options->TestingV3AuthInitialDistDelay = 1;
  1529. time_t now = 1441223455 -1;
  1530. dirvote_recalculate_timing(mock_options, now);
  1531. const char *msg_out = NULL;
  1532. int status_out = 0;
  1533. struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1534. &status_out);
  1535. tt_assert(pv);
  1536. status_vote_current_d_test(&header, &body, &body_used);
  1537. tt_assert(header);
  1538. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1539. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1540. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1541. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1542. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1543. tor_free(header);
  1544. tor_free(body);
  1545. status_vote_next_d_test(&header, &body, &body_used);
  1546. tt_assert(header);
  1547. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1548. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1549. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1550. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1551. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1552. done:
  1553. tor_free(header);
  1554. tor_free(body);
  1555. or_options_free(mock_options); mock_options = NULL;
  1556. clear_dir_servers();
  1557. dirvote_free_all();
  1558. }
  1559. static void
  1560. test_dir_handle_get_status_vote_next_not_found(void* data)
  1561. {
  1562. dir_connection_t *conn = NULL;
  1563. char *header = NULL;
  1564. (void) data;
  1565. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1566. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1567. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1568. GET("/tor/status-vote/next/" HEX1), NULL, 0));
  1569. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1570. NULL, NULL, 1, 0);
  1571. tt_assert(header);
  1572. tt_str_op(NOT_FOUND, OP_EQ, header);
  1573. done:
  1574. UNMOCK(connection_write_to_buf_impl_);
  1575. connection_free_(TO_CONN(conn));
  1576. tor_free(header);
  1577. }
  1578. static void
  1579. status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
  1580. {
  1581. dir_connection_t *conn = NULL;
  1582. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1583. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1584. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1585. GET("/tor/status-vote/next/consensus"), NULL, 0));
  1586. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1587. body, body_used, 18, 0);
  1588. done:
  1589. UNMOCK(connection_write_to_buf_impl_);
  1590. connection_free_(TO_CONN(conn));
  1591. }
  1592. static void
  1593. test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
  1594. {
  1595. char *header = NULL, *body = NULL;
  1596. size_t body_used;
  1597. (void) data;
  1598. status_vote_next_consensus_test(&header, &body, &body_used);
  1599. tt_assert(header);
  1600. tt_str_op(NOT_FOUND, OP_EQ, header);
  1601. done:
  1602. tor_free(header);
  1603. tor_free(body);
  1604. }
  1605. static void
  1606. test_dir_handle_get_status_vote_current_authority_not_found(void* data)
  1607. {
  1608. dir_connection_t *conn = NULL;
  1609. char *header = NULL;
  1610. (void) data;
  1611. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1612. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1613. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1614. GET("/tor/status-vote/current/authority"), NULL, 0));
  1615. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1616. NULL, NULL, 1, 0);
  1617. tt_assert(header);
  1618. tt_str_op(NOT_FOUND, OP_EQ, header);
  1619. done:
  1620. UNMOCK(connection_write_to_buf_impl_);
  1621. connection_free_(TO_CONN(conn));
  1622. tor_free(header);
  1623. }
  1624. static void
  1625. test_dir_handle_get_status_vote_next_authority_not_found(void* data)
  1626. {
  1627. dir_connection_t *conn = NULL;
  1628. char *header = NULL;
  1629. (void) data;
  1630. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1631. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1632. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1633. GET("/tor/status-vote/next/authority"), NULL, 0));
  1634. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1635. NULL, NULL, 1, 0);
  1636. tt_assert(header);
  1637. tt_str_op(NOT_FOUND, OP_EQ, header);
  1638. done:
  1639. UNMOCK(connection_write_to_buf_impl_);
  1640. connection_free_(TO_CONN(conn));
  1641. tor_free(header);
  1642. }
  1643. NS_DECL(const char*,
  1644. dirvote_get_pending_consensus, (consensus_flavor_t flav));
  1645. const char*
  1646. NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
  1647. {
  1648. (void)flav;
  1649. return "pending consensus";
  1650. }
  1651. static void
  1652. test_dir_handle_get_status_vote_next_consensus(void* data)
  1653. {
  1654. char *header = NULL, *body = NULL;
  1655. size_t body_used = 0;
  1656. (void) data;
  1657. NS_MOCK(dirvote_get_pending_consensus);
  1658. status_vote_next_consensus_test(&header, &body, &body_used);
  1659. tt_assert(header);
  1660. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1661. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1662. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1663. tt_assert(strstr(header, "Content-Length: 17\r\n"));
  1664. tt_str_op("pending consensus", OP_EQ, body);
  1665. done:
  1666. NS_UNMOCK(dirvote_get_pending_consensus);
  1667. tor_free(header);
  1668. tor_free(body);
  1669. }
  1670. static void
  1671. test_dir_handle_get_status_vote_next_consensus_busy(void* data)
  1672. {
  1673. char *header = NULL, *body = NULL;
  1674. size_t body_used = 0;
  1675. (void) data;
  1676. MOCK(get_options, mock_get_options);
  1677. NS_MOCK(dirvote_get_pending_consensus);
  1678. //Make it busy
  1679. init_mock_options();
  1680. mock_options->CountPrivateBandwidth = 1;
  1681. status_vote_next_consensus_test(&header, &body, &body_used);
  1682. tt_assert(header);
  1683. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1684. done:
  1685. NS_UNMOCK(dirvote_get_pending_consensus);
  1686. UNMOCK(get_options);
  1687. tor_free(header);
  1688. tor_free(body);
  1689. or_options_free(mock_options); mock_options = NULL;
  1690. }
  1691. static void
  1692. status_vote_next_consensus_signatures_test(char **header, char **body,
  1693. size_t *body_used)
  1694. {
  1695. dir_connection_t *conn = NULL;
  1696. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1697. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1698. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1699. GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
  1700. fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
  1701. body, body_used, 22, 0);
  1702. done:
  1703. connection_free_(TO_CONN(conn));
  1704. UNMOCK(connection_write_to_buf_impl_);
  1705. }
  1706. static void
  1707. test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
  1708. {
  1709. char *header = NULL, *body = NULL;
  1710. size_t body_used;
  1711. (void) data;
  1712. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1713. tt_assert(header);
  1714. tt_str_op(NOT_FOUND, OP_EQ, header);
  1715. done:
  1716. tor_free(header);
  1717. tor_free(body);
  1718. }
  1719. NS_DECL(const char*,
  1720. dirvote_get_pending_detached_signatures, (void));
  1721. const char*
  1722. NS(dirvote_get_pending_detached_signatures)(void)
  1723. {
  1724. return "pending detached sigs";
  1725. }
  1726. static void
  1727. test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
  1728. {
  1729. char *header = NULL, *body = NULL;
  1730. size_t body_used = 0;
  1731. (void) data;
  1732. NS_MOCK(dirvote_get_pending_detached_signatures);
  1733. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1734. tt_assert(header);
  1735. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1736. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1737. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1738. tt_assert(strstr(header, "Content-Length: 21\r\n"));
  1739. tt_str_op("pending detached sigs", OP_EQ, body);
  1740. done:
  1741. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1742. tor_free(header);
  1743. tor_free(body);
  1744. }
  1745. static void
  1746. test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
  1747. {
  1748. char *header = NULL, *body = NULL;
  1749. size_t body_used;
  1750. (void) data;
  1751. NS_MOCK(dirvote_get_pending_detached_signatures);
  1752. MOCK(get_options, mock_get_options);
  1753. //Make it busy
  1754. init_mock_options();
  1755. mock_options->CountPrivateBandwidth = 1;
  1756. status_vote_next_consensus_signatures_test(&header, &body, &body_used);
  1757. tt_assert(header);
  1758. tt_str_op(SERVER_BUSY, OP_EQ, header);
  1759. done:
  1760. UNMOCK(get_options);
  1761. NS_UNMOCK(dirvote_get_pending_detached_signatures);
  1762. tor_free(header);
  1763. tor_free(body);
  1764. or_options_free(mock_options); mock_options = NULL;
  1765. }
  1766. static void
  1767. test_dir_handle_get_status_vote_next_authority(void* data)
  1768. {
  1769. dir_connection_t *conn = NULL;
  1770. char *header = NULL, *body = NULL;
  1771. const char *msg_out = NULL;
  1772. int status_out = 0;
  1773. size_t body_used = 0;
  1774. dir_server_t *ds = NULL;
  1775. const char digest[DIGEST_LEN] = "";
  1776. (void) data;
  1777. clear_dir_servers();
  1778. routerlist_free_all();
  1779. dirvote_free_all();
  1780. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1781. /* create a trusted ds */
  1782. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1783. NULL, V3_DIRINFO, 1.0);
  1784. tt_assert(ds);
  1785. dir_server_add(ds);
  1786. /* ds v3_identity_digest is the certificate's identity_key */
  1787. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1788. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1789. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1790. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1791. init_mock_options();
  1792. mock_options->AuthoritativeDir = 1;
  1793. mock_options->V3AuthoritativeDir = 1;
  1794. mock_options->TestingV3AuthVotingStartOffset = 0;
  1795. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1796. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1797. mock_options->TestingV3AuthInitialDistDelay = 1;
  1798. time_t now = 1441223455 -1;
  1799. dirvote_recalculate_timing(mock_options, now);
  1800. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1801. &status_out);
  1802. tt_assert(vote);
  1803. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1804. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1805. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1806. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1807. GET("/tor/status-vote/next/authority"), NULL, 0));
  1808. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1809. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1810. tt_assert(header);
  1811. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1812. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1813. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1814. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1815. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1816. done:
  1817. UNMOCK(connection_write_to_buf_impl_);
  1818. UNMOCK(get_my_v3_authority_cert);
  1819. connection_free_(TO_CONN(conn));
  1820. tor_free(header);
  1821. tor_free(body);
  1822. authority_cert_free(mock_cert); mock_cert = NULL;
  1823. or_options_free(mock_options); mock_options = NULL;
  1824. clear_dir_servers();
  1825. routerlist_free_all();
  1826. dirvote_free_all();
  1827. }
  1828. static void
  1829. test_dir_handle_get_status_vote_current_authority(void* data)
  1830. {
  1831. dir_connection_t *conn = NULL;
  1832. char *header = NULL, *body = NULL;
  1833. const char *msg_out = NULL;
  1834. int status_out = 0;
  1835. size_t body_used = 0;
  1836. const char digest[DIGEST_LEN] = "";
  1837. dir_server_t *ds = NULL;
  1838. (void) data;
  1839. clear_dir_servers();
  1840. routerlist_free_all();
  1841. dirvote_free_all();
  1842. mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
  1843. /* create a trusted ds */
  1844. ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
  1845. NULL, V3_DIRINFO, 1.0);
  1846. tt_assert(ds);
  1847. dir_server_add(ds);
  1848. /* ds v3_identity_digest is the certificate's identity_key */
  1849. base16_decode(ds->v3_identity_digest, DIGEST_LEN,
  1850. TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
  1851. tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
  1852. TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1));
  1853. init_mock_options();
  1854. mock_options->AuthoritativeDir = 1;
  1855. mock_options->V3AuthoritativeDir = 1;
  1856. mock_options->TestingV3AuthVotingStartOffset = 0;
  1857. mock_options->TestingV3AuthInitialVotingInterval = 1;
  1858. mock_options->TestingV3AuthInitialVoteDelay = 1;
  1859. mock_options->TestingV3AuthInitialDistDelay = 1;
  1860. time_t now = 1441223455;
  1861. dirvote_recalculate_timing(mock_options, now-1);
  1862. struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
  1863. &status_out);
  1864. tt_assert(vote);
  1865. // move the pending vote to previous vote
  1866. dirvote_act(mock_options, now+1);
  1867. MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
  1868. MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
  1869. conn = dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR));
  1870. tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
  1871. GET("/tor/status-vote/current/authority"), NULL, 0));
  1872. fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
  1873. &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
  1874. tt_assert(header);
  1875. tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
  1876. tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
  1877. tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
  1878. tt_assert(strstr(header, "Content-Length: 4135\r\n"));
  1879. tt_str_op(VOTE_BODY_V3, OP_EQ, body);
  1880. done:
  1881. UNMOCK(connection_write_to_buf_impl_);
  1882. UNMOCK(get_my_v3_authority_cert);
  1883. connection_free_(TO_CONN(conn));
  1884. tor_free(header);
  1885. tor_free(body);
  1886. authority_cert_free(mock_cert); mock_cert = NULL;
  1887. or_options_free(mock_options); mock_options = NULL;
  1888. clear_dir_servers();
  1889. routerlist_free_all();
  1890. dirvote_free_all();
  1891. }
  1892. #define DIR_HANDLE_CMD(name,flags) \
  1893. { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
  1894. struct testcase_t dir_handle_get_tests[] = {
  1895. DIR_HANDLE_CMD(not_found, 0),
  1896. DIR_HANDLE_CMD(bad_request, 0),
  1897. DIR_HANDLE_CMD(v1_command_not_found, 0),
  1898. DIR_HANDLE_CMD(v1_command, 0),
  1899. DIR_HANDLE_CMD(robots_txt, 0),
  1900. DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
  1901. DIR_HANDLE_CMD(rendezvous2_not_found, 0),
  1902. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
  1903. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
  1904. DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
  1905. DIR_HANDLE_CMD(micro_d_not_found, 0),
  1906. DIR_HANDLE_CMD(micro_d_server_busy, 0),
  1907. DIR_HANDLE_CMD(micro_d, 0),
  1908. DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
  1909. DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
  1910. DIR_HANDLE_CMD(networkstatus_bridges, 0),
  1911. DIR_HANDLE_CMD(server_descriptors_not_found, 0),
  1912. DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
  1913. DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
  1914. DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
  1915. DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
  1916. DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
  1917. DIR_HANDLE_CMD(server_keys_bad_req, 0),
  1918. DIR_HANDLE_CMD(server_keys_busy, 0),
  1919. DIR_HANDLE_CMD(server_keys_all_not_found, 0),
  1920. DIR_HANDLE_CMD(server_keys_all, 0),
  1921. DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
  1922. DIR_HANDLE_CMD(server_keys_authority, 0),
  1923. DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
  1924. DIR_HANDLE_CMD(server_keys_fp, 0),
  1925. DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
  1926. DIR_HANDLE_CMD(server_keys_sk, 0),
  1927. DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
  1928. DIR_HANDLE_CMD(server_keys_fpsk, 0),
  1929. DIR_HANDLE_CMD(status_vote_current_not_found, 0),
  1930. DIR_HANDLE_CMD(status_vote_next_not_found, 0),
  1931. DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
  1932. DIR_HANDLE_CMD(status_vote_current_authority, 0),
  1933. DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
  1934. DIR_HANDLE_CMD(status_vote_next_authority, 0),
  1935. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, 0),
  1936. DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, 0),
  1937. DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, 0),
  1938. DIR_HANDLE_CMD(status_vote_current_consensus_ns, 0),
  1939. DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
  1940. DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
  1941. DIR_HANDLE_CMD(status_vote_d, 0),
  1942. DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
  1943. DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
  1944. DIR_HANDLE_CMD(status_vote_next_consensus, 0),
  1945. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
  1946. DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
  1947. DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
  1948. END_OF_TESTCASES
  1949. };