Remove APIPermission::kFileSystemWriteDirectory
[chromium-blink-merge.git] / chrome / common / extensions / permissions / permission_set_unittest.cc
blob7f8f381d9aa7c8aed7ed9ac2e16469cb7449cfea
1 // Copyright (c) 2012 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/command_line.h"
6 #include "base/json/json_file_value_serializer.h"
7 #include "base/logging.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/common/chrome_paths.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/extensions/extension_test_util.h"
14 #include "chrome/common/extensions/features/feature_channel.h"
15 #include "chrome/common/extensions/permissions/chrome_permission_message_provider.h"
16 #include "chrome/grit/generated_resources.h"
17 #include "components/version_info/version_info.h"
18 #include "extensions/common/error_utils.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_builder.h"
21 #include "extensions/common/permissions/permission_message_provider.h"
22 #include "extensions/common/permissions/permission_message_test_util.h"
23 #include "extensions/common/permissions/permission_message_util.h"
24 #include "extensions/common/permissions/permission_set.h"
25 #include "extensions/common/permissions/permissions_data.h"
26 #include "extensions/common/permissions/permissions_info.h"
27 #include "extensions/common/permissions/socket_permission.h"
28 #include "extensions/common/value_builder.h"
29 #include "extensions/strings/grit/extensions_strings.h"
30 #include "testing/gtest/include/gtest/gtest.h"
31 #include "ui/base/l10n/l10n_util.h"
33 using extension_test_util::LoadManifest;
35 namespace extensions {
37 namespace {
39 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
40 int schemes = URLPattern::SCHEME_ALL;
41 extent->AddPattern(URLPattern(schemes, pattern));
44 size_t IndexOf(const CoalescedPermissionMessages& warnings,
45 const std::string& warning) {
46 size_t i = 0;
47 for (const CoalescedPermissionMessage& msg : warnings) {
48 if (msg.message() == base::ASCIIToUTF16(warning))
49 return i;
50 ++i;
53 return warnings.size();
56 PermissionIDSet MakePermissionIDSet(APIPermission::ID id) {
57 PermissionIDSet set;
58 set.insert(id);
59 return set;
62 PermissionIDSet MakePermissionIDSet(APIPermission::ID id1,
63 APIPermission::ID id2) {
64 PermissionIDSet set;
65 set.insert(id1);
66 set.insert(id2);
67 return set;
70 PermissionIDSet MakePermissionIDSet(const APIPermissionSet& permissions) {
71 PermissionIDSet set;
72 for (const APIPermission* permission : permissions)
73 set.insert(permission->id());
74 return set;
77 std::string PermissionIDsToString(const PermissionIDSet& ids) {
78 std::vector<std::string> strs;
79 for (const PermissionID& id : ids)
80 strs.push_back(base::IntToString(id.id()));
81 return base::StringPrintf("[ %s ]", base::JoinString(strs, ", ").c_str());
84 std::string CoalescedPermissionIDsToString(
85 const CoalescedPermissionMessages& msgs) {
86 std::vector<std::string> strs;
87 for (const CoalescedPermissionMessage& msg : msgs)
88 strs.push_back(PermissionIDsToString(msg.permissions()));
89 return base::JoinString(strs, " ");
92 // Check that the given |permissions| produce a single warning message,
93 // identified by the set of |expected_ids|.
94 testing::AssertionResult PermissionSetProducesMessage(
95 const PermissionSet* permissions,
96 Manifest::Type extension_type,
97 const PermissionIDSet& expected_ids) {
98 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
100 CoalescedPermissionMessages msgs = provider->GetPermissionMessages(
101 provider->GetAllPermissionIDs(permissions, extension_type));
102 if (msgs.size() != 1) {
103 return testing::AssertionFailure()
104 << "Expected single permission message with IDs "
105 << PermissionIDsToString(expected_ids) << " but got " << msgs.size()
106 << " messages: " << CoalescedPermissionIDsToString(msgs);
108 if (!msgs.front().permissions().Equals(expected_ids)) {
109 return testing::AssertionFailure()
110 << "Expected permission IDs " << PermissionIDsToString(expected_ids)
111 << " but got " << PermissionIDsToString(msgs.front().permissions());
114 return testing::AssertionSuccess();
117 } // namespace
119 // Tests GetByID.
120 TEST(PermissionsTest, GetByID) {
121 PermissionsInfo* info = PermissionsInfo::GetInstance();
122 APIPermissionSet apis = info->GetAll();
123 for (APIPermissionSet::const_iterator i = apis.begin();
124 i != apis.end(); ++i) {
125 EXPECT_EQ(i->id(), i->info()->id());
129 // Tests that GetByName works with normal permission names and aliases.
130 TEST(PermissionsTest, GetByName) {
131 PermissionsInfo* info = PermissionsInfo::GetInstance();
132 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
133 EXPECT_EQ(APIPermission::kManagement,
134 info->GetByName("management")->id());
135 EXPECT_FALSE(info->GetByName("alsdkfjasldkfj"));
138 TEST(PermissionsTest, GetAll) {
139 size_t count = 0;
140 PermissionsInfo* info = PermissionsInfo::GetInstance();
141 APIPermissionSet apis = info->GetAll();
142 for (APIPermissionSet::const_iterator api = apis.begin();
143 api != apis.end(); ++api) {
144 // Make sure only the valid permission IDs get returned.
145 EXPECT_NE(APIPermission::kInvalid, api->id());
146 EXPECT_NE(APIPermission::kUnknown, api->id());
147 count++;
149 EXPECT_EQ(count, info->get_permission_count());
152 TEST(PermissionsTest, GetAllByName) {
153 std::set<std::string> names;
154 names.insert("background");
155 names.insert("management");
157 // This is an alias of kTab
158 names.insert("windows");
160 // This unknown name should get dropped.
161 names.insert("sdlkfjasdlkfj");
163 APIPermissionSet expected;
164 expected.insert(APIPermission::kBackground);
165 expected.insert(APIPermission::kManagement);
166 expected.insert(APIPermission::kTab);
168 EXPECT_EQ(expected,
169 PermissionsInfo::GetInstance()->GetAllByName(names));
172 // Tests that the aliases are properly mapped.
173 TEST(PermissionsTest, Aliases) {
174 PermissionsInfo* info = PermissionsInfo::GetInstance();
175 // tabs: tabs, windows
176 std::string tabs_name = "tabs";
177 EXPECT_EQ(tabs_name, info->GetByID(APIPermission::kTab)->name());
178 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
179 EXPECT_EQ(APIPermission::kTab, info->GetByName("windows")->id());
181 // unlimitedStorage: unlimitedStorage, unlimited_storage
182 std::string storage_name = "unlimitedStorage";
183 EXPECT_EQ(storage_name, info->GetByID(
184 APIPermission::kUnlimitedStorage)->name());
185 EXPECT_EQ(APIPermission::kUnlimitedStorage,
186 info->GetByName("unlimitedStorage")->id());
187 EXPECT_EQ(APIPermission::kUnlimitedStorage,
188 info->GetByName("unlimited_storage")->id());
191 TEST(PermissionsTest, EffectiveHostPermissions) {
192 scoped_refptr<Extension> extension;
193 scoped_refptr<const PermissionSet> permissions;
195 extension = LoadManifest("effective_host_permissions", "empty.json");
196 permissions = extension->permissions_data()->active_permissions();
197 EXPECT_EQ(0u,
198 extension->permissions_data()
199 ->GetEffectiveHostPermissions()
200 .patterns()
201 .size());
202 EXPECT_FALSE(
203 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
204 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
206 extension = LoadManifest("effective_host_permissions", "one_host.json");
207 permissions = extension->permissions_data()->active_permissions();
208 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
209 GURL("http://www.google.com")));
210 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(
211 GURL("https://www.google.com")));
212 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
214 extension = LoadManifest("effective_host_permissions",
215 "one_host_wildcard.json");
216 permissions = extension->permissions_data()->active_permissions();
217 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
218 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
219 GURL("http://foo.google.com")));
220 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
222 extension = LoadManifest("effective_host_permissions", "two_hosts.json");
223 permissions = extension->permissions_data()->active_permissions();
224 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
225 GURL("http://www.google.com")));
226 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
227 GURL("http://www.reddit.com")));
228 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
230 extension = LoadManifest("effective_host_permissions",
231 "https_not_considered.json");
232 permissions = extension->permissions_data()->active_permissions();
233 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
234 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://google.com")));
235 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
237 extension = LoadManifest("effective_host_permissions",
238 "two_content_scripts.json");
239 permissions = extension->permissions_data()->active_permissions();
240 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
241 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
242 GURL("http://www.reddit.com")));
243 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
244 GURL("http://news.ycombinator.com")));
245 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
247 extension = LoadManifest("effective_host_permissions", "all_hosts.json");
248 permissions = extension->permissions_data()->active_permissions();
249 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
250 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("https://test/")));
251 EXPECT_TRUE(
252 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
253 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
255 extension = LoadManifest("effective_host_permissions", "all_hosts2.json");
256 permissions = extension->permissions_data()->active_permissions();
257 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
258 EXPECT_TRUE(
259 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
260 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
262 extension = LoadManifest("effective_host_permissions", "all_hosts3.json");
263 permissions = extension->permissions_data()->active_permissions();
264 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
265 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://test/")));
266 EXPECT_TRUE(
267 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
268 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
271 TEST(PermissionsTest, ExplicitAccessToOrigin) {
272 APIPermissionSet apis;
273 ManifestPermissionSet manifest_permissions;
274 URLPatternSet explicit_hosts;
275 URLPatternSet scriptable_hosts;
277 AddPattern(&explicit_hosts, "http://*.google.com/*");
278 // The explicit host paths should get set to /*.
279 AddPattern(&explicit_hosts, "http://www.example.com/a/particular/path/*");
281 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
282 apis, manifest_permissions, explicit_hosts, scriptable_hosts);
283 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
284 GURL("http://www.google.com/")));
285 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
286 GURL("http://test.google.com/")));
287 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
288 GURL("http://www.example.com")));
289 ASSERT_TRUE(perm_set->HasEffectiveAccessToURL(
290 GURL("http://www.example.com")));
291 ASSERT_FALSE(perm_set->HasExplicitAccessToOrigin(
292 GURL("http://test.example.com")));
295 TEST(PermissionsTest, CreateUnion) {
296 APIPermission* permission = NULL;
298 ManifestPermissionSet manifest_permissions;
299 APIPermissionSet apis1;
300 APIPermissionSet apis2;
301 APIPermissionSet expected_apis;
303 URLPatternSet explicit_hosts1;
304 URLPatternSet explicit_hosts2;
305 URLPatternSet expected_explicit_hosts;
307 URLPatternSet scriptable_hosts1;
308 URLPatternSet scriptable_hosts2;
309 URLPatternSet expected_scriptable_hosts;
311 URLPatternSet effective_hosts;
313 scoped_refptr<PermissionSet> set1;
314 scoped_refptr<PermissionSet> set2;
315 scoped_refptr<PermissionSet> union_set;
317 const APIPermissionInfo* permission_info =
318 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
319 permission = permission_info->CreateAPIPermission();
321 scoped_ptr<base::ListValue> value(new base::ListValue());
322 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
323 value->Append(new base::StringValue("udp-bind::8080"));
324 value->Append(new base::StringValue("udp-send-to::8888"));
325 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
328 // Union with an empty set.
329 apis1.insert(APIPermission::kTab);
330 apis1.insert(APIPermission::kBackground);
331 apis1.insert(permission->Clone());
332 expected_apis.insert(APIPermission::kTab);
333 expected_apis.insert(APIPermission::kBackground);
334 expected_apis.insert(permission);
336 AddPattern(&explicit_hosts1, "http://*.google.com/*");
337 AddPattern(&expected_explicit_hosts, "http://*.google.com/*");
338 AddPattern(&effective_hosts, "http://*.google.com/*");
340 set1 = new PermissionSet(apis1, manifest_permissions,
341 explicit_hosts1, scriptable_hosts1);
342 set2 = new PermissionSet(apis2, manifest_permissions,
343 explicit_hosts2, scriptable_hosts2);
344 union_set = PermissionSet::CreateUnion(set1.get(), set2.get());
345 EXPECT_TRUE(set1->Contains(*set2.get()));
346 EXPECT_TRUE(set1->Contains(*union_set.get()));
347 EXPECT_FALSE(set2->Contains(*set1.get()));
348 EXPECT_FALSE(set2->Contains(*union_set.get()));
349 EXPECT_TRUE(union_set->Contains(*set1.get()));
350 EXPECT_TRUE(union_set->Contains(*set2.get()));
352 EXPECT_FALSE(union_set->HasEffectiveFullAccess());
353 EXPECT_EQ(expected_apis, union_set->apis());
354 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
355 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
356 EXPECT_EQ(expected_explicit_hosts, union_set->effective_hosts());
358 // Now use a real second set.
359 apis2.insert(APIPermission::kTab);
360 apis2.insert(APIPermission::kProxy);
361 apis2.insert(APIPermission::kClipboardWrite);
362 apis2.insert(APIPermission::kPlugin);
364 permission = permission_info->CreateAPIPermission();
366 scoped_ptr<base::ListValue> value(new base::ListValue());
367 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
368 value->Append(new base::StringValue("udp-send-to::8899"));
369 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
371 apis2.insert(permission);
373 expected_apis.insert(APIPermission::kTab);
374 expected_apis.insert(APIPermission::kProxy);
375 expected_apis.insert(APIPermission::kClipboardWrite);
376 expected_apis.insert(APIPermission::kPlugin);
378 permission = permission_info->CreateAPIPermission();
380 scoped_ptr<base::ListValue> value(new base::ListValue());
381 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
382 value->Append(new base::StringValue("udp-bind::8080"));
383 value->Append(new base::StringValue("udp-send-to::8888"));
384 value->Append(new base::StringValue("udp-send-to::8899"));
385 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
387 // Insert a new permission socket permisssion which will replace the old one.
388 expected_apis.insert(permission);
390 AddPattern(&explicit_hosts2, "http://*.example.com/*");
391 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
392 AddPattern(&expected_explicit_hosts, "http://*.example.com/*");
393 AddPattern(&expected_scriptable_hosts, "http://*.google.com/*");
395 URLPatternSet::CreateUnion(
396 explicit_hosts2, scriptable_hosts2, &effective_hosts);
398 set2 = new PermissionSet(apis2, manifest_permissions,
399 explicit_hosts2, scriptable_hosts2);
400 union_set = PermissionSet::CreateUnion(set1.get(), set2.get());
402 EXPECT_FALSE(set1->Contains(*set2.get()));
403 EXPECT_FALSE(set1->Contains(*union_set.get()));
404 EXPECT_FALSE(set2->Contains(*set1.get()));
405 EXPECT_FALSE(set2->Contains(*union_set.get()));
406 EXPECT_TRUE(union_set->Contains(*set1.get()));
407 EXPECT_TRUE(union_set->Contains(*set2.get()));
409 EXPECT_TRUE(union_set->HasEffectiveFullAccess());
410 EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts());
411 EXPECT_EQ(expected_apis, union_set->apis());
412 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
413 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
414 EXPECT_EQ(effective_hosts, union_set->effective_hosts());
417 TEST(PermissionsTest, CreateIntersection) {
418 APIPermission* permission = NULL;
420 ManifestPermissionSet manifest_permissions;
421 APIPermissionSet apis1;
422 APIPermissionSet apis2;
423 APIPermissionSet expected_apis;
425 URLPatternSet explicit_hosts1;
426 URLPatternSet explicit_hosts2;
427 URLPatternSet expected_explicit_hosts;
429 URLPatternSet scriptable_hosts1;
430 URLPatternSet scriptable_hosts2;
431 URLPatternSet expected_scriptable_hosts;
433 URLPatternSet effective_hosts;
435 scoped_refptr<PermissionSet> set1;
436 scoped_refptr<PermissionSet> set2;
437 scoped_refptr<PermissionSet> new_set;
439 const APIPermissionInfo* permission_info =
440 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
442 // Intersection with an empty set.
443 apis1.insert(APIPermission::kTab);
444 apis1.insert(APIPermission::kBackground);
445 permission = permission_info->CreateAPIPermission();
447 scoped_ptr<base::ListValue> value(new base::ListValue());
448 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
449 value->Append(new base::StringValue("udp-bind::8080"));
450 value->Append(new base::StringValue("udp-send-to::8888"));
451 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
453 apis1.insert(permission);
455 AddPattern(&explicit_hosts1, "http://*.google.com/*");
456 AddPattern(&scriptable_hosts1, "http://www.reddit.com/*");
458 set1 = new PermissionSet(apis1, manifest_permissions,
459 explicit_hosts1, scriptable_hosts1);
460 set2 = new PermissionSet(apis2, manifest_permissions,
461 explicit_hosts2, scriptable_hosts2);
462 new_set = PermissionSet::CreateIntersection(set1.get(), set2.get());
463 EXPECT_TRUE(set1->Contains(*new_set.get()));
464 EXPECT_TRUE(set2->Contains(*new_set.get()));
465 EXPECT_TRUE(set1->Contains(*set2.get()));
466 EXPECT_FALSE(set2->Contains(*set1.get()));
467 EXPECT_FALSE(new_set->Contains(*set1.get()));
468 EXPECT_TRUE(new_set->Contains(*set2.get()));
470 EXPECT_TRUE(new_set->IsEmpty());
471 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
472 EXPECT_EQ(expected_apis, new_set->apis());
473 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
474 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
475 EXPECT_EQ(expected_explicit_hosts, new_set->effective_hosts());
477 // Now use a real second set.
478 apis2.insert(APIPermission::kTab);
479 apis2.insert(APIPermission::kProxy);
480 apis2.insert(APIPermission::kClipboardWrite);
481 apis2.insert(APIPermission::kPlugin);
482 permission = permission_info->CreateAPIPermission();
484 scoped_ptr<base::ListValue> value(new base::ListValue());
485 value->Append(new base::StringValue("udp-bind::8080"));
486 value->Append(new base::StringValue("udp-send-to::8888"));
487 value->Append(new base::StringValue("udp-send-to::8899"));
488 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
490 apis2.insert(permission);
492 expected_apis.insert(APIPermission::kTab);
493 permission = permission_info->CreateAPIPermission();
495 scoped_ptr<base::ListValue> value(new base::ListValue());
496 value->Append(new base::StringValue("udp-bind::8080"));
497 value->Append(new base::StringValue("udp-send-to::8888"));
498 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
500 expected_apis.insert(permission);
502 AddPattern(&explicit_hosts2, "http://*.example.com/*");
503 AddPattern(&explicit_hosts2, "http://*.google.com/*");
504 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
505 AddPattern(&expected_explicit_hosts, "http://*.google.com/*");
507 effective_hosts.ClearPatterns();
508 AddPattern(&effective_hosts, "http://*.google.com/*");
510 set2 = new PermissionSet(apis2, manifest_permissions,
511 explicit_hosts2, scriptable_hosts2);
512 new_set = PermissionSet::CreateIntersection(set1.get(), set2.get());
514 EXPECT_TRUE(set1->Contains(*new_set.get()));
515 EXPECT_TRUE(set2->Contains(*new_set.get()));
516 EXPECT_FALSE(set1->Contains(*set2.get()));
517 EXPECT_FALSE(set2->Contains(*set1.get()));
518 EXPECT_FALSE(new_set->Contains(*set1.get()));
519 EXPECT_FALSE(new_set->Contains(*set2.get()));
521 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
522 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts());
523 EXPECT_EQ(expected_apis, new_set->apis());
524 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
525 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
526 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
529 TEST(PermissionsTest, CreateDifference) {
530 APIPermission* permission = NULL;
532 ManifestPermissionSet manifest_permissions;
533 APIPermissionSet apis1;
534 APIPermissionSet apis2;
535 APIPermissionSet expected_apis;
537 URLPatternSet explicit_hosts1;
538 URLPatternSet explicit_hosts2;
539 URLPatternSet expected_explicit_hosts;
541 URLPatternSet scriptable_hosts1;
542 URLPatternSet scriptable_hosts2;
543 URLPatternSet expected_scriptable_hosts;
545 URLPatternSet effective_hosts;
547 scoped_refptr<PermissionSet> set1;
548 scoped_refptr<PermissionSet> set2;
549 scoped_refptr<PermissionSet> new_set;
551 const APIPermissionInfo* permission_info =
552 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
554 // Difference with an empty set.
555 apis1.insert(APIPermission::kTab);
556 apis1.insert(APIPermission::kBackground);
557 permission = permission_info->CreateAPIPermission();
559 scoped_ptr<base::ListValue> value(new base::ListValue());
560 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
561 value->Append(new base::StringValue("udp-bind::8080"));
562 value->Append(new base::StringValue("udp-send-to::8888"));
563 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
565 apis1.insert(permission);
567 AddPattern(&explicit_hosts1, "http://*.google.com/*");
568 AddPattern(&scriptable_hosts1, "http://www.reddit.com/*");
570 set1 = new PermissionSet(apis1, manifest_permissions,
571 explicit_hosts1, scriptable_hosts1);
572 set2 = new PermissionSet(apis2, manifest_permissions,
573 explicit_hosts2, scriptable_hosts2);
574 new_set = PermissionSet::CreateDifference(set1.get(), set2.get());
575 EXPECT_EQ(*set1.get(), *new_set.get());
577 // Now use a real second set.
578 apis2.insert(APIPermission::kTab);
579 apis2.insert(APIPermission::kProxy);
580 apis2.insert(APIPermission::kClipboardWrite);
581 apis2.insert(APIPermission::kPlugin);
582 permission = permission_info->CreateAPIPermission();
584 scoped_ptr<base::ListValue> value(new base::ListValue());
585 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
586 value->Append(new base::StringValue("udp-send-to::8899"));
587 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
589 apis2.insert(permission);
591 expected_apis.insert(APIPermission::kBackground);
592 permission = permission_info->CreateAPIPermission();
594 scoped_ptr<base::ListValue> value(new base::ListValue());
595 value->Append(new base::StringValue("udp-bind::8080"));
596 value->Append(new base::StringValue("udp-send-to::8888"));
597 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
599 expected_apis.insert(permission);
601 AddPattern(&explicit_hosts2, "http://*.example.com/*");
602 AddPattern(&explicit_hosts2, "http://*.google.com/*");
603 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
604 AddPattern(&expected_scriptable_hosts, "http://www.reddit.com/*");
606 effective_hosts.ClearPatterns();
607 AddPattern(&effective_hosts, "http://www.reddit.com/*");
609 set2 = new PermissionSet(apis2, manifest_permissions,
610 explicit_hosts2, scriptable_hosts2);
611 new_set = PermissionSet::CreateDifference(set1.get(), set2.get());
613 EXPECT_TRUE(set1->Contains(*new_set.get()));
614 EXPECT_FALSE(set2->Contains(*new_set.get()));
616 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
617 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts());
618 EXPECT_EQ(expected_apis, new_set->apis());
619 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
620 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
621 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
623 // |set3| = |set1| - |set2| --> |set3| intersect |set2| == empty_set
624 set1 = PermissionSet::CreateIntersection(new_set.get(), set2.get());
625 EXPECT_TRUE(set1->IsEmpty());
628 TEST(PermissionsTest, IsPrivilegeIncrease) {
629 const struct {
630 const char* base_name;
631 bool expect_increase;
632 } kTests[] = {
633 { "allhosts1", false }, // all -> all
634 { "allhosts2", false }, // all -> one
635 { "allhosts3", true }, // one -> all
636 { "hosts1", false }, // http://a,http://b -> http://a,http://b
637 { "hosts2", true }, // http://a,http://b -> https://a,http://*.b
638 { "hosts3", false }, // http://a,http://b -> http://a
639 { "hosts4", true }, // http://a -> http://a,http://b
640 { "hosts5", false }, // http://a,b,c -> http://a,b,c + https://a,b,c
641 { "hosts6", false }, // http://a.com -> http://a.com + http://a.co.uk
642 { "permissions1", false }, // tabs -> tabs
643 { "permissions2", true }, // tabs -> tabs,bookmarks
644 // TODO(treib): This is wrong, kAllHosts implies kTabs. crbug.com/512344
645 { "permissions3", true }, // http://*/* -> http://*/*,tabs
646 { "permissions5", true }, // bookmarks -> bookmarks,history
647 { "equivalent_warnings", false }, // tabs --> tabs, webNavigation
648 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS
649 { "permissions4", false }, // plugin -> plugin,tabs
650 { "plugin1", false }, // plugin -> plugin
651 { "plugin2", false }, // plugin -> none
652 { "plugin3", true }, // none -> plugin
653 #endif
654 { "storage", false }, // none -> storage
655 { "notifications", false }, // none -> notifications
656 { "platformapp1", false }, // host permissions for platform apps
657 { "platformapp2", true }, // API permissions for platform apps
658 { "media_galleries1", true }, // all -> read|all
659 { "media_galleries2", true }, // read|all -> read|delete|copyTo|all
660 { "media_galleries3", true }, // all -> read|delete|all
661 { "media_galleries4", false }, // read|all -> all
662 { "media_galleries5", false }, // read|copyTo|delete|all -> read|all
663 { "media_galleries6", false }, // read|all -> read|all
664 { "media_galleries7", true }, // read|delete|all -> read|copyTo|delete|all
665 { "sockets1", true }, // none -> tcp:*:*
666 { "sockets2", false }, // tcp:*:* -> tcp:*:*
667 { "sockets3", true }, // tcp:a.com:80 -> tcp:*:*
670 for (size_t i = 0; i < arraysize(kTests); ++i) {
671 scoped_refptr<Extension> old_extension(
672 LoadManifest("allow_silent_upgrade",
673 std::string(kTests[i].base_name) + "_old.json"));
674 scoped_refptr<Extension> new_extension(
675 LoadManifest("allow_silent_upgrade",
676 std::string(kTests[i].base_name) + "_new.json"));
678 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json";
679 if (!new_extension.get())
680 continue;
682 scoped_refptr<const PermissionSet> old_p(
683 old_extension->permissions_data()->active_permissions());
684 scoped_refptr<const PermissionSet> new_p(
685 new_extension->permissions_data()->active_permissions());
686 Manifest::Type extension_type = old_extension->GetType();
688 bool increased = PermissionMessageProvider::Get()->IsPrivilegeIncrease(
689 old_p.get(), new_p.get(), extension_type);
690 EXPECT_EQ(kTests[i].expect_increase, increased) << kTests[i].base_name;
694 TEST(PermissionsTest, PermissionMessages) {
695 // Ensure that all permissions that needs to show install UI actually have
696 // strings associated with them.
697 APIPermissionSet skip;
699 // These are considered "nuisance" or "trivial" permissions that don't need
700 // a prompt.
701 skip.insert(APIPermission::kActiveTab);
702 skip.insert(APIPermission::kAlarms);
703 skip.insert(APIPermission::kAlphaEnabled);
704 skip.insert(APIPermission::kAlwaysOnTopWindows);
705 skip.insert(APIPermission::kAppView);
706 skip.insert(APIPermission::kAudio);
707 skip.insert(APIPermission::kAudioModem);
708 skip.insert(APIPermission::kBrowsingData);
709 skip.insert(APIPermission::kCastStreaming);
710 skip.insert(APIPermission::kCommandsAccessibility);
711 skip.insert(APIPermission::kContextMenus);
712 skip.insert(APIPermission::kCryptotokenPrivate);
713 skip.insert(APIPermission::kCopresencePrivate);
714 skip.insert(APIPermission::kDesktopCapturePrivate);
715 skip.insert(APIPermission::kDiagnostics);
716 skip.insert(APIPermission::kDns);
717 skip.insert(APIPermission::kDownloadsShelf);
718 skip.insert(APIPermission::kEmbeddedExtensionOptions);
719 skip.insert(APIPermission::kExtensionView);
720 skip.insert(APIPermission::kFontSettings);
721 skip.insert(APIPermission::kFullscreen);
722 skip.insert(APIPermission::kGcm);
723 skip.insert(APIPermission::kIdle);
724 skip.insert(APIPermission::kImeWindowEnabled);
725 skip.insert(APIPermission::kInlineInstallPrivate);
726 skip.insert(APIPermission::kIdltest);
727 skip.insert(APIPermission::kLogPrivate);
728 skip.insert(APIPermission::kNotifications);
729 skip.insert(APIPermission::kNotificationProvider);
730 skip.insert(APIPermission::kOverrideEscFullscreen);
731 skip.insert(APIPermission::kPointerLock);
732 skip.insert(APIPermission::kPower);
733 skip.insert(APIPermission::kPrinterProvider);
734 skip.insert(APIPermission::kSessions);
735 skip.insert(APIPermission::kStorage);
736 skip.insert(APIPermission::kSystemCpu);
737 skip.insert(APIPermission::kSystemDisplay);
738 skip.insert(APIPermission::kSystemMemory);
739 skip.insert(APIPermission::kSystemNetwork);
740 skip.insert(APIPermission::kSystemStorage);
741 skip.insert(APIPermission::kTts);
742 skip.insert(APIPermission::kUnlimitedStorage);
743 skip.insert(APIPermission::kWebcamPrivate);
744 skip.insert(APIPermission::kWebView);
745 skip.insert(APIPermission::kWindowShape);
747 // These permissions are restricted to extensions force-installed by policy
748 // and don't require a prompt, i.e. they're restricted to location 'policy'.
749 skip.insert(APIPermission::kEnterprisePlatformKeys);
750 skip.insert(APIPermission::kEnterpriseDeviceAttributes);
752 // TODO(erikkay) add a string for this permission.
753 skip.insert(APIPermission::kBackground);
755 skip.insert(APIPermission::kClipboardWrite);
757 // The cookie permission does nothing unless you have associated host
758 // permissions.
759 skip.insert(APIPermission::kCookie);
761 // These are warned as part of host permission checks.
762 skip.insert(APIPermission::kDataReductionProxy);
763 skip.insert(APIPermission::kDeclarativeContent);
764 skip.insert(APIPermission::kPageCapture);
765 skip.insert(APIPermission::kProxy);
766 skip.insert(APIPermission::kTabCapture);
767 skip.insert(APIPermission::kWebRequest);
768 skip.insert(APIPermission::kWebRequestBlocking);
770 // This permission requires explicit user action (context menu handler)
771 // so we won't prompt for it for now.
772 skip.insert(APIPermission::kFileBrowserHandler);
774 // These permissions require explicit user action (configuration dialog)
775 // so we don't prompt for them at install time.
776 skip.insert(APIPermission::kMediaGalleries);
778 // If you've turned on the experimental command-line flag, we don't need
779 // to warn you further.
780 skip.insert(APIPermission::kExperimental);
782 // The Identity API has its own server-driven permission prompts.
783 skip.insert(APIPermission::kIdentity);
785 // These are private.
786 skip.insert(APIPermission::kAccessibilityPrivate);
787 skip.insert(APIPermission::kAutoTestPrivate);
788 skip.insert(APIPermission::kBookmarkManagerPrivate);
789 skip.insert(APIPermission::kBrailleDisplayPrivate);
790 skip.insert(APIPermission::kCast);
791 skip.insert(APIPermission::kCastStreaming);
792 skip.insert(APIPermission::kChromeosInfoPrivate);
793 skip.insert(APIPermission::kCloudPrintPrivate);
794 skip.insert(APIPermission::kCommandLinePrivate);
795 skip.insert(APIPermission::kDeveloperPrivate);
796 skip.insert(APIPermission::kDial);
797 skip.insert(APIPermission::kDownloadsInternal);
798 skip.insert(APIPermission::kEasyUnlockPrivate);
799 skip.insert(APIPermission::kEchoPrivate);
800 skip.insert(APIPermission::kEnterprisePlatformKeysPrivate);
801 skip.insert(APIPermission::kFeedbackPrivate);
802 skip.insert(APIPermission::kFileBrowserHandlerInternal);
803 skip.insert(APIPermission::kFileManagerPrivate);
804 skip.insert(APIPermission::kFirstRunPrivate);
805 skip.insert(APIPermission::kGcdPrivate);
806 skip.insert(APIPermission::kHotwordPrivate);
807 skip.insert(APIPermission::kIdentityPrivate);
808 skip.insert(APIPermission::kInputMethodPrivate);
809 skip.insert(APIPermission::kLanguageSettingsPrivate);
810 skip.insert(APIPermission::kMediaPlayerPrivate);
811 skip.insert(APIPermission::kMediaRouterPrivate);
812 skip.insert(APIPermission::kMetricsPrivate);
813 skip.insert(APIPermission::kPreferencesPrivate);
814 skip.insert(APIPermission::kPrincipalsPrivate);
815 skip.insert(APIPermission::kImageWriterPrivate);
816 skip.insert(APIPermission::kReadingListPrivate);
817 skip.insert(APIPermission::kRtcPrivate);
818 skip.insert(APIPermission::kStreamsPrivate);
819 skip.insert(APIPermission::kSystemPrivate);
820 skip.insert(APIPermission::kTabCaptureForTab);
821 skip.insert(APIPermission::kTerminalPrivate);
822 skip.insert(APIPermission::kVirtualKeyboardPrivate);
823 skip.insert(APIPermission::kWallpaperPrivate);
824 skip.insert(APIPermission::kWebrtcAudioPrivate);
825 skip.insert(APIPermission::kWebrtcDesktopCapturePrivate);
826 skip.insert(APIPermission::kWebrtcLoggingPrivate);
827 skip.insert(APIPermission::kWebstorePrivate);
828 skip.insert(APIPermission::kWebstoreWidgetPrivate);
830 // Warned as part of host permissions.
831 skip.insert(APIPermission::kDevtools);
833 // Platform apps.
834 skip.insert(APIPermission::kBrowser);
835 skip.insert(APIPermission::kHid);
836 skip.insert(APIPermission::kFileSystem);
837 skip.insert(APIPermission::kFileSystemProvider);
838 skip.insert(APIPermission::kFileSystemRequestFileSystem);
839 skip.insert(APIPermission::kFileSystemRetainEntries);
840 skip.insert(APIPermission::kFileSystemWrite);
841 skip.insert(APIPermission::kSocket);
842 skip.insert(APIPermission::kUsb);
843 skip.insert(APIPermission::kLauncherSearchProvider);
845 // We already have a generic message for declaring externally_connectable.
846 skip.insert(APIPermission::kExternallyConnectableAllUrls);
848 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
849 PermissionsInfo* info = PermissionsInfo::GetInstance();
850 APIPermissionSet permissions = info->GetAll();
851 for (APIPermissionSet::const_iterator i = permissions.begin();
852 i != permissions.end(); ++i) {
853 const APIPermissionInfo* permission_info = i->info();
854 EXPECT_TRUE(permission_info != NULL);
856 PermissionIDSet id;
857 id.insert(permission_info->id());
858 bool has_message = !provider->GetPermissionMessages(id).empty();
859 bool should_have_message = !skip.count(i->id());
860 EXPECT_EQ(should_have_message, has_message) << permission_info->name();
864 TEST(PermissionsTest, FileSystemPermissionMessages) {
865 APIPermissionSet api_permissions;
866 api_permissions.insert(APIPermission::kFileSystemWrite);
867 api_permissions.insert(APIPermission::kFileSystemDirectory);
868 scoped_refptr<PermissionSet> permissions(
869 new PermissionSet(api_permissions, ManifestPermissionSet(),
870 URLPatternSet(), URLPatternSet()));
871 EXPECT_TRUE(PermissionSetProducesMessage(
872 permissions.get(), Manifest::TYPE_PLATFORM_APP,
873 MakePermissionIDSet(api_permissions)));
876 TEST(PermissionsTest, HiddenFileSystemPermissionMessages) {
877 APIPermissionSet api_permissions;
878 api_permissions.insert(APIPermission::kFileSystemWrite);
879 api_permissions.insert(APIPermission::kFileSystemDirectory);
880 scoped_refptr<PermissionSet> permissions(
881 new PermissionSet(api_permissions, ManifestPermissionSet(),
882 URLPatternSet(), URLPatternSet()));
883 EXPECT_TRUE(PermissionSetProducesMessage(
884 permissions.get(), Manifest::TYPE_PLATFORM_APP,
885 MakePermissionIDSet(api_permissions)));
888 TEST(PermissionsTest, SuppressedPermissionMessages) {
890 // Tabs warning suppresses favicon warning.
891 APIPermissionSet api_permissions;
892 api_permissions.insert(APIPermission::kTab);
893 URLPatternSet hosts;
894 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI,
895 "chrome://favicon/"));
896 scoped_refptr<PermissionSet> permissions(
897 new PermissionSet(api_permissions, ManifestPermissionSet(),
898 hosts, URLPatternSet()));
899 EXPECT_TRUE(PermissionSetProducesMessage(
900 permissions.get(), Manifest::TYPE_EXTENSION,
901 MakePermissionIDSet(APIPermission::kTab, APIPermission::kFavicon)));
904 // History warning suppresses favicon warning.
905 APIPermissionSet api_permissions;
906 api_permissions.insert(APIPermission::kHistory);
907 URLPatternSet hosts;
908 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI,
909 "chrome://favicon/"));
910 scoped_refptr<PermissionSet> permissions(
911 new PermissionSet(api_permissions, ManifestPermissionSet(),
912 hosts, URLPatternSet()));
913 EXPECT_TRUE(PermissionSetProducesMessage(
914 permissions.get(), Manifest::TYPE_EXTENSION,
915 MakePermissionIDSet(APIPermission::kHistory, APIPermission::kFavicon)));
918 // All sites warning suppresses tabs warning.
919 APIPermissionSet api_permissions;
920 api_permissions.insert(APIPermission::kTab);
921 URLPatternSet hosts;
922 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
923 scoped_refptr<PermissionSet> permissions(new PermissionSet(
924 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
925 EXPECT_TRUE(PermissionSetProducesMessage(
926 permissions.get(), Manifest::TYPE_EXTENSION,
927 MakePermissionIDSet(APIPermission::kHostsAll, APIPermission::kTab)));
930 // All sites warning suppresses topSites warning.
931 APIPermissionSet api_permissions;
932 api_permissions.insert(APIPermission::kTopSites);
933 URLPatternSet hosts;
934 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
935 scoped_refptr<PermissionSet> permissions(new PermissionSet(
936 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
937 EXPECT_TRUE(PermissionSetProducesMessage(
938 permissions.get(), Manifest::TYPE_EXTENSION,
939 MakePermissionIDSet(APIPermission::kHostsAll,
940 APIPermission::kTopSites)));
943 // All sites warning suppresses declarativeWebRequest warning.
944 APIPermissionSet api_permissions;
945 api_permissions.insert(APIPermission::kDeclarativeWebRequest);
946 URLPatternSet hosts;
947 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
948 scoped_refptr<PermissionSet> permissions(new PermissionSet(
949 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
950 EXPECT_TRUE(PermissionSetProducesMessage(
951 permissions.get(), Manifest::TYPE_EXTENSION,
952 MakePermissionIDSet(APIPermission::kHostsAll)));
955 // BrowsingHistory warning suppresses all history read/write warnings.
956 APIPermissionSet api_permissions;
957 api_permissions.insert(APIPermission::kHistory);
958 api_permissions.insert(APIPermission::kTab);
959 api_permissions.insert(APIPermission::kTopSites);
960 api_permissions.insert(APIPermission::kProcesses);
961 api_permissions.insert(APIPermission::kWebNavigation);
962 scoped_refptr<PermissionSet> permissions(
963 new PermissionSet(api_permissions, ManifestPermissionSet(),
964 URLPatternSet(), URLPatternSet()));
965 EXPECT_TRUE(PermissionSetProducesMessage(
966 permissions.get(), Manifest::TYPE_EXTENSION,
967 MakePermissionIDSet(api_permissions)));
970 // Tabs warning suppresses all read-only history warnings.
971 APIPermissionSet api_permissions;
972 api_permissions.insert(APIPermission::kTab);
973 api_permissions.insert(APIPermission::kTopSites);
974 api_permissions.insert(APIPermission::kProcesses);
975 api_permissions.insert(APIPermission::kWebNavigation);
976 scoped_refptr<PermissionSet> permissions(
977 new PermissionSet(api_permissions, ManifestPermissionSet(),
978 URLPatternSet(), URLPatternSet()));
979 EXPECT_TRUE(PermissionSetProducesMessage(
980 permissions.get(), Manifest::TYPE_EXTENSION,
981 MakePermissionIDSet(api_permissions)));
985 TEST(PermissionsTest, AccessToDevicesMessages) {
987 APIPermissionSet api_permissions;
988 api_permissions.insert(APIPermission::kSerial);
989 scoped_refptr<PermissionSet> permissions(
990 new PermissionSet(api_permissions,
991 ManifestPermissionSet(),
992 URLPatternSet(),
993 URLPatternSet()));
994 VerifyOnePermissionMessage(
995 permissions.get(), Manifest::TYPE_EXTENSION,
996 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_SERIAL));
999 // Testing that multiple permissions will show the one message.
1000 APIPermissionSet api_permissions;
1001 api_permissions.insert(APIPermission::kSerial);
1002 api_permissions.insert(APIPermission::kSerial);
1003 scoped_refptr<PermissionSet> permissions(
1004 new PermissionSet(api_permissions,
1005 ManifestPermissionSet(),
1006 URLPatternSet(),
1007 URLPatternSet()));
1008 VerifyOnePermissionMessage(
1009 permissions.get(), Manifest::TYPE_EXTENSION,
1010 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_SERIAL));
1013 scoped_refptr<Extension> extension =
1014 LoadManifest("permissions", "access_to_devices_bluetooth.json");
1015 PermissionSet* set = const_cast<PermissionSet*>(
1016 extension->permissions_data()->active_permissions().get());
1017 VerifyOnePermissionMessage(
1018 set, extension->GetType(),
1019 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH));
1021 // Test Bluetooth and Serial
1022 set->apis_.insert(APIPermission::kSerial);
1023 VerifyOnePermissionMessage(
1024 set, extension->GetType(),
1025 l10n_util::GetStringUTF16(
1026 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_SERIAL));
1030 TEST(PermissionsTest, MergedFileSystemPermissionComparison) {
1031 APIPermissionSet write_api_permissions;
1032 write_api_permissions.insert(APIPermission::kFileSystemWrite);
1033 scoped_refptr<PermissionSet> write_permissions(
1034 new PermissionSet(write_api_permissions, ManifestPermissionSet(),
1035 URLPatternSet(), URLPatternSet()));
1037 APIPermissionSet directory_api_permissions;
1038 directory_api_permissions.insert(APIPermission::kFileSystemDirectory);
1039 scoped_refptr<PermissionSet> directory_permissions(
1040 new PermissionSet(directory_api_permissions, ManifestPermissionSet(),
1041 URLPatternSet(), URLPatternSet()));
1043 APIPermissionSet write_directory_api_permissions;
1044 write_directory_api_permissions.insert(APIPermission::kFileSystemWrite);
1045 write_directory_api_permissions.insert(APIPermission::kFileSystemDirectory);
1046 scoped_refptr<PermissionSet> write_directory_permissions(
1047 new PermissionSet(write_directory_api_permissions,
1048 ManifestPermissionSet(),
1049 URLPatternSet(),
1050 URLPatternSet()));
1052 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1053 EXPECT_FALSE(provider->IsPrivilegeIncrease(write_directory_permissions.get(),
1054 write_permissions.get(),
1055 Manifest::TYPE_PLATFORM_APP));
1056 EXPECT_FALSE(provider->IsPrivilegeIncrease(write_directory_permissions.get(),
1057 directory_permissions.get(),
1058 Manifest::TYPE_PLATFORM_APP));
1059 EXPECT_TRUE(provider->IsPrivilegeIncrease(write_permissions.get(),
1060 directory_permissions.get(),
1061 Manifest::TYPE_PLATFORM_APP));
1062 EXPECT_TRUE(provider->IsPrivilegeIncrease(write_permissions.get(),
1063 write_directory_permissions.get(),
1064 Manifest::TYPE_PLATFORM_APP));
1065 EXPECT_FALSE(provider->IsPrivilegeIncrease(directory_permissions.get(),
1066 write_permissions.get(),
1067 Manifest::TYPE_PLATFORM_APP));
1068 EXPECT_TRUE(provider->IsPrivilegeIncrease(directory_permissions.get(),
1069 write_directory_permissions.get(),
1070 Manifest::TYPE_PLATFORM_APP));
1073 TEST(PermissionsTest, GetWarningMessages_ManyHosts) {
1074 scoped_refptr<Extension> extension;
1075 extension = LoadManifest("permissions", "many-hosts.json");
1076 EXPECT_TRUE(VerifyOnePermissionMessage(
1077 extension->permissions_data(),
1078 "Read and change your data on encrypted.google.com and www.google.com"));
1081 TEST(PermissionsTest, GetWarningMessages_Plugins) {
1082 scoped_refptr<Extension> extension;
1083 extension = LoadManifest("permissions", "plugins.json");
1084 // We don't parse the plugins key on Chrome OS, so it should not ask for any
1085 // permissions.
1086 #if defined(OS_CHROMEOS)
1087 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1088 #else
1089 EXPECT_TRUE(VerifyOnePermissionMessage(
1090 extension->permissions_data(),
1091 "Read and change all your data on your computer and the websites you "
1092 "visit"));
1093 #endif
1096 TEST(PermissionsTest, GetWarningMessages_AudioVideo) {
1097 const std::string kAudio("Use your microphone");
1098 const std::string kVideo("Use your camera");
1099 const std::string kBoth("Use your microphone and camera");
1101 // Both audio and video present.
1102 scoped_refptr<Extension> extension =
1103 LoadManifest("permissions", "audio-video.json");
1104 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1105 PermissionSet* set = const_cast<PermissionSet*>(
1106 extension->permissions_data()->active_permissions().get());
1107 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1108 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1109 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1110 CoalescedPermissionMessages warnings = provider->GetPermissionMessages(
1111 provider->GetAllPermissionIDs(set, extension->GetType()));
1112 size_t combined_index = IndexOf(warnings, kBoth);
1113 size_t combined_size = warnings.size();
1115 // Just audio present.
1116 set->apis_.erase(APIPermission::kVideoCapture);
1117 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1118 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1119 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1120 CoalescedPermissionMessages warnings2 = provider->GetPermissionMessages(
1121 provider->GetAllPermissionIDs(set, extension->GetType()));
1122 EXPECT_EQ(combined_size, warnings2.size());
1123 EXPECT_EQ(combined_index, IndexOf(warnings2, kAudio));
1125 // Just video present.
1126 set->apis_.erase(APIPermission::kAudioCapture);
1127 set->apis_.insert(APIPermission::kVideoCapture);
1128 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1129 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1130 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1131 CoalescedPermissionMessages warnings3 = provider->GetPermissionMessages(
1132 provider->GetAllPermissionIDs(set, extension->GetType()));
1133 EXPECT_EQ(combined_size, warnings3.size());
1134 EXPECT_EQ(combined_index, IndexOf(warnings3, kVideo));
1137 TEST(PermissionsTest, GetWarningMessages_CombinedSessions) {
1139 APIPermissionSet api_permissions;
1140 api_permissions.insert(APIPermission::kTab);
1141 api_permissions.insert(APIPermission::kTopSites);
1142 api_permissions.insert(APIPermission::kProcesses);
1143 api_permissions.insert(APIPermission::kWebNavigation);
1144 api_permissions.insert(APIPermission::kSessions);
1145 scoped_refptr<PermissionSet> permissions(
1146 new PermissionSet(api_permissions, ManifestPermissionSet(),
1147 URLPatternSet(), URLPatternSet()));
1148 EXPECT_TRUE(VerifyOnePermissionMessage(
1149 permissions.get(), Manifest::TYPE_EXTENSION,
1150 l10n_util::GetStringUTF16(
1151 IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ_AND_SESSIONS)));
1154 APIPermissionSet api_permissions;
1155 api_permissions.insert(APIPermission::kHistory);
1156 api_permissions.insert(APIPermission::kTab);
1157 api_permissions.insert(APIPermission::kTopSites);
1158 api_permissions.insert(APIPermission::kProcesses);
1159 api_permissions.insert(APIPermission::kWebNavigation);
1160 api_permissions.insert(APIPermission::kSessions);
1161 scoped_refptr<PermissionSet> permissions(
1162 new PermissionSet(api_permissions, ManifestPermissionSet(),
1163 URLPatternSet(), URLPatternSet()));
1164 EXPECT_TRUE(VerifyOnePermissionMessage(
1165 permissions.get(), Manifest::TYPE_EXTENSION,
1166 l10n_util::GetStringUTF16(
1167 IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE_AND_SESSIONS)));
1171 TEST(PermissionsTest, GetWarningMessages_DeclarativeWebRequest) {
1172 // Test that if the declarativeWebRequest permission is present
1173 // in combination with all hosts permission, then only the warning
1174 // for host permissions is shown, because that covers the use of
1175 // declarativeWebRequest.
1177 // Until Declarative Web Request is in stable, let's make sure it is enabled
1178 // on the current channel.
1179 ScopedCurrentChannel sc(version_info::Channel::CANARY);
1181 // First verify that declarativeWebRequest produces a message when host
1182 // permissions do not cover all hosts.
1183 scoped_refptr<Extension> extension =
1184 LoadManifest("permissions", "web_request_not_all_host_permissions.json");
1185 const PermissionSet* set =
1186 extension->permissions_data()->active_permissions().get();
1187 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(),
1188 "Block parts of web pages"));
1189 EXPECT_FALSE(VerifyHasPermissionMessage(
1190 set, extension->GetType(),
1191 "Read and change all your data on the websites you visit"));
1193 // Now verify that declarativeWebRequest does not produce a message when host
1194 // permissions do cover all hosts.
1195 extension =
1196 LoadManifest("permissions", "web_request_all_host_permissions.json");
1197 set = extension->permissions_data()->active_permissions().get();
1198 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(),
1199 "Block parts of web pages"));
1200 EXPECT_TRUE(VerifyHasPermissionMessage(
1201 set, extension->GetType(),
1202 "Read and change all your data on the websites you visit"));
1205 TEST(PermissionsTest, GetWarningMessages_Serial) {
1206 scoped_refptr<Extension> extension =
1207 LoadManifest("permissions", "serial.json");
1209 EXPECT_TRUE(extension->is_platform_app());
1210 EXPECT_TRUE(
1211 extension->permissions_data()->HasAPIPermission(APIPermission::kSerial));
1212 EXPECT_TRUE(VerifyOnePermissionMessage(extension->permissions_data(),
1213 "Access your serial devices"));
1216 TEST(PermissionsTest, GetWarningMessages_Socket_AnyHost) {
1217 ScopedCurrentChannel channel(version_info::Channel::DEV);
1219 scoped_refptr<Extension> extension =
1220 LoadManifest("permissions", "socket_any_host.json");
1221 EXPECT_TRUE(extension->is_platform_app());
1222 EXPECT_TRUE(
1223 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1224 EXPECT_TRUE(VerifyOnePermissionMessage(
1225 extension->permissions_data(),
1226 "Exchange data with any device on the local network or internet"));
1229 TEST(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) {
1230 ScopedCurrentChannel channel(version_info::Channel::DEV);
1232 scoped_refptr<Extension> extension =
1233 LoadManifest("permissions", "socket_one_domain_two_hostnames.json");
1234 EXPECT_TRUE(extension->is_platform_app());
1235 EXPECT_TRUE(
1236 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1238 // Verify the warnings, including support for unicode characters, the fact
1239 // that domain host warnings come before specific host warnings, and the fact
1240 // that domains and hostnames are in alphabetical order regardless of the
1241 // order in the manifest file.
1242 EXPECT_TRUE(VerifyTwoPermissionMessages(
1243 extension->permissions_data(),
1244 "Exchange data with any device in the domain example.org",
1245 "Exchange data with the devices named: "
1246 "b\xC3\xA5r.example.com foo.example.com",
1247 // "\xC3\xA5" = UTF-8 for lowercase A with ring above
1248 true));
1251 TEST(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) {
1252 ScopedCurrentChannel channel(version_info::Channel::DEV);
1254 scoped_refptr<Extension> extension =
1255 LoadManifest("permissions", "socket_two_domains_one_hostname.json");
1256 EXPECT_TRUE(extension->is_platform_app());
1257 EXPECT_TRUE(
1258 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1260 // Verify the warnings, including the fact that domain host warnings come
1261 // before specific host warnings and the fact that domains and hostnames are
1262 // in alphabetical order regardless of the order in the manifest file.
1263 EXPECT_TRUE(VerifyTwoPermissionMessages(
1264 extension->permissions_data(),
1265 "Exchange data with any device in the domains: "
1266 "example.com foo.example.org",
1267 "Exchange data with the device named bar.example.org", true));
1270 // Since platform apps always use isolated storage, they can't (silently)
1271 // access user data on other domains, so there's no need to prompt about host
1272 // permissions. See crbug.com/255229.
1273 TEST(PermissionsTest, GetWarningMessages_PlatformAppHosts) {
1274 scoped_refptr<Extension> extension;
1276 extension = LoadManifest("permissions", "platform_app_hosts.json");
1277 EXPECT_TRUE(extension->is_platform_app());
1278 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1280 extension = LoadManifest("permissions", "platform_app_all_urls.json");
1281 EXPECT_TRUE(extension->is_platform_app());
1282 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1285 testing::AssertionResult ShowsAllHostsWarning(const std::string& pattern) {
1286 scoped_refptr<Extension> extension =
1287 ExtensionBuilder()
1288 .SetManifest(DictionaryBuilder()
1289 .Set("name", "TLDWildCardTest")
1290 .Set("version", "0.1.0")
1291 .Set("permissions", ListBuilder().Append(pattern))
1292 .Build())
1293 .Build();
1295 return VerifyHasPermissionMessage(
1296 extension->permissions_data(),
1297 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS));
1300 TEST(PermissionsTest, GetWarningMessages_TLDWildcardTreatedAsAllHosts) {
1301 EXPECT_TRUE(ShowsAllHostsWarning("http://*.com/*")); // most popular.
1302 EXPECT_TRUE(ShowsAllHostsWarning("http://*.org/*")); // sanity check.
1303 EXPECT_TRUE(ShowsAllHostsWarning("http://*.co.uk/*")); // eTLD.
1304 EXPECT_TRUE(ShowsAllHostsWarning("http://*.de/*")); // foreign country tld.
1306 // We should still show the normal permissions (i.e., "Can access your data on
1307 // *.rdcronin.com") for things that are not TLDs.
1308 EXPECT_FALSE(ShowsAllHostsWarning("http://*.rdcronin.com/*"));
1310 // Pseudo-TLDs, like appspot.com, should not show all hosts.
1311 EXPECT_FALSE(ShowsAllHostsWarning("http://*.appspot.com/*"));
1313 // Non-TLDs should be likewise exempt.
1314 EXPECT_FALSE(ShowsAllHostsWarning("http://*.notarealtld/*"));
1316 // Our internal checks use "foo", so let's make sure we're not messing
1317 // something up with it.
1318 EXPECT_FALSE(ShowsAllHostsWarning("http://*.foo.com"));
1319 EXPECT_FALSE(ShowsAllHostsWarning("http://foo.com"));
1320 // This will fail if foo becomes a recognized TLD. Which could be soon.
1321 // Update as needed.
1322 EXPECT_FALSE(ShowsAllHostsWarning("http://*.foo"));
1325 TEST(PermissionsTest, GetDistinctHosts) {
1326 URLPatternSet explicit_hosts;
1327 std::set<std::string> expected;
1328 expected.insert("www.foo.com");
1329 expected.insert("www.bar.com");
1330 expected.insert("www.baz.com");
1333 SCOPED_TRACE("no dupes");
1335 // Simple list with no dupes.
1336 explicit_hosts.AddPattern(
1337 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1338 explicit_hosts.AddPattern(
1339 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path"));
1340 explicit_hosts.AddPattern(
1341 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
1342 EXPECT_EQ(expected,
1343 permission_message_util::GetDistinctHosts(
1344 explicit_hosts, true, true));
1348 SCOPED_TRACE("two dupes");
1350 // Add some dupes.
1351 explicit_hosts.AddPattern(
1352 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1353 explicit_hosts.AddPattern(
1354 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
1355 EXPECT_EQ(expected,
1356 permission_message_util::GetDistinctHosts(
1357 explicit_hosts, true, true));
1361 SCOPED_TRACE("schemes differ");
1363 // Add a pattern that differs only by scheme. This should be filtered out.
1364 explicit_hosts.AddPattern(
1365 URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path"));
1366 EXPECT_EQ(expected,
1367 permission_message_util::GetDistinctHosts(
1368 explicit_hosts, true, true));
1372 SCOPED_TRACE("paths differ");
1374 // Add some dupes by path.
1375 explicit_hosts.AddPattern(
1376 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath"));
1377 EXPECT_EQ(expected,
1378 permission_message_util::GetDistinctHosts(
1379 explicit_hosts, true, true));
1383 SCOPED_TRACE("subdomains differ");
1385 // We don't do anything special for subdomains.
1386 explicit_hosts.AddPattern(
1387 URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path"));
1388 explicit_hosts.AddPattern(
1389 URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path"));
1391 expected.insert("monkey.www.bar.com");
1392 expected.insert("bar.com");
1394 EXPECT_EQ(expected,
1395 permission_message_util::GetDistinctHosts(
1396 explicit_hosts, true, true));
1400 SCOPED_TRACE("RCDs differ");
1402 // Now test for RCD uniquing.
1403 explicit_hosts.AddPattern(
1404 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1405 explicit_hosts.AddPattern(
1406 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1407 explicit_hosts.AddPattern(
1408 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.de/path"));
1409 explicit_hosts.AddPattern(
1410 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca.us/path"));
1411 explicit_hosts.AddPattern(
1412 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1413 explicit_hosts.AddPattern(
1414 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com.my/path"));
1416 // This is an unknown RCD, which shouldn't be uniqued out.
1417 explicit_hosts.AddPattern(
1418 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
1419 // But it should only occur once.
1420 explicit_hosts.AddPattern(
1421 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
1423 expected.insert("www.foo.xyzzy");
1425 EXPECT_EQ(expected,
1426 permission_message_util::GetDistinctHosts(
1427 explicit_hosts, true, true));
1431 SCOPED_TRACE("wildcards");
1433 explicit_hosts.AddPattern(
1434 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
1436 expected.insert("*.google.com");
1438 EXPECT_EQ(expected,
1439 permission_message_util::GetDistinctHosts(
1440 explicit_hosts, true, true));
1444 SCOPED_TRACE("scriptable hosts");
1446 APIPermissionSet empty_perms;
1447 explicit_hosts.ClearPatterns();
1448 URLPatternSet scriptable_hosts;
1449 expected.clear();
1451 explicit_hosts.AddPattern(
1452 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
1453 scriptable_hosts.AddPattern(
1454 URLPattern(URLPattern::SCHEME_HTTP, "http://*.example.com/*"));
1456 expected.insert("*.google.com");
1457 expected.insert("*.example.com");
1459 scoped_refptr<PermissionSet> perm_set(new PermissionSet(
1460 empty_perms, ManifestPermissionSet(),
1461 explicit_hosts, scriptable_hosts));
1462 EXPECT_EQ(expected,
1463 permission_message_util::GetDistinctHosts(
1464 perm_set->effective_hosts(), true, true));
1468 // We don't display warnings for file URLs because they are off by default.
1469 SCOPED_TRACE("file urls");
1471 explicit_hosts.ClearPatterns();
1472 expected.clear();
1474 explicit_hosts.AddPattern(
1475 URLPattern(URLPattern::SCHEME_FILE, "file:///*"));
1477 EXPECT_EQ(expected,
1478 permission_message_util::GetDistinctHosts(
1479 explicit_hosts, true, true));
1483 TEST(PermissionsTest, GetDistinctHosts_ComIsBestRcd) {
1484 URLPatternSet explicit_hosts;
1485 explicit_hosts.AddPattern(
1486 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1487 explicit_hosts.AddPattern(
1488 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1489 explicit_hosts.AddPattern(
1490 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1491 explicit_hosts.AddPattern(
1492 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1493 explicit_hosts.AddPattern(
1494 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1495 explicit_hosts.AddPattern(
1496 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1498 std::set<std::string> expected;
1499 expected.insert("www.foo.com");
1500 EXPECT_EQ(expected,
1501 permission_message_util::GetDistinctHosts(
1502 explicit_hosts, true, true));
1505 TEST(PermissionsTest, GetDistinctHosts_NetIs2ndBestRcd) {
1506 URLPatternSet explicit_hosts;
1507 explicit_hosts.AddPattern(
1508 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1509 explicit_hosts.AddPattern(
1510 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1511 explicit_hosts.AddPattern(
1512 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1513 explicit_hosts.AddPattern(
1514 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1515 explicit_hosts.AddPattern(
1516 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1517 // No http://www.foo.com/path
1519 std::set<std::string> expected;
1520 expected.insert("www.foo.net");
1521 EXPECT_EQ(expected,
1522 permission_message_util::GetDistinctHosts(
1523 explicit_hosts, true, true));
1526 TEST(PermissionsTest, GetDistinctHosts_OrgIs3rdBestRcd) {
1527 URLPatternSet explicit_hosts;
1528 explicit_hosts.AddPattern(
1529 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1530 explicit_hosts.AddPattern(
1531 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1532 explicit_hosts.AddPattern(
1533 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1534 // No http://www.foo.net/path
1535 explicit_hosts.AddPattern(
1536 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1537 // No http://www.foo.com/path
1539 std::set<std::string> expected;
1540 expected.insert("www.foo.org");
1541 EXPECT_EQ(expected,
1542 permission_message_util::GetDistinctHosts(
1543 explicit_hosts, true, true));
1546 TEST(PermissionsTest, GetDistinctHosts_FirstInListIs4thBestRcd) {
1547 URLPatternSet explicit_hosts;
1548 explicit_hosts.AddPattern(
1549 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1550 // No http://www.foo.org/path
1551 explicit_hosts.AddPattern(
1552 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1553 // No http://www.foo.net/path
1554 explicit_hosts.AddPattern(
1555 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1556 // No http://www.foo.com/path
1558 std::set<std::string> expected;
1559 expected.insert("www.foo.ca");
1560 EXPECT_EQ(expected,
1561 permission_message_util::GetDistinctHosts(
1562 explicit_hosts, true, true));
1565 TEST(PermissionsTest, IsHostPrivilegeIncrease) {
1566 Manifest::Type type = Manifest::TYPE_EXTENSION;
1567 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1568 ManifestPermissionSet empty_manifest_permissions;
1569 URLPatternSet elist1;
1570 URLPatternSet elist2;
1571 URLPatternSet slist1;
1572 URLPatternSet slist2;
1573 scoped_refptr<PermissionSet> set1;
1574 scoped_refptr<PermissionSet> set2;
1575 APIPermissionSet empty_perms;
1576 elist1.AddPattern(
1577 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
1578 elist1.AddPattern(
1579 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1581 // Test that the host order does not matter.
1582 elist2.AddPattern(
1583 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1584 elist2.AddPattern(
1585 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
1587 set1 = new PermissionSet(empty_perms, empty_manifest_permissions,
1588 elist1, slist1);
1589 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1590 elist2, slist2);
1592 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1593 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1595 // Test that paths are ignored.
1596 elist2.ClearPatterns();
1597 elist2.AddPattern(
1598 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/*"));
1599 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1600 elist2, slist2);
1601 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1602 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1604 // Test that RCDs are ignored.
1605 elist2.ClearPatterns();
1606 elist2.AddPattern(
1607 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/*"));
1608 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1609 elist2, slist2);
1610 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1611 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1613 // Test that subdomain wildcards are handled properly.
1614 elist2.ClearPatterns();
1615 elist2.AddPattern(
1616 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com.hk/*"));
1617 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1618 elist2, slist2);
1619 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1620 // TODO(jstritar): Does not match subdomains properly. http://crbug.com/65337
1621 // EXPECT_FALSE(provider->IsPrivilegeIncrease(set2, set1, type));
1623 // Test that different domains count as different hosts.
1624 elist2.ClearPatterns();
1625 elist2.AddPattern(
1626 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1627 elist2.AddPattern(
1628 URLPattern(URLPattern::SCHEME_HTTP, "http://www.example.org/path"));
1629 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1630 elist2, slist2);
1631 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1632 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1634 // Test that different subdomains count as different hosts.
1635 elist2.ClearPatterns();
1636 elist2.AddPattern(
1637 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*"));
1638 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1639 elist2, slist2);
1640 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1641 EXPECT_TRUE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1643 // Test that platform apps do not have host permissions increases.
1644 type = Manifest::TYPE_PLATFORM_APP;
1645 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1646 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1649 TEST(PermissionsTest, GetAPIsAsStrings) {
1650 APIPermissionSet apis;
1651 URLPatternSet empty_set;
1653 apis.insert(APIPermission::kProxy);
1654 apis.insert(APIPermission::kBackground);
1655 apis.insert(APIPermission::kNotifications);
1656 apis.insert(APIPermission::kTab);
1658 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
1659 apis, ManifestPermissionSet(), empty_set, empty_set);
1660 std::set<std::string> api_names = perm_set->GetAPIsAsStrings();
1662 // The result is correct if it has the same number of elements
1663 // and we can convert it back to the id set.
1664 EXPECT_EQ(4u, api_names.size());
1665 EXPECT_EQ(apis,
1666 PermissionsInfo::GetInstance()->GetAllByName(api_names));
1669 TEST(PermissionsTest, IsEmpty) {
1670 APIPermissionSet empty_apis;
1671 URLPatternSet empty_extent;
1673 scoped_refptr<PermissionSet> empty = new PermissionSet();
1674 EXPECT_TRUE(empty->IsEmpty());
1675 scoped_refptr<PermissionSet> perm_set;
1677 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1678 empty_extent, empty_extent);
1679 EXPECT_TRUE(perm_set->IsEmpty());
1681 APIPermissionSet non_empty_apis;
1682 non_empty_apis.insert(APIPermission::kBackground);
1683 perm_set = new PermissionSet(non_empty_apis, ManifestPermissionSet(),
1684 empty_extent, empty_extent);
1685 EXPECT_FALSE(perm_set->IsEmpty());
1687 // Try non standard host
1688 URLPatternSet non_empty_extent;
1689 AddPattern(&non_empty_extent, "http://www.google.com/*");
1691 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1692 non_empty_extent, empty_extent);
1693 EXPECT_FALSE(perm_set->IsEmpty());
1695 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1696 empty_extent, non_empty_extent);
1697 EXPECT_FALSE(perm_set->IsEmpty());
1700 TEST(PermissionsTest, ImpliedPermissions) {
1701 URLPatternSet empty_extent;
1702 APIPermissionSet apis;
1703 apis.insert(APIPermission::kFileBrowserHandler);
1704 EXPECT_EQ(1U, apis.size());
1706 scoped_refptr<PermissionSet> perm_set;
1707 perm_set = new PermissionSet(apis, ManifestPermissionSet(),
1708 empty_extent, empty_extent);
1709 EXPECT_EQ(2U, perm_set->apis().size());
1712 TEST(PermissionsTest, SyncFileSystemPermission) {
1713 scoped_refptr<Extension> extension = LoadManifest(
1714 "permissions", "sync_file_system.json");
1715 APIPermissionSet apis;
1716 apis.insert(APIPermission::kSyncFileSystem);
1717 EXPECT_TRUE(extension->is_platform_app());
1718 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
1719 APIPermission::kSyncFileSystem));
1720 EXPECT_TRUE(
1721 VerifyOnePermissionMessage(extension->permissions_data(),
1722 "Store data in your Google Drive account"));
1725 // Make sure that we don't crash when we're trying to show the permissions
1726 // even though chrome://thumb (and everything that's not chrome://favicon with
1727 // a chrome:// scheme) is not a valid permission.
1728 // More details here: crbug/246314.
1729 TEST(PermissionsTest, ChromeURLs) {
1730 URLPatternSet allowed_hosts;
1731 allowed_hosts.AddPattern(
1732 URLPattern(URLPattern::SCHEME_ALL, "http://www.google.com/"));
1733 allowed_hosts.AddPattern(
1734 URLPattern(URLPattern::SCHEME_ALL, "chrome://favicon/"));
1735 allowed_hosts.AddPattern(
1736 URLPattern(URLPattern::SCHEME_ALL, "chrome://thumb/"));
1737 scoped_refptr<PermissionSet> permissions(
1738 new PermissionSet(APIPermissionSet(), ManifestPermissionSet(),
1739 allowed_hosts, URLPatternSet()));
1740 PermissionMessageProvider::Get()->GetPermissionMessages(
1741 PermissionMessageProvider::Get()->GetAllPermissionIDs(
1742 permissions.get(), Manifest::TYPE_EXTENSION));
1745 TEST(PermissionsTest, IsPrivilegeIncrease_DeclarativeWebRequest) {
1746 scoped_refptr<Extension> extension(
1747 LoadManifest("permissions", "permissions_all_urls.json"));
1748 scoped_refptr<const PermissionSet> permissions(
1749 extension->permissions_data()->active_permissions());
1751 scoped_refptr<Extension> extension_dwr(
1752 LoadManifest("permissions", "web_request_all_host_permissions.json"));
1753 scoped_refptr<const PermissionSet> permissions_dwr(
1754 extension_dwr->permissions_data()->active_permissions());
1756 EXPECT_FALSE(PermissionMessageProvider::Get()->
1757 IsPrivilegeIncrease(permissions.get(),
1758 permissions_dwr.get(),
1759 extension->GetType()));
1762 } // namespace extensions