Elim cr-checkbox
[chromium-blink-merge.git] / extensions / utility / unpacker_unittest.cc
blob82e5a1f7dc1e5bcf4decd4591649df51a1d4790b
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/pattern.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "extensions/common/constants.h"
13 #include "extensions/common/extension.h"
14 #include "extensions/common/extension_paths.h"
15 #include "extensions/common/manifest_constants.h"
16 #include "extensions/test/test_extensions_client.h"
17 #include "extensions/utility/unpacker.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "third_party/zlib/google/zip.h"
22 using base::ASCIIToUTF16;
24 namespace extensions {
26 namespace errors = manifest_errors;
27 namespace keys = manifest_keys;
29 class UnpackerTest : public testing::Test {
30 public:
31 ~UnpackerTest() override {
32 VLOG(1) << "Deleting temp dir: " << temp_dir_.path().LossyDisplayName();
33 VLOG(1) << temp_dir_.Delete();
36 void SetupUnpacker(const std::string& crx_name) {
37 base::FilePath crx_path;
38 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &crx_path));
39 crx_path = crx_path.AppendASCII("unpacker").AppendASCII(crx_name);
40 ASSERT_TRUE(base::PathExists(crx_path)) << crx_path.value();
42 // Try bots won't let us write into DIR_TEST_DATA, so we have to create
43 // a temp folder to play in.
44 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
46 base::FilePath unzipped_dir = temp_dir_.path().AppendASCII("unzipped");
47 ASSERT_TRUE(zip::Unzip(crx_path, unzipped_dir))
48 << "Failed to unzip " << crx_path.value() << " to "
49 << unzipped_dir.value();
51 unpacker_.reset(new Unpacker(temp_dir_.path(), unzipped_dir, std::string(),
52 Manifest::INTERNAL, Extension::NO_FLAGS));
55 protected:
56 base::ScopedTempDir temp_dir_;
57 scoped_ptr<Unpacker> unpacker_;
60 TEST_F(UnpackerTest, EmptyDefaultLocale) {
61 SetupUnpacker("empty_default_locale.crx");
62 EXPECT_FALSE(unpacker_->Run());
63 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
64 unpacker_->error_message());
67 TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) {
68 SetupUnpacker("has_default_missing_locales.crx");
69 EXPECT_FALSE(unpacker_->Run());
70 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
71 unpacker_->error_message());
74 TEST_F(UnpackerTest, InvalidDefaultLocale) {
75 SetupUnpacker("invalid_default_locale.crx");
76 EXPECT_FALSE(unpacker_->Run());
77 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
78 unpacker_->error_message());
81 TEST_F(UnpackerTest, InvalidMessagesFile) {
82 SetupUnpacker("invalid_messages_file.crx");
83 EXPECT_FALSE(unpacker_->Run());
84 EXPECT_TRUE(base::MatchPattern(
85 unpacker_->error_message(),
86 ASCIIToUTF16(
87 "*_locales?en_US?messages.json: Line: 2, column: 11,"
88 " Syntax error.")))
89 << unpacker_->error_message();
92 TEST_F(UnpackerTest, MissingDefaultData) {
93 SetupUnpacker("missing_default_data.crx");
94 EXPECT_FALSE(unpacker_->Run());
95 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
96 unpacker_->error_message());
99 TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) {
100 SetupUnpacker("missing_default_has_locales.crx");
101 EXPECT_FALSE(unpacker_->Run());
102 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
103 unpacker_->error_message());
106 TEST_F(UnpackerTest, MissingMessagesFile) {
107 SetupUnpacker("missing_messages_file.crx");
108 EXPECT_FALSE(unpacker_->Run());
109 EXPECT_TRUE(
110 base::MatchPattern(unpacker_->error_message(),
111 ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
112 ASCIIToUTF16("*_locales?en_US?messages.json")));
115 TEST_F(UnpackerTest, NoLocaleData) {
116 SetupUnpacker("no_locale_data.crx");
117 EXPECT_FALSE(unpacker_->Run());
118 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
119 unpacker_->error_message());
122 TEST_F(UnpackerTest, GoodL10n) {
123 SetupUnpacker("good_l10n.crx");
124 EXPECT_TRUE(unpacker_->Run());
125 EXPECT_TRUE(unpacker_->error_message().empty());
126 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
129 TEST_F(UnpackerTest, NoL10n) {
130 SetupUnpacker("no_l10n.crx");
131 EXPECT_TRUE(unpacker_->Run());
132 EXPECT_TRUE(unpacker_->error_message().empty());
133 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
136 namespace {
138 // Inserts an illegal path into the browser images returned by
139 // TestExtensionsClient for any extension.
140 class IllegalImagePathInserter
141 : public TestExtensionsClient::BrowserImagePathsFilter {
142 public:
143 IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) {
144 client_->AddBrowserImagePathsFilter(this);
147 virtual ~IllegalImagePathInserter() {
148 client_->RemoveBrowserImagePathsFilter(this);
151 void Filter(const Extension* extension,
152 std::set<base::FilePath>* paths) override {
153 base::FilePath illegal_path =
154 base::FilePath(base::FilePath::kParentDirectory)
155 .AppendASCII(kTempExtensionName)
156 .AppendASCII("product_logo_128.png");
157 paths->insert(illegal_path);
160 private:
161 TestExtensionsClient* client_;
164 } // namespace
166 TEST_F(UnpackerTest, BadPathError) {
167 const char kExpected[] = "Illegal path (absolute or relative with '..'): ";
168 SetupUnpacker("good_package.crx");
169 IllegalImagePathInserter inserter(
170 static_cast<TestExtensionsClient*>(ExtensionsClient::Get()));
172 EXPECT_FALSE(unpacker_->Run());
173 EXPECT_TRUE(base::StartsWith(unpacker_->error_message(),
174 ASCIIToUTF16(kExpected),
175 base::CompareCase::INSENSITIVE_ASCII))
176 << "Expected prefix: \"" << kExpected << "\", actual error: \""
177 << unpacker_->error_message() << "\"";
180 TEST_F(UnpackerTest, ImageDecodingError) {
181 const char kExpected[] = "Could not decode image: ";
182 SetupUnpacker("bad_image.crx");
183 EXPECT_FALSE(unpacker_->Run());
184 EXPECT_TRUE(base::StartsWith(unpacker_->error_message(),
185 ASCIIToUTF16(kExpected),
186 base::CompareCase::INSENSITIVE_ASCII))
187 << "Expected prefix: \"" << kExpected << "\", actual error: \""
188 << unpacker_->error_message() << "\"";
191 } // namespace extensions