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