1 // Copyright (c) 2012 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 "net/test/cert_test_util.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "net/cert/ev_root_ca_metadata.h"
10 #include "net/cert/x509_certificate.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 CertificateList
CreateCertificateListFromFile(
16 const base::FilePath
& certs_dir
,
17 const std::string
& cert_file
,
19 base::FilePath cert_path
= certs_dir
.AppendASCII(cert_file
);
20 std::string cert_data
;
21 if (!base::ReadFileToString(cert_path
, &cert_data
))
22 return CertificateList();
23 return X509Certificate::CreateCertificateListFromBytes(cert_data
.data(),
28 scoped_refptr
<X509Certificate
> CreateCertificateChainFromFile(
29 const base::FilePath
& certs_dir
,
30 const std::string
& cert_file
,
32 CertificateList certs
= CreateCertificateListFromFile(
33 certs_dir
, cert_file
, format
);
37 X509Certificate::OSCertHandles intermediates
;
38 for (size_t i
= 1; i
< certs
.size(); ++i
)
39 intermediates
.push_back(certs
[i
]->os_cert_handle());
41 scoped_refptr
<X509Certificate
> result(X509Certificate::CreateFromHandle(
42 certs
[0]->os_cert_handle(), intermediates
));
46 scoped_refptr
<X509Certificate
> ImportCertFromFile(
47 const base::FilePath
& certs_dir
,
48 const std::string
& cert_file
) {
49 base::FilePath cert_path
= certs_dir
.AppendASCII(cert_file
);
50 std::string cert_data
;
51 if (!base::ReadFileToString(cert_path
, &cert_data
))
54 CertificateList certs_in_file
=
55 X509Certificate::CreateCertificateListFromBytes(
56 cert_data
.data(), cert_data
.size(), X509Certificate::FORMAT_AUTO
);
57 if (certs_in_file
.empty())
59 return certs_in_file
[0];
62 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata
* ev_root_ca_metadata
,
63 const SHA1HashValue
& fingerprint
,
65 : fingerprint_(fingerprint
),
66 ev_root_ca_metadata_(ev_root_ca_metadata
) {
67 EXPECT_TRUE(ev_root_ca_metadata
->AddEVCA(fingerprint
, policy
));
70 ScopedTestEVPolicy::~ScopedTestEVPolicy() {
71 EXPECT_TRUE(ev_root_ca_metadata_
->RemoveEVCA(fingerprint_
));