Finish refactoring of DomCodeToUsLayoutKeyboardCode().
[chromium-blink-merge.git] / extensions / browser / sandboxed_unpacker_unittest.cc
blob02fceaf6806b4f3dc68b14d344db89e3cc6a50d8
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/bind.h"
6 #include "base/command_line.h"
7 #include "base/files/file_util.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/path_service.h"
10 #include "base/run_loop.h"
11 #include "base/strings/string_util.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "base/values.h"
14 #include "content/public/test/test_browser_thread_bundle.h"
15 #include "content/public/test/test_utils.h"
16 #include "extensions/browser/extensions_test.h"
17 #include "extensions/browser/sandboxed_unpacker.h"
18 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_paths.h"
21 #include "extensions/common/switches.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/skia/include/core/SkBitmap.h"
25 namespace extensions {
27 class MockSandboxedUnpackerClient : public SandboxedUnpackerClient {
28 public:
29 void WaitForUnpack() {
30 scoped_refptr<content::MessageLoopRunner> runner =
31 new content::MessageLoopRunner;
32 quit_closure_ = runner->QuitClosure();
33 runner->Run();
36 base::FilePath temp_dir() const { return temp_dir_; }
37 base::string16 unpack_err() const { return error_; }
39 private:
40 ~MockSandboxedUnpackerClient() override {}
42 void OnUnpackSuccess(const base::FilePath& temp_dir,
43 const base::FilePath& extension_root,
44 const base::DictionaryValue* original_manifest,
45 const Extension* extension,
46 const SkBitmap& install_icon) override {
47 temp_dir_ = temp_dir;
48 quit_closure_.Run();
51 void OnUnpackFailure(const CrxInstallError& error) override {
52 error_ = error.message();
53 quit_closure_.Run();
56 base::string16 error_;
57 base::Closure quit_closure_;
58 base::FilePath temp_dir_;
61 class SandboxedUnpackerTest : public ExtensionsTest {
62 public:
63 void SetUp() override {
64 ExtensionsTest::SetUp();
65 ASSERT_TRUE(extensions_dir_.CreateUniqueTempDir());
66 browser_threads_.reset(new content::TestBrowserThreadBundle(
67 content::TestBrowserThreadBundle::IO_MAINLOOP));
68 in_process_utility_thread_helper_.reset(
69 new content::InProcessUtilityThreadHelper);
70 // It will delete itself.
71 client_ = new MockSandboxedUnpackerClient;
74 void TearDown() override {
75 // Need to destruct SandboxedUnpacker before the message loop since
76 // it posts a task to it.
77 sandboxed_unpacker_ = NULL;
78 base::RunLoop().RunUntilIdle();
79 ExtensionsTest::TearDown();
82 void SetupUnpacker(const std::string& crx_name,
83 const std::string& package_hash) {
84 base::FilePath original_path;
85 ASSERT_TRUE(PathService::Get(extensions::DIR_TEST_DATA, &original_path));
86 original_path = original_path.AppendASCII("unpacker").AppendASCII(crx_name);
87 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value();
89 sandboxed_unpacker_ = new SandboxedUnpacker(
90 extensions::CRXFileInfo(std::string(), original_path, package_hash),
91 Manifest::INTERNAL, Extension::NO_FLAGS, extensions_dir_.path(),
92 base::ThreadTaskRunnerHandle::Get(), client_);
94 base::ThreadTaskRunnerHandle::Get()->PostTask(
95 FROM_HERE,
96 base::Bind(&SandboxedUnpacker::Start, sandboxed_unpacker_.get()));
97 client_->WaitForUnpack();
100 base::FilePath GetInstallPath() {
101 return client_->temp_dir().AppendASCII(kTempExtensionName);
104 base::string16 GetInstallError() { return client_->unpack_err(); }
106 protected:
107 base::ScopedTempDir extensions_dir_;
108 MockSandboxedUnpackerClient* client_;
109 scoped_refptr<SandboxedUnpacker> sandboxed_unpacker_;
110 scoped_ptr<content::TestBrowserThreadBundle> browser_threads_;
111 scoped_ptr<content::InProcessUtilityThreadHelper>
112 in_process_utility_thread_helper_;
115 TEST_F(SandboxedUnpackerTest, NoCatalogsSuccess) {
116 SetupUnpacker("no_l10n.crx", "");
117 // Check that there is no _locales folder.
118 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
119 EXPECT_FALSE(base::PathExists(install_path));
122 TEST_F(SandboxedUnpackerTest, WithCatalogsSuccess) {
123 SetupUnpacker("good_l10n.crx", "");
124 // Check that there is _locales folder.
125 base::FilePath install_path = GetInstallPath().Append(kLocaleFolder);
126 EXPECT_TRUE(base::PathExists(install_path));
129 TEST_F(SandboxedUnpackerTest, FailHashCheck) {
130 base::CommandLine::ForCurrentProcess()->AppendSwitch(
131 extensions::switches::kEnableCrxHashCheck);
132 SetupUnpacker("good_l10n.crx", "badhash");
133 // Check that there is an error message.
134 EXPECT_NE(base::string16(), GetInstallError());
137 TEST_F(SandboxedUnpackerTest, PassHashCheck) {
138 base::CommandLine::ForCurrentProcess()->AppendSwitch(
139 extensions::switches::kEnableCrxHashCheck);
140 SetupUnpacker(
141 "good_l10n.crx",
142 "6fa171c726373785aa4fcd2df448c3db0420a95d5044fbee831f089b979c4068");
143 // Check that there is no error message.
144 EXPECT_EQ(base::string16(), GetInstallError());
147 TEST_F(SandboxedUnpackerTest, SkipHashCheck) {
148 SetupUnpacker("good_l10n.crx", "badhash");
149 // Check that there is no error message.
150 EXPECT_EQ(base::string16(), GetInstallError());
153 } // namespace extensions