Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / test / cert_test_util.cc
blobc6c412d60e3e26cdac90211df4302111e547d881
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"
13 namespace net {
15 CertificateList CreateCertificateListFromFile(
16 const base::FilePath& certs_dir,
17 const std::string& cert_file,
18 int format) {
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(),
24 cert_data.size(),
25 format);
28 scoped_refptr<X509Certificate> CreateCertificateChainFromFile(
29 const base::FilePath& certs_dir,
30 const std::string& cert_file,
31 int format) {
32 CertificateList certs = CreateCertificateListFromFile(
33 certs_dir, cert_file, format);
34 if (certs.empty())
35 return NULL;
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));
43 return result;
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))
52 return NULL;
54 CertificateList certs_in_file =
55 X509Certificate::CreateCertificateListFromBytes(
56 cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO);
57 if (certs_in_file.empty())
58 return NULL;
59 return certs_in_file[0];
62 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
63 const SHA1HashValue& fingerprint,
64 const char* policy)
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_));
74 } // namespace net