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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/ash/components/network/cellular_inhibitor.h"
#include <memory>
#include <variant>
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/task_environment.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/dbus_thread_manager.h"
#include "chromeos/ash/components/dbus/shill/fake_shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_clients.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/network/network_device_handler_impl.h"
#include "chromeos/ash/components/network/network_handler_callbacks.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
#include "third_party/cros_system_api/dbus/shill/dbus-constants.h"
namespace ash {
namespace {
const char kDefaultCellularDevicePath[] = "stub_cellular_device";
const char kInhibitOperationResultHistogram[] =
"Network.Cellular.InhibitResult";
constexpr base::TimeDelta kScanningChangeTimeout = base::Seconds(120);
enum class GetInhibitedPropertyResult { kTrue, kFalse, kOperationFailed };
class TestObserver : public CellularInhibitor::Observer {
public:
TestObserver() = default;
~TestObserver() override = default;
size_t num_observer_events() const { return num_observer_events_; }
private:
// CellularInhibitor::Observer:
void OnInhibitStateChanged() override { ++num_observer_events_; }
size_t num_observer_events_ = 0u;
};
} // namespace
class CellularInhibitorTest : public testing::Test {
protected:
CellularInhibitorTest()
: task_environment_(
base::test::SingleThreadTaskEnvironment::MainThreadType::UI,
base::test::TaskEnvironment::TimeSource::MOCK_TIME),
helper_(/*use_default_devices_and_services=*/false) {}
~CellularInhibitorTest() override = default;
// testing::Test:
void SetUp() override {
// Disable inhibit scanning simulation since this test tests the
// intermediate state where the scanning is set to true but not set to false
// yet.
helper_.device_test()->SetSimulateInhibitScanning(false);
helper_.device_test()->ClearDevices();
cellular_inhibitor_.Init(helper_.network_state_handler(),
helper_.network_device_handler());
cellular_inhibitor_.AddObserver(&observer_);
}
void TearDown() override {
cellular_inhibitor_.RemoveObserver(&observer_);
helper_.device_test()->SetPropertyChangeDelay(std::nullopt);
}
void AddCellularDevice() {
helper_.device_test()->AddDevice(kDefaultCellularDevicePath,
shill::kTypeCellular, "cellular1");
base::RunLoop().RunUntilIdle();
}
std::unique_ptr<CellularInhibitor::InhibitLock> InhibitCellularScanningSync(
CellularInhibitor::InhibitReason inhibit_reason) {
base::RunLoop run_loop;
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock;
cellular_inhibitor_.InhibitCellularScanning(
inhibit_reason,
base::BindLambdaForTesting(
[&](std::unique_ptr<CellularInhibitor::InhibitLock> result) {
// Shill will start scanning before setting inhibit
SetScanning(/*is_scanning=*/true);
inhibit_lock = std::move(result);
run_loop.Quit();
}));
run_loop.Run();
return inhibit_lock;
}
void InhibitCellularScanning(
CellularInhibitor::InhibitReason inhibit_reason,
std::unique_ptr<CellularInhibitor::InhibitLock>& lock) {
cellular_inhibitor_.InhibitCellularScanning(
inhibit_reason,
base::BindLambdaForTesting(
[&](std::unique_ptr<CellularInhibitor::InhibitLock> result) {
// Shill will start scanning before setting inhibit
SetScanning(/*is_scanning=*/true);
lock = std::move(result);
}));
}
void SetScanning(bool is_scanning) {
helper_.network_device_handler()->SetDeviceProperty(
kDefaultCellularDevicePath, shill::kScanningProperty,
base::Value(is_scanning), base::DoNothing(), base::DoNothing());
base::RunLoop().RunUntilIdle();
}
GetInhibitedPropertyResult GetInhibitedProperty() {
properties_.reset();
helper_.network_device_handler()->GetDeviceProperties(
kDefaultCellularDevicePath,
base::BindOnce(&CellularInhibitorTest::GetPropertiesCallback,
base::Unretained(this)));
base::RunLoop().RunUntilIdle();
if (!properties_.has_value()) {
return GetInhibitedPropertyResult::kOperationFailed;
}
std::optional<bool> inhibited =
properties_->FindBool(shill::kInhibitedProperty);
EXPECT_TRUE(inhibited.has_value());
return inhibited.value() ? GetInhibitedPropertyResult::kTrue
: GetInhibitedPropertyResult::kFalse;
}
std::optional<CellularInhibitor::InhibitReason> GetInhibitReason() const {
return cellular_inhibitor_.GetInhibitReason();
}
void SetDevicePropertyChangeDelay() {
helper_.device_test()->SetPropertyChangeDelay(
CellularInhibitor::kInhibitPropertyChangeTimeout);
}
void FastForwardInhibitPropertyChangeTimeout() {
task_environment_.FastForwardBy(
CellularInhibitor::kInhibitPropertyChangeTimeout);
}
void FastForwardScanningChangeTimeout() {
task_environment_.FastForwardBy(kScanningChangeTimeout);
}
size_t GetNumObserverEvents() const {
return observer_.num_observer_events();
}
base::HistogramTester& histogram_tester() { return histogram_tester_; }
private:
void GetPropertiesCallback(const std::string& device_path,
std::optional<base::Value::Dict> properties) {
properties_ = std::move(properties);
}
base::test::SingleThreadTaskEnvironment task_environment_;
base::HistogramTester histogram_tester_;
NetworkStateTestHelper helper_;
CellularInhibitor cellular_inhibitor_;
TestObserver observer_;
std::optional<base::Value::Dict> properties_;
};
TEST_F(CellularInhibitorTest, SuccessSingleRequest) {
AddCellularDevice();
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
InhibitCellularScanningSync(
CellularInhibitor::InhibitReason::kInstallingProfile);
// Ensure that a valid lock is returned and Inhibit property is set on
// Cellular device.
EXPECT_TRUE(inhibit_lock);
EXPECT_EQ(1u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
EXPECT_EQ(GetInhibitedPropertyResult::kTrue, GetInhibitedProperty());
// Ensure that deleting lock uninhibits the Cellular device.
inhibit_lock.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(GetInhibitedPropertyResult::kFalse, GetInhibitedProperty());
// At this point, we are still in the inhibited state, since scanning has not
// yet started and stopped.
EXPECT_EQ(1u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
// Just stop scanning; this should alert observers that the inhibit
// operation has completed.
SetScanning(/*is_scanning=*/false);
EXPECT_EQ(2u, GetNumObserverEvents());
EXPECT_FALSE(GetInhibitReason().has_value());
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kSuccess,
/*expected_count=*/1);
}
TEST_F(CellularInhibitorTest, SuccessMultipleRequests) {
AddCellularDevice();
// Make two inhibit requests in parallel.
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock;
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock2;
InhibitCellularScanning(CellularInhibitor::InhibitReason::kInstallingProfile,
inhibit_lock);
InhibitCellularScanning(CellularInhibitor::InhibitReason::kRemovingProfile,
inhibit_lock2);
base::RunLoop().RunUntilIdle();
// Ensure that the only one inhibit lock has been granted.
EXPECT_TRUE(inhibit_lock);
EXPECT_FALSE(inhibit_lock2);
EXPECT_EQ(1u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
EXPECT_EQ(GetInhibitedPropertyResult::kTrue, GetInhibitedProperty());
// Release the first lock; though this causes the Inhibited property to false,
// it should not yet start the next (queued) Inhibit flow, since the scanning
// flow has not yet started.
inhibit_lock.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(1u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
EXPECT_EQ(GetInhibitedPropertyResult::kFalse, GetInhibitedProperty());
EXPECT_FALSE(inhibit_lock2);
// Change scanning back to false, which should trigger the second lock being
// set.
SetScanning(/*is_scanning=*/false);
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kSuccess,
/*expected_count=*/1);
EXPECT_TRUE(inhibit_lock2);
EXPECT_EQ(3u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kRemovingProfile,
GetInhibitReason());
EXPECT_EQ(GetInhibitedPropertyResult::kTrue, GetInhibitedProperty());
// Ensure that inhibited property is set to false when all locks are deleted.
inhibit_lock2.reset();
base::RunLoop().RunUntilIdle();
EXPECT_EQ(3u, GetNumObserverEvents());
EXPECT_EQ(CellularInhibitor::InhibitReason::kRemovingProfile,
GetInhibitReason());
EXPECT_EQ(GetInhibitedPropertyResult::kFalse, GetInhibitedProperty());
SetScanning(/*is_scanning=*/false);
base::RunLoop().RunUntilIdle();
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kSuccess,
/*expected_count=*/2);
}
TEST_F(CellularInhibitorTest, Failure) {
// Do not add a Cellular device. This should cause commands below to fail,
// since the device cannot be inhibited if it does not exist.
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
InhibitCellularScanningSync(
CellularInhibitor::InhibitReason::kInstallingProfile);
EXPECT_EQ(GetInhibitedPropertyResult::kOperationFailed,
GetInhibitedProperty());
EXPECT_FALSE(inhibit_lock);
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kSetInhibitNoDevice,
/*expected_count=*/1);
}
TEST_F(CellularInhibitorTest, FailurePropertySetTimeout) {
AddCellularDevice();
SetDevicePropertyChangeDelay();
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock;
InhibitCellularScanning(CellularInhibitor::InhibitReason::kInstallingProfile,
inhibit_lock);
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(inhibit_lock);
FastForwardInhibitPropertyChangeTimeout();
base::RunLoop().RunUntilIdle();
EXPECT_FALSE(inhibit_lock);
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kSetInhibitTimeout,
/*expected_count=*/1);
}
TEST_F(CellularInhibitorTest, FailureScanningChangeTimeout) {
AddCellularDevice();
std::unique_ptr<CellularInhibitor::InhibitLock> inhibit_lock =
InhibitCellularScanningSync(
CellularInhibitor::InhibitReason::kInstallingProfile);
// Ensure that a valid lock is returned and inhibit reason is returned.
EXPECT_TRUE(inhibit_lock);
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
// Delete lock to start uninhibiting and run till uninhibit is waiting for
// scanning state change.
inhibit_lock.reset();
base::RunLoop().RunUntilIdle();
// Verify that no inhibit reason is returned after timeout.
EXPECT_EQ(CellularInhibitor::InhibitReason::kInstallingProfile,
GetInhibitReason());
FastForwardScanningChangeTimeout();
EXPECT_FALSE(GetInhibitReason().has_value());
histogram_tester().ExpectBucketCount(
kInhibitOperationResultHistogram,
CellularInhibitor::InhibitOperationResult::kUninhibitTimeout,
/*expected_count=*/1);
}
} // namespace ash
|