[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | ffbec69 | 2012-02-26 20:26:42 | [diff] [blame] | 5 | #include "base/json/json_file_value_serializer.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 6 | #include "base/message_loop.h" |
| 7 | #include "base/path_service.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 8 | #include "chrome/browser/extensions/extension_info_map.h" |
| 9 | #include "chrome/common/chrome_paths.h" |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 10 | #include "chrome/common/extensions/extension.h" |
[email protected] | a52c0e9 | 2012-03-23 06:02:24 | [diff] [blame] | 11 | #include "chrome/common/extensions/extension_manifest_constants.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 12 | #include "content/test/test_browser_thread.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | 75bff52 | 2011-12-03 00:04:20 | [diff] [blame] | 14 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 15 | #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 16 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 17 | using content::BrowserThread; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame^] | 18 | using extensions::Extension; |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 19 | using WebKit::WebSecurityOrigin; |
| 20 | using WebKit::WebString; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 21 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 22 | namespace keys = extension_manifest_keys; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | class ExtensionInfoMapTest : public testing::Test { |
| 27 | public: |
| 28 | ExtensionInfoMapTest() |
[email protected] | ca4b5fa3 | 2010-10-09 12:42:18 | [diff] [blame] | 29 | : ui_thread_(BrowserThread::UI, &message_loop_), |
| 30 | io_thread_(BrowserThread::IO, &message_loop_) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | private: |
| 34 | MessageLoop message_loop_; |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 35 | content::TestBrowserThread ui_thread_; |
| 36 | content::TestBrowserThread io_thread_; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | // Returns a barebones test Extension object with the given name. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 40 | static scoped_refptr<Extension> CreateExtension(const std::string& name) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 41 | #if defined(OS_WIN) |
| 42 | FilePath path(FILE_PATH_LITERAL("c:\\foo")); |
| 43 | #elif defined(OS_POSIX) |
| 44 | FilePath path(FILE_PATH_LITERAL("/foo")); |
| 45 | #endif |
| 46 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 47 | DictionaryValue manifest; |
| 48 | manifest.SetString(keys::kVersion, "1.0.0.0"); |
| 49 | manifest.SetString(keys::kName, name); |
| 50 | |
| 51 | std::string error; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 52 | scoped_refptr<Extension> extension = Extension::Create( |
[email protected] | 83048a2 | 2011-03-29 00:14:13 | [diff] [blame] | 53 | path.AppendASCII(name), Extension::INVALID, manifest, |
| 54 | Extension::STRICT_ERROR_CHECKS, &error); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 55 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 56 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 57 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 60 | static scoped_refptr<Extension> LoadManifest(const std::string& dir, |
| 61 | const std::string& test_file) { |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 62 | FilePath path; |
| 63 | PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 64 | path = path.AppendASCII("extensions") |
| 65 | .AppendASCII(dir) |
| 66 | .AppendASCII(test_file); |
| 67 | |
| 68 | JSONFileValueSerializer serializer(path); |
| 69 | scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL)); |
| 70 | if (!result.get()) |
| 71 | return NULL; |
| 72 | |
| 73 | std::string error; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 74 | scoped_refptr<Extension> extension = Extension::Create( |
| 75 | path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()), |
[email protected] | 83048a2 | 2011-03-29 00:14:13 | [diff] [blame] | 76 | Extension::STRICT_ERROR_CHECKS, &error); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 77 | EXPECT_TRUE(extension) << error; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 78 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 79 | return extension; |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 82 | // Test that the ExtensionInfoMap handles refcounting properly. |
| 83 | TEST_F(ExtensionInfoMapTest, RefCounting) { |
| 84 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 85 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 86 | // New extensions should have a single reference holding onto them. |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 87 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 88 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
| 89 | scoped_refptr<Extension> extension3(CreateExtension("extension3")); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 90 | EXPECT_TRUE(extension1->HasOneRef()); |
| 91 | EXPECT_TRUE(extension2->HasOneRef()); |
| 92 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 93 | |
[email protected] | 05cc4e7 | 2011-03-08 21:29:48 | [diff] [blame] | 94 | // Add a ref to each extension and give it to the info map. |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 95 | info_map->AddExtension(extension1, base::Time(), false); |
| 96 | info_map->AddExtension(extension2, base::Time(), false); |
| 97 | info_map->AddExtension(extension3, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 98 | |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 99 | // Release extension1, and the info map should have the only ref. |
| 100 | const Extension* weak_extension1 = extension1; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 101 | extension1 = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 102 | EXPECT_TRUE(weak_extension1->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 103 | |
| 104 | // Remove extension2, and the extension2 object should have the only ref. |
[email protected] | 814a7bf0f | 2011-08-13 05:30:59 | [diff] [blame] | 105 | info_map->RemoveExtension( |
| 106 | extension2->id(), extension_misc::UNLOAD_REASON_UNINSTALL); |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 107 | EXPECT_TRUE(extension2->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 108 | |
| 109 | // Delete the info map, and the extension3 object should have the only ref. |
| 110 | info_map = NULL; |
[email protected] | 6f229e8 | 2010-11-02 17:47:26 | [diff] [blame] | 111 | EXPECT_TRUE(extension3->HasOneRef()); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | // Tests that we can query a few extension properties from the ExtensionInfoMap. |
| 115 | TEST_F(ExtensionInfoMapTest, Properties) { |
| 116 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 117 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 118 | scoped_refptr<Extension> extension1(CreateExtension("extension1")); |
| 119 | scoped_refptr<Extension> extension2(CreateExtension("extension2")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 120 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 121 | info_map->AddExtension(extension1, base::Time(), false); |
| 122 | info_map->AddExtension(extension2, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 123 | |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 124 | EXPECT_EQ(2u, info_map->extensions().size()); |
| 125 | EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id())); |
| 126 | EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id())); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | // Tests CheckURLAccessToExtensionPermission given both extension and app URLs. |
| 130 | TEST_F(ExtensionInfoMapTest, CheckPermissions) { |
| 131 | scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap()); |
| 132 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 133 | scoped_refptr<Extension> app(LoadManifest("manifest_tests", |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 134 | "valid_app.json")); |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 135 | scoped_refptr<Extension> extension(LoadManifest("manifest_tests", |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 136 | "tabs_extension.json")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 137 | |
| 138 | GURL app_url("http://www.google.com/mail/foo.html"); |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 139 | WebSecurityOrigin app_origin = WebSecurityOrigin::create( |
| 140 | GURL("http://www.google.com/mail/foo.html")); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 141 | ASSERT_TRUE(app->is_app()); |
[email protected] | cced75a | 2011-05-20 08:31:12 | [diff] [blame] | 142 | ASSERT_TRUE(app->web_extent().MatchesURL(app_url)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 143 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 144 | info_map->AddExtension(app, base::Time(), false); |
| 145 | info_map->AddExtension(extension, base::Time(), false); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 146 | |
| 147 | // The app should have the notifications permission, either from a |
| 148 | // chrome-extension URL or from its web extent. |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 149 | const Extension* match = info_map->extensions().GetExtensionOrAppByURL( |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 150 | ExtensionURLInfo(app_origin, app->GetResourceURL("a.html"))); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 151 | EXPECT_TRUE(match && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 152 | match->HasAPIPermission(ExtensionAPIPermission::kNotification)); |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 153 | match = info_map->extensions().GetExtensionOrAppByURL( |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 154 | ExtensionURLInfo(app_origin, app_url)); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 155 | EXPECT_TRUE(match && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 156 | match->HasAPIPermission(ExtensionAPIPermission::kNotification)); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 157 | EXPECT_FALSE(match && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 158 | match->HasAPIPermission(ExtensionAPIPermission::kTab)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 159 | |
| 160 | // The extension should have the tabs permission. |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 161 | match = info_map->extensions().GetExtensionOrAppByURL( |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 162 | ExtensionURLInfo(app_origin, extension->GetResourceURL("a.html"))); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 163 | EXPECT_TRUE(match && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 164 | match->HasAPIPermission(ExtensionAPIPermission::kTab)); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 165 | EXPECT_FALSE(match && |
[email protected] | 0d3e4a2 | 2011-06-23 19:02:52 | [diff] [blame] | 166 | match->HasAPIPermission(ExtensionAPIPermission::kNotification)); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 167 | |
| 168 | // Random URL should not have any permissions. |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 169 | GURL evil_url("http://evil.com/a.html"); |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 170 | match = info_map->extensions().GetExtensionOrAppByURL( |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 171 | ExtensionURLInfo(WebSecurityOrigin::create(evil_url), evil_url)); |
| 172 | EXPECT_FALSE(match); |
| 173 | |
| 174 | // Sandboxed origins should not have any permissions. |
[email protected] | 615d88f | 2011-12-13 01:47:44 | [diff] [blame] | 175 | match = info_map->extensions().GetExtensionOrAppByURL(ExtensionURLInfo( |
[email protected] | 69729ca | 2011-12-02 08:01:25 | [diff] [blame] | 176 | WebSecurityOrigin::createFromString(WebString::fromUTF8("null")), |
| 177 | app_url)); |
[email protected] | be0a2cfd | 2011-06-02 21:36:42 | [diff] [blame] | 178 | EXPECT_FALSE(match); |
[email protected] | 4361c7c | 2010-09-30 21:57:53 | [diff] [blame] | 179 | } |
[email protected] | 5eaba82a | 2010-10-01 20:55:53 | [diff] [blame] | 180 | |
| 181 | } // namespace |