Commit a52f490d authored by Denis Kuznetsov's avatar Denis Kuznetsov Committed by Commit Bot

cros: Extract creation/configuring of MockEnterpriseEnrollmentHelper as mixin.

Also changed a way the MockEnterpiseEnrollmentHelper is created/configured.

Bug: 912704
Change-Id: I66756c147c8e25dcf9655da92a89d7c6b124b74e
Reviewed-on: https://chromium-review.googlesource.com/c/1477235
Commit-Queue: Denis Kuznetsov <antrim@chromium.org>
Reviewed-by: default avatarJacob Dufault <jdufault@chromium.org>
Reviewed-by: default avatarAga Wronska <agawronska@chromium.org>
Reviewed-by: default avatarRoman Sorokin [CET] <rsorokin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#634396}
parent 3d628fb8
...@@ -24,6 +24,7 @@ ...@@ -24,6 +24,7 @@
#include "chrome/browser/chromeos/login/screens/network_screen.h" #include "chrome/browser/chromeos/login/screens/network_screen.h"
#include "chrome/browser/chromeos/login/screens/screen_exit_code.h" #include "chrome/browser/chromeos/login/screens/screen_exit_code.h"
#include "chrome/browser/chromeos/login/startup_utils.h" #include "chrome/browser/chromeos/login/startup_utils.h"
#include "chrome/browser/chromeos/login/test/enrollment_helper_mixin.h"
#include "chrome/browser/chromeos/login/test/js_checker.h" #include "chrome/browser/chromeos/login/test/js_checker.h"
#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h" #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
#include "chrome/browser/chromeos/login/test/test_condition_waiter.h" #include "chrome/browser/chromeos/login/test/test_condition_waiter.h"
...@@ -47,8 +48,6 @@ ...@@ -47,8 +48,6 @@
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
using chromeos::test::DemoModeSetupResult; using chromeos::test::DemoModeSetupResult;
using chromeos::test::MockDemoModeOfflineEnrollmentHelperCreator;
using chromeos::test::MockDemoModeOnlineEnrollmentHelperCreator;
using chromeos::test::SetupDummyOfflinePolicyDir; using chromeos::test::SetupDummyOfflinePolicyDir;
namespace chromeos { namespace chromeos {
...@@ -327,9 +326,11 @@ class DemoSetupTest : public LoginManagerTest { ...@@ -327,9 +326,11 @@ class DemoSetupTest : public LoginManagerTest {
void SkipToErrorDialog() { void SkipToErrorDialog() {
// Simulate online setup error. // Simulate online setup error.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockDemoModeOnlineEnrollmentHelperCreator< policy::EnrollmentConfig::MODE_ATTESTATION);
DemoModeSetupResult::ERROR_DEFAULT>); enrollment_helper_.ExpectAttestationEnrollmentError(
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::DM_STATUS_TEMPORARY_UNAVAILABLE));
// Enrollment type is set in the part of the flow that is skipped, That is // Enrollment type is set in the part of the flow that is skipped, That is
// why we need to set it here. // why we need to set it here.
...@@ -431,6 +432,9 @@ class DemoSetupTest : public LoginManagerTest { ...@@ -431,6 +432,9 @@ class DemoSetupTest : public LoginManagerTest {
test::ExecuteOobeJS(query); test::ExecuteOobeJS(query);
} }
protected:
test::EnrollmentHelperMixin enrollment_helper_{&mixin_host_};
private: private:
void DisableConfirmationDialogAnimations() { void DisableConfirmationDialogAnimations() {
test::ExecuteOobeJS( test::ExecuteOobeJS(
...@@ -504,8 +508,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, DoNotInvokeWithNonConsecutiveTaps) { ...@@ -504,8 +508,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, DoNotInvokeWithNonConsecutiveTaps) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowSuccess) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowSuccess) {
// Simulate successful online setup. // Simulate successful online setup.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>); policy::EnrollmentConfig::MODE_ATTESTATION);
enrollment_helper_.ExpectAttestationEnrollmentSuccess();
SimulateNetworkConnected(); SimulateNetworkConnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -554,9 +559,11 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowSuccess) { ...@@ -554,9 +559,11 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowSuccess) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorDefault) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorDefault) {
// Simulate online setup failure. // Simulate online setup failure.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockDemoModeOnlineEnrollmentHelperCreator< policy::EnrollmentConfig::MODE_ATTESTATION);
DemoModeSetupResult::ERROR_DEFAULT>); enrollment_helper_.ExpectAttestationEnrollmentError(
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::DM_STATUS_TEMPORARY_UNAVAILABLE));
SimulateNetworkConnected(); SimulateNetworkConnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -618,9 +625,11 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorDefault) { ...@@ -618,9 +625,11 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorDefault) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorPowerwashRequired) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorPowerwashRequired) {
// Simulate online setup failure that requires powerwash. // Simulate online setup failure that requires powerwash.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockDemoModeOnlineEnrollmentHelperCreator< policy::EnrollmentConfig::MODE_ATTESTATION);
DemoModeSetupResult::ERROR_POWERWASH_REQUIRED>); enrollment_helper_.ExpectAttestationEnrollmentError(
policy::EnrollmentStatus::ForLockError(
chromeos::InstallAttributes::LOCK_ALREADY_LOCKED));
SimulateNetworkConnected(); SimulateNetworkConnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -681,8 +690,8 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorPowerwashRequired) { ...@@ -681,8 +690,8 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowErrorPowerwashRequired) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowCrosComponentFailure) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OnlineSetupFlowCrosComponentFailure) {
// Simulate failure to load demo resources CrOS component. // Simulate failure to load demo resources CrOS component.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( // There is no enrollment attempt, as process fails earlier.
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>); enrollment_helper_.ExpectNoEnrollment();
SimulateNetworkConnected(); SimulateNetworkConnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -762,9 +771,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineDemoModeUnavailable) { ...@@ -762,9 +771,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineDemoModeUnavailable) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowSuccess) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowSuccess) {
// Simulate offline setup success. // Simulate offline setup success.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentSuccess();
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -816,9 +823,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowSuccess) { ...@@ -816,9 +823,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowSuccess) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorDefault) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorDefault) {
// Simulate offline setup failure. // Simulate offline setup failure.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentError(
&MockDemoModeOfflineEnrollmentHelperCreator< policy::EnrollmentStatus::ForStatus(
DemoModeSetupResult::ERROR_DEFAULT>); policy::EnrollmentStatus::OFFLINE_POLICY_DECODING_FAILED));
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -883,9 +890,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorDefault) { ...@@ -883,9 +890,9 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorDefault) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorPowerwashRequired) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, OfflineSetupFlowErrorPowerwashRequired) {
// Simulate offline setup failure. // Simulate offline setup failure.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentError(
&MockDemoModeOfflineEnrollmentHelperCreator< policy::EnrollmentStatus::ForLockError(
DemoModeSetupResult::ERROR_POWERWASH_REQUIRED>); chromeos::InstallAttributes::LOCK_READBACK_ERROR));
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -1023,9 +1030,12 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, BackOnErrorScreen) { ...@@ -1023,9 +1030,12 @@ IN_PROC_BROWSER_TEST_F(DemoSetupTest, BackOnErrorScreen) {
IN_PROC_BROWSER_TEST_F(DemoSetupTest, RetryOnErrorScreen) { IN_PROC_BROWSER_TEST_F(DemoSetupTest, RetryOnErrorScreen) {
SkipToErrorDialog(); SkipToErrorDialog();
// We need to create another mock after showing error dialog.
enrollment_helper_.ResetMock();
// Simulate successful online setup on retry. // Simulate successful online setup on retry.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>); policy::EnrollmentConfig::MODE_ATTESTATION);
enrollment_helper_.ExpectAttestationEnrollmentSuccess();
ClickScreenDialogButtonWithSelector(OobeScreen::SCREEN_OOBE_DEMO_SETUP, ClickScreenDialogButtonWithSelector(OobeScreen::SCREEN_OOBE_DEMO_SETUP,
DemoSetupDialog::kError, "#retryButton", DemoSetupDialog::kError, "#retryButton",
JSExecution::kAsync); JSExecution::kAsync);
...@@ -1110,9 +1120,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, DeviceFromFactory) { ...@@ -1110,9 +1120,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, DeviceFromFactory) {
// "block_devmode" flags do not exist in VPD. // "block_devmode" flags do not exist in VPD.
// Simulate offline setup success. // Simulate offline setup success.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentSuccess();
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -1147,9 +1155,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, NonEnterpriseDevice) { ...@@ -1147,9 +1155,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, NonEnterpriseDevice) {
statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "0"); statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "0");
// Simulate offline setup success. // Simulate offline setup success.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentSuccess();
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -1185,9 +1191,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, LegacyDemoModeDevice) { ...@@ -1185,9 +1191,7 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, LegacyDemoModeDevice) {
statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "0"); statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "0");
// Simulate offline setup success. // Simulate offline setup success.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectOfflineEnrollmentSuccess();
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
...@@ -1220,10 +1224,8 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, DeviceWithFRE) { ...@@ -1220,10 +1224,8 @@ IN_PROC_BROWSER_TEST_F(DemoSetupFRETest, DeviceWithFRE) {
statistics_provider_.SetMachineStatistic(system::kCheckEnrollmentKey, "1"); statistics_provider_.SetMachineStatistic(system::kCheckEnrollmentKey, "1");
statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "1"); statistics_provider_.SetMachineStatistic(system::kBlockDevModeKey, "1");
// Simulate offline setup success. // Expect no enrollment to take place due to error.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectNoEnrollment();
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
SimulateNetworkDisconnected(); SimulateNetworkDisconnected();
InvokeDemoModeWithAccelerator(); InvokeDemoModeWithAccelerator();
......
...@@ -32,9 +32,10 @@ ...@@ -32,9 +32,10 @@
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
using chromeos::test::DemoModeSetupResult; using chromeos::test::DemoModeSetupResult;
using chromeos::test::MockDemoModeOfflineEnrollmentHelperCreator;
using chromeos::test::MockDemoModeOnlineEnrollmentHelperCreator;
using chromeos::test::SetupDummyOfflinePolicyDir; using chromeos::test::SetupDummyOfflinePolicyDir;
using chromeos::test::SetupMockDemoModeNoEnrollmentHelper;
using chromeos::test::SetupMockDemoModeOfflineEnrollmentHelper;
using chromeos::test::SetupMockDemoModeOnlineEnrollmentHelper;
using testing::_; using testing::_;
namespace chromeos { namespace chromeos {
...@@ -110,6 +111,7 @@ class DemoSetupControllerTest : public testing::Test { ...@@ -110,6 +111,7 @@ class DemoSetupControllerTest : public testing::Test {
} }
void TearDown() override { void TearDown() override {
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(nullptr);
DBusThreadManager::Shutdown(); DBusThreadManager::Shutdown();
SystemSaltGetter::Shutdown(); SystemSaltGetter::Shutdown();
DeviceSettingsService::Shutdown(); DeviceSettingsService::Shutdown();
...@@ -138,10 +140,7 @@ class DemoSetupControllerTest : public testing::Test { ...@@ -138,10 +140,7 @@ class DemoSetupControllerTest : public testing::Test {
TEST_F(DemoSetupControllerTest, OfflineSuccess) { TEST_F(DemoSetupControllerTest, OfflineSuccess) {
base::ScopedTempDir temp_dir; base::ScopedTempDir temp_dir;
ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir)); ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir));
SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult::SUCCESS);
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock(
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
policy::MockCloudPolicyStore mock_store; policy::MockCloudPolicyStore mock_store;
EXPECT_CALL(mock_store, Store(_)) EXPECT_CALL(mock_store, Store(_))
.WillOnce(testing::InvokeWithoutArgs( .WillOnce(testing::InvokeWithoutArgs(
...@@ -165,10 +164,8 @@ TEST_F(DemoSetupControllerTest, OfflineSuccess) { ...@@ -165,10 +164,8 @@ TEST_F(DemoSetupControllerTest, OfflineSuccess) {
TEST_F(DemoSetupControllerTest, OfflineDeviceLocalAccountPolicyStoreFailed) { TEST_F(DemoSetupControllerTest, OfflineDeviceLocalAccountPolicyStoreFailed) {
base::ScopedTempDir temp_dir; base::ScopedTempDir temp_dir;
ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir)); ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir));
SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult::SUCCESS);
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock(
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
policy::MockCloudPolicyStore mock_store; policy::MockCloudPolicyStore mock_store;
EXPECT_CALL(mock_store, Store(_)) EXPECT_CALL(mock_store, Store(_))
.WillOnce(testing::InvokeWithoutArgs( .WillOnce(testing::InvokeWithoutArgs(
...@@ -193,10 +190,7 @@ TEST_F(DemoSetupControllerTest, OfflineDeviceLocalAccountPolicyStoreFailed) { ...@@ -193,10 +190,7 @@ TEST_F(DemoSetupControllerTest, OfflineDeviceLocalAccountPolicyStoreFailed) {
TEST_F(DemoSetupControllerTest, OfflineInvalidDeviceLocalAccountPolicyBlob) { TEST_F(DemoSetupControllerTest, OfflineInvalidDeviceLocalAccountPolicyBlob) {
base::ScopedTempDir temp_dir; base::ScopedTempDir temp_dir;
ASSERT_TRUE(SetupDummyOfflinePolicyDir("", &temp_dir)); ASSERT_TRUE(SetupDummyOfflinePolicyDir("", &temp_dir));
SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult::SUCCESS);
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock(
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::SUCCESS>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOffline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOffline);
tested_controller_->SetPreinstalledOfflineResourcesPathForTesting( tested_controller_->SetPreinstalledOfflineResourcesPathForTesting(
...@@ -217,9 +211,7 @@ TEST_F(DemoSetupControllerTest, OfflineErrorDefault) { ...@@ -217,9 +211,7 @@ TEST_F(DemoSetupControllerTest, OfflineErrorDefault) {
base::ScopedTempDir temp_dir; base::ScopedTempDir temp_dir;
ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir)); ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir));
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult::ERROR_DEFAULT);
&MockDemoModeOfflineEnrollmentHelperCreator<
DemoModeSetupResult::ERROR_DEFAULT>);
policy::MockCloudPolicyStore mock_store; policy::MockCloudPolicyStore mock_store;
EXPECT_CALL(mock_store, Store(_)).Times(0); EXPECT_CALL(mock_store, Store(_)).Times(0);
...@@ -244,9 +236,8 @@ TEST_F(DemoSetupControllerTest, OfflineErrorPowerwashRequired) { ...@@ -244,9 +236,8 @@ TEST_F(DemoSetupControllerTest, OfflineErrorPowerwashRequired) {
base::ScopedTempDir temp_dir; base::ScopedTempDir temp_dir;
ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir)); ASSERT_TRUE(SetupDummyOfflinePolicyDir("test", &temp_dir));
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOfflineEnrollmentHelper(
&MockDemoModeOfflineEnrollmentHelperCreator< DemoModeSetupResult::ERROR_POWERWASH_REQUIRED);
DemoModeSetupResult::ERROR_POWERWASH_REQUIRED>);
policy::MockCloudPolicyStore mock_store; policy::MockCloudPolicyStore mock_store;
EXPECT_CALL(mock_store, Store(_)).Times(0); EXPECT_CALL(mock_store, Store(_)).Times(0);
...@@ -268,8 +259,7 @@ TEST_F(DemoSetupControllerTest, OfflineErrorPowerwashRequired) { ...@@ -268,8 +259,7 @@ TEST_F(DemoSetupControllerTest, OfflineErrorPowerwashRequired) {
} }
TEST_F(DemoSetupControllerTest, OnlineSuccess) { TEST_F(DemoSetupControllerTest, OnlineSuccess) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult::SUCCESS);
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->Enroll( tested_controller_->Enroll(
...@@ -283,9 +273,7 @@ TEST_F(DemoSetupControllerTest, OnlineSuccess) { ...@@ -283,9 +273,7 @@ TEST_F(DemoSetupControllerTest, OnlineSuccess) {
} }
TEST_F(DemoSetupControllerTest, OnlineErrorDefault) { TEST_F(DemoSetupControllerTest, OnlineErrorDefault) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult::ERROR_DEFAULT);
&MockDemoModeOnlineEnrollmentHelperCreator<
DemoModeSetupResult::ERROR_DEFAULT>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->Enroll( tested_controller_->Enroll(
...@@ -300,9 +288,8 @@ TEST_F(DemoSetupControllerTest, OnlineErrorDefault) { ...@@ -300,9 +288,8 @@ TEST_F(DemoSetupControllerTest, OnlineErrorDefault) {
} }
TEST_F(DemoSetupControllerTest, OnlineErrorPowerwashRequired) { TEST_F(DemoSetupControllerTest, OnlineErrorPowerwashRequired) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOnlineEnrollmentHelper(
&MockDemoModeOnlineEnrollmentHelperCreator< DemoModeSetupResult::ERROR_POWERWASH_REQUIRED);
DemoModeSetupResult::ERROR_POWERWASH_REQUIRED>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->Enroll( tested_controller_->Enroll(
...@@ -317,8 +304,8 @@ TEST_F(DemoSetupControllerTest, OnlineErrorPowerwashRequired) { ...@@ -317,8 +304,8 @@ TEST_F(DemoSetupControllerTest, OnlineErrorPowerwashRequired) {
} }
TEST_F(DemoSetupControllerTest, OnlineComponentError) { TEST_F(DemoSetupControllerTest, OnlineComponentError) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( // Expect no enrollment attempt.
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>); SetupMockDemoModeNoEnrollmentHelper();
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->SetCrOSComponentLoadErrorForTest( tested_controller_->SetCrOSComponentLoadErrorForTest(
...@@ -336,9 +323,7 @@ TEST_F(DemoSetupControllerTest, OnlineComponentError) { ...@@ -336,9 +323,7 @@ TEST_F(DemoSetupControllerTest, OnlineComponentError) {
} }
TEST_F(DemoSetupControllerTest, EnrollTwice) { TEST_F(DemoSetupControllerTest, EnrollTwice) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult::ERROR_DEFAULT);
&MockDemoModeOnlineEnrollmentHelperCreator<
DemoModeSetupResult::ERROR_DEFAULT>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->Enroll( tested_controller_->Enroll(
...@@ -353,8 +338,7 @@ TEST_F(DemoSetupControllerTest, EnrollTwice) { ...@@ -353,8 +338,7 @@ TEST_F(DemoSetupControllerTest, EnrollTwice) {
helper_->Reset(); helper_->Reset();
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult::SUCCESS);
&MockDemoModeOnlineEnrollmentHelperCreator<DemoModeSetupResult::SUCCESS>);
tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline); tested_controller_->set_demo_config(DemoSession::DemoModeConfig::kOnline);
tested_controller_->Enroll( tested_controller_->Enroll(
......
...@@ -9,10 +9,90 @@ ...@@ -9,10 +9,90 @@
#include "base/threading/thread_restrictions.h" #include "base/threading/thread_restrictions.h"
#include "components/policy/proto/device_management_backend.pb.h" #include "components/policy/proto/device_management_backend.pb.h"
using testing::_;
namespace {
MATCHER(ConfigIsAttestation, "") {
return arg.mode == policy::EnrollmentConfig::MODE_ATTESTATION;
}
MATCHER(ConfigIsOfflineDemo, "") {
return arg.mode == policy::EnrollmentConfig::MODE_OFFLINE_DEMO;
}
} // namespace
namespace chromeos { namespace chromeos {
namespace test { namespace test {
void SetupMockDemoModeNoEnrollmentHelper() {
std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
std::make_unique<EnterpriseEnrollmentHelperMock>();
EXPECT_CALL(*mock, Setup(_, _, _)).Times(0);
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
}
void SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult result) {
std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
std::make_unique<EnterpriseEnrollmentHelperMock>();
auto* mock_ptr = mock.get();
EXPECT_CALL(*mock, Setup(_, ConfigIsAttestation(), _));
EXPECT_CALL(*mock, EnrollUsingAttestation())
.WillRepeatedly(testing::Invoke([mock_ptr, result]() {
switch (result) {
case DemoModeSetupResult::SUCCESS:
mock_ptr->status_consumer()->OnDeviceEnrolled();
break;
case DemoModeSetupResult::ERROR_POWERWASH_REQUIRED:
mock_ptr->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForLockError(
chromeos::InstallAttributes::LOCK_ALREADY_LOCKED));
break;
case DemoModeSetupResult::ERROR_DEFAULT:
mock_ptr->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::
DM_STATUS_TEMPORARY_UNAVAILABLE));
break;
default:
NOTREACHED();
}
}));
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
};
void SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult result) {
std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
std::make_unique<EnterpriseEnrollmentHelperMock>();
auto* mock_ptr = mock.get();
EXPECT_CALL(*mock, Setup(_, ConfigIsOfflineDemo(), _));
EXPECT_CALL(*mock, EnrollForOfflineDemo())
.WillRepeatedly(testing::Invoke([mock_ptr, result]() {
switch (result) {
case DemoModeSetupResult::SUCCESS:
mock_ptr->status_consumer()->OnDeviceEnrolled();
break;
case DemoModeSetupResult::ERROR_POWERWASH_REQUIRED:
mock_ptr->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForLockError(
chromeos::InstallAttributes::LOCK_READBACK_ERROR));
break;
case DemoModeSetupResult::ERROR_DEFAULT:
mock_ptr->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForStatus(
policy::EnrollmentStatus::OFFLINE_POLICY_DECODING_FAILED));
break;
default:
NOTREACHED();
}
}));
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
};
bool SetupDummyOfflinePolicyDir(const std::string& account_id, bool SetupDummyOfflinePolicyDir(const std::string& account_id,
base::ScopedTempDir* temp_dir) { base::ScopedTempDir* temp_dir) {
base::ScopedAllowBlockingForTesting allow_io; base::ScopedAllowBlockingForTesting allow_io;
......
...@@ -29,75 +29,17 @@ enum class DemoModeSetupResult { ...@@ -29,75 +29,17 @@ enum class DemoModeSetupResult {
ERROR_POWERWASH_REQUIRED ERROR_POWERWASH_REQUIRED
}; };
// Helper method that mocks EnterpriseEnrollmentHelper to ensure that no
// enrollment attempt was made.
void SetupMockDemoModeNoEnrollmentHelper();
// Helper method that mocks EnterpriseEnrollmentHelper for online Demo Mode // Helper method that mocks EnterpriseEnrollmentHelper for online Demo Mode
// setup. It simulates specified Demo Mode enrollment |result|. // setup. It simulates specified Demo Mode enrollment |result|.
template <DemoModeSetupResult result> void SetupMockDemoModeOnlineEnrollmentHelper(DemoModeSetupResult result);
EnterpriseEnrollmentHelper* MockDemoModeOnlineEnrollmentHelperCreator(
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) {
EnterpriseEnrollmentHelperMock* mock =
new EnterpriseEnrollmentHelperMock(status_consumer);
EXPECT_EQ(enrollment_config.mode, policy::EnrollmentConfig::MODE_ATTESTATION);
EXPECT_CALL(*mock, EnrollUsingAttestation())
.WillRepeatedly(testing::Invoke([mock]() {
switch (result) {
case DemoModeSetupResult::SUCCESS:
mock->status_consumer()->OnDeviceEnrolled();
break;
case DemoModeSetupResult::ERROR_POWERWASH_REQUIRED:
mock->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForLockError(
chromeos::InstallAttributes::LOCK_ALREADY_LOCKED));
break;
case DemoModeSetupResult::ERROR_DEFAULT:
mock->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::
DM_STATUS_TEMPORARY_UNAVAILABLE));
break;
default:
NOTREACHED();
}
}));
return mock;
}
// Helper method that mocks EnterpriseEnrollmentHelper for offline Demo Mode // Helper method that mocks EnterpriseEnrollmentHelper for offline Demo Mode
// setup. It simulates specified Demo Mode enrollment |result|. // setup. It simulates specified Demo Mode enrollment |result|.
template <DemoModeSetupResult result> void SetupMockDemoModeOfflineEnrollmentHelper(DemoModeSetupResult result);
EnterpriseEnrollmentHelper* MockDemoModeOfflineEnrollmentHelperCreator(
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) {
EnterpriseEnrollmentHelperMock* mock =
new EnterpriseEnrollmentHelperMock(status_consumer);
EXPECT_EQ(enrollment_config.mode,
policy::EnrollmentConfig::MODE_OFFLINE_DEMO);
EXPECT_CALL(*mock, EnrollForOfflineDemo())
.WillRepeatedly(testing::Invoke([mock]() {
switch (result) {
case DemoModeSetupResult::SUCCESS:
mock->status_consumer()->OnDeviceEnrolled();
break;
case DemoModeSetupResult::ERROR_POWERWASH_REQUIRED:
mock->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForLockError(
chromeos::InstallAttributes::LOCK_READBACK_ERROR));
break;
case DemoModeSetupResult::ERROR_DEFAULT:
mock->status_consumer()->OnEnrollmentError(
policy::EnrollmentStatus::ForStatus(
policy::EnrollmentStatus::OFFLINE_POLICY_DECODING_FAILED));
break;
default:
NOTREACHED();
}
}));
return mock;
}
// Creates fake offline policy directory to be used in tests. // Creates fake offline policy directory to be used in tests.
bool SetupDummyOfflinePolicyDir(const std::string& account_id, bool SetupDummyOfflinePolicyDir(const std::string& account_id,
......
...@@ -33,6 +33,10 @@ namespace chromeos { ...@@ -33,6 +33,10 @@ namespace chromeos {
class BaseScreenDelegate; class BaseScreenDelegate;
class ScreenManager; class ScreenManager;
namespace test {
class EnrollmentHelperMixin;
}
// The screen implementation that links the enterprise enrollment UI into the // The screen implementation that links the enterprise enrollment UI into the
// OOBE wizard. // OOBE wizard.
class EnrollmentScreen class EnrollmentScreen
...@@ -92,28 +96,12 @@ class EnrollmentScreen ...@@ -92,28 +96,12 @@ class EnrollmentScreen
friend class MultiLicenseEnrollmentScreenUnitTest; friend class MultiLicenseEnrollmentScreenUnitTest;
friend class ZeroTouchEnrollmentScreenUnitTest; friend class ZeroTouchEnrollmentScreenUnitTest;
friend class AutomaticReenrollmentScreenUnitTest; friend class AutomaticReenrollmentScreenUnitTest;
friend class EnterpriseEnrollmentConfigurationTest; friend class test::EnrollmentHelperMixin;
FRIEND_TEST_ALL_PREFIXES(AttestationAuthEnrollmentScreenTest, TestCancel); FRIEND_TEST_ALL_PREFIXES(AttestationAuthEnrollmentScreenTest, TestCancel);
FRIEND_TEST_ALL_PREFIXES(ForcedAttestationAuthEnrollmentScreenTest, FRIEND_TEST_ALL_PREFIXES(ForcedAttestationAuthEnrollmentScreenTest,
TestCancel); TestCancel);
FRIEND_TEST_ALL_PREFIXES(MultiAuthEnrollmentScreenTest, TestCancel); FRIEND_TEST_ALL_PREFIXES(MultiAuthEnrollmentScreenTest, TestCancel);
FRIEND_TEST_ALL_PREFIXES(EnterpriseEnrollmentTest,
TestProperPageGetsLoadedOnEnrollmentSuccess);
FRIEND_TEST_ALL_PREFIXES(EnterpriseEnrollmentTest,
TestAttributePromptPageGetsLoaded);
FRIEND_TEST_ALL_PREFIXES(EnterpriseEnrollmentTest,
TestAuthCodeGetsProperlyReceivedFromGaia);
FRIEND_TEST_ALL_PREFIXES(ActiveDirectoryJoinTest,
TestActiveDirectoryEnrollment_Success);
FRIEND_TEST_ALL_PREFIXES(ActiveDirectoryJoinTest,
TestActiveDirectoryEnrollment_DistinguishedName);
FRIEND_TEST_ALL_PREFIXES(ActiveDirectoryJoinTest,
TestActiveDirectoryEnrollment_UIErrors);
FRIEND_TEST_ALL_PREFIXES(ActiveDirectoryJoinTest,
TestActiveDirectoryEnrollment_ErrorCard);
FRIEND_TEST_ALL_PREFIXES(ActiveDirectoryJoinTest,
TestActiveDirectoryEnrollment_Streamline);
FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest, Retry); FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest, Retry);
FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest, TestSuccess); FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest, TestSuccess);
FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest, FRIEND_TEST_ALL_PREFIXES(ZeroTouchEnrollmentScreenUnitTest,
......
...@@ -94,19 +94,16 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -94,19 +94,16 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
// appropriate expectations for testing with the Google Mock framework. // appropriate expectations for testing with the Google Mock framework.
// The template parameter should_enroll indicates whether or not // The template parameter should_enroll indicates whether or not
// the EnterpriseEnrollmentHelper should be mocked to successfully enroll. // the EnterpriseEnrollmentHelper should be mocked to successfully enroll.
template <AttestationEnrollmentStatus status> void SetupMockEnrollmentHelper(AttestationEnrollmentStatus status) {
static EnterpriseEnrollmentHelper* MockEnrollmentHelperCreator( std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer, std::make_unique<EnterpriseEnrollmentHelperMock>();
const policy::EnrollmentConfig& enrollment_config, EnterpriseEnrollmentHelperMock* mock_ptr = mock.get();
const std::string& enrolling_user_domain) {
EnterpriseEnrollmentHelperMock* mock =
new EnterpriseEnrollmentHelperMock(status_consumer);
if (status == SUCCESS) { if (status == SUCCESS) {
// Define behavior of EnrollUsingAttestation to successfully enroll. // Define behavior of EnrollUsingAttestation to successfully enroll.
EXPECT_CALL(*mock, EnrollUsingAttestation()) EXPECT_CALL(*mock, EnrollUsingAttestation())
.Times(AnyNumber()) .Times(AnyNumber())
.WillRepeatedly(Invoke([mock]() { .WillRepeatedly(Invoke([mock_ptr]() {
static_cast<EnrollmentScreen*>(mock->status_consumer()) static_cast<EnrollmentScreen*>(mock_ptr->status_consumer())
->ShowEnrollmentStatusOnSuccess(); ->ShowEnrollmentStatusOnSuccess();
})); }));
} else { } else {
...@@ -120,16 +117,17 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -120,16 +117,17 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
DM_STATUS_TEMPORARY_UNAVAILABLE); DM_STATUS_TEMPORARY_UNAVAILABLE);
EXPECT_CALL(*mock, EnrollUsingAttestation()) EXPECT_CALL(*mock, EnrollUsingAttestation())
.Times(AnyNumber()) .Times(AnyNumber())
.WillRepeatedly(Invoke([mock, enrollment_status]() { .WillRepeatedly(Invoke([mock_ptr, enrollment_status]() {
mock->status_consumer()->OnEnrollmentError(enrollment_status); mock_ptr->status_consumer()->OnEnrollmentError(enrollment_status);
})); }));
} }
// Define behavior of ClearAuth to only run the callback it is given. // Define behavior of ClearAuth to only run the callback it is given.
EXPECT_CALL(*mock, ClearAuth(_)) EXPECT_CALL(*mock, ClearAuth(_))
.Times(AnyNumber()) .Times(AnyNumber())
.WillRepeatedly(Invoke( .WillRepeatedly(Invoke(
[](const base::RepeatingClosure& callback) { callback.Run(); })); [](base::OnceClosure callback) { std::move(callback).Run(); }));
return mock;
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
} }
void SetUpEnrollmentScreen() override { void SetUpEnrollmentScreen() override {
...@@ -159,9 +157,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -159,9 +157,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
void TestRetry() { void TestRetry() {
// Define behavior of EnterpriseEnrollmentHelperMock to always fail // Define behavior of EnterpriseEnrollmentHelperMock to always fail
// enrollment. // enrollment.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockEnrollmentHelper(DMSERVER_ERROR);
&ZeroTouchEnrollmentScreenUnitTest::MockEnrollmentHelperCreator<
DMSERVER_ERROR>);
SetUpEnrollmentScreen(); SetUpEnrollmentScreen();
...@@ -180,9 +176,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -180,9 +176,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
void TestFinishEnrollmentFlow() { void TestFinishEnrollmentFlow() {
// Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll. // Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockEnrollmentHelper(SUCCESS);
&ZeroTouchEnrollmentScreenUnitTest::MockEnrollmentHelperCreator<
SUCCESS>);
SetUpEnrollmentScreen(); SetUpEnrollmentScreen();
...@@ -201,9 +195,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -201,9 +195,7 @@ class ZeroTouchEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
void TestFallback() { void TestFallback() {
// Define behavior of EnterpriseEnrollmentHelperMock to fail // Define behavior of EnterpriseEnrollmentHelperMock to fail
// attestation-based enrollment. // attestation-based enrollment.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockEnrollmentHelper(DEVICE_NOT_SETUP_FOR_ZERO_TOUCH);
&ZeroTouchEnrollmentScreenUnitTest::MockEnrollmentHelperCreator<
DEVICE_NOT_SETUP_FOR_ZERO_TOUCH>);
SetUpEnrollmentScreenForFallback(); SetUpEnrollmentScreenForFallback();
...@@ -233,9 +225,7 @@ TEST_F(ZeroTouchEnrollmentScreenUnitTest, Retry) { ...@@ -233,9 +225,7 @@ TEST_F(ZeroTouchEnrollmentScreenUnitTest, Retry) {
TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryOnTopOfUser) { TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryOnTopOfUser) {
// Define behavior of EnterpriseEnrollmentHelperMock to always fail // Define behavior of EnterpriseEnrollmentHelperMock to always fail
// enrollment. // enrollment.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockEnrollmentHelper(DMSERVER_ERROR);
&ZeroTouchEnrollmentScreenUnitTest::MockEnrollmentHelperCreator<
DMSERVER_ERROR>);
SetUpEnrollmentScreen(); SetUpEnrollmentScreen();
...@@ -261,8 +251,7 @@ TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryOnTopOfUser) { ...@@ -261,8 +251,7 @@ TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryOnTopOfUser) {
TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryAfterSuccess) { TEST_F(ZeroTouchEnrollmentScreenUnitTest, DoNotRetryAfterSuccess) {
// Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll. // Define behavior of EnterpriseEnrollmentHelperMock to successfully enroll.
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( SetupMockEnrollmentHelper(SUCCESS);
&ZeroTouchEnrollmentScreenUnitTest::MockEnrollmentHelperCreator<SUCCESS>);
SetUpEnrollmentScreen(); SetUpEnrollmentScreen();
...@@ -329,32 +318,25 @@ class MultiLicenseEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest { ...@@ -329,32 +318,25 @@ class MultiLicenseEnrollmentScreenUnitTest : public EnrollmentScreenUnitTest {
EnrollmentScreenUnitTest::SetUpEnrollmentScreen(); EnrollmentScreenUnitTest::SetUpEnrollmentScreen();
} }
static EnterpriseEnrollmentHelper* MockEnrollmentHelperCreator(
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) {
EnterpriseEnrollmentHelperMock* mock =
new EnterpriseEnrollmentHelperMock(status_consumer);
EXPECT_CALL(*mock, EnrollUsingAuthCode(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([mock](const std::string&, bool) {
EnrollmentLicenseMap licenses;
static_cast<EnrollmentScreen*>(mock->status_consumer())
->OnMultipleLicensesAvailable(licenses);
}));
EXPECT_CALL(*mock, UseLicenseType(::policy::LicenseType::ANNUAL)).Times(1);
return mock;
}
private: private:
DISALLOW_COPY_AND_ASSIGN(MultiLicenseEnrollmentScreenUnitTest); DISALLOW_COPY_AND_ASSIGN(MultiLicenseEnrollmentScreenUnitTest);
}; };
// Sign in and check that selected license type is propagated correctly. // Sign in and check that selected license type is propagated correctly.
TEST_F(MultiLicenseEnrollmentScreenUnitTest, TestLicenseSelection) { TEST_F(MultiLicenseEnrollmentScreenUnitTest, TestLicenseSelection) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
&MultiLicenseEnrollmentScreenUnitTest::MockEnrollmentHelperCreator); std::make_unique<EnterpriseEnrollmentHelperMock>();
auto* mock_ref = mock.get();
EXPECT_CALL(*mock, EnrollUsingAuthCode(_, _))
.Times(AnyNumber())
.WillRepeatedly(Invoke([mock_ref](const std::string&, bool) {
EnrollmentLicenseMap licenses;
static_cast<EnrollmentScreen*>(mock_ref->status_consumer())
->OnMultipleLicensesAvailable(licenses);
}));
EXPECT_CALL(*mock, UseLicenseType(::policy::LicenseType::ANNUAL)).Times(1);
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
EXPECT_CALL(*GetMockScreenView(), SetEnrollmentConfig(_, _)).Times(1); EXPECT_CALL(*GetMockScreenView(), SetEnrollmentConfig(_, _)).Times(1);
......
...@@ -10,15 +10,18 @@ ...@@ -10,15 +10,18 @@
namespace chromeos { namespace chromeos {
EnterpriseEnrollmentHelper::CreateMockEnrollmentHelper EnterpriseEnrollmentHelper*
EnterpriseEnrollmentHelper::create_mock_enrollment_helper_ = nullptr; EnterpriseEnrollmentHelper::mock_enrollment_helper_ = nullptr;
EnterpriseEnrollmentHelper::~EnterpriseEnrollmentHelper() {} EnterpriseEnrollmentHelper::~EnterpriseEnrollmentHelper() {}
// static // static
void EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( void EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(
CreateMockEnrollmentHelper creator) { std::unique_ptr<EnterpriseEnrollmentHelper> mock) {
create_mock_enrollment_helper_ = creator; if (mock_enrollment_helper_) {
delete mock_enrollment_helper_;
}
mock_enrollment_helper_ = mock.release();
} }
// static // static
...@@ -27,26 +30,26 @@ std::unique_ptr<EnterpriseEnrollmentHelper> EnterpriseEnrollmentHelper::Create( ...@@ -27,26 +30,26 @@ std::unique_ptr<EnterpriseEnrollmentHelper> EnterpriseEnrollmentHelper::Create(
ActiveDirectoryJoinDelegate* ad_join_delegate, ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config, const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) { const std::string& enrolling_user_domain) {
std::unique_ptr<EnterpriseEnrollmentHelper> result;
// Create a mock instance. // Create a mock instance.
if (create_mock_enrollment_helper_) { if (mock_enrollment_helper_) {
// The evaluated code might call |SetupEnrollmentHelperMock| to setup a new result = base::WrapUnique(mock_enrollment_helper_);
// allocator, so we reset the enrollment helper to null before that. mock_enrollment_helper_ = nullptr;
auto enrollment_helper_allocator = create_mock_enrollment_helper_; } else {
create_mock_enrollment_helper_ = nullptr; result = std::make_unique<EnterpriseEnrollmentHelperImpl>();
EnterpriseEnrollmentHelper* helper = enrollment_helper_allocator(
status_consumer, enrollment_config, enrolling_user_domain);
return base::WrapUnique(helper);
} }
result->set_status_consumer(status_consumer);
return base::WrapUnique(new EnterpriseEnrollmentHelperImpl( result->Setup(ad_join_delegate, enrollment_config, enrolling_user_domain);
status_consumer, ad_join_delegate, enrollment_config, return result;
enrolling_user_domain));
} }
EnterpriseEnrollmentHelper::EnterpriseEnrollmentHelper( EnterpriseEnrollmentHelper::EnterpriseEnrollmentHelper() {}
EnrollmentStatusConsumer* status_consumer)
: status_consumer_(status_consumer) { void EnterpriseEnrollmentHelper::set_status_consumer(
EnrollmentStatusConsumer* status_consumer) {
DCHECK(status_consumer_); DCHECK(status_consumer_);
status_consumer_ = status_consumer;
} }
} // namespace chromeos } // namespace chromeos
...@@ -88,16 +88,10 @@ class EnterpriseEnrollmentHelper { ...@@ -88,16 +88,10 @@ class EnterpriseEnrollmentHelper {
const policy::EnrollmentConfig& enrollment_config, const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain); const std::string& enrolling_user_domain);
using CreateMockEnrollmentHelper = // Sets up a mock object that would be returned by next Create call.
EnterpriseEnrollmentHelper* (*)(EnrollmentStatusConsumer* status_consumer, // This call passes ownership of |mock|.
const policy::EnrollmentConfig& static void SetEnrollmentHelperMock(
enrollment_config, std::unique_ptr<EnterpriseEnrollmentHelper> mock);
const std::string& enrolling_user_domain);
// Use |creator| instead of the default enrollment helper allocator. This
// allows tests to substitute in a mock enrollment helper. This function will
// only be used once.
static void SetupEnrollmentHelperMock(CreateMockEnrollmentHelper creator);
virtual ~EnterpriseEnrollmentHelper(); virtual ~EnterpriseEnrollmentHelper();
...@@ -160,23 +154,29 @@ class EnterpriseEnrollmentHelper { ...@@ -160,23 +154,29 @@ class EnterpriseEnrollmentHelper {
// used) and revokes fetched tokens. // used) and revokes fetched tokens.
// Does not revoke the additional token if enrollment finished successfully. // Does not revoke the additional token if enrollment finished successfully.
// Calls |callback| on completion. // Calls |callback| on completion.
virtual void ClearAuth(const base::Closure& callback) = 0; virtual void ClearAuth(base::OnceClosure callback) = 0;
protected: protected:
// |status_consumer| must outlive |this|. Moreover, the user of this class // The user of this class is responsible for clearing auth data in some cases
// is responsible for clearing auth data in some cases (see comment for // (see comment for EnrollUsingProfile()).
// EnrollUsingProfile()). EnterpriseEnrollmentHelper();
explicit EnterpriseEnrollmentHelper(
EnrollmentStatusConsumer* status_consumer); // This method is called once from Create method.
virtual void Setup(ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) = 0;
// This method is used in Create method. |status_consumer| must outlive
// |this|.
void set_status_consumer(EnrollmentStatusConsumer* status_consumer);
EnrollmentStatusConsumer* status_consumer() { return status_consumer_; } EnrollmentStatusConsumer* status_consumer() const { return status_consumer_; }
private: private:
EnrollmentStatusConsumer* status_consumer_; EnrollmentStatusConsumer* status_consumer_;
// If this is not nullptr, then it will be used to create the enrollment // If this is not nullptr, then it will be used to as next enrollment helper.
// helper. |create_mock_enrollment_helper_| needs to outlive this class. static EnterpriseEnrollmentHelper* mock_enrollment_helper_;
static CreateMockEnrollmentHelper create_mock_enrollment_helper_;
DISALLOW_COPY_AND_ASSIGN(EnterpriseEnrollmentHelper); DISALLOW_COPY_AND_ASSIGN(EnterpriseEnrollmentHelper);
}; };
......
...@@ -72,16 +72,7 @@ void TokenRevoker::OnOAuth2RevokeTokenCompleted( ...@@ -72,16 +72,7 @@ void TokenRevoker::OnOAuth2RevokeTokenCompleted(
namespace chromeos { namespace chromeos {
EnterpriseEnrollmentHelperImpl::EnterpriseEnrollmentHelperImpl( EnterpriseEnrollmentHelperImpl::EnterpriseEnrollmentHelperImpl() {
EnrollmentStatusConsumer* status_consumer,
ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain)
: EnterpriseEnrollmentHelper(status_consumer),
enrollment_config_(enrollment_config),
enrolling_user_domain_(enrolling_user_domain),
ad_join_delegate_(ad_join_delegate),
weak_ptr_factory_(this) {
// Init the TPM if it has not been done until now (in debug build we might // Init the TPM if it has not been done until now (in debug build we might
// have not done that yet). // have not done that yet).
DBusThreadManager::Get()->GetCryptohomeClient()->TpmCanAttemptOwnership( DBusThreadManager::Get()->GetCryptohomeClient()->TpmCanAttemptOwnership(
...@@ -95,6 +86,15 @@ EnterpriseEnrollmentHelperImpl::~EnterpriseEnrollmentHelperImpl() { ...@@ -95,6 +86,15 @@ EnterpriseEnrollmentHelperImpl::~EnterpriseEnrollmentHelperImpl() {
(oauth_status_ == OAUTH_FINISHED && (success_ || oauth_data_cleared_))); (oauth_status_ == OAUTH_FINISHED && (success_ || oauth_data_cleared_)));
} }
void EnterpriseEnrollmentHelperImpl::Setup(
ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) {
ad_join_delegate_ = ad_join_delegate;
enrollment_config_ = enrollment_config;
enrolling_user_domain_ = enrolling_user_domain;
}
void EnterpriseEnrollmentHelperImpl::EnrollUsingAuthCode( void EnterpriseEnrollmentHelperImpl::EnrollUsingAuthCode(
const std::string& auth_code, const std::string& auth_code,
bool fetch_additional_token) { bool fetch_additional_token) {
...@@ -206,7 +206,7 @@ void EnterpriseEnrollmentHelperImpl::OnDeviceAccountClientError( ...@@ -206,7 +206,7 @@ void EnterpriseEnrollmentHelperImpl::OnDeviceAccountClientError(
FROM_HERE, device_account_initializer_.release()); FROM_HERE, device_account_initializer_.release());
} }
void EnterpriseEnrollmentHelperImpl::ClearAuth(const base::Closure& callback) { void EnterpriseEnrollmentHelperImpl::ClearAuth(base::OnceClosure callback) {
if (oauth_status_ != OAUTH_NOT_STARTED) { if (oauth_status_ != OAUTH_NOT_STARTED) {
// Do not revoke the additional token if enrollment has finished // Do not revoke the additional token if enrollment has finished
// successfully. // successfully.
...@@ -228,8 +228,9 @@ void EnterpriseEnrollmentHelperImpl::ClearAuth(const base::Closure& callback) { ...@@ -228,8 +228,9 @@ void EnterpriseEnrollmentHelperImpl::ClearAuth(const base::Closure& callback) {
} }
auth_data_.reset(); auth_data_.reset();
chromeos::ProfileHelper::Get()->ClearSigninProfile( chromeos::ProfileHelper::Get()->ClearSigninProfile(
base::Bind(&EnterpriseEnrollmentHelperImpl::OnSigninProfileCleared, base::AdaptCallbackForRepeating(base::BindOnce(
weak_ptr_factory_.GetWeakPtr(), callback)); &EnterpriseEnrollmentHelperImpl::OnSigninProfileCleared,
weak_ptr_factory_.GetWeakPtr(), std::move(callback))));
} }
bool EnterpriseEnrollmentHelperImpl::ShouldCheckLicenseType() const { bool EnterpriseEnrollmentHelperImpl::ShouldCheckLicenseType() const {
...@@ -601,9 +602,9 @@ void EnterpriseEnrollmentHelperImpl::UMA(policy::MetricEnrollment sample) { ...@@ -601,9 +602,9 @@ void EnterpriseEnrollmentHelperImpl::UMA(policy::MetricEnrollment sample) {
} }
void EnterpriseEnrollmentHelperImpl::OnSigninProfileCleared( void EnterpriseEnrollmentHelperImpl::OnSigninProfileCleared(
const base::Closure& callback) { base::OnceClosure callback) {
oauth_data_cleared_ = true; oauth_data_cleared_ = true;
callback.Run(); std::move(callback).Run();
} }
} // namespace chromeos } // namespace chromeos
...@@ -31,14 +31,10 @@ class EnterpriseEnrollmentHelperImpl ...@@ -31,14 +31,10 @@ class EnterpriseEnrollmentHelperImpl
public policy::DeviceAccountInitializer::Delegate, public policy::DeviceAccountInitializer::Delegate,
public policy::DeviceCloudPolicyManagerChromeOS::Observer { public policy::DeviceCloudPolicyManagerChromeOS::Observer {
public: public:
EnterpriseEnrollmentHelperImpl( EnterpriseEnrollmentHelperImpl();
EnrollmentStatusConsumer* status_consumer,
ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain);
~EnterpriseEnrollmentHelperImpl() override; ~EnterpriseEnrollmentHelperImpl() override;
// Overridden from EnterpriseEnrollmentHelper: // EnterpriseEnrollmentHelper:
void EnrollUsingAuthCode(const std::string& auth_code, void EnrollUsingAuthCode(const std::string& auth_code,
bool fetch_additional_token) override; bool fetch_additional_token) override;
void EnrollUsingToken(const std::string& token) override; void EnrollUsingToken(const std::string& token) override;
...@@ -46,11 +42,14 @@ class EnterpriseEnrollmentHelperImpl ...@@ -46,11 +42,14 @@ class EnterpriseEnrollmentHelperImpl
void EnrollUsingAttestation() override; void EnrollUsingAttestation() override;
void EnrollForOfflineDemo() override; void EnrollForOfflineDemo() override;
void RestoreAfterRollback() override; void RestoreAfterRollback() override;
void ClearAuth(const base::Closure& callback) override; void ClearAuth(base::OnceClosure callback) override;
void UseLicenseType(policy::LicenseType type) override; void UseLicenseType(policy::LicenseType type) override;
void GetDeviceAttributeUpdatePermission() override; void GetDeviceAttributeUpdatePermission() override;
void UpdateDeviceAttributes(const std::string& asset_id, void UpdateDeviceAttributes(const std::string& asset_id,
const std::string& location) override; const std::string& location) override;
void Setup(ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) override;
// DeviceCloudPolicyManagerChromeOS::Observer: // DeviceCloudPolicyManagerChromeOS::Observer:
void OnDeviceCloudPolicyManagerConnected() override; void OnDeviceCloudPolicyManagerConnected() override;
...@@ -101,13 +100,13 @@ class EnterpriseEnrollmentHelperImpl ...@@ -101,13 +100,13 @@ class EnterpriseEnrollmentHelperImpl
// Called by ProfileHelper when a signin profile clearance has finished. // Called by ProfileHelper when a signin profile clearance has finished.
// |callback| is a callback, that was passed to ClearAuth() before. // |callback| is a callback, that was passed to ClearAuth() before.
void OnSigninProfileCleared(const base::Closure& callback); void OnSigninProfileCleared(base::OnceClosure callback);
// Called when CloudPolicyClient exists, so device account can be initialized. // Called when CloudPolicyClient exists, so device account can be initialized.
void RestoreAfterRollbackInitialized(); void RestoreAfterRollbackInitialized();
const policy::EnrollmentConfig enrollment_config_; policy::EnrollmentConfig enrollment_config_;
const std::string enrolling_user_domain_; std::string enrolling_user_domain_;
bool fetch_additional_token_; bool fetch_additional_token_;
std::string additional_token_; std::string additional_token_;
...@@ -125,7 +124,7 @@ class EnterpriseEnrollmentHelperImpl ...@@ -125,7 +124,7 @@ class EnterpriseEnrollmentHelperImpl
std::unique_ptr<policy::PolicyOAuth2TokenFetcher> oauth_fetcher_; std::unique_ptr<policy::PolicyOAuth2TokenFetcher> oauth_fetcher_;
std::unique_ptr<policy::DeviceAccountInitializer> device_account_initializer_; std::unique_ptr<policy::DeviceAccountInitializer> device_account_initializer_;
base::WeakPtrFactory<EnterpriseEnrollmentHelperImpl> weak_ptr_factory_; base::WeakPtrFactory<EnterpriseEnrollmentHelperImpl> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(EnterpriseEnrollmentHelperImpl); DISALLOW_COPY_AND_ASSIGN(EnterpriseEnrollmentHelperImpl);
}; };
......
...@@ -7,14 +7,12 @@ ...@@ -7,14 +7,12 @@
namespace chromeos { namespace chromeos {
EnterpriseEnrollmentHelperMock::EnterpriseEnrollmentHelperMock( EnterpriseEnrollmentHelperMock::EnterpriseEnrollmentHelperMock() {}
EnrollmentStatusConsumer* status_consumer)
: EnterpriseEnrollmentHelper(status_consumer) {}
EnterpriseEnrollmentHelperMock::~EnterpriseEnrollmentHelperMock() {} EnterpriseEnrollmentHelperMock::~EnterpriseEnrollmentHelperMock() {}
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* EnterpriseEnrollmentHelper::EnrollmentStatusConsumer*
EnterpriseEnrollmentHelperMock::status_consumer() { EnterpriseEnrollmentHelperMock::status_consumer() const {
return EnterpriseEnrollmentHelper::status_consumer(); return EnterpriseEnrollmentHelper::status_consumer();
} }
......
...@@ -16,12 +16,15 @@ namespace chromeos { ...@@ -16,12 +16,15 @@ namespace chromeos {
// Mocks out EnterpriseEnrollmentHelper. // Mocks out EnterpriseEnrollmentHelper.
class EnterpriseEnrollmentHelperMock : public EnterpriseEnrollmentHelper { class EnterpriseEnrollmentHelperMock : public EnterpriseEnrollmentHelper {
public: public:
explicit EnterpriseEnrollmentHelperMock( EnterpriseEnrollmentHelperMock();
EnrollmentStatusConsumer* status_consumer);
~EnterpriseEnrollmentHelperMock() override; ~EnterpriseEnrollmentHelperMock() override;
EnrollmentStatusConsumer* status_consumer(); EnrollmentStatusConsumer* status_consumer() const;
MOCK_METHOD3(Setup,
void(ActiveDirectoryJoinDelegate* ad_join_delegate,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain));
MOCK_METHOD2(EnrollUsingAuthCode, MOCK_METHOD2(EnrollUsingAuthCode,
void(const std::string& auth_code, bool fetch_additional_token)); void(const std::string& auth_code, bool fetch_additional_token));
MOCK_METHOD1(EnrollUsingToken, void(const std::string& token)); MOCK_METHOD1(EnrollUsingToken, void(const std::string& token));
...@@ -33,7 +36,7 @@ class EnterpriseEnrollmentHelperMock : public EnterpriseEnrollmentHelper { ...@@ -33,7 +36,7 @@ class EnterpriseEnrollmentHelperMock : public EnterpriseEnrollmentHelper {
MOCK_METHOD0(GetDeviceAttributeUpdatePermission, void()); MOCK_METHOD0(GetDeviceAttributeUpdatePermission, void());
MOCK_METHOD2(UpdateDeviceAttributes, MOCK_METHOD2(UpdateDeviceAttributes,
void(const std::string& asset_id, const std::string& location)); void(const std::string& asset_id, const std::string& location));
MOCK_METHOD1(ClearAuth, void(const base::Closure& callback)); MOCK_METHOD1(ClearAuth, void(base::OnceClosure callback));
}; };
} // namespace chromeos } // namespace chromeos
......
...@@ -7,12 +7,13 @@ ...@@ -7,12 +7,13 @@
#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper_mock.h" #include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper_mock.h"
#include "chrome/browser/chromeos/login/existing_user_controller.h" #include "chrome/browser/chromeos/login/existing_user_controller.h"
#include "chrome/browser/chromeos/login/login_wizard.h" #include "chrome/browser/chromeos/login/login_wizard.h"
#include "chrome/browser/chromeos/login/mixin_based_in_process_browser_test.h"
#include "chrome/browser/chromeos/login/oobe_screen.h" #include "chrome/browser/chromeos/login/oobe_screen.h"
#include "chrome/browser/chromeos/login/startup_utils.h" #include "chrome/browser/chromeos/login/startup_utils.h"
#include "chrome/browser/chromeos/login/test/enrollment_helper_mixin.h"
#include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h" #include "chrome/browser/chromeos/login/test/oobe_screen_waiter.h"
#include "chrome/browser/chromeos/login/wizard_controller.h" #include "chrome/browser/chromeos/login/wizard_controller.h"
#include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h" #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chromeos/constants/chromeos_switches.h" #include "chromeos/constants/chromeos_switches.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/shill_service_client.h" #include "chromeos/dbus/shill_service_client.h"
...@@ -32,21 +33,21 @@ constexpr char kDefaultNetworkServicePath[] = "/service/eth1"; ...@@ -32,21 +33,21 @@ constexpr char kDefaultNetworkServicePath[] = "/service/eth1";
} // namespace } // namespace
// Hands-off enrollment flow test. // Hands-off enrollment flow test.
class HandsOffEnrollmentTest : public InProcessBrowserTest { class HandsOffEnrollmentTest : public MixinBasedInProcessBrowserTest {
protected: protected:
HandsOffEnrollmentTest() {} HandsOffEnrollmentTest() {}
~HandsOffEnrollmentTest() override = default; ~HandsOffEnrollmentTest() override = default;
// InProcessBrowserTest: // InProcessBrowserTest:
void SetUpCommandLine(base::CommandLine* command_line) override { void SetUpCommandLine(base::CommandLine* command_line) override {
InProcessBrowserTest::SetUpCommandLine(command_line); MixinBasedInProcessBrowserTest::SetUpCommandLine(command_line);
command_line->AppendArg(switches::kLoginManager); command_line->AppendArg(switches::kLoginManager);
command_line->AppendSwitchASCII( command_line->AppendSwitchASCII(
switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off"); switches::kEnterpriseEnableZeroTouchEnrollment, "hands-off");
} }
void SetUpOnMainThread() override { void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread(); MixinBasedInProcessBrowserTest::SetUpOnMainThread();
ShowLoginWizard(OobeScreen::SCREEN_TEST_NO_WINDOW); ShowLoginWizard(OobeScreen::SCREEN_TEST_NO_WINDOW);
// Set official build so EULA screen is not skipped by default. // Set official build so EULA screen is not skipped by default.
...@@ -79,56 +80,20 @@ class HandsOffEnrollmentTest : public InProcessBrowserTest { ...@@ -79,56 +80,20 @@ class HandsOffEnrollmentTest : public InProcessBrowserTest {
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
} }
// Result of attestation based enrollment used by protected:
// EnterpriseEnrollmentHelperMock. test::EnrollmentHelperMixin enrollment_helper_{&mixin_host_};
enum class AttestationEnrollmentResult { SUCCESS, ERROR };
// Helper method that mocks EnterpriseEnrollmentHelper for hands-off
// enrollment. It simulates specified attestation based enrollment |result|.
template <AttestationEnrollmentResult result>
static EnterpriseEnrollmentHelper* MockEnrollmentHelperCreator(
EnterpriseEnrollmentHelper::EnrollmentStatusConsumer* status_consumer,
const policy::EnrollmentConfig& enrollment_config,
const std::string& enrolling_user_domain) {
EnterpriseEnrollmentHelperMock* mock =
new EnterpriseEnrollmentHelperMock(status_consumer);
if (result == AttestationEnrollmentResult::SUCCESS) {
// Simulate successful attestation based enrollment.
EXPECT_CALL(*mock, EnrollUsingAttestation())
.Times(testing::AnyNumber())
.WillRepeatedly(testing::Invoke(
[mock]() { mock->status_consumer()->OnDeviceEnrolled(); }));
EXPECT_CALL(*mock, GetDeviceAttributeUpdatePermission())
.Times(testing::AnyNumber())
.WillRepeatedly(testing::Invoke([mock]() {
mock->status_consumer()->OnDeviceAttributeUpdatePermission(false);
}));
} else {
// Simulate error during attestation based enrollment.
const policy::EnrollmentStatus enrollment_status =
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::DM_STATUS_TEMPORARY_UNAVAILABLE);
EXPECT_CALL(*mock, GetDeviceAttributeUpdatePermission())
.Times(testing::AnyNumber())
.WillRepeatedly(testing::Invoke([mock, enrollment_status]() {
mock->status_consumer()->OnEnrollmentError(enrollment_status);
}));
}
// Define behavior of ClearAuth to only run the callback it is given.
EXPECT_CALL(*mock, ClearAuth(testing::_))
.Times(testing::AnyNumber())
.WillRepeatedly(testing::Invoke(
[](const base::RepeatingClosure& callback) { callback.Run(); }));
return mock;
}
private: private:
DISALLOW_COPY_AND_ASSIGN(HandsOffEnrollmentTest); DISALLOW_COPY_AND_ASSIGN(HandsOffEnrollmentTest);
}; };
IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, NetworkConnectionReady) { IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, NetworkConnectionReady) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockEnrollmentHelperCreator<AttestationEnrollmentResult::SUCCESS>); policy::EnrollmentConfig::MODE_ATTESTATION_LOCAL_FORCED);
enrollment_helper_.ExpectAttestationEnrollmentSuccess();
enrollment_helper_.DisableAttributePromptUpdate();
enrollment_helper_.SetupClearAuth();
SimulateNetworkConnected(); SimulateNetworkConnected();
WizardController::default_controller()->AdvanceToScreen( WizardController::default_controller()->AdvanceToScreen(
...@@ -146,9 +111,11 @@ IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, NetworkConnectionReady) { ...@@ -146,9 +111,11 @@ IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, NetworkConnectionReady) {
} }
IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, WaitForNetworkConnection) { IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, WaitForNetworkConnection) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.ExpectEnrollmentMode(
&MockEnrollmentHelperCreator<AttestationEnrollmentResult::SUCCESS>); policy::EnrollmentConfig::MODE_ATTESTATION_LOCAL_FORCED);
enrollment_helper_.ExpectAttestationEnrollmentSuccess();
enrollment_helper_.DisableAttributePromptUpdate();
enrollment_helper_.SetupClearAuth();
WizardController::default_controller()->AdvanceToScreen( WizardController::default_controller()->AdvanceToScreen(
OobeScreen::SCREEN_OOBE_WELCOME); OobeScreen::SCREEN_OOBE_WELCOME);
...@@ -166,8 +133,17 @@ IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, WaitForNetworkConnection) { ...@@ -166,8 +133,17 @@ IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, WaitForNetworkConnection) {
} }
IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, EnrollmentError) { IN_PROC_BROWSER_TEST_F(HandsOffEnrollmentTest, EnrollmentError) {
EnterpriseEnrollmentHelper::SetupEnrollmentHelperMock( enrollment_helper_.SetupClearAuth();
&MockEnrollmentHelperCreator<AttestationEnrollmentResult::ERROR>); const policy::EnrollmentStatus enrollment_status =
policy::EnrollmentStatus::ForRegistrationError(
policy::DeviceManagementStatus::DM_STATUS_TEMPORARY_UNAVAILABLE);
// With hands-off we expect that there will be a retry attempt, so
// set up repeated expectations.
enrollment_helper_.ExpectEnrollmentModeRepeated(
policy::EnrollmentConfig::MODE_ATTESTATION_LOCAL_FORCED);
enrollment_helper_.ExpectAttestationEnrollmentErrorRepeated(
enrollment_status);
SimulateNetworkConnected(); SimulateNetworkConnected();
WizardController::default_controller()->AdvanceToScreen( WizardController::default_controller()->AdvanceToScreen(
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/login/test/enrollment_helper_mixin.h"
#include "base/bind.h"
#include "chrome/browser/chromeos/login/enrollment/enrollment_screen.h"
#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper.h"
#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper_mock.h"
#include "chrome/browser/chromeos/login/wizard_controller.h"
#include "chrome/browser/chromeos/policy/active_directory_join_delegate.h"
using testing::_;
using testing::AtLeast;
using testing::Invoke;
using testing::InvokeWithoutArgs;
namespace {
MATCHER_P(ConfigModeMatches, mode, "") {
return arg.mode == mode;
}
} // namespace
namespace chromeos {
namespace test {
// static
const char EnrollmentHelperMixin::kTestAuthCode[] = "test_auth_code";
EnrollmentHelperMixin::EnrollmentHelperMixin(
InProcessBrowserTestMixinHost* host)
: InProcessBrowserTestMixin(host) {}
EnrollmentHelperMixin::~EnrollmentHelperMixin() = default;
void EnrollmentHelperMixin::SetUpInProcessBrowserTestFixture() {
ResetMock();
}
void EnrollmentHelperMixin::TearDownInProcessBrowserTestFixture() {
mock_ = nullptr;
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(nullptr);
// Enrollment screen might have reference to enrollment_helper_.
if (WizardController::default_controller()) {
auto* screen_manager =
WizardController::default_controller()->screen_manager();
if (screen_manager->HasScreen(OobeScreen::SCREEN_OOBE_ENROLLMENT)) {
EnrollmentScreen::Get(screen_manager)->enrollment_helper_.reset();
}
}
}
void EnrollmentHelperMixin::ResetMock() {
std::unique_ptr<EnterpriseEnrollmentHelperMock> mock =
std::make_unique<EnterpriseEnrollmentHelperMock>();
mock_ = mock.get();
EnterpriseEnrollmentHelper::SetEnrollmentHelperMock(std::move(mock));
}
void EnrollmentHelperMixin::ExpectNoEnrollment() {
EXPECT_CALL(*mock_, Setup(_, _, _)).Times(0);
}
void EnrollmentHelperMixin::ExpectEnrollmentMode(
policy::EnrollmentConfig::Mode mode) {
EXPECT_CALL(*mock_, Setup(_, ConfigModeMatches(mode), _));
}
void EnrollmentHelperMixin::ExpectEnrollmentModeRepeated(
policy::EnrollmentConfig::Mode mode) {
EXPECT_CALL(*mock_, Setup(_, ConfigModeMatches(mode), _)).Times(AtLeast(1));
}
void EnrollmentHelperMixin::ExpectAttestationEnrollmentSuccess() {
EXPECT_CALL(*mock_, EnrollUsingAttestation())
.WillOnce(testing::Invoke(
[this]() { mock_->status_consumer()->OnDeviceEnrolled(); }));
}
void EnrollmentHelperMixin::ExpectAttestationEnrollmentError(
policy::EnrollmentStatus status) {
EXPECT_CALL(*mock_, EnrollUsingAttestation())
.WillOnce(testing::Invoke([this, status]() {
mock_->status_consumer()->OnEnrollmentError(status);
}));
}
void EnrollmentHelperMixin::ExpectAttestationEnrollmentErrorRepeated(
policy::EnrollmentStatus status) {
EXPECT_CALL(*mock_, EnrollUsingAttestation())
.Times(AtLeast(1))
.WillRepeatedly(testing::Invoke([this, status]() {
mock_->status_consumer()->OnEnrollmentError(status);
}));
}
void EnrollmentHelperMixin::ExpectOfflineEnrollmentSuccess() {
ExpectEnrollmentMode(policy::EnrollmentConfig::MODE_OFFLINE_DEMO);
EXPECT_CALL(*mock_, EnrollForOfflineDemo())
.WillOnce(testing::Invoke(
[this]() { mock_->status_consumer()->OnDeviceEnrolled(); }));
}
void EnrollmentHelperMixin::ExpectOfflineEnrollmentError(
policy::EnrollmentStatus status) {
ExpectEnrollmentMode(policy::EnrollmentConfig::MODE_OFFLINE_DEMO);
EXPECT_CALL(*mock_, EnrollForOfflineDemo())
.WillOnce(testing::Invoke([this, status]() {
mock_->status_consumer()->OnEnrollmentError(status);
}));
}
void EnrollmentHelperMixin::SetupClearAuth() {
ON_CALL(*mock_, ClearAuth(_))
.WillByDefault(Invoke(
[](base::OnceClosure callback) { std::move(callback).Run(); }));
}
void EnrollmentHelperMixin::ExpectEnrollmentCredentials() {
EXPECT_CALL(*mock_, EnrollUsingAuthCode(kTestAuthCode, _));
}
void EnrollmentHelperMixin::DisableAttributePromptUpdate() {
EXPECT_CALL(*mock_, GetDeviceAttributeUpdatePermission())
.WillOnce(InvokeWithoutArgs([this]() {
mock_->status_consumer()->OnDeviceAttributeUpdatePermission(false);
}));
}
void EnrollmentHelperMixin::ExpectAttributePromptUpdate(
const std::string& asset_id,
const std::string& location) {
// Causes the attribute-prompt flow to activate.
ON_CALL(*mock_, GetDeviceAttributeUpdatePermission())
.WillByDefault(InvokeWithoutArgs([this]() {
mock_->status_consumer()->OnDeviceAttributeUpdatePermission(true);
}));
// Ensures we receive the updates attributes.
EXPECT_CALL(*mock_, UpdateDeviceAttributes(asset_id, location));
}
void EnrollmentHelperMixin::SetupActiveDirectoryJoin(
ActiveDirectoryJoinDelegate* delegate,
const std::string& expected_domain,
const std::string& domain_join_config,
const std::string& dm_token) {
EXPECT_CALL(*mock_, EnrollUsingAuthCode(kTestAuthCode, _))
.WillOnce(InvokeWithoutArgs(
[delegate, expected_domain, domain_join_config, dm_token]() {
delegate->JoinDomain(dm_token, domain_join_config,
base::BindOnce(
[](const std::string& expected_domain,
const std::string& domain) {
ASSERT_EQ(expected_domain, domain);
},
expected_domain));
}));
}
void EnrollmentHelperMixin::ExpectTokenEnrollmentSuccess(
const std::string& token) {
ExpectEnrollmentMode(
policy::EnrollmentConfig::MODE_ATTESTATION_ENROLLMENT_TOKEN);
EXPECT_CALL(*mock_, EnrollUsingEnrollmentToken(token))
.WillOnce(InvokeWithoutArgs(
[this]() { mock_->status_consumer()->OnDeviceEnrolled(); }));
}
} // namespace test
} // namespace chromeos
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_CHROMEOS_LOGIN_TEST_ENROLLMENT_HELPER_MIXIN_H_
#define CHROME_BROWSER_CHROMEOS_LOGIN_TEST_ENROLLMENT_HELPER_MIXIN_H_
#include <string>
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/login/enrollment/enterprise_enrollment_helper.h"
#include "chrome/browser/chromeos/login/mixin_based_in_process_browser_test.h"
#include "chrome/browser/chromeos/policy/enrollment_config.h"
#include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace chromeos {
class EnterpriseEnrollmentHelperMock;
class ActiveDirectoryJoinDelegate;
namespace test {
// This test mixin covers mocking backend interaction during enterprise
// enrollment on EnterpriseEnrollmentHelper level.
class EnrollmentHelperMixin : public InProcessBrowserTestMixin {
public:
static const char kTestAuthCode[];
explicit EnrollmentHelperMixin(InProcessBrowserTestMixinHost* host);
~EnrollmentHelperMixin() override;
// Resets mock (to be used in tests that retry enrollment.
void ResetMock();
// Sets up expectation of no enrollment attempt.
void ExpectNoEnrollment();
// Sets up expectation of enrollment mode.
void ExpectEnrollmentMode(policy::EnrollmentConfig::Mode mode);
void ExpectEnrollmentModeRepeated(policy::EnrollmentConfig::Mode mode);
// Configures and sets expectations for successful attestation-based flow.
void ExpectAttestationEnrollmentSuccess();
// Configures and sets expectations for attestation-based flow resulting in
// error.
void ExpectAttestationEnrollmentError(policy::EnrollmentStatus status);
void ExpectAttestationEnrollmentErrorRepeated(
policy::EnrollmentStatus status);
// Configures and sets expectations for successful offline demo flow.
void ExpectOfflineEnrollmentSuccess();
// Configures and sets expectations for offline demo flow resulting in error.
void ExpectOfflineEnrollmentError(policy::EnrollmentStatus status);
// Sets up expectation of kTestAuthCode as enrollment credentials.
void ExpectEnrollmentCredentials();
// Sets up default ClearAuth handling.
void SetupClearAuth();
// Configures not to show an attribute prompt.
void DisableAttributePromptUpdate();
// Attribute prompt should be displayed during enrollment, and
// |asset_id| / |location| should be sent back to server.
void ExpectAttributePromptUpdate(const std::string& asset_id,
const std::string& location);
// Forces the Active Directory domain join flow during enterprise enrollment.
void SetupActiveDirectoryJoin(ActiveDirectoryJoinDelegate* delegate,
const std::string& expected_domain,
const std::string& domain_join_config,
const std::string& dm_token);
// Sets up expectations for token enrollment.
void ExpectTokenEnrollmentSuccess(const std::string& token);
// InProcessBrowserTestMixin:
void SetUpInProcessBrowserTestFixture() override;
void TearDownInProcessBrowserTestFixture() override;
private:
// Unowned reference to last created mock.
EnterpriseEnrollmentHelperMock* mock_ = nullptr;
base::WeakPtrFactory<EnrollmentHelperMixin> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(EnrollmentHelperMixin);
};
} // namespace test
} // namespace chromeos
#endif // CHROME_BROWSER_CHROMEOS_LOGIN_TEST_ENROLLMENT_HELPER_MIXIN_H_
...@@ -1864,6 +1864,8 @@ test("browser_tests") { ...@@ -1864,6 +1864,8 @@ test("browser_tests") {
"../browser/chromeos/login/session_login_browsertest.cc", "../browser/chromeos/login/session_login_browsertest.cc",
"../browser/chromeos/login/signin/device_id_browsertest.cc", "../browser/chromeos/login/signin/device_id_browsertest.cc",
"../browser/chromeos/login/signin/oauth2_browsertest.cc", "../browser/chromeos/login/signin/oauth2_browsertest.cc",
"../browser/chromeos/login/test/enrollment_helper_mixin.cc",
"../browser/chromeos/login/test/enrollment_helper_mixin.h",
"../browser/chromeos/login/test/hid_controller_mixin.cc", "../browser/chromeos/login/test/hid_controller_mixin.cc",
"../browser/chromeos/login/test/hid_controller_mixin.h", "../browser/chromeos/login/test/hid_controller_mixin.h",
"../browser/chromeos/login/test/https_forwarder.cc", "../browser/chromeos/login/test/https_forwarder.cc",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment