Chromium Code Reviews| 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 "base/base64.h" | |
| 6 #include "base/command_line.h" | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "base/path_service.h" | |
| 9 #include "base/run_loop.h" | |
| 10 #include "base/strings/stringprintf.h" | |
| 11 #include "chrome/browser/chromeos/app_mode/fake_cws.h" | |
| 12 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" | |
| 13 #include "chrome/browser/chromeos/login/test/app_window_waiter.h" | |
| 14 #include "chrome/browser/chromeos/net/network_portal_detector_test_impl.h" | |
| 15 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos_fact ory.h" | |
| 16 #include "chrome/browser/chromeos/policy/device_local_account.h" | |
| 17 #include "chrome/browser/chromeos/settings/device_settings_cache.h" | |
| 18 #include "chrome/browser/chromeos/settings/device_settings_test_helper.h" | |
| 19 #include "chrome/browser/profiles/profile_manager.h" | |
| 20 #include "chrome/common/chrome_constants.h" | |
| 21 #include "chrome/common/chrome_paths.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/test/base/in_process_browser_test.h" | |
| 24 #include "chromeos/chromeos_paths.h" | |
| 25 #include "chromeos/chromeos_switches.h" | |
| 26 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 27 #include "chromeos/dbus/fake_session_manager_client.h" | |
| 28 #include "chromeos/dbus/fake_shill_manager_client.h" | |
| 29 #include "extensions/browser/app_window/app_window.h" | |
| 30 #include "extensions/browser/app_window/app_window_registry.h" | |
| 31 #include "extensions/browser/app_window/native_app_window.h" | |
| 32 #include "extensions/test/extension_test_message_listener.h" | |
| 33 #include "net/dns/mock_host_resolver.h" | |
| 34 | |
| 35 namespace em = enterprise_management; | |
| 36 | |
| 37 namespace chromeos { | |
| 38 | |
| 39 namespace { | |
| 40 | |
| 41 const char kTestKioskApp[] = "ggbflgnkafappblpkiflbgpmkfdpnhhe"; | |
| 42 | |
| 43 void CreateAndInitializeLocalCache() { | |
| 44 base::FilePath extension_cache_dir; | |
| 45 CHECK(PathService::Get(chromeos::DIR_DEVICE_EXTENSION_LOCAL_CACHE, | |
|
bartfab (slow)
2016/04/07 14:55:41
Should we not override this path to avoid litterin
xiyuan
2016/04/07 21:43:57
This is overridden under user data dir when not ru
| |
| 46 &extension_cache_dir)); | |
| 47 base::FilePath cache_init_file = extension_cache_dir.Append( | |
| 48 extensions::LocalExtensionCache::kCacheReadyFlagFileName); | |
| 49 EXPECT_EQ(base::WriteFile(cache_init_file, "", 0), 0); | |
| 50 } | |
| 51 | |
| 52 } // namespace | |
| 53 | |
| 54 class KioskCrashRestoreTest : public InProcessBrowserTest { | |
| 55 public: | |
| 56 KioskCrashRestoreTest() | |
| 57 : owner_key_util_(new ownership::MockOwnerKeyUtil()), | |
| 58 fake_cws_(new FakeCWS) {} | |
| 59 ~KioskCrashRestoreTest() override {} | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: Do you really need to override this?
xiyuan
2016/04/07 21:43:56
Removed.
| |
| 60 | |
| 61 // InProcessBrowserTest | |
| 62 void SetUp() override { | |
| 63 ASSERT_TRUE(embedded_test_server()->InitializeAndListen()); | |
| 64 InProcessBrowserTest::SetUp(); | |
| 65 } | |
| 66 | |
| 67 bool SetUpUserDataDirectory() override { | |
| 68 SetUpExistingKioskApp(); | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 void SetUpInProcessBrowserTestFixture() override { | |
| 73 host_resolver()->AddRule("*", "127.0.0.1"); | |
| 74 SimulateNetworkOnline(); | |
| 75 | |
| 76 OverrideDevicePolicy(); | |
| 77 } | |
| 78 | |
| 79 void SetUpCommandLine(base::CommandLine* command_line) override { | |
| 80 const AccountId account_id = | |
| 81 AccountId::FromUserEmail(policy::GenerateDeviceLocalAccountUserId( | |
| 82 test_app_id_, policy::DeviceLocalAccount::TYPE_KIOSK_APP)); | |
| 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& test_app_id() const { return test_app_id_; } | |
| 101 | |
| 102 private: | |
| 103 void SetUpExistingKioskApp() { | |
| 104 // Create policy data that contains the test app as an existing kiosk app. | |
| 105 em::DeviceLocalAccountsProto* device_local_accounts = | |
|
bartfab (slow)
2016/04/07 14:55:41
Nit: const pointer.
xiyuan
2016/04/07 21:43:56
Done.
| |
| 106 device_policy_.payload().mutable_device_local_accounts(); | |
| 107 device_local_accounts->clear_account(); | |
|
bartfab (slow)
2016/04/07 14:55:42
Can you not safely assume that the payload starts
xiyuan
2016/04/07 21:43:56
Removed.
| |
| 108 | |
| 109 em::DeviceLocalAccountInfoProto* account = | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: const pointer.
xiyuan
2016/04/07 21:43:56
Done.
| |
| 110 device_local_accounts->add_account(); | |
| 111 account->set_account_id(test_app_id_); | |
| 112 account->set_type( | |
| 113 em::DeviceLocalAccountInfoProto_AccountType_ACCOUNT_TYPE_KIOSK_APP); | |
| 114 account->mutable_kiosk_app()->set_app_id(test_app_id_); | |
| 115 device_policy_.Build(); | |
| 116 | |
| 117 // Store the policy data in device policy cache. | |
| 118 em::PolicyData policy_data; | |
| 119 CHECK(device_policy_.payload().SerializeToString( | |
| 120 policy_data.mutable_policy_value())); | |
| 121 const std::string policy_data_string = policy_data.SerializeAsString(); | |
| 122 std::string encoded; | |
| 123 base::Base64Encode(policy_data_string, &encoded); | |
| 124 | |
| 125 const std::string local_state_json = base::StringPrintf( | |
|
bartfab (slow)
2016/04/07 14:55:42
This looks odd. There are many tests that stub out
xiyuan
2016/04/07 21:43:57
The code that needs to be tested runs early in Pre
| |
| 126 "{\"%s\":\"%s\"}", prefs::kDeviceSettingsCache, encoded.c_str()); | |
| 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* network_portal_detector = | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: const pointer.
xiyuan
2016/04/07 21:43:56
Done.
| |
| 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 scoped_ptr<SessionManagerClient>(session_manager_client_)); | |
|
bartfab (slow)
2016/04/07 14:55:41
Nit: s/scoped_ptr/std::unique_ptr/
xiyuan
2016/04/07 21:43:57
Done.
| |
| 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 scoped_ptr<FakeCWS> fake_cws_; | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: s/scoped_ptr/std::unique_ptr/
xiyuan
2016/04/07 21:43:56
Done.
| |
| 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* app_profile = ProfileManager::GetPrimaryUserProfile(); | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: const pointer.
| |
| 178 ASSERT_TRUE(app_profile); | |
| 179 extensions::AppWindowRegistry* app_window_registry = | |
|
bartfab (slow)
2016/04/07 14:55:41
Nit: const pointer.
| |
| 180 extensions::AppWindowRegistry::Get(app_profile); | |
| 181 extensions::AppWindow* window = | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: const pointer.
| |
| 182 AppWindowWaiter(app_window_registry, test_app_id()).Wait(); | |
| 183 EXPECT_TRUE(window); | |
|
bartfab (slow)
2016/04/07 14:55:42
Nit: s/EXPECT/ASSERT/
xiyuan
2016/04/07 21:43:56
Done.
| |
| 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 |