| 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 | |
| 18 using extensions::ExtensionAPI; | 16 using extensions::ExtensionAPI; |
| 19 using extensions::Feature; | |
| 20 | 17 |
| 21 TEST(ExtensionAPI, IsPrivileged) { | 18 TEST(ExtensionAPI, IsPrivileged) { |
| 22 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); | 19 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); |
| 23 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); | 20 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); |
| 24 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); | 21 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); |
| 25 | 22 |
| 26 // Properties are not supported yet. | 23 // Properties are not supported yet. |
| 27 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); | 24 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); |
| 28 | 25 |
| 29 // Default unknown names to privileged for paranoia's sake. | 26 // Default unknown names to privileged for paranoia's sake. |
| 30 EXPECT_TRUE(extension_api->IsPrivileged("")); | 27 EXPECT_TRUE(extension_api->IsPrivileged("")); |
| 31 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); | 28 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); |
| 32 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); | 29 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); |
| 33 | 30 |
| 34 // Exists, but privileged. | 31 // Exists, but privileged. |
| 35 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); | 32 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); |
| 36 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); | 33 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); |
| 37 | 34 |
| 38 // Whole APIs that are unprivileged. | 35 // Whole APIs that are unprivileged. |
| 39 EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails")); | |
| 40 EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled")); | |
| 41 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); | 36 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); |
| 42 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); | 37 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); |
| 43 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); | 38 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); |
| 44 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); | 39 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); |
| 45 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); | 40 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); |
| 46 } | 41 } |
| 47 | 42 |
| 48 scoped_refptr<Extension> CreateExtensionWithPermissions( | 43 TEST(ExtensionAPI, IsWholeAPIPrivileged) { |
| 49 const std::set<std::string>& permissions) { | 44 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); |
| 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. |
| 50 DictionaryValue manifest; | 64 DictionaryValue manifest; |
| 51 manifest.SetString("name", "extension"); | 65 manifest.SetString("name", "test extension"); |
| 52 manifest.SetString("version", "1.0"); | 66 manifest.SetString("version", "1.0"); |
| 53 { | 67 { |
| 54 scoped_ptr<ListValue> permissions_list(new ListValue()); | 68 scoped_ptr<ListValue> permissions(new ListValue()); |
| 55 for (std::set<std::string>::const_iterator i = permissions.begin(); | 69 permissions->Append(Value::CreateStringValue("ttsEngine")); |
| 56 i != permissions.end(); ++i) { | 70 manifest.Set("permissions", permissions.release()); |
| 57 permissions_list->Append(Value::CreateStringValue(*i)); | |
| 58 } | |
| 59 manifest.Set("permissions", permissions_list.release()); | |
| 60 } | 71 } |
| 61 | 72 |
| 62 std::string error; | 73 std::string error; |
| 63 scoped_refptr<Extension> extension(Extension::Create( | 74 scoped_refptr<Extension> extension(Extension::Create( |
| 64 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); | 75 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); |
| 65 CHECK(extension.get()); | 76 CHECK(extension.get()); |
| 66 CHECK(error.empty()); | 77 CHECK(error.empty()); |
| 67 | 78 |
| 68 return extension; | 79 ExtensionAPI::SchemaMap schemas; |
| 80 ExtensionAPI::GetInstance()->GetSchemasForExtension( |
| 81 *extension, ExtensionAPI::ALL, &schemas); |
| 82 EXPECT_EQ(1u, schemas.count("tts")); |
| 69 } | 83 } |
| 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 |