Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / device_local_account_external_policy_loader_unittest.cc
blobd997ed1e7ccbcbd0c5cff187be90b03811fd0d0a
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/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"
42 #include "url/gurl.h"
44 using ::testing::InvokeWithoutArgs;
45 using ::testing::Mock;
46 using ::testing::_;
48 namespace chromeos {
50 namespace {
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 {
62 public:
63 MockExternalPolicyProviderVisitor();
64 virtual ~MockExternalPolicyProviderVisitor();
66 MOCK_METHOD6(OnExternalExtensionFileFound,
67 bool(const std::string&,
68 const base::Version*,
69 const base::FilePath&,
70 extensions::Manifest::Location,
71 int,
72 bool));
73 MOCK_METHOD5(OnExternalExtensionUpdateUrlFound,
74 bool(const std::string&,
75 const GURL&,
76 extensions::Manifest::Location,
77 int,
78 bool));
79 MOCK_METHOD1(OnExternalProviderReady,
80 void(const extensions::ExternalProviderInterface* provider));
82 private:
83 DISALLOW_COPY_AND_ASSIGN(MockExternalPolicyProviderVisitor);
86 MockExternalPolicyProviderVisitor::MockExternalPolicyProviderVisitor() {
89 MockExternalPolicyProviderVisitor::~MockExternalPolicyProviderVisitor() {
92 } // namespace
94 class DeviceLocalAccountExternalPolicyLoaderTest : public testing::Test {
95 protected:
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(
140 &visitor_,
141 loader_,
142 NULL,
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(_, _, _, _, _, _))
158 .Times(0);
159 EXPECT_CALL(visitor_, OnExternalExtensionUpdateUrlFound(_, _, _, _, _))
160 .Times(0);
161 EXPECT_CALL(visitor_, OnExternalProviderReady(_))
162 .Times(0);
165 void DeviceLocalAccountExternalPolicyLoaderTest::SetForceInstallListPolicy() {
166 scoped_ptr<base::ListValue> forcelist(new base::ListValue);
167 forcelist->AppendString("invalid");
168 forcelist->AppendString(base::StringPrintf(
169 "%s;%s",
170 kExtensionId,
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,
175 forcelist.release(),
176 NULL);
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()))
201 .Times(1);
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()))
208 .Times(1);
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.
215 run_loop.Run();
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
221 // extension.
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();
227 // Start the cache.
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()))
234 .Times(1);
235 base::MessageLoop::current()->RunUntilIdle();
237 // Verify that a downloader has started and is attempting to download an
238 // update manifest.
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),
247 &manifest));
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(
277 kExtensionId,
279 cached_crx_path,
280 extensions::Manifest::EXTERNAL_POLICY,
282 _));
283 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
284 .Times(1)
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),
293 cached_crx_path));
295 // Stop the cache. Verify that the loader announces an empty extension list.
296 EXPECT_CALL(visitor_, OnExternalProviderReady(provider_.get()))
297 .Times(1);
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