test_dir_handle_get.c 75 KB

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