blob: 17213d963d7cb7fca8c76dbea9aec031fcfab5b8 [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
David Benjamina353cdb2016-06-09 16:48:33 -040023OPENSSL_MSVC_PRAGMA(warning(push))
Martin Kreichgauer7c125872017-04-24 13:29:11 -070024OPENSSL_MSVC_PRAGMA(warning(disable : 4702))
Brian Smith906e2992015-07-21 21:46:20 -040025
David Benjamin06b94de2015-05-09 22:46:47 -040026#include <map>
27#include <set>
Martin Kreichgauer7c125872017-04-24 13:29:11 -070028#include <string>
David Benjamin06b94de2015-05-09 22:46:47 -040029#include <vector>
30
David Benjamina353cdb2016-06-09 16:48:33 -040031OPENSSL_MSVC_PRAGMA(warning(pop))
David Benjamin06b94de2015-05-09 22:46:47 -040032
33// File-based test framework.
34//
35// This module provides a file-based test framework. The file format is based on
Martin Kreichgauer7c125872017-04-24 13:29:11 -070036// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. NIST CAVP test
37// vector files are also supported. Each input file is a sequence of attributes,
38// instructions and blank lines.
David Benjamin06b94de2015-05-09 22:46:47 -040039//
40// Each attribute has the form:
41//
42// Name = Value
43//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070044// Instructions are enclosed in square brackets and may appear without a value:
45//
46// [Name = Value]
47//
48// or
49//
50// [Name]
51//
David Benjamin0c292ed2017-04-28 17:41:28 -040052// Commas in instruction lines are treated as separate instructions. Thus this:
53//
54// [Name1,Name2]
55//
56// is the same as:
57//
58// [Name1]
59// [Name2]
60//
David Benjamin06b94de2015-05-09 22:46:47 -040061// Either '=' or ':' may be used to delimit the name from the value. Both the
62// name and value have leading and trailing spaces stripped.
63//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070064// Each file contains a number of instruction blocks and test cases.
David Benjamin06b94de2015-05-09 22:46:47 -040065//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070066// An instruction block is a sequence of instructions followed by a blank line.
67// Instructions apply to all test cases following its appearance, until the next
68// instruction block. Instructions are unordered.
69//
70// A test is a sequence of one or more attributes followed by a blank line. For
71// tests that process multiple kinds of test cases, the first attribute is
72// parsed out as the test's type and parameter. Otherwise, attributes are
73// unordered. The first attribute is also included in the set of attributes, so
74// tests which do not dispatch may ignore this mechanism.
75//
76// Additional blank lines and lines beginning with # are ignored.
David Benjamin06b94de2015-05-09 22:46:47 -040077//
78// Functions in this module freely output to |stderr| on failure. Tests should
79// also do so, and it is recommended they include the corresponding test's line
80// number in any output. |PrintLine| does this automatically.
81//
Martin Kreichgauer7c125872017-04-24 13:29:11 -070082// Each attribute in a test and all instructions applying to it must be
83// consumed. When a test completes, if any attributes or insturctions haven't
84// been processed, the framework reports an error.
David Benjamin06b94de2015-05-09 22:46:47 -040085
86
87class FileTest {
88 public:
89 explicit FileTest(const char *path);
90 ~FileTest();
91
92 // is_open returns true if the file was successfully opened.
93 bool is_open() const { return file_ != nullptr; }
94
95 enum ReadResult {
96 kReadSuccess,
97 kReadEOF,
98 kReadError,
99 };
100
101 // ReadNext reads the next test from the file. It returns |kReadSuccess| if
102 // successfully reading a test and |kReadEOF| at the end of the file. On
103 // error or if the previous test had unconsumed attributes, it returns
104 // |kReadError|.
105 ReadResult ReadNext();
106
107 // PrintLine is a variant of printf which prepends the line number and appends
108 // a trailing newline.
Brian Smith061332f2016-01-17 09:30:42 -1000109 void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
David Benjamin06b94de2015-05-09 22:46:47 -0400110
111 unsigned start_line() const { return start_line_; }
112
113 // GetType returns the name of the first attribute of the current test.
114 const std::string &GetType();
115 // GetParameter returns the value of the first attribute of the current test.
116 const std::string &GetParameter();
David Benjamin06b94de2015-05-09 22:46:47 -0400117
118 // HasAttribute returns true if the current test has an attribute named |key|.
119 bool HasAttribute(const std::string &key);
120
121 // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
122 // the value and returns true if it exists and returns false with an error to
123 // |stderr| otherwise.
124 bool GetAttribute(std::string *out_value, const std::string &key);
125
David Benjamin5c694e32015-05-11 15:58:08 -0400126 // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
David Benjamin68772b32015-12-30 21:40:40 -0500127 // missing. It should only be used after a |HasAttribute| call.
David Benjamin5c694e32015-05-11 15:58:08 -0400128 const std::string &GetAttributeOrDie(const std::string &key);
129
David Benjamin06b94de2015-05-09 22:46:47 -0400130 // GetBytes looks up the attribute with key |key| and decodes it as a byte
131 // string. On success, it writes the result to |*out| and returns
132 // true. Otherwise it returns false with an error to |stderr|. The value may
133 // be either a hexadecimal string or a quoted ASCII string. It returns true on
134 // success and returns false with an error to |stderr| on failure.
135 bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
136
137 // ExpectBytesEqual returns true if |expected| and |actual| are equal.
138 // Otherwise, it returns false and prints a message to |stderr|.
139 bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
140 const uint8_t *actual, size_t actual_len);
141
Martin Kreichgauer2b2676f2017-05-01 11:56:43 -0700142 // AtNewInstructionBlock returns true if the current test was immediately
143 // preceded by an instruction block.
144 bool IsAtNewInstructionBlock() const;
145
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700146 // HasInstruction returns true if the current test has an instruction.
147 bool HasInstruction(const std::string &key);
148
149 // GetInstruction looks up the instruction with key |key|. It sets
150 // |*out_value| to the value (empty string if the instruction has no value)
151 // and returns true if it exists and returns false with an error to |stderr|
152 // otherwise.
153 bool GetInstruction(std::string *out_value, const std::string &key);
154
155 // CurrentTestToString returns the file content parsed for the current test.
156 // If the current test was preceded by an instruction block, the return test
157 // case is preceded by the instruction block and a single blank line. All
158 // other blank or comment lines are omitted.
159 const std::string &CurrentTestToString() const;
160
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700161 // InjectInstruction adds a key value pair to the most recently parsed set of
162 // instructions.
163 void InjectInstruction(const std::string &key, const std::string &value);
164
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700165 void SetIgnoreUnusedAttributes(bool ignore);
166
David Benjamin06b94de2015-05-09 22:46:47 -0400167 private:
168 void ClearTest();
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700169 void ClearInstructions();
David Benjamin06b94de2015-05-09 22:46:47 -0400170 void OnKeyUsed(const std::string &key);
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700171 void OnInstructionUsed(const std::string &key);
David Benjamin06b94de2015-05-09 22:46:47 -0400172
173 FILE *file_ = nullptr;
174 // line_ is the number of lines read.
175 unsigned line_ = 0;
176
177 // start_line_ is the line number of the first attribute of the test.
178 unsigned start_line_ = 0;
179 // type_ is the name of the first attribute of the test.
180 std::string type_;
181 // parameter_ is the value of the first attribute.
182 std::string parameter_;
183 // attributes_ contains all attributes in the test, including the first.
184 std::map<std::string, std::string> attributes_;
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700185 // instructions_ contains all instructions in scope for the test.
186 std::map<std::string, std::string> instructions_;
David Benjamin06b94de2015-05-09 22:46:47 -0400187
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700188 // unused_attributes_ is the set of attributes that have not been queried.
David Benjamin06b94de2015-05-09 22:46:47 -0400189 std::set<std::string> unused_attributes_;
David Benjamin06b94de2015-05-09 22:46:47 -0400190
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700191 // unused_instructions_ is the set of instructions that have not been queried.
192 std::set<std::string> unused_instructions_;
193
194 std::string current_test_;
195
Martin Kreichgauer2b2676f2017-05-01 11:56:43 -0700196 bool is_at_new_instruction_block_ = false;
197
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700198 bool ignore_unused_attributes_ = false;
199
200 FileTest(const FileTest &) = delete;
201 FileTest &operator=(const FileTest &) = delete;
David Benjamin06b94de2015-05-09 22:46:47 -0400202};
203
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700204typedef bool (*FileTestFunc)(FileTest *t, void *arg);
205
David Benjamin06b94de2015-05-09 22:46:47 -0400206// FileTestMain runs a file-based test out of |path| and returns an exit code
207// suitable to return out of |main|. |run_test| should return true on pass and
David Benjamin5c694e32015-05-11 15:58:08 -0400208// false on failure. FileTestMain also implements common handling of the 'Error'
209// attribute. A test with that attribute is expected to fail. The value of the
210// attribute is the reason string of the expected OpenSSL error code.
David Benjamin06b94de2015-05-09 22:46:47 -0400211//
212// Tests are guaranteed to run serially and may affect global state if need be.
213// It is legal to use "tests" which, for example, import a private key into a
214// list of keys. This may be used to initialize a shared set of keys for many
215// tests. However, if one test fails, the framework will continue to run
216// subsequent tests.
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700217int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
David Benjamin06b94de2015-05-09 22:46:47 -0400218
Martin Kreichgauer7c125872017-04-24 13:29:11 -0700219// FileTestMainSilent behaves like FileTestMain but does not print a final
220// FAIL/PASS message to stdout.
Martin Kreichgauer6dd055d2017-05-01 15:31:43 -0700221int FileTestMainSilent(FileTestFunc run_test, void *arg, const char *path);
David Benjamin06b94de2015-05-09 22:46:47 -0400222
223#endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */