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

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

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

Powered by Google App Engine
This is Rietveld 408576698