blob: 9be88c01b0dbb89caa42e423f252e01c501e9a32 [file] [log] [blame]
[email protected]ffbec692012-02-26 20:26:421// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]4361c7c2010-09-30 21:57:532// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]ffbec692012-02-26 20:26:425#include "base/json/json_file_value_serializer.h"
[email protected]4361c7c2010-09-30 21:57:536#include "base/message_loop.h"
7#include "base/path_service.h"
[email protected]4361c7c2010-09-30 21:57:538#include "chrome/browser/extensions/extension_info_map.h"
9#include "chrome/common/chrome_paths.h"
[email protected]6f229e82010-11-02 17:47:2610#include "chrome/common/extensions/extension.h"
[email protected]a52c0e92012-03-23 06:02:2411#include "chrome/common/extensions/extension_manifest_constants.h"
[email protected]e97882f2012-06-04 02:23:1712#include "content/public/test/test_browser_thread.h"
[email protected]4361c7c2010-09-30 21:57:5313#include "testing/gtest/include/gtest/gtest.h"
[email protected]e48cf4f2013-05-30 12:40:1214#include "third_party/WebKit/public/platform/WebString.h"
15#include "third_party/WebKit/public/platform/WebURL.h"
[email protected]4361c7c2010-09-30 21:57:5316
[email protected]631bb742011-11-02 11:29:3917using content::BrowserThread;
[email protected]c2e66e12012-06-27 06:27:0618using extensions::APIPermission;
[email protected]1c321ee2012-05-21 03:02:3419using extensions::Extension;
[email protected]1d5e58b2013-01-31 08:41:4020using extensions::Manifest;
[email protected]69729ca2011-12-02 08:01:2521using WebKit::WebSecurityOrigin;
22using WebKit::WebString;
[email protected]631bb742011-11-02 11:29:3923
[email protected]4361c7c2010-09-30 21:57:5324namespace keys = extension_manifest_keys;
25
26namespace {
27
[email protected]d592b1bd2013-05-06 06:40:4728class ExtensionInfoMapTest : public testing::Test {
[email protected]4361c7c2010-09-30 21:57:5329 public:
30 ExtensionInfoMapTest()
[email protected]ca4b5fa32010-10-09 12:42:1831 : ui_thread_(BrowserThread::UI, &message_loop_),
32 io_thread_(BrowserThread::IO, &message_loop_) {
[email protected]4361c7c2010-09-30 21:57:5333 }
34
35 private:
[email protected]b3a25092013-05-28 22:08:1636 base::MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4937 content::TestBrowserThread ui_thread_;
38 content::TestBrowserThread io_thread_;
[email protected]4361c7c2010-09-30 21:57:5339};
40
41// Returns a barebones test Extension object with the given name.
[email protected]66e4eb32010-10-27 20:37:4142static scoped_refptr<Extension> CreateExtension(const std::string& name) {
[email protected]4361c7c2010-09-30 21:57:5343#if defined(OS_WIN)
[email protected]650b2d52013-02-10 03:41:4544 base::FilePath path(FILE_PATH_LITERAL("c:\\foo"));
[email protected]4361c7c2010-09-30 21:57:5345#elif defined(OS_POSIX)
[email protected]650b2d52013-02-10 03:41:4546 base::FilePath path(FILE_PATH_LITERAL("/foo"));
[email protected]4361c7c2010-09-30 21:57:5347#endif
48
[email protected]4361c7c2010-09-30 21:57:5349 DictionaryValue manifest;
50 manifest.SetString(keys::kVersion, "1.0.0.0");
51 manifest.SetString(keys::kName, name);
52
53 std::string error;
[email protected]66e4eb32010-10-27 20:37:4154 scoped_refptr<Extension> extension = Extension::Create(
[email protected]1d5e58b2013-01-31 08:41:4055 path.AppendASCII(name), Manifest::INVALID_LOCATION, manifest,
[email protected]ed3b9b12012-05-31 18:37:5156 Extension::NO_FLAGS, &error);
[email protected]66e4eb32010-10-27 20:37:4157 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5358
[email protected]66e4eb32010-10-27 20:37:4159 return extension;
[email protected]4361c7c2010-09-30 21:57:5360}
61
[email protected]66e4eb32010-10-27 20:37:4162static scoped_refptr<Extension> LoadManifest(const std::string& dir,
63 const std::string& test_file) {
[email protected]650b2d52013-02-10 03:41:4564 base::FilePath path;
[email protected]4361c7c2010-09-30 21:57:5365 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]3eeddd892013-04-17 17:00:1172 if (!result)
[email protected]4361c7c2010-09-30 21:57:5373 return NULL;
74
75 std::string error;
[email protected]66e4eb32010-10-27 20:37:4176 scoped_refptr<Extension> extension = Extension::Create(
[email protected]1d5e58b2013-01-31 08:41:4077 path, Manifest::INVALID_LOCATION,
78 *static_cast<DictionaryValue*>(result.get()),
[email protected]ed3b9b12012-05-31 18:37:5179 Extension::NO_FLAGS, &error);
[email protected]66e4eb32010-10-27 20:37:4180 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5381
[email protected]66e4eb32010-10-27 20:37:4182 return extension;
[email protected]4361c7c2010-09-30 21:57:5383}
84
[email protected]4361c7c2010-09-30 21:57:5385// Test that the ExtensionInfoMap handles refcounting properly.
86TEST_F(ExtensionInfoMapTest, RefCounting) {
87 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
88
[email protected]6f229e82010-11-02 17:47:2689 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4190 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
91 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
92 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2693 EXPECT_TRUE(extension1->HasOneRef());
94 EXPECT_TRUE(extension2->HasOneRef());
95 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5396
[email protected]05cc4e72011-03-08 21:29:4897 // Add a ref to each extension and give it to the info map.
[email protected]c357acb42011-06-09 20:52:4298 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]4361c7c2010-09-30 21:57:53101
[email protected]6f229e82010-11-02 17:47:26102 // Release extension1, and the info map should have the only ref.
103 const Extension* weak_extension1 = extension1;
[email protected]66e4eb32010-10-27 20:37:41104 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:26105 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53106
107 // Remove extension2, and the extension2 object should have the only ref.
[email protected]814a7bf0f2011-08-13 05:30:59108 info_map->RemoveExtension(
109 extension2->id(), extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]6f229e82010-11-02 17:47:26110 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53111
112 // Delete the info map, and the extension3 object should have the only ref.
113 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:26114 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53115}
116
117// Tests that we can query a few extension properties from the ExtensionInfoMap.
118TEST_F(ExtensionInfoMapTest, Properties) {
119 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
120
[email protected]66e4eb32010-10-27 20:37:41121 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
122 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:53123
[email protected]c357acb42011-06-09 20:52:42124 info_map->AddExtension(extension1, base::Time(), false);
125 info_map->AddExtension(extension2, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53126
[email protected]be0a2cfd2011-06-02 21:36:42127 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]4361c7c2010-09-30 21:57:53130}
131
132// Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
133TEST_F(ExtensionInfoMapTest, CheckPermissions) {
134 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
135
[email protected]66e4eb32010-10-27 20:37:41136 scoped_refptr<Extension> app(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42137 "valid_app.json"));
[email protected]66e4eb32010-10-27 20:37:41138 scoped_refptr<Extension> extension(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42139 "tabs_extension.json"));
[email protected]4361c7c2010-09-30 21:57:53140
141 GURL app_url("http://www.google.com/mail/foo.html");
[email protected]69729ca2011-12-02 08:01:25142 WebSecurityOrigin app_origin = WebSecurityOrigin::create(
143 GURL("http://www.google.com/mail/foo.html"));
[email protected]4361c7c2010-09-30 21:57:53144 ASSERT_TRUE(app->is_app());
[email protected]cced75a2011-05-20 08:31:12145 ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
[email protected]4361c7c2010-09-30 21:57:53146
[email protected]c357acb42011-06-09 20:52:42147 info_map->AddExtension(app, base::Time(), false);
148 info_map->AddExtension(extension, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53149
150 // The app should have the notifications permission, either from a
151 // chrome-extension URL or from its web extent.
[email protected]615d88f2011-12-13 01:47:44152 const Extension* match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25153 ExtensionURLInfo(app_origin, app->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42154 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06155 match->HasAPIPermission(APIPermission::kNotification));
[email protected]615d88f2011-12-13 01:47:44156 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25157 ExtensionURLInfo(app_origin, app_url));
[email protected]be0a2cfd2011-06-02 21:36:42158 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06159 match->HasAPIPermission(APIPermission::kNotification));
[email protected]be0a2cfd2011-06-02 21:36:42160 EXPECT_FALSE(match &&
[email protected]c2e66e12012-06-27 06:27:06161 match->HasAPIPermission(APIPermission::kTab));
[email protected]4361c7c2010-09-30 21:57:53162
163 // The extension should have the tabs permission.
[email protected]615d88f2011-12-13 01:47:44164 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25165 ExtensionURLInfo(app_origin, extension->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42166 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06167 match->HasAPIPermission(APIPermission::kTab));
[email protected]be0a2cfd2011-06-02 21:36:42168 EXPECT_FALSE(match &&
[email protected]c2e66e12012-06-27 06:27:06169 match->HasAPIPermission(APIPermission::kNotification));
[email protected]4361c7c2010-09-30 21:57:53170
171 // Random URL should not have any permissions.
[email protected]69729ca2011-12-02 08:01:25172 GURL evil_url("http://evil.com/a.html");
[email protected]615d88f2011-12-13 01:47:44173 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25174 ExtensionURLInfo(WebSecurityOrigin::create(evil_url), evil_url));
175 EXPECT_FALSE(match);
176
177 // Sandboxed origins should not have any permissions.
[email protected]615d88f2011-12-13 01:47:44178 match = info_map->extensions().GetExtensionOrAppByURL(ExtensionURLInfo(
[email protected]69729ca2011-12-02 08:01:25179 WebSecurityOrigin::createFromString(WebString::fromUTF8("null")),
180 app_url));
[email protected]be0a2cfd2011-06-02 21:36:42181 EXPECT_FALSE(match);
[email protected]4361c7c2010-09-30 21:57:53182}
[email protected]5eaba82a2010-10-01 20:55:53183
184} // namespace