blob: 204ef9cb85a270824cb671b0b07c4fc26ebe8231 [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>
David Benjamin06b94de2015-05-09 22:46:47 -040021
David Benjamina353cdb2016-06-09 16:48:33 -040022OPENSSL_MSVC_PRAGMA(warning(push))
Martin Kreichgauer7c125872017-04-24 13:29:11 -070023OPENSSL_MSVC_PRAGMA(warning(disable : 4702))
Brian Smith906e2992015-07-21 21:46:20 -040024
David Benjamin3ecd0a52017-05-19 15:26:18 -040025#include <functional>
David Benjamin06b94de2015-05-09 22:46:47 -040026#include <map>
David Benjamin1f1eeea2017-05-18 19:39:06 -040027#include <memory>
David Benjamin06b94de2015-05-09 22:46:47 -040028#include <set>
Martin Kreichgauer7c125872017-04-24 13:29:11 -070029#include <string>
David Benjamin06b94de2015-05-09 22:46:47 -040030#include <vector>
31
David Benjamina353cdb2016-06-09 16:48:33 -040032OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin06b94de2015-05-09 22:46:47 -040033
34// File-based test framework.
35//
36// This module provides a file-based test framework. The file format is based on
Martin Kreichgauer7c125872017-04-24 13:29:11 -070037// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. NIST CAVP test
38// vector files are also supported. Each input file is a sequence of attributes,
39// instructions and blank lines.
David Benjamin06b94de2015-05-09 22:46:47 -040040//
41// Each attribute has the form:
42//
43// Name = Value
44//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070045// Instructions are enclosed in square brackets and may appear without a value:
46//
47// [Name = Value]
48//
49// or
50//
51// [Name]
52//
David Benjamin0c292ed2017-04-28 17:41:28 -040053// Commas in instruction lines are treated as separate instructions. Thus this:
54//
55// [Name1,Name2]
56//
57// is the same as:
58//
59// [Name1]
60// [Name2]
61//
David Benjamin06b94de2015-05-09 22:46:47 -040062// Either '=' or ':' may be used to delimit the name from the value. Both the
63// name and value have leading and trailing spaces stripped.
64//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070065// Each file contains a number of instruction blocks and test cases.
David Benjamin06b94de2015-05-09 22:46:47 -040066//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070067// An instruction block is a sequence of instructions followed by a blank line.
68// Instructions apply to all test cases following its appearance, until the next
69// instruction block. Instructions are unordered.
70//
71// A test is a sequence of one or more attributes followed by a blank line. For
72// tests that process multiple kinds of test cases, the first attribute is
73// parsed out as the test's type and parameter. Otherwise, attributes are
74// unordered. The first attribute is also included in the set of attributes, so
75// tests which do not dispatch may ignore this mechanism.
76//
77// Additional blank lines and lines beginning with # are ignored.
David Benjamin06b94de2015-05-09 22:46:47 -040078//
79// Functions in this module freely output to |stderr| on failure. Tests should
80// also do so, and it is recommended they include the corresponding test's line
81// number in any output. |PrintLine| does this automatically.
82//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070083// Each attribute in a test and all instructions applying to it must be
84// consumed. When a test completes, if any attributes or insturctions haven't
85// been processed, the framework reports an error.
David Benjamin06b94de2015-05-09 22:46:47 -040086
Adam Langleyd79bc9d2017-05-30 15:37:27 -070087class FileTest;
88typedef bool (*FileTestFunc)(FileTest *t, void *arg);
David Benjamin06b94de2015-05-09 22:46:47 -040089
90class FileTest {
91 public:
David Benjamin06b94de2015-05-09 22:46:47 -040092 enum ReadResult {
93 kReadSuccess,
94 kReadEOF,
95 kReadError,
96 };
97
David Benjamin1f1eeea2017-05-18 19:39:06 -040098 class LineReader {
99 public:
100 virtual ~LineReader() {}
101 virtual ReadResult ReadLine(char *out, size_t len) = 0;
102 };
103
Adam Langleyd79bc9d2017-05-30 15:37:27 -0700104 struct Options {
105 // path is the path to the input file.
106 const char *path = nullptr;
107 // callback is called for each test. It should get the parameters from this
108 // object and signal any errors by returning false.
109 FileTestFunc callback = nullptr;
110 // arg is an opaque pointer that is passed to |callback|.
111 void *arg = nullptr;
112 // silent suppressed the "PASS" string that is otherwise printed after
113 // successful runs.
114 bool silent = false;
115 // comment_callback is called after each comment in the input is parsed.
116 std::function<void(const std::string&)> comment_callback;
117 };
118
119 explicit FileTest(std::unique_ptr<LineReader> reader,
120 std::function<void(const std::string &)> comment_callback);
David Benjamin1f1eeea2017-05-18 19:39:06 -0400121 ~FileTest();
122
David Benjamin06b94de2015-05-09 22:46:47 -0400123 // ReadNext reads the next test from the file. It returns |kReadSuccess| if
124 // successfully reading a test and |kReadEOF| at the end of the file. On
125 // error or if the previous test had unconsumed attributes, it returns
126 // |kReadError|.
127 ReadResult ReadNext();
128
129 // PrintLine is a variant of printf which prepends the line number and appends
130 // a trailing newline.
Brian Smith061332f2016-01-17 09:30:42 -1000131 void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
David Benjamin06b94de2015-05-09 22:46:47 -0400132
133 unsigned start_line() const { return start_line_; }
134
135 // GetType returns the name of the first attribute of the current test.
136 const std::string &GetType();
137 // GetParameter returns the value of the first attribute of the current test.
138 const std::string &GetParameter();
David Benjamin06b94de2015-05-09 22:46:47 -0400139
140 // HasAttribute returns true if the current test has an attribute named |key|.
141 bool HasAttribute(const std::string &key);
142
143 // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
144 // the value and returns true if it exists and returns false with an error to
145 // |stderr| otherwise.
146 bool GetAttribute(std::string *out_value, const std::string &key);
147
David Benjamin5c694e32015-05-11 15:58:08 -0400148 // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
David Benjamin68772b32015-12-30 21:40:40 -0500149 // missing. It should only be used after a |HasAttribute| call.
David Benjamin5c694e32015-05-11 15:58:08 -0400150 const std::string &GetAttributeOrDie(const std::string &key);
151
David Benjamin06b94de2015-05-09 22:46:47 -0400152 // GetBytes looks up the attribute with key |key| and decodes it as a byte
153 // string. On success, it writes the result to |*out| and returns
154 // true. Otherwise it returns false with an error to |stderr|. The value may
155 // be either a hexadecimal string or a quoted ASCII string. It returns true on
156 // success and returns false with an error to |stderr| on failure.
157 bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
158
159 // ExpectBytesEqual returns true if |expected| and |actual| are equal.
160 // Otherwise, it returns false and prints a message to |stderr|.
161 bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
162 const uint8_t *actual, size_t actual_len);
163
Martin Kreichgauer2b2676f2017-05-01 11:56:43 -0700164 // AtNewInstructionBlock returns true if the current test was immediately
165 // preceded by an instruction block.
166 bool IsAtNewInstructionBlock() const;
167
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700168 // HasInstruction returns true if the current test has an instruction.
169 bool HasInstruction(const std::string &key);
170
171 // GetInstruction looks up the instruction with key |key|. It sets
172 // |*out_value| to the value (empty string if the instruction has no value)
173 // and returns true if it exists and returns false with an error to |stderr|
174 // otherwise.
175 bool GetInstruction(std::string *out_value, const std::string &key);
176
177 // CurrentTestToString returns the file content parsed for the current test.
178 // If the current test was preceded by an instruction block, the return test
179 // case is preceded by the instruction block and a single blank line. All
180 // other blank or comment lines are omitted.
181 const std::string &CurrentTestToString() const;
182
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700183 // InjectInstruction adds a key value pair to the most recently parsed set of
184 // instructions.
185 void InjectInstruction(const std::string &key, const std::string &value);
186
Martin Kreichgauerd977eaa2017-06-26 10:16:50 -0700187 // SkipCurrent passes the current test case. Unused attributes are ignored.
188 void SkipCurrent();
189
David Benjamin06b94de2015-05-09 22:46:47 -0400190 private:
191 void ClearTest();
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700192 void ClearInstructions();
David Benjamin06b94de2015-05-09 22:46:47 -0400193 void OnKeyUsed(const std::string &key);
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700194 void OnInstructionUsed(const std::string &key);
David Benjamin06b94de2015-05-09 22:46:47 -0400195
David Benjamin1f1eeea2017-05-18 19:39:06 -0400196 std::unique_ptr<LineReader> reader_;
David Benjamin06b94de2015-05-09 22:46:47 -0400197 // line_ is the number of lines read.
198 unsigned line_ = 0;
199
200 // start_line_ is the line number of the first attribute of the test.
201 unsigned start_line_ = 0;
202 // type_ is the name of the first attribute of the test.
203 std::string type_;
204 // parameter_ is the value of the first attribute.
205 std::string parameter_;
206 // attributes_ contains all attributes in the test, including the first.
207 std::map<std::string, std::string> attributes_;
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700208 // instructions_ contains all instructions in scope for the test.
209 std::map<std::string, std::string> instructions_;
David Benjamin06b94de2015-05-09 22:46:47 -0400210
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700211 // unused_attributes_ is the set of attributes that have not been queried.
David Benjamin06b94de2015-05-09 22:46:47 -0400212 std::set<std::string> unused_attributes_;
David Benjamin06b94de2015-05-09 22:46:47 -0400213
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700214 // unused_instructions_ is the set of instructions that have not been queried.
215 std::set<std::string> unused_instructions_;
216
217 std::string current_test_;
218
Martin Kreichgauer2b2676f2017-05-01 11:56:43 -0700219 bool is_at_new_instruction_block_ = false;
220
Adam Langleyd79bc9d2017-05-30 15:37:27 -0700221 // comment_callback_, if set, is a callback function that is called with the
222 // contents of each comment as they are parsed.
223 std::function<void(const std::string&)> comment_callback_;
224
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700225 FileTest(const FileTest &) = delete;
226 FileTest &operator=(const FileTest &) = delete;
David Benjamin06b94de2015-05-09 22:46:47 -0400227};
228
229// FileTestMain runs a file-based test out of |path| and returns an exit code
230// suitable to return out of |main|. |run_test| should return true on pass and
David Benjamin5c694e32015-05-11 15:58:08 -0400231// false on failure. FileTestMain also implements common handling of the 'Error'
232// attribute. A test with that attribute is expected to fail. The value of the
233// attribute is the reason string of the expected OpenSSL error code.
David Benjamin06b94de2015-05-09 22:46:47 -0400234//
235// Tests are guaranteed to run serially and may affect global state if need be.
236// It is legal to use "tests" which, for example, import a private key into a
237// list of keys. This may be used to initialize a shared set of keys for many
238// tests. However, if one test fails, the framework will continue to run
239// subsequent tests.
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700240int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
David Benjamin06b94de2015-05-09 22:46:47 -0400241
Adam Langleyd79bc9d2017-05-30 15:37:27 -0700242// FileTestMain accepts a larger number of options via a struct.
243int FileTestMain(const FileTest::Options &opts);
David Benjamin06b94de2015-05-09 22:46:47 -0400244
David Benjamin3ecd0a52017-05-19 15:26:18 -0400245// FileTestGTest behaves like FileTestMain, but for GTest. |path| must be the
246// name of a test file embedded in the test binary.
247void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test);
248
David Benjamin808f8322017-08-18 14:06:02 -0400249#endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H