File: device_command_reboot_job_unittest.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 (452 lines) | stat: -rw-r--r-- 18,488 bytes parent folder | download | duplicates (5)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
// 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/remote_commands/device_command_reboot_job.h"

#include <memory>

#include "ash/constants/ash_pref_names.h"
#include "ash/constants/ash_switches.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_command_line.h"
#include "base/test/test_future.h"
#include "base/time/time.h"
#include "chrome/browser/ash/policy/remote_commands/device_command_reboot_job_test_util.h"
#include "chromeos/dbus/power/fake_power_manager_client.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace em = enterprise_management;

namespace policy {

namespace {

constexpr base::TimeDelta kCommandAge = base::Minutes(5);
constexpr base::TimeDelta kAlmostExpiredCommandAge = base::Minutes(10);

constexpr base::TimeDelta kUserSessionRebootDelay = base::Minutes(3);
constexpr base::TimeDelta kDefaultUserSessionRebootDelay = base::Minutes(5);

}  // namespace

class DeviceCommandRebootJobTest : public DeviceCommandRebootJobTestBase,
                                   public testing::Test {
 protected:
  std::unique_ptr<DeviceCommandRebootJob> CreateAndInitializeCommand(
      base::TimeDelta age_of_command) {
    return DeviceCommandRebootJobTestBase::CreateAndInitializeCommand(
        age_of_command, kUserSessionRebootDelay);
  }

  std::unique_ptr<DeviceCommandRebootJob> CreateAndInitializeCommand() {
    return DeviceCommandRebootJobTestBase::CreateAndInitializeCommand(
        /*age_of_command=*/base::TimeDelta(), kUserSessionRebootDelay);
  }
};

// Test that the command expires after default expiration time.
TEST_F(DeviceCommandRebootJobTest, ExpiresAfterExpirationTime) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  auto old_command = CreateAndInitializeCommand();
  task_environment_.FastForwardBy(kAlmostExpiredCommandAge + base::Seconds(1));

  EXPECT_FALSE(old_command->Run(Now(), NowTicks(),
                                RemoteCommandJob::FinishedCallback()));
  EXPECT_EQ(old_command->status(), RemoteCommandJob::EXPIRED);
}

// Test that the command does not expire before default expiration time.
TEST_F(DeviceCommandRebootJobTest, DoesNotExpireBeforeExpirationTime) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  auto fresh_command = CreateAndInitializeCommand();
  task_environment_.FastForwardBy(kAlmostExpiredCommandAge);

  EXPECT_TRUE(fresh_command->Run(Now(), NowTicks(),
                                 RemoteCommandJob::FinishedCallback()));
}

// Test that the command does not trigger reboot if boot happened after the
// command was issued
TEST_F(DeviceCommandRebootJobTest, DoesNotRebootIfBootedRecently) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  // Boot time is considered as 0 time ticks. Initialize the command so it's
  // issued before boot time but has not expired yet.
  // The implementation relies on base::TimeTicks supporting negative values.
  //   -5min        0min                 1min
  // ---------------------------------------
  //   ^            ^                    ^
  //   |            |                    |
  //   issue_time   boot_time            now

  // Create the command with `kCommandAge` age before boot time.
  auto command = CreateAndInitializeCommand(kCommandAge);

  base::test::TestFuture<void> future;
  EXPECT_TRUE(command->Run(Now(), NowTicks(), future.GetCallback()));

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);
}

// Test that the command instantly reboots the device in a kiosk mode.
TEST_F(DeviceCommandRebootJobTest, RebootsKioskInstantly) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

