Make castv2 performance test work.
[chromium-blink-merge.git] / extensions / utility / unpacker_unittest.cc
blob917f66e9c2f99d2d3d87c497f71771f67b17b064
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/files/file_util.h"
6 #include "base/files/scoped_temp_dir.h"
7 #include "base/path_service.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "extensions/common/constants.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/extension_paths.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/test/test_extensions_client.h"
16 #include "extensions/utility/unpacker.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
20 using base::ASCIIToUTF16;
22 namespace extensions {
24 namespace errors = manifest_errors;
25 namespace keys = manifest_keys;
27 class UnpackerTest : public testing::Test {
28 public:
29 ~UnpackerTest() override {
30 VLOG(1) << "Deleting temp dir: " << temp_dir_.path().LossyDisplayName();
31 VLOG(1) << temp_dir_.Delete();
34 void SetupUnpacker(const std::string& crx_name) {
35 base::FilePath original_path;
36 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &original_path));
37 original_path = original_path.AppendASCII("unpacker").AppendASCII(crx_name);
38 ASSERT_TRUE(base::PathExists(original_path)) << original_path.value();
40 // Try bots won't let us write into DIR_TEST_DATA, so we have to create
41 // a temp folder to play in.
42 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
44 base::FilePath crx_path = temp_dir_.path().AppendASCII(crx_name);
45 ASSERT_TRUE(base::CopyFile(original_path, crx_path))
46 << "Original path " << original_path.value() << ", Crx path "
47 << crx_path.value();
49 unpacker_.reset(new Unpacker(crx_path, std::string(), Manifest::INTERNAL,
50 Extension::NO_FLAGS));
53 protected:
54 base::ScopedTempDir temp_dir_;
55 scoped_ptr<Unpacker> unpacker_;
58 TEST_F(UnpackerTest, EmptyDefaultLocale) {
59 SetupUnpacker("empty_default_locale.crx");
60 EXPECT_FALSE(unpacker_->Run());
61 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
62 unpacker_->error_message());
65 TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) {
66 SetupUnpacker("has_default_missing_locales.crx");
67 EXPECT_FALSE(unpacker_->Run());
68 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
69 unpacker_->error_message());
72 TEST_F(UnpackerTest, InvalidDefaultLocale) {
73 SetupUnpacker("invalid_default_locale.crx");
74 EXPECT_FALSE(unpacker_->Run());
75 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
76 unpacker_->error_message());
79 TEST_F(UnpackerTest, InvalidMessagesFile) {
80 SetupUnpacker("invalid_messages_file.crx");
81 EXPECT_FALSE(unpacker_->Run());
82 EXPECT_TRUE(
83 MatchPattern(unpacker_->error_message(),
84 ASCIIToUTF16(
85 "*_locales?en_US?messages.json: Line: 2, column: 11,"
86 " Syntax error.")))
87 << unpacker_->error_message();
90 TEST_F(UnpackerTest, MissingDefaultData) {
91 SetupUnpacker("missing_default_data.crx");
92 EXPECT_FALSE(unpacker_->Run());
93 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
94 unpacker_->error_message());
97 TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) {
98 SetupUnpacker("missing_default_has_locales.crx");
99 EXPECT_FALSE(unpacker_->Run());
100 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
101 unpacker_->error_message());
104 TEST_F(UnpackerTest, MissingMessagesFile) {
105 SetupUnpacker("missing_messages_file.crx");
106 EXPECT_FALSE(unpacker_->Run());
107 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
108 ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
109 ASCIIToUTF16("*_locales?en_US?messages.json")));
112 TEST_F(UnpackerTest, NoLocaleData) {
113 SetupUnpacker("no_locale_data.crx");
114 EXPECT_FALSE(unpacker_->Run());
115 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
116 unpacker_->error_message());
119 TEST_F(UnpackerTest, GoodL10n) {
120 SetupUnpacker("good_l10n.crx");
121 EXPECT_TRUE(unpacker_->Run());
122 EXPECT_TRUE(unpacker_->error_message().empty());
123 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
126 TEST_F(UnpackerTest, NoL10n) {
127 SetupUnpacker("no_l10n.crx");
128 EXPECT_TRUE(unpacker_->Run());
129 EXPECT_TRUE(unpacker_->error_message().empty());
130 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
133 TEST_F(UnpackerTest, UnzipDirectoryError) {
134 const char kExpected[] = "Could not create directory for unzipping: ";
135 SetupUnpacker("good_package.crx");
136 base::FilePath path = temp_dir_.path().AppendASCII(kTempExtensionName);
137 ASSERT_TRUE(base::WriteFile(path, "foo", 3));
138 EXPECT_FALSE(unpacker_->Run());
139 EXPECT_TRUE(
140 StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false))
141 << "Expected prefix: \"" << kExpected << "\", actual error: \""
142 << unpacker_->error_message() << "\"";
145 TEST_F(UnpackerTest, UnzipError) {
146 const char kExpected[] = "Could not unzip extension";
147 SetupUnpacker("bad_zip.crx");
148 EXPECT_FALSE(unpacker_->Run());
149 EXPECT_EQ(ASCIIToUTF16(kExpected), unpacker_->error_message());
152 namespace {
154 // Inserts an illegal path into the browser images returned by
155 // TestExtensionsClient for any extension.
156 class IllegalImagePathInserter
157 : public TestExtensionsClient::BrowserImagePathsFilter {
158 public:
159 IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) {
160 client_->AddBrowserImagePathsFilter(this);
163 virtual ~IllegalImagePathInserter() {
164 client_->RemoveBrowserImagePathsFilter(this);
167 void Filter(const Extension* extension,
168 std::set<base::FilePath>* paths) override {
169 base::FilePath illegal_path =
170 base::FilePath(base::FilePath::kParentDirectory)
171 .AppendASCII(kTempExtensionName)
172 .AppendASCII("product_logo_128.png");
173 paths->insert(illegal_path);
176 private:
177 TestExtensionsClient* client_;
180 } // namespace
182 TEST_F(UnpackerTest, BadPathError) {
183 const char kExpected[] = "Illegal path (absolute or relative with '..'): ";
184 SetupUnpacker("good_package.crx");
185 IllegalImagePathInserter inserter(
186 static_cast<TestExtensionsClient*>(ExtensionsClient::Get()));
188 EXPECT_FALSE(unpacker_->Run());
189 EXPECT_TRUE(
190 StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false))
191 << "Expected prefix: \"" << kExpected << "\", actual error: \""
192 << unpacker_->error_message() << "\"";
195 TEST_F(UnpackerTest, ImageDecodingError) {
196 const char kExpected[] = "Could not decode image: ";
197 SetupUnpacker("bad_image.crx");
198 EXPECT_FALSE(unpacker_->Run());
199 EXPECT_TRUE(
200 StartsWith(unpacker_->error_message(), ASCIIToUTF16(kExpected), false))
201 << "Expected prefix: \"" << kExpected << "\", actual error: \""
202 << unpacker_->error_message() << "\"";
205 } // namespace extensions