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 #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
42 DISABLE_GCC_WARNING(overlength
-strings
)
43 /* We allow huge string constants in the unit tests, but not in the code
46 #include "vote_descriptors.inc"
47 #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
48 ENABLE_GCC_WARNING(overlength
-strings
)
51 #define NS_MODULE dir_handle_get
54 connection_write_to_buf_mock(const char *string
, size_t len
,
55 connection_t
*conn
, int zlib
)
62 write_to_buf(string
, len
, conn
->outbuf
);
65 #define GET(path) "GET " path " HTTP/1.0\r\n\r\n"
66 #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
67 #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
68 #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
69 #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
70 "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
72 static tor_addr_t MOCK_TOR_ADDR
;
75 test_dir_handle_get_bad_request(void *data
)
77 dir_connection_t
*conn
= NULL
;
81 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
83 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
84 tt_int_op(directory_handle_command_get(conn
, "", NULL
, 0), OP_EQ
, 0);
86 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
89 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
92 UNMOCK(connection_write_to_buf_impl_
);
93 connection_free_(TO_CONN(conn
));
98 test_dir_handle_get_v1_command_not_found(void *data
)
100 dir_connection_t
*conn
= NULL
;
104 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
106 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
108 // no frontpage configured
109 tt_ptr_op(get_dirportfrontpage(), OP_EQ
, NULL
);
112 tt_int_op(directory_handle_command_get(conn
, GET("/tor/"), NULL
, 0),
115 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
118 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
121 UNMOCK(connection_write_to_buf_impl_
);
122 connection_free_(TO_CONN(conn
));
127 mock_get_dirportfrontpage(void)
129 return "HELLO FROM FRONTPAGE";
133 test_dir_handle_get_v1_command(void *data
)
135 dir_connection_t
*conn
= NULL
;
138 size_t body_used
= 0, body_len
= 0;
139 const char *exp_body
= NULL
;
142 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
143 MOCK(get_dirportfrontpage
, mock_get_dirportfrontpage
);
145 exp_body
= get_dirportfrontpage();
146 body_len
= strlen(exp_body
);
148 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
149 tt_int_op(directory_handle_command_get(conn
, GET("/tor/"), NULL
, 0),
152 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
153 &body
, &body_used
, body_len
+1, 0);
158 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
159 tt_assert(strstr(header
, "Content-Type: text/html\r\n"));
160 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
161 tt_assert(strstr(header
, "Content-Length: 20\r\n"));
163 tt_int_op(body_used
, OP_EQ
, strlen(body
));
164 tt_str_op(body
, OP_EQ
, exp_body
);
167 UNMOCK(connection_write_to_buf_impl_
);
168 UNMOCK(get_dirportfrontpage
);
169 connection_free_(TO_CONN(conn
));
175 test_dir_handle_get_not_found(void *data
)
177 dir_connection_t
*conn
= NULL
;
181 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
183 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
185 /* Unrecognized path */
186 tt_int_op(directory_handle_command_get(conn
, GET("/anything"), NULL
, 0),
188 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
191 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
194 UNMOCK(connection_write_to_buf_impl_
);
195 connection_free_(TO_CONN(conn
));
200 test_dir_handle_get_robots_txt(void *data
)
202 dir_connection_t
*conn
= NULL
;
205 size_t body_used
= 0;
208 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
210 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
212 tt_int_op(directory_handle_command_get(conn
, GET("/tor/robots.txt"),
214 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
215 &body
, &body_used
, 29, 0);
220 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
221 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
222 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
223 tt_assert(strstr(header
, "Content-Length: 28\r\n"));
225 tt_int_op(body_used
, OP_EQ
, strlen(body
));
226 tt_str_op(body
, OP_EQ
, "User-agent: *\r\nDisallow: /\r\n");
229 UNMOCK(connection_write_to_buf_impl_
);
230 connection_free_(TO_CONN(conn
));
235 #define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
237 test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data
)
239 dir_connection_t
*conn
= NULL
;
243 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
245 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
247 // connection is not encrypted
248 tt_assert(!connection_dir_is_encrypted(conn
))
250 tt_int_op(directory_handle_command_get(conn
, RENDEZVOUS2_GET(), NULL
, 0),
252 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
255 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
258 UNMOCK(connection_write_to_buf_impl_
);
259 connection_free_(TO_CONN(conn
));
264 test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
267 dir_connection_t
*conn
= NULL
;
271 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
272 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
274 // connection is encrypted
275 TO_CONN(conn
)->linked
= 1;
276 tt_assert(connection_dir_is_encrypted(conn
));
278 tt_int_op(directory_handle_command_get(conn
,
279 RENDEZVOUS2_GET("invalid-desc-id"), NULL
, 0), OP_EQ
, 0);
280 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
283 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
286 UNMOCK(connection_write_to_buf_impl_
);
287 connection_free_(TO_CONN(conn
));
292 test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data
)
294 dir_connection_t
*conn
= NULL
;
298 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
299 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
301 // connection is encrypted
302 TO_CONN(conn
)->linked
= 1;
303 tt_assert(connection_dir_is_encrypted(conn
));
305 //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
306 //case to happen. This test is the same as
307 //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
308 //We should refactor to remove the case from the switch.
310 const char *req
= RENDEZVOUS2_GET("1bababababababababababababababab");
311 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
313 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
316 tt_str_op(header
, OP_EQ
, BAD_REQUEST
);
319 UNMOCK(connection_write_to_buf_impl_
);
320 connection_free_(TO_CONN(conn
));
325 test_dir_handle_get_rendezvous2_not_found(void *data
)
327 dir_connection_t
*conn
= NULL
;
331 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
332 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
336 // connection is encrypted
337 TO_CONN(conn
)->linked
= 1;
338 tt_assert(connection_dir_is_encrypted(conn
));
340 const char *req
= RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
341 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
342 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
345 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
348 UNMOCK(connection_write_to_buf_impl_
);
349 connection_free_(TO_CONN(conn
));
351 rend_cache_free_all();
354 NS_DECL(const routerinfo_t
*, router_get_my_routerinfo
, (void));
356 static routerinfo_t
*mock_routerinfo
;
358 static const routerinfo_t
*
359 NS(router_get_my_routerinfo
)(void)
361 if (!mock_routerinfo
) {
362 mock_routerinfo
= tor_malloc_zero(sizeof(routerinfo_t
));
365 return mock_routerinfo
;
369 test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data
)
371 dir_connection_t
*conn
= NULL
;
374 size_t body_used
= 0;
377 rend_encoded_v2_service_descriptor_t
*desc_holder
= NULL
;
378 char *service_id
= NULL
;
379 char desc_id_base32
[REND_DESC_ID_V2_LEN_BASE32
+ 1];
383 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
384 NS_MOCK(router_get_my_routerinfo
);
388 /* create a valid rend service descriptor */
389 #define RECENT_TIME -10
390 generate_desc(RECENT_TIME
, &desc_holder
, &service_id
, 3);
392 tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder
->desc_str
),
395 base32_encode(desc_id_base32
, sizeof(desc_id_base32
), desc_holder
->desc_id
,
398 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
400 // connection is encrypted
401 TO_CONN(conn
)->linked
= 1;
402 tt_assert(connection_dir_is_encrypted(conn
));
404 tor_snprintf(req
, sizeof(req
), RENDEZVOUS2_GET("%s"), desc_id_base32
);
406 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
408 body_len
= strlen(desc_holder
->desc_str
);
409 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
410 &body
, &body_used
, body_len
+1, 0);
415 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
416 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
417 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
418 tt_assert(strstr(header
, "Pragma: no-cache\r\n"));
419 tor_snprintf(buff
, sizeof(buff
), "Content-Length: %ld\r\n", (long) body_len
);
420 tt_assert(strstr(header
, buff
));
422 tt_int_op(body_used
, OP_EQ
, strlen(body
));
423 tt_str_op(body
, OP_EQ
, desc_holder
->desc_str
);
426 UNMOCK(connection_write_to_buf_impl_
);
427 NS_UNMOCK(router_get_my_routerinfo
);
429 connection_free_(TO_CONN(conn
));
432 rend_encoded_v2_service_descriptor_free(desc_holder
);
433 tor_free(service_id
);
434 rend_cache_free_all();
437 #define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
439 test_dir_handle_get_micro_d_not_found(void *data
)
441 dir_connection_t
*conn
= NULL
;
445 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
447 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
448 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
449 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
451 const char *req
= MICRODESC_GET(B64_256_1
"-" B64_256_2
);
452 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
454 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
457 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
460 UNMOCK(connection_write_to_buf_impl_
);
462 connection_free_(TO_CONN(conn
));
466 static or_options_t
*mock_options
= NULL
;
468 init_mock_options(void)
470 mock_options
= tor_malloc(sizeof(or_options_t
));
471 memset(mock_options
, 0, sizeof(or_options_t
));
472 mock_options
->TestingTorNetwork
= 1;
475 static const or_options_t
*
476 mock_get_options(void)
478 tor_assert(mock_options
);
482 static const char microdesc
[] =
484 "-----BEGIN RSA PUBLIC KEY-----\n"
485 "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
486 "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
487 "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
488 "-----END RSA PUBLIC KEY-----\n";
491 test_dir_handle_get_micro_d(void *data
)
493 dir_connection_t
*conn
= NULL
;
494 microdesc_cache_t
*mc
= NULL
;
495 smartlist_t
*list
= NULL
;
496 char digest
[DIGEST256_LEN
];
497 char digest_base64
[128];
501 size_t body_used
= 0;
504 MOCK(get_options
, mock_get_options
);
505 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
509 const char *fn
= get_fname("dir_handle_datadir_test1");
510 mock_options
->DataDirectory
= tor_strdup(fn
);
513 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
));
515 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
, 0700));
518 /* Add microdesc to cache */
519 crypto_digest256(digest
, microdesc
, strlen(microdesc
), DIGEST_SHA256
);
520 base64_encode_nopad(digest_base64
, sizeof(digest_base64
),
521 (uint8_t *) digest
, DIGEST256_LEN
);
523 mc
= get_microdesc_cache();
524 list
= microdescs_add_to_cache(mc
, microdesc
, NULL
, SAVED_NOWHERE
, 0,
526 tt_int_op(1, OP_EQ
, smartlist_len(list
));
528 /* Make the request */
529 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
531 tor_snprintf(path
, sizeof(path
), MICRODESC_GET("%s"), digest_base64
);
532 tt_int_op(directory_handle_command_get(conn
, path
, NULL
, 0), OP_EQ
, 0);
534 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
535 &body
, &body_used
, strlen(microdesc
)+1, 0);
540 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
541 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
542 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
544 tt_int_op(body_used
, OP_EQ
, strlen(body
));
545 tt_str_op(body
, OP_EQ
, microdesc
);
549 UNMOCK(connection_write_to_buf_impl_
);
551 or_options_free(mock_options
); mock_options
= NULL
;
552 connection_free_(TO_CONN(conn
));
555 smartlist_free(list
);
556 microdesc_free_all();
560 test_dir_handle_get_micro_d_server_busy(void *data
)
562 dir_connection_t
*conn
= NULL
;
563 microdesc_cache_t
*mc
= NULL
;
564 smartlist_t
*list
= NULL
;
565 char digest
[DIGEST256_LEN
];
566 char digest_base64
[128];
571 MOCK(get_options
, mock_get_options
);
572 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
576 const char *fn
= get_fname("dir_handle_datadir_test2");
577 mock_options
->DataDirectory
= tor_strdup(fn
);
580 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
));
582 tt_int_op(0, OP_EQ
, mkdir(mock_options
->DataDirectory
, 0700));
585 /* Add microdesc to cache */
586 crypto_digest256(digest
, microdesc
, strlen(microdesc
), DIGEST_SHA256
);
587 base64_encode_nopad(digest_base64
, sizeof(digest_base64
),
588 (uint8_t *) digest
, DIGEST256_LEN
);
590 mc
= get_microdesc_cache();
591 list
= microdescs_add_to_cache(mc
, microdesc
, NULL
, SAVED_NOWHERE
, 0,
593 tt_int_op(1, OP_EQ
, smartlist_len(list
));
596 mock_options
->CountPrivateBandwidth
= 1;
598 /* Make the request */
599 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
601 tor_snprintf(path
, sizeof(path
), MICRODESC_GET("%s"), digest_base64
);
602 tt_int_op(directory_handle_command_get(conn
, path
, NULL
, 0), OP_EQ
, 0);
604 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
607 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
611 UNMOCK(connection_write_to_buf_impl_
);
613 or_options_free(mock_options
); mock_options
= NULL
;
614 connection_free_(TO_CONN(conn
));
616 smartlist_free(list
);
617 microdesc_free_all();
620 #define BRIDGES_PATH "/tor/networkstatus-bridges"
622 test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data
)
624 dir_connection_t
*conn
= NULL
;
628 MOCK(get_options
, mock_get_options
);
629 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
633 mock_options
->BridgeAuthoritativeDir
= 1;
634 mock_options
->BridgePassword_AuthDigest_
= tor_strdup("digest");
636 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
637 TO_CONN(conn
)->linked
= 1;
639 const char *req
= GET(BRIDGES_PATH
);
640 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
642 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
645 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
649 UNMOCK(connection_write_to_buf_impl_
);
650 or_options_free(mock_options
); mock_options
= NULL
;
651 connection_free_(TO_CONN(conn
));
656 test_dir_handle_get_networkstatus_bridges(void *data
)
658 dir_connection_t
*conn
= NULL
;
662 MOCK(get_options
, mock_get_options
);
663 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
667 mock_options
->BridgeAuthoritativeDir
= 1;
668 mock_options
->BridgePassword_AuthDigest_
= tor_malloc(DIGEST256_LEN
);
669 crypto_digest256(mock_options
->BridgePassword_AuthDigest_
,
670 "abcdefghijklm12345", 18, DIGEST_SHA256
);
672 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
673 TO_CONN(conn
)->linked
= 1;
675 const char *req
= "GET " BRIDGES_PATH
" HTTP/1.0\r\n"
676 "Authorization: Basic abcdefghijklm12345\r\n\r\n";
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_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
683 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
684 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
685 tt_assert(strstr(header
, "Content-Length: 0\r\n"));
689 UNMOCK(connection_write_to_buf_impl_
);
690 or_options_free(mock_options
); mock_options
= NULL
;
691 connection_free_(TO_CONN(conn
));
696 test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data
)
698 dir_connection_t
*conn
= NULL
;
702 MOCK(get_options
, mock_get_options
);
703 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
707 mock_options
->BridgeAuthoritativeDir
= 1;
708 mock_options
->BridgePassword_AuthDigest_
= tor_malloc(DIGEST256_LEN
);
709 crypto_digest256(mock_options
->BridgePassword_AuthDigest_
,
710 "abcdefghijklm12345", 18, DIGEST_SHA256
);
712 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
713 TO_CONN(conn
)->linked
= 1;
715 const char *req
= "GET " BRIDGES_PATH
" HTTP/1.0\r\n"
716 "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
717 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
719 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
722 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
726 UNMOCK(connection_write_to_buf_impl_
);
727 or_options_free(mock_options
); mock_options
= NULL
;
728 connection_free_(TO_CONN(conn
));
732 #define SERVER_DESC_GET(id) GET("/tor/server/" id)
734 test_dir_handle_get_server_descriptors_not_found(void* data
)
736 dir_connection_t
*conn
= NULL
;
740 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
742 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
744 const char *req
= SERVER_DESC_GET("invalid");
745 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
747 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
750 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
751 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_SERVER_BY_FP
);
754 UNMOCK(connection_write_to_buf_impl_
);
755 or_options_free(mock_options
); mock_options
= NULL
;
756 connection_free_(TO_CONN(conn
));
761 test_dir_handle_get_server_descriptors_all(void* data
)
763 dir_connection_t
*conn
= NULL
;
766 size_t body_used
= 0;
769 /* Setup fake routerlist. */
770 helper_setup_fake_routerlist();
772 //TODO: change to router_get_my_extrainfo when testing "extra" path
773 NS_MOCK(router_get_my_routerinfo
);
774 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
776 // We are one of the routers
777 routerlist_t
*our_routerlist
= router_get_routerlist();
778 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
779 mock_routerinfo
= smartlist_get(our_routerlist
->routers
, 0);
780 set_server_identity_key(mock_routerinfo
->identity_pkey
);
782 /* Treat "all" requests as if they were unencrypted */
783 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
785 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
787 const char *req
= SERVER_DESC_GET("all");
788 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
790 //TODO: Is this a BUG?
791 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
792 //which is smaller than that by annotation_len bytes
793 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
795 mock_routerinfo
->cache_info
.signed_descriptor_len
+1, 0);
800 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
801 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
802 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
804 //TODO: Is this a BUG?
805 //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
806 tt_int_op(body_used
, OP_EQ
,
807 mock_routerinfo
->cache_info
.signed_descriptor_len
);
809 tt_str_op(body
, OP_EQ
, mock_routerinfo
->cache_info
.signed_descriptor_body
+
810 mock_routerinfo
->cache_info
.annotations_len
);
811 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
814 NS_UNMOCK(router_get_my_routerinfo
);
815 UNMOCK(connection_write_to_buf_impl_
);
816 connection_free_(TO_CONN(conn
));
820 routerlist_free_all();
822 entry_guards_free_all();
827 "@uploaded-at 2014-06-08 19:20:11\n"
828 "@source \"127.0.0.1\"\n"
829 "router test000a 127.0.0.1 5000 0 7000\n"
830 "platform Tor 0.2.5.3-alpha-dev on Linux\n"
831 "protocols Link 1 2 Circuit 1\n"
832 "published 2014-06-08 19:20:11\n"
833 "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
835 "bandwidth 1073741824 1073741824 0\n"
836 "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
837 "caches-extra-info\n"
839 "-----BEGIN RSA PUBLIC KEY-----\n"
840 "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
841 "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
842 "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
843 "-----END RSA PUBLIC KEY-----\n"
845 "-----BEGIN RSA PUBLIC KEY-----\n"
846 "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
847 "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
848 "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
849 "-----END RSA PUBLIC KEY-----\n"
850 "hidden-service-dir\n"
851 "contact auth0@test.test\n"
852 "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
855 "-----BEGIN SIGNATURE-----\n"
856 "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
857 "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
858 "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
859 "-----END SIGNATURE-----\n";
862 test_dir_handle_get_server_descriptors_authority(void* data
)
864 dir_connection_t
*conn
= NULL
;
867 size_t body_used
= 0;
868 crypto_pk_t
*identity_pkey
= pk_generate(0);
871 NS_MOCK(router_get_my_routerinfo
);
872 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
875 router_get_my_routerinfo();
876 crypto_pk_get_digest(identity_pkey
,
877 mock_routerinfo
->cache_info
.identity_digest
);
879 // the digest is mine (the channel is unnecrypted, so we must allow sending)
880 set_server_identity_key(identity_pkey
);
881 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
883 /* Setup descriptor */
884 long annotation_len
= strstr(TEST_DESCRIPTOR
, "router ") - TEST_DESCRIPTOR
;
885 mock_routerinfo
->cache_info
.signed_descriptor_body
=
886 tor_strdup(TEST_DESCRIPTOR
);
887 mock_routerinfo
->cache_info
.signed_descriptor_len
=
888 strlen(TEST_DESCRIPTOR
) - annotation_len
;;
889 mock_routerinfo
->cache_info
.annotations_len
= annotation_len
;
891 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
893 const char *req
= SERVER_DESC_GET("authority");
894 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
896 //TODO: Is this a BUG?
897 //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
898 //is smaller than that by annotation_len bytes
899 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
900 &body
, &body_used
, strlen(TEST_DESCRIPTOR
)+1, 0);
905 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
906 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
907 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
909 tt_int_op(body_used
, OP_EQ
, strlen(body
));
911 tt_str_op(body
, OP_EQ
, TEST_DESCRIPTOR
+ annotation_len
);
912 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
915 NS_UNMOCK(router_get_my_routerinfo
);
916 UNMOCK(connection_write_to_buf_impl_
);
917 tor_free(mock_routerinfo
->cache_info
.signed_descriptor_body
);
918 tor_free(mock_routerinfo
);
919 connection_free_(TO_CONN(conn
));
922 crypto_pk_free(identity_pkey
);
926 test_dir_handle_get_server_descriptors_fp(void* data
)
928 dir_connection_t
*conn
= NULL
;
931 size_t body_used
= 0;
932 crypto_pk_t
*identity_pkey
= pk_generate(0);
935 NS_MOCK(router_get_my_routerinfo
);
936 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
939 router_get_my_routerinfo();
940 crypto_pk_get_digest(identity_pkey
,
941 mock_routerinfo
->cache_info
.identity_digest
);
943 // the digest is mine (the channel is unnecrypted, so we must allow sending)
944 set_server_identity_key(identity_pkey
);
945 mock_routerinfo
->cache_info
.send_unencrypted
= 1;
947 /* Setup descriptor */
948 long annotation_len
= strstr(TEST_DESCRIPTOR
, "router ") - TEST_DESCRIPTOR
;
949 mock_routerinfo
->cache_info
.signed_descriptor_body
=
950 tor_strdup(TEST_DESCRIPTOR
);
951 mock_routerinfo
->cache_info
.signed_descriptor_len
=
952 strlen(TEST_DESCRIPTOR
) - annotation_len
;
953 mock_routerinfo
->cache_info
.annotations_len
= annotation_len
;
955 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
957 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
958 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
959 const char *hex_digest
= hex_str(mock_routerinfo
->cache_info
.identity_digest
,
963 tor_snprintf(req
, sizeof(req
), SERVER_DESC_GET("fp/%s+" HEX1
"+" HEX2
),
965 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
967 //TODO: Is this a BUG?
968 //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
969 //is smaller than that by annotation_len bytes
970 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
971 &body
, &body_used
, strlen(TEST_DESCRIPTOR
)+1, 0);
976 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
977 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
978 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
980 tt_int_op(body_used
, OP_EQ
, strlen(body
));
982 tt_str_op(body
, OP_EQ
, TEST_DESCRIPTOR
+ annotation_len
);
983 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
986 NS_UNMOCK(router_get_my_routerinfo
);
987 UNMOCK(connection_write_to_buf_impl_
);
988 tor_free(mock_routerinfo
->cache_info
.signed_descriptor_body
);
989 tor_free(mock_routerinfo
);
990 connection_free_(TO_CONN(conn
));
993 crypto_pk_free(identity_pkey
);
996 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
997 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1000 test_dir_handle_get_server_descriptors_d(void* data
)
1002 dir_connection_t
*conn
= NULL
;
1003 char *header
= NULL
;
1005 size_t body_used
= 0;
1006 crypto_pk_t
*identity_pkey
= pk_generate(0);
1009 /* Setup fake routerlist. */
1010 helper_setup_fake_routerlist();
1012 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1014 /* Get one router's signed_descriptor_digest */
1015 routerlist_t
*our_routerlist
= router_get_routerlist();
1016 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
1017 routerinfo_t
*router
= smartlist_get(our_routerlist
->routers
, 0);
1018 const char *hex_digest
= hex_str(router
->cache_info
.signed_descriptor_digest
,
1021 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1023 char req_header
[155]; /* XXX Why 155? What kind of number is that?? */
1024 tor_snprintf(req_header
, sizeof(req_header
),
1025 SERVER_DESC_GET("d/%s+" HEX1
"+" HEX2
), hex_digest
);
1026 tt_int_op(directory_handle_command_get(conn
, req_header
, NULL
, 0), OP_EQ
, 0);
1028 //TODO: Is this a BUG?
1029 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
1030 //which is smaller than that by annotation_len bytes
1031 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1033 router
->cache_info
.signed_descriptor_len
+1, 0);
1038 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1039 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1040 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1042 //TODO: Is this a BUG?
1043 //This is what should be expected:
1044 //tt_int_op(body_used, OP_EQ, strlen(body));
1045 tt_int_op(body_used
, OP_EQ
, router
->cache_info
.signed_descriptor_len
);
1047 tt_str_op(body
, OP_EQ
, router
->cache_info
.signed_descriptor_body
+
1048 router
->cache_info
.annotations_len
);
1049 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
1052 UNMOCK(connection_write_to_buf_impl_
);
1053 tor_free(mock_routerinfo
);
1054 connection_free_(TO_CONN(conn
));
1057 crypto_pk_free(identity_pkey
);
1059 routerlist_free_all();
1060 nodelist_free_all();
1061 entry_guards_free_all();
1065 test_dir_handle_get_server_descriptors_busy(void* data
)
1067 dir_connection_t
*conn
= NULL
;
1068 char *header
= NULL
;
1069 crypto_pk_t
*identity_pkey
= pk_generate(0);
1072 /* Setup fake routerlist. */
1073 helper_setup_fake_routerlist();
1075 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1078 MOCK(get_options
, mock_get_options
);
1079 init_mock_options();
1080 mock_options
->CountPrivateBandwidth
= 1;
1082 /* Get one router's signed_descriptor_digest */
1083 routerlist_t
*our_routerlist
= router_get_routerlist();
1084 tt_int_op(smartlist_len(our_routerlist
->routers
), OP_GE
, 1);
1085 routerinfo_t
*router
= smartlist_get(our_routerlist
->routers
, 0);
1086 const char *hex_digest
= hex_str(router
->cache_info
.signed_descriptor_digest
,
1089 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1091 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1092 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1093 char req_header
[155]; /* XXX 155? Why 155? */
1094 tor_snprintf(req_header
, sizeof(req_header
),
1095 SERVER_DESC_GET("d/%s+" HEX1
"+" HEX2
), hex_digest
);
1096 tt_int_op(directory_handle_command_get(conn
, req_header
, NULL
, 0), OP_EQ
, 0);
1098 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1102 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1104 tt_int_op(conn
->dir_spool_src
, OP_EQ
, DIR_SPOOL_NONE
);
1107 UNMOCK(get_options
);
1108 UNMOCK(connection_write_to_buf_impl_
);
1109 tor_free(mock_routerinfo
);
1110 connection_free_(TO_CONN(conn
));
1112 crypto_pk_free(identity_pkey
);
1114 routerlist_free_all();
1115 nodelist_free_all();
1116 entry_guards_free_all();
1120 test_dir_handle_get_server_keys_bad_req(void* data
)
1122 dir_connection_t
*conn
= NULL
;
1123 char *header
= NULL
;
1126 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1128 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1130 const char *req
= GET("/tor/keys/");
1131 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1133 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1137 tt_str_op(BAD_REQUEST
, OP_EQ
, header
);
1140 UNMOCK(connection_write_to_buf_impl_
);
1141 connection_free_(TO_CONN(conn
));
1146 test_dir_handle_get_server_keys_all_not_found(void* data
)
1148 dir_connection_t
*conn
= NULL
;
1149 char *header
= NULL
;
1152 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1154 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1156 const char *req
= GET("/tor/keys/all");
1157 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1159 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1163 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1166 UNMOCK(connection_write_to_buf_impl_
);
1167 connection_free_(TO_CONN(conn
));
1171 #define TEST_CERTIFICATE AUTHORITY_CERT_3
1172 #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
1174 static const char TEST_CERT_IDENT_KEY
[] =
1175 "D867ACF56A9D229B35C25F0090BC9867E906BE69";
1178 test_dir_handle_get_server_keys_all(void* data
)
1180 dir_connection_t
*conn
= NULL
;
1181 char *header
= NULL
;
1183 size_t body_used
= 0;
1184 const char digest
[DIGEST_LEN
] = "";
1186 dir_server_t
*ds
= NULL
;
1189 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1191 clear_dir_servers();
1192 routerlist_free_all();
1194 /* create a trusted ds */
1195 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1196 NULL
, V3_DIRINFO
, 1.0);
1200 /* ds v3_identity_digest is the certificate's identity_key */
1201 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1202 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1203 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1204 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
1206 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1208 const char *req
= GET("/tor/keys/all");
1209 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1211 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1212 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1217 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1218 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1219 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1220 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1222 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1225 UNMOCK(connection_write_to_buf_impl_
);
1226 connection_free_(TO_CONN(conn
));
1230 clear_dir_servers();
1231 routerlist_free_all();
1235 test_dir_handle_get_server_keys_authority_not_found(void* data
)
1237 dir_connection_t
*conn
= NULL
;
1238 char *header
= NULL
;
1241 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1243 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1245 const char *req
= GET("/tor/keys/authority");
1246 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1248 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1252 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1255 UNMOCK(connection_write_to_buf_impl_
);
1256 connection_free_(TO_CONN(conn
));
1260 static authority_cert_t
* mock_cert
= NULL
;
1262 static authority_cert_t
*
1263 get_my_v3_authority_cert_m(void)
1265 tor_assert(mock_cert
);
1270 test_dir_handle_get_server_keys_authority(void* data
)
1272 dir_connection_t
*conn
= NULL
;
1273 char *header
= NULL
;
1275 size_t body_used
= 0;
1278 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
1280 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
1281 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1283 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1285 const char *req
= GET("/tor/keys/authority");
1286 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1288 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1289 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1294 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1295 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1296 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1297 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1299 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1302 UNMOCK(get_my_v3_authority_cert
);
1303 UNMOCK(connection_write_to_buf_impl_
);
1304 connection_free_(TO_CONN(conn
));
1307 authority_cert_free(mock_cert
); mock_cert
= NULL
;
1311 test_dir_handle_get_server_keys_fp_not_found(void* data
)
1313 dir_connection_t
*conn
= NULL
;
1314 char *header
= NULL
;
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/fp/somehex");
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
,
1328 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1331 UNMOCK(connection_write_to_buf_impl_
);
1332 connection_free_(TO_CONN(conn
));
1337 test_dir_handle_get_server_keys_fp(void* data
)
1339 dir_connection_t
*conn
= NULL
;
1340 char *header
= NULL
;
1342 size_t body_used
= 0;
1343 dir_server_t
*ds
= NULL
;
1344 const char digest
[DIGEST_LEN
] = "";
1347 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1349 clear_dir_servers();
1350 routerlist_free_all();
1352 /* create a trusted ds */
1353 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1354 NULL
, V3_DIRINFO
, 1.0);
1358 /* ds v3_identity_digest is the certificate's identity_key */
1359 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1360 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1362 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1363 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
1365 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1367 tor_snprintf(req
, sizeof(req
),
1368 GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY
);
1369 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1371 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1372 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1377 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1378 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1379 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1380 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1382 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1385 UNMOCK(connection_write_to_buf_impl_
);
1386 connection_free_(TO_CONN(conn
));
1389 clear_dir_servers();
1390 routerlist_free_all();
1394 test_dir_handle_get_server_keys_sk_not_found(void* data
)
1396 dir_connection_t
*conn
= NULL
;
1397 char *header
= NULL
;
1400 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1402 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1404 const char *req
= GET("/tor/keys/sk/somehex");
1405 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1407 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1411 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1414 UNMOCK(connection_write_to_buf_impl_
);
1415 connection_free_(TO_CONN(conn
));
1420 test_dir_handle_get_server_keys_sk(void* data
)
1422 dir_connection_t
*conn
= NULL
;
1423 char *header
= NULL
;
1425 size_t body_used
= 0;
1428 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
1429 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
1430 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1432 clear_dir_servers();
1433 routerlist_free_all();
1435 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1436 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
1438 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1440 tor_snprintf(req
, sizeof(req
),
1441 GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY
);
1442 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1444 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1445 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1450 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1451 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1452 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1453 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1455 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1458 UNMOCK(get_my_v3_authority_cert
);
1459 UNMOCK(connection_write_to_buf_impl_
);
1460 connection_free_(TO_CONN(conn
));
1461 authority_cert_free(mock_cert
); mock_cert
= NULL
;
1467 test_dir_handle_get_server_keys_fpsk_not_found(void* data
)
1469 dir_connection_t
*conn
= NULL
;
1470 char *header
= NULL
;
1473 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1475 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1477 const char *req
= GET("/tor/keys/fp-sk/somehex");
1478 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1480 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1484 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1487 UNMOCK(connection_write_to_buf_impl_
);
1488 connection_free_(TO_CONN(conn
));
1493 test_dir_handle_get_server_keys_fpsk(void* data
)
1495 dir_connection_t
*conn
= NULL
;
1496 char *header
= NULL
;
1498 size_t body_used
= 0;
1499 dir_server_t
*ds
= NULL
;
1500 const char digest
[DIGEST_LEN
] = "";
1503 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1505 clear_dir_servers();
1506 routerlist_free_all();
1508 /* create a trusted ds */
1509 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1510 NULL
, V3_DIRINFO
, 1.0);
1513 /* ds v3_identity_digest is the certificate's identity_key */
1514 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1515 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1518 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1519 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
1521 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1524 tor_snprintf(req
, sizeof(req
),
1525 GET("/tor/keys/fp-sk/%s-%s"),
1526 TEST_CERT_IDENT_KEY
, TEST_SIGNING_KEY
);
1528 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1530 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1531 &body
, &body_used
, strlen(TEST_CERTIFICATE
)+1, 0);
1536 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1537 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1538 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1539 tt_assert(strstr(header
, "Content-Length: 1883\r\n"));
1541 tt_str_op(TEST_CERTIFICATE
, OP_EQ
, body
);
1544 UNMOCK(connection_write_to_buf_impl_
);
1545 connection_free_(TO_CONN(conn
));
1549 clear_dir_servers();
1550 routerlist_free_all();
1554 test_dir_handle_get_server_keys_busy(void* data
)
1556 dir_connection_t
*conn
= NULL
;
1557 char *header
= NULL
;
1558 dir_server_t
*ds
= NULL
;
1559 const char digest
[DIGEST_LEN
] = "";
1562 clear_dir_servers();
1563 routerlist_free_all();
1565 /* create a trusted ds */
1566 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1567 NULL
, V3_DIRINFO
, 1.0);
1570 /* ds v3_identity_digest is the certificate's identity_key */
1571 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1572 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1575 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
1576 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
1578 MOCK(get_options
, mock_get_options
);
1579 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1581 /* setup busy server */
1582 init_mock_options();
1583 mock_options
->CountPrivateBandwidth
= 1;
1585 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1587 tor_snprintf(req
, sizeof(req
), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY
);
1588 tt_int_op(directory_handle_command_get(conn
, req
, NULL
, 0), OP_EQ
, 0);
1590 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1594 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1597 UNMOCK(get_options
);
1598 UNMOCK(connection_write_to_buf_impl_
);
1599 connection_free_(TO_CONN(conn
));
1601 or_options_free(mock_options
); mock_options
= NULL
;
1603 clear_dir_servers();
1604 routerlist_free_all();
1607 static networkstatus_t
*mock_ns_val
= NULL
;
1608 static networkstatus_t
*
1609 mock_ns_get_by_flavor(consensus_flavor_t f
)
1616 test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d
)
1618 dir_connection_t
*conn
= NULL
;
1619 char *header
= NULL
;
1624 mock_ns_val
= tor_malloc_zero(sizeof(networkstatus_t
));
1625 mock_ns_val
->flavor
= FLAV_NS
;
1626 mock_ns_val
->voters
= smartlist_new();
1629 init_mock_options();
1631 MOCK(get_options
, mock_get_options
);
1632 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1633 MOCK(networkstatus_get_latest_consensus_by_flavor
, mock_ns_get_by_flavor
);
1635 /* start gathering stats */
1636 mock_options
->DirReqStatistics
= 1;
1637 geoip_dirreq_stats_init(time(NULL
));
1639 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1641 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1642 GET("/tor/status-vote/current/consensus-ns/" HEX1
"+" HEX2
), NULL
, 0));
1644 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1648 tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES
, OP_EQ
, header
);
1650 stats
= geoip_format_dirreq_stats(time(NULL
));
1652 tt_assert(strstr(stats
, "not-enough-sigs=8"));
1655 UNMOCK(networkstatus_get_latest_consensus_by_flavor
);
1656 UNMOCK(connection_write_to_buf_impl_
);
1657 UNMOCK(get_options
);
1659 connection_free_(TO_CONN(conn
));
1662 smartlist_free(mock_ns_val
->voters
);
1663 tor_free(mock_ns_val
);
1664 or_options_free(mock_options
); mock_options
= NULL
;
1668 test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data
)
1670 dir_connection_t
*conn
= NULL
;
1671 char *header
= NULL
;
1675 init_mock_options();
1677 MOCK(get_options
, mock_get_options
);
1678 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1680 /* start gathering stats */
1681 mock_options
->DirReqStatistics
= 1;
1682 geoip_dirreq_stats_init(time(NULL
));
1684 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1685 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1686 GET("/tor/status-vote/current/consensus-ns"), NULL
, 0));
1688 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1691 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1693 stats
= geoip_format_dirreq_stats(time(NULL
));
1695 tt_assert(strstr(stats
, "not-found=8"));
1698 UNMOCK(connection_write_to_buf_impl_
);
1699 UNMOCK(get_options
);
1700 connection_free_(TO_CONN(conn
));
1703 or_options_free(mock_options
); mock_options
= NULL
;
1706 NS_DECL(int, geoip_get_country_by_addr
, (const tor_addr_t
*addr
));
1709 NS(geoip_get_country_by_addr
)(const tor_addr_t
*addr
)
1712 CALLED(geoip_get_country_by_addr
)++;
1717 status_vote_current_consensus_ns_test(char **header
, char **body
,
1720 common_digests_t digests
;
1721 dir_connection_t
*conn
= NULL
;
1723 #define NETWORK_STATUS "some network status string"
1724 dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS
, "ns", &digests
,
1727 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1729 tt_assert(mock_options
);
1730 mock_options
->DirReqStatistics
= 1;
1731 geoip_dirreq_stats_init(time(NULL
));
1733 /* init geoip database */
1734 geoip_parse_entry("10,50,AB", AF_INET
);
1735 tt_str_op("ab", OP_EQ
, geoip_get_country_name(1));
1737 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1738 TO_CONN(conn
)->address
= tor_strdup("127.0.0.1");
1740 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1741 GET("/tor/status-vote/current/consensus-ns"), NULL
, 0));
1743 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1744 body
, body_len
, strlen(NETWORK_STATUS
)+7, 0);
1747 UNMOCK(connection_write_to_buf_impl_
);
1748 connection_free_(TO_CONN(conn
));
1752 test_dir_handle_get_status_vote_current_consensus_ns(void* data
)
1754 char *header
= NULL
;
1755 char *body
= NULL
, *comp_body
= NULL
;
1756 size_t body_used
= 0, comp_body_used
= 0;
1757 char *stats
= NULL
, *hist
= NULL
;
1763 NS_MOCK(geoip_get_country_by_addr
);
1764 MOCK(get_options
, mock_get_options
);
1766 init_mock_options();
1768 status_vote_current_consensus_ns_test(&header
, &comp_body
, &comp_body_used
);
1771 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1772 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1773 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1774 tt_assert(strstr(header
, "Pragma: no-cache\r\n"));
1776 compress_method_t compression
= detect_compression_method(comp_body
,
1778 tt_int_op(ZLIB_METHOD
, OP_EQ
, compression
);
1780 tor_gzip_uncompress(&body
, &body_used
, comp_body
, comp_body_used
,
1781 compression
, 0, LOG_PROTOCOL_WARN
);
1783 tt_str_op(NETWORK_STATUS
, OP_EQ
, body
);
1784 tt_int_op(strlen(NETWORK_STATUS
), OP_EQ
, body_used
);
1786 stats
= geoip_format_dirreq_stats(time(NULL
));
1789 tt_assert(strstr(stats
, "ok=8"));
1790 tt_assert(strstr(stats
, "dirreq-v3-ips ab=8"));
1791 tt_assert(strstr(stats
, "dirreq-v3-reqs ab=8"));
1792 tt_assert(strstr(stats
, "dirreq-v3-direct-dl"
1793 " complete=0,timeout=0,running=4"));
1795 hist
= geoip_get_request_history();
1797 tt_str_op("ab=8", OP_EQ
, hist
);
1800 NS_UNMOCK(geoip_get_country_by_addr
);
1801 UNMOCK(get_options
);
1803 tor_free(comp_body
);
1807 or_options_free(mock_options
); mock_options
= NULL
;
1814 test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data
)
1816 char *header
= NULL
;
1818 size_t body_used
= 0;
1825 MOCK(get_options
, mock_get_options
);
1828 init_mock_options();
1829 mock_options
->CountPrivateBandwidth
= 1;
1831 status_vote_current_consensus_ns_test(&header
, &body
, &body_used
);
1834 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
1836 stats
= geoip_format_dirreq_stats(time(NULL
));
1838 tt_assert(strstr(stats
, "busy=8"));
1841 UNMOCK(get_options
);
1844 or_options_free(mock_options
); mock_options
= NULL
;
1852 test_dir_handle_get_status_vote_current_not_found(void* data
)
1854 dir_connection_t
*conn
= NULL
;
1855 char *header
= NULL
;
1858 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1860 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1861 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1862 GET("/tor/status-vote/current/" HEX1
), NULL
, 0));
1864 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
1867 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1870 UNMOCK(connection_write_to_buf_impl_
);
1871 connection_free_(TO_CONN(conn
));
1875 #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
1878 status_vote_current_d_test(char **header
, char **body
, size_t *body_l
)
1880 dir_connection_t
*conn
= NULL
;
1882 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1884 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1885 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1886 GET("/tor/status-vote/current/d/" VOTE_DIGEST
), NULL
, 0));
1888 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1889 body
, body_l
, strlen(VOTE_BODY_V3
)+1, 0);
1893 UNMOCK(connection_write_to_buf_impl_
);
1894 connection_free_(TO_CONN(conn
));
1898 status_vote_next_d_test(char **header
, char **body
, size_t *body_l
)
1900 dir_connection_t
*conn
= NULL
;
1902 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
1904 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
1905 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
1906 GET("/tor/status-vote/next/d/" VOTE_DIGEST
), NULL
, 0));
1908 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
1909 body
, body_l
, strlen(VOTE_BODY_V3
)+1, 0);
1913 UNMOCK(connection_write_to_buf_impl_
);
1914 connection_free_(TO_CONN(conn
));
1918 test_dir_handle_get_status_vote_current_d_not_found(void* data
)
1920 char *header
= NULL
;
1923 status_vote_current_d_test(&header
, NULL
, NULL
);
1926 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1933 test_dir_handle_get_status_vote_next_d_not_found(void* data
)
1935 char *header
= NULL
;
1938 status_vote_next_d_test(&header
, NULL
, NULL
);
1941 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
1944 UNMOCK(connection_write_to_buf_impl_
);
1949 test_dir_handle_get_status_vote_d(void* data
)
1951 char *header
= NULL
, *body
= NULL
;
1952 size_t body_used
= 0;
1953 dir_server_t
*ds
= NULL
;
1954 const char digest
[DIGEST_LEN
] = "";
1957 clear_dir_servers();
1960 /* create a trusted ds */
1961 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
1962 NULL
, V3_DIRINFO
, 1.0);
1966 /* ds v3_identity_digest is the certificate's identity_key */
1967 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
1968 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
1970 init_mock_options();
1971 mock_options
->AuthoritativeDir
= 1;
1972 mock_options
->V3AuthoritativeDir
= 1;
1973 mock_options
->TestingV3AuthVotingStartOffset
= 0;
1974 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
1975 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
1976 mock_options
->TestingV3AuthInitialDistDelay
= 1;
1978 time_t now
= 1441223455 -1;
1979 dirvote_recalculate_timing(mock_options
, now
);
1981 const char *msg_out
= NULL
;
1983 struct pending_vote_t
*pv
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
1987 status_vote_current_d_test(&header
, &body
, &body_used
);
1990 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
1991 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
1992 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
1993 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
1995 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2000 status_vote_next_d_test(&header
, &body
, &body_used
);
2003 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2004 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2005 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2006 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2008 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2013 or_options_free(mock_options
); mock_options
= NULL
;
2015 clear_dir_servers();
2020 test_dir_handle_get_status_vote_next_not_found(void* data
)
2022 dir_connection_t
*conn
= NULL
;
2023 char *header
= NULL
;
2026 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2028 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2029 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2030 GET("/tor/status-vote/next/" HEX1
), NULL
, 0));
2032 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2035 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2038 UNMOCK(connection_write_to_buf_impl_
);
2039 connection_free_(TO_CONN(conn
));
2044 status_vote_next_consensus_test(char **header
, char **body
, size_t *body_used
)
2046 dir_connection_t
*conn
= NULL
;
2048 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2050 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2051 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2052 GET("/tor/status-vote/next/consensus"), NULL
, 0));
2054 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
2055 body
, body_used
, 18, 0);
2057 UNMOCK(connection_write_to_buf_impl_
);
2058 connection_free_(TO_CONN(conn
));
2062 test_dir_handle_get_status_vote_next_consensus_not_found(void* data
)
2064 char *header
= NULL
, *body
= NULL
;
2068 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2071 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2079 test_dir_handle_get_status_vote_current_authority_not_found(void* data
)
2081 dir_connection_t
*conn
= NULL
;
2082 char *header
= NULL
;
2085 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2087 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2088 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2089 GET("/tor/status-vote/current/authority"), NULL
, 0));
2091 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2094 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2097 UNMOCK(connection_write_to_buf_impl_
);
2098 connection_free_(TO_CONN(conn
));
2103 test_dir_handle_get_status_vote_next_authority_not_found(void* data
)
2105 dir_connection_t
*conn
= NULL
;
2106 char *header
= NULL
;
2109 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2111 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2112 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2113 GET("/tor/status-vote/next/authority"), NULL
, 0));
2115 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2118 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2121 UNMOCK(connection_write_to_buf_impl_
);
2122 connection_free_(TO_CONN(conn
));
2126 NS_DECL(const char*,
2127 dirvote_get_pending_consensus
, (consensus_flavor_t flav
));
2130 NS(dirvote_get_pending_consensus
)(consensus_flavor_t flav
)
2133 return "pending consensus";
2137 test_dir_handle_get_status_vote_next_consensus(void* data
)
2139 char *header
= NULL
, *body
= NULL
;
2140 size_t body_used
= 0;
2143 NS_MOCK(dirvote_get_pending_consensus
);
2145 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2148 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2149 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2150 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2151 tt_assert(strstr(header
, "Content-Length: 17\r\n"));
2153 tt_str_op("pending consensus", OP_EQ
, body
);
2156 NS_UNMOCK(dirvote_get_pending_consensus
);
2162 test_dir_handle_get_status_vote_next_consensus_busy(void* data
)
2164 char *header
= NULL
, *body
= NULL
;
2165 size_t body_used
= 0;
2168 MOCK(get_options
, mock_get_options
);
2169 NS_MOCK(dirvote_get_pending_consensus
);
2172 init_mock_options();
2173 mock_options
->CountPrivateBandwidth
= 1;
2175 status_vote_next_consensus_test(&header
, &body
, &body_used
);
2178 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
2181 NS_UNMOCK(dirvote_get_pending_consensus
);
2182 UNMOCK(get_options
);
2185 or_options_free(mock_options
); mock_options
= NULL
;
2189 status_vote_next_consensus_signatures_test(char **header
, char **body
,
2192 dir_connection_t
*conn
= NULL
;
2194 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2196 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2197 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2198 GET("/tor/status-vote/next/consensus-signatures"), NULL
, 0));
2200 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, header
, MAX_HEADERS_SIZE
,
2201 body
, body_used
, 22, 0);
2204 connection_free_(TO_CONN(conn
));
2205 UNMOCK(connection_write_to_buf_impl_
);
2209 test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data
)
2211 char *header
= NULL
, *body
= NULL
;
2215 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2218 tt_str_op(NOT_FOUND
, OP_EQ
, header
);
2225 NS_DECL(const char*,
2226 dirvote_get_pending_detached_signatures
, (void));
2229 NS(dirvote_get_pending_detached_signatures
)(void)
2231 return "pending detached sigs";
2235 test_dir_handle_get_status_vote_next_consensus_signatures(void* data
)
2237 char *header
= NULL
, *body
= NULL
;
2238 size_t body_used
= 0;
2241 NS_MOCK(dirvote_get_pending_detached_signatures
);
2243 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2246 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2247 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2248 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2249 tt_assert(strstr(header
, "Content-Length: 21\r\n"));
2251 tt_str_op("pending detached sigs", OP_EQ
, body
);
2254 NS_UNMOCK(dirvote_get_pending_detached_signatures
);
2260 test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data
)
2262 char *header
= NULL
, *body
= NULL
;
2266 NS_MOCK(dirvote_get_pending_detached_signatures
);
2267 MOCK(get_options
, mock_get_options
);
2270 init_mock_options();
2271 mock_options
->CountPrivateBandwidth
= 1;
2273 status_vote_next_consensus_signatures_test(&header
, &body
, &body_used
);
2276 tt_str_op(SERVER_BUSY
, OP_EQ
, header
);
2279 UNMOCK(get_options
);
2280 NS_UNMOCK(dirvote_get_pending_detached_signatures
);
2283 or_options_free(mock_options
); mock_options
= NULL
;
2287 test_dir_handle_get_status_vote_next_authority(void* data
)
2289 dir_connection_t
*conn
= NULL
;
2290 char *header
= NULL
, *body
= NULL
;
2291 const char *msg_out
= NULL
;
2293 size_t body_used
= 0;
2294 dir_server_t
*ds
= NULL
;
2295 const char digest
[DIGEST_LEN
] = "";
2298 clear_dir_servers();
2299 routerlist_free_all();
2302 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
2304 /* create a trusted ds */
2305 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
2306 NULL
, V3_DIRINFO
, 1.0);
2310 /* ds v3_identity_digest is the certificate's identity_key */
2311 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
2312 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
2313 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
2314 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
2316 init_mock_options();
2317 mock_options
->AuthoritativeDir
= 1;
2318 mock_options
->V3AuthoritativeDir
= 1;
2319 mock_options
->TestingV3AuthVotingStartOffset
= 0;
2320 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
2321 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
2322 mock_options
->TestingV3AuthInitialDistDelay
= 1;
2324 time_t now
= 1441223455 -1;
2325 dirvote_recalculate_timing(mock_options
, now
);
2327 struct pending_vote_t
*vote
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
2331 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
2332 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2334 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2335 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2336 GET("/tor/status-vote/next/authority"), NULL
, 0));
2338 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2339 &body
, &body_used
, strlen(VOTE_BODY_V3
)+1, 0);
2342 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2343 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2344 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2345 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2347 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2350 UNMOCK(connection_write_to_buf_impl_
);
2351 UNMOCK(get_my_v3_authority_cert
);
2352 connection_free_(TO_CONN(conn
));
2355 authority_cert_free(mock_cert
); mock_cert
= NULL
;
2356 or_options_free(mock_options
); mock_options
= NULL
;
2358 clear_dir_servers();
2359 routerlist_free_all();
2364 test_dir_handle_get_status_vote_current_authority(void* data
)
2366 dir_connection_t
*conn
= NULL
;
2367 char *header
= NULL
, *body
= NULL
;
2368 const char *msg_out
= NULL
;
2370 size_t body_used
= 0;
2371 const char digest
[DIGEST_LEN
] = "";
2373 dir_server_t
*ds
= NULL
;
2376 clear_dir_servers();
2377 routerlist_free_all();
2380 mock_cert
= authority_cert_parse_from_string(TEST_CERTIFICATE
, NULL
);
2382 /* create a trusted ds */
2383 ds
= trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL
, digest
,
2384 NULL
, V3_DIRINFO
, 1.0);
2388 /* ds v3_identity_digest is the certificate's identity_key */
2389 base16_decode(ds
->v3_identity_digest
, DIGEST_LEN
,
2390 TEST_CERT_IDENT_KEY
, HEX_DIGEST_LEN
);
2392 tt_int_op(0, OP_EQ
, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE
,
2393 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST
, 1, NULL
));
2395 init_mock_options();
2396 mock_options
->AuthoritativeDir
= 1;
2397 mock_options
->V3AuthoritativeDir
= 1;
2398 mock_options
->TestingV3AuthVotingStartOffset
= 0;
2399 mock_options
->TestingV3AuthInitialVotingInterval
= 1;
2400 mock_options
->TestingV3AuthInitialVoteDelay
= 1;
2401 mock_options
->TestingV3AuthInitialDistDelay
= 1;
2403 time_t now
= 1441223455;
2404 dirvote_recalculate_timing(mock_options
, now
-1);
2406 struct pending_vote_t
*vote
= dirvote_add_vote(VOTE_BODY_V3
, &msg_out
,
2410 // move the pending vote to previous vote
2411 dirvote_act(mock_options
, now
+1);
2413 MOCK(get_my_v3_authority_cert
, get_my_v3_authority_cert_m
);
2414 MOCK(connection_write_to_buf_impl_
, connection_write_to_buf_mock
);
2416 conn
= dir_connection_new(tor_addr_family(&MOCK_TOR_ADDR
));
2417 tt_int_op(0, OP_EQ
, directory_handle_command_get(conn
,
2418 GET("/tor/status-vote/current/authority"), NULL
, 0));
2420 fetch_from_buf_http(TO_CONN(conn
)->outbuf
, &header
, MAX_HEADERS_SIZE
,
2421 &body
, &body_used
, strlen(VOTE_BODY_V3
)+1, 0);
2424 tt_ptr_op(strstr(header
, "HTTP/1.0 200 OK\r\n"), OP_EQ
, header
);
2425 tt_assert(strstr(header
, "Content-Type: text/plain\r\n"));
2426 tt_assert(strstr(header
, "Content-Encoding: identity\r\n"));
2427 tt_assert(strstr(header
, "Content-Length: 4135\r\n"));
2429 tt_str_op(VOTE_BODY_V3
, OP_EQ
, body
);
2432 UNMOCK(connection_write_to_buf_impl_
);
2433 UNMOCK(get_my_v3_authority_cert
);
2434 connection_free_(TO_CONN(conn
));
2437 authority_cert_free(mock_cert
); mock_cert
= NULL
;
2438 or_options_free(mock_options
); mock_options
= NULL
;
2440 clear_dir_servers();
2441 routerlist_free_all();
2445 #define DIR_HANDLE_CMD(name,flags) \
2446 { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
2448 struct testcase_t dir_handle_get_tests
[] = {
2449 DIR_HANDLE_CMD(not_found
, 0),
2450 DIR_HANDLE_CMD(bad_request
, 0),
2451 DIR_HANDLE_CMD(v1_command_not_found
, 0),
2452 DIR_HANDLE_CMD(v1_command
, 0),
2453 DIR_HANDLE_CMD(robots_txt
, 0),
2454 DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted
, 0),
2455 DIR_HANDLE_CMD(rendezvous2_not_found
, 0),
2456 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id
, 0),
2457 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed
, 0),
2458 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success
, 0),
2459 DIR_HANDLE_CMD(micro_d_not_found
, 0),
2460 DIR_HANDLE_CMD(micro_d_server_busy
, 0),
2461 DIR_HANDLE_CMD(micro_d
, 0),
2462 DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth
, 0),
2463 DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth
, 0),
2464 DIR_HANDLE_CMD(networkstatus_bridges
, 0),
2465 DIR_HANDLE_CMD(server_descriptors_not_found
, 0),
2466 DIR_HANDLE_CMD(server_descriptors_busy
, TT_FORK
),
2467 DIR_HANDLE_CMD(server_descriptors_all
, TT_FORK
),
2468 DIR_HANDLE_CMD(server_descriptors_authority
, TT_FORK
),
2469 DIR_HANDLE_CMD(server_descriptors_fp
, TT_FORK
),
2470 DIR_HANDLE_CMD(server_descriptors_d
, TT_FORK
),
2471 DIR_HANDLE_CMD(server_keys_bad_req
, 0),
2472 DIR_HANDLE_CMD(server_keys_busy
, 0),
2473 DIR_HANDLE_CMD(server_keys_all_not_found
, 0),
2474 DIR_HANDLE_CMD(server_keys_all
, 0),
2475 DIR_HANDLE_CMD(server_keys_authority_not_found
, 0),
2476 DIR_HANDLE_CMD(server_keys_authority
, 0),
2477 DIR_HANDLE_CMD(server_keys_fp_not_found
, 0),
2478 DIR_HANDLE_CMD(server_keys_fp
, 0),
2479 DIR_HANDLE_CMD(server_keys_sk_not_found
, 0),
2480 DIR_HANDLE_CMD(server_keys_sk
, 0),
2481 DIR_HANDLE_CMD(server_keys_fpsk_not_found
, 0),
2482 DIR_HANDLE_CMD(server_keys_fpsk
, 0),
2483 DIR_HANDLE_CMD(status_vote_current_not_found
, 0),
2484 DIR_HANDLE_CMD(status_vote_next_not_found
, 0),
2485 DIR_HANDLE_CMD(status_vote_current_authority_not_found
, 0),
2486 DIR_HANDLE_CMD(status_vote_current_authority
, 0),
2487 DIR_HANDLE_CMD(status_vote_next_authority_not_found
, 0),
2488 DIR_HANDLE_CMD(status_vote_next_authority
, 0),
2489 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs
, 0),
2490 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found
, 0),
2491 DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy
, 0),
2492 DIR_HANDLE_CMD(status_vote_current_consensus_ns
, 0),
2493 DIR_HANDLE_CMD(status_vote_current_d_not_found
, 0),
2494 DIR_HANDLE_CMD(status_vote_next_d_not_found
, 0),
2495 DIR_HANDLE_CMD(status_vote_d
, 0),
2496 DIR_HANDLE_CMD(status_vote_next_consensus_not_found
, 0),
2497 DIR_HANDLE_CMD(status_vote_next_consensus_busy
, 0),
2498 DIR_HANDLE_CMD(status_vote_next_consensus
, 0),
2499 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found
, 0),
2500 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy
, 0),
2501 DIR_HANDLE_CMD(status_vote_next_consensus_signatures
, 0),