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

Side by Side Diff: net/base/x509_certificate_unittest.cc

Issue 2944008: Refactor X509Certificate caching to cache the OS handle, rather than the X509Certificate (Closed)
Patch Set: Rebase before commit Created 9 years, 5 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 | « net/base/x509_certificate_nss.cc ('k') | net/base/x509_certificate_win.cc » ('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) 2011 The Chromium Authors. All rights reserved. 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 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/path_service.h" 7 #include "base/path_service.h"
8 #include "base/pickle.h" 8 #include "base/pickle.h"
9 #include "base/sha1.h" 9 #include "base/sha1.h"
10 #include "base/string_number_conversions.h" 10 #include "base/string_number_conversions.h"
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); 468 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert);
469 469
470 FilePath root_cert_path = certs_dir.AppendASCII("dod_root_ca_2_cert.der"); 470 FilePath root_cert_path = certs_dir.AppendASCII("dod_root_ca_2_cert.der");
471 TestRootCerts* root_certs = TestRootCerts::GetInstance(); 471 TestRootCerts* root_certs = TestRootCerts::GetInstance();
472 ASSERT_TRUE(root_certs->AddFromFile(root_cert_path)); 472 ASSERT_TRUE(root_certs->AddFromFile(root_cert_path));
473 473
474 X509Certificate::OSCertHandles intermediates; 474 X509Certificate::OSCertHandles intermediates;
475 intermediates.push_back(intermediate_cert->os_cert_handle()); 475 intermediates.push_back(intermediate_cert->os_cert_handle());
476 scoped_refptr<X509Certificate> cert_chain = 476 scoped_refptr<X509Certificate> cert_chain =
477 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), 477 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(),
478 X509Certificate::SOURCE_FROM_NETWORK,
479 intermediates); 478 intermediates);
480 479
481 int flags = 0; 480 int flags = 0;
482 CertVerifyResult verify_result; 481 CertVerifyResult verify_result;
483 int error = cert_chain->Verify("www.us.army.mil", flags, &verify_result); 482 int error = cert_chain->Verify("www.us.army.mil", flags, &verify_result);
484 EXPECT_EQ(OK, error); 483 EXPECT_EQ(OK, error);
485 EXPECT_EQ(0, verify_result.cert_status); 484 EXPECT_EQ(0, verify_result.cert_status);
486 root_certs->Clear(); 485 root_certs->Clear();
487 } 486 }
488 487
(...skipping 14 matching lines...) Expand all
503 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert); 502 ASSERT_NE(static_cast<X509Certificate*>(NULL), server_cert);
504 503
505 scoped_refptr<X509Certificate> intermediate_cert = 504 scoped_refptr<X509Certificate> intermediate_cert =
506 ImportCertFromFile(certs_dir, "globalsign_ev_sha256_ca_cert.pem"); 505 ImportCertFromFile(certs_dir, "globalsign_ev_sha256_ca_cert.pem");
507 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); 506 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert);
508 507
509 X509Certificate::OSCertHandles intermediates; 508 X509Certificate::OSCertHandles intermediates;
510 intermediates.push_back(intermediate_cert->os_cert_handle()); 509 intermediates.push_back(intermediate_cert->os_cert_handle());
511 scoped_refptr<X509Certificate> cert_chain = 510 scoped_refptr<X509Certificate> cert_chain =
512 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(), 511 X509Certificate::CreateFromHandle(server_cert->os_cert_handle(),
513 X509Certificate::SOURCE_FROM_NETWORK,
514 intermediates); 512 intermediates);
515 513
516 CertVerifyResult verify_result; 514 CertVerifyResult verify_result;
517 int flags = X509Certificate::VERIFY_REV_CHECKING_ENABLED | 515 int flags = X509Certificate::VERIFY_REV_CHECKING_ENABLED |
518 X509Certificate::VERIFY_EV_CERT; 516 X509Certificate::VERIFY_EV_CERT;
519 int error = cert_chain->Verify("2029.globalsign.com", flags, &verify_result); 517 int error = cert_chain->Verify("2029.globalsign.com", flags, &verify_result);
520 if (error == OK) 518 if (error == OK)
521 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_IS_EV); 519 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_IS_EV);
522 else 520 else
523 EXPECT_EQ(ERR_CERT_DATE_INVALID, error); 521 EXPECT_EQ(ERR_CERT_DATE_INVALID, error);
524 } 522 }
525 523
526 TEST(X509CertificateTest, TestKnownRoot) { 524 TEST(X509CertificateTest, TestKnownRoot) {
527 FilePath certs_dir = GetTestCertsDirectory(); 525 FilePath certs_dir = GetTestCertsDirectory();
528 scoped_refptr<X509Certificate> cert = 526 scoped_refptr<X509Certificate> cert =
529 ImportCertFromFile(certs_dir, "nist.der"); 527 ImportCertFromFile(certs_dir, "nist.der");
530 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert); 528 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert);
531 529
532 // This intermediate is only needed for old Linux machines. Modern NSS 530 // This intermediate is only needed for old Linux machines. Modern NSS
533 // includes it as a root already. 531 // includes it as a root already.
534 scoped_refptr<X509Certificate> intermediate_cert = 532 scoped_refptr<X509Certificate> intermediate_cert =
535 ImportCertFromFile(certs_dir, "nist_intermediate.der"); 533 ImportCertFromFile(certs_dir, "nist_intermediate.der");
536 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); 534 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert);
537 535
538 X509Certificate::OSCertHandles intermediates; 536 X509Certificate::OSCertHandles intermediates;
539 intermediates.push_back(intermediate_cert->os_cert_handle()); 537 intermediates.push_back(intermediate_cert->os_cert_handle());
540 scoped_refptr<X509Certificate> cert_chain = 538 scoped_refptr<X509Certificate> cert_chain =
541 X509Certificate::CreateFromHandle(cert->os_cert_handle(), 539 X509Certificate::CreateFromHandle(cert->os_cert_handle(),
542 X509Certificate::SOURCE_FROM_NETWORK,
543 intermediates); 540 intermediates);
544 541
545 int flags = 0; 542 int flags = 0;
546 CertVerifyResult verify_result; 543 CertVerifyResult verify_result;
547 // This is going to blow up in Feb 2012. Sorry! Disable and file a bug 544 // This is going to blow up in Feb 2012. Sorry! Disable and file a bug
548 // against agl. Also see PublicKeyHashes in this file. 545 // against agl. Also see PublicKeyHashes in this file.
549 int error = cert_chain->Verify("www.nist.gov", flags, &verify_result); 546 int error = cert_chain->Verify("www.nist.gov", flags, &verify_result);
550 EXPECT_EQ(OK, error); 547 EXPECT_EQ(OK, error);
551 EXPECT_EQ(0, verify_result.cert_status); 548 EXPECT_EQ(0, verify_result.cert_status);
552 EXPECT_TRUE(verify_result.is_issued_by_known_root); 549 EXPECT_TRUE(verify_result.is_issued_by_known_root);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
608 scoped_refptr<X509Certificate> intermediate_cert = 605 scoped_refptr<X509Certificate> intermediate_cert =
609 ImportCertFromFile(certs_dir, "nist_intermediate.der"); 606 ImportCertFromFile(certs_dir, "nist_intermediate.der");
610 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert); 607 ASSERT_NE(static_cast<X509Certificate*>(NULL), intermediate_cert);
611 608
612 TestRootCerts::GetInstance()->Add(intermediate_cert.get()); 609 TestRootCerts::GetInstance()->Add(intermediate_cert.get());
613 610
614 X509Certificate::OSCertHandles intermediates; 611 X509Certificate::OSCertHandles intermediates;
615 intermediates.push_back(intermediate_cert->os_cert_handle()); 612 intermediates.push_back(intermediate_cert->os_cert_handle());
616 scoped_refptr<X509Certificate> cert_chain = 613 scoped_refptr<X509Certificate> cert_chain =
617 X509Certificate::CreateFromHandle(cert->os_cert_handle(), 614 X509Certificate::CreateFromHandle(cert->os_cert_handle(),
618 X509Certificate::SOURCE_FROM_NETWORK,
619 intermediates); 615 intermediates);
620 616
621 int flags = 0; 617 int flags = 0;
622 CertVerifyResult verify_result; 618 CertVerifyResult verify_result;
623 619
624 int error = cert_chain->Verify("www.nist.gov", flags, &verify_result); 620 int error = cert_chain->Verify("www.nist.gov", flags, &verify_result);
625 EXPECT_EQ(OK, error); 621 EXPECT_EQ(OK, error);
626 EXPECT_EQ(0, verify_result.cert_status); 622 EXPECT_EQ(0, verify_result.cert_status);
627 ASSERT_LE(2u, verify_result.public_key_hashes.size()); 623 ASSERT_LE(2u, verify_result.public_key_hashes.size());
628 EXPECT_EQ(HexEncode(nistSPKIHash, base::SHA1_LENGTH), 624 EXPECT_EQ(HexEncode(nistSPKIHash, base::SHA1_LENGTH),
(...skipping 27 matching lines...) Expand all
656 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_INVALID); 652 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_INVALID);
657 #endif 653 #endif
658 // TODO(wtc): fix http://crbug.com/75520 to get all the certificate errors 654 // TODO(wtc): fix http://crbug.com/75520 to get all the certificate errors
659 // from NSS. 655 // from NSS.
660 #if !defined(USE_NSS) 656 #if !defined(USE_NSS)
661 // The certificate is issued by an unknown CA. 657 // The certificate is issued by an unknown CA.
662 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID); 658 EXPECT_NE(0, verify_result.cert_status & CERT_STATUS_AUTHORITY_INVALID);
663 #endif 659 #endif
664 } 660 }
665 661
666 // Tests X509Certificate::Cache via X509Certificate::CreateFromHandle. We 662 // Tests X509CertificateCache via X509Certificate::CreateFromHandle. We
667 // call X509Certificate::CreateFromHandle several times and observe whether 663 // call X509Certificate::CreateFromHandle several times and observe whether
668 // it returns a cached or new X509Certificate object. 664 // it returns a cached or new OSCertHandle.
669 //
670 // All the OS certificate handles in this test are actually from the same
671 // source (the bytes of a lone certificate), but we pretend that some of them
672 // come from the network.
673 TEST(X509CertificateTest, Cache) { 665 TEST(X509CertificateTest, Cache) {
674 X509Certificate::OSCertHandle google_cert_handle; 666 X509Certificate::OSCertHandle google_cert_handle;
667 X509Certificate::OSCertHandle thawte_cert_handle;
675 668
676 // Add a certificate from the source SOURCE_LONE_CERT_IMPORT to our 669 // Add a single certificate to the certificate cache.
677 // certificate cache.
678 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 670 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
679 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 671 reinterpret_cast<const char*>(google_der), sizeof(google_der));
680 scoped_refptr<X509Certificate> cert1(X509Certificate::CreateFromHandle( 672 scoped_refptr<X509Certificate> cert1(X509Certificate::CreateFromHandle(
681 google_cert_handle, X509Certificate::SOURCE_LONE_CERT_IMPORT, 673 google_cert_handle, X509Certificate::OSCertHandles()));
682 X509Certificate::OSCertHandles()));
683 X509Certificate::FreeOSCertHandle(google_cert_handle); 674 X509Certificate::FreeOSCertHandle(google_cert_handle);
684 675
685 // Add a certificate from the same source (SOURCE_LONE_CERT_IMPORT). This 676 // Add the same certificate, but as a new handle.
686 // should return the cached certificate (cert1).
687 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 677 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
688 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 678 reinterpret_cast<const char*>(google_der), sizeof(google_der));
689 scoped_refptr<X509Certificate> cert2(X509Certificate::CreateFromHandle( 679 scoped_refptr<X509Certificate> cert2(X509Certificate::CreateFromHandle(
690 google_cert_handle, X509Certificate::SOURCE_LONE_CERT_IMPORT, 680 google_cert_handle, X509Certificate::OSCertHandles()));
691 X509Certificate::OSCertHandles()));
692 X509Certificate::FreeOSCertHandle(google_cert_handle); 681 X509Certificate::FreeOSCertHandle(google_cert_handle);
693 682
694 EXPECT_EQ(cert1, cert2); 683 // A new X509Certificate should be returned.
684 EXPECT_NE(cert1.get(), cert2.get());
685 // But both instances should share the underlying OS certificate handle.
686 EXPECT_EQ(cert1->os_cert_handle(), cert2->os_cert_handle());
687 EXPECT_EQ(0u, cert1->GetIntermediateCertificates().size());
688 EXPECT_EQ(0u, cert2->GetIntermediateCertificates().size());
695 689
696 // Add a certificate from the network. This should kick out the original 690 // Add the same certificate, but this time with an intermediate. This
697 // cached certificate (cert1) and return a new certificate. 691 // should result in the intermediate being cached. Note that this is not
692 // a legitimate chain, but is suitable for testing.
698 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 693 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
699 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 694 reinterpret_cast<const char*>(google_der), sizeof(google_der));
695 thawte_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
696 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der));
697 X509Certificate::OSCertHandles intermediates;
698 intermediates.push_back(thawte_cert_handle);
700 scoped_refptr<X509Certificate> cert3(X509Certificate::CreateFromHandle( 699 scoped_refptr<X509Certificate> cert3(X509Certificate::CreateFromHandle(
701 google_cert_handle, X509Certificate::SOURCE_FROM_NETWORK, 700 google_cert_handle, intermediates));
702 X509Certificate::OSCertHandles()));
703 X509Certificate::FreeOSCertHandle(google_cert_handle); 701 X509Certificate::FreeOSCertHandle(google_cert_handle);
702 X509Certificate::FreeOSCertHandle(thawte_cert_handle);
704 703
705 EXPECT_NE(cert1, cert3); 704 // Test that the new certificate, even with intermediates, results in the
706 705 // same underlying handle being used.
707 // Add one certificate from each source. Both should return the new cached 706 EXPECT_EQ(cert1->os_cert_handle(), cert3->os_cert_handle());
708 // certificate (cert3). 707 // Though they use the same OS handle, the intermediates should be different.
709 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes( 708 EXPECT_NE(cert1->GetIntermediateCertificates().size(),
710 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 709 cert3->GetIntermediateCertificates().size());
711 scoped_refptr<X509Certificate> cert4(X509Certificate::CreateFromHandle(
712 google_cert_handle, X509Certificate::SOURCE_FROM_NETWORK,
713 X509Certificate::OSCertHandles()));
714 X509Certificate::FreeOSCertHandle(google_cert_handle);
715
716 EXPECT_EQ(cert3, cert4);
717
718 google_cert_handle = X509Certificate::CreateOSCertHandleFromBytes(
719 reinterpret_cast<const char*>(google_der), sizeof(google_der));
720 scoped_refptr<X509Certificate> cert5(X509Certificate::CreateFromHandle(
721 google_cert_handle, X509Certificate::SOURCE_FROM_NETWORK,
722 X509Certificate::OSCertHandles()));
723 X509Certificate::FreeOSCertHandle(google_cert_handle);
724
725 EXPECT_EQ(cert3, cert5);
726 } 710 }
727 711
728 TEST(X509CertificateTest, Pickle) { 712 TEST(X509CertificateTest, Pickle) {
729 X509Certificate::OSCertHandle google_cert_handle = 713 X509Certificate::OSCertHandle google_cert_handle =
730 X509Certificate::CreateOSCertHandleFromBytes( 714 X509Certificate::CreateOSCertHandleFromBytes(
731 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 715 reinterpret_cast<const char*>(google_der), sizeof(google_der));
732 X509Certificate::OSCertHandle thawte_cert_handle = 716 X509Certificate::OSCertHandle thawte_cert_handle =
733 X509Certificate::CreateOSCertHandleFromBytes( 717 X509Certificate::CreateOSCertHandleFromBytes(
734 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)); 718 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der));
735 719
736 X509Certificate::OSCertHandles intermediates; 720 X509Certificate::OSCertHandles intermediates;
737 intermediates.push_back(thawte_cert_handle); 721 intermediates.push_back(thawte_cert_handle);
738 // Faking SOURCE_LONE_CERT_IMPORT so that when the pickled certificate is
739 // read, it successfully evicts |cert| from the X509Certificate::Cache.
740 // This will be fixed when http://crbug.com/49377 is fixed.
741 scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromHandle( 722 scoped_refptr<X509Certificate> cert = X509Certificate::CreateFromHandle(
742 google_cert_handle, 723 google_cert_handle, intermediates);
743 X509Certificate::SOURCE_LONE_CERT_IMPORT,
744 intermediates);
745 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert.get()); 724 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert.get());
746 725
747 X509Certificate::FreeOSCertHandle(google_cert_handle); 726 X509Certificate::FreeOSCertHandle(google_cert_handle);
748 X509Certificate::FreeOSCertHandle(thawte_cert_handle); 727 X509Certificate::FreeOSCertHandle(thawte_cert_handle);
749 728
750 Pickle pickle; 729 Pickle pickle;
751 cert->Persist(&pickle); 730 cert->Persist(&pickle);
752 731
753 void* iter = NULL; 732 void* iter = NULL;
754 scoped_refptr<X509Certificate> cert_from_pickle = 733 scoped_refptr<X509Certificate> cert_from_pickle =
755 X509Certificate::CreateFromPickle( 734 X509Certificate::CreateFromPickle(
756 pickle, &iter, X509Certificate::PICKLETYPE_CERTIFICATE_CHAIN); 735 pickle, &iter, X509Certificate::PICKLETYPE_CERTIFICATE_CHAIN);
757 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert_from_pickle); 736 ASSERT_NE(static_cast<X509Certificate*>(NULL), cert_from_pickle);
758 EXPECT_NE(cert.get(), cert_from_pickle.get());
759 EXPECT_TRUE(X509Certificate::IsSameOSCert( 737 EXPECT_TRUE(X509Certificate::IsSameOSCert(
760 cert->os_cert_handle(), cert_from_pickle->os_cert_handle())); 738 cert->os_cert_handle(), cert_from_pickle->os_cert_handle()));
761 EXPECT_TRUE(cert->HasIntermediateCertificates( 739 EXPECT_TRUE(cert->HasIntermediateCertificates(
762 cert_from_pickle->GetIntermediateCertificates())); 740 cert_from_pickle->GetIntermediateCertificates()));
763 } 741 }
764 742
765 TEST(X509CertificateTest, Policy) { 743 TEST(X509CertificateTest, Policy) {
766 scoped_refptr<X509Certificate> google_cert(X509Certificate::CreateFromBytes( 744 scoped_refptr<X509Certificate> google_cert(X509Certificate::CreateFromBytes(
767 reinterpret_cast<const char*>(google_der), sizeof(google_der))); 745 reinterpret_cast<const char*>(google_der), sizeof(google_der)));
768 746
(...skipping 22 matching lines...) Expand all
791 EXPECT_TRUE(policy.HasDeniedCert()); 769 EXPECT_TRUE(policy.HasDeniedCert());
792 770
793 policy.Allow(webkit_cert.get()); 771 policy.Allow(webkit_cert.get());
794 772
795 EXPECT_EQ(policy.Check(google_cert.get()), CertPolicy::DENIED); 773 EXPECT_EQ(policy.Check(google_cert.get()), CertPolicy::DENIED);
796 EXPECT_EQ(policy.Check(webkit_cert.get()), CertPolicy::ALLOWED); 774 EXPECT_EQ(policy.Check(webkit_cert.get()), CertPolicy::ALLOWED);
797 EXPECT_TRUE(policy.HasAllowedCert()); 775 EXPECT_TRUE(policy.HasAllowedCert());
798 EXPECT_TRUE(policy.HasDeniedCert()); 776 EXPECT_TRUE(policy.HasDeniedCert());
799 } 777 }
800 778
801 #if defined(OS_MACOSX) || defined(OS_WIN)
802 TEST(X509CertificateTest, IntermediateCertificates) { 779 TEST(X509CertificateTest, IntermediateCertificates) {
803 scoped_refptr<X509Certificate> webkit_cert( 780 scoped_refptr<X509Certificate> webkit_cert(
804 X509Certificate::CreateFromBytes( 781 X509Certificate::CreateFromBytes(
805 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der))); 782 reinterpret_cast<const char*>(webkit_der), sizeof(webkit_der)));
806 783
807 scoped_refptr<X509Certificate> thawte_cert( 784 scoped_refptr<X509Certificate> thawte_cert(
808 X509Certificate::CreateFromBytes( 785 X509Certificate::CreateFromBytes(
809 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der))); 786 reinterpret_cast<const char*>(thawte_der), sizeof(thawte_der)));
810 787
811 scoped_refptr<X509Certificate> paypal_cert( 788 scoped_refptr<X509Certificate> paypal_cert(
812 X509Certificate::CreateFromBytes( 789 X509Certificate::CreateFromBytes(
813 reinterpret_cast<const char*>(paypal_null_der), 790 reinterpret_cast<const char*>(paypal_null_der),
814 sizeof(paypal_null_der))); 791 sizeof(paypal_null_der)));
815 792
816 X509Certificate::OSCertHandle google_handle; 793 X509Certificate::OSCertHandle google_handle;
817 // Create object with no intermediates: 794 // Create object with no intermediates:
818 google_handle = X509Certificate::CreateOSCertHandleFromBytes( 795 google_handle = X509Certificate::CreateOSCertHandleFromBytes(
819 reinterpret_cast<const char*>(google_der), sizeof(google_der)); 796 reinterpret_cast<const char*>(google_der), sizeof(google_der));
820 X509Certificate::OSCertHandles intermediates1; 797 X509Certificate::OSCertHandles intermediates1;
821 scoped_refptr<X509Certificate> cert1; 798 scoped_refptr<X509Certificate> cert1;
822 cert1 = X509Certificate::CreateFromHandle( 799 cert1 = X509Certificate::CreateFromHandle(google_handle, intermediates1);
823 google_handle, X509Certificate::SOURCE_FROM_NETWORK, intermediates1);
824 EXPECT_TRUE(cert1->HasIntermediateCertificates(intermediates1)); 800 EXPECT_TRUE(cert1->HasIntermediateCertificates(intermediates1));
825 EXPECT_FALSE(cert1->HasIntermediateCertificate( 801 EXPECT_FALSE(cert1->HasIntermediateCertificate(
826 webkit_cert->os_cert_handle())); 802 webkit_cert->os_cert_handle()));
827 803
828 // Create object with 2 intermediates: 804 // Create object with 2 intermediates:
829 X509Certificate::OSCertHandles intermediates2; 805 X509Certificate::OSCertHandles intermediates2;
830 intermediates2.push_back(webkit_cert->os_cert_handle()); 806 intermediates2.push_back(webkit_cert->os_cert_handle());
831 intermediates2.push_back(thawte_cert->os_cert_handle()); 807 intermediates2.push_back(thawte_cert->os_cert_handle());
832 scoped_refptr<X509Certificate> cert2; 808 scoped_refptr<X509Certificate> cert2;
833 cert2 = X509Certificate::CreateFromHandle( 809 cert2 = X509Certificate::CreateFromHandle(google_handle, intermediates2);
834 google_handle, X509Certificate::SOURCE_FROM_NETWORK, intermediates2);
835
836 // The cache should have stored cert2 'cause it has more intermediates:
837 EXPECT_NE(cert1, cert2);
838 810
839 // Verify it has all the intermediates: 811 // Verify it has all the intermediates:
840 EXPECT_TRUE(cert2->HasIntermediateCertificate( 812 EXPECT_TRUE(cert2->HasIntermediateCertificate(
841 webkit_cert->os_cert_handle())); 813 webkit_cert->os_cert_handle()));
842 EXPECT_TRUE(cert2->HasIntermediateCertificate( 814 EXPECT_TRUE(cert2->HasIntermediateCertificate(
843 thawte_cert->os_cert_handle())); 815 thawte_cert->os_cert_handle()));
844 EXPECT_FALSE(cert2->HasIntermediateCertificate( 816 EXPECT_FALSE(cert2->HasIntermediateCertificate(
845 paypal_cert->os_cert_handle())); 817 paypal_cert->os_cert_handle()));
846 818
847 // Create object with 1 intermediate:
848 X509Certificate::OSCertHandles intermediates3;
849 intermediates2.push_back(thawte_cert->os_cert_handle());
850 scoped_refptr<X509Certificate> cert3;
851 cert3 = X509Certificate::CreateFromHandle(
852 google_handle, X509Certificate::SOURCE_FROM_NETWORK, intermediates3);
853
854 // The cache should have returned cert2 'cause it has more intermediates:
855 EXPECT_EQ(cert3, cert2);
856
857 // Cleanup 819 // Cleanup
858 X509Certificate::FreeOSCertHandle(google_handle); 820 X509Certificate::FreeOSCertHandle(google_handle);
859 } 821 }
860 #endif
861 822
862 #if defined(OS_MACOSX) 823 #if defined(OS_MACOSX)
863 TEST(X509CertificateTest, IsIssuedBy) { 824 TEST(X509CertificateTest, IsIssuedBy) {
864 FilePath certs_dir = GetTestCertsDirectory(); 825 FilePath certs_dir = GetTestCertsDirectory();
865 826
866 // Test a client certificate from MIT. 827 // Test a client certificate from MIT.
867 scoped_refptr<X509Certificate> mit_davidben_cert( 828 scoped_refptr<X509Certificate> mit_davidben_cert(
868 ImportCertFromFile(certs_dir, "mit.davidben.der")); 829 ImportCertFromFile(certs_dir, "mit.davidben.der"));
869 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert); 830 ASSERT_NE(static_cast<X509Certificate*>(NULL), mit_davidben_cert);
870 831
(...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after
1270 } 1231 }
1271 1232
1272 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname( 1233 EXPECT_EQ(test_data.expected, X509Certificate::VerifyHostname(
1273 test_data.hostname, common_name, dns_names, ip_addressses)); 1234 test_data.hostname, common_name, dns_names, ip_addressses));
1274 } 1235 }
1275 1236
1276 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest, 1237 INSTANTIATE_TEST_CASE_P(, X509CertificateNameVerifyTest,
1277 testing::ValuesIn(kNameVerifyTestData)); 1238 testing::ValuesIn(kNameVerifyTestData));
1278 1239
1279 } // namespace net 1240 } // namespace net
OLDNEW
« no previous file with comments | « net/base/x509_certificate_nss.cc ('k') | net/base/x509_certificate_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698