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/values.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/test_extension_system.h"
16 #include "chrome/browser/extensions/zipfile_installer.h"
17 #include "chrome/common/chrome_paths.h"
18 #include "chrome/test/base/testing_profile.h"
19 #include "content/public/test/test_browser_thread_bundle.h"
20 #include "content/public/test/test_utils.h"
21 #include "extensions/browser/extension_registry.h"
22 #include "extensions/browser/extension_registry_observer.h"
23 #include "extensions/common/constants.h"
24 #include "extensions/common/extension.h"
25 #include "testing/gtest/include/gtest/gtest.h"
27 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/chromeos/login/users/scoped_test_user_manager.h"
29 #include "chrome/browser/chromeos/settings/cros_settings.h"
30 #include "chrome/browser/chromeos/settings/device_settings_service.h"
33 namespace extensions
{
37 struct MockExtensionRegistryObserver
: public ExtensionRegistryObserver
{
38 void WaitForInstall() {
39 scoped_refptr
<content::MessageLoopRunner
> runner
=
40 new content::MessageLoopRunner
;
41 quit_closure_
= runner
->QuitClosure();
45 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
46 const Extension
* extension
,
49 const std::string
& old_name
) override
{
50 last_extension_installed
= extension
->id();
54 std::string last_extension_installed
;
55 base::Closure quit_closure_
;
60 class ZipFileInstallerTest
: public testing::Test
{
62 ZipFileInstallerTest()
63 : browser_threads_(content::TestBrowserThreadBundle::IO_MAINLOOP
) {}
65 void SetUp() override
{
66 in_process_utility_thread_helper_
.reset(
67 new content::InProcessUtilityThreadHelper
);
69 // Create profile for extension service.
70 profile_
.reset(new TestingProfile());
71 TestExtensionSystem
* system
=
72 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(profile_
.get()));
73 extension_service_
= system
->CreateExtensionService(
74 base::CommandLine::ForCurrentProcess(), base::FilePath(), false);
75 ExtensionRegistry
* registry(ExtensionRegistry::Get(profile_
.get()));
76 registry
->AddObserver(&observer_
);
79 void TearDown() override
{
80 // Need to destruct ZipFileInstaller before the message loop since
81 // it posts a task to it.
82 zipfile_installer_
= NULL
;
83 ExtensionRegistry
* registry(ExtensionRegistry::Get(profile_
.get()));
84 registry
->RemoveObserver(&observer_
);
86 base::RunLoop().RunUntilIdle();
89 void RunInstaller(const std::string
& zip_name
) {
90 base::FilePath original_path
;
91 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA
, &original_path
));
92 original_path
= original_path
.AppendASCII("extensions")
93 .AppendASCII("zipfile_installer")
94 .AppendASCII(zip_name
);
95 ASSERT_TRUE(base::PathExists(original_path
)) << original_path
.value();
97 zipfile_installer_
= ZipFileInstaller::Create(extension_service_
);
99 base::MessageLoopProxy::current()->PostTask(
101 base::Bind(&ZipFileInstaller::LoadFromZipFile
,
102 zipfile_installer_
.get(),
104 observer_
.WaitForInstall();
108 scoped_refptr
<ZipFileInstaller
> zipfile_installer_
;
110 scoped_ptr
<TestingProfile
> profile_
;
111 ExtensionService
* extension_service_
;
113 content::TestBrowserThreadBundle browser_threads_
;
114 scoped_ptr
<content::InProcessUtilityThreadHelper
>
115 in_process_utility_thread_helper_
;
116 MockExtensionRegistryObserver observer_
;
118 #if defined(OS_CHROMEOS)
119 chromeos::ScopedTestDeviceSettingsService test_device_settings_service_
;
120 chromeos::ScopedTestCrosSettings test_cros_settings_
;
121 // ChromeOS needs a user manager to instantiate an extension service.
122 chromeos::ScopedTestUserManager test_user_manager_
;
126 TEST_F(ZipFileInstallerTest
, GoodZip
) {
127 RunInstaller("good.zip");
130 TEST_F(ZipFileInstallerTest
, ZipWithPublicKey
) {
131 RunInstaller("public_key.zip");
132 const char kIdForPublicKey
[] = "ikppjpenhoddphklkpdfdfdabbakkpal";
133 EXPECT_EQ(observer_
.last_extension_installed
, kIdForPublicKey
);
136 } // namespace extensions