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
{
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
,
52 Extension::NO_FLAGS
));
56 ScopedTempDir temp_dir_
;
57 scoped_ptr
<Unpacker
> unpacker_
;
60 // Crashes intermittently on Windows, see http://crbug.com/109238
62 #define MAYBE_EmptyDefaultLocale DISABLED_EmptyDefaultLocale
64 #define MAYBE_EmptyDefaultLocale EmptyDefaultLocale
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
75 #define MAYBE_HasDefaultLocaleMissingLocalesFolder \
76 DISABLED_HasDefaultLocaleMissingLocalesFolder
78 #define MAYBE_HasDefaultLocaleMissingLocalesFolder \
79 HasDefaultLocaleMissingLocalesFolder
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
90 #define MAYBE_InvalidDefaultLocale DISABLED_InvalidDefaultLocale
92 #define MAYBE_InvalidDefaultLocale InvalidDefaultLocale
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
103 #define MAYBE_InvalidMessagesFile DISABLED_InvalidMessagesFile
105 #define MAYBE_InvalidMessagesFile InvalidMessagesFile
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
117 #define MAYBE_MissingDefaultData DISABLED_MissingDefaultData
119 #define MAYBE_MissingDefaultData MissingDefaultData
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
130 #define MAYBE_MissingDefaultLocaleHasLocalesFolder \
131 DISABLED_MissingDefaultLocaleHasLocalesFolder
133 #define MAYBE_MissingDefaultLocaleHasLocalesFolder \
134 MissingDefaultLocaleHasLocalesFolder
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
145 #define MAYBE_MissingMessagesFile DISABLED_MissingMessagesFile
147 #define MAYBE_MissingMessagesFile MissingMessagesFile
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
159 #define MAYBE_NoLocaleData DISABLED_NoLocaleData
161 #define MAYBE_NoLocaleData NoLocaleData
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
172 #define MAYBE_GoodL10n DISABLED_GoodL10n
174 #define MAYBE_GoodL10n GoodL10n
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
185 #define MAYBE_NoL10n DISABLED_NoL10n
187 #define MAYBE_NoL10n NoL10n
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.
200 #define MAYBE_UnzipDirectoryError DISABLED_UnzipDirectoryError
202 #define MAYBE_UnzipDirectoryError UnzipDirectoryError
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()
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.
221 #define MAYBE_UnzipError DISABLED_UnzipError
223 #define MAYBE_UnzipError UnzipError
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.
236 #define MAYBE_BadPathError DISABLED_BadPathError
238 #define MAYBE_BadPathError BadPathError
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()
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.
256 #define MAYBE_ImageDecodingError DISABLED_ImageDecodingError
258 #define MAYBE_ImageDecodingError ImageDecodingError
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()
271 } // namespace extensions