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/cert/test_root_certs.h"
9 #include "base/files/file_path.h"
10 #include "base/files/file_util.h"
11 #include "base/logging.h"
12 #include "net/cert/x509_certificate.h"
18 bool g_has_instance
= false;
20 base::LazyInstance
<TestRootCerts
>::Leaky
21 g_test_root_certs
= LAZY_INSTANCE_INITIALIZER
;
23 CertificateList
LoadCertificates(const base::FilePath
& filename
) {
25 if (!base::ReadFileToString(filename
, &raw_cert
)) {
26 LOG(ERROR
) << "Can't load certificate " << filename
.value();
27 return CertificateList();
30 return X509Certificate::CreateCertificateListFromBytes(
31 raw_cert
.data(), raw_cert
.length(), X509Certificate::FORMAT_AUTO
);
37 TestRootCerts
* TestRootCerts::GetInstance() {
38 return g_test_root_certs
.Pointer();
41 bool TestRootCerts::HasInstance() {
42 return g_has_instance
;
45 bool TestRootCerts::AddFromFile(const base::FilePath
& file
) {
46 CertificateList root_certs
= LoadCertificates(file
);
47 if (root_certs
.empty() || root_certs
.size() > 1)
50 return Add(root_certs
.front().get());
53 TestRootCerts::TestRootCerts() {
55 g_has_instance
= true;
58 ScopedTestRoot::ScopedTestRoot() {}
60 ScopedTestRoot::ScopedTestRoot(X509Certificate
* cert
) {
64 ScopedTestRoot::~ScopedTestRoot() {
68 void ScopedTestRoot::Reset(X509Certificate
* cert
) {
70 TestRootCerts::GetInstance()->Clear();
72 TestRootCerts::GetInstance()->Add(cert
);