test_dir_handle_get.c 75 KB

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