1// Copyright 2016 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.
4
5#include "components/cast_certificate/cast_cert_validator_test_helpers.h"
6
7#include "base/files/file_util.h"
8#include "base/path_service.h"
9#include "net/cert/internal/cert_errors.h"
10#include "net/cert/pem_tokenizer.h"
11#include "net/cert/x509_util.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace cast_certificate {
15
16namespace testing {
17
18std::string ReadTestFileToString(const base::StringPiece& file_name) {
19 base::FilePath filepath;
20 base::PathService::Get(base::DIR_SOURCE_ROOT, &filepath);
21 filepath = filepath.Append(FILE_PATH_LITERAL("components"));
22 filepath = filepath.Append(FILE_PATH_LITERAL("test"));
23 filepath = filepath.Append(FILE_PATH_LITERAL("data"));
24 filepath = filepath.Append(FILE_PATH_LITERAL("cast_certificate"));
25 filepath = filepath.AppendASCII(file_name);
26
27 // Read the full contents of the file.
28 std::string file_data;
29 if (!base::ReadFileToString(filepath, &file_data)) {
30 ADD_FAILURE() << "Couldn't read file: " << filepath.value();
31 return std::string();
32 }
33
34 return file_data;
35}
36
37std::vector<std::string> ReadCertificateChainFromFile(
38 const base::StringPiece& file_name) {
39 std::string file_data = ReadTestFileToString(file_name);
40
41 std::vector<std::string> pem_headers;
42 pem_headers.push_back("CERTIFICATE");
43
44 std::vector<std::string> certs;
45 net::PEMTokenizer pem_tokenizer(file_data, pem_headers);
46 while (pem_tokenizer.GetNext())
47 certs.push_back(pem_tokenizer.data());
48
49 EXPECT_FALSE(certs.empty());
50 return certs;
51}
52
53SignatureTestData ReadSignatureTestData(const base::StringPiece& file_name) {
54 SignatureTestData result;
55
56 std::string file_data = ReadTestFileToString(file_name);
57 EXPECT_FALSE(file_data.empty());
58
59 std::vector<std::string> pem_headers;
60 pem_headers.push_back("MESSAGE");
61 pem_headers.push_back("SIGNATURE SHA1");
62 pem_headers.push_back("SIGNATURE SHA256");
63
64 net::PEMTokenizer pem_tokenizer(file_data, pem_headers);
65 while (pem_tokenizer.GetNext()) {
66 const std::string& type = pem_tokenizer.block_type();
67 const std::string& value = pem_tokenizer.data();
68
69 if (type == "MESSAGE") {
70 result.message = value;
71 } else if (type == "SIGNATURE SHA1") {
72 result.signature_sha1 = value;
73 } else if (type == "SIGNATURE SHA256") {
74 result.signature_sha256 = value;
75 }
76 }
77
78 EXPECT_FALSE(result.message.empty());
79 EXPECT_FALSE(result.signature_sha1.empty());
80 EXPECT_FALSE(result.signature_sha256.empty());
81
82 return result;
83}
84
85std::unique_ptr<net::TrustStoreInMemory> CreateTrustStoreFromFile(
86 const std::string& path) {
87 std::unique_ptr<net::TrustStoreInMemory> trust_store(
88 new net::TrustStoreInMemory());
89 const auto trusted_test_roots =
90 cast_certificate::testing::ReadCertificateChainFromFile(path);
91 for (const auto& trusted_root : trusted_test_roots) {
92 net::CertErrors errors;
93 scoped_refptr<net::ParsedCertificate> cert(net::ParsedCertificate::Create(
94 net::x509_util::CreateCryptoBuffer(trusted_root), {}, &errors));
95 EXPECT_TRUE(cert) << errors.ToDebugString();
96 trust_store->AddTrustAnchorWithConstraints(cert);
97 }
98 return trust_store;
99}
100
101base::Time ConvertUnixTimestampSeconds(uint64_t time) {
102 return base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(time);
103}
104
105} // namespace testing
106
107} // namespace cast_certificate
108