blob: 6b725b9fc8e3a93853c2101e97c87d4d88603985 [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#include "file_test.h"
16
17#include <ctype.h>
18#include <errno.h>
19#include <stdarg.h>
David Benjamin771a1382015-05-12 18:01:17 -040020#include <stdlib.h>
David Benjamin06b94de2015-05-09 22:46:47 -040021#include <string.h>
22
David Benjamin5c694e32015-05-11 15:58:08 -040023#include <openssl/err.h>
24
David Benjamin06b94de2015-05-09 22:46:47 -040025
26FileTest::FileTest(const char *path) {
27 file_ = fopen(path, "r");
28 if (file_ == nullptr) {
29 fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
30 }
31}
32
33FileTest::~FileTest() {
34 if (file_ != nullptr) {
35 fclose(file_);
36 }
37}
38
39// FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
40// if there is none.
41static const char *FindDelimiter(const char *str) {
42 while (*str) {
43 if (*str == ':' || *str == '=') {
44 return str;
45 }
46 str++;
47 }
48 return nullptr;
49}
50
51// StripSpace returns a string containing up to |len| characters from |str| with
52// leading and trailing whitespace removed.
53static std::string StripSpace(const char *str, size_t len) {
54 // Remove leading space.
55 while (len > 0 && isspace(*str)) {
56 str++;
57 len--;
58 }
59 while (len > 0 && isspace(str[len-1])) {
60 len--;
61 }
62 return std::string(str, len);
63}
64
65FileTest::ReadResult FileTest::ReadNext() {
66 // If the previous test had unused attributes or block, it is an error.
67 if (!unused_attributes_.empty()) {
68 for (const std::string &key : unused_attributes_) {
69 PrintLine("Unused attribute: %s", key.c_str());
70 }
71 return kReadError;
72 }
73 if (!block_.empty() && !used_block_) {
74 PrintLine("Unused block");
75 return kReadError;
76 }
77
78 ClearTest();
79
80 bool in_block = false;
81 while (true) {
82 // Read the next line.
83 char buf[4096];
84 if (fgets(buf, sizeof(buf), file_) == nullptr) {
85 if (feof(file_)) {
86 if (in_block) {
87 fprintf(stderr, "Unterminated block.\n");
88 return kReadError;
89 }
90 // EOF is a valid terminator for a test.
91 return start_line_ > 0 ? kReadSuccess : kReadEOF;
92 }
93 fprintf(stderr, "Error reading from input.\n");
94 return kReadError;
95 }
96
97 line_++;
98 size_t len = strlen(buf);
99 // Check for truncation.
100 if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
101 fprintf(stderr, "Line %u too long.\n", line_);
102 return kReadError;
103 }
104
105 bool is_delimiter = strncmp(buf, "---", 3) == 0;
106 if (in_block) {
107 block_ += buf;
108 if (is_delimiter) {
109 // Ending the block completes the test.
110 return kReadSuccess;
111 }
112 } else if (is_delimiter) {
113 if (start_line_ == 0) {
114 fprintf(stderr, "Line %u: Unexpected block.\n", line_);
115 return kReadError;
116 }
117 in_block = true;
118 block_ += buf;
119 } else if (buf[0] == '\n' || buf[0] == '\0') {
120 // Empty lines delimit tests.
121 if (start_line_ > 0) {
122 return kReadSuccess;
123 }
124 } else if (buf[0] != '#') { // Comment lines are ignored.
125 // Parse the line as an attribute.
126 const char *delimiter = FindDelimiter(buf);
127 if (delimiter == nullptr) {
128 fprintf(stderr, "Line %u: Could not parse attribute.\n", line_);
Brian Smith7b5f08e2015-08-06 10:42:49 -0400129 return kReadError;
David Benjamin06b94de2015-05-09 22:46:47 -0400130 }
131 std::string key = StripSpace(buf, delimiter - buf);
132 std::string value = StripSpace(delimiter + 1,
133 buf + len - delimiter - 1);
134
135 unused_attributes_.insert(key);
136 attributes_[key] = value;
137 if (start_line_ == 0) {
138 // This is the start of a test.
139 type_ = key;
140 parameter_ = value;
141 start_line_ = line_;
142 }
143 }
144 }
145}
146
147void FileTest::PrintLine(const char *format, ...) {
148 va_list args;
149 va_start(args, format);
150
151 fprintf(stderr, "Line %u: ", start_line_);
152 vfprintf(stderr, format, args);
153 fprintf(stderr, "\n");
154
155 va_end(args);
156}
157
158const std::string &FileTest::GetType() {
159 OnKeyUsed(type_);
160 return type_;
161}
162
163const std::string &FileTest::GetParameter() {
164 OnKeyUsed(type_);
165 return parameter_;
166}
167
168const std::string &FileTest::GetBlock() {
169 used_block_ = true;
170 return block_;
171}
172
173bool FileTest::HasAttribute(const std::string &key) {
174 OnKeyUsed(key);
175 return attributes_.count(key) > 0;
176}
177
178bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
179 OnKeyUsed(key);
180 auto iter = attributes_.find(key);
181 if (iter == attributes_.end()) {
182 PrintLine("Missing attribute '%s'.", key.c_str());
183 return false;
184 }
185 *out_value = iter->second;
186 return true;
187}
188
David Benjamin5c694e32015-05-11 15:58:08 -0400189const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
190 if (!HasAttribute(key)) {
191 abort();
192 }
193 return attributes_[key];
194}
195
David Benjamin06b94de2015-05-09 22:46:47 -0400196static bool FromHexDigit(uint8_t *out, char c) {
197 if ('0' <= c && c <= '9') {
198 *out = c - '0';
199 return true;
200 }
201 if ('a' <= c && c <= 'f') {
202 *out = c - 'a' + 10;
203 return true;
204 }
205 if ('A' <= c && c <= 'F') {
206 *out = c - 'A' + 10;
207 return true;
208 }
209 return false;
210}
211
212bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
213 std::string value;
214 if (!GetAttribute(&value, key)) {
215 return false;
216 }
217
218 if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
219 out->assign(value.begin() + 1, value.end() - 1);
220 return true;
221 }
222
223 if (value.size() % 2 != 0) {
224 PrintLine("Error decoding value: %s", value.c_str());
225 return false;
226 }
David Benjamine30a09e2016-01-01 01:17:30 -0500227 out->clear();
David Benjamin06b94de2015-05-09 22:46:47 -0400228 out->reserve(value.size() / 2);
229 for (size_t i = 0; i < value.size(); i += 2) {
230 uint8_t hi, lo;
231 if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i+1])) {
232 PrintLine("Error decoding value: %s", value.c_str());
233 return false;
234 }
235 out->push_back((hi << 4) | lo);
236 }
237 return true;
238}
239
240static std::string EncodeHex(const uint8_t *in, size_t in_len) {
241 static const char kHexDigits[] = "0123456789abcdef";
242 std::string ret;
243 ret.reserve(in_len * 2);
244 for (size_t i = 0; i < in_len; i++) {
245 ret += kHexDigits[in[i] >> 4];
246 ret += kHexDigits[in[i] & 0xf];
247 }
248 return ret;
249}
250
251bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
252 const uint8_t *actual, size_t actual_len) {
253 if (expected_len == actual_len &&
254 memcmp(expected, actual, expected_len) == 0) {
255 return true;
256 }
257
258 std::string expected_hex = EncodeHex(expected, expected_len);
259 std::string actual_hex = EncodeHex(actual, actual_len);
260 PrintLine("Expected: %s", expected_hex.c_str());
261 PrintLine("Actual: %s", actual_hex.c_str());
262 return false;
263}
264
265void FileTest::ClearTest() {
266 start_line_ = 0;
267 type_.clear();
268 parameter_.clear();
269 attributes_.clear();
270 block_.clear();
271 unused_attributes_.clear();
272 used_block_ = false;
273}
274
275void FileTest::OnKeyUsed(const std::string &key) {
276 unused_attributes_.erase(key);
277}
278
David Benjamin5c694e32015-05-11 15:58:08 -0400279int FileTestMain(bool (*run_test)(FileTest *t, void *arg), void *arg,
280 const char *path) {
David Benjamin06b94de2015-05-09 22:46:47 -0400281 FileTest t(path);
282 if (!t.is_open()) {
283 return 1;
284 }
285
286 bool failed = false;
287 while (true) {
288 FileTest::ReadResult ret = t.ReadNext();
289 if (ret == FileTest::kReadError) {
290 return 1;
291 } else if (ret == FileTest::kReadEOF) {
292 break;
293 }
294
David Benjamin5c694e32015-05-11 15:58:08 -0400295 bool result = run_test(&t, arg);
296 if (t.HasAttribute("Error")) {
297 if (result) {
298 t.PrintLine("Operation unexpectedly succeeded.");
299 failed = true;
300 continue;
301 }
302 uint32_t err = ERR_peek_error();
303 if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) {
304 t.PrintLine("Unexpected error; wanted '%s', got '%s'.",
305 t.GetAttributeOrDie("Error").c_str(),
306 ERR_reason_error_string(err));
307 failed = true;
David Benjamine30a09e2016-01-01 01:17:30 -0500308 ERR_clear_error();
David Benjamin5c694e32015-05-11 15:58:08 -0400309 continue;
310 }
311 ERR_clear_error();
312 } else if (!result) {
313 // In case the test itself doesn't print output, print something so the
314 // line number is reported.
315 t.PrintLine("Test failed");
316 ERR_print_errors_fp(stderr);
David Benjamin06b94de2015-05-09 22:46:47 -0400317 failed = true;
David Benjamin5c694e32015-05-11 15:58:08 -0400318 continue;
David Benjamin06b94de2015-05-09 22:46:47 -0400319 }
320 }
321
322 if (failed) {
323 return 1;
324 }
325
326 printf("PASS\n");
327 return 0;
328}