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

Side by Side Diff: net/cert/internal/path_builder_unittest.cc

Issue 2597003002: Re-enable the omitted tests in path_builder_unittest.cc. (Closed)
Patch Set: Created 4 years 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/cert/internal/path_builder.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "net/cert/internal/path_builder.h" 5 #include "net/cert/internal/path_builder.h"
6 6
7 #include "base/base_paths.h" 7 #include "base/base_paths.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/path_service.h" 9 #include "base/path_service.h"
10 #include "net/cert/internal/cert_issuer_source_static.h" 10 #include "net/cert/internal/cert_issuer_source_static.h"
(...skipping 863 matching lines...) Expand 10 before | Expand all | Expand 10 after
874 EXPECT_FALSE(result.paths[0]->valid); 874 EXPECT_FALSE(result.paths[0]->valid);
875 const auto& path = result.paths[0]->path; 875 const auto& path = result.paths[0]->path;
876 ASSERT_EQ(2U, path.certs.size()); 876 ASSERT_EQ(2U, path.certs.size());
877 EXPECT_EQ(target_, path.certs[0]); 877 EXPECT_EQ(target_, path.certs[0]);
878 EXPECT_EQ(oldintermediate_, path.certs[1]); 878 EXPECT_EQ(oldintermediate_, path.certs[1]);
879 // Compare the DER instead of ParsedCertificate pointer, don't care which copy 879 // Compare the DER instead of ParsedCertificate pointer, don't care which copy
880 // of newroot was used in the path. 880 // of newroot was used in the path.
881 EXPECT_EQ(newroot_->der_cert(), path.trust_anchor->cert()->der_cert()); 881 EXPECT_EQ(newroot_->der_cert(), path.trust_anchor->cert()->der_cert());
882 } 882 }
883 883
884 // TODO(eroman): Re-enable these tests
885 #if 0
886 class MockCertIssuerSourceRequest : public CertIssuerSource::Request { 884 class MockCertIssuerSourceRequest : public CertIssuerSource::Request {
887 public: 885 public:
888 MOCK_METHOD1(GetNext, void(ParsedCertificateList*)); 886 MOCK_METHOD1(GetNext, void(ParsedCertificateList*));
889 }; 887 };
890 888
891 class MockCertIssuerSource : public CertIssuerSource { 889 class MockCertIssuerSource : public CertIssuerSource {
892 public: 890 public:
893 MOCK_METHOD2(SyncGetIssuersOf, 891 MOCK_METHOD2(SyncGetIssuersOf,
894 void(const ParsedCertificate*, ParsedCertificateList*)); 892 void(const ParsedCertificate*, ParsedCertificateList*));
895 MOCK_METHOD3(AsyncGetIssuersOf, 893 MOCK_METHOD2(AsyncGetIssuersOf,
896 void(const ParsedCertificate*, 894 void(const ParsedCertificate*, std::unique_ptr<Request>*));
897 std::unique_ptr<Request>*));
898 }; 895 };
899 896
900 // Helper class to pass the Request to the PathBuilder when it calls 897 // Helper class to pass the Request to the PathBuilder when it calls
901 // AsyncGetIssuersOf. (GoogleMock has a ByMove helper, but it apparently can 898 // AsyncGetIssuersOf. (GoogleMock has a ByMove helper, but it apparently can
902 // only be used with Return, not SetArgPointee.) 899 // only be used with Return, not SetArgPointee.)
903 class CertIssuerSourceRequestMover { 900 class CertIssuerSourceRequestMover {
904 public: 901 public:
905 CertIssuerSourceRequestMover(std::unique_ptr<CertIssuerSource::Request> req) 902 CertIssuerSourceRequestMover(std::unique_ptr<CertIssuerSource::Request> req)
906 : request_(std::move(req)) {} 903 : request_(std::move(req)) {}
907 void MoveIt(const ParsedCertificate* cert, 904 void MoveIt(const ParsedCertificate* cert,
908 const CertIssuerSource::IssuerCallback& issuers_callback,
909 std::unique_ptr<CertIssuerSource::Request>* out_req) { 905 std::unique_ptr<CertIssuerSource::Request>* out_req) {
910 *out_req = std::move(request_); 906 *out_req = std::move(request_);
911 } 907 }
912 908
913 private: 909 private:
914 std::unique_ptr<CertIssuerSource::Request> request_; 910 std::unique_ptr<CertIssuerSource::Request> request_;
915 }; 911 };
916 912
913 // Functor that when called with a ParsedCertificateList* will append the
914 // specified certificate.
915 class AppendCertToList {
916 public:
917 explicit AppendCertToList(const scoped_refptr<ParsedCertificate>& cert)
918 : cert_(cert) {}
919
920 void operator()(ParsedCertificateList* out) { out->push_back(cert_); }
921
922 private:
923 scoped_refptr<ParsedCertificate> cert_;
924 };
925
917 // Test that a single CertIssuerSource returning multiple async batches of 926 // Test that a single CertIssuerSource returning multiple async batches of
918 // issuers is handled correctly. Due to the StrictMocks, it also tests that path 927 // issuers is handled correctly. Due to the StrictMocks, it also tests that path
919 // builder does not request issuers of certs that it shouldn't. 928 // builder does not request issuers of certs that it shouldn't.
920 TEST_F(PathBuilderKeyRolloverTest, TestMultipleAsyncCallbacksFromSingleSource) { 929 TEST_F(PathBuilderKeyRolloverTest, TestMultipleAsyncIssuersFromSingleSource) {
921 StrictMock<MockCertIssuerSource> cert_issuer_source; 930 StrictMock<MockCertIssuerSource> cert_issuer_source;
922 931
923 // Only newroot is a trusted root. 932 // Only newroot is a trusted root.
924 TrustStoreInMemory trust_store; 933 TrustStoreInMemory trust_store;
925 AddTrustedCertificate(newroot_, &trust_store); 934 AddTrustedCertificate(newroot_, &trust_store);
926 935
927 CertPathBuilder::Result result; 936 CertPathBuilder::Result result;
928 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 937 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
929 &result); 938 &result);
930 path_builder.AddCertIssuerSource(&cert_issuer_source); 939 path_builder.AddCertIssuerSource(&cert_issuer_source);
931 940
932 CertIssuerSource::IssuerCallback target_issuers_callback;
933 // Create the mock CertIssuerSource::Request... 941 // Create the mock CertIssuerSource::Request...
934 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>> 942 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>>
935 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>()); 943 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>());
936 // Keep a raw pointer to the Request... 944 // Keep a raw pointer to the Request...
937 StrictMock<MockCertIssuerSourceRequest>* target_issuers_req = 945 StrictMock<MockCertIssuerSourceRequest>* target_issuers_req =
938 target_issuers_req_owner.get(); 946 target_issuers_req_owner.get();
939 // Setup helper class to pass ownership of the Request to the PathBuilder when 947 // Setup helper class to pass ownership of the Request to the PathBuilder when
940 // it calls AsyncGetIssuersOf. 948 // it calls AsyncGetIssuersOf.
941 CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner)); 949 CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner));
942 { 950 {
943 ::testing::InSequence s; 951 ::testing::InSequence s;
944 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _)); 952 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _));
945 EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _, _)) 953 EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _))
946 .WillOnce( 954 .WillOnce(Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt));
947 DoAll(SaveArg<1>(&target_issuers_callback),
948 Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt)));
949 } 955 }
950 956
951 TestClosure callback;
952 CompletionStatus rv = path_builder.Run(callback.closure());
953 ASSERT_EQ(CompletionStatus::ASYNC, rv);
954
955 ASSERT_FALSE(target_issuers_callback.is_null());
956
957 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
958
959 // First async batch: return oldintermediate_.
960 EXPECT_CALL(*target_issuers_req, GetNext(_)) 957 EXPECT_CALL(*target_issuers_req, GetNext(_))
961 .WillOnce(DoAll(SetArgPointee<0>(oldintermediate_), 958 // First async batch: return oldintermediate_.
962 Return(CompletionStatus::SYNC))) 959 .WillOnce(Invoke(AppendCertToList(oldintermediate_)))
963 .WillOnce( 960 // Second async batch: return newintermediate_.
964 DoAll(SetArgPointee<0>(nullptr), Return(CompletionStatus::ASYNC))); 961 .WillOnce(Invoke(AppendCertToList(newintermediate_)));
965 { 962 {
966 ::testing::InSequence s; 963 ::testing::InSequence s;
967 // oldintermediate_ does not create a valid path, so both sync and async 964 // oldintermediate_ does not create a valid path, so both sync and async
968 // lookups are expected. 965 // lookups are expected.
969 EXPECT_CALL(cert_issuer_source, 966 EXPECT_CALL(cert_issuer_source,
970 SyncGetIssuersOf(oldintermediate_.get(), _)); 967 SyncGetIssuersOf(oldintermediate_.get(), _));
971 EXPECT_CALL(cert_issuer_source, 968 EXPECT_CALL(cert_issuer_source,
972 AsyncGetIssuersOf(oldintermediate_.get(), _, _)); 969 AsyncGetIssuersOf(oldintermediate_.get(), _));
973 } 970 }
974 target_issuers_callback.Run(target_issuers_req);
975 ::testing::Mock::VerifyAndClearExpectations(target_issuers_req);
976 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
977 971
978 // Second async batch: return newintermediate_.
979 EXPECT_CALL(*target_issuers_req, GetNext(_))
980 .WillOnce(DoAll(SetArgPointee<0>(newintermediate_),
981 Return(CompletionStatus::SYNC)))
982 .WillOnce(
983 DoAll(SetArgPointee<0>(nullptr), Return(CompletionStatus::ASYNC)));
984 // newroot_ is in the trust store, so this path will be completed 972 // newroot_ is in the trust store, so this path will be completed
985 // synchronously. AsyncGetIssuersOf will not be called on newintermediate_. 973 // synchronously. AsyncGetIssuersOf will not be called on newintermediate_.
986 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _)); 974 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _));
987 target_issuers_callback.Run(target_issuers_req); 975
976 // Ensure pathbuilder finished and filled result.
977 path_builder.Run();
978
988 // Note that VerifyAndClearExpectations(target_issuers_req) is not called 979 // Note that VerifyAndClearExpectations(target_issuers_req) is not called
989 // here. PathBuilder could have destroyed it already, so just let the 980 // here. PathBuilder could have destroyed it already, so just let the
990 // expectations get checked by the destructor. 981 // expectations get checked by the destructor.
991 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source); 982 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
992 983
993 // Ensure pathbuilder finished and filled result.
994 callback.WaitForResult();
995
996 EXPECT_TRUE(result.HasValidPath()); 984 EXPECT_TRUE(result.HasValidPath());
997 ASSERT_EQ(2U, result.paths.size()); 985 ASSERT_EQ(2U, result.paths.size());
998 986
999 // Path builder first attempts: target <- oldintermediate <- newroot 987 // Path builder first attempts: target <- oldintermediate <- newroot
1000 // but it will fail since oldintermediate is signed by oldroot. 988 // but it will fail since oldintermediate is signed by oldroot.
1001 EXPECT_FALSE(result.paths[0]->valid); 989 EXPECT_FALSE(result.paths[0]->valid);
1002 const auto& path0 = result.paths[0]->path; 990 const auto& path0 = result.paths[0]->path;
1003 ASSERT_EQ(2U, path0.certs.size()); 991 ASSERT_EQ(2U, path0.certs.size());
1004 EXPECT_EQ(target_, path0.certs[0]); 992 EXPECT_EQ(target_, path0.certs[0]);
1005 EXPECT_EQ(oldintermediate_, path0.certs[1]); 993 EXPECT_EQ(oldintermediate_, path0.certs[1]);
(...skipping 16 matching lines...) Expand all
1022 1010
1023 // Only newroot is a trusted root. 1011 // Only newroot is a trusted root.
1024 TrustStoreInMemory trust_store; 1012 TrustStoreInMemory trust_store;
1025 AddTrustedCertificate(newroot_, &trust_store); 1013 AddTrustedCertificate(newroot_, &trust_store);
1026 1014
1027 CertPathBuilder::Result result; 1015 CertPathBuilder::Result result;
1028 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_, 1016 CertPathBuilder path_builder(target_, &trust_store, &signature_policy_, time_,
1029 &result); 1017 &result);
1030 path_builder.AddCertIssuerSource(&cert_issuer_source); 1018 path_builder.AddCertIssuerSource(&cert_issuer_source);
1031 1019
1032 CertIssuerSource::IssuerCallback target_issuers_callback;
1033 // Create the mock CertIssuerSource::Request... 1020 // Create the mock CertIssuerSource::Request...
1034 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>> 1021 std::unique_ptr<StrictMock<MockCertIssuerSourceRequest>>
1035 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>()); 1022 target_issuers_req_owner(new StrictMock<MockCertIssuerSourceRequest>());
1036 // Keep a raw pointer to the Request... 1023 // Keep a raw pointer to the Request...
1037 StrictMock<MockCertIssuerSourceRequest>* target_issuers_req = 1024 StrictMock<MockCertIssuerSourceRequest>* target_issuers_req =
1038 target_issuers_req_owner.get(); 1025 target_issuers_req_owner.get();
1039 // Setup helper class to pass ownership of the Request to the PathBuilder when 1026 // Setup helper class to pass ownership of the Request to the PathBuilder when
1040 // it calls AsyncGetIssuersOf. 1027 // it calls AsyncGetIssuersOf.
1041 CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner)); 1028 CertIssuerSourceRequestMover req_mover(std::move(target_issuers_req_owner));
1042 { 1029 {
1043 ::testing::InSequence s; 1030 ::testing::InSequence s;
1044 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _)); 1031 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(target_.get(), _));
1045 EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _, _)) 1032 EXPECT_CALL(cert_issuer_source, AsyncGetIssuersOf(target_.get(), _))
1046 .WillOnce( 1033 .WillOnce(Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt));
1047 DoAll(SaveArg<1>(&target_issuers_callback),
1048 Invoke(&req_mover, &CertIssuerSourceRequestMover::MoveIt)));
1049 } 1034 }
1050 1035
1051 TestClosure callback; 1036 scoped_refptr<ParsedCertificate> oldintermediate_dupe(
1052 CompletionStatus rv = path_builder.Run(callback.closure()); 1037 ParsedCertificate::Create(oldintermediate_->der_cert().AsStringPiece(),
1053 ASSERT_EQ(CompletionStatus::ASYNC, rv); 1038 {}, nullptr));
1054 1039
1055 ASSERT_FALSE(target_issuers_callback.is_null()); 1040 EXPECT_CALL(*target_issuers_req, GetNext(_))
1041 // First async batch: return oldintermediate_.
1042 .WillOnce(Invoke(AppendCertToList(oldintermediate_)))
1043 // Second async batch: return a different copy of oldintermediate_ again.
1044 .WillOnce(Invoke(AppendCertToList(oldintermediate_dupe)))
1045 // Third async batch: return newintermediate_.
1046 .WillOnce(Invoke(AppendCertToList(newintermediate_)));
1056 1047
1057 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
1058
1059 // First async batch: return oldintermediate_.
1060 EXPECT_CALL(*target_issuers_req, GetNext(_))
1061 .WillOnce(DoAll(SetArgPointee<0>(oldintermediate_),
1062 Return(CompletionStatus::SYNC)))
1063 .WillOnce(
1064 DoAll(SetArgPointee<0>(nullptr), Return(CompletionStatus::ASYNC)));
1065 { 1048 {
1066 ::testing::InSequence s; 1049 ::testing::InSequence s;
1067 // oldintermediate_ does not create a valid path, so both sync and async 1050 // oldintermediate_ does not create a valid path, so both sync and async
1068 // lookups are expected. 1051 // lookups are expected.
1069 EXPECT_CALL(cert_issuer_source, 1052 EXPECT_CALL(cert_issuer_source,
1070 SyncGetIssuersOf(oldintermediate_.get(), _)); 1053 SyncGetIssuersOf(oldintermediate_.get(), _));
1071 EXPECT_CALL(cert_issuer_source, 1054 EXPECT_CALL(cert_issuer_source,
1072 AsyncGetIssuersOf(oldintermediate_.get(), _, _)); 1055 AsyncGetIssuersOf(oldintermediate_.get(), _));
1073 } 1056 }
1074 target_issuers_callback.Run(target_issuers_req);
1075 ::testing::Mock::VerifyAndClearExpectations(target_issuers_req);
1076 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
1077 1057
1078 // Second async batch: return a different copy of oldintermediate_ again.
1079 scoped_refptr<ParsedCertificate> oldintermediate_dupe(
1080 ParsedCertificate::Create(oldintermediate_->der_cert().AsStringPiece(),
1081 {}, nullptr));
1082 EXPECT_CALL(*target_issuers_req, GetNext(_))
1083 .WillOnce(DoAll(SetArgPointee<0>(oldintermediate_dupe),
1084 Return(CompletionStatus::SYNC)))
1085 .WillOnce(
1086 DoAll(SetArgPointee<0>(nullptr), Return(CompletionStatus::ASYNC)));
1087 target_issuers_callback.Run(target_issuers_req);
1088 // oldintermediate was already processed above, it should not generate any
1089 // more requests.
1090 ::testing::Mock::VerifyAndClearExpectations(target_issuers_req);
1091 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
1092
1093 // Third async batch: return newintermediate_.
1094 EXPECT_CALL(*target_issuers_req, GetNext(_))
1095 .WillOnce(DoAll(SetArgPointee<0>(newintermediate_),
1096 Return(CompletionStatus::SYNC)))
1097 .WillOnce(
1098 DoAll(SetArgPointee<0>(nullptr), Return(CompletionStatus::ASYNC)));
1099 // newroot_ is in the trust store, so this path will be completed 1058 // newroot_ is in the trust store, so this path will be completed
1100 // synchronously. AsyncGetIssuersOf will not be called on newintermediate_. 1059 // synchronously. AsyncGetIssuersOf will not be called on newintermediate_.
1101 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _)); 1060 EXPECT_CALL(cert_issuer_source, SyncGetIssuersOf(newintermediate_.get(), _));
1102 target_issuers_callback.Run(target_issuers_req);
1103 // Note that VerifyAndClearExpectations(target_issuers_req) is not called
1104 // here. PathBuilder could have destroyed it already, so just let the
1105 // expectations get checked by the destructor.
1106 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
1107 1061
1108 // Ensure pathbuilder finished and filled result. 1062 // Ensure pathbuilder finished and filled result.
1109 callback.WaitForResult(); 1063 path_builder.Run();
1064
1065 ::testing::Mock::VerifyAndClearExpectations(&cert_issuer_source);
1110 1066
1111 EXPECT_TRUE(result.HasValidPath()); 1067 EXPECT_TRUE(result.HasValidPath());
1112 ASSERT_EQ(2U, result.paths.size()); 1068 ASSERT_EQ(2U, result.paths.size());
1113 1069
1114 // Path builder first attempts: target <- oldintermediate <- newroot 1070 // Path builder first attempts: target <- oldintermediate <- newroot
1115 // but it will fail since oldintermediate is signed by oldroot. 1071 // but it will fail since oldintermediate is signed by oldroot.
1116 EXPECT_FALSE(result.paths[0]->valid); 1072 EXPECT_FALSE(result.paths[0]->valid);
1117 const auto& path0 = result.paths[0]->path; 1073 const auto& path0 = result.paths[0]->path;
1118 ASSERT_EQ(2U, path0.certs.size()); 1074 ASSERT_EQ(2U, path0.certs.size());
1119 EXPECT_EQ(target_, path0.certs[0]); 1075 EXPECT_EQ(target_, path0.certs[0]);
1120 EXPECT_EQ(oldintermediate_, path0.certs[1]); 1076 EXPECT_EQ(oldintermediate_, path0.certs[1]);
1121 EXPECT_EQ(newroot_, path0.trust_anchor->cert()); 1077 EXPECT_EQ(newroot_, path0.trust_anchor->cert());
1122 1078
1123 // The second async result does not generate any path. 1079 // The second async result does not generate any path.
1124 1080
1125 // After the third batch of async results, path builder will attempt: 1081 // After the third batch of async results, path builder will attempt:
1126 // target <- newintermediate <- newroot which will succeed. 1082 // target <- newintermediate <- newroot which will succeed.
1127 EXPECT_TRUE(result.paths[1]->valid); 1083 EXPECT_TRUE(result.paths[1]->valid);
1128 const auto& path1 = result.paths[1]->path; 1084 const auto& path1 = result.paths[1]->path;
1129 ASSERT_EQ(2U, path1.certs.size()); 1085 ASSERT_EQ(2U, path1.certs.size());
1130 EXPECT_EQ(target_, path1.certs[0]); 1086 EXPECT_EQ(target_, path1.certs[0]);
1131 EXPECT_EQ(newintermediate_, path1.certs[1]); 1087 EXPECT_EQ(newintermediate_, path1.certs[1]);
1132 EXPECT_EQ(newroot_, path1.trust_anchor->cert()); 1088 EXPECT_EQ(newroot_, path1.trust_anchor->cert());
1133 } 1089 }
1134 1090
1135 #endif
1136
1137 } // namespace 1091 } // namespace
1138 1092
1139 } // namespace net 1093 } // namespace net
OLDNEW
« no previous file with comments | « net/cert/internal/path_builder.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698