OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include <memory> |
| 6 #include <string> |
| 7 #include <vector> |
| 8 |
| 9 #include "apps/test/app_window_waiter.h" |
| 10 #include "base/base64.h" |
| 11 #include "base/command_line.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/logging.h" |
| 15 #include "base/path_service.h" |
| 16 #include "base/run_loop.h" |
| 17 #include "base/values.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" |
| 19 #include "chrome/browser/chromeos/app_mode/fake_cws.h" |
| 20 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| 21 #include "chrome/browser/chromeos/login/app_launch_controller.h" |
| 22 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact
ory.h" |
| 23 #include "chrome/browser/chromeos/policy/device_local_account.h" |
| 24 #include "chrome/browser/chromeos/policy/device_policy_builder.h" |
| 25 #include "chrome/browser/chromeos/settings/stub_install_attributes.h" |
| 26 #include "chrome/browser/extensions/browsertest_util.h" |
| 27 #include "chrome/browser/extensions/extension_apitest.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/common/chrome_constants.h" |
| 30 #include "chrome/common/chrome_paths.h" |
| 31 #include "chrome/common/pref_names.h" |
| 32 #include "chromeos/dbus/cryptohome_client.h" |
| 33 #include "chromeos/dbus/dbus_thread_manager.h" |
| 34 #include "chromeos/dbus/fake_session_manager_client.h" |
| 35 #include "chromeos/dbus/shill_manager_client.h" |
| 36 #include "components/ownership/mock_owner_key_util.h" |
| 37 #include "content/public/browser/notification_observer.h" |
| 38 #include "content/public/browser/notification_registrar.h" |
| 39 #include "content/public/browser/notification_service.h" |
| 40 #include "extensions/browser/app_window/app_window.h" |
| 41 #include "extensions/browser/app_window/app_window_registry.h" |
| 42 #include "extensions/browser/app_window/native_app_window.h" |
| 43 #include "extensions/common/value_builder.h" |
| 44 #include "extensions/test/extension_test_message_listener.h" |
| 45 #include "net/dns/mock_host_resolver.h" |
| 46 #include "third_party/cros_system_api/switches/chrome_switches.h" |
| 47 |
| 48 namespace em = enterprise_management; |
| 49 |
| 50 namespace chromeos { |
| 51 |
| 52 namespace { |
| 53 |
| 54 // This is a simple test app that creates an app window and immediately closes |
| 55 // it again. Webstore data json is in |
| 56 // chrome/test/data/chromeos/app_mode/webstore/inlineinstall/ |
| 57 // detail/ggbflgnkafappblpkiflbgpmkfdpnhhe |
| 58 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; |
| 59 |
| 60 const char kTestAccountId[] = "enterprise-kiosk-app@localhost"; |
| 61 |
| 62 const char kSessionManagerStateCache[] = "test_session_manager_state.json"; |
| 63 |
| 64 // Keys for values in dictionary used to preserve session manager state. |
| 65 const char kLoginArgsKey[] = "login_args"; |
| 66 const char kExtraArgsKey[] = "extra_args"; |
| 67 const char kArgNameKey[] = "name"; |
| 68 const char kArgValueKey[] = "value"; |
| 69 |
| 70 // Default set policy switches. |
| 71 const struct { |
| 72 const char* name; |
| 73 const char* value; |
| 74 } kDefaultPolicySwitches[] = {{"test_switch_1", ""}, |
| 75 {"test_switch_2", "test_switch_2_value"}}; |
| 76 |
| 77 // Fake session manager implementation that persists its state in local file. |
| 78 // It can be used to preserve session state in PRE_ browser tests. |
| 79 // Primarily used for testing user/login switches. |
| 80 class PersistentSessionManagerClient : public FakeSessionManagerClient { |
| 81 public: |
| 82 PersistentSessionManagerClient() {} |
| 83 |
| 84 ~PersistentSessionManagerClient() override { |
| 85 PersistFlagsToFile(backing_file_); |
| 86 } |
| 87 |
| 88 // Initializes session state (primarily session flags)- if |backing_file| |
| 89 // exists, the session state is restored from the file value. Otherwise it's |
| 90 // set to the default session state. |
| 91 void Initialize(const base::FilePath& backing_file) { |
| 92 backing_file_ = backing_file; |
| 93 |
| 94 if (ExtractFlagsFromFile(backing_file_)) |
| 95 return; |
| 96 |
| 97 // Failed to extract ached flags - set the default values. |
| 98 login_args_ = {{"login-manager", ""}}; |
| 99 |
| 100 extra_args_ = {{switches::kPolicySwitchesBegin, ""}}; |
| 101 for (size_t i = 0; i < arraysize(kDefaultPolicySwitches); ++i) { |
| 102 extra_args_.push_back( |
| 103 {kDefaultPolicySwitches[i].name, kDefaultPolicySwitches[i].value}); |
| 104 } |
| 105 extra_args_.push_back({switches::kPolicySwitchesEnd, ""}); |
| 106 } |
| 107 |
| 108 void AppendSwitchesToCommandLine(base::CommandLine* command_line) { |
| 109 for (const auto& flag : login_args_) |
| 110 command_line->AppendSwitchASCII(flag.name, flag.value); |
| 111 for (const auto& flag : extra_args_) |
| 112 command_line->AppendSwitchASCII(flag.name, flag.value); |
| 113 } |
| 114 |
| 115 void StartSession(const cryptohome::Identification& cryptohome_id) override { |
| 116 FakeSessionManagerClient::StartSession(cryptohome_id); |
| 117 |
| 118 std::string user_id_hash = |
| 119 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id); |
| 120 login_args_ = {{"login-user", cryptohome_id.id()}, |
| 121 {"login-profile", user_id_hash}}; |
| 122 } |
| 123 |
| 124 void StopSession() override { |
| 125 FakeSessionManagerClient::StopSession(); |
| 126 |
| 127 login_args_ = {{"login-manager", ""}}; |
| 128 } |
| 129 |
| 130 bool SupportsRestartToApplyUserFlags() const override { return true; } |
| 131 |
| 132 void SetFlagsForUser(const cryptohome::Identification& identification, |
| 133 const std::vector<std::string>& flags) override { |
| 134 extra_args_.clear(); |
| 135 FakeSessionManagerClient::SetFlagsForUser(identification, flags); |
| 136 |
| 137 std::vector<std::string> argv = {"" /* Empty program */}; |
| 138 argv.insert(argv.end(), flags.begin(), flags.end()); |
| 139 |
| 140 // Parse flag name-value pairs using command line initialization. |
| 141 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); |
| 142 cmd_line.InitFromArgv(argv); |
| 143 |
| 144 for (const auto& flag : cmd_line.GetSwitches()) |
| 145 extra_args_.push_back({flag.first, flag.second}); |
| 146 } |
| 147 |
| 148 private: |
| 149 // Keeps information about a switch - its name and value. |
| 150 struct Switch { |
| 151 std::string name; |
| 152 std::string value; |
| 153 }; |
| 154 |
| 155 bool ExtractFlagsFromFile(const base::FilePath& backing_file) { |
| 156 JSONFileValueDeserializer deserializer(backing_file); |
| 157 |
| 158 int error_code = 0; |
| 159 std::unique_ptr<base::Value> value = |
| 160 deserializer.Deserialize(&error_code, nullptr); |
| 161 if (error_code != JSONFileValueDeserializer::JSON_NO_ERROR) |
| 162 return false; |
| 163 |
| 164 std::unique_ptr<base::DictionaryValue> value_dict = |
| 165 base::DictionaryValue::From(std::move(value)); |
| 166 CHECK(value_dict); |
| 167 |
| 168 CHECK(InitArgListFromCachedValue(*value_dict, kLoginArgsKey, &login_args_)); |
| 169 CHECK(InitArgListFromCachedValue(*value_dict, kExtraArgsKey, &extra_args_)); |
| 170 return true; |
| 171 } |
| 172 |
| 173 bool PersistFlagsToFile(const base::FilePath& backing_file) { |
| 174 base::DictionaryValue cached_state; |
| 175 cached_state.Set(kLoginArgsKey, GetArgListValue(login_args_)); |
| 176 cached_state.Set(kExtraArgsKey, GetArgListValue(extra_args_)); |
| 177 |
| 178 JSONFileValueSerializer serializer(backing_file); |
| 179 return serializer.Serialize(cached_state); |
| 180 } |
| 181 |
| 182 std::unique_ptr<base::ListValue> GetArgListValue( |
| 183 const std::vector<Switch>& args) { |
| 184 std::unique_ptr<base::ListValue> result(new base::ListValue()); |
| 185 for (const auto& arg : args) { |
| 186 result->Append(extensions::DictionaryBuilder() |
| 187 .Set(kArgNameKey, arg.name) |
| 188 .Set(kArgValueKey, arg.value) |
| 189 .Build()); |
| 190 } |
| 191 return result; |
| 192 } |
| 193 |
| 194 bool InitArgListFromCachedValue(const base::DictionaryValue& cache_value, |
| 195 const std::string& list_key, |
| 196 std::vector<Switch>* arg_list_out) { |
| 197 arg_list_out->clear(); |
| 198 const base::ListValue* arg_list_value; |
| 199 if (!cache_value.GetList(list_key, &arg_list_value)) |
| 200 return false; |
| 201 for (size_t i = 0; i < arg_list_value->GetSize(); ++i) { |
| 202 const base::DictionaryValue* arg_value; |
| 203 if (!arg_list_value->GetDictionary(i, &arg_value)) |
| 204 return false; |
| 205 Switch arg; |
| 206 if (!arg_value->GetStringASCII(kArgNameKey, &arg.name) || |
| 207 !arg_value->GetStringASCII(kArgValueKey, &arg.value)) { |
| 208 return false; |
| 209 } |
| 210 arg_list_out->push_back(arg); |
| 211 } |
| 212 return true; |
| 213 } |
| 214 |
| 215 std::vector<Switch> login_args_; |
| 216 std::vector<Switch> extra_args_; |
| 217 |
| 218 base::FilePath backing_file_; |
| 219 |
| 220 DISALLOW_COPY_AND_ASSIGN(PersistentSessionManagerClient); |
| 221 }; |
| 222 |
| 223 // Used to listen for app termination notification. |
| 224 class TerminationObserver : public content::NotificationObserver { |
| 225 public: |
| 226 TerminationObserver() { |
| 227 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 228 content::NotificationService::AllSources()); |
| 229 } |
| 230 ~TerminationObserver() override = default; |
| 231 |
| 232 // Whether app has been terminated - i.e. whether app termination notification |
| 233 // has been observed. |
| 234 bool terminated() const { return notification_seen_; } |
| 235 |
| 236 private: |
| 237 void Observe(int type, |
| 238 const content::NotificationSource& source, |
| 239 const content::NotificationDetails& details) override { |
| 240 ASSERT_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| 241 notification_seen_ = true; |
| 242 } |
| 243 |
| 244 bool notification_seen_ = false; |
| 245 content::NotificationRegistrar registrar_; |
| 246 |
| 247 DISALLOW_COPY_AND_ASSIGN(TerminationObserver); |
| 248 }; |
| 249 |
| 250 } // namespace |
| 251 |
| 252 class AutoLaunchedKioskTest : public ExtensionApiTest { |
| 253 public: |
| 254 AutoLaunchedKioskTest() |
| 255 : install_attributes_( |
| 256 chromeos::ScopedStubInstallAttributes::CreateEnterprise( |
| 257 "domain.com", |
| 258 "device_id")), |
| 259 owner_key_util_(new ownership::MockOwnerKeyUtil()), |
| 260 fake_session_manager_(new PersistentSessionManagerClient()), |
| 261 fake_cws_(new FakeCWS) { |
| 262 set_chromeos_user_ = false; |
| 263 } |
| 264 |
| 265 ~AutoLaunchedKioskTest() override = default; |
| 266 |
| 267 void SetUp() override { |
| 268 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 269 AppLaunchController::SkipSplashWaitForTesting(); |
| 270 |
| 271 ExtensionApiTest::SetUp(); |
| 272 } |
| 273 |
| 274 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 275 fake_cws_->Init(embedded_test_server()); |
| 276 fake_cws_->SetUpdateCrx(kTestKioskApp, std::string(kTestKioskApp) + ".crx", |
| 277 "1.0.0"); |
| 278 ExtensionApiTest::SetUpCommandLine(command_line); |
| 279 } |
| 280 |
| 281 bool SetUpUserDataDirectory() override { |
| 282 InitDevicePolicy(); |
| 283 |
| 284 base::FilePath user_data_path; |
| 285 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_path)) { |
| 286 ADD_FAILURE() << "Unable to get used data dir"; |
| 287 return false; |
| 288 } |
| 289 |
| 290 if (!CacheDevicePolicyToLocalState(user_data_path)) |
| 291 return false; |
| 292 |
| 293 // Restore session_manager state and ensure session manager flags are |
| 294 // applied. |
| 295 fake_session_manager_->Initialize( |
| 296 user_data_path.Append(kSessionManagerStateCache)); |
| 297 fake_session_manager_->AppendSwitchesToCommandLine( |
| 298 base::CommandLine::ForCurrentProcess()); |
| 299 |
| 300 return true; |
| 301 } |
| 302 |
| 303 void SetUpInProcessBrowserTestFixture() override { |
| 304 host_resolver()->AddRule("*", "127.0.0.1"); |
| 305 |
| 306 OwnerSettingsServiceChromeOSFactory::GetInstance() |
| 307 ->SetOwnerKeyUtilForTesting(owner_key_util_); |
| 308 owner_key_util_->SetPublicKeyFromPrivateKey( |
| 309 *device_policy_.GetSigningKey()); |
| 310 |
| 311 fake_session_manager_->set_device_policy(device_policy_.GetBlob()); |
| 312 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 313 std::move(fake_session_manager_)); |
| 314 |
| 315 ExtensionApiTest::SetUpInProcessBrowserTestFixture(); |
| 316 } |
| 317 |
| 318 void SetUpOnMainThread() override { |
| 319 extensions::browsertest_util::CreateAndInitializeLocalCache(); |
| 320 |
| 321 embedded_test_server()->StartAcceptingConnections(); |
| 322 |
| 323 ExtensionApiTest::SetUpOnMainThread(); |
| 324 } |
| 325 |
| 326 void RunTestOnMainThreadLoop() override { |
| 327 termination_observer_.reset(new TerminationObserver()); |
| 328 |
| 329 ExtensionApiTest::RunTestOnMainThreadLoop(); |
| 330 } |
| 331 |
| 332 void TearDownOnMainThread() override { |
| 333 termination_observer_.reset(); |
| 334 |
| 335 ExtensionApiTest::TearDownOnMainThread(); |
| 336 } |
| 337 |
| 338 void InitDevicePolicy() { |
| 339 // Create device policy, and cache it to local state. |
| 340 em::DeviceLocalAccountsProto* const device_local_accounts = |
| 341 device_policy_.payload().mutable_device_local_accounts(); |
| 342 |
| 343 em::DeviceLocalAccountInfoProto* const account = |
| 344 device_local_accounts->add_account(); |
| 345 account->set_account_id(kTestAccountId); |
| 346 account->set_type(em::DeviceLocalAccountInfoProto::ACCOUNT_TYPE_KIOSK_APP); |
| 347 account->mutable_kiosk_app()->set_app_id(kTestKioskApp); |
| 348 |
| 349 device_local_accounts->set_auto_login_id(kTestAccountId); |
| 350 |
| 351 device_policy_.Build(); |
| 352 } |
| 353 |
| 354 bool CacheDevicePolicyToLocalState(const base::FilePath& user_data_path) { |
| 355 em::PolicyData policy_data; |
| 356 if (!device_policy_.payload().SerializeToString( |
| 357 policy_data.mutable_policy_value())) { |
| 358 ADD_FAILURE() << "Failed to serialize device policy."; |
| 359 return false; |
| 360 } |
| 361 const std::string policy_data_str = policy_data.SerializeAsString(); |
| 362 std::string policy_data_encoded; |
| 363 base::Base64Encode(policy_data_str, &policy_data_encoded); |
| 364 |
| 365 std::unique_ptr<base::DictionaryValue> local_state = |
| 366 extensions::DictionaryBuilder() |
| 367 .Set(prefs::kDeviceSettingsCache, policy_data_encoded) |
| 368 .Set("PublicAccounts", |
| 369 extensions::ListBuilder().Append(GetTestAppUserId()).Build()) |
| 370 .Build(); |
| 371 |
| 372 JSONFileValueSerializer serializer( |
| 373 user_data_path.Append(chrome::kLocalStateFilename)); |
| 374 if (!serializer.Serialize(*local_state)) { |
| 375 ADD_FAILURE() << "Failed to write local state."; |
| 376 return false; |
| 377 } |
| 378 return true; |
| 379 } |
| 380 |
| 381 const std::string GetTestAppUserId() const { |
| 382 return policy::GenerateDeviceLocalAccountUserId( |
| 383 kTestAccountId, policy::DeviceLocalAccount::TYPE_KIOSK_APP); |
| 384 } |
| 385 |
| 386 bool CloseAppWindow(const std::string& app_id) { |
| 387 Profile* const app_profile = ProfileManager::GetPrimaryUserProfile(); |
| 388 if (!app_profile) { |
| 389 ADD_FAILURE() << "No primary (app) profile."; |
| 390 return false; |
| 391 } |
| 392 |
| 393 extensions::AppWindowRegistry* const app_window_registry = |
| 394 extensions::AppWindowRegistry::Get(app_profile); |
| 395 extensions::AppWindow* const window = |
| 396 apps::AppWindowWaiter(app_window_registry, app_id).Wait(); |
| 397 if (!window) { |
| 398 ADD_FAILURE() << "No app window found for " << app_id << "."; |
| 399 return false; |
| 400 } |
| 401 |
| 402 window->GetBaseWindow()->Close(); |
| 403 |
| 404 // Wait until the app terminates if it is still running. |
| 405 if (!app_window_registry->GetAppWindowsForApp(app_id).empty()) |
| 406 base::RunLoop().Run(); |
| 407 return true; |
| 408 } |
| 409 |
| 410 bool IsKioskAppAutoLaunched(const std::string& app_id) { |
| 411 KioskAppManager::App app; |
| 412 if (!KioskAppManager::Get()->GetApp(app_id, &app)) { |
| 413 ADD_FAILURE() << "App " << app_id << " not found."; |
| 414 return false; |
| 415 } |
| 416 return app.was_auto_launched_with_zero_delay; |
| 417 } |
| 418 |
| 419 void ExpectCommandLineHasDefaultPolicySwitches( |
| 420 const base::CommandLine& cmd_line) { |
| 421 for (size_t i = 0u; i < arraysize(kDefaultPolicySwitches); ++i) { |
| 422 EXPECT_TRUE(cmd_line.HasSwitch(kDefaultPolicySwitches[i].name)) |
| 423 << "Missing flag " << kDefaultPolicySwitches[i].name; |
| 424 EXPECT_EQ(kDefaultPolicySwitches[i].value, |
| 425 cmd_line.GetSwitchValueASCII(kDefaultPolicySwitches[i].name)) |
| 426 << "Invalid value for switch " << kDefaultPolicySwitches[i].name; |
| 427 } |
| 428 } |
| 429 |
| 430 protected: |
| 431 std::unique_ptr<TerminationObserver> termination_observer_; |
| 432 |
| 433 private: |
| 434 chromeos::ScopedStubInstallAttributes install_attributes_; |
| 435 policy::DevicePolicyBuilder device_policy_; |
| 436 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_; |
| 437 std::unique_ptr<PersistentSessionManagerClient> fake_session_manager_; |
| 438 std::unique_ptr<FakeCWS> fake_cws_; |
| 439 |
| 440 DISALLOW_COPY_AND_ASSIGN(AutoLaunchedKioskTest); |
| 441 }; |
| 442 |
| 443 IN_PROC_BROWSER_TEST_F(AutoLaunchedKioskTest, PRE_CrashRestore) { |
| 444 // Verify that Chrome hasn't already exited, e.g. in order to apply user |
| 445 // session flags. |
| 446 ASSERT_FALSE(termination_observer_->terminated()); |
| 447 |
| 448 // Set up default network connections, so tests think the device is online. |
| 449 DBusThreadManager::Get() |
| 450 ->GetShillManagerClient() |
| 451 ->GetTestInterface() |
| 452 ->SetupDefaultEnvironment(); |
| 453 |
| 454 // Check that policy flags have not been lost. |
| 455 ExpectCommandLineHasDefaultPolicySwitches( |
| 456 *base::CommandLine::ForCurrentProcess()); |
| 457 |
| 458 ExtensionTestMessageListener listener("appWindowLoaded", false); |
| 459 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 460 |
| 461 EXPECT_TRUE(IsKioskAppAutoLaunched(kTestKioskApp)); |
| 462 |
| 463 ASSERT_TRUE(CloseAppWindow(kTestKioskApp)); |
| 464 } |
| 465 |
| 466 IN_PROC_BROWSER_TEST_F(AutoLaunchedKioskTest, CrashRestore) { |
| 467 // Verify that Chrome hasn't already exited, e.g. in order to apply user |
| 468 // session flags. |
| 469 ASSERT_FALSE(termination_observer_->terminated()); |
| 470 |
| 471 ExpectCommandLineHasDefaultPolicySwitches( |
| 472 *base::CommandLine::ForCurrentProcess()); |
| 473 |
| 474 ExtensionTestMessageListener listener("appWindowLoaded", false); |
| 475 EXPECT_TRUE(listener.WaitUntilSatisfied()); |
| 476 |
| 477 EXPECT_TRUE(IsKioskAppAutoLaunched(kTestKioskApp)); |
| 478 |
| 479 ASSERT_TRUE(CloseAppWindow(kTestKioskApp)); |
| 480 } |
| 481 |
| 482 } // namespace chromeos |
OLD | NEW |