test_dir_handle_get.c 76 KB

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