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 "apps/test/app_window_waiter.h" |
| 9 #include "base/base64.h" |
| 10 #include "base/command_line.h" |
| 11 #include "base/files/file_util.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/run_loop.h" |
| 14 #include "chrome/browser/chromeos/app_mode/fake_cws.h" |
| 15 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" |
| 16 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.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 "chromeos/dbus/fake_shill_manager_client.h" |
| 30 #include "components/ownership/mock_owner_key_util.h" |
| 31 #include "extensions/browser/app_window/app_window.h" |
| 32 #include "extensions/browser/app_window/app_window_registry.h" |
| 33 #include "extensions/browser/app_window/native_app_window.h" |
| 34 #include "extensions/common/value_builder.h" |
| 35 #include "extensions/test/extension_test_message_listener.h" |
| 36 #include "net/dns/mock_host_resolver.h" |
| 37 |
| 38 namespace em = enterprise_management; |
| 39 |
| 40 namespace chromeos { |
| 41 |
| 42 namespace { |
| 43 |
| 44 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 class KioskCrashRestoreTest : public InProcessBrowserTest { |
| 49 public: |
| 50 KioskCrashRestoreTest() |
| 51 : owner_key_util_(new ownership::MockOwnerKeyUtil()), |
| 52 fake_cws_(new FakeCWS) {} |
| 53 |
| 54 // InProcessBrowserTest |
| 55 void SetUp() override { |
| 56 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); |
| 57 InProcessBrowserTest::SetUp(); |
| 58 } |
| 59 |
| 60 bool SetUpUserDataDirectory() override { |
| 61 SetUpExistingKioskApp(); |
| 62 return true; |
| 63 } |
| 64 |
| 65 void SetUpInProcessBrowserTestFixture() override { |
| 66 host_resolver()->AddRule("*", "127.0.0.1"); |
| 67 SimulateNetworkOnline(); |
| 68 |
| 69 OverrideDevicePolicy(); |
| 70 } |
| 71 |
| 72 void SetUpCommandLine(base::CommandLine* command_line) override { |
| 73 const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId()); |
| 74 const cryptohome::Identification cryptohome_id(account_id); |
| 75 |
| 76 command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id()); |
| 77 command_line->AppendSwitchASCII( |
| 78 switches::kLoginProfile, |
| 79 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id)); |
| 80 |
| 81 fake_cws_->Init(embedded_test_server()); |
| 82 fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0"); |
| 83 } |
| 84 |
| 85 void SetUpOnMainThread() override { |
| 86 extensions::browsertest_util::CreateAndInitializeLocalCache(); |
| 87 |
| 88 embedded_test_server()->StartAcceptingConnections(); |
| 89 } |
| 90 |
| 91 const std::string GetTestAppUserId() const { |
| 92 return policy::GenerateDeviceLocalAccountUserId( |
| 93 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP); |
| 94 } |
| 95 |
| 96 const std::string& test_app_id() const { return test_app_id_; } |
| 97 |
| 98 private: |
| 99 void SetUpExistingKioskApp() { |
| 100 // Create policy data that contains the test app as an existing kiosk app. |
| 101 em::DeviceLocalAccountsProto* const device_local_accounts = |
| 102 device_policy_.payload().mutable_device_local_accounts(); |
| 103 |
| 104 em::DeviceLocalAccountInfoProto* const account = |
| 105 device_local_accounts->add_account(); |
| 106 account->set_account_id(test_app_id_); |
| 107 account->set_type( |
| 108 em::DeviceLocalAccountInfoProto_AccountType_ACCOUNT_TYPE_KIOSK_APP); |
| 109 account->mutable_kiosk_app()->set_app_id(test_app_id_); |
| 110 device_policy_.Build(); |
| 111 |
| 112 // Prepare the policy data to store in device policy cache. |
| 113 em::PolicyData policy_data; |
| 114 CHECK(device_policy_.payload().SerializeToString( |
| 115 policy_data.mutable_policy_value())); |
| 116 const std::string policy_data_string = policy_data.SerializeAsString(); |
| 117 std::string encoded; |
| 118 base::Base64Encode(policy_data_string, &encoded); |
| 119 |
| 120 // Store policy data and existing device local accounts in local state. |
| 121 const std::string local_state_json = |
| 122 extensions::DictionaryBuilder() |
| 123 .Set(prefs::kDeviceSettingsCache, encoded) |
| 124 .Set("PublicAccounts", |
| 125 extensions::ListBuilder().Append(GetTestAppUserId()).Build()) |
| 126 .ToJSON(); |
| 127 |
| 128 base::FilePath local_state_file; |
| 129 CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file)); |
| 130 local_state_file = local_state_file.Append(chrome::kLocalStateFilename); |
| 131 base::WriteFile(local_state_file, local_state_json.data(), |
| 132 local_state_json.size()); |
| 133 } |
| 134 |
| 135 void SimulateNetworkOnline() { |
| 136 NetworkPortalDetectorTestImpl* const network_portal_detector = |
| 137 new NetworkPortalDetectorTestImpl(); |
| 138 // Takes ownership of |network_portal_detector|. |
| 139 network_portal_detector::InitializeForTesting(network_portal_detector); |
| 140 network_portal_detector->SetDefaultNetworkForTesting( |
| 141 FakeShillManagerClient::kFakeEthernetNetworkGuid); |
| 142 |
| 143 NetworkPortalDetector::CaptivePortalState online_state; |
| 144 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE; |
| 145 online_state.response_code = 204; |
| 146 network_portal_detector->SetDetectionResultsForTesting( |
| 147 FakeShillManagerClient::kFakeEthernetNetworkGuid, online_state); |
| 148 } |
| 149 |
| 150 void OverrideDevicePolicy() { |
| 151 OwnerSettingsServiceChromeOSFactory::GetInstance() |
| 152 ->SetOwnerKeyUtilForTesting(owner_key_util_); |
| 153 owner_key_util_->SetPublicKeyFromPrivateKey( |
| 154 *device_policy_.GetSigningKey()); |
| 155 |
| 156 session_manager_client_ = new FakeSessionManagerClient; |
| 157 session_manager_client_->set_device_policy(device_policy_.GetBlob()); |
| 158 |
| 159 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient( |
| 160 std::unique_ptr<SessionManagerClient>(session_manager_client_)); |
| 161 } |
| 162 |
| 163 std::string test_app_id_ = kTestKioskApp; |
| 164 |
| 165 policy::DevicePolicyBuilder device_policy_; |
| 166 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_; |
| 167 FakeSessionManagerClient* session_manager_client_; |
| 168 std::unique_ptr<FakeCWS> fake_cws_; |
| 169 |
| 170 DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest); |
| 171 }; |
| 172 |
| 173 IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, Basic) { |
| 174 ExtensionTestMessageListener launch_data_check_listener( |
| 175 "launchData.isKioskSession = true", false); |
| 176 |
| 177 Profile* const app_profile = ProfileManager::GetPrimaryUserProfile(); |
| 178 ASSERT_TRUE(app_profile); |
| 179 extensions::AppWindowRegistry* const app_window_registry = |
| 180 extensions::AppWindowRegistry::Get(app_profile); |
| 181 extensions::AppWindow* const window = |
| 182 apps::AppWindowWaiter(app_window_registry, test_app_id()).Wait(); |
| 183 ASSERT_TRUE(window); |
| 184 |
| 185 window->GetBaseWindow()->Close(); |
| 186 |
| 187 // Wait until the app terminates if it is still running. |
| 188 if (!app_window_registry->GetAppWindowsForApp(test_app_id()).empty()) |
| 189 base::RunLoop().Run(); |
| 190 |
| 191 EXPECT_TRUE(launch_data_check_listener.was_satisfied()); |
| 192 } |
| 193 |
| 194 } // namespace chromeos |
OLD | NEW |