| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#ifndef OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H |
| 5 |
#define OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H |
| 6 |
|
| 7 |
#include <openssl/base.h> |
| 8 |
|
| 9 |
#include <stdint.h> |
| 10 |
|
| 11 |
OPENSSL_MSVC_PRAGMA(warning(push)) |
| 12 |
OPENSSL_MSVC_PRAGMA(warning(disable : 4702)) |
| 13 |
|
| 14 |
#include <functional> |
| 15 |
#include <map> |
| 16 |
#include <memory> |
| 17 |
#include <set> |
| 18 |
#include <string> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 22 |
|
| 23 |
// File-based test framework. |
| 24 |
// |
| 25 |
// This module provides a file-based test framework. The file format is based on |
| 26 |
// that of OpenSSL upstream's evp_test and BoringSSL's aead_test. NIST CAVP test |
| 27 |
// vector files are also supported. Each input file is a sequence of attributes, |
| 28 |
// instructions and blank lines. |
| 29 |
// |
| 30 |
// Each attribute has the form: |
| 31 |
// |
| 32 |
// Name = Value |
| 33 |
// |
| 34 |
// Instructions are enclosed in square brackets and may appear without a value: |
| 35 |
// |
| 36 |
// [Name = Value] |
| 37 |
// |
| 38 |
// or |
| 39 |
// |
| 40 |
// [Name] |
| 41 |
// |
| 42 |
// Commas in instruction lines are treated as separate instructions. Thus this: |
| 43 |
// |
| 44 |
// [Name1,Name2] |
| 45 |
// |
| 46 |
// is the same as: |
| 47 |
// |
| 48 |
// [Name1] |
| 49 |
// [Name2] |
| 50 |
// |
| 51 |
// Either '=' or ':' may be used to delimit the name from the value. Both the |
| 52 |
// name and value have leading and trailing spaces stripped. |
| 53 |
// |
| 54 |
// Each file contains a number of instruction blocks and test cases. |
| 55 |
// |
| 56 |
// An instruction block is a sequence of instructions followed by a blank line. |
| 57 |
// Instructions apply to all test cases following its appearance, until the next |
| 58 |
// instruction block. Instructions are unordered. |
| 59 |
// |
| 60 |
// A test is a sequence of one or more attributes followed by a blank line. For |
| 61 |
// tests that process multiple kinds of test cases, the first attribute is |
| 62 |
// parsed out as the test's type and parameter. Otherwise, attributes are |
| 63 |
// unordered. The first attribute is also included in the set of attributes, so |
| 64 |
// tests which do not dispatch may ignore this mechanism. |
| 65 |
// |
| 66 |
// Additional blank lines and lines beginning with # are ignored. |
| 67 |
// |
| 68 |
// Functions in this module freely output to |stderr| on failure. Tests should |
| 69 |
// also do so, and it is recommended they include the corresponding test's line |
| 70 |
// number in any output. |PrintLine| does this automatically. |
| 71 |
// |
| 72 |
// Each attribute in a test and all instructions applying to it must be |
| 73 |
// consumed. When a test completes, if any attributes or insturctions haven't |
| 74 |
// been processed, the framework reports an error. |
| 75 |
|
| 76 |
class FileTest; |
| 77 |
typedef bool (*FileTestFunc)(FileTest *t, void *arg); |
| 78 |
|
| 79 |
class FileTest { |
| 80 |
public: |
| 81 |
enum ReadResult { |
| 82 |
kReadSuccess, |
| 83 |
kReadEOF, |
| 84 |
kReadError, |
| 85 |
}; |
| 86 |
|
| 87 |
class LineReader { |
| 88 |
public: |
| 89 |
virtual ~LineReader() {} |
| 90 |
virtual ReadResult ReadLine(char *out, size_t len) = 0; |
| 91 |
}; |
| 92 |
|
| 93 |
struct Options { |
| 94 |
// path is the path to the input file. |
| 95 |
const char *path = nullptr; |
| 96 |
// callback is called for each test. It should get the parameters from this |
| 97 |
// object and signal any errors by returning false. |
| 98 |
FileTestFunc callback = nullptr; |
| 99 |
// arg is an opaque pointer that is passed to |callback|. |
| 100 |
void *arg = nullptr; |
| 101 |
// silent suppressed the "PASS" string that is otherwise printed after |
| 102 |
// successful runs. |
| 103 |
bool silent = false; |
| 104 |
// comment_callback is called after each comment in the input is parsed. |
| 105 |
std::function<void(const std::string&)> comment_callback; |
| 106 |
// is_kas_test is true if a NIST “KAS” test is being parsed. These tests |
| 107 |
// are inconsistent with the other NIST files to such a degree that they |
| 108 |
// need their own boolean. |
| 109 |
bool is_kas_test = false; |
| 110 |
}; |
| 111 |
|
| 112 |
explicit FileTest(std::unique_ptr<LineReader> reader, |
| 113 |
std::function<void(const std::string &)> comment_callback, |
| 114 |
bool is_kas_test); |
| 115 |
~FileTest(); |
| 116 |
|
| 117 |
// ReadNext reads the next test from the file. It returns |kReadSuccess| if |
| 118 |
// successfully reading a test and |kReadEOF| at the end of the file. On |
| 119 |
// error or if the previous test had unconsumed attributes, it returns |
| 120 |
// |kReadError|. |
| 121 |
ReadResult ReadNext(); |
| 122 |
|
| 123 |
// PrintLine is a variant of printf which prepends the line number and appends |
| 124 |
// a trailing newline. |
| 125 |
void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3); |
| 126 |
|
| 127 |
unsigned start_line() const { return start_line_; } |
| 128 |
|
| 129 |
// GetType returns the name of the first attribute of the current test. |
| 130 |
const std::string &GetType(); |
| 131 |
// GetParameter returns the value of the first attribute of the current test. |
| 132 |
const std::string &GetParameter(); |
| 133 |
|
| 134 |
// HasAttribute returns true if the current test has an attribute named |key|. |
| 135 |
bool HasAttribute(const std::string &key); |
| 136 |
|
| 137 |
// GetAttribute looks up the attribute with key |key|. It sets |*out_value| to |
| 138 |
// the value and returns true if it exists and returns false with an error to |
| 139 |
// |stderr| otherwise. |
| 140 |
bool GetAttribute(std::string *out_value, const std::string &key); |
| 141 |
|
| 142 |
// GetAttributeOrDie looks up the attribute with key |key| and aborts if it is |
| 143 |
// missing. It should only be used after a |HasAttribute| call. |
| 144 |
const std::string &GetAttributeOrDie(const std::string &key); |
| 145 |
|
| 146 |
// IgnoreAttribute marks the attribute with key |key| as used. |
| 147 |
void IgnoreAttribute(const std::string &key) { HasAttribute(key); } |
| 148 |
|
| 149 |
// GetBytes looks up the attribute with key |key| and decodes it as a byte |
| 150 |
// string. On success, it writes the result to |*out| and returns |
| 151 |
// true. Otherwise it returns false with an error to |stderr|. The value may |
| 152 |
// be either a hexadecimal string or a quoted ASCII string. It returns true on |
| 153 |
// success and returns false with an error to |stderr| on failure. |
| 154 |
bool GetBytes(std::vector<uint8_t> *out, const std::string &key); |
| 155 |
|
| 156 |
// AtNewInstructionBlock returns true if the current test was immediately |
| 157 |
// preceded by an instruction block. |
| 158 |
bool IsAtNewInstructionBlock() const; |
| 159 |
|
| 160 |
// HasInstruction returns true if the current test has an instruction. |
| 161 |
bool HasInstruction(const std::string &key); |
| 162 |
|
| 163 |
// IgnoreInstruction marks the instruction with key |key| as used. |
| 164 |
void IgnoreInstruction(const std::string &key) { HasInstruction(key); } |
| 165 |
|
| 166 |
// IgnoreAllUnusedInstructions disables checking for unused instructions. |
| 167 |
void IgnoreAllUnusedInstructions(); |
| 168 |
|
| 169 |
// GetInstruction looks up the instruction with key |key|. It sets |
| 170 |
// |*out_value| to the value (empty string if the instruction has no value) |
| 171 |
// and returns true if it exists and returns false with an error to |stderr| |
| 172 |
// otherwise. |
| 173 |
bool GetInstruction(std::string *out_value, const std::string &key); |
| 174 |
|
| 175 |
// GetInstructionOrDie looks up the instruction with key |key| and aborts if |
| 176 |
// it is missing. It should only be used after a |HasInstruction| call. |
| 177 |
const std::string &GetInstructionOrDie(const std::string &key); |
| 178 |
|
| 179 |
// GetInstructionBytes behaves like GetBytes, but looks up the corresponding |
| 180 |
// instruction. |
| 181 |
bool GetInstructionBytes(std::vector<uint8_t> *out, const std::string &key); |
| 182 |
|
| 183 |
// CurrentTestToString returns the file content parsed for the current test. |
| 184 |
// If the current test was preceded by an instruction block, the return test |
| 185 |
// case is preceded by the instruction block and a single blank line. All |
| 186 |
// other blank or comment lines are omitted. |
| 187 |
const std::string &CurrentTestToString() const; |
| 188 |
|
| 189 |
// InjectInstruction adds a key value pair to the most recently parsed set of |
| 190 |
// instructions. |
| 191 |
void InjectInstruction(const std::string &key, const std::string &value); |
| 192 |
|
| 193 |
// SkipCurrent passes the current test case. Unused attributes are ignored. |
| 194 |
void SkipCurrent(); |
| 195 |
|
| 196 |
private: |
| 197 |
void ClearTest(); |
| 198 |
void ClearInstructions(); |
| 199 |
void OnKeyUsed(const std::string &key); |
| 200 |
void OnInstructionUsed(const std::string &key); |
| 201 |
bool ConvertToBytes(std::vector<uint8_t> *out, const std::string &value); |
| 202 |
|
| 203 |
std::unique_ptr<LineReader> reader_; |
| 204 |
// line_ is the number of lines read. |
| 205 |
unsigned line_ = 0; |
| 206 |
|
| 207 |
// start_line_ is the line number of the first attribute of the test. |
| 208 |
unsigned start_line_ = 0; |
| 209 |
// type_ is the name of the first attribute of the test. |
| 210 |
std::string type_; |
| 211 |
// parameter_ is the value of the first attribute. |
| 212 |
std::string parameter_; |
| 213 |
// attribute_count_ maps unsuffixed attribute names to the number of times |
| 214 |
// they have occurred so far. |
| 215 |
std::map<std::string, size_t> attribute_count_; |
| 216 |
// attributes_ contains all attributes in the test, including the first. |
| 217 |
std::map<std::string, std::string> attributes_; |
| 218 |
// instructions_ contains all instructions in scope for the test. |
| 219 |
std::map<std::string, std::string> instructions_; |
| 220 |
|
| 221 |
// unused_attributes_ is the set of attributes that have not been queried. |
| 222 |
std::set<std::string> unused_attributes_; |
| 223 |
|
| 224 |
// unused_instructions_ is the set of instructions that have not been queried. |
| 225 |
std::set<std::string> unused_instructions_; |
| 226 |
|
| 227 |
std::string current_test_; |
| 228 |
|
| 229 |
bool is_at_new_instruction_block_ = false; |
| 230 |
bool seen_non_comment_ = false; |
| 231 |
bool is_kas_test_ = false; |
| 232 |
|
| 233 |
// comment_callback_, if set, is a callback function that is called with the |
| 234 |
// contents of each comment as they are parsed. |
| 235 |
std::function<void(const std::string&)> comment_callback_; |
| 236 |
|
| 237 |
FileTest(const FileTest &) = delete; |
| 238 |
FileTest &operator=(const FileTest &) = delete; |
| 239 |
}; |
| 240 |
|
| 241 |
// FileTestMain runs a file-based test out of |path| and returns an exit code |
| 242 |
// suitable to return out of |main|. |run_test| should return true on pass and |
| 243 |
// false on failure. FileTestMain also implements common handling of the 'Error' |
| 244 |
// attribute. A test with that attribute is expected to fail. The value of the |
| 245 |
// attribute is the reason string of the expected OpenSSL error code. |
| 246 |
// |
| 247 |
// Tests are guaranteed to run serially and may affect global state if need be. |
| 248 |
// It is legal to use "tests" which, for example, import a private key into a |
| 249 |
// list of keys. This may be used to initialize a shared set of keys for many |
| 250 |
// tests. However, if one test fails, the framework will continue to run |
| 251 |
// subsequent tests. |
| 252 |
int FileTestMain(FileTestFunc run_test, void *arg, const char *path); |
| 253 |
|
| 254 |
// FileTestMain accepts a larger number of options via a struct. |
| 255 |
int FileTestMain(const FileTest::Options &opts); |
| 256 |
|
| 257 |
// FileTestGTest behaves like FileTestMain, but for GTest. |path| must be the |
| 258 |
// name of a test file embedded in the test binary. |
| 259 |
void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test); |
| 260 |
|
| 261 |
#endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H |