| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 <algorithm> | 5 #include <algorithm> |
| 6 #include <vector> | 6 #include <vector> |
| 7 | 7 |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/json_reader.h" | 10 #include "base/json_reader.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 #if defined(OS_WIN) | 30 #if defined(OS_WIN) |
| 31 #include "base/registry.h" | 31 #include "base/registry.h" |
| 32 #endif | 32 #endif |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 // Extension ids used during testing. | 36 // Extension ids used during testing. |
| 37 const char* const all_zero = "0000000000000000000000000000000000000000"; | 37 const char* const all_zero = "0000000000000000000000000000000000000000"; |
| 38 const char* const zero_n_one = "0000000000000000000000000000000000000001"; | 38 const char* const zero_n_one = "0000000000000000000000000000000000000001"; |
| 39 const char* const good0 = "fc6f6ba6693faf6773c13701019f2e7a12f0febe"; | 39 const char* const good0 = "00123456789abcdef0123456789abcdef0123456"; |
| 40 const char* const good1 = "e5ead92b2c6795c1d2b92df9c5cb37de5582471a"; | 40 const char* const good1 = "10123456789abcdef0123456789abcdef0123456"; |
| 41 const char* const good2 = "a37fed892f622823f4daaec4426a32fc7f6147dc"; | 41 const char* const good2 = "20123456789abcdef0123456789abcdef0123456"; |
| 42 const char* const good_crx = "b3dd733cd71a98fa83f387455e12f5c5501c519e"; | 42 const char* const good_crx = "00123456789abcdef0123456789abcdef0123456"; |
| 43 const char* const page_action = "a4ca7d01469a010acb200568a0b8f4d9b3ac1f91"; | 43 const char* const page_action = "8a5e4cb023c61b431e9b603a97c293429ce057c8"; |
| 44 const char* const theme_crx = "80c45f5ae9e0f839d105c6a6d2461a036bc40a04"; | 44 const char* const theme_crx = "f0123456789abcdef0123456789abcdef0126456"; |
| 45 const char* const theme2_crx = "f9f6c52c01efdd5edd7c396b5f995a15fc7ad6d1"; | 45 const char* const theme2_crx = "f0123456789adddef0123456789abcdef0126456"; |
| 46 | 46 |
| 47 struct ExtensionsOrder { | 47 struct ExtensionsOrder { |
| 48 bool operator()(const Extension* a, const Extension* b) { | 48 bool operator()(const Extension* a, const Extension* b) { |
| 49 return a->name() < b->name(); | 49 return a->name() < b->name(); |
| 50 } | 50 } |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 static std::vector<std::string> GetErrors() { | 53 static std::vector<std::string> GetErrors() { |
| 54 const std::vector<std::string>* errors = | 54 const std::vector<std::string>* errors = |
| 55 ExtensionErrorReporter::GetInstance()->GetErrors(); | 55 ExtensionErrorReporter::GetInstance()->GetErrors(); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 ASSERT_TRUE(service_->Init()); | 228 ASSERT_TRUE(service_->Init()); |
| 229 loop_.RunAllPending(); | 229 loop_.RunAllPending(); |
| 230 | 230 |
| 231 std::vector<std::string> errors = GetErrors(); | 231 std::vector<std::string> errors = GetErrors(); |
| 232 for (std::vector<std::string>::iterator err = errors.begin(); | 232 for (std::vector<std::string>::iterator err = errors.begin(); |
| 233 err != errors.end(); ++err) { | 233 err != errors.end(); ++err) { |
| 234 LOG(ERROR) << *err; | 234 LOG(ERROR) << *err; |
| 235 } | 235 } |
| 236 ASSERT_EQ(3u, loaded_.size()); | 236 ASSERT_EQ(3u, loaded_.size()); |
| 237 | 237 |
| 238 EXPECT_EQ(std::string(good0), loaded_[0]->id()); | 238 EXPECT_EQ(std::string(good_crx), loaded_[0]->id()); |
| 239 EXPECT_EQ(std::string("My extension 1"), | 239 EXPECT_EQ(std::string("My extension 1"), |
| 240 loaded_[0]->name()); | 240 loaded_[0]->name()); |
| 241 EXPECT_EQ(std::string("The first extension that I made."), | 241 EXPECT_EQ(std::string("The first extension that I made."), |
| 242 loaded_[0]->description()); | 242 loaded_[0]->description()); |
| 243 EXPECT_EQ(Extension::INTERNAL, loaded_[0]->location()); | 243 EXPECT_EQ(Extension::INTERNAL, loaded_[0]->location()); |
| 244 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id())); | 244 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id())); |
| 245 EXPECT_EQ(3u, service_->extensions()->size()); | 245 EXPECT_EQ(3u, service_->extensions()->size()); |
| 246 | 246 |
| 247 ValidatePrefKeyCount(3); | 247 ValidatePrefKeyCount(3); |
| 248 ValidatePref(good0, L"state", Extension::ENABLED); | 248 ValidatePref(good_crx, L"state", Extension::ENABLED); |
| 249 ValidatePref(good0, L"location", Extension::INTERNAL); | 249 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| 250 ValidatePref(good1, L"state", Extension::ENABLED); | 250 ValidatePref(good1, L"state", Extension::ENABLED); |
| 251 ValidatePref(good1, L"location", Extension::INTERNAL); | 251 ValidatePref(good1, L"location", Extension::INTERNAL); |
| 252 ValidatePref(good2, L"state", Extension::ENABLED); | 252 ValidatePref(good2, L"state", Extension::ENABLED); |
| 253 ValidatePref(good2, L"location", Extension::INTERNAL); | 253 ValidatePref(good2, L"location", Extension::INTERNAL); |
| 254 | 254 |
| 255 Extension* extension = loaded_[0]; | 255 Extension* extension = loaded_[0]; |
| 256 const UserScriptList& scripts = extension->content_scripts(); | 256 const UserScriptList& scripts = extension->content_scripts(); |
| 257 const std::vector<std::string>& toolstrips = extension->toolstrips(); | 257 const std::vector<std::string>& toolstrips = extension->toolstrips(); |
| 258 ASSERT_EQ(2u, scripts.size()); | 258 ASSERT_EQ(2u, scripts.size()); |
| 259 EXPECT_EQ(2u, scripts[0].url_patterns().size()); | 259 EXPECT_EQ(2u, scripts[0].url_patterns().size()); |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 391 ValidatePref(good_crx, L"state", Extension::ENABLED); | 391 ValidatePref(good_crx, L"state", Extension::ENABLED); |
| 392 ValidatePref(good_crx, L"location", Extension::INTERNAL); | 392 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| 393 | 393 |
| 394 // An extension with page actions. | 394 // An extension with page actions. |
| 395 path = extensions_path.AppendASCII("page_action.crx"); | 395 path = extensions_path.AppendASCII("page_action.crx"); |
| 396 InstallExtension(path, true); | 396 InstallExtension(path, true); |
| 397 ValidatePrefKeyCount(++pref_count); | 397 ValidatePrefKeyCount(++pref_count); |
| 398 ValidatePref(page_action, L"state", Extension::ENABLED); | 398 ValidatePref(page_action, L"state", Extension::ENABLED); |
| 399 ValidatePref(page_action, L"location", Extension::INTERNAL); | 399 ValidatePref(page_action, L"location", Extension::INTERNAL); |
| 400 | 400 |
| 401 // Bad signature. | |
| 402 path = extensions_path.AppendASCII("bad_signature.crx"); | |
| 403 InstallExtension(path, false); | |
| 404 | |
| 405 // 0-length extension file. | 401 // 0-length extension file. |
| 406 path = extensions_path.AppendASCII("not_an_extension.crx"); | 402 path = extensions_path.AppendASCII("not_an_extension.crx"); |
| 407 InstallExtension(path, false); | 403 InstallExtension(path, false); |
| 408 ValidatePrefKeyCount(pref_count); | 404 ValidatePrefKeyCount(pref_count); |
| 409 | 405 |
| 410 // Bad magic number. | 406 // Bad magic number. |
| 411 path = extensions_path.AppendASCII("bad_magic.crx"); | 407 path = extensions_path.AppendASCII("bad_magic.crx"); |
| 412 InstallExtension(path, false); | 408 InstallExtension(path, false); |
| 413 ValidatePrefKeyCount(pref_count); | 409 ValidatePrefKeyCount(pref_count); |
| 414 | 410 |
| 411 // Poorly formed JSON. |
| 412 path = extensions_path.AppendASCII("bad_json.crx"); |
| 413 InstallExtension(path, false); |
| 414 ValidatePrefKeyCount(pref_count); |
| 415 |
| 416 // Incorrect zip hash. |
| 417 path = extensions_path.AppendASCII("bad_hash.crx"); |
| 418 InstallExtension(path, false); |
| 419 ValidatePrefKeyCount(pref_count); |
| 420 |
| 415 // TODO(erikkay): add more tests for many of the failure cases. | 421 // TODO(erikkay): add more tests for many of the failure cases. |
| 416 // TODO(erikkay): add tests for upgrade cases. | 422 // TODO(erikkay): add tests for upgrade cases. |
| 417 } | 423 } |
| 418 | 424 |
| 419 #if defined(OS_WIN) // TODO(port) | 425 #if defined(OS_WIN) // TODO(port) |
| 420 // Test Packaging and installing an extension. | 426 // Test Packaging and installing an extension. |
| 421 // TODO(aa): add a test that uses an openssl-generate private key. | 427 // TODO(aa): add a test that uses an openssl-generate private key. |
| 422 // TODO(rafaelw): add more tests for failure cases. | 428 // TODO(rafaelw): add more tests for failure cases. |
| 423 TEST_F(ExtensionsServiceTest, PackExtension) { | 429 TEST_F(ExtensionsServiceTest, PackExtension) { |
| 424 SetExtensionsEnabled(true); | 430 SetExtensionsEnabled(true); |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 546 loop_.RunAllPending(); | 552 loop_.RunAllPending(); |
| 547 | 553 |
| 548 ASSERT_FALSE(installed_); | 554 ASSERT_FALSE(installed_); |
| 549 ASSERT_EQ(0u, loaded_.size()); | 555 ASSERT_EQ(0u, loaded_.size()); |
| 550 ASSERT_EQ(0u, GetErrors().size()); | 556 ASSERT_EQ(0u, GetErrors().size()); |
| 551 ValidatePrefKeyCount(1); | 557 ValidatePrefKeyCount(1); |
| 552 ValidatePref(good_crx, L"state", Extension::ENABLED); | 558 ValidatePref(good_crx, L"state", Extension::ENABLED); |
| 553 ValidatePref(good_crx, L"location", Extension::INTERNAL); | 559 ValidatePref(good_crx, L"location", Extension::INTERNAL); |
| 554 } | 560 } |
| 555 | 561 |
| 556 // Test upgrading a signed extension. | |
| 557 TEST_F(ExtensionsServiceTest, UpgradeSignedGood) { | |
| 558 FilePath extensions_path; | |
| 559 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | |
| 560 extensions_path = extensions_path.AppendASCII("extensions"); | |
| 561 | |
| 562 FilePath path = extensions_path.AppendASCII("good.crx"); | |
| 563 service_->InstallExtension(path); | |
| 564 loop_.RunAllPending(); | |
| 565 | |
| 566 ASSERT_TRUE(installed_); | |
| 567 ASSERT_EQ(1u, loaded_.size()); | |
| 568 ASSERT_EQ(0u, GetErrors().size()); | |
| 569 | |
| 570 // Upgrade to version 2.0 | |
| 571 path = extensions_path.AppendASCII("good2.crx"); | |
| 572 service_->InstallExtension(path); | |
| 573 loop_.RunAllPending(); | |
| 574 | |
| 575 ASSERT_TRUE(installed_); | |
| 576 ASSERT_EQ(2u, loaded_.size()); | |
| 577 ASSERT_EQ(0u, GetErrors().size()); | |
| 578 } | |
| 579 | |
| 580 // Test upgrading a signed extension with a bad signature. | |
| 581 TEST_F(ExtensionsServiceTest, UpgradeSignedBad) { | |
| 582 FilePath extensions_path; | |
| 583 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | |
| 584 extensions_path = extensions_path.AppendASCII("extensions"); | |
| 585 | |
| 586 FilePath path = extensions_path.AppendASCII("good.crx"); | |
| 587 service_->InstallExtension(path); | |
| 588 loop_.RunAllPending(); | |
| 589 | |
| 590 ASSERT_TRUE(installed_); | |
| 591 ASSERT_EQ(1u, loaded_.size()); | |
| 592 ASSERT_EQ(0u, GetErrors().size()); | |
| 593 installed_ = NULL; | |
| 594 | |
| 595 // Try upgrading with a bad signature. This should fail during the unpack, | |
| 596 // because the key will not match the signature. | |
| 597 path = extensions_path.AppendASCII("good2_bad_signature.crx"); | |
| 598 service_->InstallExtension(path); | |
| 599 loop_.RunAllPending(); | |
| 600 | |
| 601 ASSERT_FALSE(installed_); | |
| 602 ASSERT_EQ(1u, loaded_.size()); | |
| 603 ASSERT_EQ(1u, GetErrors().size()); | |
| 604 } | |
| 605 | |
| 606 // Tests uninstalling normal extensions | 562 // Tests uninstalling normal extensions |
| 607 TEST_F(ExtensionsServiceTest, UninstallExtension) { | 563 TEST_F(ExtensionsServiceTest, UninstallExtension) { |
| 608 FilePath extensions_path; | 564 FilePath extensions_path; |
| 609 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 565 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 610 extensions_path = extensions_path.AppendASCII("extensions"); | 566 extensions_path = extensions_path.AppendASCII("extensions"); |
| 611 | 567 |
| 612 // A simple extension that should install without error. | 568 // A simple extension that should install without error. |
| 613 FilePath path = extensions_path.AppendASCII("good.crx"); | 569 FilePath path = extensions_path.AppendASCII("good.crx"); |
| 614 InstallExtension(path, true); | 570 InstallExtension(path, true); |
| 615 | 571 |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 EXPECT_EQ(id, unloaded_id_); | 649 EXPECT_EQ(id, unloaded_id_); |
| 694 | 650 |
| 695 ValidatePrefKeyCount(1); | 651 ValidatePrefKeyCount(1); |
| 696 ValidatePref(good0, L"state", Extension::DISABLED); | 652 ValidatePref(good0, L"state", Extension::DISABLED); |
| 697 ValidatePref(good0, L"location", Extension::INTERNAL); | 653 ValidatePref(good0, L"location", Extension::INTERNAL); |
| 698 } | 654 } |
| 699 | 655 |
| 700 // Tests that we generate IDs when they are not specified in the manifest for | 656 // Tests that we generate IDs when they are not specified in the manifest for |
| 701 // --load-extension. | 657 // --load-extension. |
| 702 TEST_F(ExtensionsServiceTest, GenerateID) { | 658 TEST_F(ExtensionsServiceTest, GenerateID) { |
| 703 Extension::ResetGeneratedIdCounter(); | |
| 704 | |
| 705 FilePath extensions_path; | 659 FilePath extensions_path; |
| 706 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); | 660 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); |
| 707 extensions_path = extensions_path.AppendASCII("extensions"); | 661 extensions_path = extensions_path.AppendASCII("extensions"); |
| 708 | 662 |
| 709 FilePath no_id_ext = extensions_path.AppendASCII("no_id"); | 663 FilePath no_id_ext = extensions_path.AppendASCII("no_id"); |
| 710 service_->LoadExtension(no_id_ext); | 664 service_->LoadExtension(no_id_ext); |
| 711 loop_.RunAllPending(); | 665 loop_.RunAllPending(); |
| 712 EXPECT_EQ(0u, GetErrors().size()); | 666 EXPECT_EQ(0u, GetErrors().size()); |
| 713 ASSERT_EQ(1u, loaded_.size()); | 667 ASSERT_EQ(1u, loaded_.size()); |
| 714 std::string id1 = loaded_[0]->id(); | 668 std::string id1 = loaded_[0]->id(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 736 #if defined(OS_WIN) | 690 #if defined(OS_WIN) |
| 737 | 691 |
| 738 TEST_F(ExtensionsServiceTest, ExternalInstallRegistry) { | 692 TEST_F(ExtensionsServiceTest, ExternalInstallRegistry) { |
| 739 // Register a test extension externally using the registry. | 693 // Register a test extension externally using the registry. |
| 740 FilePath source_path; | 694 FilePath source_path; |
| 741 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_path)); | 695 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_path)); |
| 742 source_path = source_path.AppendASCII("extensions").AppendASCII("good.crx"); | 696 source_path = source_path.AppendASCII("extensions").AppendASCII("good.crx"); |
| 743 | 697 |
| 744 RegKey key; | 698 RegKey key; |
| 745 std::wstring reg_path = ASCIIToWide(registry_path_); | 699 std::wstring reg_path = ASCIIToWide(registry_path_); |
| 746 reg_path += L"\\b3dd733cd71a98fa83f387455e12f5c5501c519e"; | 700 reg_path += L"\\00123456789ABCDEF0123456789ABCDEF0123456"; |
| 747 ASSERT_TRUE(key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_WRITE)); | 701 ASSERT_TRUE(key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_WRITE)); |
| 748 ASSERT_TRUE(key.WriteValue(L"path", source_path.ToWStringHack().c_str())); | 702 ASSERT_TRUE(key.WriteValue(L"path", source_path.ToWStringHack().c_str())); |
| 749 ASSERT_TRUE(key.WriteValue(L"version", L"1.0.0.0")); | 703 ASSERT_TRUE(key.WriteValue(L"version", L"1.0.0.0")); |
| 750 | 704 |
| 751 // Start up the service, it should find our externally registered extension | 705 // Start up the service, it should find our externally registered extension |
| 752 // and install it. | 706 // and install it. |
| 753 service_->Init(); | 707 service_->Init(); |
| 754 loop_.RunAllPending(); | 708 loop_.RunAllPending(); |
| 755 | 709 |
| 756 ASSERT_EQ(0u, GetErrors().size()); | 710 ASSERT_EQ(0u, GetErrors().size()); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 940 ValidatePrefKeyCount(1); | 894 ValidatePrefKeyCount(1); |
| 941 ValidatePref(good_crx, L"state", Extension::KILLBIT); | 895 ValidatePref(good_crx, L"state", Extension::KILLBIT); |
| 942 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); | 896 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); |
| 943 | 897 |
| 944 // The extension should also be gone from disk. | 898 // The extension should also be gone from disk. |
| 945 FilePath extension_path = install_path.DirName(); | 899 FilePath extension_path = install_path.DirName(); |
| 946 extension_path = extension_path.AppendASCII(good_crx); | 900 extension_path = extension_path.AppendASCII(good_crx); |
| 947 EXPECT_FALSE(file_util::PathExists(extension_path)) << | 901 EXPECT_FALSE(file_util::PathExists(extension_path)) << |
| 948 extension_path.ToWStringHack(); | 902 extension_path.ToWStringHack(); |
| 949 } | 903 } |
| OLD | NEW |