| 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 "chrome/common/extensions/api/extension_api.h" | 5 #include "chrome/common/extensions/api/extension_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/common/extensions/extension.h" | 13 #include "chrome/common/extensions/extension.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 16 using extensions::ExtensionAPI; | 18 using extensions::ExtensionAPI; |
| 19 using extensions::Feature; |
| 17 | 20 |
| 18 TEST(ExtensionAPI, IsPrivileged) { | 21 TEST(ExtensionAPI, IsPrivileged) { |
| 19 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); | 22 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); |
| 20 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); | 23 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); |
| 21 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); | 24 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); |
| 22 | 25 |
| 23 // Properties are not supported yet. | 26 // Properties are not supported yet. |
| 24 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); | 27 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); |
| 25 | 28 |
| 26 // Default unknown names to privileged for paranoia's sake. | 29 // Default unknown names to privileged for paranoia's sake. |
| 27 EXPECT_TRUE(extension_api->IsPrivileged("")); | 30 EXPECT_TRUE(extension_api->IsPrivileged("")); |
| 28 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); | 31 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); |
| 29 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); | 32 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); |
| 30 | 33 |
| 31 // Exists, but privileged. | 34 // Exists, but privileged. |
| 32 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); | 35 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); |
| 33 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); | 36 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); |
| 34 | 37 |
| 35 // Whole APIs that are unprivileged. | 38 // Whole APIs that are unprivileged. |
| 39 EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails")); |
| 40 EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled")); |
| 36 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); | 41 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); |
| 37 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); | 42 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); |
| 38 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); | 43 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); |
| 39 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); | 44 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); |
| 40 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); | 45 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); |
| 41 } | 46 } |
| 42 | 47 |
| 43 TEST(ExtensionAPI, IsWholeAPIPrivileged) { | 48 scoped_refptr<Extension> CreateExtensionWithPermissions( |
| 44 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); | 49 const std::set<std::string>& permissions) { |
| 45 | |
| 46 // Completely unprivileged. | |
| 47 EXPECT_FALSE(extension_api->IsWholeAPIPrivileged("storage")); | |
| 48 | |
| 49 // Partially unprivileged. | |
| 50 EXPECT_FALSE(extension_api->IsWholeAPIPrivileged("extension")); | |
| 51 EXPECT_FALSE(extension_api->IsWholeAPIPrivileged("test")); | |
| 52 | |
| 53 // Nothing unprivileged. | |
| 54 EXPECT_TRUE(extension_api->IsWholeAPIPrivileged("history")); | |
| 55 | |
| 56 // Paranoid above... paranoid here, too. | |
| 57 EXPECT_TRUE(extension_api->IsWholeAPIPrivileged("")); | |
| 58 EXPECT_TRUE(extension_api->IsWholeAPIPrivileged("<unknown-namespace>")); | |
| 59 } | |
| 60 | |
| 61 TEST(ExtensionAPI, Depends) { | |
| 62 // Fake extension with the "ttsEngine" permission but not the "tts" | |
| 63 // permission; it must load TTS. | |
| 64 DictionaryValue manifest; | 50 DictionaryValue manifest; |
| 65 manifest.SetString("name", "test extension"); | 51 manifest.SetString("name", "extension"); |
| 66 manifest.SetString("version", "1.0"); | 52 manifest.SetString("version", "1.0"); |
| 67 { | 53 { |
| 68 scoped_ptr<ListValue> permissions(new ListValue()); | 54 scoped_ptr<ListValue> permissions_list(new ListValue()); |
| 69 permissions->Append(Value::CreateStringValue("ttsEngine")); | 55 for (std::set<std::string>::const_iterator i = permissions.begin(); |
| 70 manifest.Set("permissions", permissions.release()); | 56 i != permissions.end(); ++i) { |
| 57 permissions_list->Append(Value::CreateStringValue(*i)); |
| 58 } |
| 59 manifest.Set("permissions", permissions_list.release()); |
| 71 } | 60 } |
| 72 | 61 |
| 73 std::string error; | 62 std::string error; |
| 74 scoped_refptr<Extension> extension(Extension::Create( | 63 scoped_refptr<Extension> extension(Extension::Create( |
| 75 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); | 64 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); |
| 76 CHECK(extension.get()); | 65 CHECK(extension.get()); |
| 77 CHECK(error.empty()); | 66 CHECK(error.empty()); |
| 78 | 67 |
| 79 ExtensionAPI::SchemaMap schemas; | 68 return extension; |
| 80 ExtensionAPI::GetInstance()->GetSchemasForExtension( | |
| 81 *extension, ExtensionAPI::ALL, &schemas); | |
| 82 EXPECT_EQ(1u, schemas.count("tts")); | |
| 83 } | 69 } |
| 70 |
| 71 scoped_refptr<Extension> CreateExtensionWithPermission( |
| 72 const std::string& permission) { |
| 73 std::set<std::string> permissions; |
| 74 permissions.insert(permission); |
| 75 return CreateExtensionWithPermissions(permissions); |
| 76 } |
| 77 |
| 78 TEST(ExtensionAPI, ExtensionWithUnprivilegedAPIs) { |
| 79 scoped_refptr<Extension> extension; |
| 80 { |
| 81 std::set<std::string> permissions; |
| 82 permissions.insert("storage"); |
| 83 permissions.insert("history"); |
| 84 extension = CreateExtensionWithPermissions(permissions); |
| 85 } |
| 86 |
| 87 scoped_ptr<std::set<std::string> > privileged_apis = |
| 88 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 89 Feature::PRIVILEGED_CONTEXT, extension.get(), GURL()); |
| 90 |
| 91 scoped_ptr<std::set<std::string> > unprivileged_apis = |
| 92 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 93 Feature::UNPRIVILEGED_CONTEXT, extension.get(), GURL()); |
| 94 |
| 95 scoped_ptr<std::set<std::string> > content_script_apis = |
| 96 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 97 Feature::CONTENT_SCRIPT_CONTEXT, extension.get(), GURL()); |
| 98 |
| 99 // "storage" is completely unprivileged. |
| 100 EXPECT_EQ(1u, privileged_apis->count("storage")); |
| 101 EXPECT_EQ(1u, unprivileged_apis->count("storage")); |
| 102 EXPECT_EQ(1u, content_script_apis->count("storage")); |
| 103 |
| 104 // "extension" is partially unprivileged. |
| 105 EXPECT_EQ(1u, privileged_apis->count("extension")); |
| 106 EXPECT_EQ(1u, unprivileged_apis->count("extension")); |
| 107 EXPECT_EQ(1u, content_script_apis->count("extension")); |
| 108 |
| 109 // "history" is entirely privileged. |
| 110 EXPECT_EQ(1u, privileged_apis->count("history")); |
| 111 EXPECT_EQ(0u, unprivileged_apis->count("history")); |
| 112 EXPECT_EQ(0u, content_script_apis->count("history")); |
| 113 } |
| 114 |
| 115 TEST(ExtensionAPI, ExtensionWithDependencies) { |
| 116 // Extension with the "ttsEngine" permission but not the "tts" permission; it |
| 117 // must load TTS. |
| 118 { |
| 119 scoped_refptr<Extension> extension = |
| 120 CreateExtensionWithPermission("ttsEngine"); |
| 121 scoped_ptr<std::set<std::string> > apis = |
| 122 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 123 Feature::PRIVILEGED_CONTEXT, extension.get(), GURL()); |
| 124 EXPECT_EQ(1u, apis->count("ttsEngine")); |
| 125 EXPECT_EQ(1u, apis->count("tts")); |
| 126 } |
| 127 |
| 128 // Conversely, extension with the "tts" permission but not the "ttsEngine" |
| 129 // permission shouldn't get the "ttsEngine" permission. |
| 130 { |
| 131 scoped_refptr<Extension> extension = |
| 132 CreateExtensionWithPermission("tts"); |
| 133 scoped_ptr<std::set<std::string> > apis = |
| 134 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 135 Feature::PRIVILEGED_CONTEXT, extension.get(), GURL()); |
| 136 EXPECT_EQ(0u, apis->count("ttsEngine")); |
| 137 EXPECT_EQ(1u, apis->count("tts")); |
| 138 } |
| 139 } |
| 140 |
| 141 bool MatchesURL(const std::string& api_name, const std::string& url) { |
| 142 scoped_ptr<std::set<std::string> > apis = |
| 143 ExtensionAPI::GetInstance()->GetAPIsForContext( |
| 144 Feature::WEB_PAGE_CONTEXT, NULL, GURL(url)); |
| 145 return apis->count(api_name); |
| 146 } |
| 147 |
| 148 TEST(ExtensionAPI, URLMatching) { |
| 149 // "app" API is available to all URLs that content scripts can be injected. |
| 150 EXPECT_TRUE(MatchesURL("app", "http://example.com/example.html")); |
| 151 EXPECT_TRUE(MatchesURL("app", "https://blah.net")); |
| 152 EXPECT_TRUE(MatchesURL("app", "file://somefile.html")); |
| 153 |
| 154 // But not internal URLs (for chrome-extension:// the app API is injected by |
| 155 // GetSchemasForExtension). |
| 156 EXPECT_FALSE(MatchesURL("app", "about:flags")); |
| 157 EXPECT_FALSE(MatchesURL("app", "chrome://flags")); |
| 158 EXPECT_FALSE(MatchesURL("app", "chrome-extension://fakeextension")); |
| 159 |
| 160 // "storage" API (for example) isn't available to any URLs. |
| 161 EXPECT_FALSE(MatchesURL("storage", "http://example.com/example.html")); |
| 162 EXPECT_FALSE(MatchesURL("storage", "https://blah.net")); |
| 163 EXPECT_FALSE(MatchesURL("storage", "file://somefile.html")); |
| 164 EXPECT_FALSE(MatchesURL("storage", "about:flags")); |
| 165 EXPECT_FALSE(MatchesURL("storage", "chrome://flags")); |
| 166 EXPECT_FALSE(MatchesURL("storage", "chrome-extension://fakeextension")); |
| 167 } |
| 168 |
| 169 } // namespace |
| OLD | NEW |