Extensions: Switch to new permission message system, part IV
[chromium-blink-merge.git] / chrome / common / extensions / permissions / permission_set_unittest.cc
blob7d8d74847e1a4c295686b1d32fadba39397989ac
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 "extensions/common/error_utils.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/extension_builder.h"
20 #include "extensions/common/permissions/permission_message_provider.h"
21 #include "extensions/common/permissions/permission_message_test_util.h"
22 #include "extensions/common/permissions/permission_message_util.h"
23 #include "extensions/common/permissions/permission_set.h"
24 #include "extensions/common/permissions/permissions_data.h"
25 #include "extensions/common/permissions/permissions_info.h"
26 #include "extensions/common/permissions/socket_permission.h"
27 #include "extensions/common/value_builder.h"
28 #include "extensions/strings/grit/extensions_strings.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30 #include "ui/base/l10n/l10n_util.h"
32 using extension_test_util::LoadManifest;
34 namespace extensions {
36 namespace {
38 static void AddPattern(URLPatternSet* extent, const std::string& pattern) {
39 int schemes = URLPattern::SCHEME_ALL;
40 extent->AddPattern(URLPattern(schemes, pattern));
43 size_t IndexOf(const PermissionMessageStrings& warnings,
44 const std::string& warning) {
45 for (size_t i = 0; i < warnings.size(); ++i) {
46 if (warnings[i].message == base::ASCIIToUTF16(warning))
47 return i;
50 return warnings.size();
53 PermissionIDSet MakePermissionIDSet(APIPermission::ID id1,
54 APIPermission::ID id2) {
55 PermissionIDSet set;
56 set.insert(id1);
57 set.insert(id2);
58 return set;
61 PermissionIDSet MakePermissionIDSet(const APIPermissionSet& permissions) {
62 PermissionIDSet set;
63 for (const APIPermission* permission : permissions)
64 set.insert(permission->id());
65 return set;
68 std::string LegacyPermissionIDsToString(const PermissionMessageIDs& ids) {
69 std::vector<std::string> strs;
70 for (const PermissionMessage::ID& id : ids)
71 strs.push_back(base::IntToString(id));
72 return base::StringPrintf("[ %s ]", JoinString(strs, ", ").c_str());
75 std::string PermissionIDsToString(const PermissionIDSet& ids) {
76 std::vector<std::string> strs;
77 for (const PermissionID& id : ids)
78 strs.push_back(base::IntToString(id.id()));
79 return base::StringPrintf("[ %s ]", JoinString(strs, ", ").c_str());
82 std::string CoalescedPermissionIDsToString(
83 const CoalescedPermissionMessages& msgs) {
84 std::vector<std::string> strs;
85 for (const CoalescedPermissionMessage& msg : msgs)
86 strs.push_back(PermissionIDsToString(msg.permissions()));
87 return JoinString(strs, " ");
90 // Check that the given |permissions| produce a single warning message,
91 // identified by |expected_legacy_id| in the old system, and by the set of
92 // |expected_ids| in the new system.
93 testing::AssertionResult PermissionSetProducesMessage(
94 const PermissionSet* permissions,
95 Manifest::Type extension_type,
96 PermissionMessage::ID expected_legacy_id,
97 const PermissionIDSet& expected_ids) {
98 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
99 PermissionMessageIDs legacy_ids =
100 provider->GetLegacyPermissionMessageIDs(permissions, extension_type);
101 if (legacy_ids.size() != 1 || expected_legacy_id != legacy_ids[0]) {
102 return testing::AssertionFailure()
103 << "Expected single legacy permission ID " << expected_legacy_id
104 << " but got " << LegacyPermissionIDsToString(legacy_ids);
107 CoalescedPermissionMessages msgs = provider->GetCoalescedPermissionMessages(
108 provider->GetAllPermissionIDs(permissions, extension_type));
109 if (msgs.size() != 1) {
110 return testing::AssertionFailure()
111 << "Expected single permission message with IDs "
112 << PermissionIDsToString(expected_ids) << " but got " << msgs.size()
113 << " messages: " << CoalescedPermissionIDsToString(msgs);
115 if (!msgs.front().permissions().Equals(expected_ids)) {
116 return testing::AssertionFailure()
117 << "Expected permission IDs " << PermissionIDsToString(expected_ids)
118 << " but got " << PermissionIDsToString(msgs.front().permissions());
121 return testing::AssertionSuccess();
124 } // namespace
126 // Tests GetByID.
127 TEST(PermissionsTest, GetByID) {
128 PermissionsInfo* info = PermissionsInfo::GetInstance();
129 APIPermissionSet apis = info->GetAll();
130 for (APIPermissionSet::const_iterator i = apis.begin();
131 i != apis.end(); ++i) {
132 EXPECT_EQ(i->id(), i->info()->id());
136 // Tests that GetByName works with normal permission names and aliases.
137 TEST(PermissionsTest, GetByName) {
138 PermissionsInfo* info = PermissionsInfo::GetInstance();
139 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
140 EXPECT_EQ(APIPermission::kManagement,
141 info->GetByName("management")->id());
142 EXPECT_FALSE(info->GetByName("alsdkfjasldkfj"));
145 TEST(PermissionsTest, GetAll) {
146 size_t count = 0;
147 PermissionsInfo* info = PermissionsInfo::GetInstance();
148 APIPermissionSet apis = info->GetAll();
149 for (APIPermissionSet::const_iterator api = apis.begin();
150 api != apis.end(); ++api) {
151 // Make sure only the valid permission IDs get returned.
152 EXPECT_NE(APIPermission::kInvalid, api->id());
153 EXPECT_NE(APIPermission::kUnknown, api->id());
154 count++;
156 EXPECT_EQ(count, info->get_permission_count());
159 TEST(PermissionsTest, GetAllByName) {
160 std::set<std::string> names;
161 names.insert("background");
162 names.insert("management");
164 // This is an alias of kTab
165 names.insert("windows");
167 // This unknown name should get dropped.
168 names.insert("sdlkfjasdlkfj");
170 APIPermissionSet expected;
171 expected.insert(APIPermission::kBackground);
172 expected.insert(APIPermission::kManagement);
173 expected.insert(APIPermission::kTab);
175 EXPECT_EQ(expected,
176 PermissionsInfo::GetInstance()->GetAllByName(names));
179 // Tests that the aliases are properly mapped.
180 TEST(PermissionsTest, Aliases) {
181 PermissionsInfo* info = PermissionsInfo::GetInstance();
182 // tabs: tabs, windows
183 std::string tabs_name = "tabs";
184 EXPECT_EQ(tabs_name, info->GetByID(APIPermission::kTab)->name());
185 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
186 EXPECT_EQ(APIPermission::kTab, info->GetByName("windows")->id());
188 // unlimitedStorage: unlimitedStorage, unlimited_storage
189 std::string storage_name = "unlimitedStorage";
190 EXPECT_EQ(storage_name, info->GetByID(
191 APIPermission::kUnlimitedStorage)->name());
192 EXPECT_EQ(APIPermission::kUnlimitedStorage,
193 info->GetByName("unlimitedStorage")->id());
194 EXPECT_EQ(APIPermission::kUnlimitedStorage,
195 info->GetByName("unlimited_storage")->id());
198 TEST(PermissionsTest, EffectiveHostPermissions) {
199 scoped_refptr<Extension> extension;
200 scoped_refptr<const PermissionSet> permissions;
202 extension = LoadManifest("effective_host_permissions", "empty.json");
203 permissions = extension->permissions_data()->active_permissions();
204 EXPECT_EQ(0u,
205 extension->permissions_data()
206 ->GetEffectiveHostPermissions()
207 .patterns()
208 .size());
209 EXPECT_FALSE(
210 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
211 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
213 extension = LoadManifest("effective_host_permissions", "one_host.json");
214 permissions = extension->permissions_data()->active_permissions();
215 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
216 GURL("http://www.google.com")));
217 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(
218 GURL("https://www.google.com")));
219 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
221 extension = LoadManifest("effective_host_permissions",
222 "one_host_wildcard.json");
223 permissions = extension->permissions_data()->active_permissions();
224 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
225 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
226 GURL("http://foo.google.com")));
227 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
229 extension = LoadManifest("effective_host_permissions", "two_hosts.json");
230 permissions = extension->permissions_data()->active_permissions();
231 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
232 GURL("http://www.google.com")));
233 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
234 GURL("http://www.reddit.com")));
235 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
237 extension = LoadManifest("effective_host_permissions",
238 "https_not_considered.json");
239 permissions = extension->permissions_data()->active_permissions();
240 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
241 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://google.com")));
242 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
244 extension = LoadManifest("effective_host_permissions",
245 "two_content_scripts.json");
246 permissions = extension->permissions_data()->active_permissions();
247 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://google.com")));
248 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
249 GURL("http://www.reddit.com")));
250 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(
251 GURL("http://news.ycombinator.com")));
252 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
254 extension = LoadManifest("effective_host_permissions", "all_hosts.json");
255 permissions = extension->permissions_data()->active_permissions();
256 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
257 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("https://test/")));
258 EXPECT_TRUE(
259 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
260 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
262 extension = LoadManifest("effective_host_permissions", "all_hosts2.json");
263 permissions = extension->permissions_data()->active_permissions();
264 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
265 EXPECT_TRUE(
266 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
267 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
269 extension = LoadManifest("effective_host_permissions", "all_hosts3.json");
270 permissions = extension->permissions_data()->active_permissions();
271 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
272 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://test/")));
273 EXPECT_TRUE(
274 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
275 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
278 TEST(PermissionsTest, ExplicitAccessToOrigin) {
279 APIPermissionSet apis;
280 ManifestPermissionSet manifest_permissions;
281 URLPatternSet explicit_hosts;
282 URLPatternSet scriptable_hosts;
284 AddPattern(&explicit_hosts, "http://*.google.com/*");
285 // The explicit host paths should get set to /*.
286 AddPattern(&explicit_hosts, "http://www.example.com/a/particular/path/*");
288 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
289 apis, manifest_permissions, explicit_hosts, scriptable_hosts);
290 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
291 GURL("http://www.google.com/")));
292 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
293 GURL("http://test.google.com/")));
294 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
295 GURL("http://www.example.com")));
296 ASSERT_TRUE(perm_set->HasEffectiveAccessToURL(
297 GURL("http://www.example.com")));
298 ASSERT_FALSE(perm_set->HasExplicitAccessToOrigin(
299 GURL("http://test.example.com")));
302 TEST(PermissionsTest, CreateUnion) {
303 APIPermission* permission = NULL;
305 ManifestPermissionSet manifest_permissions;
306 APIPermissionSet apis1;
307 APIPermissionSet apis2;
308 APIPermissionSet expected_apis;
310 URLPatternSet explicit_hosts1;
311 URLPatternSet explicit_hosts2;
312 URLPatternSet expected_explicit_hosts;
314 URLPatternSet scriptable_hosts1;
315 URLPatternSet scriptable_hosts2;
316 URLPatternSet expected_scriptable_hosts;
318 URLPatternSet effective_hosts;
320 scoped_refptr<PermissionSet> set1;
321 scoped_refptr<PermissionSet> set2;
322 scoped_refptr<PermissionSet> union_set;
324 const APIPermissionInfo* permission_info =
325 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
326 permission = permission_info->CreateAPIPermission();
328 scoped_ptr<base::ListValue> value(new base::ListValue());
329 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
330 value->Append(new base::StringValue("udp-bind::8080"));
331 value->Append(new base::StringValue("udp-send-to::8888"));
332 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
335 // Union with an empty set.
336 apis1.insert(APIPermission::kTab);
337 apis1.insert(APIPermission::kBackground);
338 apis1.insert(permission->Clone());
339 expected_apis.insert(APIPermission::kTab);
340 expected_apis.insert(APIPermission::kBackground);
341 expected_apis.insert(permission);
343 AddPattern(&explicit_hosts1, "http://*.google.com/*");
344 AddPattern(&expected_explicit_hosts, "http://*.google.com/*");
345 AddPattern(&effective_hosts, "http://*.google.com/*");
347 set1 = new PermissionSet(apis1, manifest_permissions,
348 explicit_hosts1, scriptable_hosts1);
349 set2 = new PermissionSet(apis2, manifest_permissions,
350 explicit_hosts2, scriptable_hosts2);
351 union_set = PermissionSet::CreateUnion(set1.get(), set2.get());
352 EXPECT_TRUE(set1->Contains(*set2.get()));
353 EXPECT_TRUE(set1->Contains(*union_set.get()));
354 EXPECT_FALSE(set2->Contains(*set1.get()));
355 EXPECT_FALSE(set2->Contains(*union_set.get()));
356 EXPECT_TRUE(union_set->Contains(*set1.get()));
357 EXPECT_TRUE(union_set->Contains(*set2.get()));
359 EXPECT_FALSE(union_set->HasEffectiveFullAccess());
360 EXPECT_EQ(expected_apis, union_set->apis());
361 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
362 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
363 EXPECT_EQ(expected_explicit_hosts, union_set->effective_hosts());
365 // Now use a real second set.
366 apis2.insert(APIPermission::kTab);
367 apis2.insert(APIPermission::kProxy);
368 apis2.insert(APIPermission::kClipboardWrite);
369 apis2.insert(APIPermission::kPlugin);
371 permission = permission_info->CreateAPIPermission();
373 scoped_ptr<base::ListValue> value(new base::ListValue());
374 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
375 value->Append(new base::StringValue("udp-send-to::8899"));
376 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
378 apis2.insert(permission);
380 expected_apis.insert(APIPermission::kTab);
381 expected_apis.insert(APIPermission::kProxy);
382 expected_apis.insert(APIPermission::kClipboardWrite);
383 expected_apis.insert(APIPermission::kPlugin);
385 permission = permission_info->CreateAPIPermission();
387 scoped_ptr<base::ListValue> value(new base::ListValue());
388 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
389 value->Append(new base::StringValue("udp-bind::8080"));
390 value->Append(new base::StringValue("udp-send-to::8888"));
391 value->Append(new base::StringValue("udp-send-to::8899"));
392 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
394 // Insert a new permission socket permisssion which will replace the old one.
395 expected_apis.insert(permission);
397 AddPattern(&explicit_hosts2, "http://*.example.com/*");
398 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
399 AddPattern(&expected_explicit_hosts, "http://*.example.com/*");
400 AddPattern(&expected_scriptable_hosts, "http://*.google.com/*");
402 URLPatternSet::CreateUnion(
403 explicit_hosts2, scriptable_hosts2, &effective_hosts);
405 set2 = new PermissionSet(apis2, manifest_permissions,
406 explicit_hosts2, scriptable_hosts2);
407 union_set = PermissionSet::CreateUnion(set1.get(), set2.get());
409 EXPECT_FALSE(set1->Contains(*set2.get()));
410 EXPECT_FALSE(set1->Contains(*union_set.get()));
411 EXPECT_FALSE(set2->Contains(*set1.get()));
412 EXPECT_FALSE(set2->Contains(*union_set.get()));
413 EXPECT_TRUE(union_set->Contains(*set1.get()));
414 EXPECT_TRUE(union_set->Contains(*set2.get()));
416 EXPECT_TRUE(union_set->HasEffectiveFullAccess());
417 EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts());
418 EXPECT_EQ(expected_apis, union_set->apis());
419 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
420 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
421 EXPECT_EQ(effective_hosts, union_set->effective_hosts());
424 TEST(PermissionsTest, CreateIntersection) {
425 APIPermission* permission = NULL;
427 ManifestPermissionSet manifest_permissions;
428 APIPermissionSet apis1;
429 APIPermissionSet apis2;
430 APIPermissionSet expected_apis;
432 URLPatternSet explicit_hosts1;
433 URLPatternSet explicit_hosts2;
434 URLPatternSet expected_explicit_hosts;
436 URLPatternSet scriptable_hosts1;
437 URLPatternSet scriptable_hosts2;
438 URLPatternSet expected_scriptable_hosts;
440 URLPatternSet effective_hosts;
442 scoped_refptr<PermissionSet> set1;
443 scoped_refptr<PermissionSet> set2;
444 scoped_refptr<PermissionSet> new_set;
446 const APIPermissionInfo* permission_info =
447 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
449 // Intersection with an empty set.
450 apis1.insert(APIPermission::kTab);
451 apis1.insert(APIPermission::kBackground);
452 permission = permission_info->CreateAPIPermission();
454 scoped_ptr<base::ListValue> value(new base::ListValue());
455 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
456 value->Append(new base::StringValue("udp-bind::8080"));
457 value->Append(new base::StringValue("udp-send-to::8888"));
458 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
460 apis1.insert(permission);
462 AddPattern(&explicit_hosts1, "http://*.google.com/*");
463 AddPattern(&scriptable_hosts1, "http://www.reddit.com/*");
465 set1 = new PermissionSet(apis1, manifest_permissions,
466 explicit_hosts1, scriptable_hosts1);
467 set2 = new PermissionSet(apis2, manifest_permissions,
468 explicit_hosts2, scriptable_hosts2);
469 new_set = PermissionSet::CreateIntersection(set1.get(), set2.get());
470 EXPECT_TRUE(set1->Contains(*new_set.get()));
471 EXPECT_TRUE(set2->Contains(*new_set.get()));
472 EXPECT_TRUE(set1->Contains(*set2.get()));
473 EXPECT_FALSE(set2->Contains(*set1.get()));
474 EXPECT_FALSE(new_set->Contains(*set1.get()));
475 EXPECT_TRUE(new_set->Contains(*set2.get()));
477 EXPECT_TRUE(new_set->IsEmpty());
478 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
479 EXPECT_EQ(expected_apis, new_set->apis());
480 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
481 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
482 EXPECT_EQ(expected_explicit_hosts, new_set->effective_hosts());
484 // Now use a real second set.
485 apis2.insert(APIPermission::kTab);
486 apis2.insert(APIPermission::kProxy);
487 apis2.insert(APIPermission::kClipboardWrite);
488 apis2.insert(APIPermission::kPlugin);
489 permission = permission_info->CreateAPIPermission();
491 scoped_ptr<base::ListValue> value(new base::ListValue());
492 value->Append(new base::StringValue("udp-bind::8080"));
493 value->Append(new base::StringValue("udp-send-to::8888"));
494 value->Append(new base::StringValue("udp-send-to::8899"));
495 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
497 apis2.insert(permission);
499 expected_apis.insert(APIPermission::kTab);
500 permission = permission_info->CreateAPIPermission();
502 scoped_ptr<base::ListValue> value(new base::ListValue());
503 value->Append(new base::StringValue("udp-bind::8080"));
504 value->Append(new base::StringValue("udp-send-to::8888"));
505 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
507 expected_apis.insert(permission);
509 AddPattern(&explicit_hosts2, "http://*.example.com/*");
510 AddPattern(&explicit_hosts2, "http://*.google.com/*");
511 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
512 AddPattern(&expected_explicit_hosts, "http://*.google.com/*");
514 effective_hosts.ClearPatterns();
515 AddPattern(&effective_hosts, "http://*.google.com/*");
517 set2 = new PermissionSet(apis2, manifest_permissions,
518 explicit_hosts2, scriptable_hosts2);
519 new_set = PermissionSet::CreateIntersection(set1.get(), set2.get());
521 EXPECT_TRUE(set1->Contains(*new_set.get()));
522 EXPECT_TRUE(set2->Contains(*new_set.get()));
523 EXPECT_FALSE(set1->Contains(*set2.get()));
524 EXPECT_FALSE(set2->Contains(*set1.get()));
525 EXPECT_FALSE(new_set->Contains(*set1.get()));
526 EXPECT_FALSE(new_set->Contains(*set2.get()));
528 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
529 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts());
530 EXPECT_EQ(expected_apis, new_set->apis());
531 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
532 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
533 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
536 TEST(PermissionsTest, CreateDifference) {
537 APIPermission* permission = NULL;
539 ManifestPermissionSet manifest_permissions;
540 APIPermissionSet apis1;
541 APIPermissionSet apis2;
542 APIPermissionSet expected_apis;
544 URLPatternSet explicit_hosts1;
545 URLPatternSet explicit_hosts2;
546 URLPatternSet expected_explicit_hosts;
548 URLPatternSet scriptable_hosts1;
549 URLPatternSet scriptable_hosts2;
550 URLPatternSet expected_scriptable_hosts;
552 URLPatternSet effective_hosts;
554 scoped_refptr<PermissionSet> set1;
555 scoped_refptr<PermissionSet> set2;
556 scoped_refptr<PermissionSet> new_set;
558 const APIPermissionInfo* permission_info =
559 PermissionsInfo::GetInstance()->GetByID(APIPermission::kSocket);
561 // Difference with an empty set.
562 apis1.insert(APIPermission::kTab);
563 apis1.insert(APIPermission::kBackground);
564 permission = permission_info->CreateAPIPermission();
566 scoped_ptr<base::ListValue> value(new base::ListValue());
567 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
568 value->Append(new base::StringValue("udp-bind::8080"));
569 value->Append(new base::StringValue("udp-send-to::8888"));
570 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
572 apis1.insert(permission);
574 AddPattern(&explicit_hosts1, "http://*.google.com/*");
575 AddPattern(&scriptable_hosts1, "http://www.reddit.com/*");
577 set1 = new PermissionSet(apis1, manifest_permissions,
578 explicit_hosts1, scriptable_hosts1);
579 set2 = new PermissionSet(apis2, manifest_permissions,
580 explicit_hosts2, scriptable_hosts2);
581 new_set = PermissionSet::CreateDifference(set1.get(), set2.get());
582 EXPECT_EQ(*set1.get(), *new_set.get());
584 // Now use a real second set.
585 apis2.insert(APIPermission::kTab);
586 apis2.insert(APIPermission::kProxy);
587 apis2.insert(APIPermission::kClipboardWrite);
588 apis2.insert(APIPermission::kPlugin);
589 permission = permission_info->CreateAPIPermission();
591 scoped_ptr<base::ListValue> value(new base::ListValue());
592 value->Append(new base::StringValue("tcp-connect:*.example.com:80"));
593 value->Append(new base::StringValue("udp-send-to::8899"));
594 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
596 apis2.insert(permission);
598 expected_apis.insert(APIPermission::kBackground);
599 permission = permission_info->CreateAPIPermission();
601 scoped_ptr<base::ListValue> value(new base::ListValue());
602 value->Append(new base::StringValue("udp-bind::8080"));
603 value->Append(new base::StringValue("udp-send-to::8888"));
604 ASSERT_TRUE(permission->FromValue(value.get(), NULL, NULL));
606 expected_apis.insert(permission);
608 AddPattern(&explicit_hosts2, "http://*.example.com/*");
609 AddPattern(&explicit_hosts2, "http://*.google.com/*");
610 AddPattern(&scriptable_hosts2, "http://*.google.com/*");
611 AddPattern(&expected_scriptable_hosts, "http://www.reddit.com/*");
613 effective_hosts.ClearPatterns();
614 AddPattern(&effective_hosts, "http://www.reddit.com/*");
616 set2 = new PermissionSet(apis2, manifest_permissions,
617 explicit_hosts2, scriptable_hosts2);
618 new_set = PermissionSet::CreateDifference(set1.get(), set2.get());
620 EXPECT_TRUE(set1->Contains(*new_set.get()));
621 EXPECT_FALSE(set2->Contains(*new_set.get()));
623 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
624 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts());
625 EXPECT_EQ(expected_apis, new_set->apis());
626 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
627 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
628 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
630 // |set3| = |set1| - |set2| --> |set3| intersect |set2| == empty_set
631 set1 = PermissionSet::CreateIntersection(new_set.get(), set2.get());
632 EXPECT_TRUE(set1->IsEmpty());
635 TEST(PermissionsTest, IsPrivilegeIncrease) {
636 const struct {
637 const char* base_name;
638 bool expect_increase;
639 } kTests[] = {
640 { "allhosts1", false }, // all -> all
641 { "allhosts2", false }, // all -> one
642 { "allhosts3", true }, // one -> all
643 { "hosts1", false }, // http://a,http://b -> http://a,http://b
644 { "hosts2", true }, // http://a,http://b -> https://a,http://*.b
645 { "hosts3", false }, // http://a,http://b -> http://a
646 { "hosts4", true }, // http://a -> http://a,http://b
647 { "hosts5", false }, // http://a,b,c -> http://a,b,c + https://a,b,c
648 { "hosts6", false }, // http://a.com -> http://a.com + http://a.co.uk
649 { "permissions1", false }, // tabs -> tabs
650 { "permissions2", true }, // tabs -> tabs,bookmarks
651 { "permissions3", true }, // http://a -> http://a,tabs
652 { "permissions5", true }, // bookmarks -> bookmarks,history
653 { "equivalent_warnings", false }, // tabs --> tabs, webNavigation
654 #if !defined(OS_CHROMEOS) // plugins aren't allowed in ChromeOS
655 { "permissions4", false }, // plugin -> plugin,tabs
656 { "plugin1", false }, // plugin -> plugin
657 { "plugin2", false }, // plugin -> none
658 { "plugin3", true }, // none -> plugin
659 #endif
660 { "storage", false }, // none -> storage
661 { "notifications", false }, // none -> notifications
662 { "platformapp1", false }, // host permissions for platform apps
663 { "platformapp2", true }, // API permissions for platform apps
664 { "media_galleries1", true }, // all -> read|all
665 { "media_galleries2", true }, // read|all -> read|delete|copyTo|all
666 { "media_galleries3", true }, // all -> read|delete|all
667 { "media_galleries4", false }, // read|all -> all
668 { "media_galleries5", false }, // read|copyTo|delete|all -> read|all
669 { "media_galleries6", false }, // read|all -> read|all
670 { "media_galleries7", true }, // read|delete|all -> read|copyTo|delete|all
671 { "sockets1", true }, // none -> tcp:*:*
672 { "sockets2", false }, // tcp:*:* -> tcp:*:*
673 { "sockets3", true }, // tcp:a.com:80 -> tcp:*:*
676 for (size_t i = 0; i < arraysize(kTests); ++i) {
677 scoped_refptr<Extension> old_extension(
678 LoadManifest("allow_silent_upgrade",
679 std::string(kTests[i].base_name) + "_old.json"));
680 scoped_refptr<Extension> new_extension(
681 LoadManifest("allow_silent_upgrade",
682 std::string(kTests[i].base_name) + "_new.json"));
684 EXPECT_TRUE(new_extension.get()) << kTests[i].base_name << "_new.json";
685 if (!new_extension.get())
686 continue;
688 scoped_refptr<const PermissionSet> old_p(
689 old_extension->permissions_data()->active_permissions());
690 scoped_refptr<const PermissionSet> new_p(
691 new_extension->permissions_data()->active_permissions());
692 Manifest::Type extension_type = old_extension->GetType();
694 bool increased = PermissionMessageProvider::Get()->IsPrivilegeIncrease(
695 old_p.get(), new_p.get(), extension_type);
696 EXPECT_EQ(kTests[i].expect_increase, increased) << kTests[i].base_name;
700 TEST(PermissionsTest, PermissionMessages) {
701 // Ensure that all permissions that needs to show install UI actually have
702 // strings associated with them.
703 APIPermissionSet skip;
705 // These are considered "nuisance" or "trivial" permissions that don't need
706 // a prompt.
707 skip.insert(APIPermission::kActiveTab);
708 skip.insert(APIPermission::kAlarms);
709 skip.insert(APIPermission::kAlphaEnabled);
710 skip.insert(APIPermission::kAlwaysOnTopWindows);
711 skip.insert(APIPermission::kAppView);
712 skip.insert(APIPermission::kAudio);
713 skip.insert(APIPermission::kAudioModem);
714 skip.insert(APIPermission::kBrowsingData);
715 skip.insert(APIPermission::kCastStreaming);
716 skip.insert(APIPermission::kCommandsAccessibility);
717 skip.insert(APIPermission::kContextMenus);
718 skip.insert(APIPermission::kCryptotokenPrivate);
719 skip.insert(APIPermission::kCopresencePrivate);
720 skip.insert(APIPermission::kDesktopCapturePrivate);
721 skip.insert(APIPermission::kDiagnostics);
722 skip.insert(APIPermission::kDns);
723 skip.insert(APIPermission::kDownloadsShelf);
724 skip.insert(APIPermission::kEmbeddedExtensionOptions);
725 skip.insert(APIPermission::kExtensionView);
726 skip.insert(APIPermission::kFontSettings);
727 skip.insert(APIPermission::kFullscreen);
728 skip.insert(APIPermission::kGcm);
729 skip.insert(APIPermission::kIdle);
730 skip.insert(APIPermission::kImeWindowEnabled);
731 skip.insert(APIPermission::kInlineInstallPrivate);
732 skip.insert(APIPermission::kIdltest);
733 skip.insert(APIPermission::kLogPrivate);
734 skip.insert(APIPermission::kNotifications);
735 skip.insert(APIPermission::kNotificationProvider);
736 skip.insert(APIPermission::kOverrideEscFullscreen);
737 skip.insert(APIPermission::kPointerLock);
738 skip.insert(APIPermission::kPower);
739 skip.insert(APIPermission::kPrinterProvider);
740 skip.insert(APIPermission::kSessions);
741 skip.insert(APIPermission::kStorage);
742 skip.insert(APIPermission::kSystemCpu);
743 skip.insert(APIPermission::kSystemDisplay);
744 skip.insert(APIPermission::kSystemMemory);
745 skip.insert(APIPermission::kSystemNetwork);
746 skip.insert(APIPermission::kSystemStorage);
747 skip.insert(APIPermission::kTts);
748 skip.insert(APIPermission::kUnlimitedStorage);
749 skip.insert(APIPermission::kWebcamPrivate);
750 skip.insert(APIPermission::kWebView);
751 skip.insert(APIPermission::kWindowShape);
753 // These permissions are restricted to extensions force-installed by policy
754 // and don't require a prompt, i.e. they're restricted to location 'policy'.
755 skip.insert(APIPermission::kEnterprisePlatformKeys);
757 // TODO(erikkay) add a string for this permission.
758 skip.insert(APIPermission::kBackground);
760 skip.insert(APIPermission::kClipboardWrite);
762 // The cookie permission does nothing unless you have associated host
763 // permissions.
764 skip.insert(APIPermission::kCookie);
766 // These are warned as part of host permission checks.
767 skip.insert(APIPermission::kDataReductionProxy);
768 skip.insert(APIPermission::kDeclarativeContent);
769 skip.insert(APIPermission::kPageCapture);
770 skip.insert(APIPermission::kProxy);
771 skip.insert(APIPermission::kTabCapture);
772 skip.insert(APIPermission::kWebRequest);
773 skip.insert(APIPermission::kWebRequestBlocking);
775 // This permission requires explicit user action (context menu handler)
776 // so we won't prompt for it for now.
777 skip.insert(APIPermission::kFileBrowserHandler);
779 // These permissions require explicit user action (configuration dialog)
780 // so we don't prompt for them at install time.
781 skip.insert(APIPermission::kMediaGalleries);
783 // If you've turned on the experimental command-line flag, we don't need
784 // to warn you further.
785 skip.insert(APIPermission::kExperimental);
787 // The Identity API has its own server-driven permission prompts.
788 skip.insert(APIPermission::kIdentity);
790 // These are private.
791 skip.insert(APIPermission::kAccessibilityPrivate);
792 skip.insert(APIPermission::kAutoTestPrivate);
793 skip.insert(APIPermission::kBookmarkManagerPrivate);
794 skip.insert(APIPermission::kBrailleDisplayPrivate);
795 skip.insert(APIPermission::kCast);
796 skip.insert(APIPermission::kCastStreaming);
797 skip.insert(APIPermission::kChromeosInfoPrivate);
798 skip.insert(APIPermission::kCloudPrintPrivate);
799 skip.insert(APIPermission::kCommandLinePrivate);
800 skip.insert(APIPermission::kDeveloperPrivate);
801 skip.insert(APIPermission::kDial);
802 skip.insert(APIPermission::kDownloadsInternal);
803 skip.insert(APIPermission::kEasyUnlockPrivate);
804 skip.insert(APIPermission::kEchoPrivate);
805 skip.insert(APIPermission::kEnterprisePlatformKeysPrivate);
806 skip.insert(APIPermission::kFeedbackPrivate);
807 skip.insert(APIPermission::kFileBrowserHandlerInternal);
808 skip.insert(APIPermission::kFileManagerPrivate);
809 skip.insert(APIPermission::kFirstRunPrivate);
810 skip.insert(APIPermission::kGcdPrivate);
811 skip.insert(APIPermission::kHotwordPrivate);
812 skip.insert(APIPermission::kIdentityPrivate);
813 skip.insert(APIPermission::kInputMethodPrivate);
814 skip.insert(APIPermission::kMediaPlayerPrivate);
815 skip.insert(APIPermission::kMediaRouterPrivate);
816 skip.insert(APIPermission::kMetricsPrivate);
817 skip.insert(APIPermission::kPreferencesPrivate);
818 skip.insert(APIPermission::kPrincipalsPrivate);
819 skip.insert(APIPermission::kImageWriterPrivate);
820 skip.insert(APIPermission::kReadingListPrivate);
821 skip.insert(APIPermission::kRtcPrivate);
822 skip.insert(APIPermission::kStreamsPrivate);
823 skip.insert(APIPermission::kSystemPrivate);
824 skip.insert(APIPermission::kTabCaptureForTab);
825 skip.insert(APIPermission::kTerminalPrivate);
826 skip.insert(APIPermission::kVirtualKeyboardPrivate);
827 skip.insert(APIPermission::kWallpaperPrivate);
828 skip.insert(APIPermission::kWebrtcAudioPrivate);
829 skip.insert(APIPermission::kWebrtcLoggingPrivate);
830 skip.insert(APIPermission::kWebstorePrivate);
832 // Warned as part of host permissions.
833 skip.insert(APIPermission::kDevtools);
835 // Platform apps.
836 skip.insert(APIPermission::kBrowser);
837 skip.insert(APIPermission::kHid);
838 skip.insert(APIPermission::kFileSystem);
839 skip.insert(APIPermission::kFileSystemProvider);
840 skip.insert(APIPermission::kFileSystemRequestFileSystem);
841 skip.insert(APIPermission::kFileSystemRetainEntries);
842 skip.insert(APIPermission::kFileSystemWrite);
843 skip.insert(APIPermission::kSocket);
844 skip.insert(APIPermission::kUsb);
845 skip.insert(APIPermission::kUsbDevice);
846 skip.insert(APIPermission::kLauncherSearchProvider);
848 // We already have a generic message for declaring externally_connectable.
849 skip.insert(APIPermission::kExternallyConnectableAllUrls);
851 PermissionsInfo* info = PermissionsInfo::GetInstance();
852 APIPermissionSet permissions = info->GetAll();
853 for (APIPermissionSet::const_iterator i = permissions.begin();
854 i != permissions.end(); ++i) {
855 const APIPermissionInfo* permission_info = i->info();
856 EXPECT_TRUE(permission_info != NULL);
858 if (skip.count(i->id())) {
859 EXPECT_EQ(PermissionMessage::kNone, permission_info->message_id())
860 << "unexpected message_id for " << permission_info->name();
861 } else {
862 EXPECT_NE(PermissionMessage::kNone, permission_info->message_id())
863 << "missing message_id for " << permission_info->name();
868 TEST(PermissionsTest, FileSystemPermissionMessages) {
869 APIPermissionSet api_permissions;
870 api_permissions.insert(APIPermission::kFileSystemWrite);
871 api_permissions.insert(APIPermission::kFileSystemDirectory);
872 scoped_refptr<PermissionSet> permissions(
873 new PermissionSet(api_permissions, ManifestPermissionSet(),
874 URLPatternSet(), URLPatternSet()));
875 EXPECT_TRUE(PermissionSetProducesMessage(
876 permissions.get(), Manifest::TYPE_PLATFORM_APP,
877 PermissionMessage::kFileSystemDirectory,
878 MakePermissionIDSet(api_permissions)));
881 // The file system permissions have a special-case hack to show a warning for
882 // write and directory at the same time.
883 // TODO(sammc): Remove this. See http://crbug.com/284849.
884 TEST(PermissionsTest, FileSystemImplicitPermissions) {
885 APIPermissionSet apis;
886 apis.insert(APIPermission::kFileSystemWrite);
887 apis.AddImpliedPermissions();
889 EXPECT_EQ(apis.find(APIPermission::kFileSystemWrite)->id(),
890 APIPermission::kFileSystemWrite);
891 EXPECT_EQ(apis.size(), 1u);
893 apis.erase(APIPermission::kFileSystemWrite);
894 apis.insert(APIPermission::kFileSystemDirectory);
895 apis.AddImpliedPermissions();
897 EXPECT_EQ(apis.find(APIPermission::kFileSystemDirectory)->id(),
898 APIPermission::kFileSystemDirectory);
899 EXPECT_EQ(apis.size(), 1u);
901 apis.insert(APIPermission::kFileSystemWrite);
902 apis.AddImpliedPermissions();
904 EXPECT_EQ(apis.find(APIPermission::kFileSystemWrite)->id(),
905 APIPermission::kFileSystemWrite);
906 EXPECT_EQ(apis.find(APIPermission::kFileSystemDirectory)->id(),
907 APIPermission::kFileSystemDirectory);
908 EXPECT_EQ(apis.find(APIPermission::kFileSystemWriteDirectory)->id(),
909 APIPermission::kFileSystemWriteDirectory);
910 EXPECT_EQ(apis.size(), 3u);
913 TEST(PermissionsTest, HiddenFileSystemPermissionMessages) {
914 APIPermissionSet api_permissions;
915 api_permissions.insert(APIPermission::kFileSystemWrite);
916 api_permissions.insert(APIPermission::kFileSystemDirectory);
917 api_permissions.insert(APIPermission::kFileSystemWriteDirectory);
918 scoped_refptr<PermissionSet> permissions(
919 new PermissionSet(api_permissions, ManifestPermissionSet(),
920 URLPatternSet(), URLPatternSet()));
921 EXPECT_TRUE(PermissionSetProducesMessage(
922 permissions.get(), Manifest::TYPE_PLATFORM_APP,
923 PermissionMessage::kFileSystemWriteDirectory,
924 MakePermissionIDSet(api_permissions)));
927 TEST(PermissionsTest, SuppressedPermissionMessages) {
929 // Tabs warning suppresses favicon warning.
930 APIPermissionSet api_permissions;
931 api_permissions.insert(APIPermission::kTab);
932 URLPatternSet hosts;
933 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI,
934 "chrome://favicon/"));
935 scoped_refptr<PermissionSet> permissions(
936 new PermissionSet(api_permissions, ManifestPermissionSet(),
937 hosts, URLPatternSet()));
938 EXPECT_TRUE(PermissionSetProducesMessage(
939 permissions.get(), Manifest::TYPE_EXTENSION, PermissionMessage::kTabs,
940 MakePermissionIDSet(APIPermission::kTab, APIPermission::kFavicon)));
943 // History warning suppresses favicon warning.
944 APIPermissionSet api_permissions;
945 api_permissions.insert(APIPermission::kHistory);
946 URLPatternSet hosts;
947 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI,
948 "chrome://favicon/"));
949 scoped_refptr<PermissionSet> permissions(
950 new PermissionSet(api_permissions, ManifestPermissionSet(),
951 hosts, URLPatternSet()));
952 EXPECT_TRUE(PermissionSetProducesMessage(
953 permissions.get(), Manifest::TYPE_EXTENSION,
954 PermissionMessage::kBrowsingHistory,
955 MakePermissionIDSet(APIPermission::kHistory, APIPermission::kFavicon)));
958 // All sites warning suppresses tabs warning.
959 APIPermissionSet api_permissions;
960 api_permissions.insert(APIPermission::kTab);
961 URLPatternSet hosts;
962 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
963 scoped_refptr<PermissionSet> permissions(new PermissionSet(
964 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
965 EXPECT_TRUE(PermissionSetProducesMessage(
966 permissions.get(), Manifest::TYPE_EXTENSION,
967 PermissionMessage::kHostsAll,
968 MakePermissionIDSet(APIPermission::kHostsAll, APIPermission::kTab)));
971 // All sites warning suppresses topSites warning.
972 APIPermissionSet api_permissions;
973 api_permissions.insert(APIPermission::kTopSites);
974 URLPatternSet hosts;
975 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
976 scoped_refptr<PermissionSet> permissions(new PermissionSet(
977 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
978 EXPECT_TRUE(PermissionSetProducesMessage(
979 permissions.get(), Manifest::TYPE_EXTENSION,
980 PermissionMessage::kHostsAll,
981 MakePermissionIDSet(APIPermission::kHostsAll,
982 APIPermission::kTopSites)));
985 // All sites warning suppresses declarativeWebRequest warning.
986 APIPermissionSet api_permissions;
987 api_permissions.insert(APIPermission::kDeclarativeWebRequest);
988 URLPatternSet hosts;
989 hosts.AddPattern(URLPattern(URLPattern::SCHEME_CHROMEUI, "*://*/*"));
990 scoped_refptr<PermissionSet> permissions(new PermissionSet(
991 api_permissions, ManifestPermissionSet(), hosts, URLPatternSet()));
992 EXPECT_TRUE(PermissionSetProducesMessage(
993 permissions.get(), Manifest::TYPE_EXTENSION,
994 PermissionMessage::kHostsAll,
995 MakePermissionIDSet(APIPermission::kHostsAll,
996 APIPermission::kDeclarativeWebRequest)));
999 // BrowsingHistory warning suppresses all history read/write warnings.
1000 APIPermissionSet api_permissions;
1001 api_permissions.insert(APIPermission::kHistory);
1002 api_permissions.insert(APIPermission::kTab);
1003 api_permissions.insert(APIPermission::kTopSites);
1004 api_permissions.insert(APIPermission::kProcesses);
1005 api_permissions.insert(APIPermission::kWebNavigation);
1006 scoped_refptr<PermissionSet> permissions(
1007 new PermissionSet(api_permissions, ManifestPermissionSet(),
1008 URLPatternSet(), URLPatternSet()));
1009 EXPECT_TRUE(PermissionSetProducesMessage(
1010 permissions.get(), Manifest::TYPE_EXTENSION,
1011 PermissionMessage::kBrowsingHistory,
1012 MakePermissionIDSet(api_permissions)));
1015 // Tabs warning suppresses all read-only history warnings.
1016 APIPermissionSet api_permissions;
1017 api_permissions.insert(APIPermission::kTab);
1018 api_permissions.insert(APIPermission::kTopSites);
1019 api_permissions.insert(APIPermission::kProcesses);
1020 api_permissions.insert(APIPermission::kWebNavigation);
1021 scoped_refptr<PermissionSet> permissions(
1022 new PermissionSet(api_permissions, ManifestPermissionSet(),
1023 URLPatternSet(), URLPatternSet()));
1024 EXPECT_TRUE(PermissionSetProducesMessage(
1025 permissions.get(), Manifest::TYPE_EXTENSION, PermissionMessage::kTabs,
1026 MakePermissionIDSet(api_permissions)));
1030 TEST(PermissionsTest, AccessToDevicesMessages) {
1032 APIPermissionSet api_permissions;
1033 api_permissions.insert(APIPermission::kSerial);
1034 scoped_refptr<PermissionSet> permissions(
1035 new PermissionSet(api_permissions,
1036 ManifestPermissionSet(),
1037 URLPatternSet(),
1038 URLPatternSet()));
1039 VerifyOnePermissionMessage(
1040 permissions.get(), Manifest::TYPE_EXTENSION,
1041 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_SERIAL));
1044 // Testing that multiple permissions will show the one message.
1045 APIPermissionSet api_permissions;
1046 api_permissions.insert(APIPermission::kSerial);
1047 api_permissions.insert(APIPermission::kSerial);
1048 scoped_refptr<PermissionSet> permissions(
1049 new PermissionSet(api_permissions,
1050 ManifestPermissionSet(),
1051 URLPatternSet(),
1052 URLPatternSet()));
1053 VerifyOnePermissionMessage(
1054 permissions.get(), Manifest::TYPE_EXTENSION,
1055 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_SERIAL));
1058 scoped_refptr<Extension> extension =
1059 LoadManifest("permissions", "access_to_devices_bluetooth.json");
1060 PermissionSet* set = const_cast<PermissionSet*>(
1061 extension->permissions_data()->active_permissions().get());
1062 VerifyOnePermissionMessage(
1063 set, extension->GetType(),
1064 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH));
1066 // Test Bluetooth and Serial
1067 set->apis_.insert(APIPermission::kSerial);
1068 VerifyOnePermissionMessage(
1069 set, extension->GetType(),
1070 l10n_util::GetStringUTF16(
1071 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_SERIAL));
1075 TEST(PermissionsTest, MergedFileSystemPermissionComparison) {
1076 APIPermissionSet write_api_permissions;
1077 write_api_permissions.insert(APIPermission::kFileSystemWrite);
1078 scoped_refptr<PermissionSet> write_permissions(
1079 new PermissionSet(write_api_permissions, ManifestPermissionSet(),
1080 URLPatternSet(), URLPatternSet()));
1082 APIPermissionSet directory_api_permissions;
1083 directory_api_permissions.insert(APIPermission::kFileSystemDirectory);
1084 scoped_refptr<PermissionSet> directory_permissions(
1085 new PermissionSet(directory_api_permissions, ManifestPermissionSet(),
1086 URLPatternSet(), URLPatternSet()));
1088 APIPermissionSet write_directory_api_permissions;
1089 write_directory_api_permissions.insert(
1090 APIPermission::kFileSystemWriteDirectory);
1091 scoped_refptr<PermissionSet> write_directory_permissions(
1092 new PermissionSet(write_directory_api_permissions,
1093 ManifestPermissionSet(),
1094 URLPatternSet(),
1095 URLPatternSet()));
1097 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1098 EXPECT_FALSE(provider->IsPrivilegeIncrease(write_directory_permissions.get(),
1099 write_permissions.get(),
1100 Manifest::TYPE_PLATFORM_APP));
1101 EXPECT_FALSE(provider->IsPrivilegeIncrease(write_directory_permissions.get(),
1102 directory_permissions.get(),
1103 Manifest::TYPE_PLATFORM_APP));
1104 EXPECT_TRUE(provider->IsPrivilegeIncrease(write_permissions.get(),
1105 directory_permissions.get(),
1106 Manifest::TYPE_PLATFORM_APP));
1107 EXPECT_TRUE(provider->IsPrivilegeIncrease(write_permissions.get(),
1108 write_directory_permissions.get(),
1109 Manifest::TYPE_PLATFORM_APP));
1110 EXPECT_FALSE(provider->IsPrivilegeIncrease(directory_permissions.get(),
1111 write_permissions.get(),
1112 Manifest::TYPE_PLATFORM_APP));
1113 EXPECT_TRUE(provider->IsPrivilegeIncrease(directory_permissions.get(),
1114 write_directory_permissions.get(),
1115 Manifest::TYPE_PLATFORM_APP));
1118 TEST(PermissionsTest, GetWarningMessages_ManyHosts) {
1119 scoped_refptr<Extension> extension;
1120 extension = LoadManifest("permissions", "many-hosts.json");
1121 EXPECT_TRUE(VerifyOnePermissionMessage(
1122 extension->permissions_data(),
1123 "Read and change your data on encrypted.google.com and www.google.com"));
1126 TEST(PermissionsTest, GetWarningMessages_Plugins) {
1127 scoped_refptr<Extension> extension;
1128 extension = LoadManifest("permissions", "plugins.json");
1129 // We don't parse the plugins key on Chrome OS, so it should not ask for any
1130 // permissions.
1131 #if defined(OS_CHROMEOS)
1132 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1133 #else
1134 EXPECT_TRUE(VerifyOnePermissionMessage(
1135 extension->permissions_data(),
1136 "Read and change all your data on your computer and the websites you "
1137 "visit"));
1138 #endif
1141 TEST(PermissionsTest, GetWarningMessages_AudioVideo) {
1142 const std::string kAudio("Use your microphone");
1143 const std::string kVideo("Use your camera");
1144 const std::string kBoth("Use your microphone and camera");
1146 // Both audio and video present.
1147 scoped_refptr<Extension> extension =
1148 LoadManifest("permissions", "audio-video.json");
1149 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1150 PermissionSet* set = const_cast<PermissionSet*>(
1151 extension->permissions_data()->active_permissions().get());
1152 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1153 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1154 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1155 PermissionMessageStrings warnings =
1156 provider->GetPermissionMessageStrings(set, extension->GetType());
1157 size_t combined_index = IndexOf(warnings, kBoth);
1158 size_t combined_size = warnings.size();
1160 // Just audio present.
1161 set->apis_.erase(APIPermission::kVideoCapture);
1162 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1163 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1164 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1165 warnings = provider->GetPermissionMessageStrings(set, extension->GetType());
1166 EXPECT_EQ(combined_size, warnings.size());
1167 EXPECT_EQ(combined_index, IndexOf(warnings, kAudio));
1169 // Just video present.
1170 set->apis_.erase(APIPermission::kAudioCapture);
1171 set->apis_.insert(APIPermission::kVideoCapture);
1172 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kAudio));
1173 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(), kVideo));
1174 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(), kBoth));
1175 warnings = provider->GetPermissionMessageStrings(set, extension->GetType());
1176 EXPECT_EQ(combined_size, warnings.size());
1177 EXPECT_EQ(combined_index, IndexOf(warnings, kVideo));
1180 TEST(PermissionsTest, GetWarningMessages_CombinedSessions) {
1182 APIPermissionSet api_permissions;
1183 api_permissions.insert(APIPermission::kTab);
1184 api_permissions.insert(APIPermission::kTopSites);
1185 api_permissions.insert(APIPermission::kProcesses);
1186 api_permissions.insert(APIPermission::kWebNavigation);
1187 api_permissions.insert(APIPermission::kSessions);
1188 scoped_refptr<PermissionSet> permissions(
1189 new PermissionSet(api_permissions, ManifestPermissionSet(),
1190 URLPatternSet(), URLPatternSet()));
1191 EXPECT_TRUE(VerifyOnePermissionMessage(
1192 permissions.get(), Manifest::TYPE_EXTENSION,
1193 l10n_util::GetStringUTF16(
1194 IDS_EXTENSION_PROMPT_WARNING_HISTORY_READ_AND_SESSIONS)));
1197 APIPermissionSet api_permissions;
1198 api_permissions.insert(APIPermission::kHistory);
1199 api_permissions.insert(APIPermission::kTab);
1200 api_permissions.insert(APIPermission::kTopSites);
1201 api_permissions.insert(APIPermission::kProcesses);
1202 api_permissions.insert(APIPermission::kWebNavigation);
1203 api_permissions.insert(APIPermission::kSessions);
1204 scoped_refptr<PermissionSet> permissions(
1205 new PermissionSet(api_permissions, ManifestPermissionSet(),
1206 URLPatternSet(), URLPatternSet()));
1207 EXPECT_TRUE(VerifyOnePermissionMessage(
1208 permissions.get(), Manifest::TYPE_EXTENSION,
1209 l10n_util::GetStringUTF16(
1210 IDS_EXTENSION_PROMPT_WARNING_HISTORY_WRITE_AND_SESSIONS)));
1214 TEST(PermissionsTest, GetWarningMessages_DeclarativeWebRequest) {
1215 // Test that if the declarativeWebRequest permission is present
1216 // in combination with all hosts permission, then only the warning
1217 // for host permissions is shown, because that covers the use of
1218 // declarativeWebRequest.
1220 // Until Declarative Web Request is in stable, let's make sure it is enabled
1221 // on the current channel.
1222 ScopedCurrentChannel sc(chrome::VersionInfo::CHANNEL_CANARY);
1224 // First verify that declarativeWebRequest produces a message when host
1225 // permissions do not cover all hosts.
1226 scoped_refptr<Extension> extension =
1227 LoadManifest("permissions", "web_request_not_all_host_permissions.json");
1228 const PermissionSet* set =
1229 extension->permissions_data()->active_permissions().get();
1230 EXPECT_TRUE(VerifyHasPermissionMessage(set, extension->GetType(),
1231 "Block parts of web pages"));
1232 EXPECT_FALSE(VerifyHasPermissionMessage(
1233 set, extension->GetType(),
1234 "Read and change all your data on the websites you visit"));
1236 // Now verify that declarativeWebRequest does not produce a message when host
1237 // permissions do cover all hosts.
1238 extension =
1239 LoadManifest("permissions", "web_request_all_host_permissions.json");
1240 set = extension->permissions_data()->active_permissions().get();
1241 EXPECT_FALSE(VerifyHasPermissionMessage(set, extension->GetType(),
1242 "Block parts of web pages"));
1243 EXPECT_TRUE(VerifyHasPermissionMessage(
1244 set, extension->GetType(),
1245 "Read and change all your data on the websites you visit"));
1248 TEST(PermissionsTest, GetWarningMessages_Serial) {
1249 scoped_refptr<Extension> extension =
1250 LoadManifest("permissions", "serial.json");
1252 EXPECT_TRUE(extension->is_platform_app());
1253 EXPECT_TRUE(
1254 extension->permissions_data()->HasAPIPermission(APIPermission::kSerial));
1255 EXPECT_TRUE(VerifyOnePermissionMessage(extension->permissions_data(),
1256 "Access your serial devices"));
1259 TEST(PermissionsTest, GetWarningMessages_Socket_AnyHost) {
1260 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
1262 scoped_refptr<Extension> extension =
1263 LoadManifest("permissions", "socket_any_host.json");
1264 EXPECT_TRUE(extension->is_platform_app());
1265 EXPECT_TRUE(
1266 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1267 EXPECT_TRUE(VerifyOnePermissionMessage(
1268 extension->permissions_data(),
1269 "Exchange data with any device on the local network or internet"));
1272 TEST(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) {
1273 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
1275 scoped_refptr<Extension> extension =
1276 LoadManifest("permissions", "socket_one_domain_two_hostnames.json");
1277 EXPECT_TRUE(extension->is_platform_app());
1278 EXPECT_TRUE(
1279 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1281 // Verify the warnings, including support for unicode characters, the fact
1282 // that domain host warnings come before specific host warnings, and the fact
1283 // that domains and hostnames are in alphabetical order regardless of the
1284 // order in the manifest file.
1285 EXPECT_TRUE(VerifyTwoPermissionMessages(
1286 extension->permissions_data(),
1287 "Exchange data with any device in the domain example.org",
1288 "Exchange data with the devices named: "
1289 "b\xC3\xA5r.example.com foo.example.com",
1290 // "\xC3\xA5" = UTF-8 for lowercase A with ring above
1291 true));
1294 TEST(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) {
1295 ScopedCurrentChannel channel(chrome::VersionInfo::CHANNEL_DEV);
1297 scoped_refptr<Extension> extension =
1298 LoadManifest("permissions", "socket_two_domains_one_hostname.json");
1299 EXPECT_TRUE(extension->is_platform_app());
1300 EXPECT_TRUE(
1301 extension->permissions_data()->HasAPIPermission(APIPermission::kSocket));
1303 // Verify the warnings, including the fact that domain host warnings come
1304 // before specific host warnings and the fact that domains and hostnames are
1305 // in alphabetical order regardless of the order in the manifest file.
1306 EXPECT_TRUE(VerifyTwoPermissionMessages(
1307 extension->permissions_data(),
1308 "Exchange data with any device in the domains: "
1309 "example.com foo.example.org",
1310 "Exchange data with the device named bar.example.org", true));
1313 // Since platform apps always use isolated storage, they can't (silently)
1314 // access user data on other domains, so there's no need to prompt about host
1315 // permissions. See crbug.com/255229.
1316 TEST(PermissionsTest, GetWarningMessages_PlatformAppHosts) {
1317 scoped_refptr<Extension> extension;
1319 extension = LoadManifest("permissions", "platform_app_hosts.json");
1320 EXPECT_TRUE(extension->is_platform_app());
1321 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1323 extension = LoadManifest("permissions", "platform_app_all_urls.json");
1324 EXPECT_TRUE(extension->is_platform_app());
1325 EXPECT_TRUE(VerifyNoPermissionMessages(extension->permissions_data()));
1328 testing::AssertionResult ShowsAllHostsWarning(const std::string& pattern) {
1329 scoped_refptr<Extension> extension =
1330 ExtensionBuilder()
1331 .SetManifest(DictionaryBuilder()
1332 .Set("name", "TLDWildCardTest")
1333 .Set("version", "0.1.0")
1334 .Set("permissions", ListBuilder().Append(pattern))
1335 .Build())
1336 .Build();
1338 return VerifyHasPermissionMessage(
1339 extension->permissions_data(),
1340 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_ALL_HOSTS));
1343 TEST(PermissionsTest, GetWarningMessages_TLDWildcardTreatedAsAllHosts) {
1344 EXPECT_TRUE(ShowsAllHostsWarning("http://*.com/*")); // most popular.
1345 EXPECT_TRUE(ShowsAllHostsWarning("http://*.org/*")); // sanity check.
1346 EXPECT_TRUE(ShowsAllHostsWarning("http://*.co.uk/*")); // eTLD.
1347 EXPECT_TRUE(ShowsAllHostsWarning("http://*.de/*")); // foreign country tld.
1349 // We should still show the normal permissions (i.e., "Can access your data on
1350 // *.rdcronin.com") for things that are not TLDs.
1351 EXPECT_FALSE(ShowsAllHostsWarning("http://*.rdcronin.com/*"));
1353 // Pseudo-TLDs, like appspot.com, should not show all hosts.
1354 EXPECT_FALSE(ShowsAllHostsWarning("http://*.appspot.com/*"));
1356 // Non-TLDs should be likewise exempt.
1357 EXPECT_FALSE(ShowsAllHostsWarning("http://*.notarealtld/*"));
1359 // Our internal checks use "foo", so let's make sure we're not messing
1360 // something up with it.
1361 EXPECT_FALSE(ShowsAllHostsWarning("http://*.foo.com"));
1362 EXPECT_FALSE(ShowsAllHostsWarning("http://foo.com"));
1363 // This will fail if foo becomes a recognized TLD. Which could be soon.
1364 // Update as needed.
1365 EXPECT_FALSE(ShowsAllHostsWarning("http://*.foo"));
1368 TEST(PermissionsTest, GetDistinctHosts) {
1369 URLPatternSet explicit_hosts;
1370 std::set<std::string> expected;
1371 expected.insert("www.foo.com");
1372 expected.insert("www.bar.com");
1373 expected.insert("www.baz.com");
1376 SCOPED_TRACE("no dupes");
1378 // Simple list with no dupes.
1379 explicit_hosts.AddPattern(
1380 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1381 explicit_hosts.AddPattern(
1382 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/path"));
1383 explicit_hosts.AddPattern(
1384 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
1385 EXPECT_EQ(expected,
1386 permission_message_util::GetDistinctHosts(
1387 explicit_hosts, true, true));
1391 SCOPED_TRACE("two dupes");
1393 // Add some dupes.
1394 explicit_hosts.AddPattern(
1395 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1396 explicit_hosts.AddPattern(
1397 URLPattern(URLPattern::SCHEME_HTTP, "http://www.baz.com/path"));
1398 EXPECT_EQ(expected,
1399 permission_message_util::GetDistinctHosts(
1400 explicit_hosts, true, true));
1404 SCOPED_TRACE("schemes differ");
1406 // Add a pattern that differs only by scheme. This should be filtered out.
1407 explicit_hosts.AddPattern(
1408 URLPattern(URLPattern::SCHEME_HTTPS, "https://www.bar.com/path"));
1409 EXPECT_EQ(expected,
1410 permission_message_util::GetDistinctHosts(
1411 explicit_hosts, true, true));
1415 SCOPED_TRACE("paths differ");
1417 // Add some dupes by path.
1418 explicit_hosts.AddPattern(
1419 URLPattern(URLPattern::SCHEME_HTTP, "http://www.bar.com/pathypath"));
1420 EXPECT_EQ(expected,
1421 permission_message_util::GetDistinctHosts(
1422 explicit_hosts, true, true));
1426 SCOPED_TRACE("subdomains differ");
1428 // We don't do anything special for subdomains.
1429 explicit_hosts.AddPattern(
1430 URLPattern(URLPattern::SCHEME_HTTP, "http://monkey.www.bar.com/path"));
1431 explicit_hosts.AddPattern(
1432 URLPattern(URLPattern::SCHEME_HTTP, "http://bar.com/path"));
1434 expected.insert("monkey.www.bar.com");
1435 expected.insert("bar.com");
1437 EXPECT_EQ(expected,
1438 permission_message_util::GetDistinctHosts(
1439 explicit_hosts, true, true));
1443 SCOPED_TRACE("RCDs differ");
1445 // Now test for RCD uniquing.
1446 explicit_hosts.AddPattern(
1447 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1448 explicit_hosts.AddPattern(
1449 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1450 explicit_hosts.AddPattern(
1451 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.de/path"));
1452 explicit_hosts.AddPattern(
1453 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca.us/path"));
1454 explicit_hosts.AddPattern(
1455 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1456 explicit_hosts.AddPattern(
1457 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com.my/path"));
1459 // This is an unknown RCD, which shouldn't be uniqued out.
1460 explicit_hosts.AddPattern(
1461 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
1462 // But it should only occur once.
1463 explicit_hosts.AddPattern(
1464 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.xyzzy/path"));
1466 expected.insert("www.foo.xyzzy");
1468 EXPECT_EQ(expected,
1469 permission_message_util::GetDistinctHosts(
1470 explicit_hosts, true, true));
1474 SCOPED_TRACE("wildcards");
1476 explicit_hosts.AddPattern(
1477 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
1479 expected.insert("*.google.com");
1481 EXPECT_EQ(expected,
1482 permission_message_util::GetDistinctHosts(
1483 explicit_hosts, true, true));
1487 SCOPED_TRACE("scriptable hosts");
1489 APIPermissionSet empty_perms;
1490 explicit_hosts.ClearPatterns();
1491 URLPatternSet scriptable_hosts;
1492 expected.clear();
1494 explicit_hosts.AddPattern(
1495 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com/*"));
1496 scriptable_hosts.AddPattern(
1497 URLPattern(URLPattern::SCHEME_HTTP, "http://*.example.com/*"));
1499 expected.insert("*.google.com");
1500 expected.insert("*.example.com");
1502 scoped_refptr<PermissionSet> perm_set(new PermissionSet(
1503 empty_perms, ManifestPermissionSet(),
1504 explicit_hosts, scriptable_hosts));
1505 EXPECT_EQ(expected,
1506 permission_message_util::GetDistinctHosts(
1507 perm_set->effective_hosts(), true, true));
1511 // We don't display warnings for file URLs because they are off by default.
1512 SCOPED_TRACE("file urls");
1514 explicit_hosts.ClearPatterns();
1515 expected.clear();
1517 explicit_hosts.AddPattern(
1518 URLPattern(URLPattern::SCHEME_FILE, "file:///*"));
1520 EXPECT_EQ(expected,
1521 permission_message_util::GetDistinctHosts(
1522 explicit_hosts, true, true));
1526 TEST(PermissionsTest, GetDistinctHosts_ComIsBestRcd) {
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 explicit_hosts.AddPattern(
1535 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1536 explicit_hosts.AddPattern(
1537 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1538 explicit_hosts.AddPattern(
1539 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1541 std::set<std::string> expected;
1542 expected.insert("www.foo.com");
1543 EXPECT_EQ(expected,
1544 permission_message_util::GetDistinctHosts(
1545 explicit_hosts, true, true));
1548 TEST(PermissionsTest, GetDistinctHosts_NetIs2ndBestRcd) {
1549 URLPatternSet explicit_hosts;
1550 explicit_hosts.AddPattern(
1551 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1552 explicit_hosts.AddPattern(
1553 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1554 explicit_hosts.AddPattern(
1555 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1556 explicit_hosts.AddPattern(
1557 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1558 explicit_hosts.AddPattern(
1559 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1560 // No http://www.foo.com/path
1562 std::set<std::string> expected;
1563 expected.insert("www.foo.net");
1564 EXPECT_EQ(expected,
1565 permission_message_util::GetDistinctHosts(
1566 explicit_hosts, true, true));
1569 TEST(PermissionsTest, GetDistinctHosts_OrgIs3rdBestRcd) {
1570 URLPatternSet explicit_hosts;
1571 explicit_hosts.AddPattern(
1572 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1573 explicit_hosts.AddPattern(
1574 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1575 explicit_hosts.AddPattern(
1576 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1577 // No http://www.foo.net/path
1578 explicit_hosts.AddPattern(
1579 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1580 // No http://www.foo.com/path
1582 std::set<std::string> expected;
1583 expected.insert("www.foo.org");
1584 EXPECT_EQ(expected,
1585 permission_message_util::GetDistinctHosts(
1586 explicit_hosts, true, true));
1589 TEST(PermissionsTest, GetDistinctHosts_FirstInListIs4thBestRcd) {
1590 URLPatternSet explicit_hosts;
1591 explicit_hosts.AddPattern(
1592 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1593 // No http://www.foo.org/path
1594 explicit_hosts.AddPattern(
1595 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1596 // No http://www.foo.net/path
1597 explicit_hosts.AddPattern(
1598 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1599 // No http://www.foo.com/path
1601 std::set<std::string> expected;
1602 expected.insert("www.foo.ca");
1603 EXPECT_EQ(expected,
1604 permission_message_util::GetDistinctHosts(
1605 explicit_hosts, true, true));
1608 TEST(PermissionsTest, IsHostPrivilegeIncrease) {
1609 Manifest::Type type = Manifest::TYPE_EXTENSION;
1610 const PermissionMessageProvider* provider = PermissionMessageProvider::Get();
1611 ManifestPermissionSet empty_manifest_permissions;
1612 URLPatternSet elist1;
1613 URLPatternSet elist2;
1614 URLPatternSet slist1;
1615 URLPatternSet slist2;
1616 scoped_refptr<PermissionSet> set1;
1617 scoped_refptr<PermissionSet> set2;
1618 APIPermissionSet empty_perms;
1619 elist1.AddPattern(
1620 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
1621 elist1.AddPattern(
1622 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1624 // Test that the host order does not matter.
1625 elist2.AddPattern(
1626 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1627 elist2.AddPattern(
1628 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
1630 set1 = new PermissionSet(empty_perms, empty_manifest_permissions,
1631 elist1, slist1);
1632 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1633 elist2, slist2);
1635 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1636 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1638 // Test that paths are ignored.
1639 elist2.ClearPatterns();
1640 elist2.AddPattern(
1641 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/*"));
1642 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1643 elist2, slist2);
1644 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1645 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1647 // Test that RCDs are ignored.
1648 elist2.ClearPatterns();
1649 elist2.AddPattern(
1650 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/*"));
1651 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1652 elist2, slist2);
1653 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1654 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1656 // Test that subdomain wildcards are handled properly.
1657 elist2.ClearPatterns();
1658 elist2.AddPattern(
1659 URLPattern(URLPattern::SCHEME_HTTP, "http://*.google.com.hk/*"));
1660 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1661 elist2, slist2);
1662 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1663 // TODO(jstritar): Does not match subdomains properly. http://crbug.com/65337
1664 // EXPECT_FALSE(provider->IsPrivilegeIncrease(set2, set1, type));
1666 // Test that different domains count as different hosts.
1667 elist2.ClearPatterns();
1668 elist2.AddPattern(
1669 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com/path"));
1670 elist2.AddPattern(
1671 URLPattern(URLPattern::SCHEME_HTTP, "http://www.example.org/path"));
1672 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1673 elist2, slist2);
1674 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1675 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1677 // Test that different subdomains count as different hosts.
1678 elist2.ClearPatterns();
1679 elist2.AddPattern(
1680 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*"));
1681 set2 = new PermissionSet(empty_perms, empty_manifest_permissions,
1682 elist2, slist2);
1683 EXPECT_TRUE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1684 EXPECT_TRUE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1686 // Test that platform apps do not have host permissions increases.
1687 type = Manifest::TYPE_PLATFORM_APP;
1688 EXPECT_FALSE(provider->IsPrivilegeIncrease(set1.get(), set2.get(), type));
1689 EXPECT_FALSE(provider->IsPrivilegeIncrease(set2.get(), set1.get(), type));
1692 TEST(PermissionsTest, GetAPIsAsStrings) {
1693 APIPermissionSet apis;
1694 URLPatternSet empty_set;
1696 apis.insert(APIPermission::kProxy);
1697 apis.insert(APIPermission::kBackground);
1698 apis.insert(APIPermission::kNotifications);
1699 apis.insert(APIPermission::kTab);
1701 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
1702 apis, ManifestPermissionSet(), empty_set, empty_set);
1703 std::set<std::string> api_names = perm_set->GetAPIsAsStrings();
1705 // The result is correct if it has the same number of elements
1706 // and we can convert it back to the id set.
1707 EXPECT_EQ(4u, api_names.size());
1708 EXPECT_EQ(apis,
1709 PermissionsInfo::GetInstance()->GetAllByName(api_names));
1712 TEST(PermissionsTest, IsEmpty) {
1713 APIPermissionSet empty_apis;
1714 URLPatternSet empty_extent;
1716 scoped_refptr<PermissionSet> empty = new PermissionSet();
1717 EXPECT_TRUE(empty->IsEmpty());
1718 scoped_refptr<PermissionSet> perm_set;
1720 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1721 empty_extent, empty_extent);
1722 EXPECT_TRUE(perm_set->IsEmpty());
1724 APIPermissionSet non_empty_apis;
1725 non_empty_apis.insert(APIPermission::kBackground);
1726 perm_set = new PermissionSet(non_empty_apis, ManifestPermissionSet(),
1727 empty_extent, empty_extent);
1728 EXPECT_FALSE(perm_set->IsEmpty());
1730 // Try non standard host
1731 URLPatternSet non_empty_extent;
1732 AddPattern(&non_empty_extent, "http://www.google.com/*");
1734 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1735 non_empty_extent, empty_extent);
1736 EXPECT_FALSE(perm_set->IsEmpty());
1738 perm_set = new PermissionSet(empty_apis, ManifestPermissionSet(),
1739 empty_extent, non_empty_extent);
1740 EXPECT_FALSE(perm_set->IsEmpty());
1743 TEST(PermissionsTest, ImpliedPermissions) {
1744 URLPatternSet empty_extent;
1745 APIPermissionSet apis;
1746 apis.insert(APIPermission::kFileBrowserHandler);
1747 EXPECT_EQ(1U, apis.size());
1749 scoped_refptr<PermissionSet> perm_set;
1750 perm_set = new PermissionSet(apis, ManifestPermissionSet(),
1751 empty_extent, empty_extent);
1752 EXPECT_EQ(2U, perm_set->apis().size());
1755 TEST(PermissionsTest, SyncFileSystemPermission) {
1756 scoped_refptr<Extension> extension = LoadManifest(
1757 "permissions", "sync_file_system.json");
1758 APIPermissionSet apis;
1759 apis.insert(APIPermission::kSyncFileSystem);
1760 EXPECT_TRUE(extension->is_platform_app());
1761 EXPECT_TRUE(extension->permissions_data()->HasAPIPermission(
1762 APIPermission::kSyncFileSystem));
1763 EXPECT_TRUE(
1764 VerifyOnePermissionMessage(extension->permissions_data(),
1765 "Store data in your Google Drive account"));
1768 // Make sure that we don't crash when we're trying to show the permissions
1769 // even though chrome://thumb (and everything that's not chrome://favicon with
1770 // a chrome:// scheme) is not a valid permission.
1771 // More details here: crbug/246314.
1772 TEST(PermissionsTest, ChromeURLs) {
1773 URLPatternSet allowed_hosts;
1774 allowed_hosts.AddPattern(
1775 URLPattern(URLPattern::SCHEME_ALL, "http://www.google.com/"));
1776 allowed_hosts.AddPattern(
1777 URLPattern(URLPattern::SCHEME_ALL, "chrome://favicon/"));
1778 allowed_hosts.AddPattern(
1779 URLPattern(URLPattern::SCHEME_ALL, "chrome://thumb/"));
1780 scoped_refptr<PermissionSet> permissions(
1781 new PermissionSet(APIPermissionSet(), ManifestPermissionSet(),
1782 allowed_hosts, URLPatternSet()));
1783 PermissionMessageProvider::Get()->GetLegacyPermissionMessageIDs(
1784 permissions.get(), Manifest::TYPE_EXTENSION);
1785 PermissionMessageProvider::Get()->GetCoalescedPermissionMessages(
1786 PermissionMessageProvider::Get()->GetAllPermissionIDs(
1787 permissions.get(), Manifest::TYPE_EXTENSION));
1790 TEST(PermissionsTest, IsPrivilegeIncrease_DeclarativeWebRequest) {
1791 scoped_refptr<Extension> extension(
1792 LoadManifest("permissions", "permissions_all_urls.json"));
1793 scoped_refptr<const PermissionSet> permissions(
1794 extension->permissions_data()->active_permissions());
1796 scoped_refptr<Extension> extension_dwr(
1797 LoadManifest("permissions", "web_request_all_host_permissions.json"));
1798 scoped_refptr<const PermissionSet> permissions_dwr(
1799 extension_dwr->permissions_data()->active_permissions());
1801 EXPECT_FALSE(PermissionMessageProvider::Get()->
1802 IsPrivilegeIncrease(permissions.get(),
1803 permissions_dwr.get(),
1804 extension->GetType()));
1807 } // namespace extensions