blob: e90cc86ad58647d871e8a9e44eb5b9140b1222ca [file] [log] [blame]
Adam Langleye9ada862015-05-11 17:20:37 -07001/* 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
David Benjamin4969cc92016-04-22 15:02:23 -040018#include <openssl/base.h>
19
Adam Langleye9ada862015-05-11 17:20:37 -070020#include <stdint.h>
21#include <stdio.h>
22
Kenny Rootb8494592015-09-25 02:29:14 +000023#if defined(_MSC_VER)
24#pragma warning(push)
25#pragma warning(disable: 4702)
26#endif
27
Adam Langleye9ada862015-05-11 17:20:37 -070028#include <string>
29#include <map>
30#include <set>
31#include <vector>
32
Kenny Rootb8494592015-09-25 02:29:14 +000033#if defined(_MSC_VER)
34#pragma warning(pop)
35#endif
Adam Langleye9ada862015-05-11 17:20:37 -070036
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
David Benjamin4969cc92016-04-22 15:02:23 -040041// file is a sequence of attributes and blank lines.
Adam Langleye9ada862015-05-11 17:20:37 -070042//
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//
David Benjamin4969cc92016-04-22 15:02:23 -040050// Lines beginning with # are ignored.
Adam Langleye9ada862015-05-11 17:20:37 -070051//
David Benjamin4969cc92016-04-22 15:02:23 -040052// A test is a sequence of one or more attributes followed by a blank line.
53// Blank lines are otherwise ignored. For tests that process multiple kinds of
54// test cases, the first attribute is parsed out as the test's type and
Adam Langleye9ada862015-05-11 17:20:37 -070055// parameter. Otherwise, attributes are unordered. The first attribute is also
56// included in the set of attributes, so tests which do not dispatch may ignore
57// this mechanism.
58//
59// Functions in this module freely output to |stderr| on failure. Tests should
60// also do so, and it is recommended they include the corresponding test's line
61// number in any output. |PrintLine| does this automatically.
62//
63// Each attribute in a test must be consumed. When a test completes, if any
64// attributes haven't been processed, the framework reports an error.
65
66
67class FileTest {
68 public:
69 explicit FileTest(const char *path);
70 ~FileTest();
71
72 // is_open returns true if the file was successfully opened.
73 bool is_open() const { return file_ != nullptr; }
74
75 enum ReadResult {
76 kReadSuccess,
77 kReadEOF,
78 kReadError,
79 };
80
81 // ReadNext reads the next test from the file. It returns |kReadSuccess| if
82 // successfully reading a test and |kReadEOF| at the end of the file. On
83 // error or if the previous test had unconsumed attributes, it returns
84 // |kReadError|.
85 ReadResult ReadNext();
86
87 // PrintLine is a variant of printf which prepends the line number and appends
88 // a trailing newline.
David Benjamin4969cc92016-04-22 15:02:23 -040089 void PrintLine(const char *format, ...) OPENSSL_PRINTF_FORMAT_FUNC(2, 3);
Adam Langleye9ada862015-05-11 17:20:37 -070090
91 unsigned start_line() const { return start_line_; }
92
93 // GetType returns the name of the first attribute of the current test.
94 const std::string &GetType();
95 // GetParameter returns the value of the first attribute of the current test.
96 const std::string &GetParameter();
Adam Langleye9ada862015-05-11 17:20:37 -070097
98 // HasAttribute returns true if the current test has an attribute named |key|.
99 bool HasAttribute(const std::string &key);
100
101 // GetAttribute looks up the attribute with key |key|. It sets |*out_value| to
102 // the value and returns true if it exists and returns false with an error to
103 // |stderr| otherwise.
104 bool GetAttribute(std::string *out_value, const std::string &key);
105
106 // GetAttributeOrDie looks up the attribute with key |key| and aborts if it is
David Benjamin4969cc92016-04-22 15:02:23 -0400107 // missing. It should only be used after a |HasAttribute| call.
Adam Langleye9ada862015-05-11 17:20:37 -0700108 const std::string &GetAttributeOrDie(const std::string &key);
109
110 // GetBytes looks up the attribute with key |key| and decodes it as a byte
111 // string. On success, it writes the result to |*out| and returns
112 // true. Otherwise it returns false with an error to |stderr|. The value may
113 // be either a hexadecimal string or a quoted ASCII string. It returns true on
114 // success and returns false with an error to |stderr| on failure.
115 bool GetBytes(std::vector<uint8_t> *out, const std::string &key);
116
117 // ExpectBytesEqual returns true if |expected| and |actual| are equal.
118 // Otherwise, it returns false and prints a message to |stderr|.
119 bool ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
120 const uint8_t *actual, size_t actual_len);
121
122 private:
123 void ClearTest();
124 void OnKeyUsed(const std::string &key);
125
126 FILE *file_ = nullptr;
127 // line_ is the number of lines read.
128 unsigned line_ = 0;
129
130 // start_line_ is the line number of the first attribute of the test.
131 unsigned start_line_ = 0;
132 // type_ is the name of the first attribute of the test.
133 std::string type_;
134 // parameter_ is the value of the first attribute.
135 std::string parameter_;
136 // attributes_ contains all attributes in the test, including the first.
137 std::map<std::string, std::string> attributes_;
Adam Langleye9ada862015-05-11 17:20:37 -0700138
139 // unused_attributes_ is the set of attributes that have been queried.
140 std::set<std::string> unused_attributes_;
Adam Langleye9ada862015-05-11 17:20:37 -0700141
142 FileTest(const FileTest&) = delete;
143 FileTest &operator=(const FileTest&) = delete;
144};
145
146// FileTestMain runs a file-based test out of |path| and returns an exit code
147// suitable to return out of |main|. |run_test| should return true on pass and
148// false on failure. FileTestMain also implements common handling of the 'Error'
149// attribute. A test with that attribute is expected to fail. The value of the
150// attribute is the reason string of the expected OpenSSL error code.
151//
152// Tests are guaranteed to run serially and may affect global state if need be.
153// It is legal to use "tests" which, for example, import a private key into a
154// list of keys. This may be used to initialize a shared set of keys for many
155// tests. However, if one test fails, the framework will continue to run
156// subsequent tests.
157int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
158 const char *path);
159
160
161#endif /* OPENSSL_HEADER_CRYPTO_TEST_FILE_TEST_H */