1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ios/web/net/cert_verifier_block_adapter.h"
7 #include "base/test/ios/wait_util.h"
8 #include "net/base/net_errors.h"
9 #include "net/cert/cert_verify_result.h"
10 #include "net/cert/crl_set.h"
11 #include "net/cert/x509_certificate.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/platform_test.h"
21 // Test hostname for CertVerifier.
22 const char kHostName
[] = "chromium.org";
23 // Test OCSP response for CertVerifier.
24 const char kOcspResponse
[] = "ocsp";
26 // Mocks CertVerifier for CertVerifierBlockAdapter testing.
27 class CertVerifierMock
: public CertVerifier
{
30 int(X509Certificate
* cert
,
31 const std::string
& hostname
,
32 const std::string
& ocsp_response
,
35 CertVerifyResult
* verify_result
,
36 const CompletionCallback
& callback
,
37 scoped_ptr
<Request
>* out_req
,
38 const BoundNetLog
& net_log
));
41 // Sets CertVerifyResult to emulate CertVerifier behavior.
42 ACTION_P(SetVerifyResult
, result
) {
46 // Calls CompletionCallback to emulate CertVerifier behavior.
53 // Test fixture to test CertVerifierBlockAdapter class.
54 class CertVerifierBlockAdapterTest
: public PlatformTest
{
56 void SetUp() override
{
57 PlatformTest::SetUp();
59 cert_
= new X509Certificate("test", "test", base::Time(), base::Time());
60 scoped_ptr
<CertVerifierMock
> cert_verifier_mock(new CertVerifierMock());
61 cert_verifier_mock_
= cert_verifier_mock
.get();
63 new CertVerifierBlockAdapter(cert_verifier_mock
.Pass()));
66 // Performs synchronous verification.
67 void Verify(CertVerifierBlockAdapter::Params params
,
68 scoped_ptr
<net::CertVerifyResult
>* result
,
70 __block
bool verification_completed
= false;
71 test_adapter_
->Verify(params
,
72 ^(scoped_ptr
<net::CertVerifyResult
> callback_result
,
73 int callback_status
) {
74 *result
= callback_result
.Pass();
75 *status
= callback_status
;
76 verification_completed
= true;
78 base::test::ios::WaitUntilCondition(^{
79 return verification_completed
;
83 // Fake certificate created for testing.
84 scoped_refptr
<X509Certificate
> cert_
;
85 // Testable |CertVerifierBlockAdapter| object.
86 scoped_ptr
<CertVerifierBlockAdapter
> test_adapter_
;
87 // CertVerifier mock owned by |test_adapter_|.
88 CertVerifierMock
* cert_verifier_mock_
;
91 // Tests |Verify| with default params and synchronous verification.
92 TEST_F(CertVerifierBlockAdapterTest
, DefaultParamsAndSync
) {
93 // Set up expectation.
94 net::CertVerifyResult expectedResult
;
95 expectedResult
.cert_status
= net::CERT_STATUS_AUTHORITY_INVALID
;
96 const int kExpectedStatus
= 0;
97 EXPECT_CALL(*cert_verifier_mock_
,
98 Verify(cert_
.get(), kHostName
, "", 0, nullptr, _
, _
, _
, _
))
100 .WillOnce(testing::DoAll(SetVerifyResult(expectedResult
),
101 testing::Return(kExpectedStatus
)));
104 scoped_ptr
<CertVerifyResult
> actualResult
;
105 int actualStatus
= -1;
106 CertVerifierBlockAdapter::Params
params(cert_
.get(), kHostName
);
107 Verify(params
, &actualResult
, &actualStatus
);
109 // Ensure that Verification results are correct.
110 EXPECT_EQ(kExpectedStatus
, actualStatus
);
111 EXPECT_EQ(expectedResult
.cert_status
, actualResult
->cert_status
);
114 // Tests |Verify| with default params and asynchronous verification.
115 TEST_F(CertVerifierBlockAdapterTest
, DefaultParamsAndAsync
) {
116 // Set up expectation.
117 net::CertVerifyResult expectedResult
;
118 expectedResult
.is_issued_by_known_root
= true;
119 const int kExpectedStatus
= 0;
120 EXPECT_CALL(*cert_verifier_mock_
,
121 Verify(cert_
.get(), kHostName
, "", 0, nullptr, _
, _
, _
, _
))
123 .WillOnce(testing::DoAll(SetVerifyResult(expectedResult
), RunCallback(),
124 testing::Return(ERR_IO_PENDING
)));
127 scoped_ptr
<CertVerifyResult
> actualResult
;
128 int actualStatus
= -1;
129 CertVerifierBlockAdapter::Params
params(cert_
.get(), kHostName
);
130 Verify(params
, &actualResult
, &actualStatus
);
132 // Ensure that Verification results are correct.
133 EXPECT_EQ(kExpectedStatus
, actualStatus
);
134 EXPECT_EQ(expectedResult
.is_issued_by_known_root
,
135 actualResult
->is_issued_by_known_root
);
138 // Tests |Verify| with invalid arguments.
139 TEST_F(CertVerifierBlockAdapterTest
, InvalidParamsAndError
) {
140 // Set up expectation.
141 const int kExpectedStatus
= ERR_INVALID_ARGUMENT
;
142 EXPECT_CALL(*cert_verifier_mock_
,
143 Verify(nullptr, "", "", 0, nullptr, _
, _
, _
, _
))
145 .WillOnce(testing::Return(kExpectedStatus
));
148 scoped_ptr
<CertVerifyResult
> actualResult
;
149 int actualStatus
= -1;
150 CertVerifierBlockAdapter::Params
params(nullptr, "");
151 Verify(params
, &actualResult
, &actualStatus
);
153 // Ensure that Verification results are correct.
154 EXPECT_EQ(kExpectedStatus
, actualStatus
);
155 EXPECT_FALSE(actualResult
);
158 // Tests |Verify| with error.
159 TEST_F(CertVerifierBlockAdapterTest
, DefaultParamsAndError
) {
160 // Set up expectation.
161 const int kExpectedStatus
= ERR_INSUFFICIENT_RESOURCES
;
162 EXPECT_CALL(*cert_verifier_mock_
,
163 Verify(cert_
.get(), kHostName
, "", 0, nullptr, _
, _
, _
, _
))
165 .WillOnce(testing::Return(kExpectedStatus
));
168 scoped_ptr
<CertVerifyResult
> actualResult
;
169 int actualStatus
= -1;
170 CertVerifierBlockAdapter::Params
params(cert_
.get(), kHostName
);
171 Verify(params
, &actualResult
, &actualStatus
);
173 // Ensure that Verification results are correct.
174 EXPECT_EQ(kExpectedStatus
, actualStatus
);
175 EXPECT_FALSE(actualResult
);
178 // Tests |Verify| with all params and synchronous verification.
179 TEST_F(CertVerifierBlockAdapterTest
, AllParamsAndSync
) {
180 // Set up expectation.
181 net::CertVerifyResult expectedResult
;
182 expectedResult
.verified_cert
= cert_
;
183 const int kExpectedStatus
= 0;
184 scoped_refptr
<CRLSet
> crl_set(CRLSet::EmptyCRLSetForTesting());
185 EXPECT_CALL(*cert_verifier_mock_
,
186 Verify(cert_
.get(), kHostName
, kOcspResponse
,
187 CertVerifier::VERIFY_EV_CERT
, crl_set
.get(), _
, _
, _
, _
))
189 .WillOnce(testing::DoAll(SetVerifyResult(expectedResult
),
190 testing::Return(kExpectedStatus
)));
193 scoped_ptr
<CertVerifyResult
> actualResult
;
194 int actualStatus
= -1;
195 CertVerifierBlockAdapter::Params
params(cert_
.get(), kHostName
);
196 params
.ocsp_response
= kOcspResponse
;
197 params
.flags
= CertVerifier::VERIFY_EV_CERT
;
198 params
.crl_set
= crl_set
;
199 Verify(params
, &actualResult
, &actualStatus
);
201 // Ensure that Verification results are correct.
202 EXPECT_EQ(kExpectedStatus
, actualStatus
);
203 EXPECT_EQ(expectedResult
.verified_cert
, actualResult
->verified_cert
);