OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 |
| 8 #include "base/base64.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/files/file_util.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "chrome/browser/chrome_notification_types.h" |
| 14 #include "chrome/browser/chromeos/app_mode/fake_cws.h" |
| 15 #include "chrome/browser/chromeos/app_mode/kiosk_app_launch_error.h" |
| 16 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| 17 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact
ory.h" |
| 18 #include "chrome/browser/chromeos/policy/device_local_account.h" |
| 19 #include "chrome/browser/chromeos/policy/device_policy_builder.h" |
| 20 #include "chrome/browser/extensions/browsertest_util.h" |
| 21 #include "chrome/browser/profiles/profile_manager.h" |
| 22 #include "chrome/common/chrome_constants.h" |
| 23 #include "chrome/common/chrome_paths.h" |
| 24 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/test/base/in_process_browser_test.h" |
| 26 #include "chromeos/chromeos_switches.h" |
| 27 #include "chromeos/dbus/dbus_thread_manager.h" |
| 28 #include "chromeos/dbus/fake_session_manager_client.h" |
| 29 #include "components/ownership/mock_owner_key_util.h" |
| 30 #include "content/public/browser/notification_observer.h" |
| 31 #include "content/public/browser/notification_registrar.h" |
| 32 #include "content/public/browser/notification_service.h" |
| 33 #include "extensions/common/value_builder.h" |
| 34 #include "extensions/test/extension_test_message_listener.h" |
| 35 #include "net/dns/mock_host_resolver.h" |
| 36 |
| 37 namespace em = enterprise_management; |
| 38 |
| 39 namespace chromeos { |
| 40 |
| 41 namespace { |
| 42 |
| 43 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; |
| 44 |
| 45 // Used to listen for app termination notification. |
| 46 class TerminationObserver : public content::NotificationObserver { |
| 47 public: |
| 48 TerminationObserver() { |
| 49 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
| 50 content::NotificationService::AllSources()); |
| 51 } |
| 52 ~TerminationObserver() override = default; |
| 53 |
| 54 // Whether app has been terminated - i.e. whether app termination notification |
| 55 // has been observed. |
| 56 bool terminated() const { return notification_seen_; } |
| 57 |
| 58 private: |
| 59 void Observe(int type, |
| 60 const content::NotificationSource& source, |
| 61 const content::NotificationDetails& details) override { |
| 62 ASSERT_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); |
| 63 notification_seen_ = true; |
| 64 } |
| 65 |
| 66 bool notification_seen_ = false; |
| 67 content::NotificationRegistrar registrar_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(TerminationObserver); |
| 70 }; |
| 71 |
| 72 } // namespace |
| 73 |
| 74 class KioskCrashRestoreTest : public InProcessBrowserTest { |
| 75 public: |
| 76 KioskCrashRestoreTest() |
| 77 : owner_key_util_(new ownership::MockOwnerKeyUtil()), |
| 78 fake_cws_(new FakeCWS) {} |
| 79 |
| 80 // InProcessBrowserTest |
| 81 void SetUp() override { |
| 82 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 83 InProcessBrowserTest::SetUp(); |
| 84 } |
| 85 |
| 86 bool SetUpUserDataDirectory() override { |
| 87 SetUpExistingKioskApp(); |
| 88 return true; |
| 89 } |
| 90 |
| 91 void SetUpInProcessBrowserTestFixture() override { |
| 92 host_resolver()->AddRule("*", "127.0.0.1"); |
| 93 |
| 94 OverrideDevicePolicy(); |
| 95 } |
| 96 |
| 97 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 98 const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId()); |
| 99 const cryptohome::Identification cryptohome_id(account_id); |
| 100 |
| 101 command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id()); |
| 102 command_line->AppendSwitchASCII( |
| 103 switches::kLoginProfile, |
| 104 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id)); |
| 105 |
| 106 fake_cws_->Init(embedded_test_server()); |
| 107 fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0"); |
| 108 } |
| 109 |
| 110 void RunTestOnMainThreadLoop() override { |
| 111 termination_observer_.reset(new TerminationObserver()); |
| 112 |
| 113 InProcessBrowserTest::RunTestOnMainThreadLoop(); |
| 114 } |
| 115 |
| 116 void SetUpOnMainThread() override { |
| 117 extensions::browsertest_util::CreateAndInitializeLocalCache(); |
| 118 |
| 119 embedded_test_server()->StartAcceptingConnections(); |
| 120 } |
| 121 |
| 122 const std::string GetTestAppUserId() const { |
| 123 return policy::GenerateDeviceLocalAccountUserId( |
| 124 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP); |
| 125 } |
| 126 |
| 127 const std::string& test_app_id() const { return test_app_id_; } |
| 128 |
| 129 protected: |
| 130 std::unique_ptr<TerminationObserver> termination_observer_; |
| 131 |
| 132 private: |
| 133 void SetUpExistingKioskApp() { |
| 134 // Create policy data that contains the test app as an existing kiosk app. |
| 135 em::DeviceLocalAccountsProto* const device_local_accounts = |
| 136 device_policy_.payload().mutable_device_local_accounts(); |
| 137 |
| 138 em::DeviceLocalAccountInfoProto* const account = |
| 139 device_local_accounts->add_account(); |
| 140 account->set_account_id(test_app_id_); |
| 141 account->set_type( |
| 142 em::DeviceLocalAccountInfoProto_AccountType_ACCOUNT_TYPE_KIOSK_APP); |
| 143 account->mutable_kiosk_app()->set_app_id(test_app_id_); |
| 144 device_policy_.Build(); |
| 145 |
| 146 // Prepare the policy data to store in device policy cache. |
| 147 em::PolicyData policy_data; |
| 148 CHECK(device_policy_.payload().SerializeToString( |
| 149 policy_data.mutable_policy_value())); |
| 150 const std::string policy_data_string = policy_data.SerializeAsString(); |
| 151 std::string encoded; |
| 152 base::Base64Encode(policy_data_string, &encoded); |
| 153 |
| 154 // Store policy data and existing device local accounts in local state. |
| 155 const std::string local_state_json = |
| 156 extensions::DictionaryBuilder() |
| 157 .Set(prefs::kDeviceSettingsCache, encoded) |
| 158 .Set("PublicAccounts", |
| 159 extensions::ListBuilder().Append(GetTestAppUserId()).Build()) |
| 160 .ToJSON(); |
| 161 |
| 162 base::FilePath local_state_file; |
| 163 CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file)); |
| 164 local_state_file = local_state_file.Append(chrome::kLocalStateFilename); |
| 165 base::WriteFile(local_state_file, local_state_json.data(), |
| 166 local_state_json.size()); |
| 167 } |
| 168 |
| 169 void OverrideDevicePolicy() { |
| 170 OwnerSettingsServiceChromeOSFactory::GetInstance() |
| 171 ->SetOwnerKeyUtilForTesting(owner_key_util_); |
| 172 owner_key_util_->SetPublicKeyFromPrivateKey( |
| 173 *device_policy_.GetSigningKey()); |
| 174 |
| 175 session_manager_client_ = new FakeSessionManagerClient; |
| 176 session_manager_client_->set_device_policy(device_policy_.GetBlob()); |
| 177 |
| 178 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 179 std::unique_ptr<SessionManagerClient>(session_manager_client_)); |
| 180 } |
| 181 |
| 182 std::string test_app_id_ = kTestKioskApp; |
| 183 |
| 184 policy::DevicePolicyBuilder device_policy_; |
| 185 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_; |
| 186 FakeSessionManagerClient* session_manager_client_; |
| 187 std::unique_ptr<FakeCWS> fake_cws_; |
| 188 |
| 189 DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest); |
| 190 }; |
| 191 |
| 192 IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, AppNotInstalled) { |
| 193 // If app is not installed when restoring from crash, the kiosk launch is |
| 194 // expected to fail, as in that case the crash occured during the app |
| 195 // initialization - before the app was actually launched. |
| 196 EXPECT_TRUE(termination_observer_->terminated()); |
| 197 EXPECT_EQ(KioskAppLaunchError::UNABLE_TO_LAUNCH, KioskAppLaunchError::Get()); |
| 198 } |
| 199 |
| 200 } // namespace chromeos |
OLD | NEW |