| 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 "extensions/common/constants.h" | 14 #include "extensions/common/constants.h" |
| 15 #include "extensions/common/extension.h" | 15 #include "extensions/common/extension.h" |
| 16 #include "extensions/common/extension_paths.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 |
| 27 namespace { | 27 namespace { |
| 28 | 28 |
| 29 scoped_refptr<Extension> LoadExtensionManifest( | 29 scoped_refptr<Extension> LoadExtensionManifest( |
| 30 base::DictionaryValue* manifest, | 30 const base::DictionaryValue& manifest, |
| 31 const base::FilePath& manifest_dir, | 31 const base::FilePath& manifest_dir, |
| 32 Manifest::Location location, | 32 Manifest::Location location, |
| 33 int extra_flags, | 33 int extra_flags, |
| 34 std::string* error) { | 34 std::string* error) { |
| 35 scoped_refptr<Extension> extension = | 35 scoped_refptr<Extension> extension = |
| 36 Extension::Create(manifest_dir, location, *manifest, extra_flags, error); | 36 Extension::Create(manifest_dir, location, manifest, extra_flags, error); |
| 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 JSONStringValueDeserializer deserializer(manifest_value); | 46 JSONStringValueDeserializer deserializer(manifest_value); |
| 47 scoped_ptr<base::Value> result(deserializer.Deserialize(NULL, error)); | 47 scoped_ptr<base::Value> result(deserializer.Deserialize(NULL, error)); |
| 48 if (!result.get()) | 48 if (!result.get()) |
| 49 return NULL; | 49 return NULL; |
| 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 *base::DictionaryValue::From(result.Pass()).get(), manifest_dir, location, |
| 53 manifest_dir, | 53 extra_flags, error); |
| 54 location, | |
| 55 extra_flags, | |
| 56 error); | |
| 57 } | 54 } |
| 58 | 55 |
| 59 } // namespace | 56 } // namespace |
| 60 | 57 |
| 61 typedef testing::Test FileUtilTest; | 58 typedef testing::Test FileUtilTest; |
| 62 | 59 |
| 63 TEST_F(FileUtilTest, InstallUninstallGarbageCollect) { | 60 TEST_F(FileUtilTest, InstallUninstallGarbageCollect) { |
| 64 base::ScopedTempDir temp; | 61 base::ScopedTempDir temp; |
| 65 ASSERT_TRUE(temp.CreateUniqueTempDir()); | 62 ASSERT_TRUE(temp.CreateUniqueTempDir()); |
| 66 | 63 |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 value->SetString("version", "1"); | 284 value->SetString("version", "1"); |
| 288 value->SetInteger("manifest_version", 1); | 285 value->SetInteger("manifest_version", 1); |
| 289 | 286 |
| 290 base::ListValue* scripts = new base::ListValue(); | 287 base::ListValue* scripts = new base::ListValue(); |
| 291 scripts->Append(new base::StringValue("foo.js")); | 288 scripts->Append(new base::StringValue("foo.js")); |
| 292 value->Set("background.scripts", scripts); | 289 value->Set("background.scripts", scripts); |
| 293 | 290 |
| 294 std::string error; | 291 std::string error; |
| 295 std::vector<extensions::InstallWarning> warnings; | 292 std::vector<extensions::InstallWarning> warnings; |
| 296 scoped_refptr<Extension> extension = LoadExtensionManifest( | 293 scoped_refptr<Extension> extension = LoadExtensionManifest( |
| 297 value.get(), temp.path(), Manifest::UNPACKED, 0, &error); | 294 *value.get(), temp.path(), Manifest::UNPACKED, 0, &error); |
| 298 ASSERT_TRUE(extension.get()) << error; | 295 ASSERT_TRUE(extension.get()) << error; |
| 299 | 296 |
| 300 EXPECT_FALSE( | 297 EXPECT_FALSE( |
| 301 file_util::ValidateExtension(extension.get(), &error, &warnings)); | 298 file_util::ValidateExtension(extension.get(), &error, &warnings)); |
| 302 EXPECT_EQ( | 299 EXPECT_EQ( |
| 303 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, | 300 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, |
| 304 base::ASCIIToUTF16("foo.js")), | 301 base::ASCIIToUTF16("foo.js")), |
| 305 error); | 302 error); |
| 306 EXPECT_EQ(0U, warnings.size()); | 303 EXPECT_EQ(0U, warnings.size()); |
| 307 | 304 |
| 308 scripts->Clear(); | 305 scripts->Clear(); |
| 309 scripts->Append(new base::StringValue("http://google.com/foo.js")); | 306 scripts->Append(new base::StringValue("http://google.com/foo.js")); |
| 310 | 307 |
| 311 extension = LoadExtensionManifest( | 308 extension = LoadExtensionManifest(*value.get(), temp.path(), |
| 312 value.get(), temp.path(), Manifest::UNPACKED, 0, &error); | 309 Manifest::UNPACKED, 0, &error); |
| 313 ASSERT_TRUE(extension.get()) << error; | 310 ASSERT_TRUE(extension.get()) << error; |
| 314 | 311 |
| 315 warnings.clear(); | 312 warnings.clear(); |
| 316 EXPECT_FALSE( | 313 EXPECT_FALSE( |
| 317 file_util::ValidateExtension(extension.get(), &error, &warnings)); | 314 file_util::ValidateExtension(extension.get(), &error, &warnings)); |
| 318 EXPECT_EQ( | 315 EXPECT_EQ( |
| 319 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, | 316 l10n_util::GetStringFUTF8(IDS_EXTENSION_LOAD_BACKGROUND_SCRIPT_FAILED, |
| 320 base::ASCIIToUTF16("http://google.com/foo.js")), | 317 base::ASCIIToUTF16("http://google.com/foo.js")), |
| 321 error); | 318 error); |
| 322 EXPECT_EQ(0U, warnings.size()); | 319 EXPECT_EQ(0U, warnings.size()); |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 535 base::FilePath actual_path = | 532 base::FilePath actual_path = |
| 536 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); | 533 extensions::file_util::ExtensionResourceURLToFilePath(url, root_path); |
| 537 EXPECT_EQ(expected_path.value(), actual_path.value()) << | 534 EXPECT_EQ(expected_path.value(), actual_path.value()) << |
| 538 " For the path " << url; | 535 " For the path " << url; |
| 539 } | 536 } |
| 540 // Remove temp files. | 537 // Remove temp files. |
| 541 ASSERT_TRUE(base::DeleteFile(root_path, true)); | 538 ASSERT_TRUE(base::DeleteFile(root_path, true)); |
| 542 } | 539 } |
| 543 | 540 |
| 544 } // namespace extensions | 541 } // namespace extensions |
| OLD | NEW |