Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / net / cert / internal / test_helpers.h
blobd10148191a4f8a1bb6c9a05ebaa2edaea3c291c5
1 // Copyright 2015 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 #ifndef NET_CERT_INTERNAL_TEST_HELPERS_H_
6 #define NET_CERT_INTERNAL_TEST_HELPERS_H_
8 #include <ostream>
9 #include <string>
10 #include <vector>
12 #include "net/der/input.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 namespace net {
17 namespace der {
19 // These functions are used by GTest to support EXPECT_EQ() for
20 // der::Input.
21 void PrintTo(const Input& data, ::std::ostream* os);
22 bool operator==(const Input& a, const Input& b);
24 } // namespace der
26 // Creates a der::Input from an std::string. The lifetimes are a bit subtle
27 // when using this function:
29 // The returned der::Input() is only valid so long as the input string is alive
30 // and is not mutated.
32 // Note that the input parameter has been made a pointer to prevent callers
33 // from accidentally passing an r-value.
34 der::Input InputFromString(const std::string* s);
36 // Helper structure that maps a PEM block header (for instance "CERTIFICATE") to
37 // the destination where the value for that block should be written.
38 struct PemBlockMapping {
39 // The name of the PEM header. Example "CERTIFICATE".
40 const char* block_name;
42 // The destination where the read value should be written to.
43 std::string* value;
45 // True to indicate that the block is not required to be present. If the
46 // block is optional and is not present, then |value| will not be modified.
47 bool optional;
50 // ReadTestDataFromPemFile() is a helper function that reads a PEM test file
51 // rooted in the "src/" directory.
53 // * file_path_ascii:
54 // The path to the PEM file, relative to src. For instance
55 // "net/data/verify_signed_data_unittest/foopy.pem"
57 // * mappings:
58 // An array of length |mappings_length| which maps the expected PEM
59 // headers to the destination to write its data.
61 // The function ensures that each of the chosen mappings is satisfied exactly
62 // once. In other words, the header must be present (unless marked as
63 // optional=true), have valid data, and appear no more than once.
64 ::testing::AssertionResult ReadTestDataFromPemFile(
65 const std::string& file_path_ascii,
66 const PemBlockMapping* mappings,
67 size_t mappings_length);
69 // This is the same as the variant above, however it uses template magic so an
70 // mappings array can be passed in directly (and the correct length is
71 // inferred).
72 template <size_t N>
73 ::testing::AssertionResult ReadTestDataFromPemFile(
74 const std::string& file_path_ascii,
75 const PemBlockMapping(&mappings)[N]) {
76 return ReadTestDataFromPemFile(file_path_ascii, mappings, N);
79 } // namespace net
81 #endif // NET_CERT_INTERNAL_TEST_HELPERS_H_