diff options
author | pauljensen <pauljensen@chromium.org> | 2015-04-16 17:11:42 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-17 00:12:08 +0000 |
commit | 101ed37a9cb2f1a7e906ab9f4e3a1c6331cdf8b3 (patch) | |
tree | d5800843c08d572a92598810ba107bb5a8b3bb80 /net/android/network_change_notifier_android_unittest.cc | |
parent | 6ae2d0ad19bfe63d071d5dd24240e3b5cc7273ca (diff) | |
download | chromium_src-101ed37a9cb2f1a7e906ab9f4e3a1c6331cdf8b3.zip chromium_src-101ed37a9cb2f1a7e906ab9f4e3a1c6331cdf8b3.tar.gz chromium_src-101ed37a9cb2f1a7e906ab9f4e3a1c6331cdf8b3.tar.bz2 |
Avoid initial NetworkChangeNotifier OnDNSChanged() signal on Android
When the DnsConfigServicePosix finishes its initial reading of the
system DNS config, it normally triggers a NetworkChangeNotifier (NCN)
OnDNSChanged() signal. This can cause in-flight network requests to
abort with ERR_NETWORK_CHANGED. Avoid aborting requests by:
1. Adding a new NCN signal, OnInitialDNSConfigRead which indicates
the initial DNS config reading completed but does not represent
a change in DNS config.
2. Modify HostResolverImpl to not abort requests upon this new
signal (like it does for the OnDNSChanged signal).
3. Add logic to NetworkChangeNotifierAndroid to emit this new
signal when safe to do so. Network requests begin being issued
immediately after the NCN (and hence DnsConfigService) is
initialized, so we need to be sure no network change signals
are missed between NCN initialization completing and the
OnInitialDNSConfigRead signal. This is tricky because the
NCN (and hence DnsConfigService) is initialized on threads
where file I/O is not allowed. Were file I/O allowed we could
simply slurp up the DNS config and hosts file. Instead we
start listening for network changes (which is our trigger signal
for DNS changes on Android) on the initialization thread and
record the current time to later compare against the hosts
file's last-modified time to check for changes to the file.
Actual loading of the DNS config and hosts file is done on
another thread that allows file I/O.
BUG=470897
Review URL: https://codereview.chromium.org/1047103002
Cr-Commit-Position: refs/heads/master@{#325560}
Diffstat (limited to 'net/android/network_change_notifier_android_unittest.cc')
-rw-r--r-- | net/android/network_change_notifier_android_unittest.cc | 64 |
1 files changed, 51 insertions, 13 deletions
diff --git a/net/android/network_change_notifier_android_unittest.cc b/net/android/network_change_notifier_android_unittest.cc index 83dd70e..30ee4f0 100644 --- a/net/android/network_change_notifier_android_unittest.cc +++ b/net/android/network_change_notifier_android_unittest.cc @@ -12,6 +12,8 @@ #include "net/android/network_change_notifier_android.h" #include "net/android/network_change_notifier_delegate_android.h" #include "net/base/network_change_notifier.h" +#include "net/dns/dns_config_service.h" +#include "net/dns/dns_protocol.h" #include "testing/gtest/include/gtest/gtest.h" namespace net { @@ -60,6 +62,30 @@ class NetworkChangeNotifierObserver int notifications_count_; }; +class DNSChangeObserver : public NetworkChangeNotifier::DNSObserver { + public: + DNSChangeObserver() + : change_notifications_count_(0), initial_notifications_count_(0) {} + + // NetworkChangeNotifier::DNSObserver: + void OnDNSChanged() override { change_notifications_count_++; } + + void OnInitialDNSConfigRead() override { + initial_notifications_count_++; + base::MessageLoop::current()->Quit(); + } + + int change_notifications_count() const { return change_notifications_count_; } + + int initial_notifications_count() const { + return initial_notifications_count_; + } + + private: + int change_notifications_count_; + int initial_notifications_count_; +}; + } // namespace class BaseNetworkChangeNotifierAndroidTest : public testing::Test { @@ -175,7 +201,12 @@ TEST_F(NetworkChangeNotifierDelegateAndroidTest, DelegateObserverNotified) { class NetworkChangeNotifierAndroidTest : public BaseNetworkChangeNotifierAndroidTest { protected: - NetworkChangeNotifierAndroidTest() : notifier_(&delegate_) { + void SetUp() override { + IPAddressNumber dns_number; + ASSERT_TRUE(ParseIPLiteralToNumber("8.8.8.8", &dns_number)); + dns_config_.nameservers.push_back( + IPEndPoint(dns_number, dns_protocol::kDefaultPort)); + notifier_.reset(new NetworkChangeNotifierAndroid(&delegate_, &dns_config_)); NetworkChangeNotifier::AddConnectionTypeObserver( &connection_type_observer_); NetworkChangeNotifier::AddConnectionTypeObserver( @@ -185,7 +216,8 @@ class NetworkChangeNotifierAndroidTest NetworkChangeNotifierObserver connection_type_observer_; NetworkChangeNotifierObserver other_connection_type_observer_; NetworkChangeNotifier::DisableForTest disable_for_test_; - NetworkChangeNotifierAndroid notifier_; + DnsConfig dns_config_; + scoped_ptr<NetworkChangeNotifierAndroid> notifier_; }; // When a NetworkChangeNotifierAndroid is observing a @@ -194,13 +226,10 @@ class NetworkChangeNotifierAndroidTest // NetworkChangeNotifierAndroid should reflect that state. TEST_F(NetworkChangeNotifierAndroidTest, NotificationsSentToNetworkChangeNotifierAndroid) { - RunTest( - base::Bind( - &NetworkChangeNotifierObserver::notifications_count, - base::Unretained(&connection_type_observer_)), - base::Bind( - &NetworkChangeNotifierAndroid::GetCurrentConnectionType, - base::Unretained(¬ifier_))); + RunTest(base::Bind(&NetworkChangeNotifierObserver::notifications_count, + base::Unretained(&connection_type_observer_)), + base::Bind(&NetworkChangeNotifierAndroid::GetCurrentConnectionType, + base::Unretained(notifier_.get()))); } // When a NetworkChangeNotifierAndroid's connection state changes, it should @@ -220,13 +249,13 @@ TEST_F(NetworkChangeNotifierAndroidTest, TEST_F(NetworkChangeNotifierAndroidTest, MaxBandwidth) { SetOnline(); EXPECT_EQ(NetworkChangeNotifier::CONNECTION_UNKNOWN, - notifier_.GetConnectionType()); + notifier_->GetConnectionType()); EXPECT_EQ(std::numeric_limits<double>::infinity(), - notifier_.GetMaxBandwidth()); + notifier_->GetMaxBandwidth()); SetOffline(); EXPECT_EQ(NetworkChangeNotifier::CONNECTION_NONE, - notifier_.GetConnectionType()); - EXPECT_EQ(0.0, notifier_.GetMaxBandwidth()); + notifier_->GetConnectionType()); + EXPECT_EQ(0.0, notifier_->GetMaxBandwidth()); } TEST_F(NetworkChangeNotifierDelegateAndroidTest, @@ -240,4 +269,13 @@ TEST_F(NetworkChangeNotifierDelegateAndroidTest, EXPECT_EQ(2, delegate_observer_.bandwidth_notifications_count()); } +TEST_F(NetworkChangeNotifierAndroidTest, InitialSignal) { + DNSChangeObserver dns_change_observer; + NetworkChangeNotifier::AddDNSObserver(&dns_change_observer); + base::MessageLoop::current()->Run(); + EXPECT_EQ(1, dns_change_observer.initial_notifications_count()); + EXPECT_EQ(0, dns_change_observer.change_notifications_count()); + NetworkChangeNotifier::RemoveDNSObserver(&dns_change_observer); +} + } // namespace net |