blob: 1317c9ef276836069c49cc3d6a485f4a0b4e9bc0 [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]c38831a12011-10-28 12:44:4912#include "content/test/test_browser_thread.h"
[email protected]4361c7c2010-09-30 21:57:5313#include "testing/gtest/include/gtest/gtest.h"
[email protected]75bff522011-12-03 00:04:2014#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]4361c7c2010-09-30 21:57:5316
[email protected]631bb742011-11-02 11:29:3917using content::BrowserThread;
[email protected]1c321ee2012-05-21 03:02:3418using extensions::Extension;
[email protected]69729ca2011-12-02 08:01:2519using WebKit::WebSecurityOrigin;
20using WebKit::WebString;
[email protected]631bb742011-11-02 11:29:3921
[email protected]4361c7c2010-09-30 21:57:5322namespace keys = extension_manifest_keys;
23
24namespace {
25
26class ExtensionInfoMapTest : public testing::Test {
27 public:
28 ExtensionInfoMapTest()
[email protected]ca4b5fa32010-10-09 12:42:1829 : ui_thread_(BrowserThread::UI, &message_loop_),
30 io_thread_(BrowserThread::IO, &message_loop_) {
[email protected]4361c7c2010-09-30 21:57:5331 }
32
33 private:
34 MessageLoop message_loop_;
[email protected]c38831a12011-10-28 12:44:4935 content::TestBrowserThread ui_thread_;
36 content::TestBrowserThread io_thread_;
[email protected]4361c7c2010-09-30 21:57:5337};
38
39// Returns a barebones test Extension object with the given name.
[email protected]66e4eb32010-10-27 20:37:4140static scoped_refptr<Extension> CreateExtension(const std::string& name) {
[email protected]4361c7c2010-09-30 21:57:5341#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]4361c7c2010-09-30 21:57:5347 DictionaryValue manifest;
48 manifest.SetString(keys::kVersion, "1.0.0.0");
49 manifest.SetString(keys::kName, name);
50
51 std::string error;
[email protected]66e4eb32010-10-27 20:37:4152 scoped_refptr<Extension> extension = Extension::Create(
[email protected]83048a22011-03-29 00:14:1353 path.AppendASCII(name), Extension::INVALID, manifest,
54 Extension::STRICT_ERROR_CHECKS, &error);
[email protected]66e4eb32010-10-27 20:37:4155 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5356
[email protected]66e4eb32010-10-27 20:37:4157 return extension;
[email protected]4361c7c2010-09-30 21:57:5358}
59
[email protected]66e4eb32010-10-27 20:37:4160static scoped_refptr<Extension> LoadManifest(const std::string& dir,
61 const std::string& test_file) {
[email protected]4361c7c2010-09-30 21:57:5362 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]66e4eb32010-10-27 20:37:4174 scoped_refptr<Extension> extension = Extension::Create(
75 path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()),
[email protected]83048a22011-03-29 00:14:1376 Extension::STRICT_ERROR_CHECKS, &error);
[email protected]66e4eb32010-10-27 20:37:4177 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5378
[email protected]66e4eb32010-10-27 20:37:4179 return extension;
[email protected]4361c7c2010-09-30 21:57:5380}
81
[email protected]4361c7c2010-09-30 21:57:5382// Test that the ExtensionInfoMap handles refcounting properly.
83TEST_F(ExtensionInfoMapTest, RefCounting) {
84 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
85
[email protected]6f229e82010-11-02 17:47:2686 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4187 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
88 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
89 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2690 EXPECT_TRUE(extension1->HasOneRef());
91 EXPECT_TRUE(extension2->HasOneRef());
92 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5393
[email protected]05cc4e72011-03-08 21:29:4894 // Add a ref to each extension and give it to the info map.
[email protected]c357acb42011-06-09 20:52:4295 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]4361c7c2010-09-30 21:57:5398
[email protected]6f229e82010-11-02 17:47:2699 // Release extension1, and the info map should have the only ref.
100 const Extension* weak_extension1 = extension1;
[email protected]66e4eb32010-10-27 20:37:41101 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:26102 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53103
104 // Remove extension2, and the extension2 object should have the only ref.
[email protected]814a7bf0f2011-08-13 05:30:59105 info_map->RemoveExtension(
106 extension2->id(), extension_misc::UNLOAD_REASON_UNINSTALL);
[email protected]6f229e82010-11-02 17:47:26107 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53108
109 // Delete the info map, and the extension3 object should have the only ref.
110 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:26111 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53112}
113
114// Tests that we can query a few extension properties from the ExtensionInfoMap.
115TEST_F(ExtensionInfoMapTest, Properties) {
116 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
117
[email protected]66e4eb32010-10-27 20:37:41118 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
119 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:53120
[email protected]c357acb42011-06-09 20:52:42121 info_map->AddExtension(extension1, base::Time(), false);
122 info_map->AddExtension(extension2, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53123
[email protected]be0a2cfd2011-06-02 21:36:42124 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]4361c7c2010-09-30 21:57:53127}
128
129// Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
130TEST_F(ExtensionInfoMapTest, CheckPermissions) {
131 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
132
[email protected]66e4eb32010-10-27 20:37:41133 scoped_refptr<Extension> app(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42134 "valid_app.json"));
[email protected]66e4eb32010-10-27 20:37:41135 scoped_refptr<Extension> extension(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42136 "tabs_extension.json"));
[email protected]4361c7c2010-09-30 21:57:53137
138 GURL app_url("http://www.google.com/mail/foo.html");
[email protected]69729ca2011-12-02 08:01:25139 WebSecurityOrigin app_origin = WebSecurityOrigin::create(
140 GURL("http://www.google.com/mail/foo.html"));
[email protected]4361c7c2010-09-30 21:57:53141 ASSERT_TRUE(app->is_app());
[email protected]cced75a2011-05-20 08:31:12142 ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
[email protected]4361c7c2010-09-30 21:57:53143
[email protected]c357acb42011-06-09 20:52:42144 info_map->AddExtension(app, base::Time(), false);
145 info_map->AddExtension(extension, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53146
147 // The app should have the notifications permission, either from a
148 // chrome-extension URL or from its web extent.
[email protected]615d88f2011-12-13 01:47:44149 const Extension* match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25150 ExtensionURLInfo(app_origin, app->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42151 EXPECT_TRUE(match &&
[email protected]0d3e4a22011-06-23 19:02:52152 match->HasAPIPermission(ExtensionAPIPermission::kNotification));
[email protected]615d88f2011-12-13 01:47:44153 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25154 ExtensionURLInfo(app_origin, app_url));
[email protected]be0a2cfd2011-06-02 21:36:42155 EXPECT_TRUE(match &&
[email protected]0d3e4a22011-06-23 19:02:52156 match->HasAPIPermission(ExtensionAPIPermission::kNotification));
[email protected]be0a2cfd2011-06-02 21:36:42157 EXPECT_FALSE(match &&
[email protected]0d3e4a22011-06-23 19:02:52158 match->HasAPIPermission(ExtensionAPIPermission::kTab));
[email protected]4361c7c2010-09-30 21:57:53159
160 // The extension should have the tabs permission.
[email protected]615d88f2011-12-13 01:47:44161 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25162 ExtensionURLInfo(app_origin, extension->GetResourceURL("a.html")));
[email protected]be0a2cfd2011-06-02 21:36:42163 EXPECT_TRUE(match &&
[email protected]0d3e4a22011-06-23 19:02:52164 match->HasAPIPermission(ExtensionAPIPermission::kTab));
[email protected]be0a2cfd2011-06-02 21:36:42165 EXPECT_FALSE(match &&
[email protected]0d3e4a22011-06-23 19:02:52166 match->HasAPIPermission(ExtensionAPIPermission::kNotification));
[email protected]4361c7c2010-09-30 21:57:53167
168 // Random URL should not have any permissions.
[email protected]69729ca2011-12-02 08:01:25169 GURL evil_url("http://evil.com/a.html");
[email protected]615d88f2011-12-13 01:47:44170 match = info_map->extensions().GetExtensionOrAppByURL(
[email protected]69729ca2011-12-02 08:01:25171 ExtensionURLInfo(WebSecurityOrigin::create(evil_url), evil_url));
172 EXPECT_FALSE(match);
173
174 // Sandboxed origins should not have any permissions.
[email protected]615d88f2011-12-13 01:47:44175 match = info_map->extensions().GetExtensionOrAppByURL(ExtensionURLInfo(
[email protected]69729ca2011-12-02 08:01:25176 WebSecurityOrigin::createFromString(WebString::fromUTF8("null")),
177 app_url));
[email protected]be0a2cfd2011-06-02 21:36:42178 EXPECT_FALSE(match);
[email protected]4361c7c2010-09-30 21:57:53179}
[email protected]5eaba82a2010-10-01 20:55:53180
181} // namespace