test_dir_handle_get.c 76 KB

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