| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/manifest.h" | 5 #include "extensions/common/manifest.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "extensions/common/error_utils.h" | 15 #include "extensions/common/error_utils.h" |
| 15 #include "extensions/common/features/feature.h" | 16 #include "extensions/common/features/feature.h" |
| 16 #include "extensions/common/features/simple_feature.h" | 17 #include "extensions/common/features/simple_feature.h" |
| 17 #include "extensions/common/install_warning.h" | 18 #include "extensions/common/install_warning.h" |
| 18 #include "extensions/common/manifest_constants.h" | 19 #include "extensions/common/manifest_constants.h" |
| 19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 46 // instead be deleted. | 47 // instead be deleted. |
| 47 void MutateManifest(scoped_ptr<Manifest>* manifest, | 48 void MutateManifest(scoped_ptr<Manifest>* manifest, |
| 48 const std::string& key, | 49 const std::string& key, |
| 49 base::Value* value) { | 50 base::Value* value) { |
| 50 scoped_ptr<base::DictionaryValue> manifest_value( | 51 scoped_ptr<base::DictionaryValue> manifest_value( |
| 51 manifest->get()->value()->DeepCopy()); | 52 manifest->get()->value()->DeepCopy()); |
| 52 if (value) | 53 if (value) |
| 53 manifest_value->Set(key, value); | 54 manifest_value->Set(key, value); |
| 54 else | 55 else |
| 55 manifest_value->Remove(key, NULL); | 56 manifest_value->Remove(key, NULL); |
| 56 manifest->reset(new Manifest(Manifest::INTERNAL, manifest_value.Pass())); | 57 manifest->reset( |
| 58 new Manifest(Manifest::INTERNAL, std::move(manifest_value))); |
| 57 } | 59 } |
| 58 | 60 |
| 59 std::string default_value_; | 61 std::string default_value_; |
| 60 }; | 62 }; |
| 61 | 63 |
| 62 // Verifies that extensions can access the correct keys. | 64 // Verifies that extensions can access the correct keys. |
| 63 TEST_F(ManifestUnitTest, Extension) { | 65 TEST_F(ManifestUnitTest, Extension) { |
| 64 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue()); | 66 scoped_ptr<base::DictionaryValue> manifest_value(new base::DictionaryValue()); |
| 65 manifest_value->SetString(keys::kName, "extension"); | 67 manifest_value->SetString(keys::kName, "extension"); |
| 66 manifest_value->SetString(keys::kVersion, "1"); | 68 manifest_value->SetString(keys::kVersion, "1"); |
| 67 // Only supported in manifest_version=1. | 69 // Only supported in manifest_version=1. |
| 68 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html"); | 70 manifest_value->SetString(keys::kBackgroundPageLegacy, "bg.html"); |
| 69 manifest_value->SetString("unknown_key", "foo"); | 71 manifest_value->SetString("unknown_key", "foo"); |
| 70 | 72 |
| 71 scoped_ptr<Manifest> manifest( | 73 scoped_ptr<Manifest> manifest( |
| 72 new Manifest(Manifest::INTERNAL, manifest_value.Pass())); | 74 new Manifest(Manifest::INTERNAL, std::move(manifest_value))); |
| 73 std::string error; | 75 std::string error; |
| 74 std::vector<InstallWarning> warnings; | 76 std::vector<InstallWarning> warnings; |
| 75 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); | 77 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); |
| 76 EXPECT_TRUE(error.empty()); | 78 EXPECT_TRUE(error.empty()); |
| 77 ASSERT_EQ(1u, warnings.size()); | 79 ASSERT_EQ(1u, warnings.size()); |
| 78 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); | 80 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); |
| 79 | 81 |
| 80 // The known key 'background_page' should be accessible. | 82 // The known key 'background_page' should be accessible. |
| 81 std::string value; | 83 std::string value; |
| 82 EXPECT_TRUE(manifest->GetString(keys::kBackgroundPageLegacy, &value)); | 84 EXPECT_TRUE(manifest->GetString(keys::kBackgroundPageLegacy, &value)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 EXPECT_FALSE(manifest->Equals(manifest2.get())); | 119 EXPECT_FALSE(manifest->Equals(manifest2.get())); |
| 118 } | 120 } |
| 119 | 121 |
| 120 // Verifies that key restriction based on type works. | 122 // Verifies that key restriction based on type works. |
| 121 TEST_F(ManifestUnitTest, ExtensionTypes) { | 123 TEST_F(ManifestUnitTest, ExtensionTypes) { |
| 122 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 124 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 123 value->SetString(keys::kName, "extension"); | 125 value->SetString(keys::kName, "extension"); |
| 124 value->SetString(keys::kVersion, "1"); | 126 value->SetString(keys::kVersion, "1"); |
| 125 | 127 |
| 126 scoped_ptr<Manifest> manifest( | 128 scoped_ptr<Manifest> manifest( |
| 127 new Manifest(Manifest::INTERNAL, value.Pass())); | 129 new Manifest(Manifest::INTERNAL, std::move(value))); |
| 128 std::string error; | 130 std::string error; |
| 129 std::vector<InstallWarning> warnings; | 131 std::vector<InstallWarning> warnings; |
| 130 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); | 132 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); |
| 131 EXPECT_TRUE(error.empty()); | 133 EXPECT_TRUE(error.empty()); |
| 132 EXPECT_TRUE(warnings.empty()); | 134 EXPECT_TRUE(warnings.empty()); |
| 133 | 135 |
| 134 // By default, the type is Extension. | 136 // By default, the type is Extension. |
| 135 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); | 137 AssertType(manifest.get(), Manifest::TYPE_EXTENSION); |
| 136 | 138 |
| 137 // Theme. | 139 // Theme. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 &manifest, keys::kLaunchWebURL, NULL); | 175 &manifest, keys::kLaunchWebURL, NULL); |
| 174 }; | 176 }; |
| 175 | 177 |
| 176 // Verifies that the getters filter restricted keys. | 178 // Verifies that the getters filter restricted keys. |
| 177 TEST_F(ManifestUnitTest, RestrictedKeys) { | 179 TEST_F(ManifestUnitTest, RestrictedKeys) { |
| 178 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 180 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 179 value->SetString(keys::kName, "extension"); | 181 value->SetString(keys::kName, "extension"); |
| 180 value->SetString(keys::kVersion, "1"); | 182 value->SetString(keys::kVersion, "1"); |
| 181 | 183 |
| 182 scoped_ptr<Manifest> manifest( | 184 scoped_ptr<Manifest> manifest( |
| 183 new Manifest(Manifest::INTERNAL, value.Pass())); | 185 new Manifest(Manifest::INTERNAL, std::move(value))); |
| 184 std::string error; | 186 std::string error; |
| 185 std::vector<InstallWarning> warnings; | 187 std::vector<InstallWarning> warnings; |
| 186 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); | 188 EXPECT_TRUE(manifest->ValidateManifest(&error, &warnings)); |
| 187 EXPECT_TRUE(error.empty()); | 189 EXPECT_TRUE(error.empty()); |
| 188 EXPECT_TRUE(warnings.empty()); | 190 EXPECT_TRUE(warnings.empty()); |
| 189 | 191 |
| 190 // "Commands" requires manifest version 2. | 192 // "Commands" requires manifest version 2. |
| 191 const base::Value* output = NULL; | 193 const base::Value* output = NULL; |
| 192 MutateManifest( | 194 MutateManifest( |
| 193 &manifest, keys::kCommands, new base::DictionaryValue()); | 195 &manifest, keys::kCommands, new base::DictionaryValue()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 213 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output)); | 215 EXPECT_FALSE(manifest->Get(keys::kPageAction, &output)); |
| 214 MutateManifest( | 216 MutateManifest( |
| 215 &manifest, keys::kPlatformAppBackground, NULL); | 217 &manifest, keys::kPlatformAppBackground, NULL); |
| 216 | 218 |
| 217 // Platform apps also can't have a "Commands" key. | 219 // Platform apps also can't have a "Commands" key. |
| 218 EXPECT_FALSE(manifest->HasKey(keys::kCommands)); | 220 EXPECT_FALSE(manifest->HasKey(keys::kCommands)); |
| 219 EXPECT_FALSE(manifest->Get(keys::kCommands, &output)); | 221 EXPECT_FALSE(manifest->Get(keys::kCommands, &output)); |
| 220 }; | 222 }; |
| 221 | 223 |
| 222 } // namespace extensions | 224 } // namespace extensions |
| OLD | NEW |