| 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 <memory> |
| 8 |
| 7 #include "base/cancelable_callback.h" | 9 #include "base/cancelable_callback.h" |
| 8 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 9 #include "base/message_loop/message_loop.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/run_loop.h" |
| 10 #include "base/sys_byteorder.h" | 13 #include "base/sys_byteorder.h" |
| 11 #include "base/test/test_timeouts.h" | 14 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
| 13 #include "net/base/ip_address.h" | 16 #include "net/base/ip_address.h" |
| 14 #include "net/dns/dns_config_service_posix.h" | 17 #include "net/dns/dns_config_service_posix.h" |
| 15 #include "net/dns/dns_protocol.h" | 18 #include "net/dns/dns_protocol.h" |
| 16 | 19 |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 21 |
| 19 #if defined(OS_ANDROID) | 22 #if defined(OS_ANDROID) |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 #endif // !defined(OS_ANDROID) | 204 #endif // !defined(OS_ANDROID) |
| 202 | 205 |
| 203 class DnsConfigServicePosixTest : public testing::Test { | 206 class DnsConfigServicePosixTest : public testing::Test { |
| 204 public: | 207 public: |
| 205 DnsConfigServicePosixTest() : seen_config_(false) {} | 208 DnsConfigServicePosixTest() : seen_config_(false) {} |
| 206 ~DnsConfigServicePosixTest() override {} | 209 ~DnsConfigServicePosixTest() override {} |
| 207 | 210 |
| 208 void OnConfigChanged(const DnsConfig& config) { | 211 void OnConfigChanged(const DnsConfig& config) { |
| 209 EXPECT_TRUE(config.IsValid()); | 212 EXPECT_TRUE(config.IsValid()); |
| 210 seen_config_ = true; | 213 seen_config_ = true; |
| 211 base::MessageLoop::current()->QuitWhenIdle(); | 214 run_loop_->QuitWhenIdle(); |
| 212 } | 215 } |
| 213 | 216 |
| 214 void WriteMockHostsFile(const char* hosts_string) { | 217 void WriteMockHostsFile(const char* hosts_string) { |
| 215 ASSERT_EQ(base::WriteFile(temp_file_, hosts_string, strlen(hosts_string)), | 218 ASSERT_EQ(base::WriteFile(temp_file_, hosts_string, strlen(hosts_string)), |
| 216 static_cast<int>(strlen(hosts_string))); | 219 static_cast<int>(strlen(hosts_string))); |
| 217 } | 220 } |
| 218 | 221 |
| 219 void MockDNSConfig(const char* dns_server) { | 222 void MockDNSConfig(const char* dns_server) { |
| 220 IPAddress dns_address; | 223 IPAddress dns_address; |
| 221 ASSERT_TRUE(dns_address.AssignFromIPLiteral(dns_server)); | 224 ASSERT_TRUE(dns_address.AssignFromIPLiteral(dns_server)); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 260 MockHostsFilePath(temp_file_.value().c_str()); | 263 MockHostsFilePath(temp_file_.value().c_str()); |
| 261 MockDNSConfig("8.8.8.8"); | 264 MockDNSConfig("8.8.8.8"); |
| 262 seen_config_ = false; | 265 seen_config_ = false; |
| 263 service_->WatchConfig(base::Bind( | 266 service_->WatchConfig(base::Bind( |
| 264 &DnsConfigServicePosixTest::OnConfigChanged, base::Unretained(this))); | 267 &DnsConfigServicePosixTest::OnConfigChanged, base::Unretained(this))); |
| 265 ExpectChange(); | 268 ExpectChange(); |
| 266 } | 269 } |
| 267 | 270 |
| 268 void ExpectChange() { | 271 void ExpectChange() { |
| 269 EXPECT_FALSE(seen_config_); | 272 EXPECT_FALSE(seen_config_); |
| 270 base::MessageLoop::current()->Run(); | 273 run_loop_ = base::MakeUnique<base::RunLoop>(); |
| 274 run_loop_->Run(); |
| 271 EXPECT_TRUE(seen_config_); | 275 EXPECT_TRUE(seen_config_); |
| 272 seen_config_ = false; | 276 seen_config_ = false; |
| 273 } | 277 } |
| 274 | 278 |
| 275 bool seen_config_; | 279 bool seen_config_; |
| 276 base::Time creation_time_; | 280 base::Time creation_time_; |
| 277 base::FilePath temp_file_; | 281 base::FilePath temp_file_; |
| 278 std::unique_ptr<DnsConfigServicePosix> service_; | 282 std::unique_ptr<DnsConfigServicePosix> service_; |
| 279 DnsConfig test_config_; | 283 DnsConfig test_config_; |
| 284 std::unique_ptr<base::RunLoop> run_loop_; |
| 280 }; | 285 }; |
| 281 | 286 |
| 282 TEST_F(DnsConfigServicePosixTest, SeenChangeSinceNetworkChange) { | 287 TEST_F(DnsConfigServicePosixTest, SeenChangeSinceNetworkChange) { |
| 283 // Verify SeenChangeSince() returns false if no changes | 288 // Verify SeenChangeSince() returns false if no changes |
| 284 StartWatching(); | 289 StartWatching(); |
| 285 EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); | 290 EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); |
| 286 // Verify SeenChangeSince() returns true if network change | 291 // Verify SeenChangeSince() returns true if network change |
| 287 MockDNSConfig("8.8.4.4"); | 292 MockDNSConfig("8.8.4.4"); |
| 288 service_->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); | 293 service_->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); |
| 289 EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); | 294 EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); |
| 290 ExpectChange(); | 295 ExpectChange(); |
| 291 } | 296 } |
| 292 | 297 |
| 293 } // namespace internal | 298 } // namespace internal |
| 294 | 299 |
| 295 #endif // OS_ANDROID | 300 #endif // OS_ANDROID |
| 296 | 301 |
| 297 } // namespace net | 302 } // namespace net |
| OLD | NEW |