blob: a3556738ef362dedeb7d9894668d6c377e5e644c [file] [log] [blame]
[email protected]83048a22011-03-29 00:14:131// Copyright (c) 2011 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
5#include "base/message_loop.h"
6#include "base/path_service.h"
[email protected]4361c7c2010-09-30 21:57:537#include "chrome/browser/extensions/extension_info_map.h"
8#include "chrome/common/chrome_paths.h"
[email protected]6f229e82010-11-02 17:47:269#include "chrome/common/extensions/extension.h"
[email protected]1bda97552011-03-01 20:11:5210#include "content/browser/browser_thread.h"
[email protected]c3113022011-04-16 03:26:3011#include "content/common/json_value_serializer.h"
[email protected]4361c7c2010-09-30 21:57:5312#include "testing/gtest/include/gtest/gtest.h"
13
14namespace keys = extension_manifest_keys;
15
16namespace {
17
18class ExtensionInfoMapTest : public testing::Test {
19 public:
20 ExtensionInfoMapTest()
[email protected]ca4b5fa32010-10-09 12:42:1821 : ui_thread_(BrowserThread::UI, &message_loop_),
22 io_thread_(BrowserThread::IO, &message_loop_) {
[email protected]4361c7c2010-09-30 21:57:5323 }
24
25 private:
26 MessageLoop message_loop_;
[email protected]ca4b5fa32010-10-09 12:42:1827 BrowserThread ui_thread_;
28 BrowserThread io_thread_;
[email protected]4361c7c2010-09-30 21:57:5329};
30
31// Returns a barebones test Extension object with the given name.
[email protected]66e4eb32010-10-27 20:37:4132static scoped_refptr<Extension> CreateExtension(const std::string& name) {
[email protected]4361c7c2010-09-30 21:57:5333#if defined(OS_WIN)
34 FilePath path(FILE_PATH_LITERAL("c:\\foo"));
35#elif defined(OS_POSIX)
36 FilePath path(FILE_PATH_LITERAL("/foo"));
37#endif
38
[email protected]4361c7c2010-09-30 21:57:5339 DictionaryValue manifest;
40 manifest.SetString(keys::kVersion, "1.0.0.0");
41 manifest.SetString(keys::kName, name);
42
43 std::string error;
[email protected]66e4eb32010-10-27 20:37:4144 scoped_refptr<Extension> extension = Extension::Create(
[email protected]83048a22011-03-29 00:14:1345 path.AppendASCII(name), Extension::INVALID, manifest,
46 Extension::STRICT_ERROR_CHECKS, &error);
[email protected]66e4eb32010-10-27 20:37:4147 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5348
[email protected]66e4eb32010-10-27 20:37:4149 return extension;
[email protected]4361c7c2010-09-30 21:57:5350}
51
[email protected]66e4eb32010-10-27 20:37:4152static scoped_refptr<Extension> LoadManifest(const std::string& dir,
53 const std::string& test_file) {
[email protected]4361c7c2010-09-30 21:57:5354 FilePath path;
55 PathService::Get(chrome::DIR_TEST_DATA, &path);
56 path = path.AppendASCII("extensions")
57 .AppendASCII(dir)
58 .AppendASCII(test_file);
59
60 JSONFileValueSerializer serializer(path);
61 scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL));
62 if (!result.get())
63 return NULL;
64
65 std::string error;
[email protected]66e4eb32010-10-27 20:37:4166 scoped_refptr<Extension> extension = Extension::Create(
67 path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()),
[email protected]83048a22011-03-29 00:14:1368 Extension::STRICT_ERROR_CHECKS, &error);
[email protected]66e4eb32010-10-27 20:37:4169 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5370
[email protected]66e4eb32010-10-27 20:37:4171 return extension;
[email protected]4361c7c2010-09-30 21:57:5372}
73
[email protected]4361c7c2010-09-30 21:57:5374// Test that the ExtensionInfoMap handles refcounting properly.
75TEST_F(ExtensionInfoMapTest, RefCounting) {
76 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
77
[email protected]6f229e82010-11-02 17:47:2678 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4179 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
80 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
81 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2682 EXPECT_TRUE(extension1->HasOneRef());
83 EXPECT_TRUE(extension2->HasOneRef());
84 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5385
[email protected]05cc4e72011-03-08 21:29:4886 // Add a ref to each extension and give it to the info map.
[email protected]c357acb42011-06-09 20:52:4287 info_map->AddExtension(extension1, base::Time(), false);
88 info_map->AddExtension(extension2, base::Time(), false);
89 info_map->AddExtension(extension3, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:5390
[email protected]6f229e82010-11-02 17:47:2691 // Release extension1, and the info map should have the only ref.
92 const Extension* weak_extension1 = extension1;
[email protected]66e4eb32010-10-27 20:37:4193 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:2694 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5395
96 // Remove extension2, and the extension2 object should have the only ref.
[email protected]dd163fb02011-05-04 22:22:1797 info_map->RemoveExtension(extension2->id(), UnloadedExtensionInfo::UNINSTALL);
[email protected]6f229e82010-11-02 17:47:2698 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5399
100 // Delete the info map, and the extension3 object should have the only ref.
101 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:26102 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53103}
104
105// Tests that we can query a few extension properties from the ExtensionInfoMap.
106TEST_F(ExtensionInfoMapTest, Properties) {
107 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
108
[email protected]66e4eb32010-10-27 20:37:41109 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
110 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:53111
[email protected]c357acb42011-06-09 20:52:42112 info_map->AddExtension(extension1, base::Time(), false);
113 info_map->AddExtension(extension2, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53114
[email protected]be0a2cfd2011-06-02 21:36:42115 EXPECT_EQ(2u, info_map->extensions().size());
116 EXPECT_EQ(extension1.get(), info_map->extensions().GetByID(extension1->id()));
117 EXPECT_EQ(extension2.get(), info_map->extensions().GetByID(extension2->id()));
[email protected]4361c7c2010-09-30 21:57:53118}
119
120// Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
121TEST_F(ExtensionInfoMapTest, CheckPermissions) {
122 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
123
[email protected]66e4eb32010-10-27 20:37:41124 scoped_refptr<Extension> app(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42125 "valid_app.json"));
[email protected]66e4eb32010-10-27 20:37:41126 scoped_refptr<Extension> extension(LoadManifest("manifest_tests",
[email protected]be0a2cfd2011-06-02 21:36:42127 "tabs_extension.json"));
[email protected]4361c7c2010-09-30 21:57:53128
129 GURL app_url("http://www.google.com/mail/foo.html");
130 ASSERT_TRUE(app->is_app());
[email protected]cced75a2011-05-20 08:31:12131 ASSERT_TRUE(app->web_extent().MatchesURL(app_url));
[email protected]4361c7c2010-09-30 21:57:53132
[email protected]c357acb42011-06-09 20:52:42133 info_map->AddExtension(app, base::Time(), false);
134 info_map->AddExtension(extension, base::Time(), false);
[email protected]4361c7c2010-09-30 21:57:53135
136 // The app should have the notifications permission, either from a
137 // chrome-extension URL or from its web extent.
[email protected]be0a2cfd2011-06-02 21:36:42138 const Extension* match = info_map->extensions().GetByURL(
139 app->GetResourceURL("a.html"));
140 EXPECT_TRUE(match &&
141 match->HasApiPermission(Extension::kNotificationPermission));
142 match = info_map->extensions().GetByURL(app_url);
143 EXPECT_TRUE(match &&
144 match->HasApiPermission(Extension::kNotificationPermission));
145 EXPECT_FALSE(match &&
146 match->HasApiPermission(Extension::kTabPermission));
[email protected]4361c7c2010-09-30 21:57:53147
148 // The extension should have the tabs permission.
[email protected]be0a2cfd2011-06-02 21:36:42149 match = info_map->extensions().GetByURL(extension->GetResourceURL("a.html"));
150 EXPECT_TRUE(match &&
151 match->HasApiPermission(Extension::kTabPermission));
152 EXPECT_FALSE(match &&
153 match->HasApiPermission(Extension::kNotificationPermission));
[email protected]4361c7c2010-09-30 21:57:53154
155 // Random URL should not have any permissions.
[email protected]be0a2cfd2011-06-02 21:36:42156 match = info_map->extensions().GetByURL(GURL("http://evil.com/a.html"));
157 EXPECT_FALSE(match);
[email protected]4361c7c2010-09-30 21:57:53158}
[email protected]5eaba82a2010-10-01 20:55:53159
160} // namespace