Line data Source code
1 : /* Copyright (c) 2001-2004, Roger Dingledine.
2 : * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 : * Copyright (c) 2007-2021, The Tor Project, Inc. */
4 : /* See LICENSE for licensing information */
5 :
6 : #include "orconfig.h"
7 : #define CONFIG_PRIVATE
8 :
9 : #include "core/or/or.h"
10 : #include "feature/dirclient/dir_server_st.h"
11 : #include "feature/nodelist/dirlist.h"
12 : #include "app/config/config.h"
13 : #include "test/test.h"
14 : #include "test/log_test_helpers.h"
15 :
16 : static void
17 1 : test_dirauth_port_parsing(void *arg)
18 : {
19 1 : (void)arg;
20 :
21 : // This one is okay.
22 1 : int rv = parse_dir_authority_line(
23 : "moria1 orport=9101 "
24 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
25 : "upload=http://128.31.0.39:9131/ "
26 : "download=http://128.31.0.39:9131 "
27 : "vote=http://128.31.0.39:9131/ "
28 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
29 : NO_DIRINFO, 1);
30 1 : tt_int_op(rv,OP_EQ,0);
31 :
32 : // These have bad syntax.
33 1 : setup_capture_of_logs(LOG_WARN);
34 1 : rv = parse_dir_authority_line(
35 : "moria1 orport=9101 "
36 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
37 : "uploadx=http://128.31.0.39:9131/ "
38 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
39 : NO_DIRINFO, 1);
40 1 : tt_int_op(rv,OP_EQ,0);
41 1 : expect_log_msg_containing("Unrecognized flag");
42 1 : mock_clean_saved_logs();
43 :
44 1 : rv = parse_dir_authority_line(
45 : "moria1 orport=9101 "
46 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
47 : "upload=https://128.31.0.39:9131/ " // https is not recognized
48 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
49 : NO_DIRINFO, 1);
50 1 : tt_int_op(rv,OP_EQ,-1);
51 1 : expect_log_msg_containing("Unsupported URL scheme");
52 1 : mock_clean_saved_logs();
53 :
54 1 : rv = parse_dir_authority_line(
55 : "moria1 orport=9101 "
56 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
57 : "upload=http://128.31.0.39:9131/tor " // suffix is not supported
58 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
59 : NO_DIRINFO, 1);
60 1 : tt_int_op(rv,OP_EQ,-1);
61 1 : expect_log_msg_containing("Unsupported URL prefix");
62 1 : mock_clean_saved_logs();
63 :
64 1 : rv = parse_dir_authority_line(
65 : "moria1 orport=9101 "
66 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
67 : "upload=http://128.31.0.256:9131/ " // "256" is not ipv4.
68 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
69 : NO_DIRINFO, 1);
70 1 : tt_int_op(rv,OP_EQ,-1);
71 1 : expect_log_msg_containing("Unable to parse address");
72 :
73 1 : rv = parse_dir_authority_line(
74 : "moria1 orport=9101 "
75 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
76 : "upload=http://xyz.example.com/ " // hostnames not supported.
77 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
78 : NO_DIRINFO, 1);
79 1 : tt_int_op(rv,OP_EQ,-1);
80 1 : expect_log_msg_containing("Unable to parse address");
81 :
82 1 : done:
83 1 : teardown_capture_of_logs();
84 1 : }
85 :
86 : static void
87 1 : test_dirauth_port_lookup(void *arg)
88 : {
89 1 : (void)arg;
90 :
91 1 : clear_dir_servers();
92 :
93 1 : int rv = parse_dir_authority_line(
94 : "moria1 orport=9101 "
95 : "v3ident=D586D18309DED4CD6D57C18FDB97EFA96D330566 "
96 : "upload=http://128.31.0.40:9132/ "
97 : "download=http://128.31.0.41:9133 "
98 : "vote=http://128.31.0.42:9134/ "
99 : "128.31.0.39:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
100 : NO_DIRINFO, 0);
101 1 : tt_int_op(rv,OP_EQ,0);
102 :
103 1 : rv = parse_dir_authority_line(
104 : "morgoth orport=9101 "
105 : "v3ident=D586D18309DED4CDFFFFFFFFDB97EFA96D330566 "
106 : "upload=http://128.31.0.43:9140/ "
107 : "128.31.0.44:9131 9695 DFC3 5FFE B861 329B 9F1A B04C 4639 7020 CE31",
108 : NO_DIRINFO, 0);
109 1 : tt_int_op(rv,OP_EQ,0);
110 :
111 1 : const smartlist_t *servers = router_get_trusted_dir_servers();
112 1 : tt_assert(servers);
113 1 : tt_int_op(smartlist_len(servers), OP_EQ, 2);
114 1 : const dir_server_t *moria = smartlist_get(servers, 0);
115 1 : const dir_server_t *morgoth = smartlist_get(servers, 1);
116 1 : tt_str_op(moria->nickname, OP_EQ, "moria1");
117 1 : tt_str_op(morgoth->nickname, OP_EQ, "morgoth");
118 :
119 1 : const tor_addr_port_t *dirport;
120 :
121 1 : dirport = trusted_dir_server_get_dirport(moria,
122 : AUTH_USAGE_UPLOAD, AF_INET);
123 1 : tt_int_op(dirport->port, OP_EQ, 9132);
124 1 : dirport = trusted_dir_server_get_dirport(moria,
125 : AUTH_USAGE_DOWNLOAD, AF_INET);
126 1 : tt_int_op(dirport->port, OP_EQ, 9133);
127 1 : dirport = trusted_dir_server_get_dirport(moria,
128 : AUTH_USAGE_VOTING, AF_INET);
129 1 : tt_int_op(dirport->port, OP_EQ, 9134);
130 :
131 1 : dirport = trusted_dir_server_get_dirport(morgoth,
132 : AUTH_USAGE_UPLOAD, AF_INET);
133 1 : tt_int_op(dirport->port, OP_EQ, 9140);
134 1 : dirport = trusted_dir_server_get_dirport(morgoth,
135 : AUTH_USAGE_DOWNLOAD, AF_INET);
136 1 : tt_int_op(dirport->port, OP_EQ, 9131); // fallback
137 1 : dirport = trusted_dir_server_get_dirport(morgoth,
138 : AUTH_USAGE_VOTING, AF_INET);
139 1 : tt_int_op(dirport->port, OP_EQ, 9131); // fallback
140 :
141 1 : done:
142 1 : ;
143 1 : }
144 :
145 : #define T(name) \
146 : { #name, test_dirauth_port_ ## name, TT_FORK, NULL, NULL }
147 :
148 : struct testcase_t dirauth_port_tests[] = {
149 : T(parsing),
150 : T(lookup),
151 : END_OF_TESTCASES
152 : };
|