File: device_disabling_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (272 lines) | stat: -rw-r--r-- 10,688 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <memory>
#include <string>

#include "ash/constants/ash_switches.h"
#include "ash/public/cpp/login_screen_test_api.h"
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/memory/scoped_refptr.h"
#include "base/run_loop.h"
#include "chrome/browser/ash/login/login_wizard.h"
#include "chrome/browser/ash/login/test/device_state_mixin.h"
#include "chrome/browser/ash/login/test/login_manager_mixin.h"
#include "chrome/browser/ash/login/test/network_portal_detector_mixin.h"
#include "chrome/browser/ash/login/test/oobe_base_test.h"
#include "chrome/browser/ash/login/test/oobe_screen_waiter.h"
#include "chrome/browser/ash/login/test/scoped_policy_update.h"
#include "chrome/browser/ash/login/wizard_controller.h"
#include "chrome/browser/ash/system/device_disabling_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/ui/ash/login/login_display_host.h"
#include "chrome/browser/ui/webui/ash/login/device_disabled_screen_handler.h"
#include "chrome/browser/ui/webui/ash/login/network_state_informer.h"
#include "chrome/browser/ui/webui/ash/login/oobe_ui.h"
#include "chromeos/ash/components/dbus/session_manager/fake_session_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "chromeos/ash/components/dbus/shill/shill_service_client.h"
#include "chromeos/ash/components/settings/cros_settings.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "dbus/object_path.h"

namespace ash {
namespace system {

namespace {

void ErrorCallbackFunction(const std::string& error_name,
                           const std::string& error_message) {
  LOG(ERROR) << "Shill Error: " << error_name << " : " << error_message;
}

bool DeviceDisabledScreenShown() {
  WizardController* const wizard_controller =
      WizardController::default_controller();
  EXPECT_TRUE(wizard_controller);
  return wizard_controller &&
         wizard_controller->current_screen() ==
             wizard_controller->GetScreen(DeviceDisabledScreenView::kScreenId);
}

}  // namespace

class DeviceDisablingTest
    : public OobeBaseTest,
      public NetworkStateInformer::NetworkStateInformerObserver {
 public:
  DeviceDisablingTest() = default;

  DeviceDisablingTest(const DeviceDisablingTest&) = delete;
  DeviceDisablingTest& operator=(const DeviceDisablingTest&) = delete;

  // Sets up a device state blob that indicates the device is disabled.
  void SetDeviceDisabledPolicy();

  // Sets up a device state blob that indicates the device is disabled, triggers
  // a policy plus device state fetch and waits for it to succeed.
  void MarkDisabledAndWaitForPolicyFetch();

  std::string GetCurrentScreenName(content::WebContents* web_contents);

 protected:
  // OobeBaseTest:
  void SetUpOnMainThread() override;

  // NetworkStateInformer::NetworkStateInformerObserver:
  void UpdateState(NetworkError::ErrorReason reason) override;

  std::unique_ptr<base::RunLoop> network_state_change_wait_run_loop_;
  NetworkPortalDetectorMixin network_portal_detector_{&mixin_host_};

 private:
  DeviceStateMixin device_state_{
      &mixin_host_, DeviceStateMixin::State::OOBE_COMPLETED_CLOUD_ENROLLED};
};

void DeviceDisablingTest::SetDeviceDisabledPolicy() {
  // Prepare a policy fetch response that indicates the device is disabled.
  std::unique_ptr<ScopedDevicePolicyUpdate> policy_update =
      device_state_.RequestDevicePolicyUpdate();
  policy_update->policy_data()->mutable_device_state()->set_device_mode(
      enterprise_management::DeviceState::DEVICE_MODE_DISABLED);
}

void DeviceDisablingTest::MarkDisabledAndWaitForPolicyFetch() {
  base::RunLoop run_loop;
  // Set up an observer that will wait for the disabled setting to change.
  base::CallbackListSubscription subscription =
      CrosSettings::Get()->AddSettingsObserver(kDeviceDisabled,
                                               run_loop.QuitClosure());
  SetDeviceDisabledPolicy();
  // Trigger a policy fetch.
  FakeSessionManagerClient::Get()->OnPropertyChangeComplete(true);
  // Wait for the policy fetch to complete and the disabled setting to change.
  run_loop.Run();
}

void DeviceDisablingTest::SetUpOnMainThread() {
  network_state_change_wait_run_loop_ = std::make_unique<base::RunLoop>();

  OobeBaseTest::SetUpOnMainThread();

  // Set up fake networks.
  ShillManagerClient::Get()->GetTestInterface()->SetupDefaultEnvironment();
}

void DeviceDisablingTest::UpdateState(NetworkError::ErrorReason reason) {
  network_state_change_wait_run_loop_->Quit();
}

IN_PROC_BROWSER_TEST_F(DeviceDisablingTest, DisableDuringNormalOperation) {
  MarkDisabledAndWaitForPolicyFetch();
  // Check for WizardController state.
  OobeScreenWaiter(DeviceDisabledScreenView::kScreenId).Wait();

  EXPECT_TRUE(ash::LoginScreenTestApi::IsOobeDialogVisible());
}

// Verifies that device disabling works when the ephemeral users policy is
// enabled. This case warrants its own test because the UI behaves somewhat
// differently when the policy is set: A background job runs on startup that
// causes the UI to try and show the login screen after some delay. It must
// be ensured that the login screen does not show and does not clobber the
// disabled screen.
IN_PROC_BROWSER_TEST_F(DeviceDisablingTest, DisableWithEphemeralUsers) {
  // Connect to the fake Ethernet network. This ensures that Chrome OS will not
  // try to show the offline error screen.
  base::RunLoop connect_run_loop;
  ShillServiceClient::Get()->Connect(dbus::ObjectPath("/service/eth1"),
                                     connect_run_loop.QuitClosure(),
                                     base::BindOnce(&ErrorCallbackFunction));
  connect_run_loop.Run();

  // Skip to the login screen.
  OobeScreenWaiter(GetFirstSigninScreen()).Wait();

  // Mark the device as disabled and wait until cros settings update.
  MarkDisabledAndWaitForPolicyFetch();

  // Check for WizardController state.
  OobeScreenWaiter(DeviceDisabledScreenView::kScreenId).Wait();

  // Disconnect from the fake Ethernet network.
  const LoginDisplayHost* host = LoginDisplayHost::default_host();
  OobeUI* const oobe_ui = host->GetOobeUI();
  ASSERT_TRUE(oobe_ui);
  const scoped_refptr<NetworkStateInformer> network_state_informer =
      oobe_ui->network_state_informer_for_test();
  ASSERT_TRUE(network_state_informer);
  network_state_informer->AddObserver(this);
  network_portal_detector_.SimulateDefaultNetworkState(
      NetworkPortalDetectorMixin::NetworkStatus::kOffline);
  network_state_change_wait_run_loop_->Run();
  network_state_informer->RemoveObserver(this);
  base::RunLoop().RunUntilIdle();

  // Verify that the offline error screen was not shown and the device disabled
  // screen is still being shown instead.
  // Check for WizardController state.
  OobeScreenWaiter(DeviceDisabledScreenView::kScreenId).Wait();
}

class DeviceDisablingWithUsersTest : public DeviceDisablingTest {
 public:
  DeviceDisablingWithUsersTest() { login_manager_.AppendRegularUsers(2); }

