blob: 781587256af01947df9fea1f7da2acf33d4e1ee8 [file] [log] [blame]
[email protected]e54d0af2012-03-03 01:07:151// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]c81d9dcc2010-03-17 00:51:442// 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]32765f82010-12-16 00:01:377#include "base/file_path.h"
[email protected]c81d9dcc2010-03-17 00:51:448#include "base/file_util.h"
[email protected]c81d9dcc2010-03-17 00:51:449#include "base/path_service.h"
[email protected]7d015e42012-03-14 16:15:1210#include "net/base/ev_root_ca_metadata.h"
[email protected]c81d9dcc2010-03-17 00:51:4411#include "net/base/x509_certificate.h"
[email protected]7d015e42012-03-14 16:15:1212#include "testing/gtest/include/gtest/gtest.h"
[email protected]c81d9dcc2010-03-17 00:51:4413
14namespace net {
15
[email protected]32765f82010-12-16 00:01:3716FilePath 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]87f26d52010-10-27 14:31:5224}
25
[email protected]e54d0af2012-03-03 01:07:1526CertificateList 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]32765f82010-12-16 00:01:3739scoped_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]1e5fead2010-10-08 14:33:1145 return NULL;
[email protected]1e5fead2010-10-08 14:33:1146
[email protected]32765f82010-12-16 00:01:3747 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]1e5fead2010-10-08 14:33:1151 return NULL;
[email protected]32765f82010-12-16 00:01:3752 return certs_in_file[0];
[email protected]1e5fead2010-10-08 14:33:1153}
[email protected]c81d9dcc2010-03-17 00:51:4454
[email protected]7d015e42012-03-14 16:15:1255ScopedTestEVPolicy::ScopedTestEVPolicy(EVRootCAMetadata* ev_root_ca_metadata,
[email protected]ede03212012-09-07 12:52:2656 const SHA1HashValue& fingerprint,
[email protected]7d015e42012-03-14 16:15:1257 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
63ScopedTestEVPolicy::~ScopedTestEVPolicy() {
64 EXPECT_TRUE(ev_root_ca_metadata_->RemoveEVCA(fingerprint_));
65}
66
[email protected]c81d9dcc2010-03-17 00:51:4467} // namespace net