// Test that the command instantly reboots the device outside of a user session
// on the login screen.
TEST_F(DeviceCommandRebootJobTest, RebootsInstantlyOutsideOfSession) {
  auto scoped_login_state = ScopedLoginState::CreateLoggedOut();

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

// Tests that in the user session, the command does not instantly reboots util a
// user logs out.
TEST_F(DeviceCommandRebootJobTest, RebootsOnUserLogout) {
  auto scoped_login_state = ScopedLoginState::CreateRegularUser();

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Fast forward time a little but before command timeout expires. Check that
  // reboot has not happened yet.
  task_environment_.FastForwardBy(kUserSessionRebootDelay / 2);

  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  session_termination_manager_.StopSession(
      login_manager::SessionStopReason::USER_REQUESTS_SIGNOUT);

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
  EXPECT_FALSE(prefs_->GetBoolean(ash::prefs::kShowPostRebootNotification));
}

// Tests that in the user session, the command shows the notification, waits for
// timeout and only then reboots.
TEST_F(DeviceCommandRebootJobTest,
       ShowsNotificationAndRebootsAfterTimeoutInSession) {
  auto scoped_login_state = ScopedLoginState::CreateRegularUser();

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that reboot notification is shown after timer starts. Fast forward
  // without an actual time so that the timer task is triggered.
  task_environment_.FastForwardBy(base::TimeDelta());
  EXPECT_EQ(fake_notifications_scheduler_->GetShowNotificationCalls(), 1);
  EXPECT_EQ(fake_notifications_scheduler_->GetShowDialogCalls(), 1);

  // Fast forward time a little but before command timeout expires. Check that
  // reboot has not happened yet.
  task_environment_.FastForwardBy(kUserSessionRebootDelay / 2);

  // Before being shown for the first time, the notification scheduler closes
  // itself. That's why we expect 1 instead of 0.
  EXPECT_EQ(fake_notifications_scheduler_->GetCloseNotificationCalls(), 1);

  // Check that the command is still running as timeout is not reached yet.
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  // Finish fastforwarding the timeout.
  task_environment_.FastForwardBy(kUserSessionRebootDelay / 2);
  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
  EXPECT_TRUE(prefs_->GetBoolean(ash::prefs::kShowPostRebootNotification));
  EXPECT_EQ(fake_notifications_scheduler_->GetCloseNotificationCalls(), 2);
}

// Tests that in the user session, the command shows the notification and
// reboots when a user clicks the reboot button.
TEST_F(DeviceCommandRebootJobTest,
       ShowsNotificationAndRebootsWhenUserClicksRestartButtonInSession) {
  auto scoped_login_state = ScopedLoginState::CreateRegularUser();

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that reboot notification is shown after timer starts. Fast forward
  // without an actual time so that the timer task is triggered.
  task_environment_.FastForwardBy(base::TimeDelta());
  EXPECT_EQ(fake_notifications_scheduler_->GetShowNotificationCalls(), 1);
  EXPECT_EQ(fake_notifications_scheduler_->GetShowDialogCalls(), 1);

  // Fast forward time a little but before command timeout expires. Check that
  // reboot has not happened yet.
  task_environment_.FastForwardBy(kUserSessionRebootDelay / 2);

  // Before being shown for the first time, the notification scheduler closes
  // itself. That's why we expect 1 instead of 0.
  EXPECT_EQ(fake_notifications_scheduler_->GetCloseNotificationCalls(), 1);

  // Check that the command is still running as timeout is not reached yet.
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  // Simulate a user clicking the reboot button on dialog and check the reboot
  // is immediately requested.
  fake_notifications_scheduler_->SimulateRebootButtonClick();

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
  EXPECT_FALSE(prefs_->GetBoolean(ash::prefs::kShowPostRebootNotification));
  EXPECT_EQ(fake_notifications_scheduler_->GetCloseNotificationCalls(), 2);
}

// Tests that the command delays execution until power manager availability is
// known, and reboots once power manager is available.
TEST_F(DeviceCommandRebootJobTest, RebootsWhenPowerManagerIsAvailable) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  chromeos::FakePowerManagerClient::Get()->SetServiceAvailability(
      /*availability=*/std::nullopt);

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that command is still running while power manager availability is
  // unknown.
  task_environment_.FastForwardBy(base::TimeDelta());
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  chromeos::FakePowerManagerClient::Get()->SetServiceAvailability(
      /*availability=*/true);

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

// Tests that the command fails when power manager is not initialized.
TEST_F(DeviceCommandRebootJobTest,
       ReportsFailureWhenPowerManagerIsNotInitialized) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  chromeos::FakePowerManagerClient::Get()->Shutdown();
  EXPECT_FALSE(chromeos::PowerManagerClient::Get());

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::FAILED);
}

// Tests that the command fails when power manager is unavailable.
TEST_F(DeviceCommandRebootJobTest,
       ReportsFailureWhenPowerManagerIsUnavailable) {
  auto scoped_login_state = ScopedLoginState::CreateKiosk();

  chromeos::FakePowerManagerClient::Get()->SetServiceAvailability(
      /*availability=*/false);

  auto command = CreateAndInitializeCommand();
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::FAILED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);
}

class DeviceCommandRebootJobPayloadTest : public DeviceCommandRebootJobTestBase,
                                          public testing::Test {
 private:
  ScopedLoginState scoped_login_state_{ScopedLoginState::CreateRegularUser()};
};

