1 // Copyright 2013 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.
5 #include "base/json/json_file_value_serializer.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/path_service.h"
8 #include "chrome/common/chrome_paths.h"
9 #include "content/public/test/test_browser_thread.h"
10 #include "extensions/browser/info_map.h"
11 #include "extensions/common/extension.h"
12 #include "extensions/common/manifest_constants.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 using content::BrowserThread
;
17 namespace keys
= extensions::manifest_keys
;
19 namespace extensions
{
21 class InfoMapTest
: public testing::Test
{
24 : ui_thread_(BrowserThread::UI
, &message_loop_
),
25 io_thread_(BrowserThread::IO
, &message_loop_
) {}
28 base::MessageLoop message_loop_
;
29 content::TestBrowserThread ui_thread_
;
30 content::TestBrowserThread io_thread_
;
33 // Returns a barebones test Extension object with the given name.
34 static scoped_refptr
<Extension
> CreateExtension(const std::string
& name
) {
36 base::FilePath
path(FILE_PATH_LITERAL("c:\\foo"));
37 #elif defined(OS_POSIX)
38 base::FilePath
path(FILE_PATH_LITERAL("/foo"));
41 base::DictionaryValue manifest
;
42 manifest
.SetString(keys::kVersion
, "1.0.0.0");
43 manifest
.SetString(keys::kName
, name
);
46 scoped_refptr
<Extension
> extension
=
47 Extension::Create(path
.AppendASCII(name
),
48 Manifest::INVALID_LOCATION
,
52 EXPECT_TRUE(extension
.get()) << error
;
57 static scoped_refptr
<Extension
> LoadManifest(const std::string
& dir
,
58 const std::string
& test_file
) {
60 PathService::Get(chrome::DIR_TEST_DATA
, &path
);
61 path
= path
.AppendASCII("extensions").AppendASCII(dir
).AppendASCII(test_file
);
63 JSONFileValueSerializer
serializer(path
);
64 scoped_ptr
<base::Value
> result(serializer
.Deserialize(NULL
, NULL
));
69 scoped_refptr
<Extension
> extension
=
70 Extension::Create(path
,
71 Manifest::INVALID_LOCATION
,
72 *static_cast<base::DictionaryValue
*>(result
.get()),
75 EXPECT_TRUE(extension
.get()) << error
;
80 // Test that the InfoMap handles refcounting properly.
81 TEST_F(InfoMapTest
, RefCounting
) {
82 scoped_refptr
<InfoMap
> info_map(new InfoMap());
84 // New extensions should have a single reference holding onto them.
85 scoped_refptr
<Extension
> extension1(CreateExtension("extension1"));
86 scoped_refptr
<Extension
> extension2(CreateExtension("extension2"));
87 scoped_refptr
<Extension
> extension3(CreateExtension("extension3"));
88 EXPECT_TRUE(extension1
->HasOneRef());
89 EXPECT_TRUE(extension2
->HasOneRef());
90 EXPECT_TRUE(extension3
->HasOneRef());
92 // Add a ref to each extension and give it to the info map.
93 info_map
->AddExtension(extension1
.get(), base::Time(), false, false);
94 info_map
->AddExtension(extension2
.get(), base::Time(), false, false);
95 info_map
->AddExtension(extension3
.get(), base::Time(), false, false);
97 // Release extension1, and the info map should have the only ref.
98 const Extension
* weak_extension1
= extension1
.get();
100 EXPECT_TRUE(weak_extension1
->HasOneRef());
102 // Remove extension2, and the extension2 object should have the only ref.
103 info_map
->RemoveExtension(
104 extension2
->id(), extensions::UnloadedExtensionInfo::REASON_UNINSTALL
);
105 EXPECT_TRUE(extension2
->HasOneRef());
107 // Delete the info map, and the extension3 object should have the only ref.
109 EXPECT_TRUE(extension3
->HasOneRef());
112 // Tests that we can query a few extension properties from the InfoMap.
113 TEST_F(InfoMapTest
, Properties
) {
114 scoped_refptr
<InfoMap
> info_map(new InfoMap());
116 scoped_refptr
<Extension
> extension1(CreateExtension("extension1"));
117 scoped_refptr
<Extension
> extension2(CreateExtension("extension2"));
119 info_map
->AddExtension(extension1
.get(), base::Time(), false, false);
120 info_map
->AddExtension(extension2
.get(), base::Time(), false, false);
122 EXPECT_EQ(2u, info_map
->extensions().size());
123 EXPECT_EQ(extension1
.get(), info_map
->extensions().GetByID(extension1
->id()));
124 EXPECT_EQ(extension2
.get(), info_map
->extensions().GetByID(extension2
->id()));
127 // Tests CheckURLAccessToExtensionPermission given both extension and app URLs.
128 TEST_F(InfoMapTest
, CheckPermissions
) {
129 scoped_refptr
<InfoMap
> info_map(new InfoMap());
131 scoped_refptr
<Extension
> app(
132 LoadManifest("manifest_tests", "valid_app.json"));
133 scoped_refptr
<Extension
> extension(
134 LoadManifest("manifest_tests", "tabs_extension.json"));
136 GURL
app_url("http://www.google.com/mail/foo.html");
137 ASSERT_TRUE(app
->is_app());
138 ASSERT_TRUE(app
->web_extent().MatchesURL(app_url
));
140 info_map
->AddExtension(app
.get(), base::Time(), false, false);
141 info_map
->AddExtension(extension
.get(), base::Time(), false, false);
143 // The app should have the notifications permission, either from a
144 // chrome-extension URL or from its web extent.
145 const Extension
* match
= info_map
->extensions().GetExtensionOrAppByURL(
146 app
->GetResourceURL("a.html"));
147 EXPECT_TRUE(match
&& match
->HasAPIPermission(APIPermission::kNotification
));
148 match
= info_map
->extensions().GetExtensionOrAppByURL(app_url
);
149 EXPECT_TRUE(match
&& match
->HasAPIPermission(APIPermission::kNotification
));
150 EXPECT_FALSE(match
&& match
->HasAPIPermission(APIPermission::kTab
));
152 // The extension should have the tabs permission.
153 match
= info_map
->extensions().GetExtensionOrAppByURL(
154 extension
->GetResourceURL("a.html"));
155 EXPECT_TRUE(match
&& match
->HasAPIPermission(APIPermission::kTab
));
156 EXPECT_FALSE(match
&& match
->HasAPIPermission(APIPermission::kNotification
));
158 // Random URL should not have any permissions.
159 GURL
evil_url("http://evil.com/a.html");
160 match
= info_map
->extensions().GetExtensionOrAppByURL(evil_url
);
164 TEST_F(InfoMapTest
, TestNotificationsDisabled
) {
165 scoped_refptr
<InfoMap
> info_map(new InfoMap());
166 scoped_refptr
<Extension
> app(LoadManifest("manifest_tests",
168 info_map
->AddExtension(app
.get(), base::Time(), false, false);
170 EXPECT_FALSE(info_map
->AreNotificationsDisabled(app
->id()));
171 info_map
->SetNotificationsDisabled(app
->id(), true);
172 EXPECT_TRUE(info_map
->AreNotificationsDisabled(app
->id()));
173 info_map
->SetNotificationsDisabled(app
->id(), false);
176 } // namespace extensions