test_dir_handle_get.c 73 KB

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