test_dir_handle_get.c 75 KB

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