1 // Copyright 2014 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.
6 #include "base/command_line.h"
7 #include "base/files/file_util.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/path_service.h"
11 #include "base/run_loop.h"
12 #include "base/strings/string_util.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/values.h"
15 #include "chrome/browser/extensions/extension_service.h"
16 #include "chrome/browser/extensions/test_extension_system.h"
17 #include "chrome/browser/extensions/zipfile_installer.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/test/base/testing_profile.h"
20 #include "content/public/test/test_browser_thread_bundle.h"
21 #include "content/public/test/test_utils.h"
22 #include "extensions/browser/extension_registry.h"
23 #include "extensions/browser/extension_registry_observer.h"
24 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
30 #include "chrome/browser/chromeos/settings/cros_settings.h"
31 #include "chrome/browser/chromeos/settings/device_settings_service.h"
34 namespace extensions
{
38 struct MockExtensionRegistryObserver
: public ExtensionRegistryObserver
{
39 void WaitForInstall() {
40 scoped_refptr
<content::MessageLoopRunner
> runner
=
41 new content::MessageLoopRunner
;
42 quit_closure_
= runner
->QuitClosure();
46 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
47 const Extension
* extension
,
50 const std::string
& old_name
) override
{
51 last_extension_installed
= extension
->id();
55 std::string last_extension_installed
;
56 base::Closure quit_closure_
;
61 class ZipFileInstallerTest
: public testing::Test
{
63 ZipFileInstallerTest()
64 : browser_threads_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {}
66 void SetUp() override
{
67 in_process_utility_thread_helper_
.reset(
68 new content::InProcessUtilityThreadHelper
);
70 // Create profile for extension service.
71 profile_
.reset(new TestingProfile());
72 TestExtensionSystem
* system
=
73 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile_
.get()));
74 extension_service_
= system
->CreateExtensionService(
75 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
76 ExtensionRegistry
* registry(ExtensionRegistry::Get(profile_
.get()));
77 registry
->AddObserver(&observer_
);
80 void TearDown() override
{
81 // Need to destruct ZipFileInstaller before the message loop since
82 // it posts a task to it.
83 zipfile_installer_
= NULL
;
84 ExtensionRegistry
* registry(ExtensionRegistry::Get(profile_
.get()));
85 registry
->RemoveObserver(&observer_
);
87 base::RunLoop().RunUntilIdle();
90 void RunInstaller(const std::string
& zip_name
) {
91 base::FilePath original_path
;
92 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &original_path
));
93 original_path
= original_path
.AppendASCII("extensions")
94 .AppendASCII("zipfile_installer")
95 .AppendASCII(zip_name
);
96 ASSERT_TRUE(base::PathExists(original_path
)) << original_path
.value();
98 zipfile_installer_
= ZipFileInstaller::Create(extension_service_
);
100 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 base::Bind(&ZipFileInstaller::LoadFromZipFile
,
103 zipfile_installer_
.get(),
105 observer_
.WaitForInstall();
109 scoped_refptr
<ZipFileInstaller
> zipfile_installer_
;
111 scoped_ptr
<TestingProfile
> profile_
;
112 ExtensionService
* extension_service_
;
114 content::TestBrowserThreadBundle browser_threads_
;
115 scoped_ptr
<content::InProcessUtilityThreadHelper
>
116 in_process_utility_thread_helper_
;
117 MockExtensionRegistryObserver observer_
;
119 #if defined(OS_CHROMEOS)
120 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
121 chromeos::ScopedTestCrosSettings test_cros_settings_
;
122 // ChromeOS needs a user manager to instantiate an extension service.
123 chromeos::ScopedTestUserManager test_user_manager_
;
127 TEST_F(ZipFileInstallerTest
, GoodZip
) {
128 RunInstaller("good.zip");
131 TEST_F(ZipFileInstallerTest
, ZipWithPublicKey
) {
132 RunInstaller("public_key.zip");
133 const char kIdForPublicKey
[] = "ikppjpenhoddphklkpdfdfdabbakkpal";
134 EXPECT_EQ(observer_
.last_extension_installed
, kIdForPublicKey
);
137 } // namespace extensions