blob: ba5a0b2c188c9d99b58b25578e13f934f7de5968 [file] [log] [blame]
[email protected]4361c7c2010-09-30 21:57:531// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2// 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]4361c7c2010-09-30 21:57:5310#include "chrome/common/json_value_serializer.h"
[email protected]1bda97552011-03-01 20:11:5211#include "content/browser/browser_thread.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(
45 path.AppendASCII(name), Extension::INVALID, manifest, false, &error);
46 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5347
[email protected]66e4eb32010-10-27 20:37:4148 return extension;
[email protected]4361c7c2010-09-30 21:57:5349}
50
[email protected]66e4eb32010-10-27 20:37:4151static scoped_refptr<Extension> LoadManifest(const std::string& dir,
52 const std::string& test_file) {
[email protected]4361c7c2010-09-30 21:57:5353 FilePath path;
54 PathService::Get(chrome::DIR_TEST_DATA, &path);
55 path = path.AppendASCII("extensions")
56 .AppendASCII(dir)
57 .AppendASCII(test_file);
58
59 JSONFileValueSerializer serializer(path);
60 scoped_ptr<Value> result(serializer.Deserialize(NULL, NULL));
61 if (!result.get())
62 return NULL;
63
64 std::string error;
[email protected]66e4eb32010-10-27 20:37:4165 scoped_refptr<Extension> extension = Extension::Create(
66 path, Extension::INVALID, *static_cast<DictionaryValue*>(result.get()),
67 false, &error);
68 EXPECT_TRUE(extension) << error;
[email protected]4361c7c2010-09-30 21:57:5369
[email protected]66e4eb32010-10-27 20:37:4170 return extension;
[email protected]4361c7c2010-09-30 21:57:5371}
72
[email protected]4361c7c2010-09-30 21:57:5373// Test that the ExtensionInfoMap handles refcounting properly.
74TEST_F(ExtensionInfoMapTest, RefCounting) {
75 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
76
[email protected]6f229e82010-11-02 17:47:2677 // New extensions should have a single reference holding onto them.
[email protected]66e4eb32010-10-27 20:37:4178 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
79 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
80 scoped_refptr<Extension> extension3(CreateExtension("extension3"));
[email protected]6f229e82010-11-02 17:47:2681 EXPECT_TRUE(extension1->HasOneRef());
82 EXPECT_TRUE(extension2->HasOneRef());
83 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5384
85 // Add a ref to each extension and give it to the info map. The info map
86 // expects the caller to add a ref for it, but then assumes ownership of that
87 // reference.
[email protected]6f229e82010-11-02 17:47:2688 extension1->AddRef();
89 info_map->AddExtension(extension1);
90 extension2->AddRef();
91 info_map->AddExtension(extension2);
92 extension3->AddRef();
93 info_map->AddExtension(extension3);
[email protected]4361c7c2010-09-30 21:57:5394
[email protected]6f229e82010-11-02 17:47:2695 // Release extension1, and the info map should have the only ref.
96 const Extension* weak_extension1 = extension1;
[email protected]66e4eb32010-10-27 20:37:4197 extension1 = NULL;
[email protected]6f229e82010-11-02 17:47:2698 EXPECT_TRUE(weak_extension1->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:5399
100 // Remove extension2, and the extension2 object should have the only ref.
101 info_map->RemoveExtension(extension2->id());
[email protected]6f229e82010-11-02 17:47:26102 EXPECT_TRUE(extension2->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53103
104 // Delete the info map, and the extension3 object should have the only ref.
105 info_map = NULL;
[email protected]6f229e82010-11-02 17:47:26106 EXPECT_TRUE(extension3->HasOneRef());
[email protected]4361c7c2010-09-30 21:57:53107}
108
109// Tests that we can query a few extension properties from the ExtensionInfoMap.
110TEST_F(ExtensionInfoMapTest, Properties) {
111 scoped_refptr<ExtensionInfoMap> info_map(new ExtensionInfoMap());
112
[email protected]66e4eb32010-10-27 20:37:41113 scoped_refptr<Extension> extension1(CreateExtension("extension1"));
114 scoped_refptr<Extension> extension2(CreateExtension("extension2"));
[email protected]4361c7c2010-09-30 21:57:53115
[email protected]6f229e82010-11-02 17:47:26116 extension1->AddRef();
117 info_map->AddExtension(extension1);
118 extension2->AddRef();
119 info_map->AddExtension(extension2);
[email protected]4361c7c2010-09-30 21:57:53120
121 EXPECT_EQ(extension1->name(),
122 info_map->GetNameForExtension(extension1->id()));
123 EXPECT_EQ(extension2->name(),
124 info_map->GetNameForExtension(extension2->id()));
125
126 EXPECT_EQ(extension1->path().value(),
127 info_map->GetPathForExtension(extension1->id()).value());
128 EXPECT_EQ(extension2->path().value(),
129 info_map->GetPathForExtension(extension2->id()).value());
130}
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]4361c7c2010-09-30 21:57:53137 "valid_app.json"));
[email protected]66e4eb32010-10-27 20:37:41138 scoped_refptr<Extension> extension(LoadManifest("manifest_tests",
[email protected]4361c7c2010-09-30 21:57:53139 "tabs_extension.json"));
140
141 GURL app_url("http://www.google.com/mail/foo.html");
142 ASSERT_TRUE(app->is_app());
143 ASSERT_TRUE(app->web_extent().ContainsURL(app_url));
144
[email protected]6f229e82010-11-02 17:47:26145 app->AddRef();
146 info_map->AddExtension(app);
147 extension->AddRef();
148 info_map->AddExtension(extension);
[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.
152 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission(
153 app->GetResourceURL("a.html"), Extension::kNotificationPermission));
154 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission(
155 app_url, Extension::kNotificationPermission));
156 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission(
157 app_url, Extension::kTabPermission));
158
159 // The extension should have the tabs permission.
160 EXPECT_TRUE(info_map->CheckURLAccessToExtensionPermission(
161 extension->GetResourceURL("a.html"), Extension::kTabPermission));
162 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission(
163 extension->GetResourceURL("a.html"), Extension::kNotificationPermission));
164
165 // Random URL should not have any permissions.
166 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission(
167 GURL("http://evil.com/a.html"), Extension::kNotificationPermission));
168 EXPECT_FALSE(info_map->CheckURLAccessToExtensionPermission(
169 GURL("http://evil.com/a.html"), Extension::kTabPermission));
170}
[email protected]5eaba82a2010-10-01 20:55:53171
172} // namespace