TEST_F(DeviceCommandRebootJobPayloadTest, RebootsAfterPayloadDelay) {
  constexpr base::TimeDelta kUserSessionPayloadDelay = base::Minutes(10);
  auto command = CreateAndInitializeCommand(
      /*age_of_command=*/base::TimeDelta(), kUserSessionPayloadDelay);
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check the command does not reboot up until timeout.
  task_environment_.FastForwardBy(kUserSessionPayloadDelay - base::Seconds(1));
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  task_environment_.FastForwardBy(base::Seconds(1));

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

TEST_F(DeviceCommandRebootJobPayloadTest, RebootsInstantlyWithZeroDelay) {
  auto command = CreateAndInitializeCommand(
      /*age_of_command=*/base::TimeDelta(),
      /*user_session_reboot_delay=*/base::TimeDelta());
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  task_environment_.FastForwardBy(base::TimeDelta());

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);

  // Check that notification is not shown but postreboot notification is
  // scheduled.
  EXPECT_EQ(fake_notifications_scheduler_->GetShowNotificationCalls(), 0);
  EXPECT_EQ(fake_notifications_scheduler_->GetShowDialogCalls(), 0);
  EXPECT_TRUE(prefs_->GetBoolean(ash::prefs::kShowPostRebootNotification));
  EXPECT_EQ(fake_notifications_scheduler_->GetCloseNotificationCalls(), 0);
}

TEST_F(DeviceCommandRebootJobPayloadTest, RebootsAfterCommandLineDelay) {
  constexpr base::TimeDelta user_session_reboot_delay = base::Minutes(20);

  base::test::ScopedCommandLine scoped_command_line;
  scoped_command_line.GetProcessCommandLine()->AppendSwitchASCII(
      ash::switches::kRemoteRebootCommandDelayInSecondsForTesting,
      base::NumberToString(user_session_reboot_delay.InSeconds()));
  // Use smaller delay in payload so we can check that reboot happens only
  // after commandline delay.
  auto command = CreateAndInitializeCommand(
      /*age_of_command=*/base::TimeDelta(), user_session_reboot_delay / 2);
  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that reboot does not happen after payload delay and before
  // commandline delay.
  task_environment_.FastForwardBy(user_session_reboot_delay / 2);
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  task_environment_.FastForwardBy(user_session_reboot_delay / 2);

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

TEST_F(DeviceCommandRebootJobPayloadTest, RebootsAfterDefaultDelay) {
  em::RemoteCommand command_proto;
  command_proto.set_type(em::RemoteCommand_Type_DEVICE_REBOOT);
  command_proto.set_command_id(123);
  command_proto.set_age_of_command(0);
  ASSERT_FALSE(command_proto.has_payload());

  auto command = CreateAndInitializeCommand(std::move(command_proto));

  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that reboot does not happen immediately after the command is
  // received.
  task_environment_.FastForwardBy(base::TimeDelta());
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  task_environment_.FastForwardBy(kDefaultUserSessionRebootDelay);

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

class DeviceCommandRebootJobInvalidPayloadTest
    : public DeviceCommandRebootJobPayloadTest,
      public testing::WithParamInterface<std::string> {
 public:
  std::string payload() const { return GetParam(); }
};

TEST_P(DeviceCommandRebootJobInvalidPayloadTest, RebootsWithInvalidPayload) {
  em::RemoteCommand command_proto;
  command_proto.set_type(em::RemoteCommand_Type_DEVICE_REBOOT);
  command_proto.set_command_id(123);
  command_proto.set_age_of_command(0);
  command_proto.set_payload(std::string(payload()));

  auto command = CreateAndInitializeCommand(std::move(command_proto));

  base::test::TestFuture<void> future;
  command->Run(Now(), NowTicks(), future.GetCallback());

  // Check that reboot does not happen immediately after the command is
  // received.
  task_environment_.FastForwardBy(base::TimeDelta());
  EXPECT_EQ(command->status(), RemoteCommandJob::RUNNING);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 0);

  task_environment_.FastForwardBy(kDefaultUserSessionRebootDelay);

  ASSERT_TRUE(future.Wait());
  EXPECT_EQ(command->status(), RemoteCommandJob::SUCCEEDED);
  EXPECT_EQ(
      chromeos::FakePowerManagerClient::Get()->num_request_restart_calls(), 1);
}

INSTANTIATE_TEST_SUITE_P(
    InvalidPayloads,
    DeviceCommandRebootJobInvalidPayloadTest,
    testing::Values("not_a_json",
                    "{}",
                    R"({"not_delay_field": 10})",
                    R"({"user_session_delay_seconds": false})",
                    R"({"user_session_delay_seconds": -1})",
                    R"({"user_session_delay_seconds": 1.0})",
                    R"({"user_session_delay_seconds": 1.10})",
                    R"({"user_session_delay_seconds": -1.0})",
                    R"({"user_session_delay_seconds": -1.10})",
                    base::StringPrintf(R"({"user_session_delay_seconds": %ld})",
                                       base::TimeDelta::Max().InSeconds()),
                    base::StringPrintf(R"({"user_session_delay_seconds": %ld})",
                                       base::TimeDelta::Min().InSeconds())));

}  // namespace policy