| 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/extension_api.h" | 5 #include "extensions/common/extension_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 78 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 79 std::string feature_type; | 79 std::string feature_type; |
| 80 std::string feature_name; | 80 std::string feature_name; |
| 81 ExtensionAPI::SplitDependencyName( | 81 ExtensionAPI::SplitDependencyName( |
| 82 test_data[i].input, &feature_type, &feature_name); | 82 test_data[i].input, &feature_type, &feature_name); |
| 83 EXPECT_EQ(test_data[i].expected_feature_type, feature_type) << i; | 83 EXPECT_EQ(test_data[i].expected_feature_type, feature_type) << i; |
| 84 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i; | 84 EXPECT_EQ(test_data[i].expected_feature_name, feature_name) << i; |
| 85 } | 85 } |
| 86 } | 86 } |
| 87 | 87 |
| 88 TEST(ExtensionAPITest, IsPrivileged) { | 88 TEST(ExtensionAPITest, IsAvailableInUntrustedContext) { |
| 89 scoped_ptr<ExtensionAPI> extension_api( | 89 scoped_ptr<ExtensionAPI> extension_api( |
| 90 ExtensionAPI::CreateWithDefaultConfiguration()); | 90 ExtensionAPI::CreateWithDefaultConfiguration()); |
| 91 scoped_refptr<const Extension> extension = |
| 92 ExtensionBuilder() |
| 93 .SetManifest(DictionaryBuilder() |
| 94 .Set("name", "extension") |
| 95 .Set("version", "1") |
| 96 .Set("permissions", ListBuilder().Append("storage")) |
| 97 .Set("manifest_version", 2)) |
| 98 .Build(); |
| 91 | 99 |
| 92 EXPECT_FALSE(extension_api->IsPrivileged("runtime.connect")); | 100 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("runtime.connect", |
| 93 EXPECT_FALSE(extension_api->IsPrivileged("runtime.onConnect")); | 101 extension.get())); |
| 94 EXPECT_FALSE(extension_api->IsPrivileged("runtime.lastError")); | 102 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("runtime.onConnect", |
| 103 extension.get())); |
| 104 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("runtime.lastError", |
| 105 extension.get())); |
| 95 | 106 |
| 96 // Exists, but privileged. | 107 // Exists, but privileged. |
| 97 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); | 108 EXPECT_FALSE(extension_api->IsAvailableInUntrustedContext( |
| 98 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); | 109 "extension.getViews", extension.get())); |
| 110 EXPECT_FALSE(extension_api->IsAvailableInUntrustedContext("history.search", |
| 111 extension.get())); |
| 99 | 112 |
| 100 // Whole APIs that are unprivileged. | 113 // Whole APIs that are unprivileged. |
| 101 EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails")); | 114 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("app.getDetails", |
| 102 EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled")); | 115 extension.get())); |
| 103 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); | 116 EXPECT_FALSE(extension_api->IsAvailableInUntrustedContext("app.isInstalled", |
| 104 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); | 117 extension.get())); |
| 105 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); | 118 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("storage.local", |
| 106 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); | 119 extension.get())); |
| 107 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); | 120 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext( |
| 121 "storage.local.onChanged", extension.get())); |
| 122 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("storage.local.set", |
| 123 extension.get())); |
| 124 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext( |
| 125 "storage.local.MAX_ITEMS", extension.get())); |
| 126 EXPECT_TRUE(extension_api->IsAvailableInUntrustedContext("storage.set", |
| 127 extension.get())); |
| 108 } | 128 } |
| 109 | 129 |
| 110 TEST(ExtensionAPITest, IsPrivilegedFeatures) { | 130 TEST(ExtensionAPITest, IsAvailableInUntrustedContextFeatures) { |
| 111 struct { | 131 struct { |
| 112 std::string api_full_name; | 132 std::string api_full_name; |
| 113 bool expect_is_privilged; | 133 bool expect_is_available; |
| 114 } test_data[] = { | 134 } test_data[] = {{"test1", true}, |
| 115 { "test1", false }, | 135 {"test1.foo", false}, |
| 116 { "test1.foo", true }, | 136 {"test2", false}, |
| 117 { "test2", true }, | 137 {"test2.foo", true}, |
| 118 { "test2.foo", false }, | 138 {"test2.bar", true}, |
| 119 { "test2.bar", false }, | 139 {"test2.baz", false}, |
| 120 { "test2.baz", true }, | 140 {"test2.qux", false}, |
| 121 { "test3", false }, | 141 {"test3", true}, |
| 122 { "test3.foo", true }, | 142 {"test3.foo", false}, |
| 123 { "test4", false } | 143 {"test4", true}, |
| 124 }; | 144 {"test5", true}}; |
| 125 | 145 |
| 126 base::FilePath api_features_path; | 146 base::FilePath api_features_path; |
| 127 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); | 147 PathService::Get(chrome::DIR_TEST_DATA, &api_features_path); |
| 128 api_features_path = api_features_path.AppendASCII("extensions") | 148 api_features_path = api_features_path.AppendASCII("extensions") |
| 129 .AppendASCII("extension_api_unittest") | 149 .AppendASCII("extension_api_unittest") |
| 130 .AppendASCII("privileged_api_features.json"); | 150 .AppendASCII("privileged_api_features.json"); |
| 131 | 151 |
| 132 std::string api_features_str; | 152 std::string api_features_str; |
| 133 ASSERT_TRUE(base::ReadFileToString( | 153 ASSERT_TRUE(base::ReadFileToString( |
| 134 api_features_path, &api_features_str)) << "privileged_api_features.json"; | 154 api_features_path, &api_features_str)) << "privileged_api_features.json"; |
| 135 | 155 |
| 136 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( | 156 scoped_ptr<base::DictionaryValue> value(static_cast<base::DictionaryValue*>( |
| 137 base::JSONReader::Read(api_features_str))); | 157 base::JSONReader::Read(api_features_str))); |
| 138 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature); | 158 BaseFeatureProvider api_feature_provider(*value, CreateAPIFeature); |
| 139 | 159 |
| 160 scoped_refptr<Extension> extension = |
| 161 BuildExtension(ExtensionBuilder().Pass()).Build(); |
| 162 |
| 140 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 163 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 141 ExtensionAPI api; | 164 ExtensionAPI api; |
| 142 api.RegisterDependencyProvider("api", &api_feature_provider); | 165 api.RegisterDependencyProvider("api", &api_feature_provider); |
| 143 EXPECT_EQ(test_data[i].expect_is_privilged, | 166 EXPECT_EQ(test_data[i].expect_is_available, |
| 144 api.IsPrivileged(test_data[i].api_full_name)) << i; | 167 api.IsAvailableInUntrustedContext(test_data[i].api_full_name, |
| 168 extension.get())) |
| 169 << i; |
| 145 } | 170 } |
| 146 } | 171 } |
| 147 | 172 |
| 148 TEST(ExtensionAPITest, APIFeatures) { | 173 TEST(ExtensionAPITest, APIFeatures) { |
| 149 struct { | 174 struct { |
| 150 std::string api_full_name; | 175 std::string api_full_name; |
| 151 bool expect_is_available; | 176 bool expect_is_available; |
| 152 Feature::Context context; | 177 Feature::Context context; |
| 153 GURL url; | 178 GURL url; |
| 154 } test_data[] = { | 179 } test_data[] = { |
| (...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 701 { bookmarks_create } | 726 { bookmarks_create } |
| 702 }; | 727 }; |
| 703 | 728 |
| 704 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { | 729 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_data); ++i) { |
| 705 SimpleFeature* feature = test_data[i].feature; | 730 SimpleFeature* feature = test_data[i].feature; |
| 706 ASSERT_TRUE(feature) << i; | 731 ASSERT_TRUE(feature) << i; |
| 707 | 732 |
| 708 EXPECT_TRUE(feature->whitelist()->empty()); | 733 EXPECT_TRUE(feature->whitelist()->empty()); |
| 709 EXPECT_TRUE(feature->extension_types()->empty()); | 734 EXPECT_TRUE(feature->extension_types()->empty()); |
| 710 | 735 |
| 711 EXPECT_EQ(1u, feature->GetContexts()->size()); | |
| 712 EXPECT_TRUE(feature->GetContexts()->count( | |
| 713 Feature::BLESSED_EXTENSION_CONTEXT)); | |
| 714 | |
| 715 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location()); | 736 EXPECT_EQ(SimpleFeature::UNSPECIFIED_LOCATION, feature->location()); |
| 716 EXPECT_TRUE(feature->platforms()->empty()); | 737 EXPECT_TRUE(feature->platforms()->empty()); |
| 717 EXPECT_EQ(0, feature->min_manifest_version()); | 738 EXPECT_EQ(0, feature->min_manifest_version()); |
| 718 EXPECT_EQ(0, feature->max_manifest_version()); | 739 EXPECT_EQ(0, feature->max_manifest_version()); |
| 719 } | 740 } |
| 720 } | 741 } |
| 721 | 742 |
| 722 TEST(ExtensionAPITest, FeaturesRequireContexts) { | 743 TEST(ExtensionAPITest, FeaturesRequireContexts) { |
| 723 // TODO(cduvall): Make this check API featues. | 744 // TODO(cduvall): Make this check API featues. |
| 724 scoped_ptr<base::DictionaryValue> api_features1(new base::DictionaryValue()); | 745 scoped_ptr<base::DictionaryValue> api_features1(new base::DictionaryValue()); |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 896 extension.get(), | 917 extension.get(), |
| 897 Feature::BLESSED_EXTENSION_CONTEXT, | 918 Feature::BLESSED_EXTENSION_CONTEXT, |
| 898 GURL()).is_available()); | 919 GURL()).is_available()); |
| 899 EXPECT_FALSE(extension_api->IsAvailable("pageAction", | 920 EXPECT_FALSE(extension_api->IsAvailable("pageAction", |
| 900 extension.get(), | 921 extension.get(), |
| 901 Feature::BLESSED_EXTENSION_CONTEXT, | 922 Feature::BLESSED_EXTENSION_CONTEXT, |
| 902 GURL()).is_available()); | 923 GURL()).is_available()); |
| 903 } | 924 } |
| 904 | 925 |
| 905 } // namespace extensions | 926 } // namespace extensions |
| OLD | NEW |