 private:
  LoginManagerMixin login_manager_{&mixin_host_};
};

// Checks that OOBE dialog is not hidden when the device disabled screen is
// shown and "StartSignInScreen" is called.
IN_PROC_BROWSER_TEST_F(DeviceDisablingWithUsersTest, DialogNotHidden) {
  EXPECT_TRUE(ash::LoginScreenTestApi::ClickAddUserButton());
  EXPECT_TRUE(ash::LoginScreenTestApi::IsOobeDialogVisible());
  OobeScreenWaiter(GetFirstSigninScreen()).Wait();
  MarkDisabledAndWaitForPolicyFetch();
  OobeScreenWaiter(DeviceDisabledScreenView::kScreenId).Wait();
  LoginDisplayHost::default_host()->StartSignInScreen();

  // Dialog should not be hidden.
  EXPECT_TRUE(ash::LoginScreenTestApi::IsOobeDialogVisible());
}

// Sets the device disabled policy before the browser is started.
class PresetPolicyDeviceDisablingTest : public DeviceDisablingTest {
 public:
  PresetPolicyDeviceDisablingTest() = default;

  PresetPolicyDeviceDisablingTest(const PresetPolicyDeviceDisablingTest&) =
      delete;
  PresetPolicyDeviceDisablingTest& operator=(
      const PresetPolicyDeviceDisablingTest&) = delete;

 protected:
  // DeviceDisablingTest:
  void SetUpInProcessBrowserTestFixture() override {
    DeviceDisablingTest::SetUpInProcessBrowserTestFixture();
    SetDeviceDisabledPolicy();
  }
};

// Same test as the one in DeviceDisablingTest, except the policy is being set
// before Chrome process is started. This test covers a crash (crbug.com/709518)
// in DeviceDisabledScreen where it would try to access DeviceDisablingManager
// even though it wasn't yet constructed fully.
IN_PROC_BROWSER_TEST_F(PresetPolicyDeviceDisablingTest,
                       DisableBeforeStartup) {
  EXPECT_TRUE(DeviceDisabledScreenShown());
  EXPECT_TRUE(ash::LoginScreenTestApi::IsOobeDialogVisible());
}

class DeviceDisablingBeforeLoginHostCreated
    : public PresetPolicyDeviceDisablingTest {
 public:
  DeviceDisablingBeforeLoginHostCreated() {
    // Start with user pods.
    login_mixin_.AppendManagedUsers(2);
  }

  bool SetUpUserDataDirectory() override {
    // LoginManagerMixin sets up command line in the SetUpUserDataDirectory.
    if (!PresetPolicyDeviceDisablingTest::SetUpUserDataDirectory())
      return false;
    // Postpone login host creation.
    base::CommandLine::ForCurrentProcess()->RemoveSwitch(
        switches::kForceLoginManagerInTests);
    return true;
  }

  bool ShouldWaitForOobeUI() override { return false; }

 protected:
  LoginManagerMixin login_mixin_{&mixin_host_};
};

// Sometimes LoginHost creation postponed (e.g. due to language switch
// https://crbug.com/1065569). This tests checks this flow.
IN_PROC_BROWSER_TEST_F(DeviceDisablingBeforeLoginHostCreated,
                       ShowsDisabledScreen) {
  EXPECT_TRUE(
      system::DeviceDisablingManager::IsDeviceDisabledDuringNormalOperation());
  EXPECT_EQ(nullptr, LoginDisplayHost::default_host());
  EXPECT_NE(nullptr,
            g_browser_process->platform_part()->device_disabling_manager());
  ShowLoginWizard(ash::OOBE_SCREEN_UNKNOWN);
  // Check for WizardController state.
  OobeScreenWaiter(DeviceDisabledScreenView::kScreenId).Wait();

  EXPECT_TRUE(ash::LoginScreenTestApi::IsOobeDialogVisible());
}

}  // namespace system
}  // namespace ash