[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/base/cert_test_util.h" |
| 6 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 7 | #include "base/file_path.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 8 | #include "base/file_util.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 9 | #include "base/path_service.h" |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 10 | #include "net/base/ev_root_ca_metadata.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 11 | #include "net/base/x509_certificate.h" |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 12 | #include "testing/gtest/include/gtest/gtest.h" |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 16 | CertificateList CreateCertificateListFromFile( |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame^] | 17 | const base::FilePath& certs_dir, |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 18 | const std::string& cert_file, |
| 19 | int format) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame^] | 20 | base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 21 | std::string cert_data; |
| 22 | if (!file_util::ReadFileToString(cert_path, &cert_data)) |
| 23 | return CertificateList(); |
| 24 | return X509Certificate::CreateCertificateListFromBytes(cert_data.data(), |
| 25 | cert_data.size(), |
| 26 | format); |
| 27 | } |
| 28 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 29 | scoped_refptr<X509Certificate> ImportCertFromFile( |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame^] | 30 | const base::FilePath& certs_dir, |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 31 | const std::string& cert_file) { |
[email protected] | 6cdfd7f | 2013-02-08 20:40:15 | [diff] [blame^] | 32 | base::FilePath cert_path = certs_dir.AppendASCII(cert_file); |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 33 | std::string cert_data; |
| 34 | if (!file_util::ReadFileToString(cert_path, &cert_data)) |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 35 | return NULL; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 36 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 37 | CertificateList certs_in_file = |
| 38 | X509Certificate::CreateCertificateListFromBytes( |
| 39 | cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO); |
| 40 | if (certs_in_file.empty()) |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 41 | return NULL; |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 42 | return certs_in_file[0]; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 43 | } |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 44 | |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 45 | ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame] | 46 | const SHA1HashValue& fingerprint, |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 47 | const char* policy) |
| 48 | : fingerprint_(fingerprint), |
| 49 | ev_root_ca_metadata_(ev_root_ca_metadata) { |
| 50 | EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy)); |
| 51 | } |
| 52 | |
| 53 | ScopedTestEVPolicy::~ScopedTestEVPolicy() { |
| 54 | EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_)); |
| 55 | } |
| 56 | |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 57 | } // namespace net |