test_dir_handle_get.c 77 KB

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