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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
// // Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/policy/enrollment/auto_enrollment_controller.h"
#include <memory>
#include <string_view>
#include <utility>
#include "ash/constants/ash_switches.h"
#include "base/containers/fixed_flat_map.h"
#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/test/bind.h"
#include "base/test/gmock_callback_support.h"
#include "base/test/scoped_command_line.h"
#include "base/test/task_environment.h"
#include "base/test/test_future.h"
#include "chrome/browser/ash/login/oobe_configuration.h"
#include "chrome/browser/ash/policy/enrollment/auto_enrollment_state.h"
#include "chrome/browser/ash/policy/enrollment/auto_enrollment_type_checker.h"
#include "chrome/browser/ash/policy/enrollment/enrollment_state_fetcher.h"
#include "chrome/browser/ash/policy/server_backed_state/server_backed_state_keys_broker.h"
#include "chromeos/ash/components/dbus/device_management/fake_install_attributes_client.h"
#include "chromeos/ash/components/dbus/session_manager/fake_session_manager_client.h"
#include "chromeos/ash/components/install_attributes/stub_install_attributes.h"
#include "chromeos/ash/components/network/network_state.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "chromeos/ash/components/network/network_state_test_helper.h"
#include "chromeos/ash/components/system/fake_statistics_provider.h"
#include "components/policy/core/common/cloud/mock_device_management_service.h"
#include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
#include "services/network/test/test_url_loader_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
// TODO(b/294350413): Cover AutoEnrollmentController with unit tests.
namespace policy {
namespace {
constexpr auto kPortalStateToStateString =
base::MakeFixedFlatMap<ash::NetworkState::PortalState, std::string_view>(
{{ash::NetworkState::PortalState::kNoInternet,
shill::kStateNoConnectivity},
{ash::NetworkState::PortalState::kOnline, shill::kStateOnline}});
constexpr base::TimeDelta kSafeguardTimeout = base::Seconds(90);
class TestingNetwork : public ash::NetworkStateTestHelper {
public:
TestingNetwork()
: ash::NetworkStateTestHelper(
/*use_default_devices_and_services=*/false),
wifi_path_(ConfigureWiFi(shill::kStateIdle)) {}
void GoOnline() { GoState(ash::NetworkState::PortalState::kOnline); }
void GoOffline() { GoState(ash::NetworkState::PortalState::kNoInternet); }
void GoState(ash::NetworkState::PortalState state) {
DCHECK(kPortalStateToStateString.contains(state))
<< "Unknown state: " << state
<< ", add it to kPortalStateToStateString";
SetServiceProperty(wifi_path_, shill::kStateProperty,
base::Value(kPortalStateToStateString.at(state)));
}
private:
std::string wifi_path_;
};
class MockDeviceSettingsService : public ash::DeviceSettingsService {
public:
MOCK_METHOD(void,
GetOwnershipStatusAsync,
(OwnershipStatusCallback callback),
(override));
};
class MockStateKeyBroker : public ServerBackedStateKeysBroker {
public:
MockStateKeyBroker() : ServerBackedStateKeysBroker(nullptr) {}
~MockStateKeyBroker() override = default;
MOCK_METHOD(void, RequestStateKeys, (StateKeysCallback), (override));
};
// This exists only to access the protected AutoEnrollmentController
// constructor.
class AutoEnrollmentControllerForTesting : public AutoEnrollmentController {
public:
template <class... Args>
explicit AutoEnrollmentControllerForTesting(Args... args)
: AutoEnrollmentController(std::forward<Args>(args)...) {}
~AutoEnrollmentControllerForTesting() override = default;
};
template <typename DbusClient>
class ScopedFakeClientInitializer {
public:
ScopedFakeClientInitializer() { DbusClient::InitializeFake(); }
~ScopedFakeClientInitializer() { DbusClient::Shutdown(); }
};
} // namespace
class MockEnrollmentStateFetcher : public EnrollmentStateFetcher {
public:
MOCK_METHOD(void, Start, ());
base::WeakPtr<MockEnrollmentStateFetcher> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
private:
base::WeakPtrFactory<MockEnrollmentStateFetcher> weak_ptr_factory_{this};
};
class MockEnrollmentStateFetcherFactory {
public:
// Create a state fetcher before it is requested by the
// `AutoEnrollmentController`. This allows interacting with the mocked `Start`
// method.
void PreCreate() {
prepared_state_fetcher_ = std::make_unique<MockEnrollmentStateFetcher>();
weak_state_fetcher_ = prepared_state_fetcher_->GetWeakPtr();
}
// Factory method that can be used by `AutoEnrollmentController`.
// If a fetcher has been created beforehand, it will be returned. Otherwise a
// new fetcher will be created.
std::unique_ptr<EnrollmentStateFetcher> Create(
base::OnceCallback<void(AutoEnrollmentState)> report_result,
PrefService* local_state,
EnrollmentStateFetcher::RlweClientFactory rlwe_client_factory,
DeviceManagementService* device_management_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
ServerBackedStateKeysBroker* state_key_broker,
ash::DeviceSettingsService* device_settings_service,
ash::OobeConfiguration* oobe_configuration) {
report_result_ = std::move(report_result);
auto state_fetcher = prepared_state_fetcher_
? std::move(prepared_state_fetcher_)
: std::make_unique<MockEnrollmentStateFetcher>();
weak_state_fetcher_ = state_fetcher->GetWeakPtr();
return state_fetcher;
}
// Get a raw pointer to the current state fetcher.
// Note that this /could/ be NULL if the fetcher was destroyed.
MockEnrollmentStateFetcher* Get() { return weak_state_fetcher_.get(); }
// Run the `report_result_` callback provided by the `Create` method.
void ReportResult(AutoEnrollmentState state) {
std::move(report_result_).Run(state);
}
private:
std::unique_ptr<MockEnrollmentStateFetcher> prepared_state_fetcher_;
base::WeakPtr<MockEnrollmentStateFetcher> weak_state_fetcher_;
base::OnceCallback<void(AutoEnrollmentState)> report_result_;
};
class AutoEnrollmentControllerTest : public testing::Test {
protected:
AutoEnrollmentControllerForTesting CreateController() {
return AutoEnrollmentControllerForTesting(
&mock_device_settings_service_, &fake_dm_service_,
&mock_state_keys_broker_, testing_network_.network_state_handler(),
AutoEnrollmentController::RlweClientFactory(),
// The factory will stay alive until the test is destroyed.
base::BindRepeating(&MockEnrollmentStateFetcherFactory::Create,
base::Unretained(&state_fetcher_factory_)),
base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
&test_url_loader_factory_));
}
void RunAndWaitForStateUpdate(AutoEnrollmentController& controller,
base::OnceClosure callback) {
base::test::TestFuture<AutoEnrollmentState> future;
auto subscription =
controller.RegisterProgressCallback(future.GetRepeatingCallback());
std::move(callback).Run();
ASSERT_TRUE(future.Wait());
}
void SetupUnifiedStateDetermination(bool enabled) {
const std::string_view switch_value =
enabled ? AutoEnrollmentTypeChecker::kUnifiedStateDeterminationAlways
: AutoEnrollmentTypeChecker::kUnifiedStateDeterminationNever;
command_line_.GetProcessCommandLine()->AppendSwitchASCII(
ash::switches::kEnterpriseEnableUnifiedStateDetermination,
switch_value);
}
protected:
base::test::SingleThreadTaskEnvironment task_environment_{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
MockEnrollmentStateFetcherFactory state_fetcher_factory_;
TestingNetwork testing_network_;
base::test::ScopedCommandLine command_line_;
testing::NiceMock<MockDeviceSettingsService> mock_device_settings_service_;
testing::NiceMock<MockJobCreationHandler> mock_job_creation_handler_;
FakeDeviceManagementService fake_dm_service_{&mock_job_creation_handler_};
testing::NiceMock<MockStateKeyBroker> mock_state_keys_broker_;
network::TestURLLoaderFactory test_url_loader_factory_;
ScopedFakeClientInitializer<ash::FakeInstallAttributesClient>
scoped_fake_install_attributes_client_initializer_;
ScopedFakeClientInitializer<ash::FakeSessionManagerClient>
scoped_fake_session_manager_client_initializer_;
};
TEST_F(AutoEnrollmentControllerTest, NoEarlyGuestMode) {
auto controller = CreateController();
// Guest signin should not be allowed before finishing state determination.
// In particular, it should not be allowed before even starting state
// determination.
EXPECT_FALSE(controller.IsGuestSigninAllowed());
}
// Tests that the controller does not start Unified State Determination if it is
// disabled by command line switch.
TEST_F(AutoEnrollmentControllerTest, NoUsdIfDisabled) {
SetupUnifiedStateDetermination(/*enabled=*/false);
auto controller = CreateController();
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
// Start auto-enrollment check and kick-off the tasks.
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(0);
controller.Start();
}
// Tests that the controller times out with timeout error when unified state
// determination takes too long.
TEST_F(AutoEnrollmentControllerTest, ReportsSafeguardTimeout) {
SetupUnifiedStateDetermination(/*enabled=*/true);
auto controller = CreateController();
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
// Start auto-enrollment check and kick-off the tasks.
{
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(1);
controller.Start();
}
task_environment_.FastForwardBy(base::TimeDelta());
EXPECT_FALSE(controller.state().has_value());
EXPECT_TRUE(controller.SafeguardTimerForTesting().IsRunning());
RunAndWaitForStateUpdate(controller, base::BindLambdaForTesting([this]() {
task_environment_.FastForwardBy(kSafeguardTimeout);
}));
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
EXPECT_EQ(controller.state(), base::unexpected(AutoEnrollmentError(
AutoEnrollmentSafeguardTimeoutError{})));
}
// Tests that the Safeguard Timeout is stopped in case state determination
// reports an error, e.g. missing state keys.
TEST_F(AutoEnrollmentControllerTest, TimeoutInterruptedByOtherError) {
SetupUnifiedStateDetermination(/*enabled=*/true);
auto controller = CreateController();
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
// Start auto-enrollment check and kick-off the tasks.
{
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(1);
controller.Start();
}
task_environment_.FastForwardBy(base::TimeDelta());
EXPECT_FALSE(controller.state().has_value());
EXPECT_TRUE(controller.SafeguardTimerForTesting().IsRunning());
state_fetcher_factory_.ReportResult(
base::unexpected(AutoEnrollmentStateKeysRetrievalError{}));
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
EXPECT_EQ(controller.state(), base::unexpected(AutoEnrollmentError(
AutoEnrollmentStateKeysRetrievalError{})));
}
// Tests that network changes do not trigger auto-enrollment check.
TEST_F(AutoEnrollmentControllerTest, DoesNotStartWhenGoesOnline) {
SetupUnifiedStateDetermination(/*enabled=*/true);
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(0);
testing_network_.GoOffline();
auto controller = CreateController();
EXPECT_FALSE(controller.state().has_value());
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
testing_network_.GoOnline();
EXPECT_FALSE(controller.state().has_value());
EXPECT_FALSE(controller.SafeguardTimerForTesting().IsRunning());
}
// Tests that the controller retries after failure when the network goes online.
TEST_F(AutoEnrollmentControllerTest, RetriesWhenGoesOnline) {
SetupUnifiedStateDetermination(/*enabled=*/true);
testing_network_.GoOnline();
auto controller = CreateController();
// Start auto-enrollment check.
{
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(1);
controller.Start();
task_environment_.FastForwardBy(base::TimeDelta());
EXPECT_FALSE(controller.state().has_value());
EXPECT_TRUE(controller.SafeguardTimerForTesting().IsRunning());
}
// Flip-flop the network state and check that it has no effect as the state
// fetcher is still running.
{
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(0);
testing_network_.GoOffline();
testing_network_.GoOnline();
task_environment_.FastForwardBy(base::TimeDelta());
EXPECT_FALSE(controller.state().has_value());
EXPECT_TRUE(controller.SafeguardTimerForTesting().IsRunning());
}
// Stop the client with a response error so the controller can retry.
{
state_fetcher_factory_.ReportResult(
base::unexpected(AutoEnrollmentStateRetrievalResponseError{}));
EXPECT_EQ(controller.state(),
AutoEnrollmentState(base::unexpected(
AutoEnrollmentStateRetrievalResponseError{})));
}
// Flip-flop the network state and check that retry is triggered.
{
state_fetcher_factory_.PreCreate();
EXPECT_CALL(*state_fetcher_factory_.Get(), Start).Times(1);
testing_network_.GoOffline();
testing_network_.GoOnline();
}
}
} // namespace policy
|