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

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

Issue 115682: Verify signed .crx extension installations (Closed)
Patch Set: moved extensions consts back to extensions_service (was causing compile errors on mac & linux) 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/common/extensions/extension.h » ('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 18 matching lines...) Expand all
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 = "00123456789abcdef0123456789abcdef0123456"; 39 const char* const good0 = "fc6f6ba6693faf6773c13701019f2e7a12f0febe";
40 const char* const good1 = "10123456789abcdef0123456789abcdef0123456"; 40 const char* const good1 = "e5ead92b2c6795c1d2b92df9c5cb37de5582471a";
41 const char* const good2 = "20123456789abcdef0123456789abcdef0123456"; 41 const char* const good2 = "a37fed892f622823f4daaec4426a32fc7f6147dc";
42 const char* const good_crx = "00123456789abcdef0123456789abcdef0123456"; 42 const char* const good_crx = "b3dd733cd71a98fa83f387455e12f5c5501c519e";
43 const char* const page_action = "8a5e4cb023c61b431e9b603a97c293429ce057c8"; 43 const char* const page_action = "a4ca7d01469a010acb200568a0b8f4d9b3ac1f91";
44 const char* const theme_crx = "f0123456789abcdef0123456789abcdef0126456"; 44 const char* const theme_crx = "80c45f5ae9e0f839d105c6a6d2461a036bc40a04";
45 const char* const theme2_crx = "f0123456789adddef0123456789abcdef0126456"; 45 const char* const theme2_crx = "f9f6c52c01efdd5edd7c396b5f995a15fc7ad6d1";
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
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(good_crx), loaded_[0]->id()); 238 EXPECT_EQ(std::string(good0), 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(good_crx, L"state", Extension::ENABLED); 248 ValidatePref(good0, L"state", Extension::ENABLED);
249 ValidatePref(good_crx, L"location", Extension::INTERNAL); 249 ValidatePref(good0, 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
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
401 // 0-length extension file. 405 // 0-length extension file.
402 path = extensions_path.AppendASCII("not_an_extension.crx"); 406 path = extensions_path.AppendASCII("not_an_extension.crx");
403 InstallExtension(path, false); 407 InstallExtension(path, false);
404 ValidatePrefKeyCount(pref_count); 408 ValidatePrefKeyCount(pref_count);
405 409
406 // Bad magic number. 410 // Bad magic number.
407 path = extensions_path.AppendASCII("bad_magic.crx"); 411 path = extensions_path.AppendASCII("bad_magic.crx");
408 InstallExtension(path, false); 412 InstallExtension(path, false);
409 ValidatePrefKeyCount(pref_count); 413 ValidatePrefKeyCount(pref_count);
410 414
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
421 // TODO(erikkay): add more tests for many of the failure cases. 415 // TODO(erikkay): add more tests for many of the failure cases.
422 // TODO(erikkay): add tests for upgrade cases. 416 // TODO(erikkay): add tests for upgrade cases.
423 } 417 }
424 418
425 #if defined(OS_WIN) // TODO(port) 419 #if defined(OS_WIN) // TODO(port)
426 // Test Packaging and installing an extension. 420 // Test Packaging and installing an extension.
427 // TODO(aa): add a test that uses an openssl-generate private key. 421 // TODO(aa): add a test that uses an openssl-generate private key.
428 // TODO(rafaelw): add more tests for failure cases. 422 // TODO(rafaelw): add more tests for failure cases.
429 TEST_F(ExtensionsServiceTest, PackExtension) { 423 TEST_F(ExtensionsServiceTest, PackExtension) {
430 SetExtensionsEnabled(true); 424 SetExtensionsEnabled(true);
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 loop_.RunAllPending(); 546 loop_.RunAllPending();
553 547
554 ASSERT_FALSE(installed_); 548 ASSERT_FALSE(installed_);
555 ASSERT_EQ(0u, loaded_.size()); 549 ASSERT_EQ(0u, loaded_.size());
556 ASSERT_EQ(0u, GetErrors().size()); 550 ASSERT_EQ(0u, GetErrors().size());
557 ValidatePrefKeyCount(1); 551 ValidatePrefKeyCount(1);
558 ValidatePref(good_crx, L"state", Extension::ENABLED); 552 ValidatePref(good_crx, L"state", Extension::ENABLED);
559 ValidatePref(good_crx, L"location", Extension::INTERNAL); 553 ValidatePref(good_crx, L"location", Extension::INTERNAL);
560 } 554 }
561 555
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
562 // Tests uninstalling normal extensions 606 // Tests uninstalling normal extensions
563 TEST_F(ExtensionsServiceTest, UninstallExtension) { 607 TEST_F(ExtensionsServiceTest, UninstallExtension) {
564 FilePath extensions_path; 608 FilePath extensions_path;
565 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 609 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
566 extensions_path = extensions_path.AppendASCII("extensions"); 610 extensions_path = extensions_path.AppendASCII("extensions");
567 611
568 // A simple extension that should install without error. 612 // A simple extension that should install without error.
569 FilePath path = extensions_path.AppendASCII("good.crx"); 613 FilePath path = extensions_path.AppendASCII("good.crx");
570 InstallExtension(path, true); 614 InstallExtension(path, true);
571 615
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
649 EXPECT_EQ(id, unloaded_id_); 693 EXPECT_EQ(id, unloaded_id_);
650 694
651 ValidatePrefKeyCount(1); 695 ValidatePrefKeyCount(1);
652 ValidatePref(good0, L"state", Extension::DISABLED); 696 ValidatePref(good0, L"state", Extension::DISABLED);
653 ValidatePref(good0, L"location", Extension::INTERNAL); 697 ValidatePref(good0, L"location", Extension::INTERNAL);
654 } 698 }
655 699
656 // Tests that we generate IDs when they are not specified in the manifest for 700 // Tests that we generate IDs when they are not specified in the manifest for
657 // --load-extension. 701 // --load-extension.
658 TEST_F(ExtensionsServiceTest, GenerateID) { 702 TEST_F(ExtensionsServiceTest, GenerateID) {
703 Extension::ResetGeneratedIdCounter();
704
659 FilePath extensions_path; 705 FilePath extensions_path;
660 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path)); 706 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &extensions_path));
661 extensions_path = extensions_path.AppendASCII("extensions"); 707 extensions_path = extensions_path.AppendASCII("extensions");
662 708
663 FilePath no_id_ext = extensions_path.AppendASCII("no_id"); 709 FilePath no_id_ext = extensions_path.AppendASCII("no_id");
664 service_->LoadExtension(no_id_ext); 710 service_->LoadExtension(no_id_ext);
665 loop_.RunAllPending(); 711 loop_.RunAllPending();
666 EXPECT_EQ(0u, GetErrors().size()); 712 EXPECT_EQ(0u, GetErrors().size());
667 ASSERT_EQ(1u, loaded_.size()); 713 ASSERT_EQ(1u, loaded_.size());
668 std::string id1 = loaded_[0]->id(); 714 std::string id1 = loaded_[0]->id();
(...skipping 21 matching lines...) Expand all
690 #if defined(OS_WIN) 736 #if defined(OS_WIN)
691 737
692 TEST_F(ExtensionsServiceTest, ExternalInstallRegistry) { 738 TEST_F(ExtensionsServiceTest, ExternalInstallRegistry) {
693 // Register a test extension externally using the registry. 739 // Register a test extension externally using the registry.
694 FilePath source_path; 740 FilePath source_path;
695 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_path)); 741 ASSERT_TRUE(PathService::Get(chrome::DIR_TEST_DATA, &source_path));
696 source_path = source_path.AppendASCII("extensions").AppendASCII("good.crx"); 742 source_path = source_path.AppendASCII("extensions").AppendASCII("good.crx");
697 743
698 RegKey key; 744 RegKey key;
699 std::wstring reg_path = ASCIIToWide(registry_path_); 745 std::wstring reg_path = ASCIIToWide(registry_path_);
700 reg_path += L"\\00123456789ABCDEF0123456789ABCDEF0123456"; 746 reg_path += L"\\b3dd733cd71a98fa83f387455e12f5c5501c519e";
701 ASSERT_TRUE(key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_WRITE)); 747 ASSERT_TRUE(key.Create(HKEY_LOCAL_MACHINE, reg_path.c_str(), KEY_WRITE));
702 ASSERT_TRUE(key.WriteValue(L"path", source_path.ToWStringHack().c_str())); 748 ASSERT_TRUE(key.WriteValue(L"path", source_path.ToWStringHack().c_str()));
703 ASSERT_TRUE(key.WriteValue(L"version", L"1.0.0.0")); 749 ASSERT_TRUE(key.WriteValue(L"version", L"1.0.0.0"));
704 750
705 // Start up the service, it should find our externally registered extension 751 // Start up the service, it should find our externally registered extension
706 // and install it. 752 // and install it.
707 service_->Init(); 753 service_->Init();
708 loop_.RunAllPending(); 754 loop_.RunAllPending();
709 755
710 ASSERT_EQ(0u, GetErrors().size()); 756 ASSERT_EQ(0u, GetErrors().size());
(...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after
894 ValidatePrefKeyCount(1); 940 ValidatePrefKeyCount(1);
895 ValidatePref(good_crx, L"state", Extension::KILLBIT); 941 ValidatePref(good_crx, L"state", Extension::KILLBIT);
896 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF); 942 ValidatePref(good_crx, L"location", Extension::EXTERNAL_PREF);
897 943
898 // The extension should also be gone from disk. 944 // The extension should also be gone from disk.
899 FilePath extension_path = install_path.DirName(); 945 FilePath extension_path = install_path.DirName();
900 extension_path = extension_path.AppendASCII(good_crx); 946 extension_path = extension_path.AppendASCII(good_crx);
901 EXPECT_FALSE(file_util::PathExists(extension_path)) << 947 EXPECT_FALSE(file_util::PathExists(extension_path)) <<
902 extension_path.ToWStringHack(); 948 extension_path.ToWStringHack();
903 } 949 }
OLDNEW
« no previous file with comments | « chrome/browser/extensions/extensions_service.cc ('k') | chrome/common/extensions/extension.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698