Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(37)

Side by Side Diff: chrome/browser/chromeos/app_mode/kiosk_crash_restore_browsertest.cc

Issue 1865133002: kiosk: Fix kiosk session restart (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: restore "PublicAccounts" local state Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/chromeos/app_mode/fake_cws.h"
14 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
15 #include "chrome/browser/chromeos/login/test/app_window_waiter.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/profiles/profile_manager.h"
21 #include "chrome/common/chrome_constants.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/test/base/in_process_browser_test.h"
25 #include "chromeos/chromeos_paths.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 void CreateAndInitializeLocalCache() {
47 base::FilePath extension_cache_dir;
48 CHECK(PathService::Get(chromeos::DIR_DEVICE_EXTENSION_LOCAL_CACHE,
49 &extension_cache_dir));
50 base::FilePath cache_init_file = extension_cache_dir.Append(
51 extensions::LocalExtensionCache::kCacheReadyFlagFileName);
52 EXPECT_EQ(base::WriteFile(cache_init_file, "", 0), 0);
53 }
54
55 } // namespace
56
57 class KioskCrashRestoreTest : public InProcessBrowserTest {
58 public:
59 KioskCrashRestoreTest()
60 : owner_key_util_(new ownership::MockOwnerKeyUtil()),
61 fake_cws_(new FakeCWS) {}
62
63 // InProcessBrowserTest
64 void SetUp() override {
65 ASSERT_TRUE(embedded_test_server()->InitializeAndListen());
66 InProcessBrowserTest::SetUp();
67 }
68
69 bool SetUpUserDataDirectory() override {
70 SetUpExistingKioskApp();
71 return true;
72 }
73
74 void SetUpInProcessBrowserTestFixture() override {
75 host_resolver()->AddRule("*", "127.0.0.1");
76 SimulateNetworkOnline();
77
78 OverrideDevicePolicy();
79 }
80
81 void SetUpCommandLine(base::CommandLine* command_line) override {
82 const AccountId account_id = AccountId::FromUserEmail(GetTestAppUserId());
83 const cryptohome::Identification cryptohome_id(account_id);
84
85 command_line->AppendSwitchASCII(switches::kLoginUser, cryptohome_id.id());
86 command_line->AppendSwitchASCII(
87 switches::kLoginProfile,
88 CryptohomeClient::GetStubSanitizedUsername(cryptohome_id));
89
90 fake_cws_->Init(embedded_test_server());
91 fake_cws_->SetUpdateCrx(test_app_id_, test_app_id_ + ".crx", "1.0.0");
92 }
93
94 void SetUpOnMainThread() override {
95 CreateAndInitializeLocalCache();
96
97 embedded_test_server()->StartAcceptingConnections();
98 }
99
100 const std::string GetTestAppUserId() const {
101 return policy::GenerateDeviceLocalAccountUserId(
102 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP);
103 }
104
105 const std::string& test_app_id() const { return test_app_id_; }
106
107 private:
108 void SetUpExistingKioskApp() {
109 // Create policy data that contains the test app as an existing kiosk app.
110 em::DeviceLocalAccountsProto* const device_local_accounts =
111 device_policy_.payload().mutable_device_local_accounts();
112
113 em::DeviceLocalAccountInfoProto* const account =
114 device_local_accounts->add_account();
115 account->set_account_id(test_app_id_);
116 account->set_type(
117 em::DeviceLocalAccountInfoProto_AccountType_ACCOUNT_TYPE_KIOSK_APP);
118 account->mutable_kiosk_app()->set_app_id(test_app_id_);
119 device_policy_.Build();
120
121 // Prepare the policy data store in device policy cache.
bartfab (slow) 2016/04/12 15:08:13 Nit: s/store/to store/
xiyuan 2016/04/12 16:22:51 Done.
122 em::PolicyData policy_data;
123 CHECK(device_policy_.payload().SerializeToString(
124 policy_data.mutable_policy_value()));
125 const std::string policy_data_string = policy_data.SerializeAsString();
126 std::string encoded;
127 base::Base64Encode(policy_data_string, &encoded);
128
129 // Store policy data and existing device local accounts in local state.
130 const std::string local_state_json =
131 extensions::DictionaryBuilder()
132 .Set(prefs::kDeviceSettingsCache, encoded)
133 .Set("PublicAccounts",
134 extensions::ListBuilder().Append(GetTestAppUserId()).Build())
135 .ToJSON();
136
137 base::FilePath local_state_file;
138 CHECK(PathService::Get(chrome::DIR_USER_DATA, &local_state_file));
139 local_state_file = local_state_file.Append(chrome::kLocalStateFilename);
140 base::WriteFile(local_state_file, local_state_json.data(),
141 local_state_json.size());
142 }
143
144 void SimulateNetworkOnline() {
145 NetworkPortalDetectorTestImpl* const network_portal_detector =
146 new NetworkPortalDetectorTestImpl();
147 // Takes ownership of |network_portal_detector|.
148 network_portal_detector::InitializeForTesting(network_portal_detector);
149 network_portal_detector->SetDefaultNetworkForTesting(
150 FakeShillManagerClient::kFakeEthernetNetworkGuid);
151
152 NetworkPortalDetector::CaptivePortalState online_state;
153 online_state.status = NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE;
154 online_state.response_code = 204;
155 network_portal_detector->SetDetectionResultsForTesting(
156 FakeShillManagerClient::kFakeEthernetNetworkGuid, online_state);
157 }
158
159 void OverrideDevicePolicy() {
160 OwnerSettingsServiceChromeOSFactory::GetInstance()
161 ->SetOwnerKeyUtilForTesting(owner_key_util_);
162 owner_key_util_->SetPublicKeyFromPrivateKey(
163 *device_policy_.GetSigningKey());
164
165 session_manager_client_ = new FakeSessionManagerClient;
166 session_manager_client_->set_device_policy(device_policy_.GetBlob());
167
168 DBusThreadManager::GetSetterForTesting()->SetSessionManagerClient(
169 std::unique_ptr<SessionManagerClient>(session_manager_client_));
170 }
171
172 std::string test_app_id_ = kTestKioskApp;
173
174 policy::DevicePolicyBuilder device_policy_;
175 scoped_refptr<ownership::MockOwnerKeyUtil> owner_key_util_;
176 FakeSessionManagerClient* session_manager_client_;
177 std::unique_ptr<FakeCWS> fake_cws_;
178
179 DISALLOW_COPY_AND_ASSIGN(KioskCrashRestoreTest);
180 };
181
182 IN_PROC_BROWSER_TEST_F(KioskCrashRestoreTest, Basic) {
183 ExtensionTestMessageListener launch_data_check_listener(
184 "launchData.isKioskSession = true", false);
185
186 Profile* const app_profile = ProfileManager::GetPrimaryUserProfile();
187 ASSERT_TRUE(app_profile);
188 extensions::AppWindowRegistry* const app_window_registry =
189 extensions::AppWindowRegistry::Get(app_profile);
190 extensions::AppWindow* const window =
191 AppWindowWaiter(app_window_registry, test_app_id()).Wait();
192 ASSERT_TRUE(window);
193
194 window->GetBaseWindow()->Close();
195
196 // Wait until the app terminates if it is still running.
197 if (!app_window_registry->GetAppWindowsForApp(test_app_id()).empty())
198 base::RunLoop().Run();
199
200 EXPECT_TRUE(launch_data_check_listener.was_satisfied());
201 }
202
203 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698