1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/check_op.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/sequence_checker.h"
#include "base/test/bind.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/network_service_instance.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/network_change_notifier.h"
#include "services/network/public/cpp/features.h"
#include "services/network/public/cpp/network_connection_tracker.h"
#include "services/network/public/mojom/network_service.mojom.h"
#include "services/network/public/mojom/network_service_test.mojom.h"
namespace {
class TestNetworkConnectionObserver
: public network::NetworkConnectionTracker::NetworkConnectionObserver {
public:
explicit TestNetworkConnectionObserver(
network::NetworkConnectionTracker* tracker)
: num_notifications_(0),
tracker_(tracker),
run_loop_(std::make_unique<base::RunLoop>()),
connection_type_(network::mojom::ConnectionType::CONNECTION_UNKNOWN) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
tracker_->AddNetworkConnectionObserver(this);
}
TestNetworkConnectionObserver(const TestNetworkConnectionObserver&) = delete;
TestNetworkConnectionObserver& operator=(
const TestNetworkConnectionObserver&) = delete;
~TestNetworkConnectionObserver() override {
tracker_->RemoveNetworkConnectionObserver(this);
}
// NetworkConnectionObserver implementation:
void OnConnectionChanged(network::mojom::ConnectionType type) override {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
network::mojom::ConnectionType queried_type;
bool sync = tracker_->GetConnectionType(
&queried_type,
base::BindOnce([](network::mojom::ConnectionType type) {}));
EXPECT_TRUE(sync);
EXPECT_EQ(type, queried_type);
num_notifications_++;
connection_type_ = type;
run_loop_->Quit();
}
void WaitForNotification() {
run_loop_->Run();
run_loop_ = std::make_unique<base::RunLoop>();
}
size_t num_notifications() const { return num_notifications_; }
network::mojom::ConnectionType connection_type() const {
return connection_type_;
}
private:
size_t num_notifications_;
raw_ptr<network::NetworkConnectionTracker> tracker_;
std::unique_ptr<base::RunLoop> run_loop_;
network::mojom::ConnectionType connection_type_;
SEQUENCE_CHECKER(sequence_checker_);
};
} // namespace
class NetworkConnectionTrackerBrowserTest : public InProcessBrowserTest {
public:
NetworkConnectionTrackerBrowserTest() = default;
~NetworkConnectionTrackerBrowserTest() override = default;
// Simulates a network connection change.
void SimulateNetworkChange(network::mojom::ConnectionType type) {
if (!content::IsInProcessNetworkService()) {
mojo::Remote<network::mojom::NetworkServiceTest> network_service_test;
content::GetNetworkService()->BindTestInterfaceForTesting(
network_service_test.BindNewPipeAndPassReceiver());
base::RunLoop run_loop;
network_service_test->SimulateNetworkChange(
type,
base::BindOnce([](base::RunLoop* run_loop) { run_loop->Quit(); },
base::Unretained(&run_loop)));
run_loop.Run();
return;
}
net::NetworkChangeNotifier::NotifyObserversOfNetworkChangeForTests(
net::NetworkChangeNotifier::ConnectionType(type));
}
private:
};
// Basic test to make sure NetworkConnectionTracker is set up.
IN_PROC_BROWSER_TEST_F(NetworkConnectionTrackerBrowserTest,
NetworkConnectionTracker) {
// NetworkService on ChromeOS doesn't yet have a NetworkChangeManager
// implementation. OSX uses a separate binary for service processes and
// browser test fixture doesn't have NetworkServiceTest mojo code.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_MAC)
network::NetworkConnectionTracker* tracker =
content::GetNetworkConnectionTracker();
EXPECT_NE(nullptr, tracker);
// Issue a GetConnectionType() request to make sure NetworkService has been
// started up. This way, NetworkService will receive the broadcast when
// SimulateNetworkChange() is called.
base::RunLoop run_loop;
network::mojom::ConnectionType ignored_type;
bool sync = tracker->GetConnectionType(
&ignored_type,
base::BindOnce(
[](base::RunLoop* run_loop, network::mojom::ConnectionType type) {
run_loop->Quit();
},
base::Unretained(&run_loop)));
if (!sync)
run_loop.Run();
TestNetworkConnectionObserver network_connection_observer(tracker);
SimulateNetworkChange(network::mojom::ConnectionType::CONNECTION_3G);
network_connection_observer.WaitForNotification();
EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_3G,
network_connection_observer.connection_type());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, network_connection_observer.num_notifications());
#endif
}
// Simulates a network service crash, and ensures that network change manager
// binds to the restarted network service.
IN_PROC_BROWSER_TEST_F(NetworkConnectionTrackerBrowserTest,
SimulateNetworkServiceCrash) {
// NetworkService on ChromeOS doesn't yet have a NetworkChangeManager
// implementation. OSX uses a separate binary for service processes and
// browser test fixture doesn't have NetworkServiceTest mojo code.
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_MAC)
// Out-of-process network service is not enabled, so network service's crash
// and restart aren't applicable.
if (!content::IsOutOfProcessNetworkService())
return;
network::NetworkConnectionTracker* tracker =
content::GetNetworkConnectionTracker();
EXPECT_NE(nullptr, tracker);
// Issue a GetConnectionType() request to make sure NetworkService has been
// started up. This way, NetworkService will receive the broadcast when
// SimulateNetworkChange() is called.
{
base::RunLoop run_loop;
network::mojom::ConnectionType ignored_type;
bool sync = tracker->GetConnectionType(
&ignored_type,
base::BindOnce(
[](base::RunLoop* run_loop, network::mojom::ConnectionType type) {
run_loop->Quit();
},
base::Unretained(&run_loop)));
if (!sync)
run_loop.Run();
}
TestNetworkConnectionObserver network_connection_observer(tracker);
SimulateNetworkChange(network::mojom::ConnectionType::CONNECTION_3G);
network_connection_observer.WaitForNotification();
EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_3G,
network_connection_observer.connection_type());
// Wait a bit longer to make sure only 1 notification is received and that
// there is no duplicate notification.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, network_connection_observer.num_notifications());
SimulateNetworkServiceCrash();
// Issue a GetConnectionType() request to make sure NetworkService has been
// started up. This way, NetworkService will receive the broadcast when
// SimulateNetworkChange() is called.
{
base::RunLoop run_loop;
network::mojom::ConnectionType ignored_type;
bool sync = tracker->GetConnectionType(
&ignored_type,
base::BindOnce(
[](base::RunLoop* run_loop, network::mojom::ConnectionType type) {
run_loop->Quit();
},
base::Unretained(&run_loop)));
if (!sync)
run_loop.Run();
}
SimulateNetworkChange(network::mojom::ConnectionType::CONNECTION_2G);
network_connection_observer.WaitForNotification();
EXPECT_EQ(network::mojom::ConnectionType::CONNECTION_2G,
network_connection_observer.connection_type());
// Wait a bit longer to make sure only 2 notifications are received.
base::RunLoop().RunUntilIdle();
EXPECT_EQ(2u, network_connection_observer.num_notifications());
#endif
}
|