[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] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 16 | FilePath GetTestCertsDirectory() { |
| 17 | FilePath certs_dir; |
| 18 | PathService::Get(base::DIR_SOURCE_ROOT, &certs_dir); |
| 19 | certs_dir = certs_dir.AppendASCII("net"); |
| 20 | certs_dir = certs_dir.AppendASCII("data"); |
| 21 | certs_dir = certs_dir.AppendASCII("ssl"); |
| 22 | certs_dir = certs_dir.AppendASCII("certificates"); |
| 23 | return certs_dir; |
[email protected] | 87f26d5 | 2010-10-27 14:31:52 | [diff] [blame] | 24 | } |
| 25 | |
[email protected] | e54d0af | 2012-03-03 01:07:15 | [diff] [blame] | 26 | CertificateList CreateCertificateListFromFile( |
| 27 | const FilePath& certs_dir, |
| 28 | const std::string& cert_file, |
| 29 | int format) { |
| 30 | FilePath cert_path = certs_dir.AppendASCII(cert_file); |
| 31 | std::string cert_data; |
| 32 | if (!file_util::ReadFileToString(cert_path, &cert_data)) |
| 33 | return CertificateList(); |
| 34 | return X509Certificate::CreateCertificateListFromBytes(cert_data.data(), |
| 35 | cert_data.size(), |
| 36 | format); |
| 37 | } |
| 38 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 39 | scoped_refptr<X509Certificate> ImportCertFromFile( |
| 40 | const FilePath& certs_dir, |
| 41 | const std::string& cert_file) { |
| 42 | FilePath cert_path = certs_dir.AppendASCII(cert_file); |
| 43 | std::string cert_data; |
| 44 | if (!file_util::ReadFileToString(cert_path, &cert_data)) |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 45 | return NULL; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 46 | |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 47 | CertificateList certs_in_file = |
| 48 | X509Certificate::CreateCertificateListFromBytes( |
| 49 | cert_data.data(), cert_data.size(), X509Certificate::FORMAT_AUTO); |
| 50 | if (certs_in_file.empty()) |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 51 | return NULL; |
[email protected] | 32765f8 | 2010-12-16 00:01:37 | [diff] [blame] | 52 | return certs_in_file[0]; |
[email protected] | 1e5fead | 2010-10-08 14:33:11 | [diff] [blame] | 53 | } |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 54 | |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 55 | ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata, |
[email protected] | ede0321 | 2012-09-07 12:52:26 | [diff] [blame^] | 56 | const SHA1HashValue& fingerprint, |
[email protected] | 7d015e4 | 2012-03-14 16:15:12 | [diff] [blame] | 57 | const char* policy) |
| 58 | : fingerprint_(fingerprint), |
| 59 | ev_root_ca_metadata_(ev_root_ca_metadata) { |
| 60 | EXPECT_TRUE(ev_root_ca_metadata->AddEVCA(fingerprint, policy)); |
| 61 | } |
| 62 | |
| 63 | ScopedTestEVPolicy::~ScopedTestEVPolicy() { |
| 64 | EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_)); |
| 65 | } |
| 66 | |
[email protected] | c81d9dcc | 2010-03-17 00:51:44 | [diff] [blame] | 67 | } // namespace net |