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