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 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
|
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <utility>
#include "base/memory/weak_ptr.h"
#include "base/test/bind.h"
#include "base/values.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics.h"
#include "chrome/browser/ash/net/network_diagnostics/network_diagnostics_test_helper.h"
#include "chromeos/ash/components/dbus/debug_daemon/fake_debug_daemon_client.h"
#include "chromeos/services/network_health/public/mojom/network_diagnostics.mojom.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace ash {
namespace network_diagnostics {
namespace {
namespace mojom = ::chromeos::network_diagnostics::mojom;
// The IP v4 config path specified here must match the IP v4 config path
// specified in NetworkStateTestHelper::ResetDevicesAndServices(), which itself
// is based on the IP v4 config path used to set up IP v4 configs in
// FakeShillManagerClient::SetupDefaultEnvironment().
const char kIPv4ConfigPath[] = "ipconfig_v4_path";
const std::vector<std::string> kWellFormedDnsServers = {
"192.168.1.100", "192.168.1.101", "192.168.1.102"};
// This fakes a DebugDaemonClient by serving fake ICMP results when the
// DebugDaemonClient calls TestICMP().
class TestDebugDaemonClient : public FakeDebugDaemonClient {
public:
TestDebugDaemonClient() = default;
TestDebugDaemonClient(const TestDebugDaemonClient&) = delete;
TestDebugDaemonClient& operator=(const TestDebugDaemonClient&) = delete;
~TestDebugDaemonClient() override = default;
void TestICMP(const std::string& ip_address,
TestICMPCallback callback) override {
// Invoke the test callback with fake output.
std::move(callback).Run(std::optional<std::string>{icmp_output_});
}
void set_icmp_output(const std::string& icmp_output) {
icmp_output_ = icmp_output;
}
private:
std::string icmp_output_;
};
// Fake ICMP output. For more details, see:
// https://gerrit.chromium.org/gerrit/#/c/30310/2/src/helpers/icmp.cc.
const char kFakeValidICMPOutput[] = R"(
{ "4.3.2.1":
{ "sent": 4,
"recvd": 4,
"time": 3005,
"min": 5.789000,
"avg": 5.913000,
"max": 6.227000,
"dev": 0.197000 }
})";
} // namespace
class NetworkDiagnosticsTest : public NetworkDiagnosticsTestHelper {
public:
NetworkDiagnosticsTest() {
// Set TestDebugDaemonClient
test_debug_daemon_client_ = std::make_unique<TestDebugDaemonClient>();
network_diagnostics_ =
std::make_unique<NetworkDiagnostics>(test_debug_daemon_client_.get());
network_diagnostics_->BindReceiver(
network_diagnostics_remote_.BindNewPipeAndPassReceiver());
// Set up properties for the WiFi service.
SetUpWiFi(shill::kStateOnline);
SetServiceProperty(wifi_path(), shill::kSecurityClassProperty,
base::Value(shill::kSecurityClassPsk));
base::RunLoop().RunUntilIdle();
}
~NetworkDiagnosticsTest() override = default;
// Set up the name servers and change the IPConfigs for the WiFi device and
// service by overwriting the initial IPConfigs that are set up in
// FakeShillManagerClient::SetupDefaultEnvironment(). Attach name
// servers to the IP config.
void SetUpNameServers(const std::vector<std::string>& name_servers) {
DCHECK(!wifi_path().empty());
// Set up the name servers
base::Value::List dns_servers;
for (const std::string& name_server : name_servers) {
dns_servers.Append(name_server);
}
// Set up the IP v4 config
auto ip_config_v4_properties = base::Value::Dict().Set(
shill::kNameServersProperty, std::move(dns_servers));
helper()->ip_config_test()->AddIPConfig(kIPv4ConfigPath,
ip_config_v4_properties.Clone());
std::string wifi_device_path =
helper()->device_test()->GetDevicePathForType(shill::kTypeWifi);
helper()->device_test()->SetDeviceProperty(
wifi_device_path, shill::kIPConfigsProperty,
base::Value(std::move(ip_config_v4_properties)),
/*notify_changed=*/true);
SetServiceProperty(wifi_path(), shill::kIPConfigProperty,
base::Value(kIPv4ConfigPath));
}
protected:
base::WeakPtr<NetworkDiagnosticsTest> weak_ptr() {
return weak_factory_.GetWeakPtr();
}
NetworkDiagnostics* network_diagnostics() {
return network_diagnostics_.get();
}
TestDebugDaemonClient* test_debug_daemon_client() {
return test_debug_daemon_client_.get();
}
private:
std::unique_ptr<TestDebugDaemonClient> test_debug_daemon_client_;
mojo::Remote<mojom::NetworkDiagnosticsRoutines> network_diagnostics_remote_;
std::unique_ptr<NetworkDiagnostics> network_diagnostics_;
base::WeakPtrFactory<NetworkDiagnosticsTest> weak_factory_{this};
};
// Test whether NetworkDiagnostics can successfully invoke the
// LanConnectivity routine.
TEST_F(NetworkDiagnosticsTest, RunLanConnectivityReachability) {
base::RunLoop run_loop;
mojom::RoutineResultPtr result;
network_diagnostics()->RunLanConnectivity(
mojom::RoutineCallSource::kDiagnosticsUI,
base::BindLambdaForTesting([&](mojom::RoutineResultPtr response) {
result = std::move(response);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(result->verdict, mojom::RoutineVerdict::kNoProblem);
std::vector<mojom::LanConnectivityProblem> no_problems;
EXPECT_EQ(result->problems->get_lan_connectivity_problems(), no_problems);
EXPECT_EQ(result->source, mojom::RoutineCallSource::kDiagnosticsUI);
}
// Test whether NetworkDiagnostics can successfully invoke the
// SignalStrength routine.
TEST_F(NetworkDiagnosticsTest, RunSignalStrengthReachability) {
base::RunLoop run_loop;
mojom::RoutineResultPtr result;
network_diagnostics()->RunSignalStrength(
mojom::RoutineCallSource::kDiagnosticsUI,
base::BindLambdaForTesting([&](mojom::RoutineResultPtr response) {
result = std::move(response);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(result->verdict, mojom::RoutineVerdict::kNoProblem);
std::vector<mojom::SignalStrengthProblem> no_problems;
EXPECT_EQ(result->problems->get_signal_strength_problems(), no_problems);
EXPECT_EQ(result->source, mojom::RoutineCallSource::kDiagnosticsUI);
}
// Test whether NetworkDiagnostics can successfully invoke the
// GatewayCanBePinged routine.
TEST_F(NetworkDiagnosticsTest, RunGatewayCanBePingedReachability) {
test_debug_daemon_client()->set_icmp_output(kFakeValidICMPOutput);
base::RunLoop run_loop;
mojom::RoutineResultPtr result;
network_diagnostics()->RunGatewayCanBePinged(
mojom::RoutineCallSource::kDiagnosticsUI,
base::BindLambdaForTesting([&](mojom::RoutineResultPtr response) {
result = std::move(response);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(result->verdict, mojom::RoutineVerdict::kNoProblem);
std::vector<mojom::GatewayCanBePingedProblem> no_problems;
EXPECT_EQ(result->problems->get_gateway_can_be_pinged_problems(),
no_problems);
EXPECT_EQ(result->source, mojom::RoutineCallSource::kDiagnosticsUI);
}
// Test whether NetworkDiagnostics can successfully invoke the
// HasSecureWiFiConnection routine.
TEST_F(NetworkDiagnosticsTest, RunHasSecureWiFiConnectionReachability) {
base::RunLoop run_loop;
mojom::RoutineResultPtr result;
network_diagnostics()->RunHasSecureWiFiConnection(
mojom::RoutineCallSource::kDiagnosticsUI,
base::BindLambdaForTesting([&](mojom::RoutineResultPtr response) {
result = std::move(response);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(result->verdict, mojom::RoutineVerdict::kNoProblem);
std::vector<mojom::HasSecureWiFiConnectionProblem> no_problems;
EXPECT_EQ(result->problems->get_has_secure_wifi_connection_problems(),
no_problems);
EXPECT_EQ(result->source, mojom::RoutineCallSource::kDiagnosticsUI);
}
// Test whether NetworkDiagnostics can successfully invoke the
// DnsResolverPresent routine.
TEST_F(NetworkDiagnosticsTest, RunDnsResolverPresentReachability) {
// Attach nameservers to the IPConfigs.
SetUpNameServers(kWellFormedDnsServers);
base::RunLoop run_loop;
mojom::RoutineResultPtr result;
network_diagnostics()->RunDnsResolverPresent(
mojom::RoutineCallSource::kDiagnosticsUI,
base::BindLambdaForTesting([&](mojom::RoutineResultPtr response) {
result = std::move(response);
run_loop.Quit();
}));
run_loop.Run();
EXPECT_EQ(result->verdict, mojom::RoutineVerdict::kNoProblem);
std::vector<mojom::DnsResolverPresentProblem> no_problems;
EXPECT_EQ(result->problems->get_dns_resolver_present_problems(), no_problems);
}
// TODO(khegde): Test whether NetworkDiagnostics can successfully invoke the
// DnsLatency routine. This would require a way to fake and inject the following
// into the DnsLatency routine: base::TickClock, network::mojom::HostResolver,
// and network::TestNetworkContext.
// TEST_F(NetworkDiagnosticsTest, DnsLatencyReachability) {}
// TODO(khegde): Test whether NetworkDiagnostics can successfully invoke the
// DnsResolution routine. This would require a way to fake and inject the
// following into the DnsResolution routine: network::mojom::HostResolver and
// network::TestNetworkContext.
// TEST_F(NetworkDiagnosticsTest, DnsResolutionReachability) {}
} // namespace network_diagnostics
} // namespace ash
|