test_dir_handle_get.c 75 KB

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