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" |
(...skipping 26 matching lines...) Expand all Loading... |
37 return extension; | 37 return extension; |
38 } | 38 } |
39 | 39 |
40 scoped_refptr<Extension> LoadExtensionManifest( | 40 scoped_refptr<Extension> LoadExtensionManifest( |
41 const std::string& manifest_value, | 41 const std::string& manifest_value, |
42 const base::FilePath& manifest_dir, | 42 const base::FilePath& manifest_dir, |
43 Manifest::Location location, | 43 Manifest::Location location, |
44 int extra_flags, | 44 int extra_flags, |
45 std::string* error) { | 45 std::string* error) { |
46 JSONStringValueSerializer serializer(manifest_value); | 46 JSONStringValueSerializer serializer(manifest_value); |
47 scoped_ptr<base::Value> result(serializer.Deserialize(NULL, error)); | 47 scoped_ptr<base::Value> result(serializer.Deserialize(nullptr, error)); |
48 if (!result.get()) | 48 if (!result.get()) |
49 return NULL; | 49 return nullptr; |
50 CHECK_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); | 50 CHECK_EQ(base::Value::TYPE_DICTIONARY, result->GetType()); |
51 return LoadExtensionManifest( | 51 return LoadExtensionManifest( |
52 static_cast<base::DictionaryValue*>(result.get()), | 52 static_cast<base::DictionaryValue*>(result.get()), |
53 manifest_dir, | 53 manifest_dir, |
54 location, | 54 location, |
55 extra_flags, | 55 extra_flags, |
56 error); | 56 error); |
57 } | 57 } |
58 | 58 |
59 } // namespace | 59 } // namespace |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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(DIR_TEST_DATA, &install_dir)); | 119 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
120 install_dir = install_dir.AppendASCII("extension_with_locales"); | 120 install_dir = install_dir.AppendASCII("extension_with_locales"); |
121 | 121 |
122 std::string error; | 122 std::string error; |
123 scoped_refptr<Extension> extension(file_util::LoadExtension( | 123 scoped_refptr<Extension> extension(file_util::LoadExtension( |
124 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 124 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
125 ASSERT_TRUE(extension.get() != NULL); | 125 ASSERT_TRUE(extension.get() != nullptr); |
126 EXPECT_EQ("The first extension that I made.", extension->description()); | 126 EXPECT_EQ("The first extension that I made.", extension->description()); |
127 } | 127 } |
128 | 128 |
129 TEST_F(FileUtilTest, LoadExtensionWithoutLocalesFolder) { | 129 TEST_F(FileUtilTest, LoadExtensionWithoutLocalesFolder) { |
130 base::FilePath install_dir; | 130 base::FilePath install_dir; |
131 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); | 131 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
132 install_dir = install_dir.AppendASCII("extension_without_locales"); | 132 install_dir = install_dir.AppendASCII("extension_without_locales"); |
133 | 133 |
134 std::string error; | 134 std::string error; |
135 scoped_refptr<Extension> extension(file_util::LoadExtension( | 135 scoped_refptr<Extension> extension(file_util::LoadExtension( |
136 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 136 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
137 ASSERT_FALSE(extension.get() == NULL); | 137 ASSERT_FALSE(extension.get() == nullptr); |
138 EXPECT_TRUE(error.empty()); | 138 EXPECT_TRUE(error.empty()); |
139 } | 139 } |
140 | 140 |
141 TEST_F(FileUtilTest, CheckIllegalFilenamesNoUnderscores) { | 141 TEST_F(FileUtilTest, CheckIllegalFilenamesNoUnderscores) { |
142 base::ScopedTempDir temp; | 142 base::ScopedTempDir temp; |
143 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 143 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
144 | 144 |
145 base::FilePath src_path = temp.path().AppendASCII("some_dir"); | 145 base::FilePath src_path = temp.path().AppendASCII("some_dir"); |
146 ASSERT_TRUE(base::CreateDirectory(src_path)); | 146 ASSERT_TRUE(base::CreateDirectory(src_path)); |
147 | 147 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
184 | 184 |
185 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnMissingManifest) { | 185 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnMissingManifest) { |
186 base::FilePath install_dir; | 186 base::FilePath install_dir; |
187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); | 187 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
188 install_dir = | 188 install_dir = |
189 install_dir.AppendASCII("file_util").AppendASCII("missing_manifest"); | 189 install_dir.AppendASCII("file_util").AppendASCII("missing_manifest"); |
190 | 190 |
191 std::string error; | 191 std::string error; |
192 scoped_refptr<Extension> extension(file_util::LoadExtension( | 192 scoped_refptr<Extension> extension(file_util::LoadExtension( |
193 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 193 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
194 ASSERT_TRUE(extension.get() == NULL); | 194 ASSERT_TRUE(extension.get() == nullptr); |
195 ASSERT_FALSE(error.empty()); | 195 ASSERT_FALSE(error.empty()); |
196 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); | 196 ASSERT_STREQ("Manifest file is missing or unreadable.", error.c_str()); |
197 } | 197 } |
198 | 198 |
199 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) { | 199 TEST_F(FileUtilTest, LoadExtensionGivesHelpfullErrorOnBadManifest) { |
200 base::FilePath install_dir; | 200 base::FilePath install_dir; |
201 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); | 201 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
202 install_dir = | 202 install_dir = |
203 install_dir.AppendASCII("file_util").AppendASCII("bad_manifest"); | 203 install_dir.AppendASCII("file_util").AppendASCII("bad_manifest"); |
204 | 204 |
205 std::string error; | 205 std::string error; |
206 scoped_refptr<Extension> extension(file_util::LoadExtension( | 206 scoped_refptr<Extension> extension(file_util::LoadExtension( |
207 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 207 install_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
208 ASSERT_TRUE(extension.get() == NULL); | 208 ASSERT_TRUE(extension.get() == nullptr); |
209 ASSERT_FALSE(error.empty()); | 209 ASSERT_FALSE(error.empty()); |
210 ASSERT_STREQ( | 210 ASSERT_STREQ( |
211 "Manifest is not valid JSON. " | 211 "Manifest is not valid JSON. " |
212 "Line: 2, column: 16, Syntax error.", | 212 "Line: 2, column: 16, Syntax error.", |
213 error.c_str()); | 213 error.c_str()); |
214 } | 214 } |
215 | 215 |
216 TEST_F(FileUtilTest, ValidateThemeUTF8) { | 216 TEST_F(FileUtilTest, ValidateThemeUTF8) { |
217 base::ScopedTempDir temp; | 217 base::ScopedTempDir temp; |
218 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 218 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 base::FilePath install_dir; | 382 base::FilePath install_dir; |
383 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); | 383 ASSERT_TRUE(PathService::Get(DIR_TEST_DATA, &install_dir)); |
384 | 384 |
385 // Try to install an extension with a zero-length icon file. | 385 // Try to install an extension with a zero-length icon file. |
386 base::FilePath ext_dir = | 386 base::FilePath ext_dir = |
387 install_dir.AppendASCII("file_util").AppendASCII("bad_icon"); | 387 install_dir.AppendASCII("file_util").AppendASCII("bad_icon"); |
388 | 388 |
389 std::string error; | 389 std::string error; |
390 scoped_refptr<Extension> extension(file_util::LoadExtension( | 390 scoped_refptr<Extension> extension(file_util::LoadExtension( |
391 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); | 391 ext_dir, Manifest::UNPACKED, Extension::NO_FLAGS, &error)); |
392 EXPECT_TRUE(extension.get() == NULL); | 392 EXPECT_TRUE(extension.get() == nullptr); |
393 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()); |
394 } | 394 } |
395 | 395 |
396 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) { | 396 TEST_F(FileUtilTest, ExtensionURLToRelativeFilePath) { |
397 #define URL_PREFIX "chrome-extension://extension-id/" | 397 #define URL_PREFIX "chrome-extension://extension-id/" |
398 struct TestCase { | 398 struct TestCase { |
399 const char* url; | 399 const char* url; |
400 const char* expected_relative_path; | 400 const char* expected_relative_path; |
401 } test_cases[] = { | 401 } test_cases[] = { |
402 { URL_PREFIX "simple.html", | 402 { URL_PREFIX "simple.html", |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
455 #ifdef FILE_PATH_USES_WIN_SEPARATORS | 455 #ifdef FILE_PATH_USES_WIN_SEPARATORS |
456 #define SEP "\\" | 456 #define SEP "\\" |
457 #else | 457 #else |
458 #define SEP "/" | 458 #define SEP "/" |
459 #endif | 459 #endif |
460 #define URL_PREFIX "chrome-extension-resource://" | 460 #define URL_PREFIX "chrome-extension-resource://" |
461 struct TestCase { | 461 struct TestCase { |
462 const char* url; | 462 const char* url; |
463 const base::FilePath::CharType* expected_path; | 463 const base::FilePath::CharType* expected_path; |
464 } test_cases[] = { | 464 } test_cases[] = { |
465 { URL_PREFIX "apiname/test.js", | 465 {URL_PREFIX "apiname/test.js", FILE_PATH_LITERAL("test.js")}, |
466 FILE_PATH_LITERAL("test.js") }, | 466 {URL_PREFIX "/apiname/test.js", FILE_PATH_LITERAL("test.js")}, |
467 { URL_PREFIX "/apiname/test.js", | 467 // Test % escape |
468 FILE_PATH_LITERAL("test.js") }, | 468 {URL_PREFIX "apiname/%74%65st.js", FILE_PATH_LITERAL("test.js")}, |
469 // Test % escape | 469 {URL_PREFIX "apiname/escape%20spaces.js", |
470 { URL_PREFIX "apiname/%74%65st.js", | 470 FILE_PATH_LITERAL("escape spaces.js")}, |
471 FILE_PATH_LITERAL("test.js") }, | 471 // Test file does not exist. |
472 { URL_PREFIX "apiname/escape%20spaces.js", | 472 {URL_PREFIX "apiname/directory/to/file.js", nullptr}, |
473 FILE_PATH_LITERAL("escape spaces.js") }, | 473 // Test apiname/../../test.js |
474 // Test file does not exist. | 474 {URL_PREFIX "apiname/../../test.js", FILE_PATH_LITERAL("test.js")}, |
475 { URL_PREFIX "apiname/directory/to/file.js", | 475 {URL_PREFIX "apiname/..%2F../test.js", nullptr}, |
476 NULL }, | 476 {URL_PREFIX "apiname/f/../../../test.js", FILE_PATH_LITERAL("test.js")}, |
477 // Test apiname/../../test.js | 477 {URL_PREFIX "apiname/f%2F..%2F..%2F../test.js", nullptr}, |
478 { URL_PREFIX "apiname/../../test.js", | 478 }; |
479 FILE_PATH_LITERAL("test.js") }, | |
480 { URL_PREFIX "apiname/..%2F../test.js", | |
481 NULL }, | |
482 { URL_PREFIX "apiname/f/../../../test.js", | |
483 FILE_PATH_LITERAL("test.js") }, | |
484 { URL_PREFIX "apiname/f%2F..%2F..%2F../test.js", | |
485 NULL }, | |
486 }; | |
487 #undef SEP | 479 #undef SEP |
488 #undef URL_PREFIX | 480 #undef URL_PREFIX |
489 | 481 |
490 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 482 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
491 GURL url(test_cases[i].url); | 483 GURL url(test_cases[i].url); |
492 base::FilePath expected_path; | 484 base::FilePath expected_path; |
493 if (test_cases[i].expected_path) | 485 if (test_cases[i].expected_path) |
494 expected_path = root_path.Append(FILE_PATH_LITERAL("apiname")).Append( | 486 expected_path = root_path.Append(FILE_PATH_LITERAL("apiname")).Append( |
495 test_cases[i].expected_path); | 487 test_cases[i].expected_path); |
496 base::FilePath actual_path = | 488 base::FilePath actual_path = |
497 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); | 489 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); |
498 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 490 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
499 " For the path " << url; | 491 " For the path " << url; |
500 } | 492 } |
501 // Remove temp files. | 493 // Remove temp files. |
502 ASSERT_TRUE(base::DeleteFile(root_path, true)); | 494 ASSERT_TRUE(base::DeleteFile(root_path, true)); |
503 } | 495 } |
504 | 496 |
505 } // namespace extensions | 497 } // namespace extensions |
OLD | NEW |