test_dirauth_ports.c (5132B)
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 test_dirauth_port_parsing(void *arg) 18 { 19 (void)arg; 20 21 // This one is okay. 22 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 tt_int_op(rv,OP_EQ,0); 31 32 // These have bad syntax. 33 setup_capture_of_logs(LOG_WARN); 34 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 tt_int_op(rv,OP_EQ,0); 41 expect_log_msg_containing("Unrecognized flag"); 42 mock_clean_saved_logs(); 43 44 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 tt_int_op(rv,OP_EQ,-1); 51 expect_log_msg_containing("Unsupported URL scheme"); 52 mock_clean_saved_logs(); 53 54 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 tt_int_op(rv,OP_EQ,-1); 61 expect_log_msg_containing("Unsupported URL prefix"); 62 mock_clean_saved_logs(); 63 64 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 tt_int_op(rv,OP_EQ,-1); 71 expect_log_msg_containing("Unable to parse address"); 72 73 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 tt_int_op(rv,OP_EQ,-1); 80 expect_log_msg_containing("Unable to parse address"); 81 82 done: 83 teardown_capture_of_logs(); 84 } 85 86 static void 87 test_dirauth_port_lookup(void *arg) 88 { 89 (void)arg; 90 91 clear_dir_servers(); 92 93 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 tt_int_op(rv,OP_EQ,0); 102 103 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 tt_int_op(rv,OP_EQ,0); 110 111 const smartlist_t *servers = router_get_trusted_dir_servers(); 112 tt_assert(servers); 113 tt_int_op(smartlist_len(servers), OP_EQ, 2); 114 const dir_server_t *moria = smartlist_get(servers, 0); 115 const dir_server_t *morgoth = smartlist_get(servers, 1); 116 tt_str_op(moria->nickname, OP_EQ, "moria1"); 117 tt_str_op(morgoth->nickname, OP_EQ, "morgoth"); 118 119 const tor_addr_port_t *dirport; 120 121 dirport = trusted_dir_server_get_dirport(moria, 122 AUTH_USAGE_UPLOAD, AF_INET); 123 tt_int_op(dirport->port, OP_EQ, 9132); 124 dirport = trusted_dir_server_get_dirport(moria, 125 AUTH_USAGE_DOWNLOAD, AF_INET); 126 tt_int_op(dirport->port, OP_EQ, 9133); 127 dirport = trusted_dir_server_get_dirport(moria, 128 AUTH_USAGE_VOTING, AF_INET); 129 tt_int_op(dirport->port, OP_EQ, 9134); 130 131 dirport = trusted_dir_server_get_dirport(morgoth, 132 AUTH_USAGE_UPLOAD, AF_INET); 133 tt_int_op(dirport->port, OP_EQ, 9140); 134 dirport = trusted_dir_server_get_dirport(morgoth, 135 AUTH_USAGE_DOWNLOAD, AF_INET); 136 tt_int_op(dirport->port, OP_EQ, 9131); // fallback 137 dirport = trusted_dir_server_get_dirport(morgoth, 138 AUTH_USAGE_VOTING, AF_INET); 139 tt_int_op(dirport->port, OP_EQ, 9131); // fallback 140 141 done: 142 ; 143 } 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 };