blob: 439e42a352013aa0e451380b871784e577394ef9 [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]12d5c8d2013-01-15 07:28:0114#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
15#include "third_party/WebKit/Source/Platform/chromium/public/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]69729ca2011-12-02 08:01:2520using WebKit::WebSecurityOrigin;
21using WebKit::WebString;
[email protected]631bb742011-11-02 11:29:3922
[email protected]4361c7c2010-09-30 21:57:5323namespace keys = extension_manifest_keys;
24
25namespace {
26
27class ExtensionInfoMapTest : public testing::Test {
28 public:
29 ExtensionInfoMapTest()
[email protected]ca4b5fa32010-10-09 12:42:1830 : ui_thread_(BrowserThread::UI, &message_loop_),
31 io_thread_(BrowserThread::IO, &message_loop_) {
[email protected]4361c7c2010-09-30 21:57:5332 }
33
34 private:
35 MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4936 content::TestBrowserThread ui_thread_;
37 content::TestBrowserThread io_thread_;
[email protected]4361c7c2010-09-30 21:57:5338};
39
40// Returns a barebones test Extension object with the given name.
[email protected]66e4eb32010-10-27 20:37:4141static scoped_refptr<Extension> CreateExtension(const std::string& name) {
[email protected]4361c7c2010-09-30 21:57:5342#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]4361c7c2010-09-30 21:57:5348 DictionaryValue manifest;
49 manifest.SetString(keys::kVersion, "1.0.0.0");
50 manifest.SetString(keys::kName, name);
51
52 std::string error;
[email protected]66e4eb32010-10-27 20:37:4153 scoped_refptr<Extension> extension = Extension::Create(
[email protected]83048a22011-03-29 00:14:1354 path.AppendASCII(name), Extension::INVALID, manifest,
[email protected]ed3b9b12012-05-31 18:37:5155 Extension::NO_FLAGS, &error);
[email protected]66e4eb32010-10-27 20:37:4156 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5357
[email protected]66e4eb32010-10-27 20:37:4158 return extension;
[email protected]4361c7c2010-09-30 21:57:5359}
60
[email protected]66e4eb32010-10-27 20:37:4161static scoped_refptr<Extension> LoadManifest(const std::string& dir,
62 const std::string& test_file) {
[email protected]4361c7c2010-09-30 21:57:5363 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]66e4eb32010-10-27 20:37:4175 scoped_refptr<Extension> extension = Extension::Create(
76 path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()),
[email protected]ed3b9b12012-05-31 18:37:5177 Extension::NO_FLAGS, &error);
[email protected]66e4eb32010-10-27 20:37:4178 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5379
[email protected]66e4eb32010-10-27 20:37:4180 return extension;
[email protected]4361c7c2010-09-30 21:57:5381}
82
[email protected]4361c7c2010-09-30 21:57:5383// Test that the ExtensionInfoMap handles refcounting properly.
84TEST_F(ExtensionInfoMapTest, RefCounting) {
85 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
86
[email protected]6f229e82010-11-02 17:47:2687 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4188 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
89 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
90 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2691 EXPECT_TRUE(extension1->HasOneRef());
92 EXPECT_TRUE(extension2->HasOneRef());
93 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5394
[email protected]05cc4e72011-03-08 21:29:4895 // Add a ref to each extension and give it to the info map.
[email protected]c357acb42011-06-09 20:52:4296 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]4361c7c2010-09-30 21:57:5399
[email protected]6f229e82010-11-02 17:47:26100 // Release extension1, and the info map should have the only ref.
101 const Extension* weak_extension1 = extension1;
[email protected]66e4eb32010-10-27 20:37:41102 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:26103 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53104
105 // Remove extension2, and the extension2 object should have the only ref.
[email protected]814a7bf0f2011-08-13 05:30:59106 info_map->RemoveExtension(
107 extension2->id(), extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]6f229e82010-11-02 17:47:26108 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53109
110 // Delete the info map, and the extension3 object should have the only ref.
111 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:26112 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53113}
114
115// Tests that we can query a few extension properties from the ExtensionInfoMap.
116TEST_F(ExtensionInfoMapTest, Properties) {
117 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
118
[email protected]66e4eb32010-10-27 20:37:41119 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
120 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:53121
[email protected]c357acb42011-06-09 20:52:42122 info_map->AddExtension(extension1, base::Time(), false);
123 info_map->AddExtension(extension2, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53124
[email protected]be0a2cfd2011-06-02 21:36:42125 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]4361c7c2010-09-30 21:57:53128}
129
130// Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
131TEST_F(ExtensionInfoMapTest, CheckPermissions) {
132 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
133
[email protected]66e4eb32010-10-27 20:37:41134 scoped_refptr<Extension> app(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42135 "valid_app.json"));
[email protected]66e4eb32010-10-27 20:37:41136 scoped_refptr<Extension> extension(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42137 "tabs_extension.json"));
[email protected]4361c7c2010-09-30 21:57:53138
139 GURL app_url("http://www.google.com/mail/foo.html");
[email protected]69729ca2011-12-02 08:01:25140 WebSecurityOrigin app_origin = WebSecurityOrigin::create(
141 GURL("http://www.google.com/mail/foo.html"));
[email protected]4361c7c2010-09-30 21:57:53142 ASSERT_TRUE(app->is_app());
[email protected]cced75a2011-05-20 08:31:12143 ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
[email protected]4361c7c2010-09-30 21:57:53144
[email protected]c357acb42011-06-09 20:52:42145 info_map->AddExtension(app, base::Time(), false);
146 info_map->AddExtension(extension, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53147
148 // The app should have the notifications permission, either from a
149 // chrome-extension URL or from its web extent.
[email protected]615d88f2011-12-13 01:47:44150 const Extension* match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25151 ExtensionURLInfo(app_origin, app->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42152 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06153 match->HasAPIPermission(APIPermission::kNotification));
[email protected]615d88f2011-12-13 01:47:44154 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25155 ExtensionURLInfo(app_origin, app_url));
[email protected]be0a2cfd2011-06-02 21:36:42156 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06157 match->HasAPIPermission(APIPermission::kNotification));
[email protected]be0a2cfd2011-06-02 21:36:42158 EXPECT_FALSE(match &&
[email protected]c2e66e12012-06-27 06:27:06159 match->HasAPIPermission(APIPermission::kTab));
[email protected]4361c7c2010-09-30 21:57:53160
161 // The extension should have the tabs permission.
[email protected]615d88f2011-12-13 01:47:44162 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25163 ExtensionURLInfo(app_origin, extension->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42164 EXPECT_TRUE(match &&
[email protected]c2e66e12012-06-27 06:27:06165 match->HasAPIPermission(APIPermission::kTab));
[email protected]be0a2cfd2011-06-02 21:36:42166 EXPECT_FALSE(match &&
[email protected]c2e66e12012-06-27 06:27:06167 match->HasAPIPermission(APIPermission::kNotification));
[email protected]4361c7c2010-09-30 21:57:53168
169 // Random URL should not have any permissions.
[email protected]69729ca2011-12-02 08:01:25170 GURL evil_url("http://evil.com/a.html");
[email protected]615d88f2011-12-13 01:47:44171 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25172 ExtensionURLInfo(WebSecurityOrigin::create(evil_url), evil_url));
173 EXPECT_FALSE(match);
174
175 // Sandboxed origins should not have any permissions.
[email protected]615d88f2011-12-13 01:47:44176 match = info_map->extensions().GetExtensionOrAppByURL(ExtensionURLInfo(
[email protected]69729ca2011-12-02 08:01:25177 WebSecurityOrigin::createFromString(WebString::fromUTF8("null")),
178 app_url));
[email protected]be0a2cfd2011-06-02 21:36:42179 EXPECT_FALSE(match);
[email protected]4361c7c2010-09-30 21:57:53180}
[email protected]5eaba82a2010-10-01 20:55:53181
182} // namespace