file_test.h
Go to the documentation of this file.
1 /* 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 
18 #include <openssl/base.h>
19 
20 #include <stdint.h>
21 
23 OPENSSL_MSVC_PRAGMA(warning(disable : 4702))
24 
25 #include <functional>
26 #include <map>
27 #include <memory>
28 #include <set>
29 #include <string>
30 #include <vector>
31 
33 
34 // File-based test framework.
35 //
36 // This module provides a file-based test framework. The file format is based on
37 // 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.
40 //
41 // Each attribute has the form:
42 //
43 // Name = Value
44 //
45 // Instructions are enclosed in square brackets and may appear without a value:
46 //
47 // [Name = Value]
48 //
49 // or
50 //
51 // [Name]
52 //
53 // 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 //
62 // 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 //
65 // Each file contains a number of instruction blocks and test cases.
66 //
67 // 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.
78 //
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 //
83 // 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.
86 
87 class FileTest;
88 typedef bool (*FileTestFunc)(FileTest *t, void *arg);
89 
90 class FileTest {
91  public:
92  enum ReadResult {
96  };
97 
98  class LineReader {
99  public:
100  virtual ~LineReader() {}
101  virtual ReadResult ReadLine(char *out, size_t len) = 0;
102  };
103 
104  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.
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.
117  // is_kas_test is true if a NIST “KAS” test is being parsed. These tests
118  // are inconsistent with the other NIST files to such a degree that they
119  // need their own boolean.
120  bool is_kas_test = false;
121  };
122 
123  explicit FileTest(std::unique_ptr<LineReader> reader,
124  std::function<void(const std::string &)> comment_callback,
125  bool is_kas_test);
126  ~FileTest();
127 
128  // ReadNext reads the next test from the file. It returns |kReadSuccess| if
129  // successfully reading a test and |kReadEOF| at the end of the file. On
130  // error or if the previous test had unconsumed attributes, it returns
131  // |kReadError|.
133 
134  // PrintLine is a variant of printf which prepends the line number and appends
135  // a trailing newline.
136  void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
137 
138  unsigned start_line() const { return start_line_; }
139 
140  // GetType returns the name of the first attribute of the current test.
141  const std::string &GetType();
142  // GetParameter returns the value of the first attribute of the current test.
143  const std::string &GetParameter();
144 
145  // HasAttribute returns true if the current test has an attribute named |key|.
146  bool HasAttribute(const std::string &key);
147 
148  // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
149  // the value and returns true if it exists and returns false with an error to
150  // |stderr| otherwise.
151  bool GetAttribute(std::string *out_value, const std::string &key);
152 
153  // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
154  // missing. It should only be used after a |HasAttribute| call.
156 
157  // IgnoreAttribute marks the attribute with key |key| as used.
159 
160  // GetBytes looks up the attribute with key |key| and decodes it as a byte
161  // string. On success, it writes the result to |*out| and returns
162  // true. Otherwise it returns false with an error to |stderr|. The value may
163  // be either a hexadecimal string or a quoted ASCII string. It returns true on
164  // success and returns false with an error to |stderr| on failure.
165  bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
166 
167  // AtNewInstructionBlock returns true if the current test was immediately
168  // preceded by an instruction block.
169  bool IsAtNewInstructionBlock() const;
170 
171  // HasInstruction returns true if the current test has an instruction.
172  bool HasInstruction(const std::string &key);
173 
174  // IgnoreInstruction marks the instruction with key |key| as used.
176 
177  // IgnoreAllUnusedInstructions disables checking for unused instructions.
179 
180  // GetInstruction looks up the instruction with key |key|. It sets
181  // |*out_value| to the value (empty string if the instruction has no value)
182  // and returns true if it exists and returns false with an error to |stderr|
183  // otherwise.
184  bool GetInstruction(std::string *out_value, const std::string &key);
185 
186  // GetInstructionOrDie looks up the instruction with key |key| and aborts if
187  // it is missing. It should only be used after a |HasInstruction| call.
189 
190  // GetInstructionBytes behaves like GetBytes, but looks up the corresponding
191  // instruction.
192  bool GetInstructionBytes(std::vector<uint8_t> *out, const std::string &key);
193 
194  // CurrentTestToString returns the file content parsed for the current test.
195  // If the current test was preceded by an instruction block, the return test
196  // case is preceded by the instruction block and a single blank line. All
197  // other blank or comment lines are omitted.
198  const std::string &CurrentTestToString() const;
199 
200  // InjectInstruction adds a key value pair to the most recently parsed set of
201  // instructions.
202  void InjectInstruction(const std::string &key, const std::string &value);
203 
204  // SkipCurrent passes the current test case. Unused attributes are ignored.
205  void SkipCurrent();
206 
207  private:
208  void ClearTest();
209  void ClearInstructions();
210  void OnKeyUsed(const std::string &key);
211  void OnInstructionUsed(const std::string &key);
212  bool ConvertToBytes(std::vector<uint8_t> *out, const std::string &value);
213 
214  std::unique_ptr<LineReader> reader_;
215  // line_ is the number of lines read.
216  unsigned line_ = 0;
217 
218  // start_line_ is the line number of the first attribute of the test.
219  unsigned start_line_ = 0;
220  // type_ is the name of the first attribute of the test.
222  // parameter_ is the value of the first attribute.
224  // attribute_count_ maps unsuffixed attribute names to the number of times
225  // they have occurred so far.
226  std::map<std::string, size_t> attribute_count_;
227  // attributes_ contains all attributes in the test, including the first.
228  std::map<std::string, std::string> attributes_;
229  // instructions_ contains all instructions in scope for the test.
230  std::map<std::string, std::string> instructions_;
231 
232  // unused_attributes_ is the set of attributes that have not been queried.
233  std::set<std::string> unused_attributes_;
234 
235  // unused_instructions_ is the set of instructions that have not been queried.
236  std::set<std::string> unused_instructions_;
237 
239 
241  bool seen_non_comment_ = false;
242  bool is_kas_test_ = false;
243 
244  // comment_callback_, if set, is a callback function that is called with the
245  // contents of each comment as they are parsed.
247 
248  FileTest(const FileTest &) = delete;
249  FileTest &operator=(const FileTest &) = delete;
250 };
251 
252 // FileTestMain runs a file-based test out of |path| and returns an exit code
253 // suitable to return out of |main|. |run_test| should return true on pass and
254 // false on failure. FileTestMain also implements common handling of the 'Error'
255 // attribute. A test with that attribute is expected to fail. The value of the
256 // attribute is the reason string of the expected OpenSSL error code.
257 //
258 // Tests are guaranteed to run serially and may affect global state if need be.
259 // It is legal to use "tests" which, for example, import a private key into a
260 // list of keys. This may be used to initialize a shared set of keys for many
261 // tests. However, if one test fails, the framework will continue to run
262 // subsequent tests.
263 int FileTestMain(FileTestFunc run_test, void *arg, const char *path);
264 
265 // FileTestMain accepts a larger number of options via a struct.
267 
268 // FileTestGTest behaves like FileTestMain, but for GTest. |path| must be the
269 // name of a test file embedded in the test binary.
270 void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test);
271 
272 #endif // OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H
FileTest::GetBytes
bool GetBytes(std::vector< uint8_t > *out, const std::string &key)
Definition: file_test.cc:310
FileTest::IgnoreAttribute
void IgnoreAttribute(const std::string &key)
Definition: file_test.h:158
FileTest::attribute_count_
std::map< std::string, size_t > attribute_count_
Definition: file_test.h:226
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
http2_test_server.format
format
Definition: http2_test_server.py:118
FileTest::line_
unsigned line_
Definition: file_test.h:216
bool
bool
Definition: setup_once.h:312
FileTest::GetParameter
const std::string & GetParameter()
Definition: file_test.cc:245
FileTest::ReadNext
ReadResult ReadNext()
Definition: file_test.cc:81
FileTest::reader_
std::unique_ptr< LineReader > reader_
Definition: file_test.h:214
FileTest::instructions_
std::map< std::string, std::string > instructions_
Definition: file_test.h:230
FileTest::IsAtNewInstructionBlock
bool IsAtNewInstructionBlock() const
Definition: file_test.cc:353
FileTest::LineReader::~LineReader
virtual ~LineReader()
Definition: file_test.h:100
FileTest::GetInstruction
bool GetInstruction(std::string *out_value, const std::string &key)
Definition: file_test.cc:278
check_version.warning
string warning
Definition: check_version.py:46
FileTest::InjectInstruction
void InjectInstruction(const std::string &key, const std::string &value)
Definition: file_test.cc:357
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
FileTest::comment_callback_
std::function< void(const std::string &)> comment_callback_
Definition: file_test.h:246
FileTest::operator=
FileTest & operator=(const FileTest &)=delete
FileTest::LineReader::ReadLine
virtual ReadResult ReadLine(char *out, size_t len)=0
cstest_report.opts
opts
Definition: cstest_report.py:81
FileTest::is_at_new_instruction_block_
bool is_at_new_instruction_block_
Definition: file_test.h:240
check_documentation.path
path
Definition: check_documentation.py:57
FileTest::unused_attributes_
std::set< std::string > unused_attributes_
Definition: file_test.h:233
base.h
FileTest::HasInstruction
bool HasInstruction(const std::string &key)
Definition: file_test.cc:273
FileTest::ReadResult
ReadResult
Definition: file_test.h:92
FileTestGTest
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
Definition: file_test_gtest.cc:68
FileTest::Options::comment_callback
std::function< void(const std::string &)> comment_callback
Definition: file_test.h:116
FileTest::type_
std::string type_
Definition: file_test.h:221
FileTest::GetInstructionOrDie
const std::string & GetInstructionOrDie(const std::string &key)
Definition: file_test.cc:293
FileTest::kReadSuccess
@ kReadSuccess
Definition: file_test.h:93
FileTestFunc
bool(* FileTestFunc)(FileTest *t, void *arg)
Definition: file_test.h:88
FileTest::GetAttributeOrDie
const std::string & GetAttributeOrDie(const std::string &key)
Definition: file_test.cc:266
FileTest::~FileTest
~FileTest()
Definition: file_test.cc:41
FileTest::LineReader
Definition: file_test.h:98
FileTest::Options::is_kas_test
bool is_kas_test
Definition: file_test.h:120
FileTest::start_line
void unsigned start_line() const
Definition: file_test.h:138
FileTest::Options::silent
bool silent
Definition: file_test.h:114
FileTest
Definition: file_test.h:90
FileTest::start_line_
unsigned start_line_
Definition: file_test.h:219
FileTest::Options::path
const char * path
Definition: file_test.h:106
arg
Definition: cmdline.cc:40
FileTestMain
int FileTestMain(FileTestFunc run_test, void *arg, const char *path)
Definition: file_test.cc:399
OPENSSL_MSVC_PRAGMA
OPENSSL_MSVC_PRAGMA(warning(disable:4702))
Definition: e_aes.c:69
FileTest::ClearInstructions
void ClearInstructions()
Definition: file_test.cc:326
FileTest::current_test_
std::string current_test_
Definition: file_test.h:238
FileTest::SkipCurrent
void SkipCurrent()
Definition: file_test.cc:462
FileTest::is_kas_test_
bool is_kas_test_
Definition: file_test.h:242
FileTest::GetInstructionBytes
bool GetInstructionBytes(std::vector< uint8_t > *out, const std::string &key)
Definition: file_test.cc:300
stdint.h
FileTest::GetType
const std::string & GetType()
Definition: file_test.cc:240
FileTest::seen_non_comment_
bool seen_non_comment_
Definition: file_test.h:241
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
FileTest::Options::callback
FileTestFunc callback
Definition: file_test.h:109
value
const char * value
Definition: hpack_parser_table.cc:165
FileTest::OnKeyUsed
void OnKeyUsed(const std::string &key)
Definition: file_test.cc:331
FileTest::GetAttribute
bool GetAttribute(std::string *out_value, const std::string &key)
Definition: file_test.cc:255
key
const char * key
Definition: hpack_parser_table.cc:164
FileTest::attributes_
std::map< std::string, std::string > attributes_
Definition: file_test.h:228
FileTest::PrintLine
void PrintLine(const char *format,...) OPENSSL_PRINTF_FORMAT_FUNC(2
Definition: file_test.cc:229
FileTest::unused_instructions_
std::set< std::string > unused_instructions_
Definition: file_test.h:236
arg
struct arg arg
FileTest::Options
Definition: file_test.h:104
tests.stress.client.run_test
def run_test(args)
Definition: src/python/grpcio_tests/tests/stress/client.py:116
FileTest::parameter_
std::string parameter_
Definition: file_test.h:223
FileTest::HasAttribute
bool HasAttribute(const std::string &key)
Definition: file_test.cc:250
FileTest::IgnoreInstruction
void IgnoreInstruction(const std::string &key)
Definition: file_test.h:175
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
FileTest::OnInstructionUsed
void OnInstructionUsed(const std::string &key)
Definition: file_test.cc:335
FileTest::kReadEOF
@ kReadEOF
Definition: file_test.h:94
FileTest::ClearTest
void ClearTest()
Definition: file_test.cc:315
FileTest::CurrentTestToString
const std::string & CurrentTestToString() const
Definition: file_test.cc:306
FileTest::FileTest
FileTest(std::unique_ptr< LineReader > reader, std::function< void(const std::string &)> comment_callback, bool is_kas_test)
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8
FileTest::IgnoreAllUnusedInstructions
void IgnoreAllUnusedInstructions()
Definition: file_test.cc:289
OPENSSL_PRINTF_FORMAT_FUNC
#define OPENSSL_PRINTF_FORMAT_FUNC(string_index, first_to_check)
Definition: base.h:240
FileTest::ConvertToBytes
bool ConvertToBytes(std::vector< uint8_t > *out, const std::string &value)
Definition: file_test.cc:339
FileTest::kReadError
@ kReadError
Definition: file_test.h:95


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:20