Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / device_local_account_external_policy_loader_unittest.cc
blob3d1121c221d1c27f6297cdcd0ed6932ccf2fc36b
1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/extensions/device_local_account_external_policy_loader.h"
7 #include <string>
9 #include "base/callback.h"
10 #include "base/files/file_util.h"
11 #include "base/files/scoped_temp_dir.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/path_service.h"
14 #include "base/run_loop.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/thread_task_runner_handle.h"
17 #include "base/values.h"
18 #include "base/version.h"
19 #include "chrome/browser/extensions/external_provider_impl.h"
20 #include "chrome/common/chrome_paths.h"
21 #include "chrome/test/base/testing_browser_process.h"
22 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
23 #include "components/policy/core/common/policy_map.h"
24 #include "components/policy/core/common/policy_types.h"
25 #include "content/public/browser/notification_service.h"
26 #include "content/public/browser/notification_source.h"
27 #include "content/public/test/test_browser_thread_bundle.h"
28 #include "content/public/test/test_utils.h"
29 #include "extensions/browser/external_provider_interface.h"
30 #include "extensions/browser/notification_types.h"
31 #include "extensions/browser/updater/extension_downloader.h"
32 #include "extensions/common/extension.h"
33 #include "extensions/common/extension_urls.h"
34 #include "extensions/common/manifest.h"
35 #include "net/url_request/test_url_fetcher_factory.h"
36 #include "net/url_request/url_fetcher_delegate.h"
37 #include "net/url_request/url_request_context_getter.h"
38 #include "net/url_request/url_request_test_util.h"
39 #include "policy/policy_constants.h"
40 #include "testing/gmock/include/gmock/gmock.h"
41 #include "testing/gtest/include/gtest/gtest.h"
42 #include "url/gurl.h"
44 #if defined(OS_CHROMEOS)
45 #include "chrome/browser/chromeos/settings/cros_settings.h"
46 #include "chrome/browser/chromeos/settings/device_settings_service.h"
47 #endif // defined(OS_CHROMEOS)
49 using ::testing::InvokeWithoutArgs;
50 using ::testing::Mock;
51 using ::testing::_;
53 namespace chromeos {
55 namespace {
57 const char kCacheDir[] = "cache";
58 const char kExtensionId[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
59 const char kExtensionUpdateManifest[] =
60 "extensions/good_v1_update_manifest.xml";
61 const char kExtensionCRXSourceDir[] = "extensions";
62 const char kExtensionCRXFile[] = "good.crx";
63 const char kExtensionCRXVersion[] = "1.0.0.0";
65 class MockExternalPolicyProviderVisitor
66 : public extensions::ExternalProviderInterface::VisitorInterface {
67 public:
68 MockExternalPolicyProviderVisitor();
69 virtual ~MockExternalPolicyProviderVisitor();
71 MOCK_METHOD7(OnExternalExtensionFileFound,
72 bool(const std::string&,
73 const base::Version*,
74 const base::FilePath&,
75 extensions::Manifest::Location,
76 int,
77 bool,
78 bool));
79 MOCK_METHOD6(OnExternalExtensionUpdateUrlFound,
80 bool(const std::string&,
81 const std::string&,
82 const GURL&,
83 extensions::Manifest::Location,
84 int,
85 bool));
86 MOCK_METHOD1(OnExternalProviderReady,
87 void(const extensions::ExternalProviderInterface* provider));
89 private:
90 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor);
93 MockExternalPolicyProviderVisitor::MockExternalPolicyProviderVisitor() {
96 MockExternalPolicyProviderVisitor::~MockExternalPolicyProviderVisitor() {
99 } // namespace
101 class DeviceLocalAccountExternalPolicyLoaderTest : public testing::Test {
102 protected:
103 DeviceLocalAccountExternalPolicyLoaderTest();
104 ~DeviceLocalAccountExternalPolicyLoaderTest() override;
106 void SetUp() override;
107 void TearDown() override;
109 void VerifyAndResetVisitorCallExpectations();
110 void SetForceInstallListPolicy();
112 content::TestBrowserThreadBundle thread_bundle_;
113 base::ScopedTempDir temp_dir_;
114 base::FilePath cache_dir_;
115 policy::MockCloudPolicyStore store_;
116 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
117 base::FilePath test_dir_;
119 scoped_refptr<DeviceLocalAccountExternalPolicyLoader> loader_;
120 MockExternalPolicyProviderVisitor visitor_;
121 scoped_ptr<extensions::ExternalProviderImpl> provider_;
123 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_;
125 #if defined(OS_CHROMEOS)
126 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_;
127 chromeos::ScopedTestCrosSettings test_cros_settings_;
128 #endif // defined(OS_CHROMEOS)
131 DeviceLocalAccountExternalPolicyLoaderTest::
132 DeviceLocalAccountExternalPolicyLoaderTest()
133 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP) {
136 DeviceLocalAccountExternalPolicyLoaderTest::
137 ~DeviceLocalAccountExternalPolicyLoaderTest() {
140 void DeviceLocalAccountExternalPolicyLoaderTest::SetUp() {
141 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
142 cache_dir_ = temp_dir_.path().Append(kCacheDir);
143 ASSERT_TRUE(base::CreateDirectoryAndGetError(cache_dir_, NULL));
144 request_context_getter_ =
145 new net::TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get());
146 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
147 request_context_getter_.get());
148 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &test_dir_));
150 loader_ = new DeviceLocalAccountExternalPolicyLoader(&store_, cache_dir_);
151 provider_.reset(new extensions::ExternalProviderImpl(
152 &visitor_,
153 loader_,
154 NULL,
155 extensions::Manifest::EXTERNAL_POLICY,
156 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD,
157 extensions::Extension::NO_FLAGS));
159 VerifyAndResetVisitorCallExpectations();
162 void DeviceLocalAccountExternalPolicyLoaderTest::TearDown() {
163 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(NULL);
166 void DeviceLocalAccountExternalPolicyLoaderTest::
167 VerifyAndResetVisitorCallExpectations() {
168 Mock::VerifyAndClearExpectations(&visitor_);
169 EXPECT_CALL(visitor_, OnExternalExtensionFileFound(_, _, _, _, _, _, _))
170 .Times(0);
171 EXPECT_CALL(visitor_, OnExternalExtensionUpdateUrlFound(_, _, _, _, _, _))
172 .Times(0);
173 EXPECT_CALL(visitor_, OnExternalProviderReady(_))
174 .Times(0);
177 void DeviceLocalAccountExternalPolicyLoaderTest::SetForceInstallListPolicy() {
178 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
179 forcelist->AppendString("invalid");
180 forcelist->AppendString(base::StringPrintf(
181 "%s;%s",
182 kExtensionId,
183 extension_urls::GetWebstoreUpdateUrl().spec().c_str()));
184 store_.policy_map_.Set(policy::key::kExtensionInstallForcelist,
185 policy::POLICY_LEVEL_MANDATORY,
186 policy::POLICY_SCOPE_USER,
187 forcelist.release(),
188 NULL);
189 store_.NotifyStoreLoaded();
192 // Verifies that when the cache is not explicitly started, the loader does not
193 // serve any extensions, even if the force-install list policy is set or a load
194 // is manually requested.
195 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, CacheNotStarted) {
196 // Set the force-install list policy.
197 SetForceInstallListPolicy();
199 // Manually request a load.
200 loader_->StartLoading();
202 EXPECT_FALSE(loader_->IsCacheRunning());
203 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
206 // Verifies that the cache can be started and stopped correctly.
207 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, ForceInstallListEmpty) {
208 // Set an empty force-install list policy.
209 store_.NotifyStoreLoaded();
211 // Start the cache. Verify that the loader announces an empty extension list.
212 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
213 .Times(1);
214 loader_->StartCache(base::ThreadTaskRunnerHandle::Get());
215 base::RunLoop().RunUntilIdle();
216 VerifyAndResetVisitorCallExpectations();
218 // Stop the cache. Verify that the loader announces an empty extension list.
219 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
220 .Times(1);
221 base::RunLoop run_loop;
222 loader_->StopCache(run_loop.QuitClosure());
223 VerifyAndResetVisitorCallExpectations();
225 // Spin the loop until the cache shutdown callback is invoked. Verify that at
226 // that point, no further file I/O tasks are pending.
227 run_loop.Run();
228 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
231 // Verifies that when a force-install list policy referencing an extension is
232 // set and the cache is started, the loader downloads, caches and serves the
233 // extension.
234 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest, ForceInstallListSet) {
235 // Set a force-install list policy that contains an invalid entry (which
236 // should be ignored) and a valid reference to an extension.
237 SetForceInstallListPolicy();
239 // Start the cache.
240 loader_->StartCache(base::ThreadTaskRunnerHandle::Get());
242 // Spin the loop, allowing the loader to process the force-install list.
243 // Verify that the loader announces an empty extension list.
244 net::TestURLFetcherFactory factory;
245 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
246 .Times(1);
247 base::MessageLoop::current()->RunUntilIdle();
249 // Verify that a downloader has started and is attempting to download an
250 // update manifest.
251 net::TestURLFetcher* fetcher = factory.GetFetcherByID(
252 extensions::ExtensionDownloader::kManifestFetcherId);
253 ASSERT_TRUE(fetcher);
254 ASSERT_TRUE(fetcher->delegate());
256 // Return a manifest to the downloader.
257 std::string manifest;
258 EXPECT_TRUE(base::ReadFileToString(test_dir_.Append(kExtensionUpdateManifest),
259 &manifest));
260 fetcher->set_response_code(200);
261 fetcher->SetResponseString(manifest);
262 fetcher->delegate()->OnURLFetchComplete(fetcher);
264 // Wait for the manifest to be parsed.
265 content::WindowedNotificationObserver(
266 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND,
267 content::NotificationService::AllSources()).Wait();
269 // Verify that the downloader is attempting to download a CRX file.
270 fetcher = factory.GetFetcherByID(
271 extensions::ExtensionDownloader::kExtensionFetcherId);
272 ASSERT_TRUE(fetcher);
273 ASSERT_TRUE(fetcher->delegate());
275 // Create a temporary CRX file and return its path to the downloader.
276 EXPECT_TRUE(base::CopyFile(
277 test_dir_.Append(kExtensionCRXSourceDir).Append(kExtensionCRXFile),
278 temp_dir_.path().Append(kExtensionCRXFile)));
279 fetcher->set_response_code(200);
280 fetcher->SetResponseFilePath(temp_dir_.path().Append(kExtensionCRXFile));
281 fetcher->delegate()->OnURLFetchComplete(fetcher);
283 // Spin the loop. Verify that the loader announces the presence of a new CRX
284 // file, served from the cache directory.
285 const base::FilePath cached_crx_path = cache_dir_.Append(base::StringPrintf(
286 "%s-%s.crx", kExtensionId, kExtensionCRXVersion));
287 base::RunLoop cache_run_loop;
288 EXPECT_CALL(visitor_, OnExternalExtensionFileFound(
289 kExtensionId,
291 cached_crx_path,
292 extensions::Manifest::EXTERNAL_POLICY,
295 _));
296 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
297 .Times(1)
298 .WillOnce(InvokeWithoutArgs(&cache_run_loop, &base::RunLoop::Quit));
299 cache_run_loop.Run();
300 VerifyAndResetVisitorCallExpectations();
302 // Verify that the CRX file actually exists in the cache directory and its
303 // contents matches the file returned to the downloader.
304 EXPECT_TRUE(base::ContentsEqual(
305 test_dir_.Append(kExtensionCRXSourceDir).Append(kExtensionCRXFile),
306 cached_crx_path));
308 // Stop the cache. Verify that the loader announces an empty extension list.
309 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
310 .Times(1);
311 base::RunLoop shutdown_run_loop;
312 loader_->StopCache(shutdown_run_loop.QuitClosure());
313 VerifyAndResetVisitorCallExpectations();
315 // Spin the loop until the cache shutdown callback is invoked. Verify that at
316 // that point, no further file I/O tasks are pending.
317 shutdown_run_loop.Run();
318 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
321 } // namespace chromeos