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