Remove linux_chromium_gn_dbg from the chromium CQ.
[chromium-blink-merge.git] / extensions / browser / sandboxed_unpacker_unittest.cc
bloba66b2fa5423cebaca15281d8d80a556ac64507da
1 // Copyright (c) 2012 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 "base/base64.h"
6 #include "base/bind.h"
7 #include "base/command_line.h"
8 #include "base/files/file_util.h"
9 #include "base/memory/ref_counted.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 "components/crx_file/id_util.h"
16 #include "content/public/test/test_browser_thread_bundle.h"
17 #include "content/public/test/test_utils.h"
18 #include "extensions/browser/extensions_test.h"
19 #include "extensions/browser/sandboxed_unpacker.h"
20 #include "extensions/common/constants.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/extension_paths.h"
23 #include "extensions/common/switches.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/skia/include/core/SkBitmap.h"
26 #include "third_party/zlib/google/zip.h"
28 namespace extensions {
30 class MockSandboxedUnpackerClient : public SandboxedUnpackerClient {
31 public:
32 void WaitForUnpack() {
33 scoped_refptr<content::MessageLoopRunner> runner =
34 new content::MessageLoopRunner;
35 quit_closure_ = runner->QuitClosure();
36 runner->Run();
39 base::FilePath temp_dir() const { return temp_dir_; }
40 base::string16 unpack_err() const { return error_; }
42 private:
43 ~MockSandboxedUnpackerClient() override {}
45 void OnUnpackSuccess(const base::FilePath& temp_dir,
46 const base::FilePath& extension_root,
47 const base::DictionaryValue* original_manifest,
48 const Extension* extension,
49 const SkBitmap& install_icon) override {
50 temp_dir_ = temp_dir;
51 quit_closure_.Run();
54 void OnUnpackFailure(const CrxInstallError& error) override {
55 error_ = error.message();
56 quit_closure_.Run();
59 base::string16 error_;
60 base::Closure quit_closure_;
61 base::FilePath temp_dir_;
64 class SandboxedUnpackerTest : public ExtensionsTest {
65 public:
66 void SetUp() override {
67 ExtensionsTest::SetUp();
68 ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir());
69 browser_threads_.reset(new content::TestBrowserThreadBundle(
70 content::TestBrowserThreadBundle::IO_MAINLOOP));
71 in_process_utility_thread_helper_.reset(
72 new content::InProcessUtilityThreadHelper);
73 // It will delete itself.
74 client_ = new MockSandboxedUnpackerClient;
76 sandboxed_unpacker_ = new SandboxedUnpacker(
77 Manifest::INTERNAL, Extension::NO_FLAGS, extensions_dir_.path(),
78 base::ThreadTaskRunnerHandle::Get(), client_);
81 void TearDown() override {
82 // Need to destruct SandboxedUnpacker before the message loop since
83 // it posts a task to it.
84 sandboxed_unpacker_ = NULL;
85 base::RunLoop().RunUntilIdle();
86 ExtensionsTest::TearDown();
89 base::FilePath GetCrxFullPath(const std::string& crx_name) {
90 base::FilePath full_path;
91 EXPECT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &full_path));
92 full_path = full_path.AppendASCII("unpacker").AppendASCII(crx_name);
93 EXPECT_TRUE(base::PathExists(full_path)) << full_path.value();
94 return full_path;
97 void SetupUnpacker(const std::string& crx_name,
98 const std::string& package_hash) {
99 base::FilePath crx_path = GetCrxFullPath(crx_name);
100 base::ThreadTaskRunnerHandle::Get()->PostTask(
101 FROM_HERE,
102 base::Bind(
103 &SandboxedUnpacker::StartWithCrx, sandboxed_unpacker_,
104 extensions::CRXFileInfo(std::string(), crx_path, package_hash)));
105 client_->WaitForUnpack();
108 void SetupUnpackerWithDirectory(const std::string& crx_name) {
109 base::ScopedTempDir temp_dir;
110 ASSERT_TRUE(temp_dir.CreateUniqueTempDir());
111 base::FilePath crx_path = GetCrxFullPath(crx_name);
112 ASSERT_TRUE(zip::Unzip(crx_path, temp_dir.path()));
114 std::string fake_id = crx_file::id_util::GenerateId(crx_name);
115 std::string fake_public_key;
116 base::Base64Encode(std::string(2048, 'k'), &fake_public_key);
117 base::ThreadTaskRunnerHandle::Get()->PostTask(
118 FROM_HERE, base::Bind(&SandboxedUnpacker::StartWithDirectory,
119 sandboxed_unpacker_.get(), fake_id,
120 fake_public_key, temp_dir.Take()));
121 client_->WaitForUnpack();
124 base::FilePath GetInstallPath() {
125 return client_->temp_dir().AppendASCII(kTempExtensionName);
128 base::string16 GetInstallError() { return client_->unpack_err(); }
130 protected:
131 base::ScopedTempDir extensions_dir_;
132 MockSandboxedUnpackerClient* client_;
133 scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_;
134 scoped_ptr<content::TestBrowserThreadBundle> browser_threads_;
135 scoped_ptr<content::InProcessUtilityThreadHelper>
136 in_process_utility_thread_helper_;
139 TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) {
140 SetupUnpacker("no_l10n.crx", "");
141 // Check that there is no _locales folder.
142 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
143 EXPECT_FALSE(base::PathExists(install_path));
146 TEST_F(SandboxedUnpackerTest, FromDirNoCatalogsSuccess) {
147 SetupUnpackerWithDirectory("no_l10n.crx");
148 // Check that there is no _locales folder.
149 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
150 EXPECT_FALSE(base::PathExists(install_path));
153 TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) {
154 SetupUnpacker("good_l10n.crx", "");
155 // Check that there is _locales folder.
156 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
157 EXPECT_TRUE(base::PathExists(install_path));
160 TEST_F(SandboxedUnpackerTest, FromDirWithCatalogsSuccess) {
161 SetupUnpackerWithDirectory("good_l10n.crx");
162 // Check that there is _locales folder.
163 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
164 EXPECT_TRUE(base::PathExists(install_path));
167 TEST_F(SandboxedUnpackerTest, FailHashCheck) {
168 base::CommandLine::ForCurrentProcess()->AppendSwitch(
169 extensions::switches::kEnableCrxHashCheck);
170 SetupUnpacker("good_l10n.crx", "badhash");
171 // Check that there is an error message.
172 EXPECT_NE(base::string16(), GetInstallError());
175 TEST_F(SandboxedUnpackerTest, PassHashCheck) {
176 base::CommandLine::ForCurrentProcess()->AppendSwitch(
177 extensions::switches::kEnableCrxHashCheck);
178 SetupUnpacker(
179 "good_l10n.crx",
180 "6fa171c726373785aa4fcd2df448c3db0420a95d5044fbee831f089b979c4068");
181 // Check that there is no error message.
182 EXPECT_EQ(base::string16(), GetInstallError());
185 TEST_F(SandboxedUnpackerTest, SkipHashCheck) {
186 SetupUnpacker("good_l10n.crx", "badhash");
187 // Check that there is no error message.
188 EXPECT_EQ(base::string16(), GetInstallError());
191 } // namespace extensions