| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/test/cert_test_util.h" | 5 #include "net/test/cert_test_util.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "net/cert/ev_root_ca_metadata.h" | 10 #include "net/cert/ev_root_ca_metadata.h" |
| 11 #include "net/cert/x509_certificate.h" | 11 #include "net/cert/x509_certificate.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 13 |
| 14 namespace net { | 14 namespace net { |
| 15 | 15 |
| 16 CertificateList CreateCertificateListFromFile( | 16 CertificateList CreateCertificateListFromFile(const base::FilePath& certs_dir, |
| 17 const base::FilePath& certs_dir, | 17 const std::string& cert_file, |
| 18 const std::string& cert_file, | 18 int format) { |
| 19 int format) { | |
| 20 base::FilePath cert_path = certs_dir.AppendASCII(cert_file); | 19 base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
| 21 std::string cert_data; | 20 std::string cert_data; |
| 22 if (!base::ReadFileToString(cert_path, &cert_data)) | 21 if (!base::ReadFileToString(cert_path, &cert_data)) |
| 23 return CertificateList(); | 22 return CertificateList(); |
| 24 return X509Certificate::CreateCertificateListFromBytes(cert_data.data(), | 23 return X509Certificate::CreateCertificateListFromBytes( |
| 25 cert_data.size(), | 24 cert_data.data(), cert_data.size(), format); |
| 26 format); | |
| 27 } | 25 } |
| 28 | 26 |
| 29 scoped_refptr<X509Certificate> CreateCertificateChainFromFile( | 27 scoped_refptr<X509Certificate> CreateCertificateChainFromFile( |
| 30 const base::FilePath& certs_dir, | 28 const base::FilePath& certs_dir, |
| 31 const std::string& cert_file, | 29 const std::string& cert_file, |
| 32 int format) { | 30 int format) { |
| 33 CertificateList certs = CreateCertificateListFromFile( | 31 CertificateList certs = |
| 34 certs_dir, cert_file, format); | 32 CreateCertificateListFromFile(certs_dir, cert_file, format); |
| 35 if (certs.empty()) | 33 if (certs.empty()) |
| 36 return NULL; | 34 return NULL; |
| 37 | 35 |
| 38 X509Certificate::OSCertHandles intermediates; | 36 X509Certificate::OSCertHandles intermediates; |
| 39 for (size_t i = 1; i < certs.size(); ++i) | 37 for (size_t i = 1; i < certs.size(); ++i) |
| 40 intermediates.push_back(certs[i]->os_cert_handle()); | 38 intermediates.push_back(certs[i]->os_cert_handle()); |
| 41 | 39 |
| 42 scoped_refptr<X509Certificate> result(X509Certificate::CreateFromHandle( | 40 scoped_refptr<X509Certificate> result(X509Certificate::CreateFromHandle( |
| 43 certs[0]->os_cert_handle(), intermediates)); | 41 certs[0]->os_cert_handle(), intermediates)); |
| 44 return result; | 42 return result; |
| 45 } | 43 } |
| 46 | 44 |
| 47 scoped_refptr<X509Certificate> ImportCertFromFile( | 45 scoped_refptr<X509Certificate> ImportCertFromFile( |
| 48 const base::FilePath& certs_dir, | 46 const base::FilePath& certs_dir, |
| 49 const std::string& cert_file) { | 47 const std::string& cert_file) { |
| 50 base::FilePath cert_path = certs_dir.AppendASCII(cert_file); | 48 base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
| 51 std::string cert_data; | 49 std::string cert_data; |
| 52 if (!base::ReadFileToString(cert_path, &cert_data)) | 50 if (!base::ReadFileToString(cert_path, &cert_data)) |
| 53 return NULL; | 51 return NULL; |
| 54 | 52 |
| 55 CertificateList certs_in_file = | 53 CertificateList certs_in_file = |
| 56 X509Certificate::CreateCertificateListFromBytes( | 54 X509Certificate::CreateCertificateListFromBytes( |
| 57 cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO); | 55 cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO); |
| 58 if (certs_in_file.empty()) | 56 if (certs_in_file.empty()) |
| 59 return NULL; | 57 return NULL; |
| 60 return certs_in_file[0]; | 58 return certs_in_file[0]; |
| 61 } | 59 } |
| 62 | 60 |
| 63 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, | 61 ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, |
| 64 const SHA1HashValue& fingerprint, | 62 const SHA1HashValue& fingerprint, |
| 65 const char* policy) | 63 const char* policy) |
| 66 : fingerprint_(fingerprint), | 64 : fingerprint_(fingerprint), ev_root_ca_metadata_(ev_root_ca_metadata) { |
| 67 ev_root_ca_metadata_(ev_root_ca_metadata) { | |
| 68 EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy)); | 65 EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy)); |
| 69 } | 66 } |
| 70 | 67 |
| 71 ScopedTestEVPolicy::~ScopedTestEVPolicy() { | 68 ScopedTestEVPolicy::~ScopedTestEVPolicy() { |
| 72 EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_)); | 69 EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_)); |
| 73 } | 70 } |
| 74 | 71 |
| 75 } // namespace net | 72 } // namespace net |
| OLD | NEW |