123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634 |
- /* Copyright (c) 2001-2004, Roger Dingledine.
- * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
- * Copyright (c) 2007-2017, The Tor Project, Inc. */
- /* See LICENSE for licensing information */
- #define RENDCOMMON_PRIVATE
- #define GEOIP_PRIVATE
- #define CONNECTION_PRIVATE
- #define CONFIG_PRIVATE
- #define RENDCACHE_PRIVATE
- #include "or.h"
- #include "config.h"
- #include "connection.h"
- #include "consdiffmgr.h"
- #include "directory.h"
- #include "test.h"
- #include "compress.h"
- #include "connection.h"
- #include "rendcommon.h"
- #include "rendcache.h"
- #include "router.h"
- #include "routerlist.h"
- #include "rend_test_helpers.h"
- #include "microdesc.h"
- #include "test_helpers.h"
- #include "nodelist.h"
- #include "entrynodes.h"
- #include "routerparse.h"
- #include "networkstatus.h"
- #include "proto_http.h"
- #include "geoip.h"
- #include "dirserv.h"
- #include "dirvote.h"
- #include "log_test_helpers.h"
- #ifdef _WIN32
- /* For mkdir() */
- #include <direct.h>
- #else
- #include <dirent.h>
- #endif /* defined(_WIN32) */
- #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
- DISABLE_GCC_WARNING(overlength-strings)
- /* We allow huge string constants in the unit tests, but not in the code
- * at large. */
- #endif
- #include "vote_descriptors.inc"
- #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
- ENABLE_GCC_WARNING(overlength-strings)
- #endif
- #define NS_MODULE dir_handle_get
- #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
- #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
- #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
- #define TOO_OLD "HTTP/1.0 404 Consensus is too old\r\n\r\n"
- #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
- "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
- static dir_connection_t *
- new_dir_conn(void)
- {
- dir_connection_t *conn = dir_connection_new(AF_INET);
- tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
- TO_CONN(conn)->address = tor_strdup("127.0.0.1");
- return conn;
- }
- static void
- test_dir_handle_get_bad_request(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(directory_handle_command_get(conn, "", NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(header, OP_EQ, BAD_REQUEST);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_v1_command_not_found(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- // no frontpage configured
- tt_ptr_op(get_dirportfrontpage(), OP_EQ, NULL);
- /* V1 path */
- tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
- OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static const char*
- mock_get_dirportfrontpage(void)
- {
- return "HELLO FROM FRONTPAGE";
- }
- static void
- test_dir_handle_get_v1_command(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0, body_len = 0;
- const char *exp_body = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- MOCK(get_dirportfrontpage, mock_get_dirportfrontpage);
- exp_body = get_dirportfrontpage();
- body_len = strlen(exp_body);
- conn = new_dir_conn();
- tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
- OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, body_len+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/html\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 20\r\n"));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, exp_body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_dirportfrontpage);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- }
- static void
- test_dir_handle_get_not_found(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- /* Unrecognized path */
- tt_int_op(directory_handle_command_get(conn, GET("/anything"), NULL, 0),
- OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_robots_txt(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(directory_handle_command_get(conn, GET("/tor/robots.txt"),
- NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, 29, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 28\r\n"));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, "User-agent: *\r\nDisallow: /\r\n");
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- }
- #define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
- static void
- test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- // connection is not encrypted
- tt_assert(!connection_dir_is_encrypted(conn))
- tt_int_op(directory_handle_command_get(conn, RENDEZVOUS2_GET(), NULL, 0),
- OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
- void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- // connection is encrypted
- TO_CONN(conn)->linked = 1;
- tt_assert(connection_dir_is_encrypted(conn));
- tt_int_op(directory_handle_command_get(conn,
- RENDEZVOUS2_GET("invalid-desc-id"), NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(header, OP_EQ, BAD_REQUEST);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- // connection is encrypted
- TO_CONN(conn)->linked = 1;
- tt_assert(connection_dir_is_encrypted(conn));
- //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
- //case to happen. This test is the same as
- //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
- //We should refactor to remove the case from the switch.
- const char *req = RENDEZVOUS2_GET("1bababababababababababababababab");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(header, OP_EQ, BAD_REQUEST);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_rendezvous2_not_found(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- rend_cache_init();
- // connection is encrypted
- TO_CONN(conn)->linked = 1;
- tt_assert(connection_dir_is_encrypted(conn));
- const char *req = RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- rend_cache_free_all();
- }
- NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
- static routerinfo_t *mock_routerinfo;
- static const routerinfo_t *
- NS(router_get_my_routerinfo)(void)
- {
- if (!mock_routerinfo) {
- mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
- }
- return mock_routerinfo;
- }
- static void
- test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- char buff[30];
- char req[70];
- rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
- char *service_id = NULL;
- char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
- size_t body_len = 0;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- NS_MOCK(router_get_my_routerinfo);
- rend_cache_init();
- /* create a valid rend service descriptor */
- #define RECENT_TIME -10
- generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
- tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str),
- OP_EQ, 0);
- base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
- DIGEST_LEN);
- conn = new_dir_conn();
- // connection is encrypted
- TO_CONN(conn)->linked = 1;
- tt_assert(connection_dir_is_encrypted(conn));
- tor_snprintf(req, sizeof(req), RENDEZVOUS2_GET("%s"), desc_id_base32);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- body_len = strlen(desc_holder->desc_str);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, body_len+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Pragma: no-cache\r\n"));
- tor_snprintf(buff, sizeof(buff), "Content-Length: %ld\r\n", (long) body_len);
- tt_assert(strstr(header, buff));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, desc_holder->desc_str);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- NS_UNMOCK(router_get_my_routerinfo);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- rend_encoded_v2_service_descriptor_free(desc_holder);
- tor_free(service_id);
- rend_cache_free_all();
- }
- #define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
- static void
- test_dir_handle_get_micro_d_not_found(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
- #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
- conn = new_dir_conn();
- const char *req = MICRODESC_GET(B64_256_1 "-" B64_256_2);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static or_options_t *mock_options = NULL;
- static void
- init_mock_options(void)
- {
- mock_options = tor_malloc(sizeof(or_options_t));
- memset(mock_options, 0, sizeof(or_options_t));
- mock_options->TestingTorNetwork = 1;
- mock_options->DataDirectory = tor_strdup(get_fname_rnd("datadir_tmp"));
- mock_options->CacheDirectory = tor_strdup(mock_options->DataDirectory);
- check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
- }
- static const or_options_t *
- mock_get_options(void)
- {
- tor_assert(mock_options);
- return mock_options;
- }
- static const char microdesc[] =
- "onion-key\n"
- "-----BEGIN RSA PUBLIC KEY-----\n"
- "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
- "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
- "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
- "-----END RSA PUBLIC KEY-----\n";
- static void
- test_dir_handle_get_micro_d(void *data)
- {
- dir_connection_t *conn = NULL;
- microdesc_cache_t *mc = NULL ;
- smartlist_t *list = NULL;
- char digest[DIGEST256_LEN];
- char digest_base64[128];
- char path[80];
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- (void) data;
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* SETUP */
- init_mock_options();
- /* Add microdesc to cache */
- crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
- base64_encode_nopad(digest_base64, sizeof(digest_base64),
- (uint8_t *) digest, DIGEST256_LEN);
- mc = get_microdesc_cache();
- list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
- time(NULL), NULL);
- tt_int_op(1, OP_EQ, smartlist_len(list));
- /* Make the request */
- conn = new_dir_conn();
- tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
- tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(microdesc)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, microdesc);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- smartlist_free(list);
- microdesc_free_all();
- }
- static void
- test_dir_handle_get_micro_d_server_busy(void *data)
- {
- dir_connection_t *conn = NULL;
- microdesc_cache_t *mc = NULL ;
- smartlist_t *list = NULL;
- char digest[DIGEST256_LEN];
- char digest_base64[128];
- char path[80];
- char *header = NULL;
- (void) data;
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* SETUP */
- init_mock_options();
- /* Add microdesc to cache */
- crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
- base64_encode_nopad(digest_base64, sizeof(digest_base64),
- (uint8_t *) digest, DIGEST256_LEN);
- mc = get_microdesc_cache();
- list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
- time(NULL), NULL);
- tt_int_op(1, OP_EQ, smartlist_len(list));
- //Make it busy
- mock_options->CountPrivateBandwidth = 1;
- /* Make the request */
- conn = new_dir_conn();
- tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
- tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- smartlist_free(list);
- microdesc_free_all();
- }
- #define BRIDGES_PATH "/tor/networkstatus-bridges"
- static void
- test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* SETUP */
- init_mock_options();
- mock_options->BridgeAuthoritativeDir = 1;
- mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
- conn = new_dir_conn();
- TO_CONN(conn)->linked = 1;
- const char *req = GET(BRIDGES_PATH);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_networkstatus_bridges(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* SETUP */
- init_mock_options();
- mock_options->BridgeAuthoritativeDir = 1;
- mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
- crypto_digest256(mock_options->BridgePassword_AuthDigest_,
- "abcdefghijklm12345", 18, DIGEST_SHA256);
- conn = new_dir_conn();
- TO_CONN(conn)->linked = 1;
- const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
- "Authorization: Basic abcdefghijklm12345\r\n\r\n";
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 0\r\n"));
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* SETUP */
- init_mock_options();
- mock_options->BridgeAuthoritativeDir = 1;
- mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
- crypto_digest256(mock_options->BridgePassword_AuthDigest_,
- "abcdefghijklm12345", 18, DIGEST_SHA256);
- conn = new_dir_conn();
- TO_CONN(conn)->linked = 1;
- const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
- "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- #define SERVER_DESC_GET(id) GET("/tor/server/" id)
- static void
- test_dir_handle_get_server_descriptors_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = SERVER_DESC_GET("invalid");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- or_options_free(mock_options); mock_options = NULL;
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_server_descriptors_all(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- (void) data;
- /* Setup fake routerlist. */
- helper_setup_fake_routerlist();
- //TODO: change to router_get_my_extrainfo when testing "extra" path
- NS_MOCK(router_get_my_routerinfo);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- // We are one of the routers
- routerlist_t *our_routerlist = router_get_routerlist();
- tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
- mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
- set_server_identity_key(mock_routerinfo->identity_pkey);
- mock_routerinfo->cache_info.published_on = time(NULL);
- /* Treat "all" requests as if they were unencrypted */
- mock_routerinfo->cache_info.send_unencrypted = 1;
- conn = new_dir_conn();
- const char *req = SERVER_DESC_GET("all");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- //TODO: Is this a BUG?
- //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
- //which is smaller than that by annotation_len bytes
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used,
- 1024*1024, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- //TODO: Is this a BUG?
- //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
- tt_int_op(body_used, OP_EQ,
- mock_routerinfo->cache_info.signed_descriptor_len);
- tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
- mock_routerinfo->cache_info.annotations_len);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- NS_UNMOCK(router_get_my_routerinfo);
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- routerlist_free_all();
- nodelist_free_all();
- entry_guards_free_all();
- }
- static char
- TEST_DESCRIPTOR[] =
- "@uploaded-at 2014-06-08 19:20:11\n"
- "@source \"127.0.0.1\"\n"
- "router test000a 127.0.0.1 5000 0 7000\n"
- "platform Tor 0.2.5.3-alpha-dev on Linux\n"
- "protocols Link 1 2 Circuit 1\n"
- "published 2014-06-08 19:20:11\n"
- "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
- "uptime 0\n"
- "bandwidth 1073741824 1073741824 0\n"
- "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
- "caches-extra-info\n"
- "onion-key\n"
- "-----BEGIN RSA PUBLIC KEY-----\n"
- "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
- "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
- "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
- "-----END RSA PUBLIC KEY-----\n"
- "signing-key\n"
- "-----BEGIN RSA PUBLIC KEY-----\n"
- "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
- "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
- "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
- "-----END RSA PUBLIC KEY-----\n"
- "hidden-service-dir\n"
- "contact auth0@test.test\n"
- "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
- "reject *:*\n"
- "router-signature\n"
- "-----BEGIN SIGNATURE-----\n"
- "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
- "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
- "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
- "-----END SIGNATURE-----\n";
- static void
- test_dir_handle_get_server_descriptors_authority(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- crypto_pk_t *identity_pkey = pk_generate(0);
- (void) data;
- NS_MOCK(router_get_my_routerinfo);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* init mock */
- router_get_my_routerinfo();
- crypto_pk_get_digest(identity_pkey,
- mock_routerinfo->cache_info.identity_digest);
- // the digest is mine (the channel is unnecrypted, so we must allow sending)
- set_server_identity_key(identity_pkey);
- mock_routerinfo->cache_info.send_unencrypted = 1;
- /* Setup descriptor */
- long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
- mock_routerinfo->cache_info.signed_descriptor_body =
- tor_strdup(TEST_DESCRIPTOR);
- mock_routerinfo->cache_info.signed_descriptor_len =
- strlen(TEST_DESCRIPTOR) - annotation_len;
- mock_routerinfo->cache_info.annotations_len = annotation_len;
- mock_routerinfo->cache_info.published_on = time(NULL);
- conn = new_dir_conn();
- const char *req = SERVER_DESC_GET("authority");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- //TODO: Is this a BUG?
- //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
- //is smaller than that by annotation_len bytes
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- NS_UNMOCK(router_get_my_routerinfo);
- UNMOCK(connection_write_to_buf_impl_);
- tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
- tor_free(mock_routerinfo);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- crypto_pk_free(identity_pkey);
- }
- static void
- test_dir_handle_get_server_descriptors_fp(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- crypto_pk_t *identity_pkey = pk_generate(0);
- (void) data;
- NS_MOCK(router_get_my_routerinfo);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* init mock */
- router_get_my_routerinfo();
- crypto_pk_get_digest(identity_pkey,
- mock_routerinfo->cache_info.identity_digest);
- // the digest is mine (the channel is unnecrypted, so we must allow sending)
- set_server_identity_key(identity_pkey);
- mock_routerinfo->cache_info.send_unencrypted = 1;
- /* Setup descriptor */
- long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
- mock_routerinfo->cache_info.signed_descriptor_body =
- tor_strdup(TEST_DESCRIPTOR);
- mock_routerinfo->cache_info.signed_descriptor_len =
- strlen(TEST_DESCRIPTOR) - annotation_len;
- mock_routerinfo->cache_info.annotations_len = annotation_len;
- mock_routerinfo->cache_info.published_on = time(NULL);
- conn = new_dir_conn();
- #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
- #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
- const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
- DIGEST_LEN);
- char req[155];
- tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
- hex_digest);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- //TODO: Is this a BUG?
- //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
- //is smaller than that by annotation_len bytes
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_int_op(body_used, OP_EQ, strlen(body));
- tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- NS_UNMOCK(router_get_my_routerinfo);
- UNMOCK(connection_write_to_buf_impl_);
- tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
- tor_free(mock_routerinfo);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- crypto_pk_free(identity_pkey);
- }
- #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
- #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
- static void
- test_dir_handle_get_server_descriptors_d(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- crypto_pk_t *identity_pkey = pk_generate(0);
- (void) data;
- /* Setup fake routerlist. */
- helper_setup_fake_routerlist();
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* Get one router's signed_descriptor_digest */
- routerlist_t *our_routerlist = router_get_routerlist();
- tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
- routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
- const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
- DIGEST_LEN);
- conn = new_dir_conn();
- char req_header[155]; /* XXX Why 155? What kind of number is that?? */
- tor_snprintf(req_header, sizeof(req_header),
- SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
- tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
- //TODO: Is this a BUG?
- //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
- //which is smaller than that by annotation_len bytes
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used,
- router->cache_info.signed_descriptor_len+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- //TODO: Is this a BUG?
- //This is what should be expected:
- //tt_int_op(body_used, OP_EQ, strlen(body));
- tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
- tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
- router->cache_info.annotations_len);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- tor_free(mock_routerinfo);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- crypto_pk_free(identity_pkey);
- routerlist_free_all();
- nodelist_free_all();
- entry_guards_free_all();
- }
- static void
- test_dir_handle_get_server_descriptors_busy(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- crypto_pk_t *identity_pkey = pk_generate(0);
- (void) data;
- /* Setup fake routerlist. */
- helper_setup_fake_routerlist();
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- //Make it busy
- MOCK(get_options, mock_get_options);
- init_mock_options();
- mock_options->CountPrivateBandwidth = 1;
- /* Get one router's signed_descriptor_digest */
- routerlist_t *our_routerlist = router_get_routerlist();
- tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
- routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
- const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
- DIGEST_LEN);
- conn = new_dir_conn();
- #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
- #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
- char req_header[155]; /* XXX 155? Why 155? */
- tor_snprintf(req_header, sizeof(req_header),
- SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
- tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- tt_ptr_op(conn->spool, OP_EQ, NULL);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- tor_free(mock_routerinfo);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- crypto_pk_free(identity_pkey);
- routerlist_free_all();
- nodelist_free_all();
- entry_guards_free_all();
- }
- static void
- test_dir_handle_get_server_keys_bad_req(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(BAD_REQUEST, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_server_keys_all_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/all");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- #define TEST_CERTIFICATE AUTHORITY_CERT_3
- #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
- static const char TEST_CERT_IDENT_KEY[] =
- "D867ACF56A9D229B35C25F0090BC9867E906BE69";
- static void
- test_dir_handle_get_server_keys_all(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- const char digest[DIGEST_LEN] = "";
- dir_server_t *ds = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- clear_dir_servers();
- routerlist_free_all();
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- dir_server_add(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/all");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 1883\r\n"));
- tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- clear_dir_servers();
- routerlist_free_all();
- }
- static void
- test_dir_handle_get_server_keys_authority_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/authority");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static authority_cert_t * mock_cert = NULL;
- static authority_cert_t *
- get_my_v3_authority_cert_m(void)
- {
- tor_assert(mock_cert);
- return mock_cert;
- }
- static void
- test_dir_handle_get_server_keys_authority(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- (void) data;
- mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/authority");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 1883\r\n"));
- tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
- done:
- UNMOCK(get_my_v3_authority_cert);
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- authority_cert_free(mock_cert); mock_cert = NULL;
- }
- static void
- test_dir_handle_get_server_keys_fp_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/fp/somehex");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_server_keys_fp(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- dir_server_t *ds = NULL;
- const char digest[DIGEST_LEN] = "";
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- clear_dir_servers();
- routerlist_free_all();
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- dir_server_add(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- conn = new_dir_conn();
- char req[71];
- tor_snprintf(req, sizeof(req),
- GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 1883\r\n"));
- tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- clear_dir_servers();
- routerlist_free_all();
- }
- static void
- test_dir_handle_get_server_keys_sk_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/sk/somehex");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_server_keys_sk(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- (void) data;
- mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- clear_dir_servers();
- routerlist_free_all();
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- conn = new_dir_conn();
- char req[71];
- tor_snprintf(req, sizeof(req),
- GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 1883\r\n"));
- tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
- done:
- UNMOCK(get_my_v3_authority_cert);
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- authority_cert_free(mock_cert); mock_cert = NULL;
- tor_free(header);
- tor_free(body);
- }
- static void
- test_dir_handle_get_server_keys_fpsk_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- const char *req = GET("/tor/keys/fp-sk/somehex");
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_server_keys_fpsk(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- dir_server_t *ds = NULL;
- const char digest[DIGEST_LEN] = "";
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- clear_dir_servers();
- routerlist_free_all();
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- dir_server_add(ds);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- conn = new_dir_conn();
- char req[115];
- tor_snprintf(req, sizeof(req),
- GET("/tor/keys/fp-sk/%s-%s"),
- TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
- tt_assert(header);
- tt_assert(body);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 1883\r\n"));
- tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- clear_dir_servers();
- routerlist_free_all();
- }
- static void
- test_dir_handle_get_server_keys_busy(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- dir_server_t *ds = NULL;
- const char digest[DIGEST_LEN] = "";
- (void) data;
- clear_dir_servers();
- routerlist_free_all();
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- dir_server_add(ds);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* setup busy server */
- init_mock_options();
- mock_options->CountPrivateBandwidth = 1;
- conn = new_dir_conn();
- char req[71];
- tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
- tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- done:
- UNMOCK(get_options);
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- or_options_free(mock_options); mock_options = NULL;
- clear_dir_servers();
- routerlist_free_all();
- }
- static networkstatus_t *mock_ns_val = NULL;
- static networkstatus_t *
- mock_ns_get_by_flavor(consensus_flavor_t f)
- {
- (void)f;
- return mock_ns_val;
- }
- static void
- test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *stats = NULL;
- (void) d;
- /* init mock */
- mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
- mock_ns_val->flavor = FLAV_NS;
- mock_ns_val->type = NS_TYPE_CONSENSUS;
- mock_ns_val->voters = smartlist_new();
- mock_ns_val->valid_after = time(NULL) - 1800;
- mock_ns_val->valid_until = time(NULL) - 60;
- #define NETWORK_STATUS "some network status string"
- consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
- /* init mock */
- init_mock_options();
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
- /* start gathering stats */
- mock_options->DirReqStatistics = 1;
- geoip_dirreq_stats_init(time(NULL));
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
- stats = geoip_format_dirreq_stats(time(NULL));
- tt_assert(stats);
- tt_assert(strstr(stats, "not-enough-sigs=8"));
- done:
- UNMOCK(networkstatus_get_latest_consensus_by_flavor);
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_options);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(stats);
- smartlist_free(mock_ns_val->voters);
- tor_free(mock_ns_val);
- or_options_free(mock_options); mock_options = NULL;
- }
- static void
- test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- char *stats = NULL;
- (void) data;
- init_mock_options();
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- /* start gathering stats */
- mock_options->DirReqStatistics = 1;
- geoip_dirreq_stats_init(time(NULL));
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- stats = geoip_format_dirreq_stats(time(NULL));
- tt_assert(stats);
- tt_assert(strstr(stats, "not-found=8"));
- done:
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_options);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(stats);
- or_options_free(mock_options); mock_options = NULL;
- }
- static void
- test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void)data;
- mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
- mock_ns_val->type = NS_TYPE_CONSENSUS;
- mock_ns_val->flavor = FLAV_MICRODESC;
- mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
- mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
- mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
- #define NETWORK_STATUS "some network status string"
- consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
- init_mock_options();
- MOCK(get_options, mock_get_options);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
- conn = new_dir_conn();
- setup_capture_of_logs(LOG_WARN);
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/consensus-microdesc"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(TOO_OLD, OP_EQ, header);
- expect_log_msg_containing("too old");
- tor_free(header);
- teardown_capture_of_logs();
- tor_free(mock_ns_val);
- mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
- mock_ns_val->type = NS_TYPE_CONSENSUS;
- mock_ns_val->flavor = FLAV_NS;
- mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
- mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
- mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
- #define NETWORK_STATUS "some network status string"
- consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
- setup_capture_of_logs(LOG_WARN);
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/consensus"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(TOO_OLD, OP_EQ, header);
- expect_no_log_entry();
- done:
- teardown_capture_of_logs();
- UNMOCK(networkstatus_get_latest_consensus_by_flavor);
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_options);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(mock_ns_val);
- or_options_free(mock_options); mock_options = NULL;
- }
- NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
- int
- NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
- {
- (void)addr;
- CALLED(geoip_get_country_by_addr)++;
- return 1;
- }
- static void
- status_vote_current_consensus_ns_test(char **header, char **body,
- size_t *body_len)
- {
- dir_connection_t *conn = NULL;
- #define NETWORK_STATUS "some network status string"
- #if 0
- common_digests_t digests;
- uint8_t sha3[DIGEST256_LEN];
- memset(&digests, 0x60, sizeof(digests));
- memset(sha3, 0x06, sizeof(sha3));
- dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
- sha3,
- time(NULL));
- #endif /* 0 */
- networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
- ns->type = NS_TYPE_CONSENSUS;
- ns->flavor = FLAV_NS;
- ns->valid_after = time(NULL) - 1800;
- ns->fresh_until = time(NULL) - 900;
- ns->valid_until = time(NULL) - 60;
- consdiffmgr_add_consensus(NETWORK_STATUS, ns);
- networkstatus_vote_free(ns);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- tt_assert(mock_options);
- mock_options->DirReqStatistics = 1;
- geoip_dirreq_stats_init(time(NULL));
- /* init geoip database */
- geoip_parse_entry("10,50,AB", AF_INET);
- tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
- body, body_len, strlen(NETWORK_STATUS)+7, 0);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- }
- static void
- test_dir_handle_get_status_vote_current_consensus_ns(void* data)
- {
- char *header = NULL;
- char *body = NULL, *comp_body = NULL;
- size_t body_used = 0, comp_body_used = 0;
- char *stats = NULL, *hist = NULL;
- (void) data;
- dirserv_free_all();
- clear_geoip_db();
- NS_MOCK(geoip_get_country_by_addr);
- MOCK(get_options, mock_get_options);
- init_mock_options();
- status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Pragma: no-cache\r\n"));
- compress_method_t compression = detect_compression_method(comp_body,
- comp_body_used);
- tt_int_op(ZLIB_METHOD, OP_EQ, compression);
- tor_uncompress(&body, &body_used, comp_body, comp_body_used,
- compression, 0, LOG_PROTOCOL_WARN);
- tt_str_op(NETWORK_STATUS, OP_EQ, body);
- tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
- stats = geoip_format_dirreq_stats(time(NULL));
- tt_assert(stats);
- tt_assert(strstr(stats, "ok=8"));
- tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
- tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
- tt_assert(strstr(stats, "dirreq-v3-direct-dl"
- " complete=0,timeout=0,running=4"));
- hist = geoip_get_request_history();
- tt_assert(hist);
- tt_str_op("ab=8", OP_EQ, hist);
- done:
- NS_UNMOCK(geoip_get_country_by_addr);
- UNMOCK(get_options);
- tor_free(header);
- tor_free(comp_body);
- tor_free(body);
- tor_free(stats);
- tor_free(hist);
- or_options_free(mock_options); mock_options = NULL;
- dirserv_free_all();
- clear_geoip_db();
- }
- static void
- test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
- {
- char *header = NULL;
- char *body = NULL;
- size_t body_used = 0;
- char *stats = NULL;
- (void) data;
- dirserv_free_all();
- clear_geoip_db();
- MOCK(get_options, mock_get_options);
- // Make it busy
- init_mock_options();
- mock_options->CountPrivateBandwidth = 1;
- status_vote_current_consensus_ns_test(&header, &body, &body_used);
- tt_assert(header);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- stats = geoip_format_dirreq_stats(time(NULL));
- tt_assert(stats);
- tt_assert(strstr(stats, "busy=8"));
- done:
- UNMOCK(get_options);
- tor_free(header);
- tor_free(body);
- or_options_free(mock_options); mock_options = NULL;
- tor_free(stats);
- dirserv_free_all();
- clear_geoip_db();
- }
- static void
- test_dir_handle_get_status_vote_current_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/" HEX1), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
- static void
- status_vote_current_d_test(char **header, char **body, size_t *body_l)
- {
- dir_connection_t *conn = NULL;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
- body, body_l, strlen(VOTE_BODY_V3)+1, 0);
- tt_assert(header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- }
- static void
- status_vote_next_d_test(char **header, char **body, size_t *body_l)
- {
- dir_connection_t *conn = NULL;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
- body, body_l, strlen(VOTE_BODY_V3)+1, 0);
- tt_assert(header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- }
- static void
- test_dir_handle_get_status_vote_current_d_not_found(void* data)
- {
- char *header = NULL;
- (void) data;
- status_vote_current_d_test(&header, NULL, NULL);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- tor_free(header);
- }
- static void
- test_dir_handle_get_status_vote_next_d_not_found(void* data)
- {
- char *header = NULL;
- (void) data;
- status_vote_next_d_test(&header, NULL, NULL);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- tor_free(header);
- }
- static void
- test_dir_handle_get_status_vote_d(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used = 0;
- dir_server_t *ds = NULL;
- const char digest[DIGEST_LEN] = "";
- (void) data;
- clear_dir_servers();
- dirvote_free_all();
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- dir_server_add(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- init_mock_options();
- mock_options->AuthoritativeDir = 1;
- mock_options->V3AuthoritativeDir = 1;
- mock_options->TestingV3AuthVotingStartOffset = 0;
- mock_options->TestingV3AuthInitialVotingInterval = 1;
- mock_options->TestingV3AuthInitialVoteDelay = 1;
- mock_options->TestingV3AuthInitialDistDelay = 1;
- time_t now = 1441223455 -1;
- dirvote_recalculate_timing(mock_options, now);
- const char *msg_out = NULL;
- int status_out = 0;
- struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
- &status_out);
- tt_assert(pv);
- status_vote_current_d_test(&header, &body, &body_used);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 4135\r\n"));
- tt_str_op(VOTE_BODY_V3, OP_EQ, body);
- tor_free(header);
- tor_free(body);
- status_vote_next_d_test(&header, &body, &body_used);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 4135\r\n"));
- tt_str_op(VOTE_BODY_V3, OP_EQ, body);
- done:
- tor_free(header);
- tor_free(body);
- or_options_free(mock_options); mock_options = NULL;
- clear_dir_servers();
- dirvote_free_all();
- }
- static void
- test_dir_handle_get_status_vote_next_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/" HEX1), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
- {
- dir_connection_t *conn = NULL;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/consensus"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
- body, body_used, 18, 0);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- }
- static void
- test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used;
- (void) data;
- status_vote_next_consensus_test(&header, &body, &body_used);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- tor_free(header);
- tor_free(body);
- }
- static void
- test_dir_handle_get_status_vote_current_authority_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/authority"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- static void
- test_dir_handle_get_status_vote_next_authority_not_found(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL;
- (void) data;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/authority"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- NULL, NULL, 1, 0);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- }
- NS_DECL(const char*,
- dirvote_get_pending_consensus, (consensus_flavor_t flav));
- const char*
- NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
- {
- (void)flav;
- return "pending consensus";
- }
- static void
- test_dir_handle_get_status_vote_next_consensus(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used = 0;
- (void) data;
- NS_MOCK(dirvote_get_pending_consensus);
- status_vote_next_consensus_test(&header, &body, &body_used);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 17\r\n"));
- tt_str_op("pending consensus", OP_EQ, body);
- done:
- NS_UNMOCK(dirvote_get_pending_consensus);
- tor_free(header);
- tor_free(body);
- }
- static void
- test_dir_handle_get_status_vote_next_consensus_busy(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used = 0;
- (void) data;
- MOCK(get_options, mock_get_options);
- NS_MOCK(dirvote_get_pending_consensus);
- //Make it busy
- init_mock_options();
- mock_options->CountPrivateBandwidth = 1;
- status_vote_next_consensus_test(&header, &body, &body_used);
- tt_assert(header);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- done:
- NS_UNMOCK(dirvote_get_pending_consensus);
- UNMOCK(get_options);
- tor_free(header);
- tor_free(body);
- or_options_free(mock_options); mock_options = NULL;
- }
- static void
- status_vote_next_consensus_signatures_test(char **header, char **body,
- size_t *body_used)
- {
- dir_connection_t *conn = NULL;
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
- body, body_used, 22, 0);
- done:
- connection_free_minimal(TO_CONN(conn));
- UNMOCK(connection_write_to_buf_impl_);
- }
- static void
- test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used;
- (void) data;
- status_vote_next_consensus_signatures_test(&header, &body, &body_used);
- tt_assert(header);
- tt_str_op(NOT_FOUND, OP_EQ, header);
- done:
- tor_free(header);
- tor_free(body);
- }
- NS_DECL(const char*,
- dirvote_get_pending_detached_signatures, (void));
- const char*
- NS(dirvote_get_pending_detached_signatures)(void)
- {
- return "pending detached sigs";
- }
- static void
- test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used = 0;
- (void) data;
- NS_MOCK(dirvote_get_pending_detached_signatures);
- status_vote_next_consensus_signatures_test(&header, &body, &body_used);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 21\r\n"));
- tt_str_op("pending detached sigs", OP_EQ, body);
- done:
- NS_UNMOCK(dirvote_get_pending_detached_signatures);
- tor_free(header);
- tor_free(body);
- }
- static void
- test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
- {
- char *header = NULL, *body = NULL;
- size_t body_used;
- (void) data;
- NS_MOCK(dirvote_get_pending_detached_signatures);
- MOCK(get_options, mock_get_options);
- //Make it busy
- init_mock_options();
- mock_options->CountPrivateBandwidth = 1;
- status_vote_next_consensus_signatures_test(&header, &body, &body_used);
- tt_assert(header);
- tt_str_op(SERVER_BUSY, OP_EQ, header);
- done:
- UNMOCK(get_options);
- NS_UNMOCK(dirvote_get_pending_detached_signatures);
- tor_free(header);
- tor_free(body);
- or_options_free(mock_options); mock_options = NULL;
- }
- static void
- test_dir_handle_get_status_vote_next_authority(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL, *body = NULL;
- const char *msg_out = NULL;
- int status_out = 0;
- size_t body_used = 0;
- dir_server_t *ds = NULL;
- const char digest[DIGEST_LEN] = "";
- (void) data;
- clear_dir_servers();
- routerlist_free_all();
- dirvote_free_all();
- mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- dir_server_add(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- init_mock_options();
- mock_options->AuthoritativeDir = 1;
- mock_options->V3AuthoritativeDir = 1;
- mock_options->TestingV3AuthVotingStartOffset = 0;
- mock_options->TestingV3AuthInitialVotingInterval = 1;
- mock_options->TestingV3AuthInitialVoteDelay = 1;
- mock_options->TestingV3AuthInitialDistDelay = 1;
- time_t now = 1441223455 -1;
- dirvote_recalculate_timing(mock_options, now);
- struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
- &status_out);
- tt_assert(vote);
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/next/authority"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 4135\r\n"));
- tt_str_op(VOTE_BODY_V3, OP_EQ, body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_my_v3_authority_cert);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- authority_cert_free(mock_cert); mock_cert = NULL;
- or_options_free(mock_options); mock_options = NULL;
- clear_dir_servers();
- routerlist_free_all();
- dirvote_free_all();
- }
- static void
- test_dir_handle_get_status_vote_current_authority(void* data)
- {
- dir_connection_t *conn = NULL;
- char *header = NULL, *body = NULL;
- const char *msg_out = NULL;
- int status_out = 0;
- size_t body_used = 0;
- const char digest[DIGEST_LEN] = "";
- dir_server_t *ds = NULL;
- (void) data;
- clear_dir_servers();
- routerlist_free_all();
- dirvote_free_all();
- mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
- /* create a trusted ds */
- ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
- NULL, V3_DIRINFO, 1.0);
- tt_assert(ds);
- dir_server_add(ds);
- /* ds v3_identity_digest is the certificate's identity_key */
- base16_decode(ds->v3_identity_digest, DIGEST_LEN,
- TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
- tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
- TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
- init_mock_options();
- mock_options->AuthoritativeDir = 1;
- mock_options->V3AuthoritativeDir = 1;
- mock_options->TestingV3AuthVotingStartOffset = 0;
- mock_options->TestingV3AuthInitialVotingInterval = 1;
- mock_options->TestingV3AuthInitialVoteDelay = 1;
- mock_options->TestingV3AuthInitialDistDelay = 1;
- time_t now = 1441223455;
- dirvote_recalculate_timing(mock_options, now-1);
- struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
- &status_out);
- tt_assert(vote);
- // move the pending vote to previous vote
- dirvote_act(mock_options, now+1);
- MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
- MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
- conn = new_dir_conn();
- tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
- GET("/tor/status-vote/current/authority"), NULL, 0));
- fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
- &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
- tt_assert(header);
- tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
- tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
- tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
- tt_assert(strstr(header, "Content-Length: 4135\r\n"));
- tt_str_op(VOTE_BODY_V3, OP_EQ, body);
- done:
- UNMOCK(connection_write_to_buf_impl_);
- UNMOCK(get_my_v3_authority_cert);
- connection_free_minimal(TO_CONN(conn));
- tor_free(header);
- tor_free(body);
- authority_cert_free(mock_cert); mock_cert = NULL;
- or_options_free(mock_options); mock_options = NULL;
- clear_dir_servers();
- routerlist_free_all();
- dirvote_free_all();
- }
- static void
- test_dir_handle_get_parse_accept_encoding(void *arg)
- {
- (void)arg;
- const unsigned B_NONE = 1u << NO_METHOD;
- const unsigned B_ZLIB = 1u << ZLIB_METHOD;
- const unsigned B_GZIP = 1u << GZIP_METHOD;
- const unsigned B_LZMA = 1u << LZMA_METHOD;
- const unsigned B_ZSTD = 1u << ZSTD_METHOD;
- unsigned encodings;
- encodings = parse_accept_encoding_header("");
- tt_uint_op(B_NONE, OP_EQ, encodings);
- encodings = parse_accept_encoding_header(" ");
- tt_uint_op(B_NONE, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("dewey, cheatham, and howe ");
- tt_uint_op(B_NONE, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("dewey, cheatham, and gzip");
- tt_uint_op(B_NONE, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("dewey, cheatham, and, gzip");
- tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
- encodings = parse_accept_encoding_header(" gzip");
- tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("gzip");
- tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("x-zstd, deflate, x-tor-lzma");
- tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA, OP_EQ, encodings);
- encodings = parse_accept_encoding_header(
- "x-zstd, deflate, x-tor-lzma, gzip");
- tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
- encodings = parse_accept_encoding_header("x-zstd,deflate,x-tor-lzma,gzip");
- tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
- done:
- ;
- }
- #define DIR_HANDLE_CMD(name,flags) \
- { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
- struct testcase_t dir_handle_get_tests[] = {
- DIR_HANDLE_CMD(not_found, 0),
- DIR_HANDLE_CMD(bad_request, 0),
- DIR_HANDLE_CMD(v1_command_not_found, 0),
- DIR_HANDLE_CMD(v1_command, 0),
- DIR_HANDLE_CMD(robots_txt, 0),
- DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
- DIR_HANDLE_CMD(rendezvous2_not_found, 0),
- DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
- DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
- DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
- DIR_HANDLE_CMD(micro_d_not_found, 0),
- DIR_HANDLE_CMD(micro_d_server_busy, 0),
- DIR_HANDLE_CMD(micro_d, 0),
- DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
- DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
- DIR_HANDLE_CMD(networkstatus_bridges, 0),
- DIR_HANDLE_CMD(server_descriptors_not_found, 0),
- DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
- DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
- DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
- DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
- DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
- DIR_HANDLE_CMD(server_keys_bad_req, 0),
- DIR_HANDLE_CMD(server_keys_busy, 0),
- DIR_HANDLE_CMD(server_keys_all_not_found, 0),
- DIR_HANDLE_CMD(server_keys_all, 0),
- DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
- DIR_HANDLE_CMD(server_keys_authority, 0),
- DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
- DIR_HANDLE_CMD(server_keys_fp, 0),
- DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
- DIR_HANDLE_CMD(server_keys_sk, 0),
- DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
- DIR_HANDLE_CMD(server_keys_fpsk, 0),
- DIR_HANDLE_CMD(status_vote_current_not_found, 0),
- DIR_HANDLE_CMD(status_vote_next_not_found, 0),
- DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
- DIR_HANDLE_CMD(status_vote_current_authority, 0),
- DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
- DIR_HANDLE_CMD(status_vote_next_authority, 0),
- DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, TT_FORK),
- DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, TT_FORK),
- DIR_HANDLE_CMD(status_vote_current_consensus_too_old, TT_FORK),
- DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, TT_FORK),
- DIR_HANDLE_CMD(status_vote_current_consensus_ns, TT_FORK),
- DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
- DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
- DIR_HANDLE_CMD(status_vote_d, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
- DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
- DIR_HANDLE_CMD(parse_accept_encoding, 0),
- END_OF_TESTCASES
- };
|