test_dir_handle_get.c 77 KB

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