test_dir_handle_get.c 74 KB

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