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