Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / unpacker_unittest.cc
blob5f69ebbde2f2b65fc63574decd1fe1083def3ace
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/file_util.h"
6 #include "base/path_service.h"
7 #include "base/scoped_temp_dir.h"
8 #include "base/string_util.h"
9 #include "base/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/extensions/extension_constants.h"
13 #include "chrome/common/extensions/extension_manifest_constants.h"
14 #include "chrome/common/extensions/unpacker.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 #include "third_party/skia/include/core/SkBitmap.h"
18 namespace errors = extension_manifest_errors;
19 namespace filenames = extension_filenames;
20 namespace keys = extension_manifest_keys;
22 namespace extensions {
24 class UnpackerTest : public testing::Test {
25 public:
26 ~UnpackerTest() {
27 LOG(WARNING) << "Deleting temp dir: "
28 << temp_dir_.path().LossyDisplayName();
29 LOG(WARNING) << temp_dir_.Delete();
32 void SetupUnpacker(const std::string& crx_name) {
33 FilePath original_path;
34 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &original_path));
35 original_path = original_path.AppendASCII("extensions")
36 .AppendASCII("unpacker")
37 .AppendASCII(crx_name);
38 ASSERT_TRUE(file_util::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 FilePath crx_path = temp_dir_.path().AppendASCII(crx_name);
45 ASSERT_TRUE(file_util::CopyFile(original_path, crx_path)) <<
46 "Original path " << original_path.value() <<
47 ", Crx path " << crx_path.value();
49 unpacker_.reset(new Unpacker(crx_path,
50 std::string(),
51 Extension::INTERNAL,
52 Extension::NO_FLAGS));
55 protected:
56 ScopedTempDir temp_dir_;
57 scoped_ptr<Unpacker> unpacker_;
60 // Crashes intermittently on Windows, see http://crbug.com/109238
61 #if defined(OS_WIN)
62 #define MAYBE_EmptyDefaultLocale DISABLED_EmptyDefaultLocale
63 #else
64 #define MAYBE_EmptyDefaultLocale EmptyDefaultLocale
65 #endif
66 TEST_F(UnpackerTest, MAYBE_EmptyDefaultLocale) {
67 SetupUnpacker("empty_default_locale.crx");
68 EXPECT_FALSE(unpacker_->Run());
69 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
70 unpacker_->error_message());
73 // Crashes intermittently on Vista, see http://crbug.com/109385
74 #if defined(OS_WIN)
75 #define MAYBE_HasDefaultLocaleMissingLocalesFolder \
76 DISABLED_HasDefaultLocaleMissingLocalesFolder
77 #else
78 #define MAYBE_HasDefaultLocaleMissingLocalesFolder \
79 HasDefaultLocaleMissingLocalesFolder
80 #endif
81 TEST_F(UnpackerTest, MAYBE_HasDefaultLocaleMissingLocalesFolder) {
82 SetupUnpacker("has_default_missing_locales.crx");
83 EXPECT_FALSE(unpacker_->Run());
84 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesTreeMissing),
85 unpacker_->error_message());
88 // Crashes intermittently on Windows, see http://crbug.com/109238
89 #if defined(OS_WIN)
90 #define MAYBE_InvalidDefaultLocale DISABLED_InvalidDefaultLocale
91 #else
92 #define MAYBE_InvalidDefaultLocale InvalidDefaultLocale
93 #endif
94 TEST_F(UnpackerTest, MAYBE_InvalidDefaultLocale) {
95 SetupUnpacker("invalid_default_locale.crx");
96 EXPECT_FALSE(unpacker_->Run());
97 EXPECT_EQ(ASCIIToUTF16(errors::kInvalidDefaultLocale),
98 unpacker_->error_message());
101 // Crashes intermittently on Windows, see http://crbug.com/109738
102 #if defined(OS_WIN)
103 #define MAYBE_InvalidMessagesFile DISABLED_InvalidMessagesFile
104 #else
105 #define MAYBE_InvalidMessagesFile InvalidMessagesFile
106 #endif
107 TEST_F(UnpackerTest, MAYBE_InvalidMessagesFile) {
108 SetupUnpacker("invalid_messages_file.crx");
109 EXPECT_FALSE(unpacker_->Run());
110 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
111 ASCIIToUTF16("*_locales?en_US?messages.json: Line: 2, column: 11,"
112 " Syntax error."))) << unpacker_->error_message();
115 // Crashes intermittently on Vista, see http://crbug.com/109238
116 #if defined(OS_WIN)
117 #define MAYBE_MissingDefaultData DISABLED_MissingDefaultData
118 #else
119 #define MAYBE_MissingDefaultData MissingDefaultData
120 #endif
121 TEST_F(UnpackerTest, MAYBE_MissingDefaultData) {
122 SetupUnpacker("missing_default_data.crx");
123 EXPECT_FALSE(unpacker_->Run());
124 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
125 unpacker_->error_message());
128 // Crashes intermittently on Vista, see http://crbug.com/109238
129 #if defined(OS_WIN)
130 #define MAYBE_MissingDefaultLocaleHasLocalesFolder \
131 DISABLED_MissingDefaultLocaleHasLocalesFolder
132 #else
133 #define MAYBE_MissingDefaultLocaleHasLocalesFolder \
134 MissingDefaultLocaleHasLocalesFolder
135 #endif
136 TEST_F(UnpackerTest, MAYBE_MissingDefaultLocaleHasLocalesFolder) {
137 SetupUnpacker("missing_default_has_locales.crx");
138 EXPECT_FALSE(unpacker_->Run());
139 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultLocaleSpecified),
140 unpacker_->error_message());
143 // Crashes intermittently on Vista, see http://crbug.com/109238
144 #if defined(OS_WIN)
145 #define MAYBE_MissingMessagesFile DISABLED_MissingMessagesFile
146 #else
147 #define MAYBE_MissingMessagesFile MissingMessagesFile
148 #endif
149 TEST_F(UnpackerTest, MAYBE_MissingMessagesFile) {
150 SetupUnpacker("missing_messages_file.crx");
151 EXPECT_FALSE(unpacker_->Run());
152 EXPECT_TRUE(MatchPattern(unpacker_->error_message(),
153 ASCIIToUTF16(errors::kLocalesMessagesFileMissing) +
154 ASCIIToUTF16("*_locales?en_US?messages.json")));
157 // Crashes intermittently on Vista, see http://crbug.com/109238
158 #if defined(OS_WIN)
159 #define MAYBE_NoLocaleData DISABLED_NoLocaleData
160 #else
161 #define MAYBE_NoLocaleData NoLocaleData
162 #endif
163 TEST_F(UnpackerTest, MAYBE_NoLocaleData) {
164 SetupUnpacker("no_locale_data.crx");
165 EXPECT_FALSE(unpacker_->Run());
166 EXPECT_EQ(ASCIIToUTF16(errors::kLocalesNoDefaultMessages),
167 unpacker_->error_message());
170 // Crashes intermittently on Vista, see http://crbug.com/109238
171 #if defined(OS_WIN)
172 #define MAYBE_GoodL10n DISABLED_GoodL10n
173 #else
174 #define MAYBE_GoodL10n GoodL10n
175 #endif
176 TEST_F(UnpackerTest, MAYBE_GoodL10n) {
177 SetupUnpacker("good_l10n.crx");
178 EXPECT_TRUE(unpacker_->Run());
179 EXPECT_TRUE(unpacker_->error_message().empty());
180 ASSERT_EQ(2U, unpacker_->parsed_catalogs()->size());
183 // Crashes intermittently on Vista, see http://crbug.com/109238
184 #if defined(OS_WIN)
185 #define MAYBE_NoL10n DISABLED_NoL10n
186 #else
187 #define MAYBE_NoL10n NoL10n
188 #endif
189 TEST_F(UnpackerTest, MAYBE_NoL10n) {
190 SetupUnpacker("no_l10n.crx");
191 EXPECT_TRUE(unpacker_->Run());
192 EXPECT_TRUE(unpacker_->error_message().empty());
193 EXPECT_EQ(0U, unpacker_->parsed_catalogs()->size());
196 // Disabled on Windows because it probably crashes intermittently as described
197 // in <http://crbug.com/109238>. However, because the logic being testing here
198 // is platform-independant, this test should still provide good coverage.
199 #if defined(OS_WIN)
200 #define MAYBE_UnzipDirectoryError DISABLED_UnzipDirectoryError
201 #else
202 #define MAYBE_UnzipDirectoryError UnzipDirectoryError
203 #endif
204 TEST_F(UnpackerTest, MAYBE_UnzipDirectoryError) {
205 const char* kExpected = "Could not create directory for unzipping: ";
206 SetupUnpacker("good_package.crx");
207 FilePath path = temp_dir_.path().AppendASCII(filenames::kTempExtensionName);
208 ASSERT_TRUE(file_util::WriteFile(path, "foo", 3));
209 EXPECT_FALSE(unpacker_->Run());
210 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
211 ASCIIToUTF16(kExpected),
212 false)) << "Expected prefix: \"" << kExpected
213 << "\", actual error: \"" << unpacker_->error_message()
214 << "\"";
217 // Disabled on Windows because it probably crashes intermittently as described
218 // in <http://crbug.com/109238>. However, because the logic being testing here
219 // is platform-independant, this test should still provide good coverage.
220 #if defined(OS_WIN)
221 #define MAYBE_UnzipError DISABLED_UnzipError
222 #else
223 #define MAYBE_UnzipError UnzipError
224 #endif
225 TEST_F(UnpackerTest, MAYBE_UnzipError) {
226 const char* kExpected = "Could not unzip extension";
227 SetupUnpacker("bad_zip.crx");
228 EXPECT_FALSE(unpacker_->Run());
229 EXPECT_EQ(ASCIIToUTF16(kExpected), unpacker_->error_message());
232 // Disabled on Windows because it probably crashes intermittently as described
233 // in <http://crbug.com/109238>. However, because the logic being testing here
234 // is platform-independant, this test should still provide good coverage.
235 #if defined(OS_WIN)
236 #define MAYBE_BadPathError DISABLED_BadPathError
237 #else
238 #define MAYBE_BadPathError BadPathError
239 #endif
240 TEST_F(UnpackerTest, MAYBE_BadPathError) {
241 const char* kExpected = "Illegal path (absolute or relative with '..'): ";
242 SetupUnpacker("bad_path.crx");
243 EXPECT_FALSE(unpacker_->Run());
244 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
245 ASCIIToUTF16(kExpected),
246 false)) << "Expected prefix: \"" << kExpected
247 << "\", actual error: \"" << unpacker_->error_message()
248 << "\"";
252 // Disabled on Windows because it probably crashes intermittently as described
253 // in <http://crbug.com/109238>. However, because the logic being testing here
254 // is platform-independant, this test should still provide good coverage.
255 #if defined(OS_WIN)
256 #define MAYBE_ImageDecodingError DISABLED_ImageDecodingError
257 #else
258 #define MAYBE_ImageDecodingError ImageDecodingError
259 #endif
260 TEST_F(UnpackerTest, MAYBE_ImageDecodingError) {
261 const char* kExpected = "Could not decode image: ";
262 SetupUnpacker("bad_image.crx");
263 EXPECT_FALSE(unpacker_->Run());
264 EXPECT_TRUE(StartsWith(unpacker_->error_message(),
265 ASCIIToUTF16(kExpected),
266 false)) << "Expected prefix: \"" << kExpected
267 << "\", actual error: \"" << unpacker_->error_message()
268 << "\"";
271 } // namespace extensions