OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/common/file_util.h" | 5 #include "extensions/common/file_util.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/json/json_string_value_serializer.h" | 10 #include "base/json/json_string_value_serializer.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/common/chrome_paths.h" | |
15 #include "extensions/common/constants.h" | 14 #include "extensions/common/constants.h" |
16 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 16 #include "extensions/common/extension_paths.h" |
17 #include "extensions/common/manifest.h" | 17 #include "extensions/common/manifest.h" |
18 #include "extensions/common/manifest_constants.h" | 18 #include "extensions/common/manifest_constants.h" |
19 #include "grit/extensions_strings.h" | 19 #include "grit/extensions_strings.h" |
20 #include "testing/gmock/include/gmock/gmock.h" | 20 #include "testing/gmock/include/gmock/gmock.h" |
21 #include "testing/gtest/include/gtest/gtest.h" | 21 #include "testing/gtest/include/gtest/gtest.h" |
22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
24 | 24 |
25 namespace extensions { | 25 namespace extensions { |
26 | 26 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 // Uninstall. Should remove entire extension subtree. | 109 // Uninstall. Should remove entire extension subtree. |
110 file_util::UninstallExtension(all_extensions, extension_id); | 110 file_util::UninstallExtension(all_extensions, extension_id); |
111 ASSERT_FALSE(base::DirectoryExists(version_1.DirName())); | 111 ASSERT_FALSE(base::DirectoryExists(version_1.DirName())); |
112 ASSERT_FALSE(base::DirectoryExists(version_2.DirName())); | 112 ASSERT_FALSE(base::DirectoryExists(version_2.DirName())); |
113 ASSERT_FALSE(base::DirectoryExists(version_3.DirName())); | 113 ASSERT_FALSE(base::DirectoryExists(version_3.DirName())); |
114 ASSERT_TRUE(base::DirectoryExists(all_extensions)); | 114 ASSERT_TRUE(base::DirectoryExists(all_extensions)); |
115 } | 115 } |
116 | 116 |
117 TEST_F(FileUtilTest, LoadExtensionWithValidLocales) { | 117 TEST_F(FileUtilTest, LoadExtensionWithValidLocales) { |
118 base::FilePath install_dir; | 118 base::FilePath install_dir; |
119 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 119 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
120 install_dir = install_dir.AppendASCII("extensions") | 120 install_dir = install_dir.AppendASCII("extension_with_locales"); |
121 .AppendASCII("good") | |
122 .AppendASCII("Extensions") | |
123 .AppendASCII("behllobkkfkfnphdnhnkndlbkcpglgmj") | |
124 .AppendASCII("1.0.0.0"); | |
125 | 121 |
126 std::string error; | 122 std::string error; |
127 scoped_refptr<Extension> extension(file_util::LoadExtension( | 123 scoped_refptr<Extension> extension(file_util::LoadExtension( |
128 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 124 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
129 ASSERT_TRUE(extension.get() != NULL); | 125 ASSERT_TRUE(extension.get() != NULL); |
130 EXPECT_EQ("The first extension that I made.", extension->description()); | 126 EXPECT_EQ("The first extension that I made.", extension->description()); |
131 } | 127 } |
132 | 128 |
133 TEST_F(FileUtilTest, LoadExtensionWithoutLocalesFolder) { | 129 TEST_F(FileUtilTest, LoadExtensionWithoutLocalesFolder) { |
134 base::FilePath install_dir; | 130 base::FilePath install_dir; |
135 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 131 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
136 install_dir = install_dir.AppendASCII("extensions") | 132 install_dir = install_dir.AppendASCII("extension_without_locales"); |
137 .AppendASCII("good") | |
138 .AppendASCII("Extensions") | |
139 .AppendASCII("bjafgdebaacbbbecmhlhpofkepfkgcpa") | |
140 .AppendASCII("1.0"); | |
141 | 133 |
142 std::string error; | 134 std::string error; |
143 scoped_refptr<Extension> extension(file_util::LoadExtension( | 135 scoped_refptr<Extension> extension(file_util::LoadExtension( |
144 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 136 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
145 ASSERT_FALSE(extension.get() == NULL); | 137 ASSERT_FALSE(extension.get() == NULL); |
146 EXPECT_TRUE(error.empty()); | 138 EXPECT_TRUE(error.empty()); |
147 } | 139 } |
148 | 140 |
149 TEST_F(FileUtilTest, CheckIllegalFilenamesNoUnderscores) { | 141 TEST_F(FileUtilTest, CheckIllegalFilenamesNoUnderscores) { |
150 base::ScopedTempDir temp; | 142 base::ScopedTempDir temp; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 177 |
186 src_path = temp.path().AppendASCII("_some_dir"); | 178 src_path = temp.path().AppendASCII("_some_dir"); |
187 ASSERT_TRUE(base::CreateDirectory(src_path)); | 179 ASSERT_TRUE(base::CreateDirectory(src_path)); |
188 | 180 |
189 std::string error; | 181 std::string error; |
190 EXPECT_FALSE(file_util::CheckForIllegalFilenames(temp.path(), &error)); | 182 EXPECT_FALSE(file_util::CheckForIllegalFilenames(temp.path(), &error)); |
191 } | 183 } |
192 | 184 |
193 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnMissingManifest) { | 185 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnMissingManifest) { |
194 base::FilePath install_dir; | 186 base::FilePath install_dir; |
195 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
196 install_dir = install_dir.AppendASCII("extensions") | 188 install_dir = |
197 .AppendASCII("bad") | 189 install_dir.AppendASCII("file_util").AppendASCII("missing_manifest"); |
198 .AppendASCII("Extensions") | |
199 .AppendASCII("dddddddddddddddddddddddddddddddd") | |
200 .AppendASCII("1.0"); | |
201 | 190 |
202 std::string error; | 191 std::string error; |
203 scoped_refptr<Extension> extension(file_util::LoadExtension( | 192 scoped_refptr<Extension> extension(file_util::LoadExtension( |
204 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 193 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
205 ASSERT_TRUE(extension.get() == NULL); | 194 ASSERT_TRUE(extension.get() == NULL); |
206 ASSERT_FALSE(error.empty()); | 195 ASSERT_FALSE(error.empty()); |
207 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); | 196 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); |
208 } | 197 } |
209 | 198 |
210 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) { | 199 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) { |
211 base::FilePath install_dir; | 200 base::FilePath install_dir; |
212 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 201 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
213 install_dir = install_dir.AppendASCII("extensions") | 202 install_dir = |
214 .AppendASCII("bad") | 203 install_dir.AppendASCII("file_util").AppendASCII("bad_manifest"); |
215 .AppendASCII("Extensions") | |
216 .AppendASCII("eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee") | |
217 .AppendASCII("1.0"); | |
218 | 204 |
219 std::string error; | 205 std::string error; |
220 scoped_refptr<Extension> extension(file_util::LoadExtension( | 206 scoped_refptr<Extension> extension(file_util::LoadExtension( |
221 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 207 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
222 ASSERT_TRUE(extension.get() == NULL); | 208 ASSERT_TRUE(extension.get() == NULL); |
223 ASSERT_FALSE(error.empty()); | 209 ASSERT_FALSE(error.empty()); |
224 ASSERT_STREQ( | 210 ASSERT_STREQ( |
225 "Manifest is not valid JSON. " | 211 "Manifest is not valid JSON. " |
226 "Line: 2, column: 16, Syntax error.", | 212 "Line: 2, column: 16, Syntax error.", |
227 error.c_str()); | 213 error.c_str()); |
228 } | 214 } |
229 | 215 |
230 TEST_F(FileUtilTest, FailLoadingNonUTF8Scripts) { | |
231 base::FilePath install_dir; | |
232 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | |
233 install_dir = install_dir.AppendASCII("extensions") | |
234 .AppendASCII("bad") | |
235 .AppendASCII("bad_encoding"); | |
236 | |
237 std::string error; | |
238 scoped_refptr<Extension> extension(file_util::LoadExtension( | |
239 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | |
240 ASSERT_TRUE(extension.get() == NULL); | |
241 ASSERT_STREQ( | |
242 "Could not load file 'bad_encoding.js' for content script. " | |
243 "It isn't UTF-8 encoded.", | |
244 error.c_str()); | |
245 } | |
246 | |
247 TEST_F(FileUtilTest, ValidateThemeUTF8) { | 216 TEST_F(FileUtilTest, ValidateThemeUTF8) { |
248 base::ScopedTempDir temp; | 217 base::ScopedTempDir temp; |
249 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 218 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
250 | 219 |
251 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. | 220 // "aeo" with accents. Use http://0xcc.net/jsescape/ to decode them. |
252 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; | 221 std::string non_ascii_file = "\xC3\xA0\xC3\xA8\xC3\xB2.png"; |
253 base::FilePath non_ascii_path = | 222 base::FilePath non_ascii_path = |
254 temp.path().Append(base::FilePath::FromUTF8Unsafe(non_ascii_file)); | 223 temp.path().Append(base::FilePath::FromUTF8Unsafe(non_ascii_file)); |
255 base::WriteFile(non_ascii_path, "", 0); | 224 base::WriteFile(non_ascii_path, "", 0); |
256 | 225 |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
402 "the_id", | 371 "the_id", |
403 Manifest::EXTERNAL_PREF, | 372 Manifest::EXTERNAL_PREF, |
404 Extension::ERROR_ON_PRIVATE_KEY, | 373 Extension::ERROR_ON_PRIVATE_KEY, |
405 &error); | 374 &error); |
406 EXPECT_FALSE(extension.get()); | 375 EXPECT_FALSE(extension.get()); |
407 EXPECT_THAT(error, | 376 EXPECT_THAT(error, |
408 testing::ContainsRegex( | 377 testing::ContainsRegex( |
409 "extension includes the key file.*ext_root.a_key.pem")); | 378 "extension includes the key file.*ext_root.a_key.pem")); |
410 } | 379 } |
411 | 380 |
412 TEST_F(FileUtilTest, CheckZeroLengthImageFile) { | 381 TEST_F(FileUtilTest, CheckZeroLengthIconFile) { |
413 base::FilePath install_dir; | 382 base::FilePath install_dir; |
414 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &install_dir)); | 383 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
415 | 384 |
416 // Try to install an extension with a zero-length icon file. | 385 // Try to install an extension with a zero-length icon file. |
417 base::FilePath ext_dir = install_dir.AppendASCII("extensions") | 386 base::FilePath ext_dir = |
418 .AppendASCII("bad") | 387 install_dir.AppendASCII("file_util").AppendASCII("bad_icon"); |
419 .AppendASCII("Extensions") | |
420 .AppendASCII("ffffffffffffffffffffffffffffffff"); | |
421 | 388 |
422 std::string error; | 389 std::string error; |
423 scoped_refptr<Extension> extension(file_util::LoadExtension( | 390 scoped_refptr<Extension> extension(file_util::LoadExtension( |
424 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 391 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
425 EXPECT_TRUE(extension.get() == NULL); | 392 EXPECT_TRUE(extension.get() == NULL); |
426 EXPECT_STREQ("Could not load extension icon 'icon.png'.", error.c_str()); | 393 EXPECT_STREQ("Could not load extension icon 'icon.png'.", error.c_str()); |
427 | |
428 // Try to install an extension with a zero-length browser action icon file. | |
429 ext_dir = install_dir.AppendASCII("extensions") | |
430 .AppendASCII("bad") | |
431 .AppendASCII("Extensions") | |
432 .AppendASCII("gggggggggggggggggggggggggggggggg"); | |
433 | |
434 scoped_refptr<Extension> extension2(file_util::LoadExtension( | |
435 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | |
436 EXPECT_TRUE(extension2.get() == NULL); | |
437 EXPECT_STREQ("Could not load icon 'icon.png' for browser action.", | |
438 error.c_str()); | |
439 | |
440 // Try to install an extension with a zero-length page action icon file. | |
441 ext_dir = install_dir.AppendASCII("extensions") | |
442 .AppendASCII("bad") | |
443 .AppendASCII("Extensions") | |
444 .AppendASCII("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"); | |
445 | |
446 scoped_refptr<Extension> extension3(file_util::LoadExtension( | |
447 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | |
448 EXPECT_TRUE(extension3.get() == NULL); | |
449 EXPECT_STREQ("Could not load icon 'icon.png' for page action.", | |
450 error.c_str()); | |
451 } | 394 } |
452 | 395 |
453 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) { | 396 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) { |
454 #define URL_PREFIX "chrome-extension://extension-id/" | 397 #define URL_PREFIX "chrome-extension://extension-id/" |
455 struct TestCase { | 398 struct TestCase { |
456 const char* url; | 399 const char* url; |
457 const char* expected_relative_path; | 400 const char* expected_relative_path; |
458 } test_cases[] = { | 401 } test_cases[] = { |
459 { URL_PREFIX "simple.html", | 402 { URL_PREFIX "simple.html", |
460 "simple.html" }, | 403 "simple.html" }, |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 base::FilePath actual_path = | 496 base::FilePath actual_path = |
554 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); | 497 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); |
555 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 498 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
556 " For the path " << url; | 499 " For the path " << url; |
557 } | 500 } |
558 // Remove temp files. | 501 // Remove temp files. |
559 ASSERT_TRUE(base::DeleteFile(root_path, true)); | 502 ASSERT_TRUE(base::DeleteFile(root_path, true)); |
560 } | 503 } |
561 | 504 |
562 } // namespace extensions | 505 } // namespace extensions |
OLD | NEW |