[email protected] | fea484e | 2012-02-07 23:21:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 5 | #include <resolv.h> |
| 6 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 7 | #include "base/cancelable_callback.h" |
| 8 | #include "base/files/file_util.h" |
[email protected] | 0e6f619 | 2011-12-28 23:18:21 | [diff] [blame] | 9 | #include "base/sys_byteorder.h" |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 10 | #include "base/test/test_timeouts.h" |
| 11 | #include "base/threading/platform_thread.h" |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 12 | #include "net/dns/dns_config_service_posix.h" |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 13 | #include "net/dns/dns_protocol.h" |
[email protected] | 6998a66 | 2011-09-03 00:17:29 | [diff] [blame] | 14 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 17 | #if defined(OS_ANDROID) |
| 18 | #include "base/android/path_utils.h" |
| 19 | #endif // defined(OS_ANDROID) |
[email protected] | f667903 | 2014-01-21 20:31:14 | [diff] [blame] | 20 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 21 | namespace net { |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 22 | |
| 23 | #if !defined(OS_ANDROID) |
| 24 | |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 25 | namespace { |
| 26 | |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 27 | // MAXNS is normally 3, but let's test 4 if possible. |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 28 | const char* const kNameserversIPv4[] = { |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 29 | "8.8.8.8", |
| 30 | "192.168.1.1", |
| 31 | "63.1.2.4", |
| 32 | "1.0.0.1", |
| 33 | }; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 34 | |
[email protected] | 629ddf85 | 2012-07-20 16:17:29 | [diff] [blame] | 35 | #if defined(OS_LINUX) |
thestig | 9d3bb0c | 2015-01-24 00:49:51 | [diff] [blame] | 36 | const char* const kNameserversIPv6[] = { |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 37 | NULL, |
| 38 | "2001:DB8:0::42", |
| 39 | NULL, |
| 40 | "::FFFF:129.144.52.38", |
| 41 | }; |
[email protected] | 629ddf85 | 2012-07-20 16:17:29 | [diff] [blame] | 42 | #endif |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 43 | |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 44 | // Fills in |res| with sane configuration. |
| 45 | void InitializeResState(res_state res) { |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 46 | memset(res, 0, sizeof(*res)); |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 47 | res->options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH | |
| 48 | RES_ROTATE; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 49 | res->ndots = 2; |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 50 | res->retrans = 4; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 51 | res->retry = 7; |
| 52 | |
| 53 | const char kDnsrch[] = "chromium.org" "\0" "example.com"; |
| 54 | memcpy(res->defdname, kDnsrch, sizeof(kDnsrch)); |
| 55 | res->dnsrch[0] = res->defdname; |
| 56 | res->dnsrch[1] = res->defdname + sizeof("chromium.org"); |
| 57 | |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 58 | for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) { |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 59 | struct sockaddr_in sa; |
| 60 | sa.sin_family = AF_INET; |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 61 | sa.sin_port = base::HostToNet16(NS_DEFAULTPORT + i); |
| 62 | inet_pton(AF_INET, kNameserversIPv4[i], &sa.sin_addr); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 63 | res->nsaddr_list[i] = sa; |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 64 | ++res->nscount; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 65 | } |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 66 | |
[email protected] | 5c5b738 | 2011-08-18 18:02:33 | [diff] [blame] | 67 | #if defined(OS_LINUX) |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 68 | // Install IPv6 addresses, replacing the corresponding IPv4 addresses. |
| 69 | unsigned nscount6 = 0; |
| 70 | for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) { |
| 71 | if (!kNameserversIPv6[i]) |
| 72 | continue; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 73 | // Must use malloc to mimick res_ninit. |
| 74 | struct sockaddr_in6 *sa6; |
| 75 | sa6 = (struct sockaddr_in6 *)malloc(sizeof(*sa6)); |
| 76 | sa6->sin6_family = AF_INET6; |
[email protected] | 9eb7b11b | 2012-03-28 20:19:31 | [diff] [blame] | 77 | sa6->sin6_port = base::HostToNet16(NS_DEFAULTPORT - i); |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 78 | inet_pton(AF_INET6, kNameserversIPv6[i], &sa6->sin6_addr); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 79 | res->_u._ext.nsaddrs[i] = sa6; |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 80 | memset(&res->nsaddr_list[i], 0, sizeof res->nsaddr_list[i]); |
| 81 | ++nscount6; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 82 | } |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 83 | res->_u._ext.nscount6 = nscount6; |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 84 | #endif |
| 85 | } |
| 86 | |
| 87 | void CloseResState(res_state res) { |
[email protected] | 5c5b738 | 2011-08-18 18:02:33 | [diff] [blame] | 88 | #if defined(OS_LINUX) |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 89 | for (int i = 0; i < res->nscount; ++i) { |
| 90 | if (res->_u._ext.nsaddrs[i] != NULL) |
| 91 | free(res->_u._ext.nsaddrs[i]); |
| 92 | } |
| 93 | #endif |
| 94 | } |
| 95 | |
| 96 | void InitializeExpectedConfig(DnsConfig* config) { |
| 97 | config->ndots = 2; |
| 98 | config->timeout = base::TimeDelta::FromSeconds(4); |
| 99 | config->attempts = 7; |
| 100 | config->rotate = true; |
| 101 | config->edns0 = false; |
| 102 | config->append_to_multi_label_name = true; |
| 103 | config->search.clear(); |
| 104 | config->search.push_back("chromium.org"); |
| 105 | config->search.push_back("example.com"); |
| 106 | |
| 107 | config->nameservers.clear(); |
| 108 | for (unsigned i = 0; i < arraysize(kNameserversIPv4) && i < MAXNS; ++i) { |
| 109 | IPAddressNumber ip; |
| 110 | ParseIPLiteralToNumber(kNameserversIPv4[i], &ip); |
| 111 | config->nameservers.push_back(IPEndPoint(ip, NS_DEFAULTPORT + i)); |
| 112 | } |
| 113 | |
| 114 | #if defined(OS_LINUX) |
| 115 | for (unsigned i = 0; i < arraysize(kNameserversIPv6) && i < MAXNS; ++i) { |
| 116 | if (!kNameserversIPv6[i]) |
| 117 | continue; |
| 118 | IPAddressNumber ip; |
| 119 | ParseIPLiteralToNumber(kNameserversIPv6[i], &ip); |
| 120 | config->nameservers[i] = IPEndPoint(ip, NS_DEFAULTPORT - i); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 121 | } |
| 122 | #endif |
| 123 | } |
| 124 | |
[email protected] | b3601bc2 | 2012-02-21 21:23:20 | [diff] [blame] | 125 | TEST(DnsConfigServicePosixTest, ConvertResStateToDnsConfig) { |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 126 | struct __res_state res; |
| 127 | DnsConfig config; |
| 128 | EXPECT_FALSE(config.IsValid()); |
| 129 | InitializeResState(&res); |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 130 | ASSERT_EQ(internal::CONFIG_PARSE_POSIX_OK, |
| 131 | internal::ConvertResStateToDnsConfig(res, &config)); |
[email protected] | 7549cc2 | 2012-05-12 09:39:55 | [diff] [blame] | 132 | CloseResState(&res); |
| 133 | EXPECT_TRUE(config.IsValid()); |
| 134 | |
| 135 | DnsConfig expected_config; |
| 136 | EXPECT_FALSE(expected_config.EqualsIgnoreHosts(config)); |
| 137 | InitializeExpectedConfig(&expected_config); |
| 138 | EXPECT_TRUE(expected_config.EqualsIgnoreHosts(config)); |
[email protected] | d84316a | 2011-08-18 04:41:21 | [diff] [blame] | 139 | } |
| 140 | |
[email protected] | 45d2f847 | 2012-06-14 17:48:39 | [diff] [blame] | 141 | TEST(DnsConfigServicePosixTest, RejectEmptyNameserver) { |
| 142 | struct __res_state res = {}; |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 143 | res.options = RES_INIT | RES_RECURSE | RES_DEFNAMES | RES_DNSRCH; |
[email protected] | 45d2f847 | 2012-06-14 17:48:39 | [diff] [blame] | 144 | const char kDnsrch[] = "chromium.org"; |
| 145 | memcpy(res.defdname, kDnsrch, sizeof(kDnsrch)); |
| 146 | res.dnsrch[0] = res.defdname; |
| 147 | |
| 148 | struct sockaddr_in sa = {}; |
| 149 | sa.sin_family = AF_INET; |
| 150 | sa.sin_port = base::HostToNet16(NS_DEFAULTPORT); |
| 151 | sa.sin_addr.s_addr = INADDR_ANY; |
| 152 | res.nsaddr_list[0] = sa; |
| 153 | sa.sin_addr.s_addr = 0xCAFE1337; |
| 154 | res.nsaddr_list[1] = sa; |
| 155 | res.nscount = 2; |
| 156 | |
| 157 | DnsConfig config; |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 158 | EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS, |
| 159 | internal::ConvertResStateToDnsConfig(res, &config)); |
[email protected] | 45d2f847 | 2012-06-14 17:48:39 | [diff] [blame] | 160 | |
| 161 | sa.sin_addr.s_addr = 0xDEADBEEF; |
| 162 | res.nsaddr_list[0] = sa; |
[email protected] | 539df6c | 2012-06-19 21:21:29 | [diff] [blame] | 163 | EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK, |
| 164 | internal::ConvertResStateToDnsConfig(res, &config)); |
[email protected] | 45d2f847 | 2012-06-14 17:48:39 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | b4481b22 | 2012-03-16 17:13:11 | [diff] [blame] | 167 | } // namespace |
[email protected] | f667903 | 2014-01-21 20:31:14 | [diff] [blame] | 168 | |
pauljensen | 101ed37 | 2015-04-17 00:11:42 | [diff] [blame] | 169 | #else // OS_ANDROID |
| 170 | |
| 171 | namespace internal { |
| 172 | |
| 173 | const char kTempHosts1[] = "127.0.0.1 localhost"; |
| 174 | const char kTempHosts2[] = "127.0.0.2 localhost"; |
| 175 | |
| 176 | class DnsConfigServicePosixTest : public testing::Test { |
| 177 | public: |
| 178 | DnsConfigServicePosixTest() : seen_config_(false) {} |
| 179 | ~DnsConfigServicePosixTest() override {} |
| 180 | |
| 181 | void OnConfigChanged(const DnsConfig& config) { |
| 182 | EXPECT_TRUE(config.IsValid()); |
| 183 | seen_config_ = true; |
| 184 | base::MessageLoop::current()->Quit(); |
| 185 | } |
| 186 | |
| 187 | void WriteMockHostsFile(const char* hosts_string) { |
| 188 | ASSERT_EQ(base::WriteFile(temp_file_, hosts_string, strlen(hosts_string)), |
| 189 | static_cast<int>(strlen(hosts_string))); |
| 190 | } |
| 191 | |
| 192 | void MockDNSConfig(const char* dns_server) { |
| 193 | IPAddressNumber dns_number; |
| 194 | ASSERT_TRUE(ParseIPLiteralToNumber(dns_server, &dns_number)); |
| 195 | test_config_.nameservers.clear(); |
| 196 | test_config_.nameservers.push_back( |
| 197 | IPEndPoint(dns_number, dns_protocol::kDefaultPort)); |
| 198 | service_->SetDnsConfigForTesting(&test_config_); |
| 199 | } |
| 200 | |
| 201 | void SetUp() override { |
| 202 | // TODO(pauljensen): Get rid of GetExternalStorageDirectory() when |
| 203 | // crbug.com/475568 is fixed. For now creating a temp file in the |
| 204 | // default temp directory (/data/data/...) will cause FilePathWatcher |
| 205 | // to fail, so create the temp file in /sdcard. |
| 206 | base::FilePath parent_dir; |
| 207 | ASSERT_TRUE(base::android::GetExternalStorageDirectory(&parent_dir)); |
| 208 | ASSERT_TRUE(base::CreateTemporaryFileInDir(parent_dir, &temp_file_)); |
| 209 | WriteMockHostsFile(kTempHosts1); |
| 210 | // Set the time on the hosts file back so it appears older than the |
| 211 | // 1s safety offset in DnsConfigServicePosix::SeenChangeSince(). |
| 212 | // TODO(pauljensen): Switch from Sleep() to TouchFile() when |
| 213 | // crbug.com/475568 is fixed. For now TouchFile() will fail in /sdcard. |
| 214 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1100)); |
| 215 | // // Copy real hosts file's last modified time to mock hosts file. |
| 216 | // base::File hosts(base::FilePath(DnsConfigServicePosix::kFilePathHosts), |
| 217 | // base::File::FLAG_OPEN | base::File::FLAG_READ); |
| 218 | // base::File::Info hosts_info; |
| 219 | // ASSERT_TRUE(hosts.GetInfo(&hosts_info)); |
| 220 | // ASSERT_TRUE(base::TouchFile(temp_file_, hosts_info.last_modified, |
| 221 | // hosts_info.last_accessed)); |
| 222 | } |
| 223 | |
| 224 | void TearDown() override { ASSERT_TRUE(base::DeleteFile(temp_file_, false)); } |
| 225 | |
| 226 | void StartWatching() { |
| 227 | creation_time_ = base::Time::Now(); |
| 228 | service_.reset(new DnsConfigServicePosix()); |
| 229 | service_->file_path_hosts_ = temp_file_.value().c_str(); |
| 230 | MockDNSConfig("8.8.8.8"); |
| 231 | seen_config_ = false; |
| 232 | service_->WatchConfig(base::Bind( |
| 233 | &DnsConfigServicePosixTest::OnConfigChanged, base::Unretained(this))); |
| 234 | ExpectChange(); |
| 235 | } |
| 236 | |
| 237 | void ExpectChange() { |
| 238 | EXPECT_FALSE(seen_config_); |
| 239 | base::MessageLoop::current()->Run(); |
| 240 | EXPECT_TRUE(seen_config_); |
| 241 | seen_config_ = false; |
| 242 | } |
| 243 | |
| 244 | bool seen_config_; |
| 245 | base::Time creation_time_; |
| 246 | base::FilePath temp_file_; |
| 247 | scoped_ptr<DnsConfigServicePosix> service_; |
| 248 | DnsConfig test_config_; |
| 249 | }; |
| 250 | |
| 251 | TEST_F(DnsConfigServicePosixTest, SeenChangeSince) { |
| 252 | // Verify SeenChangeSince() returns false if no changes |
| 253 | StartWatching(); |
| 254 | EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); |
| 255 | // Verify SeenChangeSince() returns true if network change |
| 256 | MockDNSConfig("8.8.4.4"); |
| 257 | service_->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); |
| 258 | EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); |
| 259 | ExpectChange(); |
| 260 | // Verify SeenChangeSince() returns true if hosts file changes |
| 261 | StartWatching(); |
| 262 | EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); |
| 263 | WriteMockHostsFile(kTempHosts2); |
| 264 | EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); |
| 265 | ExpectChange(); |
| 266 | } |
| 267 | |
| 268 | } // namespace internal |
| 269 | |
| 270 | #endif // OS_ANDROID |
| 271 | |
| 272 | } // namespace net |