1*6777b538SAndroid Build Coastguard Worker // Copyright 2015 The Chromium Authors
2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file.
4*6777b538SAndroid Build Coastguard Worker
5*6777b538SAndroid Build Coastguard Worker #ifndef NET_CERT_INTERNAL_TEST_HELPERS_H_
6*6777b538SAndroid Build Coastguard Worker #define NET_CERT_INTERNAL_TEST_HELPERS_H_
7*6777b538SAndroid Build Coastguard Worker
8*6777b538SAndroid Build Coastguard Worker #include <stddef.h>
9*6777b538SAndroid Build Coastguard Worker
10*6777b538SAndroid Build Coastguard Worker #include <string>
11*6777b538SAndroid Build Coastguard Worker
12*6777b538SAndroid Build Coastguard Worker #include "testing/gtest/include/gtest/gtest.h"
13*6777b538SAndroid Build Coastguard Worker #include "third_party/boringssl/src/pki/parsed_certificate.h"
14*6777b538SAndroid Build Coastguard Worker
15*6777b538SAndroid Build Coastguard Worker namespace net {
16*6777b538SAndroid Build Coastguard Worker
17*6777b538SAndroid Build Coastguard Worker // Helper structure that maps a PEM block header (for instance "CERTIFICATE") to
18*6777b538SAndroid Build Coastguard Worker // the destination where the value for that block should be written.
19*6777b538SAndroid Build Coastguard Worker struct PemBlockMapping {
20*6777b538SAndroid Build Coastguard Worker // The name of the PEM header. Example "CERTIFICATE".
21*6777b538SAndroid Build Coastguard Worker const char* block_name;
22*6777b538SAndroid Build Coastguard Worker
23*6777b538SAndroid Build Coastguard Worker // The destination where the read value should be written to.
24*6777b538SAndroid Build Coastguard Worker std::string* value;
25*6777b538SAndroid Build Coastguard Worker
26*6777b538SAndroid Build Coastguard Worker // True to indicate that the block is not required to be present. If the
27*6777b538SAndroid Build Coastguard Worker // block is optional and is not present, then |value| will not be modified.
28*6777b538SAndroid Build Coastguard Worker bool optional = false;
29*6777b538SAndroid Build Coastguard Worker };
30*6777b538SAndroid Build Coastguard Worker
31*6777b538SAndroid Build Coastguard Worker // ReadTestDataFromPemFile() is a helper function that reads a PEM test file
32*6777b538SAndroid Build Coastguard Worker // rooted in the "src/" directory.
33*6777b538SAndroid Build Coastguard Worker //
34*6777b538SAndroid Build Coastguard Worker // * file_path_ascii:
35*6777b538SAndroid Build Coastguard Worker // The path to the PEM file, relative to src. For instance
36*6777b538SAndroid Build Coastguard Worker // "net/data/verify_signed_data_unittest/foopy.pem"
37*6777b538SAndroid Build Coastguard Worker //
38*6777b538SAndroid Build Coastguard Worker // * mappings:
39*6777b538SAndroid Build Coastguard Worker // An array of length |mappings_length| which maps the expected PEM
40*6777b538SAndroid Build Coastguard Worker // headers to the destination to write its data.
41*6777b538SAndroid Build Coastguard Worker //
42*6777b538SAndroid Build Coastguard Worker // The function ensures that each of the chosen mappings is satisfied exactly
43*6777b538SAndroid Build Coastguard Worker // once. In other words, the header must be present (unless marked as
44*6777b538SAndroid Build Coastguard Worker // optional=true), have valid data, and appear no more than once.
45*6777b538SAndroid Build Coastguard Worker ::testing::AssertionResult ReadTestDataFromPemFile(
46*6777b538SAndroid Build Coastguard Worker const std::string& file_path_ascii,
47*6777b538SAndroid Build Coastguard Worker const PemBlockMapping* mappings,
48*6777b538SAndroid Build Coastguard Worker size_t mappings_length);
49*6777b538SAndroid Build Coastguard Worker
50*6777b538SAndroid Build Coastguard Worker // This is the same as the variant above, however it uses template magic so an
51*6777b538SAndroid Build Coastguard Worker // mappings array can be passed in directly (and the correct length is
52*6777b538SAndroid Build Coastguard Worker // inferred).
53*6777b538SAndroid Build Coastguard Worker template <size_t N>
ReadTestDataFromPemFile(const std::string & file_path_ascii,const PemBlockMapping (& mappings)[N])54*6777b538SAndroid Build Coastguard Worker ::testing::AssertionResult ReadTestDataFromPemFile(
55*6777b538SAndroid Build Coastguard Worker const std::string& file_path_ascii,
56*6777b538SAndroid Build Coastguard Worker const PemBlockMapping (&mappings)[N]) {
57*6777b538SAndroid Build Coastguard Worker return ReadTestDataFromPemFile(file_path_ascii, mappings, N);
58*6777b538SAndroid Build Coastguard Worker }
59*6777b538SAndroid Build Coastguard Worker
60*6777b538SAndroid Build Coastguard Worker // Reads a certificate chain from |file_path_ascii|
61*6777b538SAndroid Build Coastguard Worker bool ReadCertChainFromFile(const std::string& file_path_ascii,
62*6777b538SAndroid Build Coastguard Worker bssl::ParsedCertificateList* chain);
63*6777b538SAndroid Build Coastguard Worker
64*6777b538SAndroid Build Coastguard Worker // Reads a certificate from |file_path_ascii|. Returns nullptr if the file
65*6777b538SAndroid Build Coastguard Worker // contained more that one certificate.
66*6777b538SAndroid Build Coastguard Worker std::shared_ptr<const bssl::ParsedCertificate> ReadCertFromFile(
67*6777b538SAndroid Build Coastguard Worker const std::string& file_path_ascii);
68*6777b538SAndroid Build Coastguard Worker
69*6777b538SAndroid Build Coastguard Worker // Reads a data file relative to the src root directory.
70*6777b538SAndroid Build Coastguard Worker std::string ReadTestFileToString(const std::string& file_path_ascii);
71*6777b538SAndroid Build Coastguard Worker
72*6777b538SAndroid Build Coastguard Worker } // namespace net
73*6777b538SAndroid Build Coastguard Worker
74*6777b538SAndroid Build Coastguard Worker #endif // NET_CERT_INTERNAL_TEST_HELPERS_H_
75