forked from google/boringssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_test.cc
426 lines (374 loc) · 11.6 KB
/
file_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* Copyright (c) 2015, Google Inc.
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include "file_test.h"
#include <algorithm>
#include <memory>
#include <ctype.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/err.h>
#include "../internal.h"
FileTest::FileTest(const char *path) {
file_ = fopen(path, "r");
if (file_ == nullptr) {
fprintf(stderr, "Could not open file %s: %s.\n", path, strerror(errno));
}
}
FileTest::~FileTest() {
if (file_ != nullptr) {
fclose(file_);
}
}
// FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr
// if there is none.
static const char *FindDelimiter(const char *str) {
while (*str) {
if (*str == ':' || *str == '=') {
return str;
}
str++;
}
return nullptr;
}
// StripSpace returns a string containing up to |len| characters from |str| with
// leading and trailing whitespace removed.
static std::string StripSpace(const char *str, size_t len) {
// Remove leading space.
while (len > 0 && isspace(*str)) {
str++;
len--;
}
while (len > 0 && isspace(str[len - 1])) {
len--;
}
return std::string(str, len);
}
static std::pair<std::string, std::string> ParseKeyValue(const char *str, const size_t len) {
const char *delimiter = FindDelimiter(str);
std::string key, value;
if (delimiter == nullptr) {
key = StripSpace(str, len);
} else {
key = StripSpace(str, delimiter - str);
value = StripSpace(delimiter + 1, str + len - delimiter - 1);
}
return {key, value};
}
FileTest::ReadResult FileTest::ReadNext() {
// If the previous test had unused attributes or instructions, it is an error.
if (!unused_attributes_.empty()) {
for (const std::string &key : unused_attributes_) {
PrintLine("Unused attribute: %s", key.c_str());
}
return kReadError;
}
if (!unused_instructions_.empty()) {
for (const std::string &key : unused_instructions_) {
PrintLine("Unused instruction: %s", key.c_str());
}
return kReadError;
}
ClearTest();
static const size_t kBufLen = 8192 * 4;
std::unique_ptr<char[]> buf(new char[kBufLen]);
bool in_instruction_block = false;
is_at_new_instruction_block_ = false;
while (true) {
// Read the next line.
if (fgets(buf.get(), kBufLen, file_) == nullptr) {
if (feof(file_)) {
// EOF is a valid terminator for a test.
return start_line_ > 0 ? kReadSuccess : kReadEOF;
}
fprintf(stderr, "Error reading from input.\n");
return kReadError;
}
line_++;
size_t len = strlen(buf.get());
// Check for truncation.
if (len > 0 && buf[len - 1] != '\n' && !feof(file_)) {
fprintf(stderr, "Line %u too long.\n", line_);
return kReadError;
}
if (buf[0] == '\n' || buf[0] == '\r' || buf[0] == '\0') {
// Empty lines delimit tests.
if (start_line_ > 0) {
return kReadSuccess;
}
if (in_instruction_block) {
in_instruction_block = false;
// Delimit instruction block from test with a blank line.
current_test_ += "\r\n";
}
} else if (buf[0] == '#' ||
strcmp("[B.4.2 Key Pair Generation by Testing Candidates]\r\n",
buf.get()) == 0) {
// Ignore comments. The above instruction-like line is treated as a
// comment because the FIPS lab's request files are hopelessly
// inconsistent.
} else if (buf[0] == '[') { // Inside an instruction block.
is_at_new_instruction_block_ = true;
if (start_line_ != 0) {
// Instructions should be separate blocks.
fprintf(stderr, "Line %u is an instruction in a test case.\n", line_);
return kReadError;
}
if (!in_instruction_block) {
ClearInstructions();
in_instruction_block = true;
}
// Parse the line as an instruction ("[key = value]" or "[key]").
std::string kv = StripSpace(buf.get(), len);
if (kv[kv.size() - 1] != ']') {
fprintf(stderr, "Line %u, invalid instruction: %s\n", line_,
kv.c_str());
return kReadError;
}
current_test_ += kv + "\r\n";
kv = std::string(kv.begin() + 1, kv.end() - 1);
for (;;) {
size_t idx = kv.find(",");
if (idx == std::string::npos) {
idx = kv.size();
}
std::string key, value;
std::tie(key, value) = ParseKeyValue(kv.c_str(), idx);
instructions_[key] = value;
if (idx == kv.size())
break;
kv = kv.substr(idx + 1);
}
} else {
// Parsing a test case.
if (in_instruction_block) {
// Some NIST CAVP test files (TDES) have a test case immediately
// following an instruction block, without a separate blank line, some
// of the time.
in_instruction_block = false;
}
current_test_ += std::string(buf.get(), len);
std::string key, value;
std::tie(key, value) = ParseKeyValue(buf.get(), len);
// Duplicate keys are rewritten to have “/2”, “/3”, … suffixes.
std::string mapped_key = key;
for (unsigned i = 2; attributes_.count(mapped_key) != 0; i++) {
char suffix[32];
snprintf(suffix, sizeof(suffix), "/%u", i);
suffix[sizeof(suffix)-1] = 0;
mapped_key = key + suffix;
}
unused_attributes_.insert(mapped_key);
attributes_[mapped_key] = value;
if (start_line_ == 0) {
// This is the start of a test.
type_ = mapped_key;
parameter_ = value;
start_line_ = line_;
for (const auto &kv : instructions_) {
unused_instructions_.insert(kv.first);
}
}
}
}
}
void FileTest::PrintLine(const char *format, ...) {
va_list args;
va_start(args, format);
fprintf(stderr, "Line %u: ", start_line_);
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
va_end(args);
}
const std::string &FileTest::GetType() {
OnKeyUsed(type_);
return type_;
}
const std::string &FileTest::GetParameter() {
OnKeyUsed(type_);
return parameter_;
}
bool FileTest::HasAttribute(const std::string &key) {
OnKeyUsed(key);
return attributes_.count(key) > 0;
}
bool FileTest::GetAttribute(std::string *out_value, const std::string &key) {
OnKeyUsed(key);
auto iter = attributes_.find(key);
if (iter == attributes_.end()) {
PrintLine("Missing attribute '%s'.", key.c_str());
return false;
}
*out_value = iter->second;
return true;
}
const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
if (!HasAttribute(key)) {
abort();
}
return attributes_[key];
}
bool FileTest::HasInstruction(const std::string &key) {
OnInstructionUsed(key);
return instructions_.count(key) > 0;
}
bool FileTest::GetInstruction(std::string *out_value, const std::string &key) {
OnInstructionUsed(key);
auto iter = instructions_.find(key);
if (iter == instructions_.end()) {
PrintLine("Missing instruction '%s'.", key.c_str());
return false;
}
*out_value = iter->second;
return true;
}
const std::string &FileTest::CurrentTestToString() const {
return current_test_;
}
static bool FromHexDigit(uint8_t *out, char c) {
if ('0' <= c && c <= '9') {
*out = c - '0';
return true;
}
if ('a' <= c && c <= 'f') {
*out = c - 'a' + 10;
return true;
}
if ('A' <= c && c <= 'F') {
*out = c - 'A' + 10;
return true;
}
return false;
}
bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) {
std::string value;
if (!GetAttribute(&value, key)) {
return false;
}
if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') {
out->assign(value.begin() + 1, value.end() - 1);
return true;
}
if (value.size() % 2 != 0) {
PrintLine("Error decoding value: %s", value.c_str());
return false;
}
out->clear();
out->reserve(value.size() / 2);
for (size_t i = 0; i < value.size(); i += 2) {
uint8_t hi, lo;
if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i + 1])) {
PrintLine("Error decoding value: %s", value.c_str());
return false;
}
out->push_back((hi << 4) | lo);
}
return true;
}
static std::string EncodeHex(const uint8_t *in, size_t in_len) {
static const char kHexDigits[] = "0123456789abcdef";
std::string ret;
ret.reserve(in_len * 2);
for (size_t i = 0; i < in_len; i++) {
ret += kHexDigits[in[i] >> 4];
ret += kHexDigits[in[i] & 0xf];
}
return ret;
}
bool FileTest::ExpectBytesEqual(const uint8_t *expected, size_t expected_len,
const uint8_t *actual, size_t actual_len) {
if (expected_len == actual_len &&
OPENSSL_memcmp(expected, actual, expected_len) == 0) {
return true;
}
std::string expected_hex = EncodeHex(expected, expected_len);
std::string actual_hex = EncodeHex(actual, actual_len);
PrintLine("Expected: %s", expected_hex.c_str());
PrintLine("Actual: %s", actual_hex.c_str());
return false;
}
void FileTest::ClearTest() {
start_line_ = 0;
type_.clear();
parameter_.clear();
attributes_.clear();
unused_attributes_.clear();
current_test_ = "";
}
void FileTest::ClearInstructions() {
instructions_.clear();
unused_attributes_.clear();
}
void FileTest::OnKeyUsed(const std::string &key) {
unused_attributes_.erase(key);
}
void FileTest::OnInstructionUsed(const std::string &key) {
unused_instructions_.erase(key);
}
bool FileTest::IsAtNewInstructionBlock() const {
return is_at_new_instruction_block_;
}
void FileTest::InjectInstruction(const std::string &key,
const std::string &value) {
instructions_[key] = value;
}
int FileTestMainSilent(FileTestFunc run_test, void *arg, const char *path) {
FileTest t(path);
if (!t.is_open()) {
return 1;
}
bool failed = false;
while (true) {
FileTest::ReadResult ret = t.ReadNext();
if (ret == FileTest::kReadError) {
return 1;
} else if (ret == FileTest::kReadEOF) {
break;
}
bool result = run_test(&t, arg);
if (t.HasAttribute("Error")) {
if (result) {
t.PrintLine("Operation unexpectedly succeeded.");
failed = true;
continue;
}
uint32_t err = ERR_peek_error();
if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) {
t.PrintLine("Unexpected error; wanted '%s', got '%s'.",
t.GetAttributeOrDie("Error").c_str(),
ERR_reason_error_string(err));
failed = true;
ERR_clear_error();
continue;
}
ERR_clear_error();
} else if (!result) {
// In case the test itself doesn't print output, print something so the
// line number is reported.
t.PrintLine("Test failed");
ERR_print_errors_fp(stderr);
failed = true;
continue;
}
}
return failed ? 1 : 0;
}
int FileTestMain(FileTestFunc run_test, void *arg, const char *path) {
int result = FileTestMainSilent(run_test, arg, path);
if (!result) {
printf("PASS\n");
}
return result;
}