blob: 501375bc466d265708befd631020a0273b8907a0 [file] [log] [blame]
David Benjamin06b94de2015-05-09 22:46:47 -04001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#ifndef OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H
16#define OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H
17
Brian Smith061332f2016-01-17 09:30:42 -100018#include <openssl/base.h>
19
David Benjamin06b94de2015-05-09 22:46:47 -040020#include <stdint.h>
21#include <stdio.h>
22
Brian Smith906e2992015-07-21 21:46:20 -040023#if defined(_MSC_VER)
24#pragma warning(push)
25#pragma warning(disable: 4702)
26#endif
27
David Benjamin06b94de2015-05-09 22:46:47 -040028#include <string>
29#include <map>
30#include <set>
31#include <vector>
32
Brian Smith906e2992015-07-21 21:46:20 -040033#if defined(_MSC_VER)
34#pragma warning(pop)
35#endif
David Benjamin06b94de2015-05-09 22:46:47 -040036
37// File-based test framework.
38//
39// This module provides a file-based test framework. The file format is based on
40// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. Each input
41// file is a sequence of attributes, blocks, and blank lines.
42//
43// Each attribute has the form:
44//
45// Name = Value
46//
47// Either '=' or ':' may be used to delimit the name from the value. Both the
48// name and value have leading and trailing spaces stripped.
49//
50// Blocks are delimited by lines beginning with three hyphens, "---". One such
51// line begins a block and another ends it. Blocks are intended as a convenient
52// way to embed PEM data and include their delimiters.
53//
54// Outside a block, lines beginning with # are ignored.
55//
56// A test is a sequence of one or more attributes followed by a block or blank
57// line. Blank lines are otherwise ignored. For tests that process multiple
58// kinds of test cases, the first attribute is parsed out as the test's type and
59// parameter. Otherwise, attributes are unordered. The first attribute is also
60// included in the set of attributes, so tests which do not dispatch may ignore
61// this mechanism.
62//
63// Functions in this module freely output to |stderr| on failure. Tests should
64// also do so, and it is recommended they include the corresponding test's line
65// number in any output. |PrintLine| does this automatically.
66//
67// Each attribute in a test must be consumed. When a test completes, if any
68// attributes haven't been processed, the framework reports an error.
69
70
71class FileTest {
72 public:
73 explicit FileTest(const char *path);
74 ~FileTest();
75
76 // is_open returns true if the file was successfully opened.
77 bool is_open() const { return file_ != nullptr; }
78
79 enum ReadResult {
80 kReadSuccess,
81 kReadEOF,
82 kReadError,
83 };
84
85 // ReadNext reads the next test from the file. It returns |kReadSuccess| if
86 // successfully reading a test and |kReadEOF| at the end of the file. On
87 // error or if the previous test had unconsumed attributes, it returns
88 // |kReadError|.
89 ReadResult ReadNext();
90
91 // PrintLine is a variant of printf which prepends the line number and appends
92 // a trailing newline.
Brian Smith061332f2016-01-17 09:30:42 -100093 void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
David Benjamin06b94de2015-05-09 22:46:47 -040094
95 unsigned start_line() const { return start_line_; }
96
97 // GetType returns the name of the first attribute of the current test.
98 const std::string &GetType();
99 // GetParameter returns the value of the first attribute of the current test.
100 const std::string &GetParameter();
101 // GetBlock returns the optional block of the current test, or the empty
102 // if there was no block.
103 const std::string &GetBlock();
104
105 // HasAttribute returns true if the current test has an attribute named |key|.
106 bool HasAttribute(const std::string &key);
107
108 // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
109 // the value and returns true if it exists and returns false with an error to
110 // |stderr| otherwise.
111 bool GetAttribute(std::string *out_value, const std::string &key);
112
David Benjamin5c694e32015-05-11 15:58:08 -0400113 // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
David Benjamin68772b32015-12-30 21:40:40 -0500114 // missing. It should only be used after a |HasAttribute| call.
David Benjamin5c694e32015-05-11 15:58:08 -0400115 const std::string &GetAttributeOrDie(const std::string &key);
116
David Benjamin06b94de2015-05-09 22:46:47 -0400117 // GetBytes looks up the attribute with key |key| and decodes it as a byte
118 // string. On success, it writes the result to |*out| and returns
119 // true. Otherwise it returns false with an error to |stderr|. The value may
120 // be either a hexadecimal string or a quoted ASCII string. It returns true on
121 // success and returns false with an error to |stderr| on failure.
122 bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
123
124 // ExpectBytesEqual returns true if |expected| and |actual| are equal.
125 // Otherwise, it returns false and prints a message to |stderr|.
126 bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
127 const uint8_t *actual, size_t actual_len);
128
129 private:
130 void ClearTest();
131 void OnKeyUsed(const std::string &key);
132
133 FILE *file_ = nullptr;
134 // line_ is the number of lines read.
135 unsigned line_ = 0;
136
137 // start_line_ is the line number of the first attribute of the test.
138 unsigned start_line_ = 0;
139 // type_ is the name of the first attribute of the test.
140 std::string type_;
141 // parameter_ is the value of the first attribute.
142 std::string parameter_;
143 // attributes_ contains all attributes in the test, including the first.
144 std::map<std::string, std::string> attributes_;
145 // block_, if non-empty, is the test's optional trailing block.
146 std::string block_;
147
148 // unused_attributes_ is the set of attributes that have been queried.
149 std::set<std::string> unused_attributes_;
150 // used_block_ is true if the block has been queried.
151 bool used_block_ = false;
152
153 FileTest(const FileTest&) = delete;
154 FileTest &operator=(const FileTest&) = delete;
155};
156
157// FileTestMain runs a file-based test out of |path| and returns an exit code
158// suitable to return out of |main|. |run_test| should return true on pass and
David Benjamin5c694e32015-05-11 15:58:08 -0400159// false on failure. FileTestMain also implements common handling of the 'Error'
160// attribute. A test with that attribute is expected to fail. The value of the
161// attribute is the reason string of the expected OpenSSL error code.
David Benjamin06b94de2015-05-09 22:46:47 -0400162//
163// Tests are guaranteed to run serially and may affect global state if need be.
164// It is legal to use "tests" which, for example, import a private key into a
165// list of keys. This may be used to initialize a shared set of keys for many
166// tests. However, if one test fails, the framework will continue to run
167// subsequent tests.
David Benjamin5c694e32015-05-11 15:58:08 -0400168int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
169 const char *path);
David Benjamin06b94de2015-05-09 22:46:47 -0400170
171
172#endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */