Index: net/dns/dns_config_service_posix_unittest.cc |
diff --git a/net/dns/dns_config_service_posix_unittest.cc b/net/dns/dns_config_service_posix_unittest.cc |
index b208ffd74c30689ac3f792494d9243363172f3fb..4a9937014e225a2b33058725f9ad7e89c84e3f59 100644 |
--- a/net/dns/dns_config_service_posix_unittest.cc |
+++ b/net/dns/dns_config_service_posix_unittest.cc |
@@ -4,14 +4,24 @@ |
#include <resolv.h> |
+#include "base/cancelable_callback.h" |
+#include "base/files/file_util.h" |
#include "base/sys_byteorder.h" |
+#include "base/test/test_timeouts.h" |
+#include "base/threading/platform_thread.h" |
#include "net/dns/dns_config_service_posix.h" |
+#include "net/dns/dns_protocol.h" |
#include "testing/gtest/include/gtest/gtest.h" |
-#if !defined(OS_ANDROID) |
+#if defined(OS_ANDROID) |
+#include "base/android/path_utils.h" |
+#endif // defined(OS_ANDROID) |
namespace net { |
+ |
+#if !defined(OS_ANDROID) |
+ |
namespace { |
// MAXNS is normally 3, but let's test 4 if possible. |
@@ -155,6 +165,108 @@ TEST(DnsConfigServicePosixTest, RejectEmptyNameserver) { |
} |
} // namespace |
-} // namespace net |
-#endif // !OS_ANDROID |
+#else // OS_ANDROID |
+ |
+namespace internal { |
+ |
+const char kTempHosts1[] = "127.0.0.1 localhost"; |
+const char kTempHosts2[] = "127.0.0.2 localhost"; |
+ |
+class DnsConfigServicePosixTest : public testing::Test { |
+ public: |
+ DnsConfigServicePosixTest() : seen_config_(false) {} |
+ ~DnsConfigServicePosixTest() override {} |
+ |
+ void OnConfigChanged(const DnsConfig& config) { |
+ EXPECT_TRUE(config.IsValid()); |
+ seen_config_ = true; |
+ base::MessageLoop::current()->Quit(); |
+ } |
+ |
+ void WriteMockHostsFile(const char* hosts_string) { |
+ ASSERT_EQ(base::WriteFile(temp_file_, hosts_string, strlen(hosts_string)), |
+ static_cast<int>(strlen(hosts_string))); |
+ } |
+ |
+ void MockDNSConfig(const char* dns_server) { |
+ IPAddressNumber dns_number; |
+ ASSERT_TRUE(ParseIPLiteralToNumber(dns_server, &dns_number)); |
+ test_config_.nameservers.clear(); |
+ test_config_.nameservers.push_back( |
+ IPEndPoint(dns_number, dns_protocol::kDefaultPort)); |
+ service_->SetDnsConfigForTesting(&test_config_); |
+ } |
+ |
+ void SetUp() override { |
+ // TODO(pauljensen): Get rid of GetExternalStorageDirectory() when |
+ // crbug.com/475568 is fixed. For now creating a temp file in the |
+ // default temp directory (/data/data/...) will cause FilePathWatcher |
+ // to fail, so create the temp file in /sdcard. |
+ base::FilePath parent_dir; |
+ ASSERT_TRUE(base::android::GetExternalStorageDirectory(&parent_dir)); |
+ ASSERT_TRUE(base::CreateTemporaryFileInDir(parent_dir, &temp_file_)); |
+ WriteMockHostsFile(kTempHosts1); |
+ // Set the time on the hosts file back so it appears older than the |
+ // 1s safety offset in DnsConfigServicePosix::SeenChangeSince(). |
+ // TODO(pauljensen): Switch from Sleep() to TouchFile() when |
+ // crbug.com/475568 is fixed. For now TouchFile() will fail in /sdcard. |
+ base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(1100)); |
+ // // Copy real hosts file's last modified time to mock hosts file. |
+ // base::File hosts(base::FilePath(DnsConfigServicePosix::kFilePathHosts), |
+ // base::File::FLAG_OPEN | base::File::FLAG_READ); |
+ // base::File::Info hosts_info; |
+ // ASSERT_TRUE(hosts.GetInfo(&hosts_info)); |
+ // ASSERT_TRUE(base::TouchFile(temp_file_, hosts_info.last_modified, |
+ // hosts_info.last_accessed)); |
+ } |
+ |
+ void TearDown() override { ASSERT_TRUE(base::DeleteFile(temp_file_, false)); } |
+ |
+ void StartWatching() { |
+ creation_time_ = base::Time::Now(); |
+ service_.reset(new DnsConfigServicePosix()); |
+ service_->file_path_hosts_ = temp_file_.value().c_str(); |
+ MockDNSConfig("8.8.8.8"); |
+ seen_config_ = false; |
+ service_->WatchConfig(base::Bind( |
+ &DnsConfigServicePosixTest::OnConfigChanged, base::Unretained(this))); |
+ ExpectChange(); |
+ } |
+ |
+ void ExpectChange() { |
+ EXPECT_FALSE(seen_config_); |
+ base::MessageLoop::current()->Run(); |
+ EXPECT_TRUE(seen_config_); |
+ seen_config_ = false; |
+ } |
+ |
+ bool seen_config_; |
+ base::Time creation_time_; |
+ base::FilePath temp_file_; |
+ scoped_ptr<DnsConfigServicePosix> service_; |
+ DnsConfig test_config_; |
+}; |
+ |
+TEST_F(DnsConfigServicePosixTest, SeenChangeSince) { |
+ // Verify SeenChangeSince() returns false if no changes |
+ StartWatching(); |
+ EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); |
+ // Verify SeenChangeSince() returns true if network change |
+ MockDNSConfig("8.8.4.4"); |
+ service_->OnNetworkChanged(NetworkChangeNotifier::CONNECTION_WIFI); |
+ EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); |
+ ExpectChange(); |
+ // Verify SeenChangeSince() returns true if hosts file changes |
+ StartWatching(); |
+ EXPECT_FALSE(service_->SeenChangeSince(creation_time_)); |
+ WriteMockHostsFile(kTempHosts2); |
+ EXPECT_TRUE(service_->SeenChangeSince(creation_time_)); |
+ ExpectChange(); |
+} |
+ |
+} // namespace internal |
+ |
+#endif // OS_ANDROID |
+ |
+} // namespace net |