Clean up extension confirmation prompts and make them consistent between Views and...
[chromium-blink-merge.git] / extensions / utility / unpacker_unittest.cc
blobb323e01eed50aa12f1ddd34133c347af19761692
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"
19 #include "third_party/zlib/google/zip.h"
21 using base::ASCIIToUTF16;
23 namespace extensions {
25 namespace errors = manifest_errors;
26 namespace keys = manifest_keys;
28 class UnpackerTest : public testing::Test {
29 public:
30 ~UnpackerTest() override {
31 VLOG(1) << "Deleting temp dir: " << temp_dir_.path().LossyDisplayName();
32 VLOG(1) << temp_dir_.Delete();
35 void SetupUnpacker(const std::string& crx_name) {
36 base::FilePath crx_path;
37 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &crx_path));
38 crx_path = crx_path.AppendASCII("unpacker").AppendASCII(crx_name);
39 ASSERT_TRUE(base::PathExists(crx_path)) << crx_path.value();
41 // Try bots won't let us write into DIR_TEST_DATA, so we have to create
42 // a temp folder to play in.
43 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
45 base::FilePath unzipped_dir = temp_dir_.path().AppendASCII("unzipped");
46 ASSERT_TRUE(zip::Unzip(crx_path, unzipped_dir))
47 << "Failed to unzip " << crx_path.value() << " to "
48 << unzipped_dir.value();
50 unpacker_.reset(new Unpacker(temp_dir_.path(), unzipped_dir, std::string(),
51 Manifest::INTERNAL, Extension::NO_FLAGS));
54 protected:
55 base::ScopedTempDir temp_dir_;
56 scoped_ptr<Unpacker> unpacker_;
59 TEST_F(UnpackerTest, EmptyDefaultLocale) {
60 SetupUnpacker("empty_default_locale.crx");
61 EXPECT_FALSE(unpacker_->Run());
62 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
63 unpacker_->error_message());
66 TEST_F(UnpackerTest, HasDefaultLocaleMissingLocalesFolder) {
67 SetupUnpacker("has_default_missing_locales.crx");
68 EXPECT_FALSE(unpacker_->Run());
69 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
70 unpacker_->error_message());
73 TEST_F(UnpackerTest, InvalidDefaultLocale) {
74 SetupUnpacker("invalid_default_locale.crx");
75 EXPECT_FALSE(unpacker_->Run());
76 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
77 unpacker_->error_message());
80 TEST_F(UnpackerTest, InvalidMessagesFile) {
81 SetupUnpacker("invalid_messages_file.crx");
82 EXPECT_FALSE(unpacker_->Run());
83 EXPECT_TRUE(
84 MatchPattern(unpacker_->error_message(),
85 ASCIIToUTF16(
86 "*_locales?en_US?messages.json: Line: 2, column: 11,"
87 " Syntax error.")))
88 << unpacker_->error_message();
91 TEST_F(UnpackerTest, MissingDefaultData) {
92 SetupUnpacker("missing_default_data.crx");
93 EXPECT_FALSE(unpacker_->Run());
94 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
95 unpacker_->error_message());
98 TEST_F(UnpackerTest, MissingDefaultLocaleHasLocalesFolder) {
99 SetupUnpacker("missing_default_has_locales.crx");
100 EXPECT_FALSE(unpacker_->Run());
101 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
102 unpacker_->error_message());
105 TEST_F(UnpackerTest, MissingMessagesFile) {
106 SetupUnpacker("missing_messages_file.crx");
107 EXPECT_FALSE(unpacker_->Run());
108 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
109 ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
110 ASCIIToUTF16("*_locales?en_US?messages.json")));
113 TEST_F(UnpackerTest, NoLocaleData) {
114 SetupUnpacker("no_locale_data.crx");
115 EXPECT_FALSE(unpacker_->Run());
116 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
117 unpacker_->error_message());
120 TEST_F(UnpackerTest, GoodL10n) {
121 SetupUnpacker("good_l10n.crx");
122 EXPECT_TRUE(unpacker_->Run());
123 EXPECT_TRUE(unpacker_->error_message().empty());
124 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
127 TEST_F(UnpackerTest, NoL10n) {
128 SetupUnpacker("no_l10n.crx");
129 EXPECT_TRUE(unpacker_->Run());
130 EXPECT_TRUE(unpacker_->error_message().empty());
131 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
134 namespace {
136 // Inserts an illegal path into the browser images returned by
137 // TestExtensionsClient for any extension.
138 class IllegalImagePathInserter
139 : public TestExtensionsClient::BrowserImagePathsFilter {
140 public:
141 IllegalImagePathInserter(TestExtensionsClient* client) : client_(client) {
142 client_->AddBrowserImagePathsFilter(this);
145 virtual ~IllegalImagePathInserter() {
146 client_->RemoveBrowserImagePathsFilter(this);
149 void Filter(const Extension* extension,
150 std::set<base::FilePath>* paths) override {
151 base::FilePath illegal_path =
152 base::FilePath(base::FilePath::kParentDirectory)
153 .AppendASCII(kTempExtensionName)
154 .AppendASCII("product_logo_128.png");
155 paths->insert(illegal_path);
158 private:
159 TestExtensionsClient* client_;
162 } // namespace
164 TEST_F(UnpackerTest, BadPathError) {
165 const char kExpected[] = "Illegal path (absolute or relative with '..'): ";
166 SetupUnpacker("good_package.crx");
167 IllegalImagePathInserter inserter(
168 static_cast<TestExtensionsClient*>(ExtensionsClient::Get()));
170 EXPECT_FALSE(unpacker_->Run());
171 EXPECT_TRUE(base::StartsWith(unpacker_->error_message(),
172 ASCIIToUTF16(kExpected), false))
173 << "Expected prefix: \"" << kExpected << "\", actual error: \""
174 << unpacker_->error_message() << "\"";
177 TEST_F(UnpackerTest, ImageDecodingError) {
178 const char kExpected[] = "Could not decode image: ";
179 SetupUnpacker("bad_image.crx");
180 EXPECT_FALSE(unpacker_->Run());
181 EXPECT_TRUE(base::StartsWith(unpacker_->error_message(),
182 ASCIIToUTF16(kExpected), false))
183 << "Expected prefix: \"" << kExpected << "\", actual error: \""
184 << unpacker_->error_message() << "\"";
187 } // namespace extensions