OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <resolv.h> | 5 #include <resolv.h> |
6 | 6 |
| 7 #include "base/cancelable_callback.h" |
| 8 #include "base/files/file_util.h" |
7 #include "base/sys_byteorder.h" | 9 #include "base/sys_byteorder.h" |
| 10 #include "base/test/test_timeouts.h" |
| 11 #include "base/threading/platform_thread.h" |
8 #include "net/dns/dns_config_service_posix.h" | 12 #include "net/dns/dns_config_service_posix.h" |
| 13 #include "net/dns/dns_protocol.h" |
9 | 14 |
10 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
11 | 16 |
| 17 #if defined(OS_ANDROID) |
| 18 #include "base/android/path_utils.h" |
| 19 #endif // defined(OS_ANDROID) |
| 20 |
| 21 namespace net { |
| 22 |
12 #if !defined(OS_ANDROID) | 23 #if !defined(OS_ANDROID) |
13 | 24 |
14 namespace net { | |
15 namespace { | 25 namespace { |
16 | 26 |
17 // MAXNS is normally 3, but let's test 4 if possible. | 27 // MAXNS is normally 3, but let's test 4 if possible. |
18 const char* const kNameserversIPv4[] = { | 28 const char* const kNameserversIPv4[] = { |
19 "8.8.8.8", | 29 "8.8.8.8", |
20 "192.168.1.1", | 30 "192.168.1.1", |
21 "63.1.2.4", | 31 "63.1.2.4", |
22 "1.0.0.1", | 32 "1.0.0.1", |
23 }; | 33 }; |
24 | 34 |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
148 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS, | 158 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_NULL_ADDRESS, |
149 internal::ConvertResStateToDnsConfig(res, &config)); | 159 internal::ConvertResStateToDnsConfig(res, &config)); |
150 | 160 |
151 sa.sin_addr.s_addr = 0xDEADBEEF; | 161 sa.sin_addr.s_addr = 0xDEADBEEF; |
152 res.nsaddr_list[0] = sa; | 162 res.nsaddr_list[0] = sa; |
153 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK, | 163 EXPECT_EQ(internal::CONFIG_PARSE_POSIX_OK, |
154 internal::ConvertResStateToDnsConfig(res, &config)); | 164 internal::ConvertResStateToDnsConfig(res, &config)); |
155 } | 165 } |
156 | 166 |
157 } // namespace | 167 } // namespace |
| 168 |
| 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 |
158 } // namespace net | 272 } // namespace net |
159 | |
160 #endif // !OS_ANDROID | |
OLD | NEW |