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"
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/message_loop/message_loop_proxy.h"
14 #include "base/path_service.h"
15 #include "base/run_loop.h"
16 #include "base/strings/stringprintf.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"
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
;
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
{
68 MockExternalPolicyProviderVisitor();
69 virtual ~MockExternalPolicyProviderVisitor();
71 MOCK_METHOD6(OnExternalExtensionFileFound
,
72 bool(const std::string
&,
74 const base::FilePath
&,
75 extensions::Manifest::Location
,
78 MOCK_METHOD6(OnExternalExtensionUpdateUrlFound
,
79 bool(const std::string
&,
82 extensions::Manifest::Location
,
85 MOCK_METHOD1(OnExternalProviderReady
,
86 void(const extensions::ExternalProviderInterface
* provider
));
89 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor
);
92 MockExternalPolicyProviderVisitor::MockExternalPolicyProviderVisitor() {
95 MockExternalPolicyProviderVisitor::~MockExternalPolicyProviderVisitor() {
100 class DeviceLocalAccountExternalPolicyLoaderTest
: public testing::Test
{
102 DeviceLocalAccountExternalPolicyLoaderTest();
103 ~DeviceLocalAccountExternalPolicyLoaderTest() override
;
105 void SetUp() override
;
106 void TearDown() override
;
108 void VerifyAndResetVisitorCallExpectations();
109 void SetForceInstallListPolicy();
111 content::TestBrowserThreadBundle thread_bundle_
;
112 base::ScopedTempDir temp_dir_
;
113 base::FilePath cache_dir_
;
114 policy::MockCloudPolicyStore store_
;
115 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
116 base::FilePath test_dir_
;
118 scoped_refptr
<DeviceLocalAccountExternalPolicyLoader
> loader_
;
119 MockExternalPolicyProviderVisitor visitor_
;
120 scoped_ptr
<extensions::ExternalProviderImpl
> provider_
;
122 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_
;
124 #if defined(OS_CHROMEOS)
125 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
126 chromeos::ScopedTestCrosSettings test_cros_settings_
;
127 #endif // defined(OS_CHROMEOS)
130 DeviceLocalAccountExternalPolicyLoaderTest::
131 DeviceLocalAccountExternalPolicyLoaderTest()
132 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
135 DeviceLocalAccountExternalPolicyLoaderTest::
136 ~DeviceLocalAccountExternalPolicyLoaderTest() {
139 void DeviceLocalAccountExternalPolicyLoaderTest::SetUp() {
140 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
141 cache_dir_
= temp_dir_
.path().Append(kCacheDir
);
142 ASSERT_TRUE(base::CreateDirectoryAndGetError(cache_dir_
, NULL
));
143 request_context_getter_
=
144 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
145 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
146 request_context_getter_
.get());
147 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_dir_
));
149 loader_
= new DeviceLocalAccountExternalPolicyLoader(&store_
, cache_dir_
);
150 provider_
.reset(new extensions::ExternalProviderImpl(
154 extensions::Manifest::EXTERNAL_POLICY
,
155 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD
,
156 extensions::Extension::NO_FLAGS
));
158 VerifyAndResetVisitorCallExpectations();
161 void DeviceLocalAccountExternalPolicyLoaderTest::TearDown() {
162 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(NULL
);
165 void DeviceLocalAccountExternalPolicyLoaderTest::
166 VerifyAndResetVisitorCallExpectations() {
167 Mock::VerifyAndClearExpectations(&visitor_
);
168 EXPECT_CALL(visitor_
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
170 EXPECT_CALL(visitor_
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
, _
))
172 EXPECT_CALL(visitor_
, OnExternalProviderReady(_
))
176 void DeviceLocalAccountExternalPolicyLoaderTest::SetForceInstallListPolicy() {
177 scoped_ptr
<base::ListValue
> forcelist(new base::ListValue
);
178 forcelist
->AppendString("invalid");
179 forcelist
->AppendString(base::StringPrintf(
182 extension_urls::GetWebstoreUpdateUrl().spec().c_str()));
183 store_
.policy_map_
.Set(policy::key::kExtensionInstallForcelist
,
184 policy::POLICY_LEVEL_MANDATORY
,
185 policy::POLICY_SCOPE_USER
,
188 store_
.NotifyStoreLoaded();
191 // Verifies that when the cache is not explicitly started, the loader does not
192 // serve any extensions, even if the force-install list policy is set or a load
193 // is manually requested.
194 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, CacheNotStarted
) {
195 // Set the force-install list policy.
196 SetForceInstallListPolicy();
198 // Manually request a load.
199 loader_
->StartLoading();
201 EXPECT_FALSE(loader_
->IsCacheRunning());
202 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
205 // Verifies that the cache can be started and stopped correctly.
206 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, ForceInstallListEmpty
) {
207 // Set an empty force-install list policy.
208 store_
.NotifyStoreLoaded();
210 // Start the cache. Verify that the loader announces an empty extension list.
211 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
213 loader_
->StartCache(base::MessageLoopProxy::current());
214 base::RunLoop().RunUntilIdle();
215 VerifyAndResetVisitorCallExpectations();
217 // Stop the cache. Verify that the loader announces an empty extension list.
218 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
220 base::RunLoop run_loop
;
221 loader_
->StopCache(run_loop
.QuitClosure());
222 VerifyAndResetVisitorCallExpectations();
224 // Spin the loop until the cache shutdown callback is invoked. Verify that at
225 // that point, no further file I/O tasks are pending.
227 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
230 // Verifies that when a force-install list policy referencing an extension is
231 // set and the cache is started, the loader downloads, caches and serves the
233 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, ForceInstallListSet
) {
234 // Set a force-install list policy that contains an invalid entry (which
235 // should be ignored) and a valid reference to an extension.
236 SetForceInstallListPolicy();
239 loader_
->StartCache(base::MessageLoopProxy::current());
241 // Spin the loop, allowing the loader to process the force-install list.
242 // Verify that the loader announces an empty extension list.
243 net::TestURLFetcherFactory factory
;
244 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
246 base::MessageLoop::current()->RunUntilIdle();
248 // Verify that a downloader has started and is attempting to download an
250 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(
251 extensions::ExtensionDownloader::kManifestFetcherId
);
252 ASSERT_TRUE(fetcher
);
253 ASSERT_TRUE(fetcher
->delegate());
255 // Return a manifest to the downloader.
256 std::string manifest
;
257 EXPECT_TRUE(base::ReadFileToString(test_dir_
.Append(kExtensionUpdateManifest
),
259 fetcher
->set_response_code(200);
260 fetcher
->SetResponseString(manifest
);
261 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
263 // Wait for the manifest to be parsed.
264 content::WindowedNotificationObserver(
265 extensions::NOTIFICATION_EXTENSION_UPDATE_FOUND
,
266 content::NotificationService::AllSources()).Wait();
268 // Verify that the downloader is attempting to download a CRX file.
269 fetcher
= factory
.GetFetcherByID(
270 extensions::ExtensionDownloader::kExtensionFetcherId
);
271 ASSERT_TRUE(fetcher
);
272 ASSERT_TRUE(fetcher
->delegate());
274 // Create a temporary CRX file and return its path to the downloader.
275 EXPECT_TRUE(base::CopyFile(
276 test_dir_
.Append(kExtensionCRXSourceDir
).Append(kExtensionCRXFile
),
277 temp_dir_
.path().Append(kExtensionCRXFile
)));
278 fetcher
->set_response_code(200);
279 fetcher
->SetResponseFilePath(temp_dir_
.path().Append(kExtensionCRXFile
));
280 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
282 // Spin the loop. Verify that the loader announces the presence of a new CRX
283 // file, served from the cache directory.
284 const base::FilePath cached_crx_path
= cache_dir_
.Append(base::StringPrintf(
285 "%s-%s.crx", kExtensionId
, kExtensionCRXVersion
));
286 base::RunLoop cache_run_loop
;
287 EXPECT_CALL(visitor_
, OnExternalExtensionFileFound(
291 extensions::Manifest::EXTERNAL_POLICY
,
294 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
296 .WillOnce(InvokeWithoutArgs(&cache_run_loop
, &base::RunLoop::Quit
));
297 cache_run_loop
.Run();
298 VerifyAndResetVisitorCallExpectations();
300 // Verify that the CRX file actually exists in the cache directory and its
301 // contents matches the file returned to the downloader.
302 EXPECT_TRUE(base::ContentsEqual(
303 test_dir_
.Append(kExtensionCRXSourceDir
).Append(kExtensionCRXFile
),
306 // Stop the cache. Verify that the loader announces an empty extension list.
307 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
309 base::RunLoop shutdown_run_loop
;
310 loader_
->StopCache(shutdown_run_loop
.QuitClosure());
311 VerifyAndResetVisitorCallExpectations();
313 // Spin the loop until the cache shutdown callback is invoked. Verify that at
314 // that point, no further file I/O tasks are pending.
315 shutdown_run_loop
.Run();
316 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
319 } // namespace chromeos