forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_cert_store_mac_unittest.cc
99 lines (81 loc) · 3.45 KB
/
client_cert_store_mac_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/ssl/client_cert_store_mac.h"
#include <memory>
#include "net/ssl/client_cert_identity_test_util.h"
#include "net/ssl/client_cert_store_unittest-inl.h"
#include "net/ssl/ssl_private_key.h"
namespace net {
class ClientCertStoreMacTestDelegate {
public:
bool SelectClientCerts(const CertificateList& input_certs,
const SSLCertRequestInfo& cert_request_info,
ClientCertIdentityList* selected_certs) {
return store_.SelectClientCertsForTesting(
FakeClientCertIdentityListFromCertificateList(input_certs),
cert_request_info, selected_certs);
}
private:
ClientCertStoreMac store_;
};
INSTANTIATE_TYPED_TEST_CASE_P(Mac,
ClientCertStoreTest,
ClientCertStoreMacTestDelegate);
class ClientCertStoreMacTest : public ::testing::Test {
protected:
bool SelectClientCertsGivenPreferred(
const scoped_refptr<X509Certificate>& preferred_cert,
const CertificateList& regular_certs,
const SSLCertRequestInfo& request,
ClientCertIdentityList* selected_certs) {
std::unique_ptr<ClientCertIdentity> preferred_identity(
std::make_unique<FakeClientCertIdentity>(preferred_cert, nullptr));
return store_.SelectClientCertsGivenPreferredForTesting(
std::move(preferred_identity),
FakeClientCertIdentityListFromCertificateList(regular_certs), request,
selected_certs);
}
private:
ClientCertStoreMac store_;
};
// Verify that the preferred cert gets filtered out when it doesn't match the
// server criteria.
TEST_F(ClientCertStoreMacTest, FilterOutThePreferredCert) {
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
std::vector<std::string> authority_2(
1, std::string(reinterpret_cast<const char*>(kAuthority2DN),
sizeof(kAuthority2DN)));
EXPECT_FALSE(cert_1->IsIssuedByEncoded(authority_2));
std::vector<scoped_refptr<X509Certificate> > certs;
scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo());
request->cert_authorities = authority_2;
ClientCertIdentityList selected_certs;
bool rv = SelectClientCertsGivenPreferred(
cert_1, certs, *request.get(), &selected_certs);
EXPECT_TRUE(rv);
EXPECT_EQ(0u, selected_certs.size());
}
// Verify that the preferred cert takes the first position in the output list,
// when it does not get filtered out.
TEST_F(ClientCertStoreMacTest, PreferredCertGoesFirst) {
scoped_refptr<X509Certificate> cert_1(
ImportCertFromFile(GetTestCertsDirectory(), "client_1.pem"));
ASSERT_TRUE(cert_1.get());
scoped_refptr<X509Certificate> cert_2(
ImportCertFromFile(GetTestCertsDirectory(), "client_2.pem"));
ASSERT_TRUE(cert_2.get());
std::vector<scoped_refptr<X509Certificate> > certs;
certs.push_back(cert_2);
scoped_refptr<SSLCertRequestInfo> request(new SSLCertRequestInfo());
ClientCertIdentityList selected_certs;
bool rv = SelectClientCertsGivenPreferred(
cert_1, certs, *request.get(), &selected_certs);
EXPECT_TRUE(rv);
ASSERT_EQ(2u, selected_certs.size());
EXPECT_TRUE(selected_certs[0]->certificate()->Equals(cert_1.get()));
EXPECT_TRUE(selected_certs[1]->certificate()->Equals(cert_2.get()));
}
} // namespace net