Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(298)

Side by Side Diff: chrome/common/extensions/extension_permission_set_unittest.cc

Issue 7003098: Start refractoring extension permissions into ExtensionPermissionSet. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup and update ExtensionPermissionSet data model. Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/common/extensions/extension_permission_set.h"
6
7 #include "base/logging.h"
8 #include "base/path_service.h"
9 #include "base/utf_string_conversions.h"
10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/extensions/extension.h"
12 #include "content/common/json_value_serializer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 static scoped_refptr<Extension> LoadManifest(const std::string& dir,
18 const std::string& test_file,
19 int extra_flags) {
20 FilePath path;
21 PathService::Get(chrome::DIR_TEST_DATA, &path);
22 path = path.AppendASCII("extensions")
23 .AppendASCII(dir)
24 .AppendASCII(test_file);
25
26 JSONFileValueSerializer serializer(path);
27 std::string error;
28 scoped_ptr<Value> result(serializer.Deserialize(NULL, &error));
29 if (!result.get()) {
30 EXPECT_EQ("", error);
31 return NULL;
32 }
33
34 scoped_refptr<Extension> extension = Extension::Create(
35 path.DirName(), Extension::INVALID,
36 *static_cast<DictionaryValue*>(result.get()),
37 Extension::STRICT_ERROR_CHECKS | extra_flags, &error);
38 EXPECT_TRUE(extension) << error;
39 return extension;
40 }
41
42 static scoped_refptr<Extension> LoadManifest(const std::string& dir,
43 const std::string& test_file) {
44 return LoadManifest(dir, test_file, Extension::NO_FLAGS);
45 }
46
47 void CompareLists(const std::vector<std::string>& expected,
48 const std::vector<std::string>& actual) {
49 ASSERT_EQ(expected.size(), actual.size());
50
51 for (size_t i = 0; i < expected.size(); ++i) {
52 EXPECT_EQ(expected[i], actual[i]);
53 }
54 }
55
56 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
57 int schemes = URLPattern::SCHEME_ALL;
58 extent->AddPattern(URLPattern(schemes, pattern));
59 }
60
61 static void AssertEqualExtents(const URLPatternSet& extent1,
62 const URLPatternSet& extent2) {
63 URLPatternList patterns1 = extent1.patterns();
64 URLPatternList patterns2 = extent2.patterns();
65 std::set<std::string> strings1;
66 EXPECT_EQ(patterns1.size(), patterns2.size());
67
68 for (size_t i = 0; i < patterns1.size(); ++i)
69 strings1.insert(patterns1.at(i).GetAsString());
70
71 std::set<std::string> strings2;
72 for (size_t i = 0; i < patterns2.size(); ++i)
73 strings2.insert(patterns2.at(i).GetAsString());
74
75 EXPECT_EQ(strings1, strings2);
76 }
77
78 } // namespace
79
80 class ExtensionAPIPermissionTest : public testing::Test {
81 };
82
83 class ExtensionPermissionSetTest : public testing::Test {
84 };
85
86
87 // Tests GetById.
88 TEST(ExtensionPermissionsInfoTest, GetById) {
89 EXPECT_EQ(ExtensionAPIPermission::kTab,
Matt Perry 2011/06/21 00:43:10 could also loop through all enum values for a thor
jstritar 2011/06/21 23:12:16 Done.
90 ExtensionPermissionsInfo::GetById(
91 ExtensionAPIPermission::kTab)->id());
92 EXPECT_EQ(ExtensionAPIPermission::kDefault,
93 ExtensionPermissionsInfo::GetById(
94 ExtensionAPIPermission::kDefault)->id());
95 EXPECT_EQ(ExtensionAPIPermission::kPlugin,
96 ExtensionPermissionsInfo::GetById(
97 ExtensionAPIPermission::kPlugin)->id());
98 EXPECT_EQ(ExtensionAPIPermission::kIdle,
99 ExtensionPermissionsInfo::GetById(
100 ExtensionAPIPermission::kIdle)->id());
101 }
102
103 // Tests that GetByName works with normal permission names and aliases.
104 TEST(ExtensionPermissionsInfoTest, GetByName) {
105 EXPECT_EQ(ExtensionAPIPermission::kTab,
106 ExtensionPermissionsInfo::GetByName("tabs")->id());
107 EXPECT_EQ(ExtensionAPIPermission::kManagement,
108 ExtensionPermissionsInfo::GetByName("management")->id());
109 EXPECT_FALSE(ExtensionPermissionsInfo::GetByName("alsdkfjasldkfj"));
110 }
111
112 TEST(ExtensionPermissionsInfoTest, GetAll) {
113 size_t count = 0;
114 ExtensionAPIPermissionSet apis = ExtensionPermissionsInfo::GetAll();
115 for (ExtensionAPIPermissionSet::iterator api = apis.begin();
116 api != apis.end(); ++api) {
117 // Make sure only the valid permission IDs get returned.
118 EXPECT_NE(ExtensionAPIPermission::kInvalid, *api);
119 EXPECT_NE(ExtensionAPIPermission::kUnknown, *api);
120 count++;
121 }
122 EXPECT_EQ(count, ExtensionPermissionsInfo::GetPermissionCount());
123 }
124
125 TEST(ExtensionPermissionInfoTest, GetAllByName) {
126 std::set<std::string> names;
127 names.insert("background");
128 names.insert("management");
129
130 // This is an alias of kTab
131 names.insert("windows");
132
133 // This unknown name should get dropped.
134 names.insert("sdlkfjasdlkfj");
135
136 ExtensionAPIPermissionSet expected;
137 expected.insert(ExtensionAPIPermission::kBackground);
138 expected.insert(ExtensionAPIPermission::kManagement);
139 expected.insert(ExtensionAPIPermission::kTab);
140
141 EXPECT_EQ(expected, ExtensionPermissionsInfo::GetAllByName(names));
142 }
143
144 // Tests that the aliases are properly mapped.
145 TEST(ExtensionAPIPermissionTest, Aliases) {
146 // tabs: tabs, windows
147 std::string tabs_name = "tabs";
148 EXPECT_EQ(tabs_name, ExtensionPermissionsInfo::GetById(
149 ExtensionAPIPermission::kTab)->name());
150 EXPECT_EQ(ExtensionAPIPermission::kTab,
151 ExtensionPermissionsInfo::GetByName("tabs")->id());
152 EXPECT_EQ(ExtensionAPIPermission::kTab,
153 ExtensionPermissionsInfo::GetByName("windows")->id());
154
155 // unlimitedStorage: unlimitedStorage, unlimited_storage
156 std::string storage_name = "unlimitedStorage";
157 EXPECT_EQ(storage_name, ExtensionPermissionsInfo::GetById(
158 ExtensionAPIPermission::kUnlimitedStorage)->name());
159 EXPECT_EQ(ExtensionAPIPermission::kUnlimitedStorage,
160 ExtensionPermissionsInfo::GetByName("unlimitedStorage")->id());
161 EXPECT_EQ(ExtensionAPIPermission::kUnlimitedStorage,
162 ExtensionPermissionsInfo::GetByName("unlimited_storage")->id());
163 }
164
165 TEST(ExtensionAPIPermissionTest, HostedAppPermissions) {
166 ExtensionAPIPermissionSet hosted_perms;
167 hosted_perms.insert(ExtensionAPIPermission::kBackground);
168 hosted_perms.insert(ExtensionAPIPermission::kClipboardRead);
169 hosted_perms.insert(ExtensionAPIPermission::kClipboardWrite);
170 hosted_perms.insert(ExtensionAPIPermission::kChromePrivate);
171 hosted_perms.insert(ExtensionAPIPermission::kExperimental);
172 hosted_perms.insert(ExtensionAPIPermission::kGeolocation);
173 hosted_perms.insert(ExtensionAPIPermission::kNotification);
174 hosted_perms.insert(ExtensionAPIPermission::kUnlimitedStorage);
175 hosted_perms.insert(ExtensionAPIPermission::kWebstorePrivate);
176
177 ExtensionAPIPermissionSet perms = ExtensionPermissionsInfo::GetAll();
178 size_t count = 0;
179 for (ExtensionAPIPermissionSet::iterator i = perms.begin();
180 i != perms.end(); ++i) {
181 count += hosted_perms.count(*i);
182 EXPECT_EQ(hosted_perms.count(*i),
183 ExtensionPermissionsInfo::GetById(*i)->is_hosted_app());
184 }
185
186 EXPECT_EQ((size_t)9, count);
Matt Perry 2011/06/21 00:43:10 use 9u
jstritar 2011/06/21 23:12:16 Done.
187 EXPECT_EQ((size_t)9, ExtensionPermissionsInfo::GetHostedAppPermissionCount());
188 }
189
190 TEST(ExtensionAPIPermissionTest, ComponentOnlyPermissions) {
191 ExtensionAPIPermissionSet private_perms;
192 private_perms.insert(ExtensionAPIPermission::kChromeosInfoPrivate);
193 private_perms.insert(ExtensionAPIPermission::kFileBrowserPrivate);
194 private_perms.insert(ExtensionAPIPermission::kMediaPlayerPrivate);
195 private_perms.insert(ExtensionAPIPermission::kWebstorePrivate);
196
197 ExtensionAPIPermissionSet perms = ExtensionPermissionsInfo::GetAll();
198 int count = 0;
199 for (ExtensionAPIPermissionSet::iterator i = perms.begin();
200 i != perms.end(); ++i) {
201 count += private_perms.count(*i);
202 EXPECT_EQ(private_perms.count(*i),
203 ExtensionPermissionsInfo::GetById(*i)->is_component_only());
204 }
205
206 EXPECT_EQ(4, count);
207 }
208
209 TEST(ExtensionPermissionSetTest, EffectiveHostPermissions) {
210 scoped_refptr<Extension> extension;
211 const ExtensionPermissionSet* permissions = NULL;
212
213 extension = LoadManifest("effective_host_permissions", "empty.json");
214 permissions = extension->permission_set();
215 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size());
216 EXPECT_FALSE(permissions->HasEffectiveAccessToHost(
217 GURL("http://www.google.com")));
218 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
219
220 extension = LoadManifest("effective_host_permissions", "one_host.json");
221 permissions = extension->permission_set();
222 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
223 GURL("http://www.google.com")));
224 EXPECT_FALSE(permissions->HasEffectiveAccessToHost(
225 GURL("https://www.google.com")));
226 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
227
228 extension = LoadManifest("effective_host_permissions",
229 "one_host_wildcard.json");
230 permissions = extension->permission_set();
231 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("http://google.com")));
232 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
233 GURL("http://foo.google.com")));
234 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
235
236 extension = LoadManifest("effective_host_permissions", "two_hosts.json");
237 permissions = extension->permission_set();
238 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
239 GURL("http://www.google.com")));
240 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
241 GURL("http://www.reddit.com")));
242 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
243
244 extension = LoadManifest("effective_host_permissions",
245 "https_not_considered.json");
246 permissions = extension->permission_set();
247 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("http://google.com")));
248 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("https://google.com"))) ;
249 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
250
251 extension = LoadManifest("effective_host_permissions",
252 "two_content_scripts.json");
253 permissions = extension->permission_set();
254 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("http://google.com")));
255 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
256 GURL("http://www.reddit.com")));
257 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(
258 GURL("http://news.ycombinator.com")));
259 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
260
261 extension = LoadManifest("effective_host_permissions", "all_hosts.json");
262 permissions = extension->permission_set();
263 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("http://test/")));
264 EXPECT_FALSE(permissions->HasEffectiveAccessToHost(GURL("https://test/")));
265 EXPECT_TRUE(
266 permissions->HasEffectiveAccessToHost(GURL("http://www.google.com")));
267 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
268
269 extension = LoadManifest("effective_host_permissions", "all_hosts2.json");
270 permissions = extension->permission_set();
271 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("http://test/")));
272 EXPECT_TRUE(
273 permissions->HasEffectiveAccessToHost(GURL("http://www.google.com")));
274 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
275
276 extension = LoadManifest("effective_host_permissions", "all_hosts3.json");
277 permissions = extension->permission_set();
278 EXPECT_FALSE(permissions->HasEffectiveAccessToHost(GURL("http://test/")));
279 EXPECT_TRUE(permissions->HasEffectiveAccessToHost(GURL("https://test/")));
280 EXPECT_TRUE(
281 permissions->HasEffectiveAccessToHost(GURL("http://www.google.com")));
282 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
283 }
284
285 TEST(ExtensionPermissionSetTest, CreateUnion) {
286 ExtensionAPIPermissionSet apis1;
287 ExtensionAPIPermissionSet apis2;
288 ExtensionAPIPermissionSet expected_apis;
289
290 URLPatternSet explicit_hosts1;
291 URLPatternSet explicit_hosts2;
292 URLPatternSet expected_explicit_hosts;
293
294 URLPatternSet scriptable_hosts1;
295 URLPatternSet scriptable_hosts2;
296 URLPatternSet expected_scriptable_hosts;
297
298 URLPatternSet effective_hosts;
299
300 scoped_ptr<ExtensionPermissionSet> set1;
301 scoped_ptr<ExtensionPermissionSet> set2;
302 scoped_ptr<ExtensionPermissionSet> union_set;
303
304 // Union with an empty set.
305 apis1.insert(ExtensionAPIPermission::kTab);
306 apis1.insert(ExtensionAPIPermission::kBackground);
307 expected_apis.insert(ExtensionAPIPermission::kTab);
308 expected_apis.insert(ExtensionAPIPermission::kBackground);
309
310 AddPattern(&explicit_hosts1, "http://*.google.com/*");
311 AddPattern(&expected_explicit_hosts, "http://*.google.com/*");
312 AddPattern(&effective_hosts, "http://*.google.com/*");
313
314 set1.reset(new ExtensionPermissionSet(
315 apis1, explicit_hosts1, scriptable_hosts1));
316 set2.reset(new ExtensionPermissionSet(
317 apis2, explicit_hosts2, scriptable_hosts2));
318 union_set.reset(ExtensionPermissionSet::CreateUnion(set1.get(), set2.get()));
319
320 EXPECT_FALSE(union_set->HasEffectiveFullAccess());
321 EXPECT_EQ(expected_apis, union_set->apis());
322 AssertEqualExtents(expected_explicit_hosts, union_set->explicit_hosts());
323 AssertEqualExtents(expected_scriptable_hosts, union_set->scriptable_hosts());
324 AssertEqualExtents(expected_explicit_hosts, union_set->effective_hosts());
325
326 // Now use a real second set.
327 apis2.insert(ExtensionAPIPermission::kTab);
328 apis2.insert(ExtensionAPIPermission::kProxy);
329 apis2.insert(ExtensionAPIPermission::kClipboardWrite);
330 apis2.insert(ExtensionAPIPermission::kPlugin);
331 expected_apis.insert(ExtensionAPIPermission::kTab);
332 expected_apis.insert(ExtensionAPIPermission::kProxy);
333 expected_apis.insert(ExtensionAPIPermission::kClipboardWrite);
334 expected_apis.insert(ExtensionAPIPermission::kPlugin);
335
336 AddPattern(&explicit_hosts2, "http://*.example.com/*");
337 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
338 AddPattern(&expected_explicit_hosts, "http://*.example.com/*");
339 AddPattern(&expected_scriptable_hosts, "http://*.google.com/*");
340
341 effective_hosts.ClearPatterns();
342 AddPattern(&effective_hosts, "<all_urls>");
343
344 set2.reset(new ExtensionPermissionSet(
345 apis2, explicit_hosts2, scriptable_hosts2));
346 union_set.reset(ExtensionPermissionSet::CreateUnion(set1.get(), set2.get()));
347 EXPECT_TRUE(union_set->HasEffectiveFullAccess());
348 EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts());
349 EXPECT_EQ(expected_apis, union_set->apis());
350 AssertEqualExtents(expected_explicit_hosts, union_set->explicit_hosts());
351 AssertEqualExtents(expected_scriptable_hosts, union_set->scriptable_hosts());
352 AssertEqualExtents(effective_hosts, union_set->effective_hosts());
353 }
354
355 TEST(ExtensionPermissionSetTest, HasLessPrivilegesThan) {
356 const struct {
357 const char* base_name;
358 // Increase these sizes if you have more than 10.
359 const char* granted_apis[10];
360 const char* granted_hosts[10];
361 bool full_access;
362 bool expect_increase;
363 } kTests[] = {
364 { "allhosts1", {NULL}, {"http://*/", NULL}, false,
365 false }, // all -> all
366 { "allhosts2", {NULL}, {"http://*/", NULL}, false,
367 false }, // all -> one
368 { "allhosts3", {NULL}, {NULL}, false, true }, // one -> all
369 { "hosts1", {NULL},
370 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false,
371 false }, // http://a,http://b -> http://a,http://b
372 { "hosts2", {NULL},
373 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false,
374 true }, // http://a,http://b -> https://a,http://*.b
375 { "hosts3", {NULL},
376 {"http://www.google.com/", "http://www.reddit.com/", NULL}, false,
377 false }, // http://a,http://b -> http://a
378 { "hosts4", {NULL},
379 {"http://www.google.com/", NULL}, false,
380 true }, // http://a -> http://a,http://b
381 { "hosts5", {"tabs", "notifications", NULL},
382 {"http://*.example.com/", "http://*.example.com/*",
383 "http://*.example.co.uk/*", "http://*.example.com.au/*",
384 NULL}, false,
385 false }, // http://a,b,c -> http://a,b,c + https://a,b,c
386 { "hosts6", {"tabs", "notifications", NULL},
387 {"http://*.example.com/", "http://*.example.com/*", NULL}, false,
388 false }, // http://a.com -> http://a.com + http://a.co.uk
389 { "permissions1", {"tabs", NULL},
390 {NULL}, false, false }, // tabs -> tabs
391 { "permissions2", {"tabs", NULL},
392 {NULL}, false, true }, // tabs -> tabs,bookmarks
393 { "permissions3", {NULL},
394 {"http://*/*", NULL},
395 false, true }, // http://a -> http://a,tabs
396 { "permissions5", {"bookmarks", NULL},
397 {NULL}, false, true }, // bookmarks -> bookmarks,history
398 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS
399 { "permissions4", {NULL},
400 {NULL}, true, false }, // plugin -> plugin,tabs
401 { "plugin1", {NULL},
402 {NULL}, true, false }, // plugin -> plugin
403 { "plugin2", {NULL},
404 {NULL}, true, false }, // plugin -> none
405 { "plugin3", {NULL},
406 {NULL}, false, true }, // none -> plugin
407 #endif
408 { "storage", {NULL},
409 {NULL}, false, false }, // none -> storage
410 { "notifications", {NULL},
411 {NULL}, false, false } // none -> notifications
412 };
413
414 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
415 scoped_refptr<Extension> old_extension(
416 LoadManifest("allow_silent_upgrade",
417 std::string(kTests[i].base_name) + "_old.json"));
418 scoped_refptr<Extension> new_extension(
419 LoadManifest("allow_silent_upgrade",
420 std::string(kTests[i].base_name) + "_new.json"));
421
422 ExtensionAPIPermissionSet granted_apis;
423 for (size_t j = 0; kTests[i].granted_apis[j] != NULL; ++j) {
424 granted_apis.insert(
425 ExtensionPermissionsInfo::GetByName(kTests[i].granted_apis[j])->id());
426 }
427
428 URLPatternSet granted_hosts;
429 for (size_t j = 0; kTests[i].granted_hosts[j] != NULL; ++j)
430 AddPattern(&granted_hosts, kTests[i].granted_hosts[j]);
431
432 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json";
433 if (!new_extension.get())
434 continue;
435
436 const ExtensionPermissionSet* old_p = old_extension->permission_set();
437 const ExtensionPermissionSet* new_p = new_extension->permission_set();
438
439 EXPECT_EQ(kTests[i].expect_increase, old_p->HasLessPrivilegesThan(new_p))
440 << kTests[i].base_name;
441 }
442 }
443
444 TEST(ExtensionPermissionSetTest, PermissionMessages) {
445 // Ensure that all permissions that needs to show install UI actually have
446 // strings associated with them.
447 ExtensionAPIPermissionSet skip;
448
449 skip.insert(ExtensionAPIPermission::kDefault);
450
451 // These are considered "nuisance" or "trivial" permissions that don't need
452 // a prompt.
453 skip.insert(ExtensionAPIPermission::kContextMenus);
454 skip.insert(ExtensionAPIPermission::kIdle);
455 skip.insert(ExtensionAPIPermission::kNotification);
456 skip.insert(ExtensionAPIPermission::kUnlimitedStorage);
457 skip.insert(ExtensionAPIPermission::kContentSettings);
458
459 // TODO(erikkay) add a string for this permission.
460 skip.insert(ExtensionAPIPermission::kBackground);
461
462 skip.insert(ExtensionAPIPermission::kClipboardWrite);
463
464 // The cookie permission does nothing unless you have associated host
465 // permissions.
466 skip.insert(ExtensionAPIPermission::kCookie);
467
468 // The proxy permission is warned as part of host permission checks.
469 skip.insert(ExtensionAPIPermission::kProxy);
470
471 // This permission requires explicit user action (context menu handler)
472 // so we won't prompt for it for now.
473 skip.insert(ExtensionAPIPermission::kFileBrowserHandler);
474
475 // If you've turned on the experimental command-line flag, we don't need
476 // to warn you further.
477 skip.insert(ExtensionAPIPermission::kExperimental);
478
479 // These are private.
480 skip.insert(ExtensionAPIPermission::kWebstorePrivate);
481 skip.insert(ExtensionAPIPermission::kFileBrowserPrivate);
482 skip.insert(ExtensionAPIPermission::kMediaPlayerPrivate);
483 skip.insert(ExtensionAPIPermission::kChromePrivate);
484 skip.insert(ExtensionAPIPermission::kChromeosInfoPrivate);
485 skip.insert(ExtensionAPIPermission::kWebSocketProxyPrivate);
486
487 // Warned as part of host permissions.
488 skip.insert(ExtensionAPIPermission::kDevtools);
489
490 ExtensionAPIPermissionSet permissions = ExtensionPermissionsInfo::GetAll();
491 for (ExtensionAPIPermissionSet::const_iterator i = permissions.begin();
492 i != permissions.end(); ++i) {
493 ExtensionAPIPermission* permission = ExtensionPermissionsInfo::GetById(*i);
494 EXPECT_TRUE(permission);
495 if (skip.count(*i)) {
496 EXPECT_EQ(ExtensionPermissionMessage::kNone, permission->message_id())
497 << "unexpected message_id for " << permission->name();
498 } else {
499 EXPECT_NE(ExtensionPermissionMessage::kNone, permission->message_id())
500 << "missing message_id for " << permission->name();
501 }
502 }
503 }
504
505 // Tests the default permissions (empty API permission set).
506 TEST(ExtensionPermissionSetTest, DefaultFunctionAccess) {
507 const struct {
508 const char* permission_name;
509 bool expect_success;
510 } kTests[] = {
511 // Negative test.
512 { "non_existing_permission", false },
513 // Test default module/package permission.
514 { "browserAction", true },
515 { "browserActions", true },
516 { "devtools", true },
517 { "extension", true },
518 { "i18n", true },
519 { "pageAction", true },
520 { "pageActions", true },
521 { "test", true },
522 // Some negative tests.
523 { "bookmarks", false },
524 { "cookies", false },
525 { "history", false },
526 { "tabs.onUpdated", false },
527 // Make sure we find the module name after stripping '.' and '/'.
528 { "browserAction/abcd/onClick", true },
529 { "browserAction.abcd.onClick", true },
530 // Test Tabs functions.
531 { "tabs.create", true},
532 { "tabs.update", true},
533 { "tabs.getSelected", false},
534 };
535
536 ExtensionPermissionSet permissions;
537 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
538 EXPECT_EQ(kTests[i].expect_success,
539 permissions.HasAccessToFunction(kTests[i].permission_name));
540 }
541 }
542
543 TEST(ExtensionPermissionSetTest, GetWarningMessages_ManyHosts) {
544 scoped_refptr<Extension> extension;
545
546 extension = LoadManifest("permissions", "many-hosts.json");
547 std::vector<string16> warnings =
548 extension->permission_set()->GetWarningMessages();
549 ASSERT_EQ(1u, warnings.size());
550 EXPECT_EQ("Your data on www.google.com and encrypted.google.com",
551 UTF16ToUTF8(warnings[0]));
552 }
553
554 TEST(ExtensionPermissionSetTest, GetWarningMessages_Plugins) {
555 scoped_refptr<Extension> extension;
556 scoped_ptr<ExtensionPermissionSet> permissions;
557
558 extension = LoadManifest("permissions", "plugins.json");
559 std::vector<string16> warnings =
560 extension->permission_set()->GetWarningMessages();
561 // We don't parse the plugins key on Chrome OS, so it should not ask for any
562 // permissions.
563 #if defined(OS_CHROMEOS)
564 ASSERT_EQ(0u, warnings.size());
565 #else
566 ASSERT_EQ(1u, warnings.size());
567 EXPECT_EQ("All data on your computer and the websites you visit",
568 UTF16ToUTF8(warnings[0]));
569 #endif
570 }
571
572 TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay) {
573 scoped_ptr<ExtensionPermissionSet> perm_set;
574 ExtensionAPIPermissionSet empty_perms;
575 std::vector<std::string> expected;
576 expected.push_back("www.foo.com");
577 expected.push_back("www.bar.com");
578 expected.push_back("www.baz.com");
579 URLPatternSet explicit_hosts;
580 URLPatternSet scriptable_hosts;
581
582 {
583 SCOPED_TRACE("no dupes");
584
585 // Simple list with no dupes.
586 explicit_hosts.AddPattern(
587 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
588 explicit_hosts.AddPattern(
589 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path"));
590 explicit_hosts.AddPattern(
591 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
592 perm_set.reset(new ExtensionPermissionSet(
593 empty_perms, explicit_hosts, scriptable_hosts));
594 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
595 }
596
597 {
598 SCOPED_TRACE("two dupes");
599
600 // Add some dupes.
601 explicit_hosts.AddPattern(
602 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
603 explicit_hosts.AddPattern(
604 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
605 perm_set.reset(new ExtensionPermissionSet(
606 empty_perms, explicit_hosts, scriptable_hosts));
607 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
608 }
609
610 {
611 SCOPED_TRACE("schemes differ");
612
613 // Add a pattern that differs only by scheme. This should be filtered out.
614 explicit_hosts.AddPattern(
615 URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path"));
616 perm_set.reset(new ExtensionPermissionSet(
617 empty_perms, explicit_hosts, scriptable_hosts));
618 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
619 }
620
621 {
622 SCOPED_TRACE("paths differ");
623
624 // Add some dupes by path.
625 explicit_hosts.AddPattern(
626 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath"));
627 perm_set.reset(new ExtensionPermissionSet(
628 empty_perms, explicit_hosts, scriptable_hosts));
629 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
630 }
631
632 {
633 SCOPED_TRACE("subdomains differ");
634
635 // We don't do anything special for subdomains.
636 explicit_hosts.AddPattern(
637 URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path"));
638 explicit_hosts.AddPattern(
639 URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path"));
640
641 expected.push_back("monkey.www.bar.com");
642 expected.push_back("bar.com");
643
644 perm_set.reset(new ExtensionPermissionSet(
645 empty_perms, explicit_hosts, scriptable_hosts));
646 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
647 }
648
649 {
650 SCOPED_TRACE("RCDs differ");
651
652 // Now test for RCD uniquing.
653 explicit_hosts.AddPattern(
654 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
655 explicit_hosts.AddPattern(
656 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
657 explicit_hosts.AddPattern(
658 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.de/path"));
659 explicit_hosts.AddPattern(
660 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca.us/path"));
661 explicit_hosts.AddPattern(
662 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
663 explicit_hosts.AddPattern(
664 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com.my/path"));
665
666 // This is an unknown RCD, which shouldn't be uniqued out.
667 explicit_hosts.AddPattern(
668 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
669 // But it should only occur once.
670 explicit_hosts.AddPattern(
671 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
672
673 expected.push_back("www.foo.xyzzy");
674
675 perm_set.reset(new ExtensionPermissionSet(
676 empty_perms, explicit_hosts, scriptable_hosts));
677 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
678 }
679
680 {
681 SCOPED_TRACE("wildcards");
682
683 explicit_hosts.AddPattern(
684 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
685
686 expected.push_back("*.google.com");
687
688 perm_set.reset(new ExtensionPermissionSet(
689 empty_perms, explicit_hosts, scriptable_hosts));
690 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
691 }
692
693 {
694 SCOPED_TRACE("scriptable hosts");
695 explicit_hosts.ClearPatterns();
696 scriptable_hosts.ClearPatterns();
697 expected.clear();
698
699 explicit_hosts.AddPattern(
700 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
701 scriptable_hosts.AddPattern(
702 URLPattern(URLPattern::SCHEME_HTTP, "http://*.example.com/*"));
703
704 expected.push_back("*.google.com");
705 expected.push_back("*.example.com");
706
707 perm_set.reset(new ExtensionPermissionSet(
708 empty_perms, explicit_hosts, scriptable_hosts));
709 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
710 }
711 }
712
713 TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay_ComIsBestRcd) {
714 scoped_ptr<ExtensionPermissionSet> perm_set;
715 ExtensionAPIPermissionSet empty_perms;
716 URLPatternSet explicit_hosts;
717 URLPatternSet scriptable_hosts;
718 explicit_hosts.AddPattern(
719 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
720 explicit_hosts.AddPattern(
721 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
722 explicit_hosts.AddPattern(
723 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
724 explicit_hosts.AddPattern(
725 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
726 explicit_hosts.AddPattern(
727 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
728 explicit_hosts.AddPattern(
729 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
730
731 std::vector<std::string> expected;
732 expected.push_back("www.foo.com");
733 perm_set.reset(new ExtensionPermissionSet(
734 empty_perms, explicit_hosts, scriptable_hosts));
735 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
736 }
737
738 TEST(ExtensionPermissionSetTest, GetDistinctHostsForDisplay_NetIs2ndBestRcd) {
739 scoped_ptr<ExtensionPermissionSet> perm_set;
740 ExtensionAPIPermissionSet empty_perms;
741 URLPatternSet explicit_hosts;
742 URLPatternSet scriptable_hosts;
743 explicit_hosts.AddPattern(
744 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
745 explicit_hosts.AddPattern(
746 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
747 explicit_hosts.AddPattern(
748 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
749 explicit_hosts.AddPattern(
750 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
751 explicit_hosts.AddPattern(
752 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
753 // No http://www.foo.com/path
754
755 std::vector<std::string> expected;
756 expected.push_back("www.foo.net");
757 perm_set.reset(new ExtensionPermissionSet(
758 empty_perms, explicit_hosts, scriptable_hosts));
759 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
760 }
761
762 TEST(ExtensionPermissionSetTest,
763 GetDistinctHostsForDisplay_OrgIs3rdBestRcd) {
764 scoped_ptr<ExtensionPermissionSet> perm_set;
765 ExtensionAPIPermissionSet empty_perms;
766 URLPatternSet explicit_hosts;
767 URLPatternSet scriptable_hosts;
768 explicit_hosts.AddPattern(
769 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
770 explicit_hosts.AddPattern(
771 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
772 explicit_hosts.AddPattern(
773 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
774 // No http://www.foo.net/path
775 explicit_hosts.AddPattern(
776 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
777 // No http://www.foo.com/path
778
779 std::vector<std::string> expected;
780 expected.push_back("www.foo.org");
781 perm_set.reset(new ExtensionPermissionSet(
782 empty_perms, explicit_hosts, scriptable_hosts));
783 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
784 }
785
786 TEST(ExtensionPermissionSetTest,
787 GetDistinctHostsForDisplay_FirstInListIs4thBestRcd) {
788 scoped_ptr<ExtensionPermissionSet> perm_set;
789 ExtensionAPIPermissionSet empty_perms;
790 URLPatternSet explicit_hosts;
791 URLPatternSet scriptable_hosts;
792 explicit_hosts.AddPattern(
793 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
794 // No http://www.foo.org/path
795 explicit_hosts.AddPattern(
796 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
797 // No http://www.foo.net/path
798 explicit_hosts.AddPattern(
799 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
800 // No http://www.foo.com/path
801
802 std::vector<std::string> expected;
803 expected.push_back("www.foo.ca");
804 perm_set.reset(new ExtensionPermissionSet(
805 empty_perms, explicit_hosts, scriptable_hosts));
806 CompareLists(expected, perm_set->GetDistinctHostsForDisplay());
807 }
808
809 TEST(ExtensionPermissionSetTest, HasLessHostPrivilegesThan) {
810 URLPatternSet elist1;
811 URLPatternSet elist2;
812 URLPatternSet slist1;
813 URLPatternSet slist2;
814 scoped_ptr<ExtensionPermissionSet> set1;
815 scoped_ptr<ExtensionPermissionSet> set2;
816 ExtensionAPIPermissionSet empty_perms;
817 elist1.AddPattern(
818 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
819 elist1.AddPattern(
820 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
821
822 // Test that the host order does not matter.
823 elist2.AddPattern(
824 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
825 elist2.AddPattern(
826 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
827
828 set1.reset(new ExtensionPermissionSet(empty_perms, elist1, slist1));
829 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
830
831 EXPECT_FALSE(set1->HasLessHostPrivilegesThan(set2.get()));
832 EXPECT_FALSE(set2->HasLessHostPrivilegesThan(set1.get()));
833
834 // Test that paths are ignored.
835 elist2.ClearPatterns();
836 elist2.AddPattern(
837 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/*"));
838 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
839 EXPECT_FALSE(set1->HasLessHostPrivilegesThan(set2.get()));
840 EXPECT_FALSE(set2->HasLessHostPrivilegesThan(set1.get()));
841
842 // Test that RCDs are ignored.
843 elist2.ClearPatterns();
844 elist2.AddPattern(
845 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/*"));
846 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
847 EXPECT_FALSE(set1->HasLessHostPrivilegesThan(set2.get()));
848 EXPECT_FALSE(set2->HasLessHostPrivilegesThan(set1.get()));
849
850 // Test that subdomain wildcards are handled properly.
851 elist2.ClearPatterns();
852 elist2.AddPattern(
853 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com.hk/*"));
854 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
855 EXPECT_TRUE(set1->HasLessHostPrivilegesThan(set2.get()));
856 //TODO(jstritar): Does not match subdomains properly. http://crbug.com/65337
857 //EXPECT_FALSE(set2->HasLessHostPrivilegesThan(set1.get()));
858
859 // Test that different domains count as different hosts.
860 elist2.ClearPatterns();
861 elist2.AddPattern(
862 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
863 elist2.AddPattern(
864 URLPattern(URLPattern::SCHEME_HTTP, "http://www.example.org/path"));
865 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
866 EXPECT_TRUE(set1->HasLessHostPrivilegesThan(set2.get()));
867 EXPECT_FALSE(set2->HasLessHostPrivilegesThan(set1.get()));
868
869 // Test that different subdomains count as different hosts.
870 elist2.ClearPatterns();
871 elist2.AddPattern(
872 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*"));
873 set2.reset(new ExtensionPermissionSet(empty_perms, elist2, slist2));
874 EXPECT_TRUE(set1->HasLessHostPrivilegesThan(set2.get()));
875 EXPECT_TRUE(set2->HasLessHostPrivilegesThan(set1.get()));
876 }
877
878 TEST(ExtensionPermissionSetTest, GetAPIsAsStrings) {
879 ExtensionAPIPermissionSet apis;
880 URLPatternSet empty_set;
881
882 apis.insert(ExtensionAPIPermission::kProxy);
883 apis.insert(ExtensionAPIPermission::kBackground);
884 apis.insert(ExtensionAPIPermission::kNotification);
885 apis.insert(ExtensionAPIPermission::kTab);
886
887 ExtensionPermissionSet perm_set(apis, empty_set, empty_set);
888 std::set<std::string> api_names = perm_set.GetAPIsAsStrings();
889
890 // The result is correct if it has the same number of elements
891 // and we can convert it back to the id set.
892 EXPECT_EQ((size_t)4, api_names.size());
893 EXPECT_EQ(apis, ExtensionPermissionsInfo::GetAllByName(api_names));
894 }
895
896 TEST(ExtensionPermissionSetTest, IsDefault) {
897 ExtensionAPIPermissionSet empty_apis;
898 URLPatternSet empty_extent;
899
900 ExtensionPermissionSet perm_set;
901 EXPECT_TRUE(perm_set.IsDefault());
902
903 perm_set = ExtensionPermissionSet(empty_apis, empty_extent, empty_extent);
904 EXPECT_TRUE(perm_set.IsDefault());
905
906 // Default permission shouldn't affect this.
907 ExtensionAPIPermissionSet non_empty_apis;
908 non_empty_apis.insert(ExtensionAPIPermission::kDefault);
909
910 perm_set = ExtensionPermissionSet(
911 non_empty_apis, empty_extent, empty_extent);
912 EXPECT_TRUE(perm_set.IsDefault());
913
914 // Add non default API.
915 non_empty_apis.insert(ExtensionAPIPermission::kBackground);
916 perm_set = ExtensionPermissionSet(
917 non_empty_apis, empty_extent, empty_extent);
918 EXPECT_FALSE(perm_set.IsDefault());
919
920 // Try non standard host
921 URLPatternSet non_empty_extent;
922 AddPattern(&non_empty_extent, "http://www.google.com/*");
923
924 perm_set = ExtensionPermissionSet(
925 empty_apis, non_empty_extent, empty_extent);
926 EXPECT_FALSE(perm_set.IsDefault());
927
928 perm_set = ExtensionPermissionSet(
929 empty_apis, empty_extent, non_empty_extent);
930 EXPECT_FALSE(perm_set.IsDefault());
931 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698