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 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
|
// Copyright 2024 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/app_mode/test/kiosk_mixin.h"
#include <algorithm>
#include <functional>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
#include "ash/constants/ash_switches.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/containers/flat_set.h"
#include "base/files/file_path.h"
#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/test/bind.h"
#include "chrome/browser/app_mode/test/fake_origin_test_server_mixin.h"
#include "chrome/browser/ash/app_mode/fake_cws.h"
#include "chrome/browser/ash/app_mode/fake_cws_mixin.h"
#include "chrome/browser/ash/app_mode/kiosk_test_helper.h"
#include "chrome/browser/ash/login/app_mode/network_ui_controller.h"
#include "chrome/browser/ash/login/test/device_state_mixin.h"
#include "chrome/browser/ash/login/test/scoped_policy_update.h"
#include "chrome/test/base/mixin_based_in_process_browser_test.h"
#include "components/policy/core/common/cloud/test/policy_builder.h"
#include "components/policy/proto/chrome_device_policy.pb.h"
#include "components/web_package/signed_web_bundles/signed_web_bundle_id.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/abseil-cpp/absl/functional/overload.h"
#include "url/gurl.h"
namespace ash {
namespace {
using enterprise_management::DeviceLocalAccountInfoProto;
using enterprise_management::DeviceLocalAccountsProto;
constexpr std::string_view kDefaultWebAppOrigin = "https://kioskmixinapp.com";
constexpr base::FilePath::StringViewType kDefaultWebAppPath =
FILE_PATH_LITERAL("chrome/test/data");
void AppendSwitchesToDisplayLoginScreen(base::CommandLine* command_line) {
command_line->AppendSwitch(switches::kLoginManager);
command_line->AppendSwitch(switches::kForceLoginManagerInTests);
}
std::string_view GetAccountId(const KioskMixin::Option& option) {
return std::visit(
absl::Overload{
[](const KioskMixin::DefaultServerWebAppOption& option) {
return std::string_view(option.account_id);
},
[](const KioskMixin::WebAppOption& option) {
return std::string_view(option.account_id);
},
[](const KioskMixin::CwsChromeAppOption& option) {
return std::string_view(option.account_id);
},
[](const KioskMixin::SelfHostedChromeAppOption& option) {
return std::string_view(option.account_id);
},
[](const KioskMixin::IsolatedWebAppOption& option) {
return std::string_view(option.account_id);
},
},
option);
}
// Returns the Web app URL of the given `option`, or the empty `GURL` if it's
// not a Web app option.
GURL GetWebAppUrl(const KioskMixin::Option& option) {
return std::visit(
absl::Overload{
[](const KioskMixin::DefaultServerWebAppOption& option) {
return GURL(kDefaultWebAppOrigin).Resolve(option.url_path);
},
[](const KioskMixin::WebAppOption& option) { return option.url; },
[](const KioskMixin::CwsChromeAppOption& option) { return GURL(); },
[](const KioskMixin::SelfHostedChromeAppOption& option) {
return GURL();
},
[](const KioskMixin::IsolatedWebAppOption& option) { return GURL(); },
},
option);
}
// Returns the Chrome app ID of the given `option`, or the empty string if it's
// not a Chrome app option.
std::string_view GetChromeAppId(const KioskMixin::Option& option) {
return std::visit(
absl::Overload{
[](const KioskMixin::DefaultServerWebAppOption& option) {
return std::string_view();
},
[](const KioskMixin::WebAppOption& option) {
return std::string_view();
},
[](const KioskMixin::CwsChromeAppOption& option) {
return std::string_view(option.app_id);
},
[](const KioskMixin::SelfHostedChromeAppOption& option) {
return std::string_view(option.app_id);
},
[](const KioskMixin::IsolatedWebAppOption& option) {
return std::string_view();
},
},
option);
}
// Runs multiple checks on `config` to avoid common errors.
void CheckIsValid(const KioskMixin::Config& config) {
// There must be at least one Kiosk app.
CHECK_NE(0ul, config.options.size());
// No two apps can have the same account ID.
auto configured_accounts = base::MakeFlatSet<std::string_view>(
config.options, std::less(), GetAccountId);
CHECK_EQ(configured_accounts.size(), config.options.size());
// If there is an auto launch app, there must also be a config option for it.
if (config.auto_launch_account_id.has_value()) {
CHECK_NE(0l,
std::ranges::count(config.options,
config.auto_launch_account_id.value().value(),
GetAccountId));
}
// No two Web apps can have the same URL.
for (auto it = config.options.begin(); it != config.options.end(); it++) {
if (GURL url = GetWebAppUrl(*it); url.is_valid()) {
CHECK_EQ(1, std::ranges::count(config.options, url, GetWebAppUrl));
}
}
// No two Chrome apps can have the same app ID.
for (auto it = config.options.begin(); it != config.options.end(); it++) {
if (std::string_view app_id = GetChromeAppId(*it); !app_id.empty()) {
CHECK_EQ(1, std::ranges::count(config.options, app_id, GetChromeAppId));
}
}
}
// Configures a Kiosk Chrome App in device policies with the given `app_id`
// and `account_id`.
void ConfigureCwsChromeApp(ScopedDevicePolicyUpdate& update,
std::string_view app_id,
std::string_view account_id) {
DeviceLocalAccountInfoProto* account =
update.policy_payload()->mutable_device_local_accounts()->add_account();
account->set_account_id(std::string(account_id));
account->set_type(DeviceLocalAccountInfoProto::ACCOUNT_TYPE_KIOSK_APP);
account->mutable_kiosk_app()->set_app_id(std::string(app_id));
}
void ConfigureSelfHostedChromeApp(ScopedDevicePolicyUpdate& update,
std::string_view app_id,
std::string_view account_id,
const GURL& update_url) {
DeviceLocalAccountInfoProto* account =
update.policy_payload()->mutable_device_local_accounts()->add_account();
account->set_account_id(std::string(account_id));
account->set_type(DeviceLocalAccountInfoProto::ACCOUNT_TYPE_KIOSK_APP);
account->mutable_kiosk_app()->set_update_url(update_url.spec());
account->mutable_kiosk_app()->set_app_id(std::string(app_id));
}
// Configures a Kiosk web app in device policies with the given `url` and
// `account_id`.
void ConfigureWebApp(ScopedDevicePolicyUpdate& update,
const GURL& url,
std::string_view account_id) {
DeviceLocalAccountInfoProto* account =
update.policy_payload()->mutable_device_local_accounts()->add_account();
account->set_account_id(std::string(account_id));
account->set_type(DeviceLocalAccountInfoProto::ACCOUNT_TYPE_WEB_KIOSK_APP);
account->mutable_web_kiosk_app()->set_url(url.spec());
}
// Configures a Kiosk isolated web app in device policies.
void ConfigureIsolatedWebApp(ScopedDevicePolicyUpdate& update,
const KioskMixin::IsolatedWebAppOption& option) {
DeviceLocalAccountInfoProto* account =
update.policy_payload()->mutable_device_local_accounts()->add_account();
account->set_account_id(std::string(option.account_id));
account->set_type(DeviceLocalAccountInfoProto::ACCOUNT_TYPE_KIOSK_IWA);
account->mutable_isolated_kiosk_app()->set_web_bundle_id(
option.web_bundle_id.id());
account->mutable_isolated_kiosk_app()->set_update_manifest_url(
option.update_manifest_url.spec());
account->mutable_isolated_kiosk_app()->set_update_channel(
option.update_channel);
account->mutable_isolated_kiosk_app()->set_pinned_version(
option.pinned_version);
account->mutable_isolated_kiosk_app()->set_allow_downgrades(
option.allow_downgrades);
}
// Configures the Kiosk account given by `account_id` as the auto launch
// account.
void ConfigureAutoLaunchAccountId(ScopedDevicePolicyUpdate& update,
std::string_view account_id) {
update.policy_payload()->mutable_device_local_accounts()->set_auto_login_id(
std::string(account_id));
}
// Configures the default user policies applied by DM server for Kiosk Web apps
// and IWAs into `update`.
void ConfigureDefaultWebAppUserPolicies(ScopedUserPolicyUpdate& update) {
update.policy_payload()
->mutable_extensioninstallblocklist()
->mutable_value()
->add_entries("*");
}
bool HasChromeApps(KioskMixin::Config config) {
return std::ranges::any_of(config.options, [](const auto& option) {
return std::holds_alternative<KioskMixin::CwsChromeAppOption>(option) ||
std::holds_alternative<KioskMixin::SelfHostedChromeAppOption>(
option);
});
}
} // namespace
KioskMixin::KioskMixin(InProcessBrowserTestMixinHost* host,
std::optional<Config> cached_configuration)
: InProcessBrowserTestMixin(host),
cached_configuration_(std::move(cached_configuration)),
skip_splash_screen_override_(KioskTestHelper::SkipSplashScreenWait()),
network_wait_override_(
NetworkUiController::SetNetworkWaitTimeoutForTesting(
base::TimeDelta())),
web_server_(host, GURL(kDefaultWebAppOrigin), kDefaultWebAppPath),
fake_cws_mixin_(host, FakeCwsMixin::kPublic),
device_state_(
host,
ash::DeviceStateMixin::State::OOBE_COMPLETED_CLOUD_ENROLLED) {
// Chrome apps default to disabled in Kiosk from M138. Re-enable Chrome apps
// in tests that need it. Tests can initialize their own `ScopedFeatureList`
// separately to override this setting if needed.
if (cached_configuration_.has_value() &&
HasChromeApps(cached_configuration_.value())) {
scoped_features_.InitFromCommandLine("AllowChromeAppsInKioskSessions", "");
}
}
KioskMixin::KioskMixin(InProcessBrowserTestMixinHost* host,
Config cached_configuration)
: KioskMixin(host, std::make_optional(std::move(cached_configuration))) {}
KioskMixin::KioskMixin(InProcessBrowserTestMixinHost* host)
: KioskMixin(host, /*cached_configuration=*/{}) {}
KioskMixin::~KioskMixin() = default;
void KioskMixin::SetUpCommandLine(base::CommandLine* command_line) {
AppendSwitchesToDisplayLoginScreen(command_line);
}
void KioskMixin::SetUpInProcessBrowserTestFixture() {
if (cached_configuration_.has_value()) {
Configure(*device_state_.RequestDevicePolicyUpdate().get(),
cached_configuration_.value());
}
}
void KioskMixin::Configure(ScopedDevicePolicyUpdate& device_policy_update,
const Config& config) {
auto user_policy_update_callback =
base::BindLambdaForTesting([this](std::string_view account_id) {
return device_state_.RequestDeviceLocalAccountPolicyUpdate(
std::string(account_id));
});
Configure(device_policy_update, user_policy_update_callback, config);
}
void KioskMixin::Configure(ScopedDevicePolicyUpdate& device_policy_update,
UserPolicyUpdateCallback user_policy_update_callback,
const Config& config) {
CheckIsValid(config);
for (const auto& option : config.options) {
auto account_id = GetAccountId(option);
auto user_policy_update = user_policy_update_callback.Run(account_id);
std::visit(
absl::Overload{
[this, &device_policy_update,
&user_policy_update](const DefaultServerWebAppOption& option) {
ConfigureWebApp(device_policy_update,
web_server_.GetUrl(option.url_path),
option.account_id);
ConfigureDefaultWebAppUserPolicies(*user_policy_update);
},
[&device_policy_update,
&user_policy_update](const WebAppOption& option) {
ConfigureWebApp(device_policy_update, option.url,
option.account_id);
ConfigureDefaultWebAppUserPolicies(*user_policy_update);
},
[this, &device_policy_update](const CwsChromeAppOption& option) {
fake_cws().SetUpdateCrx(option.app_id, option.crx_filename,
option.crx_version);
ConfigureCwsChromeApp(device_policy_update, option.app_id,
option.account_id);
},
[&device_policy_update](const SelfHostedChromeAppOption& option) {
ConfigureSelfHostedChromeApp(device_policy_update, option.app_id,
option.account_id,
option.update_url);
},
[&device_policy_update,
&user_policy_update](const IsolatedWebAppOption& option) {
ConfigureIsolatedWebApp(device_policy_update, option);
ConfigureDefaultWebAppUserPolicies(*user_policy_update);
},
},
option);
}
if (config.auto_launch_account_id.has_value()) {
ConfigureAutoLaunchAccountId(device_policy_update,
config.auto_launch_account_id->value());
}
}
GURL KioskMixin::GetDefaultServerUrl(std::string_view url_suffix) const {
return web_server_.GetUrl(url_suffix);
}
// static
std::vector<KioskMixin::Config> KioskMixin::ConfigsToAutoLaunchEachAppType() {
// TODO(crbug.com/379633748): Add IWA.
return {
Config{/*name=*/"WebApp",
AutoLaunchAccount{SimpleWebAppOption().account_id},
{SimpleWebAppOption()}},
Config{/*name=*/"ChromeApp",
AutoLaunchAccount{SimpleChromeAppOption().account_id},
{SimpleChromeAppOption()}},
};
}
// static
KioskMixin::DefaultServerWebAppOption KioskMixin::SimpleWebAppOption() {
// Serves //chrome/test/data/title3.html.
return DefaultServerWebAppOption{/*account_id=*/"simple-web-app@localhost",
/*url_path=*/"/title3.html"};
}
// static
KioskMixin::CwsChromeAppOption KioskMixin::SimpleChromeAppOption() {
// Configures the Chrome app in:
// //chrome/test/data/chromeos/app_mode/apps_and_extensions/kiosk_test_app.
static constexpr char kChromeAppId[] = "ggaeimfdpnmlhdhpcikgoblffmkckdmn";
return CwsChromeAppOption{
/*account_id=*/"simple-chrome-app@localhost",
/*app_id=*/kChromeAppId,
/*crx_filename=*/base::StrCat({kChromeAppId, ".crx"}),
/*crx_version=*/"1.0.0"};
}
// static
std::string KioskMixin::ConfigName(const testing::TestParamInfo<Config>& info) {
return info.param.name.value_or(base::StringPrintf("%zu", info.index));
}
KioskMixin::DefaultServerWebAppOption::DefaultServerWebAppOption(
std::string_view account_id,
std::string_view url_path)
: account_id(std::string(account_id)), url_path(std::string(url_path)) {}
KioskMixin::DefaultServerWebAppOption::DefaultServerWebAppOption(
const KioskMixin::DefaultServerWebAppOption&) = default;
KioskMixin::DefaultServerWebAppOption::DefaultServerWebAppOption(
KioskMixin::DefaultServerWebAppOption&&) = default;
KioskMixin::DefaultServerWebAppOption&
KioskMixin::DefaultServerWebAppOption::operator=(
const KioskMixin::DefaultServerWebAppOption&) = default;
KioskMixin::DefaultServerWebAppOption&
KioskMixin::DefaultServerWebAppOption::operator=(
KioskMixin::DefaultServerWebAppOption&&) = default;
KioskMixin::DefaultServerWebAppOption::~DefaultServerWebAppOption() = default;
KioskMixin::WebAppOption::WebAppOption(std::string_view account_id, GURL url)
: account_id(std::string(account_id)), url(std::move(url)) {}
KioskMixin::WebAppOption::WebAppOption(const KioskMixin::WebAppOption&) =
default;
KioskMixin::WebAppOption::WebAppOption(KioskMixin::WebAppOption&&) = default;
KioskMixin::WebAppOption& KioskMixin::WebAppOption::operator=(
const KioskMixin::WebAppOption&) = default;
KioskMixin::WebAppOption& KioskMixin::WebAppOption::operator=(
KioskMixin::WebAppOption&&) = default;
KioskMixin::WebAppOption::~WebAppOption() = default;
KioskMixin::CwsChromeAppOption::CwsChromeAppOption(
std::string_view account_id,
std::string_view app_id,
std::string_view crx_filename,
std::string_view crx_version)
: account_id(std::string(account_id)),
app_id(std::string(app_id)),
crx_filename(std::string(crx_filename)),
crx_version(std::string(crx_version)) {}
KioskMixin::CwsChromeAppOption::CwsChromeAppOption(
const KioskMixin::CwsChromeAppOption&) = default;
KioskMixin::CwsChromeAppOption::CwsChromeAppOption(
KioskMixin::CwsChromeAppOption&&) = default;
KioskMixin::CwsChromeAppOption& KioskMixin::CwsChromeAppOption::operator=(
const KioskMixin::CwsChromeAppOption&) = default;
KioskMixin::CwsChromeAppOption& KioskMixin::CwsChromeAppOption::operator=(
KioskMixin::CwsChromeAppOption&&) = default;
KioskMixin::CwsChromeAppOption::~CwsChromeAppOption() = default;
KioskMixin::SelfHostedChromeAppOption::SelfHostedChromeAppOption(
std::string_view account_id,
std::string_view app_id,
const GURL& update_url)
: account_id(std::string(account_id)),
app_id(std::string(app_id)),
update_url(update_url) {}
KioskMixin::SelfHostedChromeAppOption::SelfHostedChromeAppOption(
const KioskMixin::SelfHostedChromeAppOption&) = default;
KioskMixin::SelfHostedChromeAppOption::SelfHostedChromeAppOption(
KioskMixin::SelfHostedChromeAppOption&&) = default;
KioskMixin::SelfHostedChromeAppOption&
KioskMixin::SelfHostedChromeAppOption::operator=(
const KioskMixin::SelfHostedChromeAppOption&) = default;
KioskMixin::SelfHostedChromeAppOption&
KioskMixin::SelfHostedChromeAppOption::operator=(
KioskMixin::SelfHostedChromeAppOption&&) = default;
KioskMixin::SelfHostedChromeAppOption::~SelfHostedChromeAppOption() = default;
KioskMixin::IsolatedWebAppOption::IsolatedWebAppOption(
std::string_view account_id,
const web_package::SignedWebBundleId& web_bundle_id,
GURL update_manifest_url,
std::string update_channel,
std::string pinned_version,
bool allow_downgrades)
: account_id(std::string(account_id)),
web_bundle_id(web_bundle_id),
update_manifest_url(std::move(update_manifest_url)),
update_channel(std::move(update_channel)),
pinned_version(std::move(pinned_version)),
allow_downgrades(allow_downgrades) {}
KioskMixin::IsolatedWebAppOption::IsolatedWebAppOption(
const KioskMixin::IsolatedWebAppOption&) = default;
KioskMixin::IsolatedWebAppOption::IsolatedWebAppOption(
KioskMixin::IsolatedWebAppOption&&) = default;
KioskMixin::IsolatedWebAppOption& KioskMixin::IsolatedWebAppOption::operator=(
const KioskMixin::IsolatedWebAppOption&) = default;
KioskMixin::IsolatedWebAppOption& KioskMixin::IsolatedWebAppOption::operator=(
KioskMixin::IsolatedWebAppOption&&) = default;
KioskMixin::IsolatedWebAppOption::~IsolatedWebAppOption() = default;
KioskMixin::Config::Config(
std::optional<std::string> name,
std::optional<AutoLaunchAccount> auto_launch_account_id,
std::vector<Option> options)
: name(std::move(name)),
auto_launch_account_id(std::move(auto_launch_account_id)),
options(std::move(options)) {}
KioskMixin::Config::Config(const KioskMixin::Config&) = default;
KioskMixin::Config::Config(KioskMixin::Config&&) = default;
KioskMixin::Config& KioskMixin::Config::Config::operator=(
const KioskMixin::Config&) = default;
KioskMixin::Config& KioskMixin::Config::Config::operator=(
KioskMixin::Config&&) = default;
KioskMixin::Config::~Config() = default;
} // namespace ash
|