| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/file_path.h" | 5 #include "base/file_path.h" |
| 6 #include "base/file_util.h" | 6 #include "base/file_util.h" |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 #include "base/path_service.h" | 8 #include "base/path_service.h" |
| 9 #include "chrome/common/chrome_paths.h" | 9 #include "chrome/common/chrome_paths.h" |
| 10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
| (...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 478 |
| 479 data.clear(); | 479 data.clear(); |
| 480 result.clear(); | 480 result.clear(); |
| 481 path = path.DirName().AppendASCII("bad_magic.crx"); | 481 path = path.DirName().AppendASCII("bad_magic.crx"); |
| 482 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); | 482 ASSERT_TRUE(file_util::ReadFileToString(path, &data)); |
| 483 EXPECT_TRUE(net::SniffMimeType(data.c_str(), data.size(), | 483 EXPECT_TRUE(net::SniffMimeType(data.c_str(), data.size(), |
| 484 GURL("http://www.example.com/foo.crx"), "", &result)); | 484 GURL("http://www.example.com/foo.crx"), "", &result)); |
| 485 EXPECT_EQ("application/octet-stream", result); | 485 EXPECT_EQ("application/octet-stream", result); |
| 486 } | 486 } |
| 487 | 487 |
| 488 TEST(ExtensionTest, PermissionClass) { | 488 static Extension* LoadManifest(const std::string& dir, |
| 489 #if defined(OS_WIN) | 489 const std::string& test_file) { |
| 490 FilePath path(FILE_PATH_LITERAL("C:\\foo")); | 490 FilePath path; |
| 491 #elif defined(OS_POSIX) | 491 PathService::Get(chrome::DIR_TEST_DATA, &path); |
| 492 FilePath path(FILE_PATH_LITERAL("/foo")); | 492 path = path.AppendASCII("extensions") |
| 493 #endif | 493 .AppendASCII(dir) |
| 494 Extension::ResetGeneratedIdCounter(); | 494 .AppendASCII(test_file); |
| 495 | 495 |
| 496 Extension extension(path); | 496 JSONFileValueSerializer serializer(path); |
| 497 scoped_ptr<Value> result(serializer.Deserialize(NULL)); |
| 498 if (!result.get()) |
| 499 return NULL; |
| 500 |
| 497 std::string error; | 501 std::string error; |
| 498 DictionaryValue bare_manifest; | 502 scoped_ptr<Extension> extension(new Extension); |
| 499 scoped_ptr<DictionaryValue> manifest; | 503 extension->InitFromValue(*static_cast<DictionaryValue*>(result.get()), |
| 504 false, &error); |
| 500 | 505 |
| 501 // Start with a minimalist extension. | 506 result.release(); |
| 502 bare_manifest.SetString(keys::kVersion, "1.0.0.0"); | 507 return extension.release(); |
| 503 bare_manifest.SetString(keys::kName, "my extension"); | 508 } |
| 504 EXPECT_TRUE(extension.InitFromValue(bare_manifest, false, &error)); | |
| 505 EXPECT_EQ(Extension::PERMISSION_CLASS_LOW, extension.GetPermissionClass()); | |
| 506 | 509 |
| 507 // Toolstrips don't affect the permission class. | 510 TEST(ExtensionTest, EffectiveHostPermissions) { |
| 508 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 511 scoped_ptr<Extension> extension; |
| 509 manifest->Set(keys::kToolstrips, ValueFromJSON( | 512 std::set<std::string> hosts; |
| 510 "[\"toolstrip.html\", \"toolstrip2.html\"]")); | |
| 511 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | |
| 512 EXPECT_EQ(Extension::PERMISSION_CLASS_LOW, extension.GetPermissionClass()); | |
| 513 | 513 |
| 514 // Requesting API permissions bumps you to medium. | 514 extension.reset(LoadManifest("effective_host_permissions", "empty.json")); |
| 515 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 515 EXPECT_EQ(0, extension->GetEffectiveHostPermissions().size()); |
| 516 manifest->Set(keys::kPermissions, ValueFromJSON( | 516 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 517 "[\"tabs\", \"bookmarks\"]")); | |
| 518 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | |
| 519 EXPECT_EQ(Extension::PERMISSION_CLASS_MEDIUM, extension.GetPermissionClass()); | |
| 520 | 517 |
| 521 // Adding a content script bumps you to high. | 518 extension.reset(LoadManifest("effective_host_permissions", "one_host.json")); |
| 522 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 519 hosts = extension->GetEffectiveHostPermissions(); |
| 523 manifest->Set(keys::kContentScripts, ValueFromJSON( | 520 EXPECT_EQ(1, hosts.size()); |
| 524 "[{" | 521 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 525 " \"matches\": [\"http://*.google.com/*\"]," | 522 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 526 " \"js\": [\"script.js\"]" | |
| 527 "}]")); | |
| 528 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | |
| 529 EXPECT_EQ(Extension::PERMISSION_CLASS_HIGH, extension.GetPermissionClass()); | |
| 530 | 523 |
| 531 // ... or asking for a host permission. | 524 extension.reset(LoadManifest("effective_host_permissions", |
| 532 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 525 "one_host_wildcard.json")); |
| 533 manifest->Set(keys::kPermissions, ValueFromJSON( | 526 hosts = extension->GetEffectiveHostPermissions(); |
| 534 "[\"tabs\", \"http://google.com/*\"]")); | 527 EXPECT_EQ(1, hosts.size()); |
| 535 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | 528 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 536 EXPECT_EQ(Extension::PERMISSION_CLASS_HIGH, extension.GetPermissionClass()); | 529 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 537 | 530 |
| 538 // Using native code (NPAPI) is automatically the max class. | 531 extension.reset(LoadManifest("effective_host_permissions", |
| 539 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 532 "two_hosts.json")); |
| 540 manifest->Set(keys::kPlugins, ValueFromJSON( | 533 hosts = extension->GetEffectiveHostPermissions(); |
| 541 "[{\"path\": \"harddrive_exploder.dll\"}]")); | 534 EXPECT_EQ(2, hosts.size()); |
| 542 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | 535 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 543 EXPECT_EQ(Extension::PERMISSION_CLASS_FULL, extension.GetPermissionClass()); | 536 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 537 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 544 | 538 |
| 545 // Using everything at once should obviously be the max class as well. | 539 extension.reset(LoadManifest("effective_host_permissions", |
| 546 manifest.reset(static_cast<DictionaryValue*>(bare_manifest.DeepCopy())); | 540 "duplicate_host.json")); |
| 547 manifest->Set(keys::kPlugins, ValueFromJSON( | 541 hosts = extension->GetEffectiveHostPermissions(); |
| 548 "[{\"path\": \"harddrive_exploder.dll\"}]")); | 542 EXPECT_EQ(1, hosts.size()); |
| 549 manifest->Set(keys::kPermissions, ValueFromJSON( | 543 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 550 "[\"tabs\", \"http://google.com/*\"]")); | 544 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 551 manifest->Set(keys::kContentScripts, ValueFromJSON( | 545 |
| 552 "[{" | 546 extension.reset(LoadManifest("effective_host_permissions", |
| 553 " \"matches\": [\"http://*.google.com/*\"]," | 547 "https_not_considered.json")); |
| 554 " \"js\": [\"script.js\"]" | 548 hosts = extension->GetEffectiveHostPermissions(); |
| 555 "}]")); | 549 EXPECT_EQ(1, hosts.size()); |
| 556 EXPECT_TRUE(extension.InitFromValue(*manifest, false, &error)); | 550 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 557 EXPECT_EQ(Extension::PERMISSION_CLASS_FULL, extension.GetPermissionClass()); | 551 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 552 |
| 553 extension.reset(LoadManifest("effective_host_permissions", |
| 554 "two_content_scripts.json")); |
| 555 hosts = extension->GetEffectiveHostPermissions(); |
| 556 EXPECT_EQ(3, hosts.size()); |
| 557 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 558 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 559 EXPECT_TRUE(hosts.find("news.ycombinator.com") != hosts.end()); |
| 560 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 561 |
| 562 extension.reset(LoadManifest("effective_host_permissions", |
| 563 "duplicate_content_script.json")); |
| 564 hosts = extension->GetEffectiveHostPermissions(); |
| 565 EXPECT_EQ(2, hosts.size()); |
| 566 EXPECT_TRUE(hosts.find("google.com") != hosts.end()); |
| 567 EXPECT_TRUE(hosts.find("www.reddit.com") != hosts.end()); |
| 568 EXPECT_FALSE(extension->HasAccessToAllHosts()); |
| 569 |
| 570 extension.reset(LoadManifest("effective_host_permissions", |
| 571 "all_hosts.json")); |
| 572 hosts = extension->GetEffectiveHostPermissions(); |
| 573 EXPECT_EQ(1, hosts.size()); |
| 574 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 575 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 576 |
| 577 extension.reset(LoadManifest("effective_host_permissions", |
| 578 "all_hosts2.json")); |
| 579 hosts = extension->GetEffectiveHostPermissions(); |
| 580 EXPECT_EQ(2, hosts.size()); |
| 581 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 582 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 583 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 584 |
| 585 extension.reset(LoadManifest("effective_host_permissions", |
| 586 "all_hosts3.json")); |
| 587 hosts = extension->GetEffectiveHostPermissions(); |
| 588 EXPECT_EQ(2, hosts.size()); |
| 589 EXPECT_TRUE(hosts.find("") != hosts.end()); |
| 590 EXPECT_TRUE(hosts.find("www.google.com") != hosts.end()); |
| 591 EXPECT_TRUE(extension->HasAccessToAllHosts()); |
| 558 } | 592 } |
| 593 |
| 594 TEST(ExtensionTest, AllowSilentUpgrade) { |
| 595 const struct { |
| 596 const char* base_name; |
| 597 bool expect_success; |
| 598 } kTests[] = { |
| 599 { "allhosts1", true }, // all -> all |
| 600 { "allhosts2", true }, // all -> one |
| 601 { "allhosts3", false }, // one -> all |
| 602 { "hosts1", true }, // http://a,http://b -> http://a,http://b |
| 603 { "hosts2", true }, // http://a,http://b -> https://a,http://*.b |
| 604 { "hosts3", true }, // http://a,http://b -> http://a |
| 605 { "hosts4", false }, // http://a -> http://a,http://b |
| 606 { "permissions1", true}, // tabs -> tabs |
| 607 { "permissions2", true}, // tabs -> tabs,bookmarks |
| 608 { "permissions3", false}, // http://a -> http://a,tabs |
| 609 { "permissions4", true}, // plugin -> plugin,tabs |
| 610 { "plugin1", true}, // plugin -> plugin |
| 611 { "plugin2", true}, // plugin -> none |
| 612 { "plugin3", false} // none -> plugin |
| 613 }; |
| 614 |
| 615 for (size_t i = 0; i < arraysize(kTests); ++i) { |
| 616 scoped_ptr<Extension> old_extension( |
| 617 LoadManifest("allow_silent_upgrade", |
| 618 std::string(kTests[i].base_name) + "_old.json")); |
| 619 scoped_ptr<Extension> new_extension( |
| 620 LoadManifest("allow_silent_upgrade", |
| 621 std::string(kTests[i].base_name) + "_new.json")); |
| 622 |
| 623 EXPECT_EQ(kTests[i].expect_success, |
| 624 Extension::AllowSilentUpgrade(old_extension.get(), |
| 625 new_extension.get())) |
| 626 << kTests[i].base_name; |
| 627 } |
| 628 } |
| OLD | NEW |