1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2016, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define RENDCOMMON_PRIVATE
8 #define CONNECTION_PRIVATE
10 #define RENDCACHE_PRIVATE
14 #include "connection.h"
15 #include "directory.h"
17 #include "connection.h"
18 #include "rendcommon.h"
19 #include "rendcache.h"
21 #include "routerlist.h"
22 #include "rend_test_helpers.h"
23 #include "microdesc.h"
24 #include "test_helpers.h"
26 #include "entrynodes.h"
27 #include "routerparse.h"
28 #include "networkstatus.h"
41 #include "vote_descriptors.inc"
43 #define NS_MODULE dir_handle_get
46 connection_write_to_buf_mock(const char *string
, size_t len
,
47 connection_t
*conn
, int zlib
)
54 write_to_buf(string
, len
, conn
->outbuf
);
57 #define GET(path) "GET " path " HTTP/1.0\r\n\r\n"
58 #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
59 #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
60 #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
61 #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
62 "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
64 static tor_addr_t MOCK_TOR_ADDR
;
67 test_dir_handle_get_bad_request(void *data
)
69 dir_connection_t
*conn
= NULL
;
73 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
75 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
76 tt_int_op(directory_handle_command_get(conn
, "", NULL
, 0), OP_EQ
, 0);
78 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
81 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
84 UNMOCK(connection_write_to_buf_impl_
);
85 connection_free_(TO_CONN(conn
));
90 test_dir_handle_get_v1_command_not_found(void *data
)
92 dir_connection_t
*conn
= NULL
;
96 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
98 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
100 // no frontpage configured
101 tt_ptr_op(get_dirportfrontpage(), OP_EQ
, NULL
);
104 tt_int_op(directory_handle_command_get(conn
, GET("/tor/"), NULL
, 0),
107 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
110 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
113 UNMOCK(connection_write_to_buf_impl_
);
114 connection_free_(TO_CONN(conn
));
119 mock_get_dirportfrontpage(void)
121 return "HELLO FROM FRONTPAGE";
125 test_dir_handle_get_v1_command(void *data
)
127 dir_connection_t
*conn
= NULL
;
130 size_t body_used
= 0, body_len
= 0;
131 const char *exp_body
= NULL
;
134 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
135 MOCK(get_dirportfrontpage
, mock_get_dirportfrontpage
);
137 exp_body
= get_dirportfrontpage();
138 body_len
= strlen(exp_body
);
140 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
141 tt_int_op(directory_handle_command_get(conn
, GET("/tor/"), NULL
, 0),
144 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
145 &body
, &body_used
, body_len
+1, 0);
150 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
151 tt_assert(strstr(header
, "Content-Type: text/html\r\n"));
152 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
153 tt_assert(strstr(header
, "Content-Length: 20\r\n"));
155 tt_int_op(body_used
, OP_EQ
, strlen(body
));
156 tt_str_op(body
, OP_EQ
, exp_body
);
159 UNMOCK(connection_write_to_buf_impl_
);
160 UNMOCK(get_dirportfrontpage
);
161 connection_free_(TO_CONN(conn
));
167 test_dir_handle_get_not_found(void *data
)
169 dir_connection_t
*conn
= NULL
;
173 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
175 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
177 /* Unrecognized path */
178 tt_int_op(directory_handle_command_get(conn
, GET("/anything"), NULL
, 0),
180 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
183 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
186 UNMOCK(connection_write_to_buf_impl_
);
187 connection_free_(TO_CONN(conn
));
192 test_dir_handle_get_robots_txt(void *data
)
194 dir_connection_t
*conn
= NULL
;
197 size_t body_used
= 0;
200 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
202 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
204 tt_int_op(directory_handle_command_get(conn
, GET("/tor/robots.txt"),
206 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
207 &body
, &body_used
, 29, 0);
212 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
213 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
214 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
215 tt_assert(strstr(header
, "Content-Length: 28\r\n"));
217 tt_int_op(body_used
, OP_EQ
, strlen(body
));
218 tt_str_op(body
, OP_EQ
, "User-agent: *\r\nDisallow: /\r\n");
221 UNMOCK(connection_write_to_buf_impl_
);
222 connection_free_(TO_CONN(conn
));
228 test_dir_handle_get_bytes_txt(void *data
)
230 dir_connection_t
*conn
= NULL
;
233 size_t body_used
= 0, body_len
= 0;
235 char *exp_body
= NULL
;
238 exp_body
= directory_dump_request_log();
239 body_len
= strlen(exp_body
);
241 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
243 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
245 tt_int_op(directory_handle_command_get(conn
, GET("/tor/bytes.txt"), NULL
, 0),
247 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
248 &body
, &body_used
, body_len
+1, 0);
253 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
254 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
255 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
256 tt_assert(strstr(header
, "Pragma: no-cache\r\n"));
258 sprintf(buff
, "Content-Length: %ld\r\n", (long) body_len
);
259 tt_assert(strstr(header
, buff
));
261 tt_int_op(body_used
, OP_EQ
, strlen(body
));
262 tt_str_op(body
, OP_EQ
, exp_body
);
265 UNMOCK(connection_write_to_buf_impl_
);
266 connection_free_(TO_CONN(conn
));
272 #define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
274 test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data
)
276 dir_connection_t
*conn
= NULL
;
280 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
282 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
284 // connection is not encrypted
285 tt_assert(!connection_dir_is_encrypted(conn
))
287 tt_int_op(directory_handle_command_get(conn
, RENDEZVOUS2_GET(), NULL
, 0),
289 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
292 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
295 UNMOCK(connection_write_to_buf_impl_
);
296 connection_free_(TO_CONN(conn
));
301 test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
304 dir_connection_t
*conn
= NULL
;
308 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
309 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
311 // connection is encrypted
312 TO_CONN(conn
)->linked
= 1;
313 tt_assert(connection_dir_is_encrypted(conn
));
315 tt_int_op(directory_handle_command_get(conn
,
316 RENDEZVOUS2_GET("invalid-desc-id"), NULL
, 0), OP_EQ
, 0);
317 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
320 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
323 UNMOCK(connection_write_to_buf_impl_
);
324 connection_free_(TO_CONN(conn
));
329 test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data
)
331 dir_connection_t
*conn
= NULL
;
335 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
336 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
338 // connection is encrypted
339 TO_CONN(conn
)->linked
= 1;
340 tt_assert(connection_dir_is_encrypted(conn
));
342 //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
343 //case to happen. This test is the same as
344 //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
345 //We should refactor to remove the case from the switch.
347 const char *req
= RENDEZVOUS2_GET("1bababababababababababababababab");
348 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
350 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
353 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
356 UNMOCK(connection_write_to_buf_impl_
);
357 connection_free_(TO_CONN(conn
));
362 test_dir_handle_get_rendezvous2_not_found(void *data
)
364 dir_connection_t
*conn
= NULL
;
368 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
369 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
373 // connection is encrypted
374 TO_CONN(conn
)->linked
= 1;
375 tt_assert(connection_dir_is_encrypted(conn
));
377 const char *req
= RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
378 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
379 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
382 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
385 UNMOCK(connection_write_to_buf_impl_
);
386 connection_free_(TO_CONN(conn
));
388 rend_cache_free_all();
391 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
393 static routerinfo_t
*mock_routerinfo
;
395 static const routerinfo_t
*
396 NS(router_get_my_routerinfo
)(void)
398 if (!mock_routerinfo
) {
399 mock_routerinfo
= tor_malloc_zero(sizeof(routerinfo_t
));
402 return mock_routerinfo
;
406 test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data
)
408 dir_connection_t
*conn
= NULL
;
411 size_t body_used
= 0;
414 rend_encoded_v2_service_descriptor_t
*desc_holder
= NULL
;
415 char *service_id
= NULL
;
416 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
420 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
421 NS_MOCK(router_get_my_routerinfo
);
425 /* create a valid rend service descriptor */
426 #define RECENT_TIME -10
427 generate_desc(RECENT_TIME
, &desc_holder
, &service_id
, 3);
429 tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder
->desc_str
),
432 base32_encode(desc_id_base32
, sizeof(desc_id_base32
), desc_holder
->desc_id
,
435 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
437 // connection is encrypted
438 TO_CONN(conn
)->linked
= 1;
439 tt_assert(connection_dir_is_encrypted(conn
));
441 sprintf(req
, RENDEZVOUS2_GET("%s"), desc_id_base32
);
443 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
445 body_len
= strlen(desc_holder
->desc_str
);
446 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
447 &body
, &body_used
, body_len
+1, 0);
452 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
453 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
454 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
455 tt_assert(strstr(header
, "Pragma: no-cache\r\n"));
456 sprintf(buff
, "Content-Length: %ld\r\n", (long) body_len
);
457 tt_assert(strstr(header
, buff
));
459 tt_int_op(body_used
, OP_EQ
, strlen(body
));
460 tt_str_op(body
, OP_EQ
, desc_holder
->desc_str
);
463 UNMOCK(connection_write_to_buf_impl_
);
464 NS_UNMOCK(router_get_my_routerinfo
);
466 connection_free_(TO_CONN(conn
));
469 rend_encoded_v2_service_descriptor_free(desc_holder
);
470 tor_free(service_id
);
471 rend_cache_free_all();
474 #define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
476 test_dir_handle_get_micro_d_not_found(void *data
)
478 dir_connection_t
*conn
= NULL
;
482 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
484 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
485 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
486 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
488 const char *req
= MICRODESC_GET(B64_256_1
"-" B64_256_2
);
489 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
491 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
494 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
497 UNMOCK(connection_write_to_buf_impl_
);
499 connection_free_(TO_CONN(conn
));
503 static or_options_t
*mock_options
= NULL
;
505 init_mock_options(void)
507 mock_options
= malloc(sizeof(or_options_t
));
508 memset(mock_options
, 0, sizeof(or_options_t
));
509 mock_options
->TestingTorNetwork
= 1;
512 static const or_options_t
*
513 mock_get_options(void)
515 tor_assert(mock_options
);
519 static const char microdesc
[] =
521 "-----BEGIN RSA PUBLIC KEY-----\n"
522 "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
523 "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
524 "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
525 "-----END RSA PUBLIC KEY-----\n";
528 test_dir_handle_get_micro_d(void *data
)
530 dir_connection_t
*conn
= NULL
;
531 microdesc_cache_t
*mc
= NULL
;
532 smartlist_t
*list
= NULL
;
533 char digest
[DIGEST256_LEN
];
534 char digest_base64
[128];
538 size_t body_used
= 0;
541 MOCK(get_options
, mock_get_options
);
542 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
546 const char *fn
= get_fname("dir_handle_datadir_test1");
547 mock_options
->DataDirectory
= tor_strdup(fn
);
550 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
));
552 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
, 0700));
555 /* Add microdesc to cache */
556 crypto_digest256(digest
, microdesc
, strlen(microdesc
), DIGEST_SHA256
);
557 base64_encode_nopad(digest_base64
, sizeof(digest_base64
),
558 (uint8_t *) digest
, DIGEST256_LEN
);
560 mc
= get_microdesc_cache();
561 list
= microdescs_add_to_cache(mc
, microdesc
, NULL
, SAVED_NOWHERE
, 0,
563 tt_int_op(1, OP_EQ
, smartlist_len(list
));
565 /* Make the request */
566 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
568 sprintf(path
, MICRODESC_GET("%s"), digest_base64
);
569 tt_int_op(directory_handle_command_get(conn
, path
, NULL
, 0), OP_EQ
, 0);
571 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
572 &body
, &body_used
, strlen(microdesc
)+1, 0);
577 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
578 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
579 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
581 tt_int_op(body_used
, OP_EQ
, strlen(body
));
582 tt_str_op(body
, OP_EQ
, microdesc
);
586 UNMOCK(connection_write_to_buf_impl_
);
588 or_options_free(mock_options
); mock_options
= NULL
;
589 connection_free_(TO_CONN(conn
));
592 smartlist_free(list
);
593 microdesc_free_all();
597 test_dir_handle_get_micro_d_server_busy(void *data
)
599 dir_connection_t
*conn
= NULL
;
600 microdesc_cache_t
*mc
= NULL
;
601 smartlist_t
*list
= NULL
;
602 char digest
[DIGEST256_LEN
];
603 char digest_base64
[128];
608 MOCK(get_options
, mock_get_options
);
609 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
613 const char *fn
= get_fname("dir_handle_datadir_test2");
614 mock_options
->DataDirectory
= tor_strdup(fn
);
617 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
));
619 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
, 0700));
622 /* Add microdesc to cache */
623 crypto_digest256(digest
, microdesc
, strlen(microdesc
), DIGEST_SHA256
);
624 base64_encode_nopad(digest_base64
, sizeof(digest_base64
),
625 (uint8_t *) digest
, DIGEST256_LEN
);
627 mc
= get_microdesc_cache();
628 list
= microdescs_add_to_cache(mc
, microdesc
, NULL
, SAVED_NOWHERE
, 0,
630 tt_int_op(1, OP_EQ
, smartlist_len(list
));
633 mock_options
->CountPrivateBandwidth
= 1;
635 /* Make the request */
636 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
638 sprintf(path
, MICRODESC_GET("%s"), digest_base64
);
639 tt_int_op(directory_handle_command_get(conn
, path
, NULL
, 0), OP_EQ
, 0);
641 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
644 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
648 UNMOCK(connection_write_to_buf_impl_
);
650 or_options_free(mock_options
); mock_options
= NULL
;
651 connection_free_(TO_CONN(conn
));
653 smartlist_free(list
);
654 microdesc_free_all();
657 #define BRIDGES_PATH "/tor/networkstatus-bridges"
659 test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data
)
661 dir_connection_t
*conn
= NULL
;
665 MOCK(get_options
, mock_get_options
);
666 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
670 mock_options
->BridgeAuthoritativeDir
= 1;
671 mock_options
->BridgePassword_AuthDigest_
= tor_strdup("digest");
673 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
674 TO_CONN(conn
)->linked
= 1;
676 const char *req
= GET(BRIDGES_PATH
);
677 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
679 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
682 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
686 UNMOCK(connection_write_to_buf_impl_
);
687 or_options_free(mock_options
); mock_options
= NULL
;
688 connection_free_(TO_CONN(conn
));
693 test_dir_handle_get_networkstatus_bridges(void *data
)
695 dir_connection_t
*conn
= NULL
;
699 MOCK(get_options
, mock_get_options
);
700 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
704 mock_options
->BridgeAuthoritativeDir
= 1;
705 mock_options
->BridgePassword_AuthDigest_
= tor_malloc(DIGEST256_LEN
);
706 crypto_digest256(mock_options
->BridgePassword_AuthDigest_
,
707 "abcdefghijklm12345", 18, DIGEST_SHA256
);
709 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
710 TO_CONN(conn
)->linked
= 1;
712 const char *req
= "GET " BRIDGES_PATH
" HTTP/1.0\r\n"
713 "Authorization: Basic abcdefghijklm12345\r\n\r\n";
714 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
716 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
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_assert(strstr(header
, "Content-Length: 0\r\n"));
726 UNMOCK(connection_write_to_buf_impl_
);
727 or_options_free(mock_options
); mock_options
= NULL
;
728 connection_free_(TO_CONN(conn
));
733 test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data
)
735 dir_connection_t
*conn
= NULL
;
739 MOCK(get_options
, mock_get_options
);
740 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
744 mock_options
->BridgeAuthoritativeDir
= 1;
745 mock_options
->BridgePassword_AuthDigest_
= tor_malloc(DIGEST256_LEN
);
746 crypto_digest256(mock_options
->BridgePassword_AuthDigest_
,
747 "abcdefghijklm12345", 18, DIGEST_SHA256
);
749 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
750 TO_CONN(conn
)->linked
= 1;
752 const char *req
= "GET " BRIDGES_PATH
" HTTP/1.0\r\n"
753 "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
754 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
756 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
759 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
763 UNMOCK(connection_write_to_buf_impl_
);
764 or_options_free(mock_options
); mock_options
= NULL
;
765 connection_free_(TO_CONN(conn
));
769 #define SERVER_DESC_GET(id) GET("/tor/server/" id)
771 test_dir_handle_get_server_descriptors_not_found(void* data
)
773 dir_connection_t
*conn
= NULL
;
777 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
779 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
781 const char *req
= SERVER_DESC_GET("invalid");
782 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
784 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
787 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
788 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_SERVER_BY_FP
);
791 UNMOCK(connection_write_to_buf_impl_
);
792 or_options_free(mock_options
); mock_options
= NULL
;
793 connection_free_(TO_CONN(conn
));
798 test_dir_handle_get_server_descriptors_all(void* data
)
800 dir_connection_t
*conn
= NULL
;
803 size_t body_used
= 0;
806 /* Setup fake routerlist. */
807 helper_setup_fake_routerlist();
809 //TODO: change to router_get_my_extrainfo when testing "extra" path
810 NS_MOCK(router_get_my_routerinfo
);
811 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
813 // We are one of the routers
814 routerlist_t
*our_routerlist
= router_get_routerlist();
815 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
816 mock_routerinfo
= smartlist_get(our_routerlist
->routers
, 0);
817 set_server_identity_key(mock_routerinfo
->identity_pkey
);
819 /* Treat "all" requests as if they were unencrypted */
820 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
822 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
824 const char *req
= SERVER_DESC_GET("all");
825 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
827 //TODO: Is this a BUG?
828 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
829 //which is smaller than that by annotation_len bytes
830 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
832 mock_routerinfo
->cache_info
.signed_descriptor_len
+1, 0);
837 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
838 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
839 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
841 //TODO: Is this a BUG?
842 //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
843 tt_int_op(body_used
, OP_EQ
,
844 mock_routerinfo
->cache_info
.signed_descriptor_len
);
846 tt_str_op(body
, OP_EQ
, mock_routerinfo
->cache_info
.signed_descriptor_body
+
847 mock_routerinfo
->cache_info
.annotations_len
);
848 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
851 NS_UNMOCK(router_get_my_routerinfo
);
852 UNMOCK(connection_write_to_buf_impl_
);
853 connection_free_(TO_CONN(conn
));
857 routerlist_free_all();
859 entry_guards_free_all();
864 "@uploaded-at 2014-06-08 19:20:11\n"
865 "@source \"127.0.0.1\"\n"
866 "router test000a 127.0.0.1 5000 0 7000\n"
867 "platform Tor 0.2.5.3-alpha-dev on Linux\n"
868 "protocols Link 1 2 Circuit 1\n"
869 "published 2014-06-08 19:20:11\n"
870 "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
872 "bandwidth 1073741824 1073741824 0\n"
873 "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
874 "caches-extra-info\n"
876 "-----BEGIN RSA PUBLIC KEY-----\n"
877 "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
878 "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
879 "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
880 "-----END RSA PUBLIC KEY-----\n"
882 "-----BEGIN RSA PUBLIC KEY-----\n"
883 "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
884 "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
885 "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
886 "-----END RSA PUBLIC KEY-----\n"
887 "hidden-service-dir\n"
888 "contact auth0@test.test\n"
889 "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
892 "-----BEGIN SIGNATURE-----\n"
893 "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
894 "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
895 "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
896 "-----END SIGNATURE-----\n";
899 test_dir_handle_get_server_descriptors_authority(void* data
)
901 dir_connection_t
*conn
= NULL
;
904 size_t body_used
= 0;
905 crypto_pk_t
*identity_pkey
= pk_generate(0);
908 NS_MOCK(router_get_my_routerinfo
);
909 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
912 router_get_my_routerinfo();
913 crypto_pk_get_digest(identity_pkey
,
914 mock_routerinfo
->cache_info
.identity_digest
);
916 // the digest is mine (the channel is unnecrypted, so we must allow sending)
917 set_server_identity_key(identity_pkey
);
918 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
920 /* Setup descriptor */
921 long annotation_len
= strstr(TEST_DESCRIPTOR
, "router ") - TEST_DESCRIPTOR
;
922 mock_routerinfo
->cache_info
.signed_descriptor_body
=
923 tor_strdup(TEST_DESCRIPTOR
);
924 mock_routerinfo
->cache_info
.signed_descriptor_len
=
925 strlen(TEST_DESCRIPTOR
) - annotation_len
;;
926 mock_routerinfo
->cache_info
.annotations_len
= annotation_len
;
928 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
930 const char *req
= SERVER_DESC_GET("authority");
931 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
933 //TODO: Is this a BUG?
934 //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
935 //is smaller than that by annotation_len bytes
936 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
937 &body
, &body_used
, strlen(TEST_DESCRIPTOR
)+1, 0);
942 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
943 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
944 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
946 tt_int_op(body_used
, OP_EQ
, strlen(body
));
948 tt_str_op(body
, OP_EQ
, TEST_DESCRIPTOR
+ annotation_len
);
949 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
952 NS_UNMOCK(router_get_my_routerinfo
);
953 UNMOCK(connection_write_to_buf_impl_
);
954 tor_free(mock_routerinfo
->cache_info
.signed_descriptor_body
);
955 tor_free(mock_routerinfo
);
956 connection_free_(TO_CONN(conn
));
959 crypto_pk_free(identity_pkey
);
963 test_dir_handle_get_server_descriptors_fp(void* data
)
965 dir_connection_t
*conn
= NULL
;
968 size_t body_used
= 0;
969 crypto_pk_t
*identity_pkey
= pk_generate(0);
972 NS_MOCK(router_get_my_routerinfo
);
973 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
976 router_get_my_routerinfo();
977 crypto_pk_get_digest(identity_pkey
,
978 mock_routerinfo
->cache_info
.identity_digest
);
980 // the digest is mine (the channel is unnecrypted, so we must allow sending)
981 set_server_identity_key(identity_pkey
);
982 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
984 /* Setup descriptor */
985 long annotation_len
= strstr(TEST_DESCRIPTOR
, "router ") - TEST_DESCRIPTOR
;
986 mock_routerinfo
->cache_info
.signed_descriptor_body
=
987 tor_strdup(TEST_DESCRIPTOR
);
988 mock_routerinfo
->cache_info
.signed_descriptor_len
=
989 strlen(TEST_DESCRIPTOR
) - annotation_len
;
990 mock_routerinfo
->cache_info
.annotations_len
= annotation_len
;
992 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
994 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
995 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
996 const char *hex_digest
= hex_str(mock_routerinfo
->cache_info
.identity_digest
,
1000 sprintf(req
, SERVER_DESC_GET("fp/%s+" HEX1
"+" HEX2
), hex_digest
);
1001 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1003 //TODO: Is this a BUG?
1004 //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
1005 //is smaller than that by annotation_len bytes
1006 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1007 &body
, &body_used
, strlen(TEST_DESCRIPTOR
)+1, 0);
1012 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1013 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1014 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1016 tt_int_op(body_used
, OP_EQ
, strlen(body
));
1018 tt_str_op(body
, OP_EQ
, TEST_DESCRIPTOR
+ annotation_len
);
1019 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
1022 NS_UNMOCK(router_get_my_routerinfo
);
1023 UNMOCK(connection_write_to_buf_impl_
);
1024 tor_free(mock_routerinfo
->cache_info
.signed_descriptor_body
);
1025 tor_free(mock_routerinfo
);
1026 connection_free_(TO_CONN(conn
));
1029 crypto_pk_free(identity_pkey
);
1032 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1033 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1036 test_dir_handle_get_server_descriptors_d(void* data
)
1038 dir_connection_t
*conn
= NULL
;
1039 char *header
= NULL
;
1041 size_t body_used
= 0;
1042 crypto_pk_t
*identity_pkey
= pk_generate(0);
1045 /* Setup fake routerlist. */
1046 helper_setup_fake_routerlist();
1048 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1050 /* Get one router's signed_descriptor_digest */
1051 routerlist_t
*our_routerlist
= router_get_routerlist();
1052 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
1053 routerinfo_t
*router
= smartlist_get(our_routerlist
->routers
, 0);
1054 const char *hex_digest
= hex_str(router
->cache_info
.signed_descriptor_digest
,
1057 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1059 char req_header
[155];
1060 sprintf(req_header
, SERVER_DESC_GET("d/%s+" HEX1
"+" HEX2
), hex_digest
);
1061 tt_int_op(directory_handle_command_get(conn
, req_header
, NULL
, 0), OP_EQ
, 0);
1063 //TODO: Is this a BUG?
1064 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
1065 //which is smaller than that by annotation_len bytes
1066 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1068 router
->cache_info
.signed_descriptor_len
+1, 0);
1073 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1074 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1075 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1077 //TODO: Is this a BUG?
1078 //This is what should be expected:
1079 //tt_int_op(body_used, OP_EQ, strlen(body));
1080 tt_int_op(body_used
, OP_EQ
, router
->cache_info
.signed_descriptor_len
);
1082 tt_str_op(body
, OP_EQ
, router
->cache_info
.signed_descriptor_body
+
1083 router
->cache_info
.annotations_len
);
1084 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
1087 UNMOCK(connection_write_to_buf_impl_
);
1088 tor_free(mock_routerinfo
);
1089 connection_free_(TO_CONN(conn
));
1092 crypto_pk_free(identity_pkey
);
1094 routerlist_free_all();
1095 nodelist_free_all();
1096 entry_guards_free_all();
1100 test_dir_handle_get_server_descriptors_busy(void* data
)
1102 dir_connection_t
*conn
= NULL
;
1103 char *header
= NULL
;
1104 crypto_pk_t
*identity_pkey
= pk_generate(0);
1107 /* Setup fake routerlist. */
1108 helper_setup_fake_routerlist();
1110 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1113 MOCK(get_options
, mock_get_options
);
1114 init_mock_options();
1115 mock_options
->CountPrivateBandwidth
= 1;
1117 /* Get one router's signed_descriptor_digest */
1118 routerlist_t
*our_routerlist
= router_get_routerlist();
1119 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
1120 routerinfo_t
*router
= smartlist_get(our_routerlist
->routers
, 0);
1121 const char *hex_digest
= hex_str(router
->cache_info
.signed_descriptor_digest
,
1124 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1126 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1127 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1128 char req_header
[155];
1129 sprintf(req_header
, SERVER_DESC_GET("d/%s+" HEX1
"+" HEX2
), hex_digest
);
1130 tt_int_op(directory_handle_command_get(conn
, req_header
, NULL
, 0), OP_EQ
, 0);
1132 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1136 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1138 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
1141 UNMOCK(get_options
);
1142 UNMOCK(connection_write_to_buf_impl_
);
1143 tor_free(mock_routerinfo
);
1144 connection_free_(TO_CONN(conn
));
1146 crypto_pk_free(identity_pkey
);
1148 routerlist_free_all();
1149 nodelist_free_all();
1150 entry_guards_free_all();
1154 test_dir_handle_get_server_keys_bad_req(void* data
)
1156 dir_connection_t
*conn
= NULL
;
1157 char *header
= NULL
;
1160 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1162 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1164 const char *req
= GET("/tor/keys/");
1165 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1167 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1171 tt_str_op(BAD_REQUEST
, OP_EQ
, header
);
1174 UNMOCK(connection_write_to_buf_impl_
);
1175 connection_free_(TO_CONN(conn
));
1180 test_dir_handle_get_server_keys_all_not_found(void* data
)
1182 dir_connection_t
*conn
= NULL
;
1183 char *header
= NULL
;
1186 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1188 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1190 const char *req
= GET("/tor/keys/all");
1191 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1193 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1197 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1200 UNMOCK(connection_write_to_buf_impl_
);
1201 connection_free_(TO_CONN(conn
));
1205 #define TEST_CERTIFICATE AUTHORITY_CERT_3
1206 #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
1207 extern const char AUTHORITY_CERT_3
[];
1208 extern const char AUTHORITY_SIGNKEY_A_DIGEST
[];
1210 static const char TEST_CERT_IDENT_KEY
[] =
1211 "D867ACF56A9D229B35C25F0090BC9867E906BE69";
1214 test_dir_handle_get_server_keys_all(void* data
)
1216 dir_connection_t
*conn
= NULL
;
1217 char *header
= NULL
;
1219 size_t body_used
= 0;
1220 const char digest
[DIGEST_LEN
] = "";
1222 dir_server_t
*ds
= NULL
;
1225 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1227 clear_dir_servers();
1228 routerlist_free_all();
1230 /* create a trusted ds */
1231 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1232 NULL
, V3_DIRINFO
, 1.0);
1236 /* ds v3_identity_digest is the certificate's identity_key */
1237 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1238 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
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));
1242 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1244 const char *req
= GET("/tor/keys/all");
1245 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1247 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1248 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1253 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1254 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1255 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1256 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1258 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1261 UNMOCK(connection_write_to_buf_impl_
);
1262 connection_free_(TO_CONN(conn
));
1266 clear_dir_servers();
1267 routerlist_free_all();
1271 test_dir_handle_get_server_keys_authority_not_found(void* data
)
1273 dir_connection_t
*conn
= NULL
;
1274 char *header
= NULL
;
1277 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1279 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1281 const char *req
= GET("/tor/keys/authority");
1282 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1284 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1288 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1291 UNMOCK(connection_write_to_buf_impl_
);
1292 connection_free_(TO_CONN(conn
));
1296 static authority_cert_t
* mock_cert
= NULL
;
1298 static authority_cert_t
*
1299 get_my_v3_authority_cert_m(void)
1301 tor_assert(mock_cert
);
1306 test_dir_handle_get_server_keys_authority(void* data
)
1308 dir_connection_t
*conn
= NULL
;
1309 char *header
= NULL
;
1311 size_t body_used
= 0;
1314 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
1316 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
1317 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1319 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1321 const char *req
= GET("/tor/keys/authority");
1322 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1324 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1325 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1330 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1331 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1332 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1333 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1335 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1338 UNMOCK(get_my_v3_authority_cert
);
1339 UNMOCK(connection_write_to_buf_impl_
);
1340 connection_free_(TO_CONN(conn
));
1343 authority_cert_free(mock_cert
); mock_cert
= NULL
;
1347 test_dir_handle_get_server_keys_fp_not_found(void* data
)
1349 dir_connection_t
*conn
= NULL
;
1350 char *header
= NULL
;
1353 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1355 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1357 const char *req
= GET("/tor/keys/fp/somehex");
1358 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1360 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1364 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1367 UNMOCK(connection_write_to_buf_impl_
);
1368 connection_free_(TO_CONN(conn
));
1373 test_dir_handle_get_server_keys_fp(void* data
)
1375 dir_connection_t
*conn
= NULL
;
1376 char *header
= NULL
;
1378 size_t body_used
= 0;
1379 dir_server_t
*ds
= NULL
;
1380 const char digest
[DIGEST_LEN
] = "";
1383 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1385 clear_dir_servers();
1386 routerlist_free_all();
1388 /* create a trusted ds */
1389 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1390 NULL
, V3_DIRINFO
, 1.0);
1394 /* ds v3_identity_digest is the certificate's identity_key */
1395 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1396 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1398 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1399 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
1401 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1403 sprintf(req
, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY
);
1404 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1406 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1407 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1412 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1413 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1414 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1415 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1417 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1420 UNMOCK(connection_write_to_buf_impl_
);
1421 connection_free_(TO_CONN(conn
));
1424 clear_dir_servers();
1425 routerlist_free_all();
1429 test_dir_handle_get_server_keys_sk_not_found(void* data
)
1431 dir_connection_t
*conn
= NULL
;
1432 char *header
= NULL
;
1435 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1437 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1439 const char *req
= GET("/tor/keys/sk/somehex");
1440 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1442 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1446 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1449 UNMOCK(connection_write_to_buf_impl_
);
1450 connection_free_(TO_CONN(conn
));
1455 test_dir_handle_get_server_keys_sk(void* data
)
1457 dir_connection_t
*conn
= NULL
;
1458 char *header
= NULL
;
1460 size_t body_used
= 0;
1463 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
1464 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
1465 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1467 clear_dir_servers();
1468 routerlist_free_all();
1470 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1471 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
1473 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1475 sprintf(req
, GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY
);
1476 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1478 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1479 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1484 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1485 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1486 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1487 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1489 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1492 UNMOCK(get_my_v3_authority_cert
);
1493 UNMOCK(connection_write_to_buf_impl_
);
1494 connection_free_(TO_CONN(conn
));
1495 authority_cert_free(mock_cert
); mock_cert
= NULL
;
1501 test_dir_handle_get_server_keys_fpsk_not_found(void* data
)
1503 dir_connection_t
*conn
= NULL
;
1504 char *header
= NULL
;
1507 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1509 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1511 const char *req
= GET("/tor/keys/fp-sk/somehex");
1512 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1514 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1518 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1521 UNMOCK(connection_write_to_buf_impl_
);
1522 connection_free_(TO_CONN(conn
));
1527 test_dir_handle_get_server_keys_fpsk(void* data
)
1529 dir_connection_t
*conn
= NULL
;
1530 char *header
= NULL
;
1532 size_t body_used
= 0;
1533 dir_server_t
*ds
= NULL
;
1534 const char digest
[DIGEST_LEN
] = "";
1537 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1539 clear_dir_servers();
1540 routerlist_free_all();
1542 /* create a trusted ds */
1543 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1544 NULL
, V3_DIRINFO
, 1.0);
1547 /* ds v3_identity_digest is the certificate's identity_key */
1548 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1549 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1552 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1553 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
1555 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1558 sprintf(req
, GET("/tor/keys/fp-sk/%s-%s"),
1559 TEST_CERT_IDENT_KEY
, TEST_SIGNING_KEY
);
1561 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1563 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1564 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1569 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1570 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1571 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1572 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1574 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1577 UNMOCK(connection_write_to_buf_impl_
);
1578 connection_free_(TO_CONN(conn
));
1582 clear_dir_servers();
1583 routerlist_free_all();
1587 test_dir_handle_get_server_keys_busy(void* data
)
1589 dir_connection_t
*conn
= NULL
;
1590 char *header
= NULL
;
1591 dir_server_t
*ds
= NULL
;
1592 const char digest
[DIGEST_LEN
] = "";
1595 clear_dir_servers();
1596 routerlist_free_all();
1598 /* create a trusted ds */
1599 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1600 NULL
, V3_DIRINFO
, 1.0);
1603 /* ds v3_identity_digest is the certificate's identity_key */
1604 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1605 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1608 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1609 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
1611 MOCK(get_options
, mock_get_options
);
1612 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1614 /* setup busy server */
1615 init_mock_options();
1616 mock_options
->CountPrivateBandwidth
= 1;
1618 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1620 sprintf(req
, GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY
);
1621 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1623 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1627 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1630 UNMOCK(get_options
);
1631 UNMOCK(connection_write_to_buf_impl_
);
1632 connection_free_(TO_CONN(conn
));
1634 or_options_free(mock_options
); mock_options
= NULL
;
1636 clear_dir_servers();
1637 routerlist_free_all();
1640 static networkstatus_t
*mock_ns_val
= NULL
;
1641 static networkstatus_t
*
1642 mock_ns_get_by_flavor(consensus_flavor_t f
)
1649 test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d
)
1651 dir_connection_t
*conn
= NULL
;
1652 char *header
= NULL
;
1657 mock_ns_val
= tor_malloc_zero(sizeof(networkstatus_t
));
1658 mock_ns_val
->flavor
= FLAV_NS
;
1659 mock_ns_val
->voters
= smartlist_new();
1662 init_mock_options();
1664 MOCK(get_options
, mock_get_options
);
1665 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1666 MOCK(networkstatus_get_latest_consensus_by_flavor
, mock_ns_get_by_flavor
);
1668 /* start gathering stats */
1669 mock_options
->DirReqStatistics
= 1;
1670 geoip_dirreq_stats_init(time(NULL
));
1672 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1674 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1675 GET("/tor/status-vote/current/consensus-ns/" HEX1
"+" HEX2
), NULL
, 0));
1677 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1681 tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES
, OP_EQ
, header
);
1683 stats
= geoip_format_dirreq_stats(time(NULL
));
1685 tt_assert(strstr(stats
, "not-enough-sigs=8"));
1688 UNMOCK(networkstatus_get_latest_consensus_by_flavor
);
1689 UNMOCK(connection_write_to_buf_impl_
);
1690 UNMOCK(get_options
);
1692 connection_free_(TO_CONN(conn
));
1695 smartlist_free(mock_ns_val
->voters
);
1696 tor_free(mock_ns_val
);
1697 or_options_free(mock_options
); mock_options
= NULL
;
1701 test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data
)
1703 dir_connection_t
*conn
= NULL
;
1704 char *header
= NULL
;
1708 init_mock_options();
1710 MOCK(get_options
, mock_get_options
);
1711 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1713 /* start gathering stats */
1714 mock_options
->DirReqStatistics
= 1;
1715 geoip_dirreq_stats_init(time(NULL
));
1717 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1718 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1719 GET("/tor/status-vote/current/consensus-ns"), NULL
, 0));
1721 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1724 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1726 stats
= geoip_format_dirreq_stats(time(NULL
));
1728 tt_assert(strstr(stats
, "not-found=8"));
1731 UNMOCK(connection_write_to_buf_impl_
);
1732 UNMOCK(get_options
);
1733 connection_free_(TO_CONN(conn
));
1736 or_options_free(mock_options
); mock_options
= NULL
;
1739 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1742 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1745 CALLED(geoip_get_country_by_addr
)++;
1750 status_vote_current_consensus_ns_test(char **header
, char **body
,
1753 common_digests_t digests
;
1754 dir_connection_t
*conn
= NULL
;
1756 #define NETWORK_STATUS "some network status string"
1757 dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS
, "ns", &digests
,
1760 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1762 tt_assert(mock_options
);
1763 mock_options
->DirReqStatistics
= 1;
1764 geoip_dirreq_stats_init(time(NULL
));
1766 /* init geoip database */
1767 geoip_parse_entry("10,50,AB", AF_INET
);
1768 tt_str_op("ab", OP_EQ
, geoip_get_country_name(1));
1770 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1771 TO_CONN(conn
)->address
= tor_strdup("127.0.0.1");
1773 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1774 GET("/tor/status-vote/current/consensus-ns"), NULL
, 0));
1776 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1777 body
, body_len
, strlen(NETWORK_STATUS
)+7, 0);
1780 UNMOCK(connection_write_to_buf_impl_
);
1781 connection_free_(TO_CONN(conn
));
1785 test_dir_handle_get_status_vote_current_consensus_ns(void* data
)
1787 char *header
= NULL
;
1788 char *body
= NULL
, *comp_body
= NULL
;
1789 size_t body_used
= 0, comp_body_used
= 0;
1790 char *stats
= NULL
, *hist
= NULL
;
1796 NS_MOCK(geoip_get_country_by_addr
);
1797 MOCK(get_options
, mock_get_options
);
1799 init_mock_options();
1801 status_vote_current_consensus_ns_test(&header
, &comp_body
, &comp_body_used
);
1804 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1805 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1806 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1807 tt_assert(strstr(header
, "Pragma: no-cache\r\n"));
1809 compress_method_t compression
= detect_compression_method(comp_body
,
1811 tt_int_op(ZLIB_METHOD
, OP_EQ
, compression
);
1813 tor_gzip_uncompress(&body
, &body_used
, comp_body
, comp_body_used
,
1814 compression
, 0, LOG_PROTOCOL_WARN
);
1816 tt_str_op(NETWORK_STATUS
, OP_EQ
, body
);
1817 tt_int_op(strlen(NETWORK_STATUS
), OP_EQ
, body_used
);
1819 stats
= geoip_format_dirreq_stats(time(NULL
));
1822 tt_assert(strstr(stats
, "ok=8"));
1823 tt_assert(strstr(stats
, "dirreq-v3-ips ab=8"));
1824 tt_assert(strstr(stats
, "dirreq-v3-reqs ab=8"));
1825 tt_assert(strstr(stats
, "dirreq-v3-direct-dl"
1826 " complete=0,timeout=0,running=4"));
1828 hist
= geoip_get_request_history();
1830 tt_str_op("ab=8", OP_EQ
, hist
);
1833 NS_UNMOCK(geoip_get_country_by_addr
);
1834 UNMOCK(get_options
);
1836 tor_free(comp_body
);
1840 or_options_free(mock_options
); mock_options
= NULL
;
1847 test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data
)
1849 char *header
= NULL
;
1851 size_t body_used
= 0;
1858 MOCK(get_options
, mock_get_options
);
1861 init_mock_options();
1862 mock_options
->CountPrivateBandwidth
= 1;
1864 status_vote_current_consensus_ns_test(&header
, &body
, &body_used
);
1867 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1869 stats
= geoip_format_dirreq_stats(time(NULL
));
1871 tt_assert(strstr(stats
, "busy=8"));
1874 UNMOCK(get_options
);
1877 or_options_free(mock_options
); mock_options
= NULL
;
1885 test_dir_handle_get_status_vote_current_not_found(void* data
)
1887 dir_connection_t
*conn
= NULL
;
1888 char *header
= NULL
;
1891 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1893 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1894 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1895 GET("/tor/status-vote/current/" HEX1
), NULL
, 0));
1897 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1900 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1903 UNMOCK(connection_write_to_buf_impl_
);
1904 connection_free_(TO_CONN(conn
));
1908 #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
1911 status_vote_current_d_test(char **header
, char **body
, size_t *body_l
)
1913 dir_connection_t
*conn
= NULL
;
1915 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1917 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1918 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1919 GET("/tor/status-vote/current/d/" VOTE_DIGEST
), NULL
, 0));
1921 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1922 body
, body_l
, strlen(VOTE_BODY_V3
)+1, 0);
1926 UNMOCK(connection_write_to_buf_impl_
);
1927 connection_free_(TO_CONN(conn
));
1931 status_vote_next_d_test(char **header
, char **body
, size_t *body_l
)
1933 dir_connection_t
*conn
= NULL
;
1935 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1937 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1938 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1939 GET("/tor/status-vote/next/d/" VOTE_DIGEST
), NULL
, 0));
1941 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1942 body
, body_l
, strlen(VOTE_BODY_V3
)+1, 0);
1946 UNMOCK(connection_write_to_buf_impl_
);
1947 connection_free_(TO_CONN(conn
));
1951 test_dir_handle_get_status_vote_current_d_not_found(void* data
)
1953 char *header
= NULL
;
1956 status_vote_current_d_test(&header
, NULL
, NULL
);
1959 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1966 test_dir_handle_get_status_vote_next_d_not_found(void* data
)
1968 char *header
= NULL
;
1971 status_vote_next_d_test(&header
, NULL
, NULL
);
1974 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1977 UNMOCK(connection_write_to_buf_impl_
);
1982 test_dir_handle_get_status_vote_d(void* data
)
1984 char *header
= NULL
, *body
= NULL
;
1985 size_t body_used
= 0;
1986 dir_server_t
*ds
= NULL
;
1987 const char digest
[DIGEST_LEN
] = "";
1990 clear_dir_servers();
1993 /* create a trusted ds */
1994 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1995 NULL
, V3_DIRINFO
, 1.0);
1999 /* ds v3_identity_digest is the certificate's identity_key */
2000 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
2001 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
2003 init_mock_options();
2004 mock_options
->AuthoritativeDir
= 1;
2005 mock_options
->V3AuthoritativeDir
= 1;
2006 mock_options
->TestingV3AuthVotingStartOffset
= 0;
2007 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
2008 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
2009 mock_options
->TestingV3AuthInitialDistDelay
= 1;
2011 time_t now
= 1441223455 -1;
2012 dirvote_recalculate_timing(mock_options
, now
);
2014 const char *msg_out
= NULL
;
2016 struct pending_vote_t
*pv
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
2020 status_vote_current_d_test(&header
, &body
, &body_used
);
2023 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2024 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2025 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2026 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2028 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2033 status_vote_next_d_test(&header
, &body
, &body_used
);
2036 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2037 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2038 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2039 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2041 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2046 or_options_free(mock_options
); mock_options
= NULL
;
2048 clear_dir_servers();
2053 test_dir_handle_get_status_vote_next_not_found(void* data
)
2055 dir_connection_t
*conn
= NULL
;
2056 char *header
= NULL
;
2059 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2061 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2062 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2063 GET("/tor/status-vote/next/" HEX1
), NULL
, 0));
2065 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2068 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2071 UNMOCK(connection_write_to_buf_impl_
);
2072 connection_free_(TO_CONN(conn
));
2077 status_vote_next_consensus_test(char **header
, char **body
, size_t *body_used
)
2079 dir_connection_t
*conn
= NULL
;
2081 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2083 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2084 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2085 GET("/tor/status-vote/next/consensus"), NULL
, 0));
2087 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
2088 body
, body_used
, 18, 0);
2090 UNMOCK(connection_write_to_buf_impl_
);
2091 connection_free_(TO_CONN(conn
));
2095 test_dir_handle_get_status_vote_next_consensus_not_found(void* data
)
2097 char *header
= NULL
, *body
= NULL
;
2101 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2104 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2112 test_dir_handle_get_status_vote_current_authority_not_found(void* data
)
2114 dir_connection_t
*conn
= NULL
;
2115 char *header
= NULL
;
2118 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2120 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2121 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2122 GET("/tor/status-vote/current/authority"), NULL
, 0));
2124 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2127 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2130 UNMOCK(connection_write_to_buf_impl_
);
2131 connection_free_(TO_CONN(conn
));
2136 test_dir_handle_get_status_vote_next_authority_not_found(void* data
)
2138 dir_connection_t
*conn
= NULL
;
2139 char *header
= NULL
;
2142 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2144 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2145 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2146 GET("/tor/status-vote/next/authority"), NULL
, 0));
2148 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2151 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2154 UNMOCK(connection_write_to_buf_impl_
);
2155 connection_free_(TO_CONN(conn
));
2159 NS_DECL(const char*,
2160 dirvote_get_pending_consensus
, (consensus_flavor_t flav
));
2163 NS(dirvote_get_pending_consensus
)(consensus_flavor_t flav
)
2166 return "pending consensus";
2170 test_dir_handle_get_status_vote_next_consensus(void* data
)
2172 char *header
= NULL
, *body
= NULL
;
2173 size_t body_used
= 0;
2176 NS_MOCK(dirvote_get_pending_consensus
);
2178 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2181 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2182 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2183 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2184 tt_assert(strstr(header
, "Content-Length: 17\r\n"));
2186 tt_str_op("pending consensus", OP_EQ
, body
);
2189 NS_UNMOCK(dirvote_get_pending_consensus
);
2195 test_dir_handle_get_status_vote_next_consensus_busy(void* data
)
2197 char *header
= NULL
, *body
= NULL
;
2198 size_t body_used
= 0;
2201 MOCK(get_options
, mock_get_options
);
2202 NS_MOCK(dirvote_get_pending_consensus
);
2205 init_mock_options();
2206 mock_options
->CountPrivateBandwidth
= 1;
2208 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2211 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
2214 NS_UNMOCK(dirvote_get_pending_consensus
);
2215 UNMOCK(get_options
);
2218 or_options_free(mock_options
); mock_options
= NULL
;
2222 status_vote_next_consensus_signatures_test(char **header
, char **body
,
2225 dir_connection_t
*conn
= NULL
;
2227 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2229 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2230 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2231 GET("/tor/status-vote/next/consensus-signatures"), NULL
, 0));
2233 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
2234 body
, body_used
, 22, 0);
2237 connection_free_(TO_CONN(conn
));
2238 UNMOCK(connection_write_to_buf_impl_
);
2242 test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data
)
2244 char *header
= NULL
, *body
= NULL
;
2248 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2251 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2258 NS_DECL(const char*,
2259 dirvote_get_pending_detached_signatures
, (void));
2262 NS(dirvote_get_pending_detached_signatures
)(void)
2264 return "pending detached sigs";
2268 test_dir_handle_get_status_vote_next_consensus_signatures(void* data
)
2270 char *header
= NULL
, *body
= NULL
;
2271 size_t body_used
= 0;
2274 NS_MOCK(dirvote_get_pending_detached_signatures
);
2276 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2279 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2280 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2281 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2282 tt_assert(strstr(header
, "Content-Length: 21\r\n"));
2284 tt_str_op("pending detached sigs", OP_EQ
, body
);
2287 NS_UNMOCK(dirvote_get_pending_detached_signatures
);
2293 test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data
)
2295 char *header
= NULL
, *body
= NULL
;
2299 NS_MOCK(dirvote_get_pending_detached_signatures
);
2300 MOCK(get_options
, mock_get_options
);
2303 init_mock_options();
2304 mock_options
->CountPrivateBandwidth
= 1;
2306 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2309 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
2312 UNMOCK(get_options
);
2313 NS_UNMOCK(dirvote_get_pending_detached_signatures
);
2316 or_options_free(mock_options
); mock_options
= NULL
;
2320 test_dir_handle_get_status_vote_next_authority(void* data
)
2322 dir_connection_t
*conn
= NULL
;
2323 char *header
= NULL
, *body
= NULL
;
2324 const char *msg_out
= NULL
;
2326 size_t body_used
= 0;
2327 dir_server_t
*ds
= NULL
;
2328 const char digest
[DIGEST_LEN
] = "";
2331 clear_dir_servers();
2332 routerlist_free_all();
2335 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
2337 /* create a trusted ds */
2338 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
2339 NULL
, V3_DIRINFO
, 1.0);
2343 /* ds v3_identity_digest is the certificate's identity_key */
2344 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
2345 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
2346 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
2347 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
2349 init_mock_options();
2350 mock_options
->AuthoritativeDir
= 1;
2351 mock_options
->V3AuthoritativeDir
= 1;
2352 mock_options
->TestingV3AuthVotingStartOffset
= 0;
2353 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
2354 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
2355 mock_options
->TestingV3AuthInitialDistDelay
= 1;
2357 time_t now
= 1441223455 -1;
2358 dirvote_recalculate_timing(mock_options
, now
);
2360 struct pending_vote_t
*vote
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
2364 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
2365 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2367 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2368 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2369 GET("/tor/status-vote/next/authority"), NULL
, 0));
2371 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2372 &body
, &body_used
, strlen(VOTE_BODY_V3
)+1, 0);
2375 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2376 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2377 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2378 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2380 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2383 UNMOCK(connection_write_to_buf_impl_
);
2384 UNMOCK(get_my_v3_authority_cert
);
2385 connection_free_(TO_CONN(conn
));
2388 authority_cert_free(mock_cert
); mock_cert
= NULL
;
2389 or_options_free(mock_options
); mock_options
= NULL
;
2391 clear_dir_servers();
2392 routerlist_free_all();
2397 test_dir_handle_get_status_vote_current_authority(void* data
)
2399 dir_connection_t
*conn
= NULL
;
2400 char *header
= NULL
, *body
= NULL
;
2401 const char *msg_out
= NULL
;
2403 size_t body_used
= 0;
2404 const char digest
[DIGEST_LEN
] = "";
2406 dir_server_t
*ds
= NULL
;
2409 clear_dir_servers();
2410 routerlist_free_all();
2413 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
2415 /* create a trusted ds */
2416 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
2417 NULL
, V3_DIRINFO
, 1.0);
2421 /* ds v3_identity_digest is the certificate's identity_key */
2422 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
2423 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
2425 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
2426 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1));
2428 init_mock_options();
2429 mock_options
->AuthoritativeDir
= 1;
2430 mock_options
->V3AuthoritativeDir
= 1;
2431 mock_options
->TestingV3AuthVotingStartOffset
= 0;
2432 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
2433 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
2434 mock_options
->TestingV3AuthInitialDistDelay
= 1;
2436 time_t now
= 1441223455;
2437 dirvote_recalculate_timing(mock_options
, now
-1);
2439 struct pending_vote_t
*vote
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
2443 // move the pending vote to previous vote
2444 dirvote_act(mock_options
, now
+1);
2446 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
2447 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2449 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2450 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2451 GET("/tor/status-vote/current/authority"), NULL
, 0));
2453 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2454 &body
, &body_used
, strlen(VOTE_BODY_V3
)+1, 0);
2457 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2458 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2459 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2460 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2462 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2465 UNMOCK(connection_write_to_buf_impl_
);
2466 UNMOCK(get_my_v3_authority_cert
);
2467 connection_free_(TO_CONN(conn
));
2470 authority_cert_free(mock_cert
); mock_cert
= NULL
;
2471 or_options_free(mock_options
); mock_options
= NULL
;
2473 clear_dir_servers();
2474 routerlist_free_all();
2478 #define DIR_HANDLE_CMD(name,flags) \
2479 { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
2481 struct testcase_t dir_handle_get_tests
[] = {
2482 DIR_HANDLE_CMD(not_found
, 0),
2483 DIR_HANDLE_CMD(bad_request
, 0),
2484 DIR_HANDLE_CMD(v1_command_not_found
, 0),
2485 DIR_HANDLE_CMD(v1_command
, 0),
2486 DIR_HANDLE_CMD(robots_txt
, 0),
2487 DIR_HANDLE_CMD(bytes_txt
, 0),
2488 DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted
, 0),
2489 DIR_HANDLE_CMD(rendezvous2_not_found
, 0),
2490 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id
, 0),
2491 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed
, 0),
2492 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success
, 0),
2493 DIR_HANDLE_CMD(micro_d_not_found
, 0),
2494 DIR_HANDLE_CMD(micro_d_server_busy
, 0),
2495 DIR_HANDLE_CMD(micro_d
, 0),
2496 DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth
, 0),
2497 DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth
, 0),
2498 DIR_HANDLE_CMD(networkstatus_bridges
, 0),
2499 DIR_HANDLE_CMD(server_descriptors_not_found
, 0),
2500 DIR_HANDLE_CMD(server_descriptors_busy
, TT_FORK
),
2501 DIR_HANDLE_CMD(server_descriptors_all
, TT_FORK
),
2502 DIR_HANDLE_CMD(server_descriptors_authority
, TT_FORK
),
2503 DIR_HANDLE_CMD(server_descriptors_fp
, TT_FORK
),
2504 DIR_HANDLE_CMD(server_descriptors_d
, TT_FORK
),
2505 DIR_HANDLE_CMD(server_keys_bad_req
, 0),
2506 DIR_HANDLE_CMD(server_keys_busy
, 0),
2507 DIR_HANDLE_CMD(server_keys_all_not_found
, 0),
2508 DIR_HANDLE_CMD(server_keys_all
, 0),
2509 DIR_HANDLE_CMD(server_keys_authority_not_found
, 0),
2510 DIR_HANDLE_CMD(server_keys_authority
, 0),
2511 DIR_HANDLE_CMD(server_keys_fp_not_found
, 0),
2512 DIR_HANDLE_CMD(server_keys_fp
, 0),
2513 DIR_HANDLE_CMD(server_keys_sk_not_found
, 0),
2514 DIR_HANDLE_CMD(server_keys_sk
, 0),
2515 DIR_HANDLE_CMD(server_keys_fpsk_not_found
, 0),
2516 DIR_HANDLE_CMD(server_keys_fpsk
, 0),
2517 DIR_HANDLE_CMD(status_vote_current_not_found
, 0),
2518 DIR_HANDLE_CMD(status_vote_next_not_found
, 0),
2519 DIR_HANDLE_CMD(status_vote_current_authority_not_found
, 0),
2520 DIR_HANDLE_CMD(status_vote_current_authority
, 0),
2521 DIR_HANDLE_CMD(status_vote_next_authority_not_found
, 0),
2522 DIR_HANDLE_CMD(status_vote_next_authority
, 0),
2523 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs
, 0),
2524 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found
, 0),
2525 DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy
, 0),
2526 DIR_HANDLE_CMD(status_vote_current_consensus_ns
, 0),
2527 DIR_HANDLE_CMD(status_vote_current_d_not_found
, 0),
2528 DIR_HANDLE_CMD(status_vote_next_d_not_found
, 0),
2529 DIR_HANDLE_CMD(status_vote_d
, 0),
2530 DIR_HANDLE_CMD(status_vote_next_consensus_not_found
, 0),
2531 DIR_HANDLE_CMD(status_vote_next_consensus_busy
, 0),
2532 DIR_HANDLE_CMD(status_vote_next_consensus
, 0),
2533 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found
, 0),
2534 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy
, 0),
2535 DIR_HANDLE_CMD(status_vote_next_consensus_signatures
, 0),