test_dir_handle_get.c 78 KB

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