File: ephemeral_kiosk_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (208 lines) | stat: -rw-r--r-- 7,644 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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <memory>
#include <string>
#include <string_view>
#include <tuple>

#include "base/check.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/app_mode/test/kiosk_mixin.h"
#include "chrome/browser/ash/app_mode/test/kiosk_test_utils.h"
#include "chrome/browser/ash/login/test/scoped_policy_update.h"
#include "chrome/browser/ash/policy/core/device_local_account.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "chromeos/ash/components/dbus/userdataauth/fake_userdataauth_client.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "content/public/test/browser_test.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace ash {

using kiosk::test::CachePolicy;
using kiosk::test::TheKioskApp;
using kiosk::test::WaitKioskLaunched;
using KioskEphemeralMode = policy::DeviceLocalAccount::EphemeralMode;

namespace {

// Possible values of the `EphemeralUsersEnabled` policy.
enum class DeviceEphemeralUsersPolicy { kUnset, kEnabled, kDisabled };

// Whether the Kiosk session should be ephemeral based on the given policies.
bool KioskShouldBeEphemeral(DeviceEphemeralUsersPolicy device_ephemeral,
                            KioskEphemeralMode kiosk_ephemeral) {
  switch (kiosk_ephemeral) {
    case KioskEphemeralMode::kUnset:
    case KioskEphemeralMode::kFollowDeviceWidePolicy:
      return device_ephemeral == DeviceEphemeralUsersPolicy::kEnabled;
    case KioskEphemeralMode::kDisable:
      return false;
    case KioskEphemeralMode::kEnable:
      return true;
  }
}

auto ToPolicyProtoValue(KioskEphemeralMode kiosk_ephemeral) {
  switch (kiosk_ephemeral) {
    case KioskEphemeralMode::kUnset:
      return enterprise_management::
          DeviceLocalAccountInfoProto_EphemeralMode_EPHEMERAL_MODE_UNSET;

    case KioskEphemeralMode::kFollowDeviceWidePolicy:
      return enterprise_management::
          DeviceLocalAccountInfoProto_EphemeralMode_EPHEMERAL_MODE_FOLLOW_DEVICE_WIDE_POLICY;

    case KioskEphemeralMode::kDisable:
      return enterprise_management::
          DeviceLocalAccountInfoProto_EphemeralMode_EPHEMERAL_MODE_DISABLE;

    case KioskEphemeralMode::kEnable:
      return enterprise_management::
          DeviceLocalAccountInfoProto_EphemeralMode_EPHEMERAL_MODE_ENABLE;
  }
}

// Sets the `DeviceEphemeralUsersEnabled` policy in `scoped_update`.
void UpdateDevicePolicy(ScopedDevicePolicyUpdate& scoped_update,
                        DeviceEphemeralUsersPolicy device_ephemeral) {
  if (device_ephemeral == DeviceEphemeralUsersPolicy::kUnset) {
    return;
  }

  scoped_update.policy_payload()
      ->mutable_ephemeral_users_enabled()
      ->set_ephemeral_users_enabled(device_ephemeral ==
                                    DeviceEphemeralUsersPolicy::kEnabled);
}

// Sets the `EphemeralMode` policy of device local accounts in `scoped_update`
// for the given `account_id`.
void UpdateDeviceLocalAccountPolicy(ScopedDevicePolicyUpdate& scoped_update,
                                    std::string_view account_id,
                                    KioskEphemeralMode kiosk_ephemeral) {
  if (kiosk_ephemeral == KioskEphemeralMode::kUnset) {
    return;
  }

  auto* accounts = scoped_update.policy_payload()
                       ->mutable_device_local_accounts()
                       ->mutable_account();
  auto account =
      std::ranges::find_if(*accounts, [&account_id](const auto& account) {
        return account.account_id() == account_id;
      });
  CHECK(account != accounts->end());
  account->set_ephemeral_mode(ToPolicyProtoValue(kiosk_ephemeral));
}

std::string_view ToString(DeviceEphemeralUsersPolicy device_ephemeral) {
  switch (device_ephemeral) {
    case DeviceEphemeralUsersPolicy::kUnset:
      return "Unset";
    case DeviceEphemeralUsersPolicy::kEnabled:
      return "Enabled";
    case DeviceEphemeralUsersPolicy::kDisabled:
      return "Disabled";
  }
}

std::string_view ToString(KioskEphemeralMode kiosk_ephemeral) {
  switch (kiosk_ephemeral) {
    case policy::DeviceLocalAccount::EphemeralMode::kUnset:
      return "Unset";
    case policy::DeviceLocalAccount::EphemeralMode::kFollowDeviceWidePolicy:
      return "FollowDeviceWidePolicy";
    case policy::DeviceLocalAccount::EphemeralMode::kDisable:
      return "Disable";
    case policy::DeviceLocalAccount::EphemeralMode::kEnable:
      return "Enable";
  }
}

// Helper alias for the tuple used to combine test parameters.
using TestParam = std::
    tuple<KioskMixin::Config, DeviceEphemeralUsersPolicy, KioskEphemeralMode>;

// Returns the `TestParam` name to be used in gtest.
std::string ParamName(const testing::TestParamInfo<TestParam>& info) {
  auto [config, device_ephemeral, kiosk_ephemeral] = info.param;
  auto config_name =
      config.name.value_or(base::StringPrintf("%zu", info.index));
  return base::StrCat({config_name, "WithDeviceEphemeralUsers",
                       ToString(device_ephemeral), "AndKioskEphemeralMode",
                       ToString(kiosk_ephemeral)});
}

}  // namespace

