Split the authority-cert and signature/hash code from routerparse
[tor.git] / src / test / test_dir_handle_get.c
blob7cae45f1310507a646b251b8dcb030462a1ef764
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2018, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #define RENDCOMMON_PRIVATE
7 #define GEOIP_PRIVATE
8 #define CONNECTION_PRIVATE
9 #define CONFIG_PRIVATE
10 #define RENDCACHE_PRIVATE
11 #define DIRCACHE_PRIVATE
13 #include "core/or/or.h"
14 #include "app/config/config.h"
15 #include "core/mainloop/connection.h"
16 #include "feature/dircache/consdiffmgr.h"
17 #include "feature/dircommon/directory.h"
18 #include "feature/dircache/dircache.h"
19 #include "test/test.h"
20 #include "lib/compress/compress.h"
21 #include "feature/rend/rendcommon.h"
22 #include "feature/rend/rendcache.h"
23 #include "feature/relay/router.h"
24 #include "feature/nodelist/authcert.h"
25 #include "feature/nodelist/dirlist.h"
26 #include "feature/nodelist/routerlist.h"
27 #include "test/rend_test_helpers.h"
28 #include "feature/nodelist/microdesc.h"
29 #include "test/test_helpers.h"
30 #include "feature/nodelist/nodelist.h"
31 #include "feature/client/entrynodes.h"
32 #include "feature/dirparse/authcert_parse.h"
33 #include "feature/dirparse/routerparse.h"
34 #include "feature/nodelist/networkstatus.h"
35 #include "core/proto/proto_http.h"
36 #include "lib/geoip/geoip.h"
37 #include "feature/stats/geoip_stats.h"
38 #include "feature/dircache/dirserv.h"
39 #include "feature/dirauth/dirvote.h"
40 #include "test/log_test_helpers.h"
41 #include "feature/dircommon/voting_schedule.h"
43 #include "feature/dircommon/dir_connection_st.h"
44 #include "feature/dirclient/dir_server_st.h"
45 #include "feature/nodelist/networkstatus_st.h"
46 #include "feature/rend/rend_encoded_v2_service_descriptor_st.h"
47 #include "feature/nodelist/routerinfo_st.h"
48 #include "feature/nodelist/routerlist_st.h"
50 #ifdef _WIN32
51 /* For mkdir() */
52 #include <direct.h>
53 #else
54 #include <dirent.h>
55 #endif /* defined(_WIN32) */
57 #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
58 DISABLE_GCC_WARNING(overlength-strings)
59 /* We allow huge string constants in the unit tests, but not in the code
60 * at large. */
61 #endif
62 #include "vote_descriptors.inc"
63 #ifdef HAVE_CFLAG_WOVERLENGTH_STRINGS
64 ENABLE_GCC_WARNING(overlength-strings)
65 #endif
67 #define NS_MODULE dir_handle_get
69 #define NOT_FOUND "HTTP/1.0 404 Not found\r\n\r\n"
70 #define BAD_REQUEST "HTTP/1.0 400 Bad request\r\n\r\n"
71 #define SERVER_BUSY "HTTP/1.0 503 Directory busy, try again later\r\n\r\n"
72 #define TOO_OLD "HTTP/1.0 404 Consensus is too old\r\n\r\n"
73 #define NOT_ENOUGH_CONSENSUS_SIGNATURES "HTTP/1.0 404 " \
74 "Consensus not signed by sufficient number of requested authorities\r\n\r\n"
76 static dir_connection_t *
77 new_dir_conn(void)
79 dir_connection_t *conn = dir_connection_new(AF_INET);
80 tor_addr_from_ipv4h(&conn->base_.addr, 0x7f000001);
81 TO_CONN(conn)->address = tor_strdup("127.0.0.1");
82 return conn;
85 static void
86 test_dir_handle_get_bad_request(void *data)
88 dir_connection_t *conn = NULL;
89 char *header = NULL;
90 (void) data;
92 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
94 conn = new_dir_conn();
95 tt_int_op(directory_handle_command_get(conn, "", NULL, 0), OP_EQ, 0);
97 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
98 NULL, NULL, 1, 0);
100 tt_str_op(header, OP_EQ, BAD_REQUEST);
102 done:
103 UNMOCK(connection_write_to_buf_impl_);
104 connection_free_minimal(TO_CONN(conn));
105 tor_free(header);
108 static void
109 test_dir_handle_get_v1_command_not_found(void *data)
111 dir_connection_t *conn = NULL;
112 char *header = NULL;
113 (void) data;
115 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
117 conn = new_dir_conn();
119 // no frontpage configured
120 tt_ptr_op(get_dirportfrontpage(), OP_EQ, NULL);
122 /* V1 path */
123 tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
124 OP_EQ, 0);
126 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
127 NULL, NULL, 1, 0);
129 tt_str_op(NOT_FOUND, OP_EQ, header);
131 done:
132 UNMOCK(connection_write_to_buf_impl_);
133 connection_free_minimal(TO_CONN(conn));
134 tor_free(header);
137 static const char*
138 mock_get_dirportfrontpage(void)
140 return "HELLO FROM FRONTPAGE";
143 static void
144 test_dir_handle_get_v1_command(void *data)
146 dir_connection_t *conn = NULL;
147 char *header = NULL;
148 char *body = NULL;
149 size_t body_used = 0, body_len = 0;
150 const char *exp_body = NULL;
151 (void) data;
153 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
154 MOCK(get_dirportfrontpage, mock_get_dirportfrontpage);
156 exp_body = get_dirportfrontpage();
157 body_len = strlen(exp_body);
159 conn = new_dir_conn();
160 tt_int_op(directory_handle_command_get(conn, GET("/tor/"), NULL, 0),
161 OP_EQ, 0);
163 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
164 &body, &body_used, body_len+1, 0);
166 tt_assert(header);
167 tt_assert(body);
169 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
170 tt_assert(strstr(header, "Content-Type: text/html\r\n"));
171 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
172 tt_assert(strstr(header, "Content-Length: 20\r\n"));
174 tt_int_op(body_used, OP_EQ, strlen(body));
175 tt_str_op(body, OP_EQ, exp_body);
177 done:
178 UNMOCK(connection_write_to_buf_impl_);
179 UNMOCK(get_dirportfrontpage);
180 connection_free_minimal(TO_CONN(conn));
181 tor_free(header);
182 tor_free(body);
185 static void
186 test_dir_handle_get_not_found(void *data)
188 dir_connection_t *conn = NULL;
189 char *header = NULL;
190 (void) data;
192 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
194 conn = new_dir_conn();
196 /* Unrecognized path */
197 tt_int_op(directory_handle_command_get(conn, GET("/anything"), NULL, 0),
198 OP_EQ, 0);
199 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
200 NULL, NULL, 1, 0);
202 tt_str_op(NOT_FOUND, OP_EQ, header);
204 done:
205 UNMOCK(connection_write_to_buf_impl_);
206 connection_free_minimal(TO_CONN(conn));
207 tor_free(header);
210 static void
211 test_dir_handle_get_robots_txt(void *data)
213 dir_connection_t *conn = NULL;
214 char *header = NULL;
215 char *body = NULL;
216 size_t body_used = 0;
217 (void) data;
219 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
221 conn = new_dir_conn();
223 tt_int_op(directory_handle_command_get(conn, GET("/tor/robots.txt"),
224 NULL, 0), OP_EQ, 0);
225 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
226 &body, &body_used, 29, 0);
228 tt_assert(header);
229 tt_assert(body);
231 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
232 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
233 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
234 tt_assert(strstr(header, "Content-Length: 28\r\n"));
236 tt_int_op(body_used, OP_EQ, strlen(body));
237 tt_str_op(body, OP_EQ, "User-agent: *\r\nDisallow: /\r\n");
239 done:
240 UNMOCK(connection_write_to_buf_impl_);
241 connection_free_minimal(TO_CONN(conn));
242 tor_free(header);
243 tor_free(body);
246 #define RENDEZVOUS2_GET(descid) GET("/tor/rendezvous2/" descid)
247 static void
248 test_dir_handle_get_rendezvous2_not_found_if_not_encrypted(void *data)
250 dir_connection_t *conn = NULL;
251 char *header = NULL;
252 (void) data;
254 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
256 conn = new_dir_conn();
258 // connection is not encrypted
259 tt_assert(!connection_dir_is_encrypted(conn))
261 tt_int_op(directory_handle_command_get(conn, RENDEZVOUS2_GET(), NULL, 0),
262 OP_EQ, 0);
263 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
264 NULL, NULL, 1, 0);
266 tt_str_op(NOT_FOUND, OP_EQ, header);
268 done:
269 UNMOCK(connection_write_to_buf_impl_);
270 connection_free_minimal(TO_CONN(conn));
271 tor_free(header);
274 static void
275 test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id(
276 void *data)
278 dir_connection_t *conn = NULL;
279 char *header = NULL;
280 (void) data;
282 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
283 conn = new_dir_conn();
285 // connection is encrypted
286 TO_CONN(conn)->linked = 1;
287 tt_assert(connection_dir_is_encrypted(conn));
289 tt_int_op(directory_handle_command_get(conn,
290 RENDEZVOUS2_GET("invalid-desc-id"), NULL, 0), OP_EQ, 0);
291 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
292 NULL, NULL, 1, 0);
294 tt_str_op(header, OP_EQ, BAD_REQUEST);
296 done:
297 UNMOCK(connection_write_to_buf_impl_);
298 connection_free_minimal(TO_CONN(conn));
299 tor_free(header);
302 static void
303 test_dir_handle_get_rendezvous2_on_encrypted_conn_not_well_formed(void *data)
305 dir_connection_t *conn = NULL;
306 char *header = NULL;
307 (void) data;
309 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
310 conn = new_dir_conn();
312 // connection is encrypted
313 TO_CONN(conn)->linked = 1;
314 tt_assert(connection_dir_is_encrypted(conn));
316 //TODO: this cant be reached because rend_valid_descriptor_id() prevents this
317 //case to happen. This test is the same as
318 //test_dir_handle_get_rendezvous2_on_encrypted_conn_with_invalid_desc_id
319 //We should refactor to remove the case from the switch.
321 const char *req = RENDEZVOUS2_GET("1bababababababababababababababab");
322 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
324 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
325 NULL, NULL, 1, 0);
327 tt_str_op(header, OP_EQ, BAD_REQUEST);
329 done:
330 UNMOCK(connection_write_to_buf_impl_);
331 connection_free_minimal(TO_CONN(conn));
332 tor_free(header);
335 static void
336 test_dir_handle_get_rendezvous2_not_found(void *data)
338 dir_connection_t *conn = NULL;
339 char *header = NULL;
340 (void) data;
342 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
343 conn = new_dir_conn();
345 rend_cache_init();
347 // connection is encrypted
348 TO_CONN(conn)->linked = 1;
349 tt_assert(connection_dir_is_encrypted(conn));
351 const char *req = RENDEZVOUS2_GET("3xqunszqnaolrrfmtzgaki7mxelgvkje");
352 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
353 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
354 NULL, NULL, 1, 0);
356 tt_str_op(NOT_FOUND, OP_EQ, header);
358 done:
359 UNMOCK(connection_write_to_buf_impl_);
360 connection_free_minimal(TO_CONN(conn));
361 tor_free(header);
362 rend_cache_free_all();
365 NS_DECL(const routerinfo_t *, router_get_my_routerinfo, (void));
367 static routerinfo_t *mock_routerinfo;
369 static const routerinfo_t *
370 NS(router_get_my_routerinfo)(void)
372 if (!mock_routerinfo) {
373 mock_routerinfo = tor_malloc_zero(sizeof(routerinfo_t));
376 return mock_routerinfo;
379 static void
380 test_dir_handle_get_rendezvous2_on_encrypted_conn_success(void *data)
382 dir_connection_t *conn = NULL;
383 char *header = NULL;
384 char *body = NULL;
385 size_t body_used = 0;
386 char buff[30];
387 char req[70];
388 rend_encoded_v2_service_descriptor_t *desc_holder = NULL;
389 char *service_id = NULL;
390 char desc_id_base32[REND_DESC_ID_V2_LEN_BASE32 + 1];
391 size_t body_len = 0;
392 (void) data;
394 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
395 NS_MOCK(router_get_my_routerinfo);
397 rend_cache_init();
399 /* create a valid rend service descriptor */
400 #define RECENT_TIME -10
401 generate_desc(RECENT_TIME, &desc_holder, &service_id, 3);
403 tt_int_op(rend_cache_store_v2_desc_as_dir(desc_holder->desc_str),
404 OP_EQ, 0);
406 base32_encode(desc_id_base32, sizeof(desc_id_base32), desc_holder->desc_id,
407 DIGEST_LEN);
409 conn = new_dir_conn();
411 // connection is encrypted
412 TO_CONN(conn)->linked = 1;
413 tt_assert(connection_dir_is_encrypted(conn));
415 tor_snprintf(req, sizeof(req), RENDEZVOUS2_GET("%s"), desc_id_base32);
417 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
419 body_len = strlen(desc_holder->desc_str);
420 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
421 &body, &body_used, body_len+1, 0);
423 tt_assert(header);
424 tt_assert(body);
426 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
427 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
428 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
429 tt_assert(strstr(header, "Pragma: no-cache\r\n"));
430 tor_snprintf(buff, sizeof(buff), "Content-Length: %ld\r\n", (long) body_len);
431 tt_assert(strstr(header, buff));
433 tt_int_op(body_used, OP_EQ, strlen(body));
434 tt_str_op(body, OP_EQ, desc_holder->desc_str);
436 done:
437 UNMOCK(connection_write_to_buf_impl_);
438 NS_UNMOCK(router_get_my_routerinfo);
440 connection_free_minimal(TO_CONN(conn));
441 tor_free(header);
442 tor_free(body);
443 rend_encoded_v2_service_descriptor_free(desc_holder);
444 tor_free(service_id);
445 rend_cache_free_all();
448 #define MICRODESC_GET(digest) GET("/tor/micro/d/" digest)
449 static void
450 test_dir_handle_get_micro_d_not_found(void *data)
452 dir_connection_t *conn = NULL;
453 char *header = NULL;
454 (void) data;
456 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
458 #define B64_256_1 "8/Pz8/u7vz8/Pz+7vz8/Pz+7u/Pz8/P7u/Pz8/P7u78"
459 #define B64_256_2 "zMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMzMw"
460 conn = new_dir_conn();
462 const char *req = MICRODESC_GET(B64_256_1 "-" B64_256_2);
463 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
465 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
466 NULL, NULL, 1, 0);
468 tt_str_op(NOT_FOUND, OP_EQ, header);
470 done:
471 UNMOCK(connection_write_to_buf_impl_);
473 connection_free_minimal(TO_CONN(conn));
474 tor_free(header);
477 static or_options_t *mock_options = NULL;
478 static void
479 init_mock_options(void)
481 mock_options = tor_malloc(sizeof(or_options_t));
482 memset(mock_options, 0, sizeof(or_options_t));
483 mock_options->TestingTorNetwork = 1;
484 mock_options->DataDirectory = tor_strdup(get_fname_rnd("datadir_tmp"));
485 mock_options->CacheDirectory = tor_strdup(mock_options->DataDirectory);
486 check_private_dir(mock_options->DataDirectory, CPD_CREATE, NULL);
489 static const or_options_t *
490 mock_get_options(void)
492 tor_assert(mock_options);
493 return mock_options;
496 static const char microdesc[] =
497 "onion-key\n"
498 "-----BEGIN RSA PUBLIC KEY-----\n"
499 "MIGJAoGBAMjlHH/daN43cSVRaHBwgUfnszzAhg98EvivJ9Qxfv51mvQUxPjQ07es\n"
500 "gV/3n8fyh3Kqr/ehi9jxkdgSRfSnmF7giaHL1SLZ29kA7KtST+pBvmTpDtHa3ykX\n"
501 "Xorc7hJvIyTZoc1HU+5XSynj3gsBE5IGK1ZRzrNS688LnuZMVp1tAgMBAAE=\n"
502 "-----END RSA PUBLIC KEY-----\n";
504 static void
505 test_dir_handle_get_micro_d(void *data)
507 dir_connection_t *conn = NULL;
508 microdesc_cache_t *mc = NULL ;
509 smartlist_t *list = NULL;
510 char digest[DIGEST256_LEN];
511 char digest_base64[128];
512 char path[80];
513 char *header = NULL;
514 char *body = NULL;
515 size_t body_used = 0;
516 (void) data;
518 MOCK(get_options, mock_get_options);
519 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
521 /* SETUP */
522 init_mock_options();
524 /* Add microdesc to cache */
525 crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
526 base64_encode_nopad(digest_base64, sizeof(digest_base64),
527 (uint8_t *) digest, DIGEST256_LEN);
529 mc = get_microdesc_cache();
530 list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
531 time(NULL), NULL);
532 tt_int_op(1, OP_EQ, smartlist_len(list));
534 /* Make the request */
535 conn = new_dir_conn();
537 tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
538 tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
540 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
541 &body, &body_used, strlen(microdesc)+1, 0);
543 tt_assert(header);
544 tt_assert(body);
546 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
547 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
548 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
550 tt_int_op(body_used, OP_EQ, strlen(body));
551 tt_str_op(body, OP_EQ, microdesc);
553 done:
554 UNMOCK(get_options);
555 UNMOCK(connection_write_to_buf_impl_);
557 or_options_free(mock_options); mock_options = NULL;
558 connection_free_minimal(TO_CONN(conn));
559 tor_free(header);
560 tor_free(body);
561 smartlist_free(list);
562 microdesc_free_all();
565 static void
566 test_dir_handle_get_micro_d_server_busy(void *data)
568 dir_connection_t *conn = NULL;
569 microdesc_cache_t *mc = NULL ;
570 smartlist_t *list = NULL;
571 char digest[DIGEST256_LEN];
572 char digest_base64[128];
573 char path[80];
574 char *header = NULL;
575 (void) data;
577 MOCK(get_options, mock_get_options);
578 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
580 /* SETUP */
581 init_mock_options();
583 /* Add microdesc to cache */
584 crypto_digest256(digest, microdesc, strlen(microdesc), DIGEST_SHA256);
585 base64_encode_nopad(digest_base64, sizeof(digest_base64),
586 (uint8_t *) digest, DIGEST256_LEN);
588 mc = get_microdesc_cache();
589 list = microdescs_add_to_cache(mc, microdesc, NULL, SAVED_NOWHERE, 0,
590 time(NULL), NULL);
591 tt_int_op(1, OP_EQ, smartlist_len(list));
593 //Make it busy
594 mock_options->CountPrivateBandwidth = 1;
596 /* Make the request */
597 conn = new_dir_conn();
599 tor_snprintf(path, sizeof(path), MICRODESC_GET("%s"), digest_base64);
600 tt_int_op(directory_handle_command_get(conn, path, NULL, 0), OP_EQ, 0);
602 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
603 NULL, NULL, 1, 0);
605 tt_str_op(SERVER_BUSY, OP_EQ, header);
607 done:
608 UNMOCK(get_options);
609 UNMOCK(connection_write_to_buf_impl_);
611 or_options_free(mock_options); mock_options = NULL;
612 connection_free_minimal(TO_CONN(conn));
613 tor_free(header);
614 smartlist_free(list);
615 microdesc_free_all();
618 #define BRIDGES_PATH "/tor/networkstatus-bridges"
619 static void
620 test_dir_handle_get_networkstatus_bridges_not_found_without_auth(void *data)
622 dir_connection_t *conn = NULL;
623 char *header = NULL;
624 (void) data;
626 MOCK(get_options, mock_get_options);
627 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
629 /* SETUP */
630 init_mock_options();
631 mock_options->BridgeAuthoritativeDir = 1;
632 mock_options->BridgePassword_AuthDigest_ = tor_strdup("digest");
634 conn = new_dir_conn();
635 TO_CONN(conn)->linked = 1;
637 const char *req = GET(BRIDGES_PATH);
638 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
640 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
641 NULL, NULL, 1, 0);
643 tt_str_op(NOT_FOUND, OP_EQ, header);
645 done:
646 UNMOCK(get_options);
647 UNMOCK(connection_write_to_buf_impl_);
648 or_options_free(mock_options); mock_options = NULL;
649 connection_free_minimal(TO_CONN(conn));
650 tor_free(header);
653 static void
654 test_dir_handle_get_networkstatus_bridges(void *data)
656 dir_connection_t *conn = NULL;
657 char *header = NULL;
658 (void) data;
660 MOCK(get_options, mock_get_options);
661 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
663 /* SETUP */
664 init_mock_options();
665 mock_options->BridgeAuthoritativeDir = 1;
666 mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
667 crypto_digest256(mock_options->BridgePassword_AuthDigest_,
668 "abcdefghijklm12345", 18, DIGEST_SHA256);
670 conn = new_dir_conn();
671 TO_CONN(conn)->linked = 1;
673 const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
674 "Authorization: Basic abcdefghijklm12345\r\n\r\n";
675 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
677 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
678 NULL, NULL, 1, 0);
680 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
681 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
682 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
683 tt_assert(strstr(header, "Content-Length: 0\r\n"));
685 done:
686 UNMOCK(get_options);
687 UNMOCK(connection_write_to_buf_impl_);
688 or_options_free(mock_options); mock_options = NULL;
689 connection_free_minimal(TO_CONN(conn));
690 tor_free(header);
693 static void
694 test_dir_handle_get_networkstatus_bridges_not_found_wrong_auth(void *data)
696 dir_connection_t *conn = NULL;
697 char *header = NULL;
698 (void) data;
700 MOCK(get_options, mock_get_options);
701 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
703 /* SETUP */
704 init_mock_options();
705 mock_options->BridgeAuthoritativeDir = 1;
706 mock_options->BridgePassword_AuthDigest_ = tor_malloc(DIGEST256_LEN);
707 crypto_digest256(mock_options->BridgePassword_AuthDigest_,
708 "abcdefghijklm12345", 18, DIGEST_SHA256);
710 conn = new_dir_conn();
711 TO_CONN(conn)->linked = 1;
713 const char *req = "GET " BRIDGES_PATH " HTTP/1.0\r\n"
714 "Authorization: Basic NOTSAMEDIGEST\r\n\r\n";
715 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
717 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
718 NULL, NULL, 1, 0);
720 tt_str_op(NOT_FOUND, OP_EQ, header);
722 done:
723 UNMOCK(get_options);
724 UNMOCK(connection_write_to_buf_impl_);
725 or_options_free(mock_options); mock_options = NULL;
726 connection_free_minimal(TO_CONN(conn));
727 tor_free(header);
730 #define SERVER_DESC_GET(id) GET("/tor/server/" id)
731 static void
732 test_dir_handle_get_server_descriptors_not_found(void* data)
734 dir_connection_t *conn = NULL;
735 char *header = NULL;
736 (void) data;
738 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
740 conn = new_dir_conn();
742 const char *req = SERVER_DESC_GET("invalid");
743 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
745 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
746 NULL, NULL, 1, 0);
748 tt_str_op(NOT_FOUND, OP_EQ, header);
749 tt_ptr_op(conn->spool, OP_EQ, NULL);
751 done:
752 UNMOCK(connection_write_to_buf_impl_);
753 or_options_free(mock_options); mock_options = NULL;
754 connection_free_minimal(TO_CONN(conn));
755 tor_free(header);
758 static void
759 test_dir_handle_get_server_descriptors_all(void* data)
761 dir_connection_t *conn = NULL;
762 char *header = NULL;
763 char *body = NULL;
764 size_t body_used = 0;
765 (void) data;
767 /* Setup fake routerlist. */
768 helper_setup_fake_routerlist();
770 //TODO: change to router_get_my_extrainfo when testing "extra" path
771 NS_MOCK(router_get_my_routerinfo);
772 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
774 // We are one of the routers
775 routerlist_t *our_routerlist = router_get_routerlist();
776 tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
777 mock_routerinfo = smartlist_get(our_routerlist->routers, 0);
778 set_server_identity_key(mock_routerinfo->identity_pkey);
779 mock_routerinfo->cache_info.published_on = time(NULL);
781 /* Treat "all" requests as if they were unencrypted */
782 mock_routerinfo->cache_info.send_unencrypted = 1;
784 conn = new_dir_conn();
786 const char *req = SERVER_DESC_GET("all");
787 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
789 //TODO: Is this a BUG?
790 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
791 //which is smaller than that by annotation_len bytes
792 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
793 &body, &body_used,
794 1024*1024, 0);
796 tt_assert(header);
797 tt_assert(body);
799 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
800 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
801 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
803 //TODO: Is this a BUG?
804 //This is what should be expected: tt_int_op(body_used, OP_EQ, strlen(body));
805 tt_int_op(body_used, OP_EQ,
806 mock_routerinfo->cache_info.signed_descriptor_len);
808 tt_str_op(body, OP_EQ, mock_routerinfo->cache_info.signed_descriptor_body +
809 mock_routerinfo->cache_info.annotations_len);
810 tt_ptr_op(conn->spool, OP_EQ, NULL);
812 done:
813 NS_UNMOCK(router_get_my_routerinfo);
814 UNMOCK(connection_write_to_buf_impl_);
815 connection_free_minimal(TO_CONN(conn));
816 tor_free(header);
817 tor_free(body);
819 routerlist_free_all();
820 nodelist_free_all();
821 entry_guards_free_all();
824 static char
825 TEST_DESCRIPTOR[] =
826 "@uploaded-at 2014-06-08 19:20:11\n"
827 "@source \"127.0.0.1\"\n"
828 "router test000a 127.0.0.1 5000 0 7000\n"
829 "platform Tor 0.2.5.3-alpha-dev on Linux\n"
830 "protocols Link 1 2 Circuit 1\n"
831 "published 2014-06-08 19:20:11\n"
832 "fingerprint C7E7 CCB8 179F 8CC3 7F5C 8A04 2B3A 180B 934B 14BA\n"
833 "uptime 0\n"
834 "bandwidth 1073741824 1073741824 0\n"
835 "extra-info-digest 67A152A4C7686FB07664F872620635F194D76D95\n"
836 "caches-extra-info\n"
837 "onion-key\n"
838 "-----BEGIN RSA PUBLIC KEY-----\n"
839 "MIGJAoGBAOuBUIEBARMkkka/TGyaQNgUEDLP0KG7sy6KNQTNOlZHUresPr/vlVjo\n"
840 "HPpLMfu9M2z18c51YX/muWwY9x4MyQooD56wI4+AqXQcJRwQfQlPn3Ay82uZViA9\n"
841 "DpBajRieLlKKkl145KjArpD7F5BVsqccvjErgFYXvhhjSrx7BVLnAgMBAAE=\n"
842 "-----END RSA PUBLIC KEY-----\n"
843 "signing-key\n"
844 "-----BEGIN RSA PUBLIC KEY-----\n"
845 "MIGJAoGBAN6NLnSxWQnFXxqZi5D3b0BMgV6y9NJLGjYQVP+eWtPZWgqyv4zeYsqv\n"
846 "O9y6c5lvxyUxmNHfoAbe/s8f2Vf3/YaC17asAVSln4ktrr3e9iY74a9RMWHv1Gzk\n"
847 "3042nMcqj3PEhRN0PoLkcOZNjjmNbaqki6qy9bWWZDNTdo+uI44dAgMBAAE=\n"
848 "-----END RSA PUBLIC KEY-----\n"
849 "hidden-service-dir\n"
850 "contact auth0@test.test\n"
851 "ntor-onion-key pK4bs08ERYN591jj7ca17Rn9Q02TIEfhnjR6hSq+fhU=\n"
852 "reject *:*\n"
853 "router-signature\n"
854 "-----BEGIN SIGNATURE-----\n"
855 "rx88DuM3Y7tODlHNDDEVzKpwh3csaG1or+T4l2Xs1oq3iHHyPEtB6QTLYrC60trG\n"
856 "aAPsj3DEowGfjga1b248g2dtic8Ab+0exfjMm1RHXfDam5TXXZU3A0wMyoHjqHuf\n"
857 "eChGPgFNUvEc+5YtD27qEDcUjcinYztTs7/dzxBT4PE=\n"
858 "-----END SIGNATURE-----\n";
860 static void
861 test_dir_handle_get_server_descriptors_authority(void* data)
863 dir_connection_t *conn = NULL;
864 char *header = NULL;
865 char *body = NULL;
866 size_t body_used = 0;
867 crypto_pk_t *identity_pkey = pk_generate(0);
868 (void) data;
870 NS_MOCK(router_get_my_routerinfo);
871 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
873 /* init mock */
874 router_get_my_routerinfo();
875 crypto_pk_get_digest(identity_pkey,
876 mock_routerinfo->cache_info.identity_digest);
878 // the digest is mine (the channel is unnecrypted, so we must allow sending)
879 set_server_identity_key(identity_pkey);
880 mock_routerinfo->cache_info.send_unencrypted = 1;
882 /* Setup descriptor */
883 long annotation_len = strstr(TEST_DESCRIPTOR, "router ") - TEST_DESCRIPTOR;
884 mock_routerinfo->cache_info.signed_descriptor_body =
885 tor_strdup(TEST_DESCRIPTOR);
886 mock_routerinfo->cache_info.signed_descriptor_len =
887 strlen(TEST_DESCRIPTOR) - annotation_len;
888 mock_routerinfo->cache_info.annotations_len = annotation_len;
889 mock_routerinfo->cache_info.published_on = time(NULL);
891 conn = new_dir_conn();
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);
902 tt_assert(header);
903 tt_assert(body);
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_ptr_op(conn->spool, OP_EQ, NULL);
914 done:
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_minimal(TO_CONN(conn));
920 tor_free(header);
921 tor_free(body);
922 crypto_pk_free(identity_pkey);
925 static void
926 test_dir_handle_get_server_descriptors_fp(void* data)
928 dir_connection_t *conn = NULL;
929 char *header = NULL;
930 char *body = NULL;
931 size_t body_used = 0;
932 crypto_pk_t *identity_pkey = pk_generate(0);
933 (void) data;
935 NS_MOCK(router_get_my_routerinfo);
936 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
938 /* init 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;
954 mock_routerinfo->cache_info.published_on = time(NULL);
956 conn = new_dir_conn();
958 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
959 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
960 const char *hex_digest = hex_str(mock_routerinfo->cache_info.identity_digest,
961 DIGEST_LEN);
963 char req[155];
964 tor_snprintf(req, sizeof(req), SERVER_DESC_GET("fp/%s+" HEX1 "+" HEX2),
965 hex_digest);
966 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
968 //TODO: Is this a BUG?
969 //It requires strlen(TEST_DESCRIPTOR)+1 as body_len but returns a body which
970 //is smaller than that by annotation_len bytes
971 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
972 &body, &body_used, strlen(TEST_DESCRIPTOR)+1, 0);
974 tt_assert(header);
975 tt_assert(body);
977 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
978 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
979 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
981 tt_int_op(body_used, OP_EQ, strlen(body));
983 tt_str_op(body, OP_EQ, TEST_DESCRIPTOR + annotation_len);
984 tt_ptr_op(conn->spool, OP_EQ, NULL);
986 done:
987 NS_UNMOCK(router_get_my_routerinfo);
988 UNMOCK(connection_write_to_buf_impl_);
989 tor_free(mock_routerinfo->cache_info.signed_descriptor_body);
990 tor_free(mock_routerinfo);
991 connection_free_minimal(TO_CONN(conn));
992 tor_free(header);
993 tor_free(body);
994 crypto_pk_free(identity_pkey);
997 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
998 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1000 static void
1001 test_dir_handle_get_server_descriptors_d(void* data)
1003 dir_connection_t *conn = NULL;
1004 char *header = NULL;
1005 char *body = NULL;
1006 size_t body_used = 0;
1007 crypto_pk_t *identity_pkey = pk_generate(0);
1008 (void) data;
1010 /* Setup fake routerlist. */
1011 helper_setup_fake_routerlist();
1013 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1015 /* Get one router's signed_descriptor_digest */
1016 routerlist_t *our_routerlist = router_get_routerlist();
1017 tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
1018 routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
1019 const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
1020 DIGEST_LEN);
1022 conn = new_dir_conn();
1024 char req_header[155]; /* XXX Why 155? What kind of number is that?? */
1025 tor_snprintf(req_header, sizeof(req_header),
1026 SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
1027 tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
1029 //TODO: Is this a BUG?
1030 //It requires strlen(signed_descriptor_len)+1 as body_len but returns a body
1031 //which is smaller than that by annotation_len bytes
1032 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1033 &body, &body_used,
1034 router->cache_info.signed_descriptor_len+1, 0);
1036 tt_assert(header);
1037 tt_assert(body);
1039 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1040 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1041 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1043 //TODO: Is this a BUG?
1044 //This is what should be expected:
1045 //tt_int_op(body_used, OP_EQ, strlen(body));
1046 tt_int_op(body_used, OP_EQ, router->cache_info.signed_descriptor_len);
1048 tt_str_op(body, OP_EQ, router->cache_info.signed_descriptor_body +
1049 router->cache_info.annotations_len);
1050 tt_ptr_op(conn->spool, OP_EQ, NULL);
1052 done:
1053 UNMOCK(connection_write_to_buf_impl_);
1054 tor_free(mock_routerinfo);
1055 connection_free_minimal(TO_CONN(conn));
1056 tor_free(header);
1057 tor_free(body);
1058 crypto_pk_free(identity_pkey);
1060 routerlist_free_all();
1061 nodelist_free_all();
1062 entry_guards_free_all();
1065 static void
1066 test_dir_handle_get_server_descriptors_busy(void* data)
1068 dir_connection_t *conn = NULL;
1069 char *header = NULL;
1070 crypto_pk_t *identity_pkey = pk_generate(0);
1071 (void) data;
1073 /* Setup fake routerlist. */
1074 helper_setup_fake_routerlist();
1076 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1078 //Make it busy
1079 MOCK(get_options, mock_get_options);
1080 init_mock_options();
1081 mock_options->CountPrivateBandwidth = 1;
1083 /* Get one router's signed_descriptor_digest */
1084 routerlist_t *our_routerlist = router_get_routerlist();
1085 tt_int_op(smartlist_len(our_routerlist->routers), OP_GE, 1);
1086 routerinfo_t *router = smartlist_get(our_routerlist->routers, 0);
1087 const char *hex_digest = hex_str(router->cache_info.signed_descriptor_digest,
1088 DIGEST_LEN);
1090 conn = new_dir_conn();
1092 #define HEX1 "Fe0daff89127389bc67558691231234551193EEE"
1093 #define HEX2 "Deadbeef99999991111119999911111111f00ba4"
1094 char req_header[155]; /* XXX 155? Why 155? */
1095 tor_snprintf(req_header, sizeof(req_header),
1096 SERVER_DESC_GET("d/%s+" HEX1 "+" HEX2), hex_digest);
1097 tt_int_op(directory_handle_command_get(conn, req_header, NULL, 0), OP_EQ, 0);
1099 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1100 NULL, NULL, 1, 0);
1102 tt_assert(header);
1103 tt_str_op(SERVER_BUSY, OP_EQ, header);
1105 tt_ptr_op(conn->spool, OP_EQ, NULL);
1107 done:
1108 UNMOCK(get_options);
1109 UNMOCK(connection_write_to_buf_impl_);
1110 tor_free(mock_routerinfo);
1111 connection_free_minimal(TO_CONN(conn));
1112 tor_free(header);
1113 crypto_pk_free(identity_pkey);
1115 routerlist_free_all();
1116 nodelist_free_all();
1117 entry_guards_free_all();
1120 static void
1121 test_dir_handle_get_server_keys_bad_req(void* data)
1123 dir_connection_t *conn = NULL;
1124 char *header = NULL;
1125 (void) data;
1127 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1129 conn = new_dir_conn();
1131 const char *req = GET("/tor/keys/");
1132 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1134 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1135 NULL, NULL, 1, 0);
1137 tt_assert(header);
1138 tt_str_op(BAD_REQUEST, OP_EQ, header);
1140 done:
1141 UNMOCK(connection_write_to_buf_impl_);
1142 connection_free_minimal(TO_CONN(conn));
1143 tor_free(header);
1146 static void
1147 test_dir_handle_get_server_keys_all_not_found(void* data)
1149 dir_connection_t *conn = NULL;
1150 char *header = NULL;
1151 (void) data;
1153 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1155 conn = new_dir_conn();
1157 const char *req = GET("/tor/keys/all");
1158 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1160 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1161 NULL, NULL, 1, 0);
1163 tt_assert(header);
1164 tt_str_op(NOT_FOUND, OP_EQ, header);
1166 done:
1167 UNMOCK(connection_write_to_buf_impl_);
1168 connection_free_minimal(TO_CONN(conn));
1169 tor_free(header);
1172 #define TEST_CERTIFICATE AUTHORITY_CERT_3
1173 #define TEST_SIGNING_KEY AUTHORITY_SIGNKEY_A_DIGEST
1175 static const char TEST_CERT_IDENT_KEY[] =
1176 "D867ACF56A9D229B35C25F0090BC9867E906BE69";
1178 static void
1179 test_dir_handle_get_server_keys_all(void* data)
1181 dir_connection_t *conn = NULL;
1182 char *header = NULL;
1183 char *body = NULL;
1184 size_t body_used = 0;
1185 const char digest[DIGEST_LEN] = "";
1187 dir_server_t *ds = NULL;
1188 (void) data;
1190 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1192 clear_dir_servers();
1193 routerlist_free_all();
1195 /* create a trusted ds */
1196 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1197 NULL, V3_DIRINFO, 1.0);
1198 tt_assert(ds);
1199 dir_server_add(ds);
1201 /* ds v3_identity_digest is the certificate's identity_key */
1202 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
1203 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
1204 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
1205 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
1207 conn = new_dir_conn();
1209 const char *req = GET("/tor/keys/all");
1210 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1212 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1213 &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
1215 tt_assert(header);
1216 tt_assert(body);
1218 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1219 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1220 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1221 tt_assert(strstr(header, "Content-Length: 1883\r\n"));
1223 tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
1225 done:
1226 UNMOCK(connection_write_to_buf_impl_);
1227 connection_free_minimal(TO_CONN(conn));
1228 tor_free(header);
1229 tor_free(body);
1231 clear_dir_servers();
1232 routerlist_free_all();
1235 static void
1236 test_dir_handle_get_server_keys_authority_not_found(void* data)
1238 dir_connection_t *conn = NULL;
1239 char *header = NULL;
1240 (void) data;
1242 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1244 conn = new_dir_conn();
1246 const char *req = GET("/tor/keys/authority");
1247 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1249 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1250 NULL, NULL, 1, 0);
1252 tt_assert(header);
1253 tt_str_op(NOT_FOUND, OP_EQ, header);
1255 done:
1256 UNMOCK(connection_write_to_buf_impl_);
1257 connection_free_minimal(TO_CONN(conn));
1258 tor_free(header);
1261 static authority_cert_t * mock_cert = NULL;
1263 static authority_cert_t *
1264 get_my_v3_authority_cert_m(void)
1266 tor_assert(mock_cert);
1267 return mock_cert;
1270 static void
1271 test_dir_handle_get_server_keys_authority(void* data)
1273 dir_connection_t *conn = NULL;
1274 char *header = NULL;
1275 char *body = NULL;
1276 size_t body_used = 0;
1277 (void) data;
1279 mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
1281 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
1282 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1284 conn = new_dir_conn();
1286 const char *req = GET("/tor/keys/authority");
1287 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1289 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1290 &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
1292 tt_assert(header);
1293 tt_assert(body);
1295 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1296 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1297 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1298 tt_assert(strstr(header, "Content-Length: 1883\r\n"));
1300 tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
1302 done:
1303 UNMOCK(get_my_v3_authority_cert);
1304 UNMOCK(connection_write_to_buf_impl_);
1305 connection_free_minimal(TO_CONN(conn));
1306 tor_free(header);
1307 tor_free(body);
1308 authority_cert_free(mock_cert); mock_cert = NULL;
1311 static void
1312 test_dir_handle_get_server_keys_fp_not_found(void* data)
1314 dir_connection_t *conn = NULL;
1315 char *header = NULL;
1316 (void) data;
1318 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1320 conn = new_dir_conn();
1322 const char *req = GET("/tor/keys/fp/somehex");
1323 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1325 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1326 NULL, NULL, 1, 0);
1328 tt_assert(header);
1329 tt_str_op(NOT_FOUND, OP_EQ, header);
1331 done:
1332 UNMOCK(connection_write_to_buf_impl_);
1333 connection_free_minimal(TO_CONN(conn));
1334 tor_free(header);
1337 static void
1338 test_dir_handle_get_server_keys_fp(void* data)
1340 dir_connection_t *conn = NULL;
1341 char *header = NULL;
1342 char *body = NULL;
1343 size_t body_used = 0;
1344 dir_server_t *ds = NULL;
1345 const char digest[DIGEST_LEN] = "";
1346 (void) data;
1348 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1350 clear_dir_servers();
1351 routerlist_free_all();
1353 /* create a trusted ds */
1354 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1355 NULL, V3_DIRINFO, 1.0);
1356 tt_assert(ds);
1357 dir_server_add(ds);
1359 /* ds v3_identity_digest is the certificate's identity_key */
1360 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
1361 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
1363 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
1364 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
1366 conn = new_dir_conn();
1367 char req[71];
1368 tor_snprintf(req, sizeof(req),
1369 GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
1370 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1372 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1373 &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
1375 tt_assert(header);
1376 tt_assert(body);
1378 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1379 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1380 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1381 tt_assert(strstr(header, "Content-Length: 1883\r\n"));
1383 tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
1385 done:
1386 UNMOCK(connection_write_to_buf_impl_);
1387 connection_free_minimal(TO_CONN(conn));
1388 tor_free(header);
1389 tor_free(body);
1390 clear_dir_servers();
1391 routerlist_free_all();
1394 static void
1395 test_dir_handle_get_server_keys_sk_not_found(void* data)
1397 dir_connection_t *conn = NULL;
1398 char *header = NULL;
1399 (void) data;
1401 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1403 conn = new_dir_conn();
1405 const char *req = GET("/tor/keys/sk/somehex");
1406 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1408 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1409 NULL, NULL, 1, 0);
1411 tt_assert(header);
1412 tt_str_op(NOT_FOUND, OP_EQ, header);
1414 done:
1415 UNMOCK(connection_write_to_buf_impl_);
1416 connection_free_minimal(TO_CONN(conn));
1417 tor_free(header);
1420 static void
1421 test_dir_handle_get_server_keys_sk(void* data)
1423 dir_connection_t *conn = NULL;
1424 char *header = NULL;
1425 char *body = NULL;
1426 size_t body_used = 0;
1427 (void) data;
1429 mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
1430 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
1431 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1433 clear_dir_servers();
1434 routerlist_free_all();
1436 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
1437 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
1439 conn = new_dir_conn();
1440 char req[71];
1441 tor_snprintf(req, sizeof(req),
1442 GET("/tor/keys/sk/%s"), TEST_SIGNING_KEY);
1443 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1445 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1446 &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
1448 tt_assert(header);
1449 tt_assert(body);
1451 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1452 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1453 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1454 tt_assert(strstr(header, "Content-Length: 1883\r\n"));
1456 tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
1458 done:
1459 UNMOCK(get_my_v3_authority_cert);
1460 UNMOCK(connection_write_to_buf_impl_);
1461 connection_free_minimal(TO_CONN(conn));
1462 authority_cert_free(mock_cert); mock_cert = NULL;
1463 tor_free(header);
1464 tor_free(body);
1467 static void
1468 test_dir_handle_get_server_keys_fpsk_not_found(void* data)
1470 dir_connection_t *conn = NULL;
1471 char *header = NULL;
1472 (void) data;
1474 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1476 conn = new_dir_conn();
1478 const char *req = GET("/tor/keys/fp-sk/somehex");
1479 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1481 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1482 NULL, NULL, 1, 0);
1484 tt_assert(header);
1485 tt_str_op(NOT_FOUND, OP_EQ, header);
1487 done:
1488 UNMOCK(connection_write_to_buf_impl_);
1489 connection_free_minimal(TO_CONN(conn));
1490 tor_free(header);
1493 static void
1494 test_dir_handle_get_server_keys_fpsk(void* data)
1496 dir_connection_t *conn = NULL;
1497 char *header = NULL;
1498 char *body = NULL;
1499 size_t body_used = 0;
1500 dir_server_t *ds = NULL;
1501 const char digest[DIGEST_LEN] = "";
1502 (void) data;
1504 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1506 clear_dir_servers();
1507 routerlist_free_all();
1509 /* create a trusted ds */
1510 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1511 NULL, V3_DIRINFO, 1.0);
1512 tt_assert(ds);
1514 /* ds v3_identity_digest is the certificate's identity_key */
1515 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
1516 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
1517 dir_server_add(ds);
1519 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
1520 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
1522 conn = new_dir_conn();
1524 char req[115];
1525 tor_snprintf(req, sizeof(req),
1526 GET("/tor/keys/fp-sk/%s-%s"),
1527 TEST_CERT_IDENT_KEY, TEST_SIGNING_KEY);
1529 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1531 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1532 &body, &body_used, strlen(TEST_CERTIFICATE)+1, 0);
1534 tt_assert(header);
1535 tt_assert(body);
1537 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1538 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1539 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1540 tt_assert(strstr(header, "Content-Length: 1883\r\n"));
1542 tt_str_op(TEST_CERTIFICATE, OP_EQ, body);
1544 done:
1545 UNMOCK(connection_write_to_buf_impl_);
1546 connection_free_minimal(TO_CONN(conn));
1547 tor_free(header);
1548 tor_free(body);
1550 clear_dir_servers();
1551 routerlist_free_all();
1554 static void
1555 test_dir_handle_get_server_keys_busy(void* data)
1557 dir_connection_t *conn = NULL;
1558 char *header = NULL;
1559 dir_server_t *ds = NULL;
1560 const char digest[DIGEST_LEN] = "";
1561 (void) data;
1563 clear_dir_servers();
1564 routerlist_free_all();
1566 /* create a trusted ds */
1567 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
1568 NULL, V3_DIRINFO, 1.0);
1569 tt_assert(ds);
1571 /* ds v3_identity_digest is the certificate's identity_key */
1572 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
1573 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
1574 dir_server_add(ds);
1576 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
1577 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
1579 MOCK(get_options, mock_get_options);
1580 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1582 /* setup busy server */
1583 init_mock_options();
1584 mock_options->CountPrivateBandwidth = 1;
1586 conn = new_dir_conn();
1587 char req[71];
1588 tor_snprintf(req, sizeof(req), GET("/tor/keys/fp/%s"), TEST_CERT_IDENT_KEY);
1589 tt_int_op(directory_handle_command_get(conn, req, NULL, 0), OP_EQ, 0);
1591 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1592 NULL, NULL, 1, 0);
1594 tt_assert(header);
1595 tt_str_op(SERVER_BUSY, OP_EQ, header);
1597 done:
1598 UNMOCK(get_options);
1599 UNMOCK(connection_write_to_buf_impl_);
1600 connection_free_minimal(TO_CONN(conn));
1601 tor_free(header);
1602 or_options_free(mock_options); mock_options = NULL;
1604 clear_dir_servers();
1605 routerlist_free_all();
1608 static networkstatus_t *mock_ns_val = NULL;
1609 static networkstatus_t *
1610 mock_ns_get_by_flavor(consensus_flavor_t f)
1612 (void)f;
1613 return mock_ns_val;
1616 static void
1617 test_dir_handle_get_status_vote_current_consensus_ns_not_enough_sigs(void* d)
1619 dir_connection_t *conn = NULL;
1620 char *header = NULL;
1621 char *stats = NULL;
1622 (void) d;
1624 /* init mock */
1625 mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
1626 mock_ns_val->flavor = FLAV_NS;
1627 mock_ns_val->type = NS_TYPE_CONSENSUS;
1628 mock_ns_val->voters = smartlist_new();
1629 mock_ns_val->valid_after = time(NULL) - 1800;
1630 mock_ns_val->valid_until = time(NULL) - 60;
1632 #define NETWORK_STATUS "some network status string"
1633 consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
1635 /* init mock */
1636 init_mock_options();
1638 MOCK(get_options, mock_get_options);
1639 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1640 MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
1642 /* start gathering stats */
1643 mock_options->DirReqStatistics = 1;
1644 geoip_dirreq_stats_init(time(NULL));
1646 conn = new_dir_conn();
1648 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1649 GET("/tor/status-vote/current/consensus-ns/" HEX1 "+" HEX2), NULL, 0));
1651 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1652 NULL, NULL, 1, 0);
1654 tt_assert(header);
1655 tt_str_op(NOT_ENOUGH_CONSENSUS_SIGNATURES, OP_EQ, header);
1657 stats = geoip_format_dirreq_stats(time(NULL));
1658 tt_assert(stats);
1659 tt_assert(strstr(stats, "not-enough-sigs=8"));
1661 done:
1662 UNMOCK(networkstatus_get_latest_consensus_by_flavor);
1663 UNMOCK(connection_write_to_buf_impl_);
1664 UNMOCK(get_options);
1666 connection_free_minimal(TO_CONN(conn));
1667 tor_free(header);
1668 tor_free(stats);
1669 smartlist_free(mock_ns_val->voters);
1670 tor_free(mock_ns_val);
1671 or_options_free(mock_options); mock_options = NULL;
1674 static void
1675 test_dir_handle_get_status_vote_current_consensus_ns_not_found(void* data)
1677 dir_connection_t *conn = NULL;
1678 char *header = NULL;
1679 char *stats = NULL;
1680 (void) data;
1682 init_mock_options();
1684 MOCK(get_options, mock_get_options);
1685 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1687 /* start gathering stats */
1688 mock_options->DirReqStatistics = 1;
1689 geoip_dirreq_stats_init(time(NULL));
1691 conn = new_dir_conn();
1692 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1693 GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
1695 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1696 NULL, NULL, 1, 0);
1697 tt_assert(header);
1698 tt_str_op(NOT_FOUND, OP_EQ, header);
1700 stats = geoip_format_dirreq_stats(time(NULL));
1701 tt_assert(stats);
1702 tt_assert(strstr(stats, "not-found=8"));
1704 done:
1705 UNMOCK(connection_write_to_buf_impl_);
1706 UNMOCK(get_options);
1707 connection_free_minimal(TO_CONN(conn));
1708 tor_free(header);
1709 tor_free(stats);
1710 or_options_free(mock_options); mock_options = NULL;
1713 static void
1714 test_dir_handle_get_status_vote_current_consensus_too_old(void *data)
1716 dir_connection_t *conn = NULL;
1717 char *header = NULL;
1718 (void)data;
1720 mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
1721 mock_ns_val->type = NS_TYPE_CONSENSUS;
1722 mock_ns_val->flavor = FLAV_MICRODESC;
1723 mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
1724 mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
1725 mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
1727 #define NETWORK_STATUS "some network status string"
1728 consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
1730 init_mock_options();
1732 MOCK(get_options, mock_get_options);
1733 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1734 MOCK(networkstatus_get_latest_consensus_by_flavor, mock_ns_get_by_flavor);
1736 conn = new_dir_conn();
1738 setup_capture_of_logs(LOG_WARN);
1740 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1741 GET("/tor/status-vote/current/consensus-microdesc"), NULL, 0));
1743 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1744 NULL, NULL, 1, 0);
1745 tt_assert(header);
1746 tt_str_op(TOO_OLD, OP_EQ, header);
1748 expect_log_msg_containing("too old");
1750 tor_free(header);
1751 teardown_capture_of_logs();
1752 tor_free(mock_ns_val);
1754 mock_ns_val = tor_malloc_zero(sizeof(networkstatus_t));
1755 mock_ns_val->type = NS_TYPE_CONSENSUS;
1756 mock_ns_val->flavor = FLAV_NS;
1757 mock_ns_val->valid_after = time(NULL) - (24 * 60 * 60 + 1800);
1758 mock_ns_val->fresh_until = time(NULL) - (24 * 60 * 60 + 900);
1759 mock_ns_val->valid_until = time(NULL) - (24 * 60 * 60 + 20);
1761 #define NETWORK_STATUS "some network status string"
1762 consdiffmgr_add_consensus(NETWORK_STATUS, mock_ns_val);
1764 setup_capture_of_logs(LOG_WARN);
1766 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1767 GET("/tor/status-vote/current/consensus"), NULL, 0));
1769 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1770 NULL, NULL, 1, 0);
1771 tt_assert(header);
1772 tt_str_op(TOO_OLD, OP_EQ, header);
1774 expect_no_log_entry();
1776 done:
1777 teardown_capture_of_logs();
1778 UNMOCK(networkstatus_get_latest_consensus_by_flavor);
1779 UNMOCK(connection_write_to_buf_impl_);
1780 UNMOCK(get_options);
1781 connection_free_minimal(TO_CONN(conn));
1782 tor_free(header);
1783 tor_free(mock_ns_val);
1784 or_options_free(mock_options); mock_options = NULL;
1787 NS_DECL(int, geoip_get_country_by_addr, (const tor_addr_t *addr));
1790 NS(geoip_get_country_by_addr)(const tor_addr_t *addr)
1792 (void)addr;
1793 CALLED(geoip_get_country_by_addr)++;
1794 return 1;
1797 static void
1798 status_vote_current_consensus_ns_test(char **header, char **body,
1799 size_t *body_len)
1801 dir_connection_t *conn = NULL;
1803 #define NETWORK_STATUS "some network status string"
1804 #if 0
1805 common_digests_t digests;
1806 uint8_t sha3[DIGEST256_LEN];
1807 memset(&digests, 0x60, sizeof(digests));
1808 memset(sha3, 0x06, sizeof(sha3));
1809 dirserv_set_cached_consensus_networkstatus(NETWORK_STATUS, "ns", &digests,
1810 sha3,
1811 time(NULL));
1812 #endif /* 0 */
1813 networkstatus_t *ns = tor_malloc_zero(sizeof(networkstatus_t));
1814 ns->type = NS_TYPE_CONSENSUS;
1815 ns->flavor = FLAV_NS;
1816 ns->valid_after = time(NULL) - 1800;
1817 ns->fresh_until = time(NULL) - 900;
1818 ns->valid_until = time(NULL) - 60;
1819 consdiffmgr_add_consensus(NETWORK_STATUS, ns);
1820 networkstatus_vote_free(ns);
1822 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1824 tt_assert(mock_options);
1825 mock_options->DirReqStatistics = 1;
1826 geoip_dirreq_stats_init(time(NULL));
1828 /* init geoip database */
1829 geoip_parse_entry("10,50,AB", AF_INET);
1830 tt_str_op("ab", OP_EQ, geoip_get_country_name(1));
1832 conn = new_dir_conn();
1834 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1835 GET("/tor/status-vote/current/consensus-ns"), NULL, 0));
1837 fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
1838 body, body_len, strlen(NETWORK_STATUS)+7, 0);
1840 done:
1841 UNMOCK(connection_write_to_buf_impl_);
1842 connection_free_minimal(TO_CONN(conn));
1845 static void
1846 test_dir_handle_get_status_vote_current_consensus_ns(void* data)
1848 char *header = NULL;
1849 char *body = NULL, *comp_body = NULL;
1850 size_t body_used = 0, comp_body_used = 0;
1851 char *stats = NULL, *hist = NULL;
1852 (void) data;
1854 dirserv_free_all();
1855 clear_geoip_db();
1857 NS_MOCK(geoip_get_country_by_addr);
1858 MOCK(get_options, mock_get_options);
1860 init_mock_options();
1862 status_vote_current_consensus_ns_test(&header, &comp_body, &comp_body_used);
1863 tt_assert(header);
1865 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
1866 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
1867 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
1868 tt_assert(strstr(header, "Pragma: no-cache\r\n"));
1870 compress_method_t compression = detect_compression_method(comp_body,
1871 comp_body_used);
1872 tt_int_op(ZLIB_METHOD, OP_EQ, compression);
1874 tor_uncompress(&body, &body_used, comp_body, comp_body_used,
1875 compression, 0, LOG_PROTOCOL_WARN);
1877 tt_str_op(NETWORK_STATUS, OP_EQ, body);
1878 tt_int_op(strlen(NETWORK_STATUS), OP_EQ, body_used);
1880 stats = geoip_format_dirreq_stats(time(NULL));
1881 tt_assert(stats);
1883 tt_assert(strstr(stats, "ok=8"));
1884 tt_assert(strstr(stats, "dirreq-v3-ips ab=8"));
1885 tt_assert(strstr(stats, "dirreq-v3-reqs ab=8"));
1886 tt_assert(strstr(stats, "dirreq-v3-direct-dl"
1887 " complete=0,timeout=0,running=4"));
1889 hist = geoip_get_request_history();
1890 tt_assert(hist);
1891 tt_str_op("ab=8", OP_EQ, hist);
1893 done:
1894 NS_UNMOCK(geoip_get_country_by_addr);
1895 UNMOCK(get_options);
1896 tor_free(header);
1897 tor_free(comp_body);
1898 tor_free(body);
1899 tor_free(stats);
1900 tor_free(hist);
1901 or_options_free(mock_options); mock_options = NULL;
1903 dirserv_free_all();
1904 clear_geoip_db();
1907 static void
1908 test_dir_handle_get_status_vote_current_consensus_ns_busy(void* data)
1910 char *header = NULL;
1911 char *body = NULL;
1912 size_t body_used = 0;
1913 char *stats = NULL;
1914 (void) data;
1916 dirserv_free_all();
1917 clear_geoip_db();
1919 MOCK(get_options, mock_get_options);
1921 // Make it busy
1922 init_mock_options();
1923 mock_options->CountPrivateBandwidth = 1;
1925 status_vote_current_consensus_ns_test(&header, &body, &body_used);
1926 tt_assert(header);
1928 tt_str_op(SERVER_BUSY, OP_EQ, header);
1930 stats = geoip_format_dirreq_stats(time(NULL));
1931 tt_assert(stats);
1932 tt_assert(strstr(stats, "busy=8"));
1934 done:
1935 UNMOCK(get_options);
1936 tor_free(header);
1937 tor_free(body);
1938 or_options_free(mock_options); mock_options = NULL;
1940 tor_free(stats);
1941 dirserv_free_all();
1942 clear_geoip_db();
1945 static void
1946 test_dir_handle_get_status_vote_current_not_found(void* data)
1948 dir_connection_t *conn = NULL;
1949 char *header = NULL;
1950 (void) data;
1952 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1954 conn = new_dir_conn();
1955 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1956 GET("/tor/status-vote/current/" HEX1), NULL, 0));
1958 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
1959 NULL, NULL, 1, 0);
1960 tt_assert(header);
1961 tt_str_op(NOT_FOUND, OP_EQ, header);
1963 done:
1964 UNMOCK(connection_write_to_buf_impl_);
1965 connection_free_minimal(TO_CONN(conn));
1966 tor_free(header);
1969 #define VOTE_DIGEST "312A4890D4D832597ABBD3089C782DBBFB81E48D"
1971 static void
1972 status_vote_current_d_test(char **header, char **body, size_t *body_l)
1974 dir_connection_t *conn = NULL;
1976 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1978 conn = new_dir_conn();
1979 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
1980 GET("/tor/status-vote/current/d/" VOTE_DIGEST), NULL, 0));
1982 fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
1983 body, body_l, strlen(VOTE_BODY_V3)+1, 0);
1984 tt_assert(header);
1986 done:
1987 UNMOCK(connection_write_to_buf_impl_);
1988 connection_free_minimal(TO_CONN(conn));
1991 static void
1992 status_vote_next_d_test(char **header, char **body, size_t *body_l)
1994 dir_connection_t *conn = NULL;
1996 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
1998 conn = new_dir_conn();
1999 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2000 GET("/tor/status-vote/next/d/" VOTE_DIGEST), NULL, 0));
2002 fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
2003 body, body_l, strlen(VOTE_BODY_V3)+1, 0);
2004 tt_assert(header);
2006 done:
2007 UNMOCK(connection_write_to_buf_impl_);
2008 connection_free_minimal(TO_CONN(conn));
2011 static void
2012 test_dir_handle_get_status_vote_current_d_not_found(void* data)
2014 char *header = NULL;
2015 (void) data;
2017 status_vote_current_d_test(&header, NULL, NULL);
2019 tt_assert(header);
2020 tt_str_op(NOT_FOUND, OP_EQ, header);
2022 done:
2023 tor_free(header);
2026 static void
2027 test_dir_handle_get_status_vote_next_d_not_found(void* data)
2029 char *header = NULL;
2030 (void) data;
2032 status_vote_next_d_test(&header, NULL, NULL);
2034 tt_assert(header);
2035 tt_str_op(NOT_FOUND, OP_EQ, header);
2037 done:
2038 UNMOCK(connection_write_to_buf_impl_);
2039 tor_free(header);
2042 static void
2043 test_dir_handle_get_status_vote_d(void* data)
2045 char *header = NULL, *body = NULL;
2046 size_t body_used = 0;
2047 dir_server_t *ds = NULL;
2048 const char digest[DIGEST_LEN] = "";
2049 (void) data;
2051 clear_dir_servers();
2052 dirvote_free_all();
2054 /* create a trusted ds */
2055 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
2056 NULL, V3_DIRINFO, 1.0);
2057 tt_assert(ds);
2058 dir_server_add(ds);
2060 /* ds v3_identity_digest is the certificate's identity_key */
2061 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
2062 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
2064 init_mock_options();
2065 mock_options->AuthoritativeDir = 1;
2066 mock_options->V3AuthoritativeDir = 1;
2067 mock_options->TestingV3AuthVotingStartOffset = 0;
2068 mock_options->TestingV3AuthInitialVotingInterval = 1;
2069 mock_options->TestingV3AuthInitialVoteDelay = 1;
2070 mock_options->TestingV3AuthInitialDistDelay = 1;
2072 time_t now = 1441223455 -1;
2073 voting_schedule_recalculate_timing(mock_options, now);
2075 const char *msg_out = NULL;
2076 int status_out = 0;
2077 struct pending_vote_t *pv = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
2078 &status_out);
2079 tt_assert(pv);
2081 status_vote_current_d_test(&header, &body, &body_used);
2083 tt_assert(header);
2084 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2085 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2086 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2087 tt_assert(strstr(header, "Content-Length: 4135\r\n"));
2089 tt_str_op(VOTE_BODY_V3, OP_EQ, body);
2091 tor_free(header);
2092 tor_free(body);
2094 status_vote_next_d_test(&header, &body, &body_used);
2096 tt_assert(header);
2097 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2098 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2099 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2100 tt_assert(strstr(header, "Content-Length: 4135\r\n"));
2102 tt_str_op(VOTE_BODY_V3, OP_EQ, body);
2104 done:
2105 tor_free(header);
2106 tor_free(body);
2107 or_options_free(mock_options); mock_options = NULL;
2109 clear_dir_servers();
2110 dirvote_free_all();
2111 routerlist_free_all();
2114 static void
2115 test_dir_handle_get_status_vote_next_not_found(void* data)
2117 dir_connection_t *conn = NULL;
2118 char *header = NULL;
2119 (void) data;
2121 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2123 conn = new_dir_conn();
2124 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2125 GET("/tor/status-vote/next/" HEX1), NULL, 0));
2127 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
2128 NULL, NULL, 1, 0);
2129 tt_assert(header);
2130 tt_str_op(NOT_FOUND, OP_EQ, header);
2132 done:
2133 UNMOCK(connection_write_to_buf_impl_);
2134 connection_free_minimal(TO_CONN(conn));
2135 tor_free(header);
2138 static void
2139 status_vote_next_consensus_test(char **header, char **body, size_t *body_used)
2141 dir_connection_t *conn = NULL;
2143 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2145 conn = new_dir_conn();
2146 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2147 GET("/tor/status-vote/next/consensus"), NULL, 0));
2149 fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
2150 body, body_used, 18, 0);
2151 done:
2152 UNMOCK(connection_write_to_buf_impl_);
2153 connection_free_minimal(TO_CONN(conn));
2156 static void
2157 test_dir_handle_get_status_vote_next_consensus_not_found(void* data)
2159 char *header = NULL, *body = NULL;
2160 size_t body_used;
2161 (void) data;
2163 status_vote_next_consensus_test(&header, &body, &body_used);
2165 tt_assert(header);
2166 tt_str_op(NOT_FOUND, OP_EQ, header);
2168 done:
2169 tor_free(header);
2170 tor_free(body);
2173 static void
2174 test_dir_handle_get_status_vote_current_authority_not_found(void* data)
2176 dir_connection_t *conn = NULL;
2177 char *header = NULL;
2178 (void) data;
2180 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2182 conn = new_dir_conn();
2183 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2184 GET("/tor/status-vote/current/authority"), NULL, 0));
2186 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
2187 NULL, NULL, 1, 0);
2188 tt_assert(header);
2189 tt_str_op(NOT_FOUND, OP_EQ, header);
2191 done:
2192 UNMOCK(connection_write_to_buf_impl_);
2193 connection_free_minimal(TO_CONN(conn));
2194 tor_free(header);
2197 static void
2198 test_dir_handle_get_status_vote_next_authority_not_found(void* data)
2200 dir_connection_t *conn = NULL;
2201 char *header = NULL;
2202 (void) data;
2204 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2206 conn = new_dir_conn();
2207 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2208 GET("/tor/status-vote/next/authority"), NULL, 0));
2210 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
2211 NULL, NULL, 1, 0);
2212 tt_assert(header);
2213 tt_str_op(NOT_FOUND, OP_EQ, header);
2215 done:
2216 UNMOCK(connection_write_to_buf_impl_);
2217 connection_free_minimal(TO_CONN(conn));
2218 tor_free(header);
2221 NS_DECL(const char*,
2222 dirvote_get_pending_consensus, (consensus_flavor_t flav));
2224 const char*
2225 NS(dirvote_get_pending_consensus)(consensus_flavor_t flav)
2227 (void)flav;
2228 return "pending consensus";
2231 static void
2232 test_dir_handle_get_status_vote_next_consensus(void* data)
2234 char *header = NULL, *body = NULL;
2235 size_t body_used = 0;
2236 (void) data;
2238 NS_MOCK(dirvote_get_pending_consensus);
2240 status_vote_next_consensus_test(&header, &body, &body_used);
2241 tt_assert(header);
2243 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2244 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2245 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2246 tt_assert(strstr(header, "Content-Length: 17\r\n"));
2248 tt_str_op("pending consensus", OP_EQ, body);
2250 done:
2251 NS_UNMOCK(dirvote_get_pending_consensus);
2252 tor_free(header);
2253 tor_free(body);
2256 static void
2257 test_dir_handle_get_status_vote_next_consensus_busy(void* data)
2259 char *header = NULL, *body = NULL;
2260 size_t body_used = 0;
2261 (void) data;
2263 MOCK(get_options, mock_get_options);
2264 NS_MOCK(dirvote_get_pending_consensus);
2266 //Make it busy
2267 init_mock_options();
2268 mock_options->CountPrivateBandwidth = 1;
2270 status_vote_next_consensus_test(&header, &body, &body_used);
2272 tt_assert(header);
2273 tt_str_op(SERVER_BUSY, OP_EQ, header);
2275 done:
2276 NS_UNMOCK(dirvote_get_pending_consensus);
2277 UNMOCK(get_options);
2278 tor_free(header);
2279 tor_free(body);
2280 or_options_free(mock_options); mock_options = NULL;
2283 static void
2284 status_vote_next_consensus_signatures_test(char **header, char **body,
2285 size_t *body_used)
2287 dir_connection_t *conn = NULL;
2289 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2291 conn = new_dir_conn();
2292 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2293 GET("/tor/status-vote/next/consensus-signatures"), NULL, 0));
2295 fetch_from_buf_http(TO_CONN(conn)->outbuf, header, MAX_HEADERS_SIZE,
2296 body, body_used, 22, 0);
2298 done:
2299 connection_free_minimal(TO_CONN(conn));
2300 UNMOCK(connection_write_to_buf_impl_);
2303 static void
2304 test_dir_handle_get_status_vote_next_consensus_signatures_not_found(void* data)
2306 char *header = NULL, *body = NULL;
2307 size_t body_used;
2308 (void) data;
2310 status_vote_next_consensus_signatures_test(&header, &body, &body_used);
2312 tt_assert(header);
2313 tt_str_op(NOT_FOUND, OP_EQ, header);
2315 done:
2316 tor_free(header);
2317 tor_free(body);
2320 NS_DECL(const char*,
2321 dirvote_get_pending_detached_signatures, (void));
2323 const char*
2324 NS(dirvote_get_pending_detached_signatures)(void)
2326 return "pending detached sigs";
2329 static void
2330 test_dir_handle_get_status_vote_next_consensus_signatures(void* data)
2332 char *header = NULL, *body = NULL;
2333 size_t body_used = 0;
2334 (void) data;
2336 NS_MOCK(dirvote_get_pending_detached_signatures);
2338 status_vote_next_consensus_signatures_test(&header, &body, &body_used);
2339 tt_assert(header);
2341 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2342 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2343 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2344 tt_assert(strstr(header, "Content-Length: 21\r\n"));
2346 tt_str_op("pending detached sigs", OP_EQ, body);
2348 done:
2349 NS_UNMOCK(dirvote_get_pending_detached_signatures);
2350 tor_free(header);
2351 tor_free(body);
2354 static void
2355 test_dir_handle_get_status_vote_next_consensus_signatures_busy(void* data)
2357 char *header = NULL, *body = NULL;
2358 size_t body_used;
2359 (void) data;
2361 NS_MOCK(dirvote_get_pending_detached_signatures);
2362 MOCK(get_options, mock_get_options);
2364 //Make it busy
2365 init_mock_options();
2366 mock_options->CountPrivateBandwidth = 1;
2368 status_vote_next_consensus_signatures_test(&header, &body, &body_used);
2370 tt_assert(header);
2371 tt_str_op(SERVER_BUSY, OP_EQ, header);
2373 done:
2374 UNMOCK(get_options);
2375 NS_UNMOCK(dirvote_get_pending_detached_signatures);
2376 tor_free(header);
2377 tor_free(body);
2378 or_options_free(mock_options); mock_options = NULL;
2381 static void
2382 test_dir_handle_get_status_vote_next_authority(void* data)
2384 dir_connection_t *conn = NULL;
2385 char *header = NULL, *body = NULL;
2386 const char *msg_out = NULL;
2387 int status_out = 0;
2388 size_t body_used = 0;
2389 dir_server_t *ds = NULL;
2390 const char digest[DIGEST_LEN] = "";
2391 (void) data;
2393 clear_dir_servers();
2394 routerlist_free_all();
2395 dirvote_free_all();
2397 mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
2399 /* create a trusted ds */
2400 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
2401 NULL, V3_DIRINFO, 1.0);
2402 tt_assert(ds);
2403 dir_server_add(ds);
2405 /* ds v3_identity_digest is the certificate's identity_key */
2406 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
2407 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
2408 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
2409 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
2411 init_mock_options();
2412 mock_options->AuthoritativeDir = 1;
2413 mock_options->V3AuthoritativeDir = 1;
2414 mock_options->TestingV3AuthVotingStartOffset = 0;
2415 mock_options->TestingV3AuthInitialVotingInterval = 1;
2416 mock_options->TestingV3AuthInitialVoteDelay = 1;
2417 mock_options->TestingV3AuthInitialDistDelay = 1;
2419 time_t now = 1441223455 -1;
2420 voting_schedule_recalculate_timing(mock_options, now);
2422 struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
2423 &status_out);
2424 tt_assert(vote);
2426 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
2427 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2429 conn = new_dir_conn();
2430 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2431 GET("/tor/status-vote/next/authority"), NULL, 0));
2433 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
2434 &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
2436 tt_assert(header);
2437 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2438 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2439 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2440 tt_assert(strstr(header, "Content-Length: 4135\r\n"));
2442 tt_str_op(VOTE_BODY_V3, OP_EQ, body);
2444 done:
2445 UNMOCK(connection_write_to_buf_impl_);
2446 UNMOCK(get_my_v3_authority_cert);
2447 connection_free_minimal(TO_CONN(conn));
2448 tor_free(header);
2449 tor_free(body);
2450 authority_cert_free(mock_cert); mock_cert = NULL;
2451 or_options_free(mock_options); mock_options = NULL;
2453 clear_dir_servers();
2454 routerlist_free_all();
2455 dirvote_free_all();
2458 static void
2459 test_dir_handle_get_status_vote_current_authority(void* data)
2461 dir_connection_t *conn = NULL;
2462 char *header = NULL, *body = NULL;
2463 const char *msg_out = NULL;
2464 int status_out = 0;
2465 size_t body_used = 0;
2466 const char digest[DIGEST_LEN] = "";
2468 dir_server_t *ds = NULL;
2469 (void) data;
2471 clear_dir_servers();
2472 routerlist_free_all();
2473 dirvote_free_all();
2475 mock_cert = authority_cert_parse_from_string(TEST_CERTIFICATE, NULL);
2477 /* create a trusted ds */
2478 ds = trusted_dir_server_new("ds", "127.0.0.1", 9059, 9060, NULL, digest,
2479 NULL, V3_DIRINFO, 1.0);
2480 tt_assert(ds);
2481 dir_server_add(ds);
2483 /* ds v3_identity_digest is the certificate's identity_key */
2484 base16_decode(ds->v3_identity_digest, DIGEST_LEN,
2485 TEST_CERT_IDENT_KEY, HEX_DIGEST_LEN);
2487 tt_int_op(0, OP_EQ, trusted_dirs_load_certs_from_string(TEST_CERTIFICATE,
2488 TRUSTED_DIRS_CERTS_SRC_DL_BY_ID_DIGEST, 1, NULL));
2490 init_mock_options();
2491 mock_options->AuthoritativeDir = 1;
2492 mock_options->V3AuthoritativeDir = 1;
2493 mock_options->TestingV3AuthVotingStartOffset = 0;
2494 mock_options->TestingV3AuthInitialVotingInterval = 1;
2495 mock_options->TestingV3AuthInitialVoteDelay = 1;
2496 mock_options->TestingV3AuthInitialDistDelay = 1;
2498 time_t now = 1441223455;
2499 voting_schedule_recalculate_timing(mock_options, now-1);
2501 struct pending_vote_t *vote = dirvote_add_vote(VOTE_BODY_V3, &msg_out,
2502 &status_out);
2503 tt_assert(vote);
2505 // move the pending vote to previous vote
2506 dirvote_act(mock_options, now+1);
2508 MOCK(get_my_v3_authority_cert, get_my_v3_authority_cert_m);
2509 MOCK(connection_write_to_buf_impl_, connection_write_to_buf_mock);
2511 conn = new_dir_conn();
2512 tt_int_op(0, OP_EQ, directory_handle_command_get(conn,
2513 GET("/tor/status-vote/current/authority"), NULL, 0));
2515 fetch_from_buf_http(TO_CONN(conn)->outbuf, &header, MAX_HEADERS_SIZE,
2516 &body, &body_used, strlen(VOTE_BODY_V3)+1, 0);
2518 tt_assert(header);
2519 tt_ptr_op(strstr(header, "HTTP/1.0 200 OK\r\n"), OP_EQ, header);
2520 tt_assert(strstr(header, "Content-Type: text/plain\r\n"));
2521 tt_assert(strstr(header, "Content-Encoding: identity\r\n"));
2522 tt_assert(strstr(header, "Content-Length: 4135\r\n"));
2524 tt_str_op(VOTE_BODY_V3, OP_EQ, body);
2526 done:
2527 UNMOCK(connection_write_to_buf_impl_);
2528 UNMOCK(get_my_v3_authority_cert);
2529 connection_free_minimal(TO_CONN(conn));
2530 tor_free(header);
2531 tor_free(body);
2532 authority_cert_free(mock_cert); mock_cert = NULL;
2533 or_options_free(mock_options); mock_options = NULL;
2535 clear_dir_servers();
2536 routerlist_free_all();
2537 dirvote_free_all();
2540 static void
2541 test_dir_handle_get_parse_accept_encoding(void *arg)
2543 (void)arg;
2544 const unsigned B_NONE = 1u << NO_METHOD;
2545 const unsigned B_ZLIB = 1u << ZLIB_METHOD;
2546 const unsigned B_GZIP = 1u << GZIP_METHOD;
2547 const unsigned B_LZMA = 1u << LZMA_METHOD;
2548 const unsigned B_ZSTD = 1u << ZSTD_METHOD;
2550 unsigned encodings;
2552 encodings = parse_accept_encoding_header("");
2553 tt_uint_op(B_NONE, OP_EQ, encodings);
2555 encodings = parse_accept_encoding_header(" ");
2556 tt_uint_op(B_NONE, OP_EQ, encodings);
2558 encodings = parse_accept_encoding_header("dewey, cheatham, and howe ");
2559 tt_uint_op(B_NONE, OP_EQ, encodings);
2561 encodings = parse_accept_encoding_header("dewey, cheatham, and gzip");
2562 tt_uint_op(B_NONE, OP_EQ, encodings);
2564 encodings = parse_accept_encoding_header("dewey, cheatham, and, gzip");
2565 tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
2567 encodings = parse_accept_encoding_header(" gzip");
2568 tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
2570 encodings = parse_accept_encoding_header("gzip");
2571 tt_uint_op(B_NONE|B_GZIP, OP_EQ, encodings);
2573 encodings = parse_accept_encoding_header("x-zstd, deflate, x-tor-lzma");
2574 tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA, OP_EQ, encodings);
2576 encodings = parse_accept_encoding_header(
2577 "x-zstd, deflate, x-tor-lzma, gzip");
2578 tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
2580 encodings = parse_accept_encoding_header("x-zstd,deflate,x-tor-lzma,gzip");
2581 tt_uint_op(B_NONE|B_ZLIB|B_ZSTD|B_LZMA|B_GZIP, OP_EQ, encodings);
2583 done:
2587 #define DIR_HANDLE_CMD(name,flags) \
2588 { #name, test_dir_handle_get_##name, (flags), NULL, NULL }
2590 struct testcase_t dir_handle_get_tests[] = {
2591 DIR_HANDLE_CMD(not_found, 0),
2592 DIR_HANDLE_CMD(bad_request, 0),
2593 DIR_HANDLE_CMD(v1_command_not_found, 0),
2594 DIR_HANDLE_CMD(v1_command, 0),
2595 DIR_HANDLE_CMD(robots_txt, 0),
2596 DIR_HANDLE_CMD(rendezvous2_not_found_if_not_encrypted, 0),
2597 DIR_HANDLE_CMD(rendezvous2_not_found, 0),
2598 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_with_invalid_desc_id, 0),
2599 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_not_well_formed, 0),
2600 DIR_HANDLE_CMD(rendezvous2_on_encrypted_conn_success, 0),
2601 DIR_HANDLE_CMD(micro_d_not_found, 0),
2602 DIR_HANDLE_CMD(micro_d_server_busy, 0),
2603 DIR_HANDLE_CMD(micro_d, 0),
2604 DIR_HANDLE_CMD(networkstatus_bridges_not_found_without_auth, 0),
2605 DIR_HANDLE_CMD(networkstatus_bridges_not_found_wrong_auth, 0),
2606 DIR_HANDLE_CMD(networkstatus_bridges, 0),
2607 DIR_HANDLE_CMD(server_descriptors_not_found, 0),
2608 DIR_HANDLE_CMD(server_descriptors_busy, TT_FORK),
2609 DIR_HANDLE_CMD(server_descriptors_all, TT_FORK),
2610 DIR_HANDLE_CMD(server_descriptors_authority, TT_FORK),
2611 DIR_HANDLE_CMD(server_descriptors_fp, TT_FORK),
2612 DIR_HANDLE_CMD(server_descriptors_d, TT_FORK),
2613 DIR_HANDLE_CMD(server_keys_bad_req, 0),
2614 DIR_HANDLE_CMD(server_keys_busy, 0),
2615 DIR_HANDLE_CMD(server_keys_all_not_found, 0),
2616 DIR_HANDLE_CMD(server_keys_all, 0),
2617 DIR_HANDLE_CMD(server_keys_authority_not_found, 0),
2618 DIR_HANDLE_CMD(server_keys_authority, 0),
2619 DIR_HANDLE_CMD(server_keys_fp_not_found, 0),
2620 DIR_HANDLE_CMD(server_keys_fp, 0),
2621 DIR_HANDLE_CMD(server_keys_sk_not_found, 0),
2622 DIR_HANDLE_CMD(server_keys_sk, 0),
2623 DIR_HANDLE_CMD(server_keys_fpsk_not_found, 0),
2624 DIR_HANDLE_CMD(server_keys_fpsk, 0),
2625 DIR_HANDLE_CMD(status_vote_current_not_found, 0),
2626 DIR_HANDLE_CMD(status_vote_next_not_found, 0),
2627 DIR_HANDLE_CMD(status_vote_current_authority_not_found, 0),
2628 DIR_HANDLE_CMD(status_vote_current_authority, 0),
2629 DIR_HANDLE_CMD(status_vote_next_authority_not_found, 0),
2630 DIR_HANDLE_CMD(status_vote_next_authority, 0),
2631 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_enough_sigs, TT_FORK),
2632 DIR_HANDLE_CMD(status_vote_current_consensus_ns_not_found, TT_FORK),
2633 DIR_HANDLE_CMD(status_vote_current_consensus_too_old, TT_FORK),
2634 DIR_HANDLE_CMD(status_vote_current_consensus_ns_busy, TT_FORK),
2635 DIR_HANDLE_CMD(status_vote_current_consensus_ns, TT_FORK),
2636 DIR_HANDLE_CMD(status_vote_current_d_not_found, 0),
2637 DIR_HANDLE_CMD(status_vote_next_d_not_found, 0),
2638 DIR_HANDLE_CMD(status_vote_d, 0),
2639 DIR_HANDLE_CMD(status_vote_next_consensus_not_found, 0),
2640 DIR_HANDLE_CMD(status_vote_next_consensus_busy, 0),
2641 DIR_HANDLE_CMD(status_vote_next_consensus, 0),
2642 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_not_found, 0),
2643 DIR_HANDLE_CMD(status_vote_next_consensus_signatures_busy, 0),
2644 DIR_HANDLE_CMD(status_vote_next_consensus_signatures, 0),
2645 DIR_HANDLE_CMD(parse_accept_encoding, 0),
2646 END_OF_TESTCASES