test_dir_handle_get.c 81 KB

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