| 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; |
| 17 | 19 |
| 20 enum MatchesURLResult { |
| 21 NO_MATCH, |
| 22 MATCH, |
| 23 UNKNOWN |
| 24 }; |
| 25 |
| 26 MatchesURLResult MatchesURL( |
| 27 const std::string& api_name, const std::string& url) { |
| 28 bool matches_url = |
| 29 ExtensionAPI::GetInstance()->MatchesURL(api_name, GURL(url)); |
| 30 |
| 31 ExtensionAPI::SchemaMap schemas; |
| 32 ExtensionAPI::GetInstance()->GetSchemasForURL(GURL(url), &schemas); |
| 33 bool get_schemas_for_url_contains = schemas.find(api_name) != schemas.end(); |
| 34 |
| 35 if (matches_url && get_schemas_for_url_contains) |
| 36 return MATCH; |
| 37 if (!matches_url && !get_schemas_for_url_contains) |
| 38 return NO_MATCH; |
| 39 return UNKNOWN; |
| 40 } |
| 41 |
| 18 TEST(ExtensionAPI, IsPrivileged) { | 42 TEST(ExtensionAPI, IsPrivileged) { |
| 19 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); | 43 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); |
| 20 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); | 44 EXPECT_FALSE(extension_api->IsPrivileged("extension.connect")); |
| 21 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); | 45 EXPECT_FALSE(extension_api->IsPrivileged("extension.onConnect")); |
| 22 | 46 |
| 23 // Properties are not supported yet. | 47 // Properties are not supported yet. |
| 24 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); | 48 EXPECT_TRUE(extension_api->IsPrivileged("extension.lastError")); |
| 25 | 49 |
| 26 // Default unknown names to privileged for paranoia's sake. | 50 // Default unknown names to privileged for paranoia's sake. |
| 27 EXPECT_TRUE(extension_api->IsPrivileged("")); | 51 EXPECT_TRUE(extension_api->IsPrivileged("")); |
| 28 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); | 52 EXPECT_TRUE(extension_api->IsPrivileged("<unknown-namespace>")); |
| 29 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); | 53 EXPECT_TRUE(extension_api->IsPrivileged("extension.<unknown-member>")); |
| 30 | 54 |
| 31 // Exists, but privileged. | 55 // Exists, but privileged. |
| 32 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); | 56 EXPECT_TRUE(extension_api->IsPrivileged("extension.getViews")); |
| 33 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); | 57 EXPECT_TRUE(extension_api->IsPrivileged("history.search")); |
| 34 | 58 |
| 35 // Whole APIs that are unprivileged. | 59 // Whole APIs that are unprivileged. |
| 60 EXPECT_FALSE(extension_api->IsPrivileged("app.getDetails")); |
| 61 EXPECT_FALSE(extension_api->IsPrivileged("app.isInstalled")); |
| 36 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); | 62 EXPECT_FALSE(extension_api->IsPrivileged("storage.local")); |
| 37 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); | 63 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.onChanged")); |
| 38 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); | 64 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.set")); |
| 39 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); | 65 EXPECT_FALSE(extension_api->IsPrivileged("storage.local.MAX_ITEMS")); |
| 40 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); | 66 EXPECT_FALSE(extension_api->IsPrivileged("storage.set")); |
| 41 } | 67 } |
| 42 | 68 |
| 43 TEST(ExtensionAPI, IsWholeAPIPrivileged) { | 69 TEST(ExtensionAPI, IsWholeAPIPrivileged) { |
| 44 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); | 70 ExtensionAPI* extension_api = ExtensionAPI::GetInstance(); |
| 45 | 71 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 74 scoped_refptr<Extension> extension(Extension::Create( | 100 scoped_refptr<Extension> extension(Extension::Create( |
| 75 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); | 101 FilePath(), Extension::LOAD, manifest, Extension::NO_FLAGS, &error)); |
| 76 CHECK(extension.get()); | 102 CHECK(extension.get()); |
| 77 CHECK(error.empty()); | 103 CHECK(error.empty()); |
| 78 | 104 |
| 79 ExtensionAPI::SchemaMap schemas; | 105 ExtensionAPI::SchemaMap schemas; |
| 80 ExtensionAPI::GetInstance()->GetSchemasForExtension( | 106 ExtensionAPI::GetInstance()->GetSchemasForExtension( |
| 81 *extension, ExtensionAPI::ALL, &schemas); | 107 *extension, ExtensionAPI::ALL, &schemas); |
| 82 EXPECT_EQ(1u, schemas.count("tts")); | 108 EXPECT_EQ(1u, schemas.count("tts")); |
| 83 } | 109 } |
| 110 |
| 111 TEST(ExtensionAPI, MatchesURL) { |
| 112 // "app" API is available to all URLs that content scripts can be injected. |
| 113 EXPECT_EQ(MATCH, MatchesURL("app", "http://example.com/example.html")); |
| 114 EXPECT_EQ(MATCH, MatchesURL("app", "https://blah.net")); |
| 115 EXPECT_EQ(MATCH, MatchesURL("app", "file://somefile.html")); |
| 116 |
| 117 // But not internal URLs (for chrome-extension:// the app API is injected by |
| 118 // GetSchemasForExtension). |
| 119 EXPECT_EQ(NO_MATCH, MatchesURL("app", "about:flags")); |
| 120 EXPECT_EQ(NO_MATCH, MatchesURL("app", "chrome://flags")); |
| 121 EXPECT_EQ(NO_MATCH, MatchesURL("app", "chrome-extension://fakeextension")); |
| 122 |
| 123 // "storage" API (for example) isn't available to any URLs. |
| 124 EXPECT_EQ(NO_MATCH, MatchesURL("storage", "http://example.com/example.html")); |
| 125 EXPECT_EQ(NO_MATCH, MatchesURL("storage", "https://blah.net")); |
| 126 EXPECT_EQ(NO_MATCH, MatchesURL("storage", "file://somefile.html")); |
| 127 EXPECT_EQ(NO_MATCH, MatchesURL("storage", "about:flags")); |
| 128 EXPECT_EQ(NO_MATCH, MatchesURL("storage", "chrome://flags")); |
| 129 EXPECT_EQ(NO_MATCH, MatchesURL("storage", |
| 130 "chrome-extension://fakeextension")); |
| 131 } |
| 132 |
| 133 } // namespace |
| OLD | NEW |