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

Side by Side Diff: chrome/browser/extensions/extensions_service_unittest.cc

Issue 126014: Verify signed .crx extension installations (Closed)
Patch Set: final changes Created 11 years, 6 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
« no previous file with comments | « chrome/browser/extensions/extensions_service.cc ('k') | chrome/chrome.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 15 matching lines...) Expand all
26 #include "chrome/common/notification_type.h" 26 #include "chrome/common/notification_type.h"
27 #include "chrome/test/testing_profile.h" 27 #include "chrome/test/testing_profile.h"
28 #include "testing/gtest/include/gtest/gtest.h" 28 #include "testing/gtest/include/gtest/gtest.h"
29 #include "testing/platform_test.h" 29 #include "testing/platform_test.h"
30 30
31 namespace { 31 namespace {
32 32
33 // Extension ids used during testing. 33 // Extension ids used during testing.
34 const char* const all_zero = "0000000000000000000000000000000000000000"; 34 const char* const all_zero = "0000000000000000000000000000000000000000";
35 const char* const zero_n_one = "0000000000000000000000000000000000000001"; 35 const char* const zero_n_one = "0000000000000000000000000000000000000001";
36 const char* const good0 = "00123456789abcdef0123456789abcdef0123456"; 36 const char* const good0 = "fc6f6ba6693faf6773c13701019f2e7a12f0febe";
37 const char* const good1 = "10123456789abcdef0123456789abcdef0123456"; 37 const char* const good1 = "e5ead92b2c6795c1d2b92df9c5cb37de5582471a";
38 const char* const good2 = "20123456789abcdef0123456789abcdef0123456"; 38 const char* const good2 = "a37fed892f622823f4daaec4426a32fc7f6147dc";
39 const char* const good_crx = "00123456789abcdef0123456789abcdef0123456"; 39 const char* const good_crx = "b3dd733cd71a98fa83f387455e12f5c5501c519e";
40 const char* const page_action = "8a5e4cb023c61b431e9b603a97c293429ce057c8"; 40 const char* const page_action = "a4ca7d01469a010acb200568a0b8f4d9b3ac1f91";
41 const char* const theme_crx = "f0123456789abcdef0123456789abcdef0126456"; 41 const char* const theme_crx = "80c45f5ae9e0f839d105c6a6d2461a036bc40a04";
42 const char* const theme2_crx = "f0123456789adddef0123456789abcdef0126456"; 42 const char* const theme2_crx = "f9f6c52c01efdd5edd7c396b5f995a15fc7ad6d1";
43 43
44 struct ExtensionsOrder { 44 struct ExtensionsOrder {
45 bool operator()(const Extension* a, const Extension* b) { 45 bool operator()(const Extension* a, const Extension* b) {
46 return a->name() < b->name(); 46 return a->name() < b->name();
47 } 47 }
48 }; 48 };
49 49
50 static std::vector<std::string> GetErrors() { 50 static std::vector<std::string> GetErrors() {
51 const std::vector<std::string>* errors = 51 const std::vector<std::string>* errors =
52 ExtensionErrorReporter::GetInstance()->GetErrors(); 52 ExtensionErrorReporter::GetInstance()->GetErrors();
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 ASSERT_TRUE(service_->Init()); 296 ASSERT_TRUE(service_->Init());
297 loop_.RunAllPending(); 297 loop_.RunAllPending();
298 298
299 std::vector<std::string> errors = GetErrors(); 299 std::vector<std::string> errors = GetErrors();
300 for (std::vector<std::string>::iterator err = errors.begin(); 300 for (std::vector<std::string>::iterator err = errors.begin();
301 err != errors.end(); ++err) { 301 err != errors.end(); ++err) {
302 LOG(ERROR) << *err; 302 LOG(ERROR) << *err;
303 } 303 }
304 ASSERT_EQ(3u, loaded_.size()); 304 ASSERT_EQ(3u, loaded_.size());
305 305
306 EXPECT_EQ(std::string(good_crx), loaded_[0]->id()); 306 EXPECT_EQ(std::string(good0), loaded_[0]->id());
307 EXPECT_EQ(std::string("My extension 1"), 307 EXPECT_EQ(std::string("My extension 1"),
308 loaded_[0]->name()); 308 loaded_[0]->name());
309 EXPECT_EQ(std::string("The first extension that I made."), 309 EXPECT_EQ(std::string("The first extension that I made."),
310 loaded_[0]->description()); 310 loaded_[0]->description());
311 EXPECT_EQ(Extension::INTERNAL, loaded_[0]->location()); 311 EXPECT_EQ(Extension::INTERNAL, loaded_[0]->location());
312 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id())); 312 EXPECT_TRUE(service_->GetExtensionByID(loaded_[0]->id()));
313 EXPECT_EQ(3u, service_->extensions()->size()); 313 EXPECT_EQ(3u, service_->extensions()->size());
314 314
315 ValidatePrefKeyCount(3); 315 ValidatePrefKeyCount(3);
316 ValidatePref(good_crx, L"state", Extension::ENABLED); 316 ValidatePref(good0, L"state", Extension::ENABLED);
317 ValidatePref(good_crx, L"location", Extension::INTERNAL); 317 ValidatePref(good0, L"location", Extension::INTERNAL);
318 ValidatePref(good1, L"state", Extension::ENABLED); 318 ValidatePref(good1, L"state", Extension::ENABLED);
319 ValidatePref(good1, L"location", Extension::INTERNAL); 319 ValidatePref(good1, L"location", Extension::INTERNAL);
320 ValidatePref(good2, L"state", Extension::ENABLED); 320 ValidatePref(good2, L"state", Extension::ENABLED);
321 ValidatePref(good2, L"location", Extension::INTERNAL); 321 ValidatePref(good2, L"location", Extension::INTERNAL);
322 322
323 Extension* extension = loaded_[0]; 323 Extension* extension = loaded_[0];
324 const UserScriptList& scripts = extension->content_scripts(); 324 const UserScriptList& scripts = extension->content_scripts();
325 const std::vector<std::string>& toolstrips = extension->toolstrips(); 325 const std::vector<std::string>& toolstrips = extension->toolstrips();
326 ASSERT_EQ(2u, scripts.size()); 326 ASSERT_EQ(2u, scripts.size());
327 EXPECT_EQ(2u, scripts[0].url_patterns().size()); 327 EXPECT_EQ(2u, scripts[0].url_patterns().size());
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 ValidatePref(good_crx, L"state", Extension::ENABLED); 459 ValidatePref(good_crx, L"state", Extension::ENABLED);
460 ValidatePref(good_crx, L"location", Extension::INTERNAL); 460 ValidatePref(good_crx, L"location", Extension::INTERNAL);
461 461
462 // An extension with page actions. 462 // An extension with page actions.
463 path = extensions_path.AppendASCII("page_action.crx"); 463 path = extensions_path.AppendASCII("page_action.crx");
464 InstallExtension(path, true); 464 InstallExtension(path, true);
465 ValidatePrefKeyCount(++pref_count); 465 ValidatePrefKeyCount(++pref_count);
466 ValidatePref(page_action, L"state", Extension::ENABLED); 466 ValidatePref(page_action, L"state", Extension::ENABLED);
467 ValidatePref(page_action, L"location", Extension::INTERNAL); 467 ValidatePref(page_action, L"location", Extension::INTERNAL);
468 468
469 // Bad signature.
470 path = extensions_path.AppendASCII("bad_signature.crx");
471 InstallExtension(path, false);
472
469 // 0-length extension file. 473 // 0-length extension file.
470 path = extensions_path.AppendASCII("not_an_extension.crx"); 474 path = extensions_path.AppendASCII("not_an_extension.crx");
471 InstallExtension(path, false); 475 InstallExtension(path, false);
472 ValidatePrefKeyCount(pref_count); 476 ValidatePrefKeyCount(pref_count);
473 477
474 // Bad magic number. 478 // Bad magic number.
475 path = extensions_path.AppendASCII("bad_magic.crx"); 479 path = extensions_path.AppendASCII("bad_magic.crx");
476 InstallExtension(path, false); 480 InstallExtension(path, false);
477 ValidatePrefKeyCount(pref_count); 481 ValidatePrefKeyCount(pref_count);
478 482
479 // Poorly formed JSON.
480 path = extensions_path.AppendASCII("bad_json.crx");
481 InstallExtension(path, false);
482 ValidatePrefKeyCount(pref_count);
483
484 // Incorrect zip hash.
485 path = extensions_path.AppendASCII("bad_hash.crx");
486 InstallExtension(path, false);
487 ValidatePrefKeyCount(pref_count);
488
489 // TODO(erikkay): add more tests for many of the failure cases. 483 // TODO(erikkay): add more tests for many of the failure cases.
490 // TODO(erikkay): add tests for upgrade cases. 484 // TODO(erikkay): add tests for upgrade cases.
491 } 485 }
492 486
493 #if defined(OS_WIN) // TODO(port) 487 #if defined(OS_WIN) // TODO(port)
494 // Test Packaging and installing an extension. 488 // Test Packaging and installing an extension.
495 // TODO(aa): add a test that uses an openssl-generate private key. 489 // TODO(aa): add a test that uses an openssl-generate private key.
496 // TODO(rafaelw): add more tests for failure cases. 490 // TODO(rafaelw): add more tests for failure cases.
497 TEST_F(ExtensionsServiceTest, PackExtension) { 491 TEST_F(ExtensionsServiceTest, PackExtension) {
498 SetExtensionsEnabled(true); 492 SetExtensionsEnabled(true);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 loop_.RunAllPending(); 614 loop_.RunAllPending();
621 615
622 ASSERT_FALSE(installed_); 616 ASSERT_FALSE(installed_);
623 ASSERT_EQ(0u, loaded_.size()); 617 ASSERT_EQ(0u, loaded_.size());
624 ASSERT_EQ(0u, GetErrors().size()); 618 ASSERT_EQ(0u, GetErrors().size());
625 ValidatePrefKeyCount(1); 619 ValidatePrefKeyCount(1);
626 ValidatePref(good_crx, L"state", Extension::ENABLED); 620 ValidatePref(good_crx, L"state", Extension::ENABLED);
627 ValidatePref(good_crx, L"location", Extension::INTERNAL); 621 ValidatePref(good_crx, L"location", Extension::INTERNAL);
628 } 622 }
629 623
624 // Test upgrading a signed extension.
625 TEST_F(ExtensionsServiceTest, UpgradeSignedGood) {
626 FilePath extensions_path;
627 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
628 extensions_path = extensions_path.AppendASCII("extensions");
629
630 FilePath path = extensions_path.AppendASCII("good.crx");
631 service_->InstallExtension(path);
632 loop_.RunAllPending();
633
634 ASSERT_TRUE(installed_);
635 ASSERT_EQ(1u, loaded_.size());
636 ASSERT_EQ(0u, GetErrors().size());
637
638 // Upgrade to version 2.0
639 path = extensions_path.AppendASCII("good2.crx");
640 service_->InstallExtension(path);
641 loop_.RunAllPending();
642
643 ASSERT_TRUE(installed_);
644 ASSERT_EQ(2u, loaded_.size());
645 ASSERT_EQ(0u, GetErrors().size());
646 }
647
648 // Test upgrading a signed extension with a bad signature.
649 TEST_F(ExtensionsServiceTest, UpgradeSignedBad) {
650 FilePath extensions_path;
651 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
652 extensions_path = extensions_path.AppendASCII("extensions");
653
654 FilePath path = extensions_path.AppendASCII("good.crx");
655 service_->InstallExtension(path);
656 loop_.RunAllPending();
657
658 ASSERT_TRUE(installed_);
659 ASSERT_EQ(1u, loaded_.size());
660 ASSERT_EQ(0u, GetErrors().size());
661 installed_ = NULL;
662
663 // Try upgrading with a bad signature. This should fail during the unpack,
664 // because the key will not match the signature.
665 path = extensions_path.AppendASCII("good2_bad_signature.crx");
666 service_->InstallExtension(path);
667 loop_.RunAllPending();
668
669 ASSERT_FALSE(installed_);
670 ASSERT_EQ(1u, loaded_.size());
671 ASSERT_EQ(1u, GetErrors().size());
672 }
673
630 // Tests uninstalling normal extensions 674 // Tests uninstalling normal extensions
631 TEST_F(ExtensionsServiceTest, UninstallExtension) { 675 TEST_F(ExtensionsServiceTest, UninstallExtension) {
632 FilePath extensions_path; 676 FilePath extensions_path;
633 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 677 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
634 extensions_path = extensions_path.AppendASCII("extensions"); 678 extensions_path = extensions_path.AppendASCII("extensions");
635 679
636 // A simple extension that should install without error. 680 // A simple extension that should install without error.
637 FilePath path = extensions_path.AppendASCII("good.crx"); 681 FilePath path = extensions_path.AppendASCII("good.crx");
638 InstallExtension(path, true); 682 InstallExtension(path, true);
639 683
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
717 EXPECT_EQ(id, unloaded_id_); 761 EXPECT_EQ(id, unloaded_id_);
718 762
719 ValidatePrefKeyCount(1); 763 ValidatePrefKeyCount(1);
720 ValidatePref(good0, L"state", Extension::DISABLED); 764 ValidatePref(good0, L"state", Extension::DISABLED);
721 ValidatePref(good0, L"location", Extension::INTERNAL); 765 ValidatePref(good0, L"location", Extension::INTERNAL);
722 } 766 }
723 767
724 // Tests that we generate IDs when they are not specified in the manifest for 768 // Tests that we generate IDs when they are not specified in the manifest for
725 // --load-extension. 769 // --load-extension.
726 TEST_F(ExtensionsServiceTest, GenerateID) { 770 TEST_F(ExtensionsServiceTest, GenerateID) {
771 Extension::ResetGeneratedIdCounter();
772
727 FilePath extensions_path; 773 FilePath extensions_path;
728 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 774 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
729 extensions_path = extensions_path.AppendASCII("extensions"); 775 extensions_path = extensions_path.AppendASCII("extensions");
730 776
731 FilePath no_id_ext = extensions_path.AppendASCII("no_id"); 777 FilePath no_id_ext = extensions_path.AppendASCII("no_id");
732 service_->LoadExtension(no_id_ext); 778 service_->LoadExtension(no_id_ext);
733 loop_.RunAllPending(); 779 loop_.RunAllPending();
734 EXPECT_EQ(0u, GetErrors().size()); 780 EXPECT_EQ(0u, GetErrors().size());
735 ASSERT_EQ(1u, loaded_.size()); 781 ASSERT_EQ(1u, loaded_.size());
736 std::string id1 = loaded_[0]->id(); 782 std::string id1 = loaded_[0]->id();
(...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after
961 ValidatePrefKeyCount(1); 1007 ValidatePrefKeyCount(1);
962 ValidatePref(good_crx, L"state", Extension::KILLBIT); 1008 ValidatePref(good_crx, L"state", Extension::KILLBIT);
963 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); 1009 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF);
964 1010
965 // The extension should also be gone from disk. 1011 // The extension should also be gone from disk.
966 FilePath extension_path = install_path.DirName(); 1012 FilePath extension_path = install_path.DirName();
967 extension_path = extension_path.AppendASCII(good_crx); 1013 extension_path = extension_path.AppendASCII(good_crx);
968 EXPECT_FALSE(file_util::PathExists(extension_path)) << 1014 EXPECT_FALSE(file_util::PathExists(extension_path)) <<
969 extension_path.ToWStringHack(); 1015 extension_path.ToWStringHack();
970 } 1016 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extensions_service.cc ('k') | chrome/chrome.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698