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/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/chrome_notification_types.h"
20 #include "chrome/browser/extensions/external_provider_impl.h"
21 #include "chrome/browser/extensions/updater/extension_downloader.h"
22 #include "chrome/common/chrome_paths.h"
23 #include "chrome/common/extensions/extension_constants.h"
24 #include "chrome/test/base/testing_browser_process.h"
25 #include "components/policy/core/common/cloud/mock_cloud_policy_store.h"
26 #include "components/policy/core/common/policy_map.h"
27 #include "components/policy/core/common/policy_types.h"
28 #include "content/public/browser/notification_service.h"
29 #include "content/public/browser/notification_source.h"
30 #include "content/public/test/test_browser_thread_bundle.h"
31 #include "content/public/test/test_utils.h"
32 #include "extensions/browser/external_provider_interface.h"
33 #include "extensions/common/extension.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 using ::testing::InvokeWithoutArgs
;
45 using ::testing::Mock
;
52 const char kCacheDir
[] = "cache";
53 const char kExtensionId
[] = "ldnnhddmnhbkjipkidpdiheffobcpfmf";
54 const char kExtensionUpdateManifest
[] =
55 "extensions/good_v1_update_manifest.xml";
56 const char kExtensionCRXSourceDir
[] = "extensions";
57 const char kExtensionCRXFile
[] = "good.crx";
58 const char kExtensionCRXVersion
[] = "1.0.0.0";
60 class MockExternalPolicyProviderVisitor
61 : public extensions::ExternalProviderInterface::VisitorInterface
{
63 MockExternalPolicyProviderVisitor();
64 virtual ~MockExternalPolicyProviderVisitor();
66 MOCK_METHOD6(OnExternalExtensionFileFound
,
67 bool(const std::string
&,
69 const base::FilePath
&,
70 extensions::Manifest::Location
,
73 MOCK_METHOD5(OnExternalExtensionUpdateUrlFound
,
74 bool(const std::string
&,
76 extensions::Manifest::Location
,
79 MOCK_METHOD1(OnExternalProviderReady
,
80 void(const extensions::ExternalProviderInterface
* provider
));
83 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor
);
86 MockExternalPolicyProviderVisitor::MockExternalPolicyProviderVisitor() {
89 MockExternalPolicyProviderVisitor::~MockExternalPolicyProviderVisitor() {
94 class DeviceLocalAccountExternalPolicyLoaderTest
: public testing::Test
{
96 DeviceLocalAccountExternalPolicyLoaderTest();
97 virtual ~DeviceLocalAccountExternalPolicyLoaderTest();
99 virtual void SetUp() OVERRIDE
;
100 virtual void TearDown() OVERRIDE
;
102 void VerifyAndResetVisitorCallExpectations();
103 void SetForceInstallListPolicy();
105 content::TestBrowserThreadBundle thread_bundle_
;
106 base::ScopedTempDir temp_dir_
;
107 base::FilePath cache_dir_
;
108 policy::MockCloudPolicyStore store_
;
109 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
110 base::FilePath test_dir_
;
112 scoped_refptr
<DeviceLocalAccountExternalPolicyLoader
> loader_
;
113 MockExternalPolicyProviderVisitor visitor_
;
114 scoped_ptr
<extensions::ExternalProviderImpl
> provider_
;
116 content::InProcessUtilityThreadHelper in_process_utility_thread_helper_
;
119 DeviceLocalAccountExternalPolicyLoaderTest::
120 DeviceLocalAccountExternalPolicyLoaderTest()
121 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {
124 DeviceLocalAccountExternalPolicyLoaderTest::
125 ~DeviceLocalAccountExternalPolicyLoaderTest() {
128 void DeviceLocalAccountExternalPolicyLoaderTest::SetUp() {
129 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
130 cache_dir_
= temp_dir_
.path().Append(kCacheDir
);
131 ASSERT_TRUE(base::CreateDirectoryAndGetError(cache_dir_
, NULL
));
132 request_context_getter_
=
133 new net::TestURLRequestContextGetter(base::MessageLoopProxy::current());
134 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(
135 request_context_getter_
.get());
136 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &test_dir_
));
138 loader_
= new DeviceLocalAccountExternalPolicyLoader(&store_
, cache_dir_
);
139 provider_
.reset(new extensions::ExternalProviderImpl(
143 extensions::Manifest::EXTERNAL_POLICY
,
144 extensions::Manifest::EXTERNAL_POLICY_DOWNLOAD
,
145 extensions::Extension::NO_FLAGS
));
147 VerifyAndResetVisitorCallExpectations();
150 void DeviceLocalAccountExternalPolicyLoaderTest::TearDown() {
151 TestingBrowserProcess::GetGlobal()->SetSystemRequestContext(NULL
);
154 void DeviceLocalAccountExternalPolicyLoaderTest::
155 VerifyAndResetVisitorCallExpectations() {
156 Mock::VerifyAndClearExpectations(&visitor_
);
157 EXPECT_CALL(visitor_
, OnExternalExtensionFileFound(_
, _
, _
, _
, _
, _
))
159 EXPECT_CALL(visitor_
, OnExternalExtensionUpdateUrlFound(_
, _
, _
, _
, _
))
161 EXPECT_CALL(visitor_
, OnExternalProviderReady(_
))
165 void DeviceLocalAccountExternalPolicyLoaderTest::SetForceInstallListPolicy() {
166 scoped_ptr
<base::ListValue
> forcelist(new base::ListValue
);
167 forcelist
->AppendString("invalid");
168 forcelist
->AppendString(base::StringPrintf(
171 extension_urls::GetWebstoreUpdateUrl().spec().c_str()));
172 store_
.policy_map_
.Set(policy::key::kExtensionInstallForcelist
,
173 policy::POLICY_LEVEL_MANDATORY
,
174 policy::POLICY_SCOPE_USER
,
177 store_
.NotifyStoreLoaded();
180 // Verifies that when the cache is not explicitly started, the loader does not
181 // serve any extensions, even if the force-install list policy is set or a load
182 // is manually requested.
183 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, CacheNotStarted
) {
184 // Set the force-install list policy.
185 SetForceInstallListPolicy();
187 // Manually request a load.
188 loader_
->StartLoading();
190 EXPECT_FALSE(loader_
->IsCacheRunning());
191 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
194 // Verifies that the cache can be started and stopped correctly.
195 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, ForceInstallListEmpty
) {
196 // Set an empty force-install list policy.
197 store_
.NotifyStoreLoaded();
199 // Start the cache. Verify that the loader announces an empty extension list.
200 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
202 loader_
->StartCache(base::MessageLoopProxy::current());
203 base::RunLoop().RunUntilIdle();
204 VerifyAndResetVisitorCallExpectations();
206 // Stop the cache. Verify that the loader announces an empty extension list.
207 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
209 base::RunLoop run_loop
;
210 loader_
->StopCache(run_loop
.QuitClosure());
211 VerifyAndResetVisitorCallExpectations();
213 // Spin the loop until the cache shutdown callback is invoked. Verify that at
214 // that point, no further file I/O tasks are pending.
216 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
219 // Verifies that when a force-install list policy referencing an extension is
220 // set and the cache is started, the loader downloads, caches and serves the
222 TEST_F(DeviceLocalAccountExternalPolicyLoaderTest
, ForceInstallListSet
) {
223 // Set a force-install list policy that contains an invalid entry (which
224 // should be ignored) and a valid reference to an extension.
225 SetForceInstallListPolicy();
228 loader_
->StartCache(base::MessageLoopProxy::current());
230 // Spin the loop, allowing the loader to process the force-install list.
231 // Verify that the loader announces an empty extension list.
232 net::TestURLFetcherFactory factory
;
233 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
235 base::MessageLoop::current()->RunUntilIdle();
237 // Verify that a downloader has started and is attempting to download an
239 net::TestURLFetcher
* fetcher
= factory
.GetFetcherByID(
240 extensions::ExtensionDownloader::kManifestFetcherId
);
241 ASSERT_TRUE(fetcher
);
242 ASSERT_TRUE(fetcher
->delegate());
244 // Return a manifest to the downloader.
245 std::string manifest
;
246 EXPECT_TRUE(base::ReadFileToString(test_dir_
.Append(kExtensionUpdateManifest
),
248 fetcher
->set_response_code(200);
249 fetcher
->SetResponseString(manifest
);
250 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
252 // Wait for the manifest to be parsed.
253 content::WindowedNotificationObserver(
254 chrome::NOTIFICATION_EXTENSION_UPDATE_FOUND
,
255 content::NotificationService::AllSources()).Wait();
257 // Verify that the downloader is attempting to download a CRX file.
258 fetcher
= factory
.GetFetcherByID(
259 extensions::ExtensionDownloader::kExtensionFetcherId
);
260 ASSERT_TRUE(fetcher
);
261 ASSERT_TRUE(fetcher
->delegate());
263 // Create a temporary CRX file and return its path to the downloader.
264 EXPECT_TRUE(base::CopyFile(
265 test_dir_
.Append(kExtensionCRXSourceDir
).Append(kExtensionCRXFile
),
266 temp_dir_
.path().Append(kExtensionCRXFile
)));
267 fetcher
->set_response_code(200);
268 fetcher
->SetResponseFilePath(temp_dir_
.path().Append(kExtensionCRXFile
));
269 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
271 // Spin the loop. Verify that the loader announces the presence of a new CRX
272 // file, served from the cache directory.
273 const base::FilePath cached_crx_path
= cache_dir_
.Append(base::StringPrintf(
274 "%s-%s.crx", kExtensionId
, kExtensionCRXVersion
));
275 base::RunLoop cache_run_loop
;
276 EXPECT_CALL(visitor_
, OnExternalExtensionFileFound(
280 extensions::Manifest::EXTERNAL_POLICY
,
283 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
285 .WillOnce(InvokeWithoutArgs(&cache_run_loop
, &base::RunLoop::Quit
));
286 cache_run_loop
.Run();
287 VerifyAndResetVisitorCallExpectations();
289 // Verify that the CRX file actually exists in the cache directory and its
290 // contents matches the file returned to the downloader.
291 EXPECT_TRUE(base::ContentsEqual(
292 test_dir_
.Append(kExtensionCRXSourceDir
).Append(kExtensionCRXFile
),
295 // Stop the cache. Verify that the loader announces an empty extension list.
296 EXPECT_CALL(visitor_
, OnExternalProviderReady(provider_
.get()))
298 base::RunLoop shutdown_run_loop
;
299 loader_
->StopCache(shutdown_run_loop
.QuitClosure());
300 VerifyAndResetVisitorCallExpectations();
302 // Spin the loop until the cache shutdown callback is invoked. Verify that at
303 // that point, no further file I/O tasks are pending.
304 shutdown_run_loop
.Run();
305 EXPECT_TRUE(base::MessageLoop::current()->IsIdleForTesting());
308 } // namespace chromeos