class EphemeralKioskTest : public MixinBasedInProcessBrowserTest,
                           public testing::WithParamInterface<TestParam> {
 public:
  EphemeralKioskTest() = default;
  EphemeralKioskTest(const EphemeralKioskTest&) = delete;
  EphemeralKioskTest& operator=(const EphemeralKioskTest&) = delete;
  ~EphemeralKioskTest() override = default;

  DeviceEphemeralUsersPolicy DeviceEphemeralUsersParam() const {
    return std::get<DeviceEphemeralUsersPolicy>(GetParam());
  }

  KioskEphemeralMode KioskEphemeralModeParam() const {
    return std::get<KioskEphemeralMode>(GetParam());
  }

  const KioskMixin::Config& ConfigParam() const {
    return std::get<KioskMixin::Config>(GetParam());
  }

  void SetUpInProcessBrowserTestFixture() override {
    MixinBasedInProcessBrowserTest::SetUpInProcessBrowserTestFixture();

    std::string_view kiosk_account_id =
        ConfigParam().auto_launch_account_id.value().value();

    auto update = kiosk_.device_state_mixin().RequestDevicePolicyUpdate();
    UpdateDevicePolicy(*update, DeviceEphemeralUsersParam());
    UpdateDeviceLocalAccountPolicy(*update, kiosk_account_id,
                                   KioskEphemeralModeParam());
  }

  void SetUpOnMainThread() override {
    MixinBasedInProcessBrowserTest::SetUpOnMainThread();
    ASSERT_TRUE(WaitKioskLaunched());
  }

  KioskMixin kiosk_{&mixin_host_,
                    /*cached_configuration=*/ConfigParam()};
};

IN_PROC_BROWSER_TEST_P(EphemeralKioskTest,
                       KioskIsEphemeralWhenPoliciesConfigureIt) {
  bool kiosk_should_be_ephemeral = KioskShouldBeEphemeral(
      DeviceEphemeralUsersParam(), KioskEphemeralModeParam());

  EXPECT_EQ(
      kiosk_should_be_ephemeral,
      FakeUserDataAuthClient::TestApi::Get()->IsCurrentSessionEphemeral());
}

INSTANTIATE_TEST_SUITE_P(
    All,
    EphemeralKioskTest,
    testing::Combine(
        testing::ValuesIn(KioskMixin::ConfigsToAutoLaunchEachAppType()),
        testing::Values(DeviceEphemeralUsersPolicy::kUnset,
                        DeviceEphemeralUsersPolicy::kEnabled,
                        DeviceEphemeralUsersPolicy::kDisabled),
        testing::Values(KioskEphemeralMode::kUnset,
                        KioskEphemeralMode::kFollowDeviceWidePolicy,
                        KioskEphemeralMode::kDisable,
                        KioskEphemeralMode::kEnable)),
    ParamName);

}  // namespace ash