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

Side by Side Diff: chrome/common/extensions/permissions/permission_set_unittest.cc

Issue 11724002: Move ContentScripts out of Extension (Closed) Base URL: http://git.chromium.org/chromium/src.git@dc_unref_browser_action
Patch Set: Wider net for registration Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/command_line.h" 5 #include "base/command_line.h"
6 #include "base/json/json_file_value_serializer.h" 6 #include "base/json/json_file_value_serializer.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/path_service.h" 8 #include "base/path_service.h"
9 #include "base/utf_string_conversions.h" 9 #include "base/utf_string_conversions.h"
10 #include "chrome/common/chrome_paths.h" 10 #include "chrome/common/chrome_paths.h"
11 #include "chrome/common/chrome_switches.h" 11 #include "chrome/common/chrome_switches.h"
12 #include "chrome/common/extensions/api/content_scripts/content_scripts_handler.h "
12 #include "chrome/common/extensions/extension.h" 13 #include "chrome/common/extensions/extension.h"
13 #include "chrome/common/extensions/extension_manifest_constants.h" 14 #include "chrome/common/extensions/extension_manifest_constants.h"
14 #include "chrome/common/extensions/features/feature.h" 15 #include "chrome/common/extensions/features/feature.h"
16 #include "chrome/common/extensions/manifest_handler.h"
15 #include "chrome/common/extensions/permissions/permission_set.h" 17 #include "chrome/common/extensions/permissions/permission_set.h"
16 #include "chrome/common/extensions/permissions/permissions_info.h" 18 #include "chrome/common/extensions/permissions/permissions_info.h"
17 #include "chrome/common/extensions/permissions/socket_permission.h" 19 #include "chrome/common/extensions/permissions/socket_permission.h"
18 #include "extensions/common/error_utils.h" 20 #include "extensions/common/error_utils.h"
19 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
20 22
21 using extensions::Extension; 23 using extensions::Extension;
22 24
23 namespace errors = extension_manifest_errors; 25 namespace errors = extension_manifest_errors;
24 namespace keys = extension_manifest_keys; 26 namespace keys = extension_manifest_keys;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 return warnings.size(); 74 return warnings.size();
73 } 75 }
74 76
75 bool Contains(const std::vector<string16>& warnings, 77 bool Contains(const std::vector<string16>& warnings,
76 const std::string& warning) { 78 const std::string& warning) {
77 return IndexOf(warnings, warning) != warnings.size(); 79 return IndexOf(warnings, warning) != warnings.size();
78 } 80 }
79 81
80 } // namespace 82 } // namespace
81 83
82
83 class PermissionsTest : public testing::Test { 84 class PermissionsTest : public testing::Test {
85 protected:
86 virtual void SetUp() OVERRIDE {
87 testing::Test::SetUp();
88 ManifestHandler::Register(extension_manifest_keys::kContentScripts,
89 new ContentScriptsHandler);
90 }
84 }; 91 };
85 92
86 // Tests GetByID. 93 // Tests GetByID.
87 TEST(PermissionsTest, GetByID) { 94 TEST_F(PermissionsTest, GetByID) {
88 PermissionsInfo* info = PermissionsInfo::GetInstance(); 95 PermissionsInfo* info = PermissionsInfo::GetInstance();
89 APIPermissionSet apis = info->GetAll(); 96 APIPermissionSet apis = info->GetAll();
90 for (APIPermissionSet::const_iterator i = apis.begin(); 97 for (APIPermissionSet::const_iterator i = apis.begin();
91 i != apis.end(); ++i) { 98 i != apis.end(); ++i) {
92 EXPECT_EQ(i->id(), i->info()->id()); 99 EXPECT_EQ(i->id(), i->info()->id());
93 } 100 }
94 } 101 }
95 102
96 // Tests that GetByName works with normal permission names and aliases. 103 // Tests that GetByName works with normal permission names and aliases.
97 TEST(PermissionsTest, GetByName) { 104 TEST_F(PermissionsTest, GetByName) {
98 PermissionsInfo* info = PermissionsInfo::GetInstance(); 105 PermissionsInfo* info = PermissionsInfo::GetInstance();
99 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id()); 106 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
100 EXPECT_EQ(APIPermission::kManagement, 107 EXPECT_EQ(APIPermission::kManagement,
101 info->GetByName("management")->id()); 108 info->GetByName("management")->id());
102 EXPECT_FALSE(info->GetByName("alsdkfjasldkfj")); 109 EXPECT_FALSE(info->GetByName("alsdkfjasldkfj"));
103 } 110 }
104 111
105 TEST(PermissionsTest, GetAll) { 112 TEST_F(PermissionsTest, GetAll) {
106 size_t count = 0; 113 size_t count = 0;
107 PermissionsInfo* info = PermissionsInfo::GetInstance(); 114 PermissionsInfo* info = PermissionsInfo::GetInstance();
108 APIPermissionSet apis = info->GetAll(); 115 APIPermissionSet apis = info->GetAll();
109 for (APIPermissionSet::const_iterator api = apis.begin(); 116 for (APIPermissionSet::const_iterator api = apis.begin();
110 api != apis.end(); ++api) { 117 api != apis.end(); ++api) {
111 // Make sure only the valid permission IDs get returned. 118 // Make sure only the valid permission IDs get returned.
112 EXPECT_NE(APIPermission::kInvalid, api->id()); 119 EXPECT_NE(APIPermission::kInvalid, api->id());
113 EXPECT_NE(APIPermission::kUnknown, api->id()); 120 EXPECT_NE(APIPermission::kUnknown, api->id());
114 count++; 121 count++;
115 } 122 }
116 EXPECT_EQ(count, info->get_permission_count()); 123 EXPECT_EQ(count, info->get_permission_count());
117 } 124 }
118 125
119 TEST(PermissionsTest, GetAllByName) { 126 TEST_F(PermissionsTest, GetAllByName) {
120 std::set<std::string> names; 127 std::set<std::string> names;
121 names.insert("background"); 128 names.insert("background");
122 names.insert("management"); 129 names.insert("management");
123 130
124 // This is an alias of kTab 131 // This is an alias of kTab
125 names.insert("windows"); 132 names.insert("windows");
126 133
127 // This unknown name should get dropped. 134 // This unknown name should get dropped.
128 names.insert("sdlkfjasdlkfj"); 135 names.insert("sdlkfjasdlkfj");
129 136
130 APIPermissionSet expected; 137 APIPermissionSet expected;
131 expected.insert(APIPermission::kBackground); 138 expected.insert(APIPermission::kBackground);
132 expected.insert(APIPermission::kManagement); 139 expected.insert(APIPermission::kManagement);
133 expected.insert(APIPermission::kTab); 140 expected.insert(APIPermission::kTab);
134 141
135 EXPECT_EQ(expected, 142 EXPECT_EQ(expected,
136 PermissionsInfo::GetInstance()->GetAllByName(names)); 143 PermissionsInfo::GetInstance()->GetAllByName(names));
137 } 144 }
138 145
139 // Tests that the aliases are properly mapped. 146 // Tests that the aliases are properly mapped.
140 TEST(PermissionsTest, Aliases) { 147 TEST_F(PermissionsTest, Aliases) {
141 PermissionsInfo* info = PermissionsInfo::GetInstance(); 148 PermissionsInfo* info = PermissionsInfo::GetInstance();
142 // tabs: tabs, windows 149 // tabs: tabs, windows
143 std::string tabs_name = "tabs"; 150 std::string tabs_name = "tabs";
144 EXPECT_EQ(tabs_name, info->GetByID(APIPermission::kTab)->name()); 151 EXPECT_EQ(tabs_name, info->GetByID(APIPermission::kTab)->name());
145 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id()); 152 EXPECT_EQ(APIPermission::kTab, info->GetByName("tabs")->id());
146 EXPECT_EQ(APIPermission::kTab, info->GetByName("windows")->id()); 153 EXPECT_EQ(APIPermission::kTab, info->GetByName("windows")->id());
147 154
148 // unlimitedStorage: unlimitedStorage, unlimited_storage 155 // unlimitedStorage: unlimitedStorage, unlimited_storage
149 std::string storage_name = "unlimitedStorage"; 156 std::string storage_name = "unlimitedStorage";
150 EXPECT_EQ(storage_name, info->GetByID( 157 EXPECT_EQ(storage_name, info->GetByID(
151 APIPermission::kUnlimitedStorage)->name()); 158 APIPermission::kUnlimitedStorage)->name());
152 EXPECT_EQ(APIPermission::kUnlimitedStorage, 159 EXPECT_EQ(APIPermission::kUnlimitedStorage,
153 info->GetByName("unlimitedStorage")->id()); 160 info->GetByName("unlimitedStorage")->id());
154 EXPECT_EQ(APIPermission::kUnlimitedStorage, 161 EXPECT_EQ(APIPermission::kUnlimitedStorage,
155 info->GetByName("unlimited_storage")->id()); 162 info->GetByName("unlimited_storage")->id());
156 } 163 }
157 164
158 TEST(PermissionsTest, EffectiveHostPermissions) { 165 TEST_F(PermissionsTest, EffectiveHostPermissions) {
159 scoped_refptr<Extension> extension; 166 scoped_refptr<Extension> extension;
160 scoped_refptr<const PermissionSet> permissions; 167 scoped_refptr<const PermissionSet> permissions;
161 168
162 extension = LoadManifest("effective_host_permissions", "empty.json"); 169 extension = LoadManifest("effective_host_permissions", "empty.json");
163 permissions = extension->GetActivePermissions(); 170 permissions = extension->GetActivePermissions();
164 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size()); 171 EXPECT_EQ(0u, extension->GetEffectiveHostPermissions().patterns().size());
165 EXPECT_FALSE(permissions->HasEffectiveAccessToURL( 172 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(
166 GURL("http://www.google.com"))); 173 GURL("http://www.google.com")));
167 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts()); 174 EXPECT_FALSE(permissions->HasEffectiveAccessToAllHosts());
168 175
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 231
225 extension = LoadManifest("effective_host_permissions", "all_hosts3.json"); 232 extension = LoadManifest("effective_host_permissions", "all_hosts3.json");
226 permissions = extension->GetActivePermissions(); 233 permissions = extension->GetActivePermissions();
227 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("http://test/"))); 234 EXPECT_FALSE(permissions->HasEffectiveAccessToURL(GURL("http://test/")));
228 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://test/"))); 235 EXPECT_TRUE(permissions->HasEffectiveAccessToURL(GURL("https://test/")));
229 EXPECT_TRUE( 236 EXPECT_TRUE(
230 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com"))); 237 permissions->HasEffectiveAccessToURL(GURL("http://www.google.com")));
231 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts()); 238 EXPECT_TRUE(permissions->HasEffectiveAccessToAllHosts());
232 } 239 }
233 240
234 TEST(PermissionsTest, ExplicitAccessToOrigin) { 241 TEST_F(PermissionsTest, ExplicitAccessToOrigin) {
235 APIPermissionSet apis; 242 APIPermissionSet apis;
236 URLPatternSet explicit_hosts; 243 URLPatternSet explicit_hosts;
237 URLPatternSet scriptable_hosts; 244 URLPatternSet scriptable_hosts;
238 245
239 AddPattern(&explicit_hosts, "http://*.google.com/*"); 246 AddPattern(&explicit_hosts, "http://*.google.com/*");
240 // The explicit host paths should get set to /*. 247 // The explicit host paths should get set to /*.
241 AddPattern(&explicit_hosts, "http://www.example.com/a/particular/path/*"); 248 AddPattern(&explicit_hosts, "http://www.example.com/a/particular/path/*");
242 249
243 scoped_refptr<PermissionSet> perm_set = new PermissionSet( 250 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
244 apis, explicit_hosts, scriptable_hosts); 251 apis, explicit_hosts, scriptable_hosts);
245 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin( 252 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
246 GURL("http://www.google.com/"))); 253 GURL("http://www.google.com/")));
247 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin( 254 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
248 GURL("http://test.google.com/"))); 255 GURL("http://test.google.com/")));
249 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin( 256 ASSERT_TRUE(perm_set->HasExplicitAccessToOrigin(
250 GURL("http://www.example.com"))); 257 GURL("http://www.example.com")));
251 ASSERT_TRUE(perm_set->HasEffectiveAccessToURL( 258 ASSERT_TRUE(perm_set->HasEffectiveAccessToURL(
252 GURL("http://www.example.com"))); 259 GURL("http://www.example.com")));
253 ASSERT_FALSE(perm_set->HasExplicitAccessToOrigin( 260 ASSERT_FALSE(perm_set->HasExplicitAccessToOrigin(
254 GURL("http://test.example.com"))); 261 GURL("http://test.example.com")));
255 } 262 }
256 263
257 TEST(PermissionsTest, CreateUnion) { 264 TEST_F(PermissionsTest, CreateUnion) {
258 APIPermission* permission = NULL; 265 APIPermission* permission = NULL;
259 266
260 APIPermissionSet apis1; 267 APIPermissionSet apis1;
261 APIPermissionSet apis2; 268 APIPermissionSet apis2;
262 APIPermissionSet expected_apis; 269 APIPermissionSet expected_apis;
263 270
264 URLPatternSet explicit_hosts1; 271 URLPatternSet explicit_hosts1;
265 URLPatternSet explicit_hosts2; 272 URLPatternSet explicit_hosts2;
266 URLPatternSet expected_explicit_hosts; 273 URLPatternSet expected_explicit_hosts;
267 274
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 EXPECT_TRUE(union_set->Contains(*set2)); 378 EXPECT_TRUE(union_set->Contains(*set2));
372 379
373 EXPECT_TRUE(union_set->HasEffectiveFullAccess()); 380 EXPECT_TRUE(union_set->HasEffectiveFullAccess());
374 EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts()); 381 EXPECT_TRUE(union_set->HasEffectiveAccessToAllHosts());
375 EXPECT_EQ(expected_apis, union_set->apis()); 382 EXPECT_EQ(expected_apis, union_set->apis());
376 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts()); 383 EXPECT_EQ(expected_explicit_hosts, union_set->explicit_hosts());
377 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts()); 384 EXPECT_EQ(expected_scriptable_hosts, union_set->scriptable_hosts());
378 EXPECT_EQ(effective_hosts, union_set->effective_hosts()); 385 EXPECT_EQ(effective_hosts, union_set->effective_hosts());
379 } 386 }
380 387
381 TEST(PermissionsTest, CreateIntersection) { 388 TEST_F(PermissionsTest, CreateIntersection) {
382 APIPermission* permission = NULL; 389 APIPermission* permission = NULL;
383 390
384 APIPermissionSet apis1; 391 APIPermissionSet apis1;
385 APIPermissionSet apis2; 392 APIPermissionSet apis2;
386 APIPermissionSet expected_apis; 393 APIPermissionSet expected_apis;
387 394
388 URLPatternSet explicit_hosts1; 395 URLPatternSet explicit_hosts1;
389 URLPatternSet explicit_hosts2; 396 URLPatternSet explicit_hosts2;
390 URLPatternSet expected_explicit_hosts; 397 URLPatternSet expected_explicit_hosts;
391 398
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 EXPECT_FALSE(new_set->Contains(*set2)); 492 EXPECT_FALSE(new_set->Contains(*set2));
486 493
487 EXPECT_FALSE(new_set->HasEffectiveFullAccess()); 494 EXPECT_FALSE(new_set->HasEffectiveFullAccess());
488 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts()); 495 EXPECT_FALSE(new_set->HasEffectiveAccessToAllHosts());
489 EXPECT_EQ(expected_apis, new_set->apis()); 496 EXPECT_EQ(expected_apis, new_set->apis());
490 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts()); 497 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
491 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts()); 498 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
492 EXPECT_EQ(effective_hosts, new_set->effective_hosts()); 499 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
493 } 500 }
494 501
495 TEST(PermissionsTest, CreateDifference) { 502 TEST_F(PermissionsTest, CreateDifference) {
496 APIPermission* permission = NULL; 503 APIPermission* permission = NULL;
497 504
498 APIPermissionSet apis1; 505 APIPermissionSet apis1;
499 APIPermissionSet apis2; 506 APIPermissionSet apis2;
500 APIPermissionSet expected_apis; 507 APIPermissionSet expected_apis;
501 508
502 URLPatternSet explicit_hosts1; 509 URLPatternSet explicit_hosts1;
503 URLPatternSet explicit_hosts2; 510 URLPatternSet explicit_hosts2;
504 URLPatternSet expected_explicit_hosts; 511 URLPatternSet expected_explicit_hosts;
505 512
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 EXPECT_EQ(expected_apis, new_set->apis()); 593 EXPECT_EQ(expected_apis, new_set->apis());
587 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts()); 594 EXPECT_EQ(expected_explicit_hosts, new_set->explicit_hosts());
588 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts()); 595 EXPECT_EQ(expected_scriptable_hosts, new_set->scriptable_hosts());
589 EXPECT_EQ(effective_hosts, new_set->effective_hosts()); 596 EXPECT_EQ(effective_hosts, new_set->effective_hosts());
590 597
591 // |set3| = |set1| - |set2| --> |set3| intersect |set2| == empty_set 598 // |set3| = |set1| - |set2| --> |set3| intersect |set2| == empty_set
592 set1 = PermissionSet::CreateIntersection(new_set.get(), set2.get()); 599 set1 = PermissionSet::CreateIntersection(new_set.get(), set2.get());
593 EXPECT_TRUE(set1->IsEmpty()); 600 EXPECT_TRUE(set1->IsEmpty());
594 } 601 }
595 602
596 TEST(PermissionsTest, HasLessPrivilegesThan) { 603 TEST_F(PermissionsTest, HasLessPrivilegesThan) {
597 const struct { 604 const struct {
598 const char* base_name; 605 const char* base_name;
599 bool expect_increase; 606 bool expect_increase;
600 } kTests[] = { 607 } kTests[] = {
601 { "allhosts1", false }, // all -> all 608 { "allhosts1", false }, // all -> all
602 { "allhosts2", false }, // all -> one 609 { "allhosts2", false }, // all -> one
603 { "allhosts3", true }, // one -> all 610 { "allhosts3", true }, // one -> all
604 { "hosts1", false }, // http://a,http://b -> http://a,http://b 611 { "hosts1", false }, // http://a,http://b -> http://a,http://b
605 { "hosts2", true }, // http://a,http://b -> https://a,http://*.b 612 { "hosts2", true }, // http://a,http://b -> https://a,http://*.b
606 { "hosts3", false }, // http://a,http://b -> http://a 613 { "hosts3", false }, // http://a,http://b -> http://a
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
641 scoped_refptr<const PermissionSet> old_p( 648 scoped_refptr<const PermissionSet> old_p(
642 old_extension->GetActivePermissions()); 649 old_extension->GetActivePermissions());
643 scoped_refptr<const PermissionSet> new_p( 650 scoped_refptr<const PermissionSet> new_p(
644 new_extension->GetActivePermissions()); 651 new_extension->GetActivePermissions());
645 652
646 EXPECT_EQ(kTests[i].expect_increase, 653 EXPECT_EQ(kTests[i].expect_increase,
647 old_p->HasLessPrivilegesThan(new_p)) << kTests[i].base_name; 654 old_p->HasLessPrivilegesThan(new_p)) << kTests[i].base_name;
648 } 655 }
649 } 656 }
650 657
651 TEST(PermissionsTest, PermissionMessages) { 658 TEST_F(PermissionsTest, PermissionMessages) {
652 // Ensure that all permissions that needs to show install UI actually have 659 // Ensure that all permissions that needs to show install UI actually have
653 // strings associated with them. 660 // strings associated with them.
654 APIPermissionSet skip; 661 APIPermissionSet skip;
655 662
656 // These are considered "nuisance" or "trivial" permissions that don't need 663 // These are considered "nuisance" or "trivial" permissions that don't need
657 // a prompt. 664 // a prompt.
658 skip.insert(APIPermission::kActiveTab); 665 skip.insert(APIPermission::kActiveTab);
659 skip.insert(APIPermission::kAlarms); 666 skip.insert(APIPermission::kAlarms);
660 skip.insert(APIPermission::kAppCurrentWindowInternal); 667 skip.insert(APIPermission::kAppCurrentWindowInternal);
661 skip.insert(APIPermission::kAppNotifications); 668 skip.insert(APIPermission::kAppNotifications);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 EXPECT_EQ(PermissionMessage::kNone, permission_info->message_id()) 761 EXPECT_EQ(PermissionMessage::kNone, permission_info->message_id())
755 << "unexpected message_id for " << permission_info->name(); 762 << "unexpected message_id for " << permission_info->name();
756 } else { 763 } else {
757 EXPECT_NE(PermissionMessage::kNone, permission_info->message_id()) 764 EXPECT_NE(PermissionMessage::kNone, permission_info->message_id())
758 << "missing message_id for " << permission_info->name(); 765 << "missing message_id for " << permission_info->name();
759 } 766 }
760 } 767 }
761 } 768 }
762 769
763 // Tests the default permissions (empty API permission set). 770 // Tests the default permissions (empty API permission set).
764 TEST(PermissionsTest, DefaultFunctionAccess) { 771 TEST_F(PermissionsTest, DefaultFunctionAccess) {
765 const struct { 772 const struct {
766 const char* permission_name; 773 const char* permission_name;
767 bool expect_success; 774 bool expect_success;
768 } kTests[] = { 775 } kTests[] = {
769 // Negative test. 776 // Negative test.
770 { "non_existing_permission", false }, 777 { "non_existing_permission", false },
771 // Test default module/package permission. 778 // Test default module/package permission.
772 { "browserAction", true }, 779 { "browserAction", true },
773 { "devtools", true }, 780 { "devtools", true },
774 { "extension", true }, 781 { "extension", true },
(...skipping 18 matching lines...) Expand all
793 800
794 scoped_refptr<PermissionSet> empty = new PermissionSet(); 801 scoped_refptr<PermissionSet> empty = new PermissionSet();
795 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { 802 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
796 EXPECT_EQ(kTests[i].expect_success, 803 EXPECT_EQ(kTests[i].expect_success,
797 empty->HasAccessToFunction(kTests[i].permission_name)) 804 empty->HasAccessToFunction(kTests[i].permission_name))
798 << "Permission being tested: " << kTests[i].permission_name; 805 << "Permission being tested: " << kTests[i].permission_name;
799 } 806 }
800 } 807 }
801 808
802 // Tests the default permissions (empty API permission set). 809 // Tests the default permissions (empty API permission set).
803 TEST(PermissionsTest, DefaultAnyAPIAccess) { 810 TEST_F(PermissionsTest, DefaultAnyAPIAccess) {
804 const struct { 811 const struct {
805 const char* api_name; 812 const char* api_name;
806 bool expect_success; 813 bool expect_success;
807 } kTests[] = { 814 } kTests[] = {
808 // Negative test. 815 // Negative test.
809 { "non_existing_permission", false }, 816 { "non_existing_permission", false },
810 // Test default module/package permission. 817 // Test default module/package permission.
811 { "browserAction", true }, 818 { "browserAction", true },
812 { "devtools", true }, 819 { "devtools", true },
813 { "extension", true }, 820 { "extension", true },
(...skipping 10 matching lines...) Expand all
824 { "tabs", true}, 831 { "tabs", true},
825 }; 832 };
826 833
827 scoped_refptr<PermissionSet> empty = new PermissionSet(); 834 scoped_refptr<PermissionSet> empty = new PermissionSet();
828 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) { 835 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTests); ++i) {
829 EXPECT_EQ(kTests[i].expect_success, 836 EXPECT_EQ(kTests[i].expect_success,
830 empty->HasAnyAccessToAPI(kTests[i].api_name)); 837 empty->HasAnyAccessToAPI(kTests[i].api_name));
831 } 838 }
832 } 839 }
833 840
834 TEST(PermissionsTest, GetWarningMessages_ManyHosts) { 841 TEST_F(PermissionsTest, GetWarningMessages_ManyHosts) {
835 scoped_refptr<Extension> extension; 842 scoped_refptr<Extension> extension;
836 843
837 extension = LoadManifest("permissions", "many-hosts.json"); 844 extension = LoadManifest("permissions", "many-hosts.json");
838 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 845 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
839 ASSERT_EQ(1u, warnings.size()); 846 ASSERT_EQ(1u, warnings.size());
840 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com", 847 EXPECT_EQ("Access your data on encrypted.google.com and www.google.com",
841 UTF16ToUTF8(warnings[0])); 848 UTF16ToUTF8(warnings[0]));
842 } 849 }
843 850
844 TEST(PermissionsTest, GetWarningMessages_Plugins) { 851 TEST_F(PermissionsTest, GetWarningMessages_Plugins) {
845 scoped_refptr<Extension> extension; 852 scoped_refptr<Extension> extension;
846 scoped_refptr<PermissionSet> permissions; 853 scoped_refptr<PermissionSet> permissions;
847 854
848 extension = LoadManifest("permissions", "plugins.json"); 855 extension = LoadManifest("permissions", "plugins.json");
849 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 856 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
850 // We don't parse the plugins key on Chrome OS, so it should not ask for any 857 // We don't parse the plugins key on Chrome OS, so it should not ask for any
851 // permissions. 858 // permissions.
852 #if defined(OS_CHROMEOS) 859 #if defined(OS_CHROMEOS)
853 ASSERT_EQ(0u, warnings.size()); 860 ASSERT_EQ(0u, warnings.size());
854 #else 861 #else
855 ASSERT_EQ(1u, warnings.size()); 862 ASSERT_EQ(1u, warnings.size());
856 EXPECT_EQ("Access all data on your computer and the websites you visit", 863 EXPECT_EQ("Access all data on your computer and the websites you visit",
857 UTF16ToUTF8(warnings[0])); 864 UTF16ToUTF8(warnings[0]));
858 #endif 865 #endif
859 } 866 }
860 867
861 TEST(PermissionsTest, GetWarningMessages_AudioVideo) { 868 TEST_F(PermissionsTest, GetWarningMessages_AudioVideo) {
862 // Both audio and video present. 869 // Both audio and video present.
863 scoped_refptr<Extension> extension = 870 scoped_refptr<Extension> extension =
864 LoadManifest("permissions", "audio-video.json"); 871 LoadManifest("permissions", "audio-video.json");
865 PermissionSet* set = 872 PermissionSet* set =
866 const_cast<PermissionSet*>( 873 const_cast<PermissionSet*>(
867 extension->GetActivePermissions().get()); 874 extension->GetActivePermissions().get());
868 std::vector<string16> warnings = 875 std::vector<string16> warnings =
869 set->GetWarningMessages(extension->GetType()); 876 set->GetWarningMessages(extension->GetType());
870 EXPECT_FALSE(Contains(warnings, "Use your microphone")); 877 EXPECT_FALSE(Contains(warnings, "Use your microphone"));
871 EXPECT_FALSE(Contains(warnings, "Use your camera")); 878 EXPECT_FALSE(Contains(warnings, "Use your camera"));
(...skipping 12 matching lines...) Expand all
884 // Just video present. 891 // Just video present.
885 set->apis_.erase(APIPermission::kAudioCapture); 892 set->apis_.erase(APIPermission::kAudioCapture);
886 set->apis_.insert(APIPermission::kVideoCapture); 893 set->apis_.insert(APIPermission::kVideoCapture);
887 warnings = set->GetWarningMessages(extension->GetType()); 894 warnings = set->GetWarningMessages(extension->GetType());
888 EXPECT_EQ(combined_size, warnings.size()); 895 EXPECT_EQ(combined_size, warnings.size());
889 EXPECT_FALSE(Contains(warnings, "Use your microphone")); 896 EXPECT_FALSE(Contains(warnings, "Use your microphone"));
890 EXPECT_FALSE(Contains(warnings, "Use your microphone and camera")); 897 EXPECT_FALSE(Contains(warnings, "Use your microphone and camera"));
891 EXPECT_TRUE(Contains(warnings, "Use your camera")); 898 EXPECT_TRUE(Contains(warnings, "Use your camera"));
892 } 899 }
893 900
894 TEST(PermissionsTest, GetWarningMessages_Serial) { 901 TEST_F(PermissionsTest, GetWarningMessages_Serial) {
895 scoped_refptr<Extension> extension = 902 scoped_refptr<Extension> extension =
896 LoadManifest("permissions", "serial.json"); 903 LoadManifest("permissions", "serial.json");
897 904
898 EXPECT_TRUE(extension->is_platform_app()); 905 EXPECT_TRUE(extension->is_platform_app());
899 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSerial)); 906 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSerial));
900 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 907 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
901 EXPECT_TRUE(Contains(warnings, 908 EXPECT_TRUE(Contains(warnings,
902 "Use serial devices attached to your computer")); 909 "Use serial devices attached to your computer"));
903 ASSERT_EQ(1u, warnings.size()); 910 ASSERT_EQ(1u, warnings.size());
904 } 911 }
905 912
906 TEST(PermissionsTest, GetWarningMessages_Socket_AnyHost) { 913 TEST_F(PermissionsTest, GetWarningMessages_Socket_AnyHost) {
907 extensions::Feature::ScopedCurrentChannel channel( 914 extensions::Feature::ScopedCurrentChannel channel(
908 chrome::VersionInfo::CHANNEL_DEV); 915 chrome::VersionInfo::CHANNEL_DEV);
909 916
910 scoped_refptr<Extension> extension = 917 scoped_refptr<Extension> extension =
911 LoadManifest("permissions", "socket_any_host.json"); 918 LoadManifest("permissions", "socket_any_host.json");
912 EXPECT_TRUE(extension->is_platform_app()); 919 EXPECT_TRUE(extension->is_platform_app());
913 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); 920 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket));
914 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 921 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
915 EXPECT_EQ(1u, warnings.size()); 922 EXPECT_EQ(1u, warnings.size());
916 EXPECT_TRUE(Contains(warnings, "Exchange data with any computer " 923 EXPECT_TRUE(Contains(warnings, "Exchange data with any computer "
917 "on the local network or internet")); 924 "on the local network or internet"));
918 } 925 }
919 926
920 TEST(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) { 927 TEST_F(PermissionsTest, GetWarningMessages_Socket_OneDomainTwoHostnames) {
921 extensions::Feature::ScopedCurrentChannel channel( 928 extensions::Feature::ScopedCurrentChannel channel(
922 chrome::VersionInfo::CHANNEL_DEV); 929 chrome::VersionInfo::CHANNEL_DEV);
923 930
924 scoped_refptr<Extension> extension = 931 scoped_refptr<Extension> extension =
925 LoadManifest("permissions", "socket_one_domain_two_hostnames.json"); 932 LoadManifest("permissions", "socket_one_domain_two_hostnames.json");
926 EXPECT_TRUE(extension->is_platform_app()); 933 EXPECT_TRUE(extension->is_platform_app());
927 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); 934 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket));
928 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 935 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
929 936
930 // Verify the warnings, including support for unicode characters, the fact 937 // Verify the warnings, including support for unicode characters, the fact
931 // that domain host warnings come before specific host warnings, and the fact 938 // that domain host warnings come before specific host warnings, and the fact
932 // that domains and hostnames are in alphabetical order regardless of the 939 // that domains and hostnames are in alphabetical order regardless of the
933 // order in the manifest file. 940 // order in the manifest file.
934 EXPECT_EQ(2u, warnings.size()); 941 EXPECT_EQ(2u, warnings.size());
935 if (warnings.size() > 0) 942 if (warnings.size() > 0)
936 EXPECT_EQ(warnings[0], 943 EXPECT_EQ(warnings[0],
937 UTF8ToUTF16("Exchange data with any computer in the domain " 944 UTF8ToUTF16("Exchange data with any computer in the domain "
938 "example.org")); 945 "example.org"));
939 if (warnings.size() > 1) 946 if (warnings.size() > 1)
940 EXPECT_EQ(warnings[1], 947 EXPECT_EQ(warnings[1],
941 UTF8ToUTF16("Exchange data with the computers named: " 948 UTF8ToUTF16("Exchange data with the computers named: "
942 "b\xC3\xA5r.example.com foo.example.com")); 949 "b\xC3\xA5r.example.com foo.example.com"));
943 // "\xC3\xA5" = UTF-8 for lowercase A with ring above 950 // "\xC3\xA5" = UTF-8 for lowercase A with ring above
944 } 951 }
945 952
946 TEST(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) { 953 TEST_F(PermissionsTest, GetWarningMessages_Socket_TwoDomainsOneHostname) {
947 extensions::Feature::ScopedCurrentChannel channel( 954 extensions::Feature::ScopedCurrentChannel channel(
948 chrome::VersionInfo::CHANNEL_DEV); 955 chrome::VersionInfo::CHANNEL_DEV);
949 956
950 scoped_refptr<Extension> extension = 957 scoped_refptr<Extension> extension =
951 LoadManifest("permissions", "socket_two_domains_one_hostname.json"); 958 LoadManifest("permissions", "socket_two_domains_one_hostname.json");
952 EXPECT_TRUE(extension->is_platform_app()); 959 EXPECT_TRUE(extension->is_platform_app());
953 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket)); 960 EXPECT_TRUE(extension->HasAPIPermission(APIPermission::kSocket));
954 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 961 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
955 962
956 // Verify the warnings, including the fact that domain host warnings come 963 // Verify the warnings, including the fact that domain host warnings come
957 // before specific host warnings and the fact that domains and hostnames are 964 // before specific host warnings and the fact that domains and hostnames are
958 // in alphabetical order regardless of the order in the manifest file. 965 // in alphabetical order regardless of the order in the manifest file.
959 EXPECT_EQ(2u, warnings.size()); 966 EXPECT_EQ(2u, warnings.size());
960 if (warnings.size() > 0) 967 if (warnings.size() > 0)
961 EXPECT_EQ(warnings[0], 968 EXPECT_EQ(warnings[0],
962 UTF8ToUTF16("Exchange data with any computer in the domains: " 969 UTF8ToUTF16("Exchange data with any computer in the domains: "
963 "example.com foo.example.org")); 970 "example.com foo.example.org"));
964 if (warnings.size() > 1) 971 if (warnings.size() > 1)
965 EXPECT_EQ(warnings[1], 972 EXPECT_EQ(warnings[1],
966 UTF8ToUTF16("Exchange data with the computer named " 973 UTF8ToUTF16("Exchange data with the computer named "
967 "bar.example.org")); 974 "bar.example.org"));
968 } 975 }
969 976
970 TEST(PermissionsTest, GetWarningMessages_PlatformApppHosts) { 977 TEST_F(PermissionsTest, GetWarningMessages_PlatformApppHosts) {
971 scoped_refptr<Extension> extension; 978 scoped_refptr<Extension> extension;
972 979
973 extension = LoadManifest("permissions", "platform_app_hosts.json"); 980 extension = LoadManifest("permissions", "platform_app_hosts.json");
974 EXPECT_TRUE(extension->is_platform_app()); 981 EXPECT_TRUE(extension->is_platform_app());
975 std::vector<string16> warnings = extension->GetPermissionMessageStrings(); 982 std::vector<string16> warnings = extension->GetPermissionMessageStrings();
976 ASSERT_EQ(0u, warnings.size()); 983 ASSERT_EQ(0u, warnings.size());
977 984
978 extension = LoadManifest("permissions", "platform_app_all_urls.json"); 985 extension = LoadManifest("permissions", "platform_app_all_urls.json");
979 EXPECT_TRUE(extension->is_platform_app()); 986 EXPECT_TRUE(extension->is_platform_app());
980 warnings = extension->GetPermissionMessageStrings(); 987 warnings = extension->GetPermissionMessageStrings();
981 ASSERT_EQ(0u, warnings.size()); 988 ASSERT_EQ(0u, warnings.size());
982 } 989 }
983 990
984 TEST(PermissionsTest, GetDistinctHostsForDisplay) { 991 TEST_F(PermissionsTest, GetDistinctHostsForDisplay) {
985 scoped_refptr<PermissionSet> perm_set; 992 scoped_refptr<PermissionSet> perm_set;
986 APIPermissionSet empty_perms; 993 APIPermissionSet empty_perms;
987 std::set<std::string> expected; 994 std::set<std::string> expected;
988 expected.insert("www.foo.com"); 995 expected.insert("www.foo.com");
989 expected.insert("www.bar.com"); 996 expected.insert("www.bar.com");
990 expected.insert("www.baz.com"); 997 expected.insert("www.baz.com");
991 URLPatternSet explicit_hosts; 998 URLPatternSet explicit_hosts;
992 URLPatternSet scriptable_hosts; 999 URLPatternSet scriptable_hosts;
993 1000
994 { 1001 {
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1130 1137
1131 explicit_hosts.AddPattern( 1138 explicit_hosts.AddPattern(
1132 URLPattern(URLPattern::SCHEME_FILE, "file:///*")); 1139 URLPattern(URLPattern::SCHEME_FILE, "file:///*"));
1133 1140
1134 perm_set = new PermissionSet( 1141 perm_set = new PermissionSet(
1135 empty_perms, explicit_hosts, scriptable_hosts); 1142 empty_perms, explicit_hosts, scriptable_hosts);
1136 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay()); 1143 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
1137 } 1144 }
1138 } 1145 }
1139 1146
1140 TEST(PermissionsTest, GetDistinctHostsForDisplay_ComIsBestRcd) { 1147 TEST_F(PermissionsTest, GetDistinctHostsForDisplay_ComIsBestRcd) {
1141 scoped_refptr<PermissionSet> perm_set; 1148 scoped_refptr<PermissionSet> perm_set;
1142 APIPermissionSet empty_perms; 1149 APIPermissionSet empty_perms;
1143 URLPatternSet explicit_hosts; 1150 URLPatternSet explicit_hosts;
1144 URLPatternSet scriptable_hosts; 1151 URLPatternSet scriptable_hosts;
1145 explicit_hosts.AddPattern( 1152 explicit_hosts.AddPattern(
1146 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path")); 1153 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1147 explicit_hosts.AddPattern( 1154 explicit_hosts.AddPattern(
1148 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path")); 1155 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1149 explicit_hosts.AddPattern( 1156 explicit_hosts.AddPattern(
1150 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); 1157 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1151 explicit_hosts.AddPattern( 1158 explicit_hosts.AddPattern(
1152 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path")); 1159 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1153 explicit_hosts.AddPattern( 1160 explicit_hosts.AddPattern(
1154 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path")); 1161 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1155 explicit_hosts.AddPattern( 1162 explicit_hosts.AddPattern(
1156 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path")); 1163 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.com/path"));
1157 1164
1158 std::set<std::string> expected; 1165 std::set<std::string> expected;
1159 expected.insert("www.foo.com"); 1166 expected.insert("www.foo.com");
1160 perm_set = new PermissionSet( 1167 perm_set = new PermissionSet(
1161 empty_perms, explicit_hosts, scriptable_hosts); 1168 empty_perms, explicit_hosts, scriptable_hosts);
1162 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay()); 1169 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
1163 } 1170 }
1164 1171
1165 TEST(PermissionsTest, GetDistinctHostsForDisplay_NetIs2ndBestRcd) { 1172 TEST_F(PermissionsTest, GetDistinctHostsForDisplay_NetIs2ndBestRcd) {
1166 scoped_refptr<PermissionSet> perm_set; 1173 scoped_refptr<PermissionSet> perm_set;
1167 APIPermissionSet empty_perms; 1174 APIPermissionSet empty_perms;
1168 URLPatternSet explicit_hosts; 1175 URLPatternSet explicit_hosts;
1169 URLPatternSet scriptable_hosts; 1176 URLPatternSet scriptable_hosts;
1170 explicit_hosts.AddPattern( 1177 explicit_hosts.AddPattern(
1171 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path")); 1178 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1172 explicit_hosts.AddPattern( 1179 explicit_hosts.AddPattern(
1173 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path")); 1180 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1174 explicit_hosts.AddPattern( 1181 explicit_hosts.AddPattern(
1175 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); 1182 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1176 explicit_hosts.AddPattern( 1183 explicit_hosts.AddPattern(
1177 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path")); 1184 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.net/path"));
1178 explicit_hosts.AddPattern( 1185 explicit_hosts.AddPattern(
1179 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path")); 1186 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1180 // No http://www.foo.com/path 1187 // No http://www.foo.com/path
1181 1188
1182 std::set<std::string> expected; 1189 std::set<std::string> expected;
1183 expected.insert("www.foo.net"); 1190 expected.insert("www.foo.net");
1184 perm_set = new PermissionSet( 1191 perm_set = new PermissionSet(
1185 empty_perms, explicit_hosts, scriptable_hosts); 1192 empty_perms, explicit_hosts, scriptable_hosts);
1186 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay()); 1193 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
1187 } 1194 }
1188 1195
1189 TEST(PermissionsTest, 1196 TEST_F(PermissionsTest,
1190 GetDistinctHostsForDisplay_OrgIs3rdBestRcd) { 1197 GetDistinctHostsForDisplay_OrgIs3rdBestRcd) {
1191 scoped_refptr<PermissionSet> perm_set; 1198 scoped_refptr<PermissionSet> perm_set;
1192 APIPermissionSet empty_perms; 1199 APIPermissionSet empty_perms;
1193 URLPatternSet explicit_hosts; 1200 URLPatternSet explicit_hosts;
1194 URLPatternSet scriptable_hosts; 1201 URLPatternSet scriptable_hosts;
1195 explicit_hosts.AddPattern( 1202 explicit_hosts.AddPattern(
1196 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path")); 1203 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1197 explicit_hosts.AddPattern( 1204 explicit_hosts.AddPattern(
1198 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path")); 1205 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.org/path"));
1199 explicit_hosts.AddPattern( 1206 explicit_hosts.AddPattern(
1200 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); 1207 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1201 // No http://www.foo.net/path 1208 // No http://www.foo.net/path
1202 explicit_hosts.AddPattern( 1209 explicit_hosts.AddPattern(
1203 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path")); 1210 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1204 // No http://www.foo.com/path 1211 // No http://www.foo.com/path
1205 1212
1206 std::set<std::string> expected; 1213 std::set<std::string> expected;
1207 expected.insert("www.foo.org"); 1214 expected.insert("www.foo.org");
1208 perm_set = new PermissionSet( 1215 perm_set = new PermissionSet(
1209 empty_perms, explicit_hosts, scriptable_hosts); 1216 empty_perms, explicit_hosts, scriptable_hosts);
1210 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay()); 1217 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
1211 } 1218 }
1212 1219
1213 TEST(PermissionsTest, 1220 TEST_F(PermissionsTest,
1214 GetDistinctHostsForDisplay_FirstInListIs4thBestRcd) { 1221 GetDistinctHostsForDisplay_FirstInListIs4thBestRcd) {
1215 scoped_refptr<PermissionSet> perm_set; 1222 scoped_refptr<PermissionSet> perm_set;
1216 APIPermissionSet empty_perms; 1223 APIPermissionSet empty_perms;
1217 URLPatternSet explicit_hosts; 1224 URLPatternSet explicit_hosts;
1218 URLPatternSet scriptable_hosts; 1225 URLPatternSet scriptable_hosts;
1219 explicit_hosts.AddPattern( 1226 explicit_hosts.AddPattern(
1220 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path")); 1227 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.ca/path"));
1221 // No http://www.foo.org/path 1228 // No http://www.foo.org/path
1222 explicit_hosts.AddPattern( 1229 explicit_hosts.AddPattern(
1223 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path")); 1230 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.co.uk/path"));
1224 // No http://www.foo.net/path 1231 // No http://www.foo.net/path
1225 explicit_hosts.AddPattern( 1232 explicit_hosts.AddPattern(
1226 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path")); 1233 URLPattern(URLPattern::SCHEME_HTTP, "http://www.foo.jp/path"));
1227 // No http://www.foo.com/path 1234 // No http://www.foo.com/path
1228 1235
1229 std::set<std::string> expected; 1236 std::set<std::string> expected;
1230 expected.insert("www.foo.ca"); 1237 expected.insert("www.foo.ca");
1231 perm_set = new PermissionSet( 1238 perm_set = new PermissionSet(
1232 empty_perms, explicit_hosts, scriptable_hosts); 1239 empty_perms, explicit_hosts, scriptable_hosts);
1233 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay()); 1240 EXPECT_EQ(expected, perm_set->GetDistinctHostsForDisplay());
1234 } 1241 }
1235 1242
1236 TEST(PermissionsTest, HasLessHostPrivilegesThan) { 1243 TEST_F(PermissionsTest, HasLessHostPrivilegesThan) {
1237 URLPatternSet elist1; 1244 URLPatternSet elist1;
1238 URLPatternSet elist2; 1245 URLPatternSet elist2;
1239 URLPatternSet slist1; 1246 URLPatternSet slist1;
1240 URLPatternSet slist2; 1247 URLPatternSet slist2;
1241 scoped_refptr<PermissionSet> set1; 1248 scoped_refptr<PermissionSet> set1;
1242 scoped_refptr<PermissionSet> set2; 1249 scoped_refptr<PermissionSet> set2;
1243 APIPermissionSet empty_perms; 1250 APIPermissionSet empty_perms;
1244 elist1.AddPattern( 1251 elist1.AddPattern(
1245 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path")); 1252 URLPattern(URLPattern::SCHEME_HTTP, "http://www.google.com.hk/path"));
1246 elist1.AddPattern( 1253 elist1.AddPattern(
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1295 1302
1296 // Test that different subdomains count as different hosts. 1303 // Test that different subdomains count as different hosts.
1297 elist2.ClearPatterns(); 1304 elist2.ClearPatterns();
1298 elist2.AddPattern( 1305 elist2.AddPattern(
1299 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*")); 1306 URLPattern(URLPattern::SCHEME_HTTP, "http://mail.google.com/*"));
1300 set2 = new PermissionSet(empty_perms, elist2, slist2); 1307 set2 = new PermissionSet(empty_perms, elist2, slist2);
1301 EXPECT_TRUE(set1->HasLessHostPrivilegesThan(set2.get())); 1308 EXPECT_TRUE(set1->HasLessHostPrivilegesThan(set2.get()));
1302 EXPECT_TRUE(set2->HasLessHostPrivilegesThan(set1.get())); 1309 EXPECT_TRUE(set2->HasLessHostPrivilegesThan(set1.get()));
1303 } 1310 }
1304 1311
1305 TEST(PermissionsTest, GetAPIsAsStrings) { 1312 TEST_F(PermissionsTest, GetAPIsAsStrings) {
1306 APIPermissionSet apis; 1313 APIPermissionSet apis;
1307 URLPatternSet empty_set; 1314 URLPatternSet empty_set;
1308 1315
1309 apis.insert(APIPermission::kProxy); 1316 apis.insert(APIPermission::kProxy);
1310 apis.insert(APIPermission::kBackground); 1317 apis.insert(APIPermission::kBackground);
1311 apis.insert(APIPermission::kNotification); 1318 apis.insert(APIPermission::kNotification);
1312 apis.insert(APIPermission::kTab); 1319 apis.insert(APIPermission::kTab);
1313 1320
1314 scoped_refptr<PermissionSet> perm_set = new PermissionSet( 1321 scoped_refptr<PermissionSet> perm_set = new PermissionSet(
1315 apis, empty_set, empty_set); 1322 apis, empty_set, empty_set);
1316 std::set<std::string> api_names = perm_set->GetAPIsAsStrings(); 1323 std::set<std::string> api_names = perm_set->GetAPIsAsStrings();
1317 1324
1318 // The result is correct if it has the same number of elements 1325 // The result is correct if it has the same number of elements
1319 // and we can convert it back to the id set. 1326 // and we can convert it back to the id set.
1320 EXPECT_EQ(4u, api_names.size()); 1327 EXPECT_EQ(4u, api_names.size());
1321 EXPECT_EQ(apis, 1328 EXPECT_EQ(apis,
1322 PermissionsInfo::GetInstance()->GetAllByName(api_names)); 1329 PermissionsInfo::GetInstance()->GetAllByName(api_names));
1323 } 1330 }
1324 1331
1325 TEST(PermissionsTest, IsEmpty) { 1332 TEST_F(PermissionsTest, IsEmpty) {
1326 APIPermissionSet empty_apis; 1333 APIPermissionSet empty_apis;
1327 URLPatternSet empty_extent; 1334 URLPatternSet empty_extent;
1328 1335
1329 scoped_refptr<PermissionSet> empty = new PermissionSet(); 1336 scoped_refptr<PermissionSet> empty = new PermissionSet();
1330 EXPECT_TRUE(empty->IsEmpty()); 1337 EXPECT_TRUE(empty->IsEmpty());
1331 scoped_refptr<PermissionSet> perm_set; 1338 scoped_refptr<PermissionSet> perm_set;
1332 1339
1333 perm_set = new PermissionSet(empty_apis, empty_extent, empty_extent); 1340 perm_set = new PermissionSet(empty_apis, empty_extent, empty_extent);
1334 EXPECT_TRUE(perm_set->IsEmpty()); 1341 EXPECT_TRUE(perm_set->IsEmpty());
1335 1342
1336 APIPermissionSet non_empty_apis; 1343 APIPermissionSet non_empty_apis;
1337 non_empty_apis.insert(APIPermission::kBackground); 1344 non_empty_apis.insert(APIPermission::kBackground);
1338 perm_set = new PermissionSet( 1345 perm_set = new PermissionSet(
1339 non_empty_apis, empty_extent, empty_extent); 1346 non_empty_apis, empty_extent, empty_extent);
1340 EXPECT_FALSE(perm_set->IsEmpty()); 1347 EXPECT_FALSE(perm_set->IsEmpty());
1341 1348
1342 // Try non standard host 1349 // Try non standard host
1343 URLPatternSet non_empty_extent; 1350 URLPatternSet non_empty_extent;
1344 AddPattern(&non_empty_extent, "http://www.google.com/*"); 1351 AddPattern(&non_empty_extent, "http://www.google.com/*");
1345 1352
1346 perm_set = new PermissionSet( 1353 perm_set = new PermissionSet(
1347 empty_apis, non_empty_extent, empty_extent); 1354 empty_apis, non_empty_extent, empty_extent);
1348 EXPECT_FALSE(perm_set->IsEmpty()); 1355 EXPECT_FALSE(perm_set->IsEmpty());
1349 1356
1350 perm_set = new PermissionSet( 1357 perm_set = new PermissionSet(
1351 empty_apis, empty_extent, non_empty_extent); 1358 empty_apis, empty_extent, non_empty_extent);
1352 EXPECT_FALSE(perm_set->IsEmpty()); 1359 EXPECT_FALSE(perm_set->IsEmpty());
1353 } 1360 }
1354 1361
1355 TEST(PermissionsTest, ImpliedPermissions) { 1362 TEST_F(PermissionsTest, ImpliedPermissions) {
1356 URLPatternSet empty_extent; 1363 URLPatternSet empty_extent;
1357 APIPermissionSet apis; 1364 APIPermissionSet apis;
1358 apis.insert(APIPermission::kWebRequest); 1365 apis.insert(APIPermission::kWebRequest);
1359 apis.insert(APIPermission::kFileBrowserHandler); 1366 apis.insert(APIPermission::kFileBrowserHandler);
1360 EXPECT_EQ(2U, apis.size()); 1367 EXPECT_EQ(2U, apis.size());
1361 1368
1362 scoped_refptr<PermissionSet> perm_set; 1369 scoped_refptr<PermissionSet> perm_set;
1363 perm_set = new PermissionSet(apis, empty_extent, empty_extent); 1370 perm_set = new PermissionSet(apis, empty_extent, empty_extent);
1364 EXPECT_EQ(4U, perm_set->apis().size()); 1371 EXPECT_EQ(4U, perm_set->apis().size());
1365 } 1372 }
1366 1373
1367 } // namespace extensions 1374 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698