| 1 |
// Copyright (c) 2015, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include "file_test.h" |
| 5 |
|
| 6 |
#include <algorithm> |
| 7 |
#include <utility> |
| 8 |
|
| 9 |
#include <assert.h> |
| 10 |
#include <ctype.h> |
| 11 |
#include <errno.h> |
| 12 |
#include <limits.h> |
| 13 |
#include <stdarg.h> |
| 14 |
#include <stdio.h> |
| 15 |
#include <stdlib.h> |
| 16 |
#include <string.h> |
| 17 |
|
| 18 |
#include <openssl/err.h> |
| 19 |
#include <openssl/mem.h> |
| 20 |
|
| 21 |
#include "../internal.h" |
| 22 |
#include "./test_util.h" |
| 23 |
|
| 24 |
|
| 25 |
FileTest::FileTest(std::unique_ptr<FileTest::LineReader> reader, |
| 26 |
std::function<void(const std::string &)> comment_callback, |
| 27 |
bool is_kas_test) |
| 28 |
: reader_(std::move(reader)), |
| 29 |
is_kas_test_(is_kas_test), |
| 30 |
comment_callback_(std::move(comment_callback)) {} |
| 31 |
|
| 32 |
FileTest::~FileTest() {} |
| 33 |
|
| 34 |
// FindDelimiter returns a pointer to the first '=' or ':' in |str| or nullptr |
| 35 |
// if there is none. |
| 36 |
static const char *FindDelimiter(const char *str) { |
| 37 |
while (*str) { |
| 38 |
if (*str == ':' || *str == '=') { |
| 39 |
return str; |
| 40 |
} |
| 41 |
str++; |
| 42 |
} |
| 43 |
return nullptr; |
| 44 |
} |
| 45 |
|
| 46 |
// StripSpace returns a string containing up to |len| characters from |str| with |
| 47 |
// leading and trailing whitespace removed. |
| 48 |
static std::string StripSpace(const char *str, size_t len) { |
| 49 |
// Remove leading space. |
| 50 |
while (len > 0 && OPENSSL_isspace(*str)) { |
| 51 |
str++; |
| 52 |
len--; |
| 53 |
} |
| 54 |
while (len > 0 && OPENSSL_isspace(str[len - 1])) { |
| 55 |
len--; |
| 56 |
} |
| 57 |
return std::string(str, len); |
| 58 |
} |
| 59 |
|
| 60 |
static std::pair<std::string, std::string> ParseKeyValue(const char *str, const size_t len) { |
| 61 |
const char *delimiter = FindDelimiter(str); |
| 62 |
std::string key, value; |
| 63 |
if (delimiter == nullptr) { |
| 64 |
key = StripSpace(str, len); |
| 65 |
} else { |
| 66 |
key = StripSpace(str, delimiter - str); |
| 67 |
value = StripSpace(delimiter + 1, str + len - delimiter - 1); |
| 68 |
} |
| 69 |
return {key, value}; |
| 70 |
} |
| 71 |
|
| 72 |
FileTest::ReadResult FileTest::ReadNext() { |
| 73 |
// If the previous test had unused attributes or instructions, it is an error. |
| 74 |
if (!unused_attributes_.empty()) { |
| 75 |
for (const std::string &key : unused_attributes_) { |
| 76 |
PrintLine("Unused attribute: %s", key.c_str()); |
| 77 |
} |
| 78 |
return kReadError; |
| 79 |
} |
| 80 |
if (!unused_instructions_.empty()) { |
| 81 |
for (const std::string &key : unused_instructions_) { |
| 82 |
PrintLine("Unused instruction: %s", key.c_str()); |
| 83 |
} |
| 84 |
return kReadError; |
| 85 |
} |
| 86 |
|
| 87 |
ClearTest(); |
| 88 |
|
| 89 |
static const size_t kBufLen = 8192 * 4; |
| 90 |
std::unique_ptr<char[]> buf(new char[kBufLen]); |
| 91 |
|
| 92 |
bool in_instruction_block = false; |
| 93 |
is_at_new_instruction_block_ = false; |
| 94 |
|
| 95 |
while (true) { |
| 96 |
// Read the next line. |
| 97 |
switch (reader_->ReadLine(buf.get(), kBufLen)) { |
| 98 |
case kReadError: |
| 99 |
fprintf(stderr, "Error reading from input at line %u.\n", line_ + 1); |
| 100 |
return kReadError; |
| 101 |
case kReadEOF: |
| 102 |
// EOF is a valid terminator for a test. |
| 103 |
return start_line_ > 0 ? kReadSuccess : kReadEOF; |
| 104 |
case kReadSuccess: |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
line_++; |
| 109 |
size_t len = strlen(buf.get()); |
| 110 |
if (buf[0] == '\n' || buf[0] == '\r' || buf[0] == '\0') { |
| 111 |
// Empty lines delimit tests. |
| 112 |
if (start_line_ > 0) { |
| 113 |
return kReadSuccess; |
| 114 |
} |
| 115 |
if (in_instruction_block) { |
| 116 |
in_instruction_block = false; |
| 117 |
// Delimit instruction block from test with a blank line. |
| 118 |
current_test_ += "\r\n"; |
| 119 |
} else if (is_kas_test_) { |
| 120 |
// KAS tests have random blank lines scattered around. |
| 121 |
current_test_ += "\r\n"; |
| 122 |
} |
| 123 |
} else if (buf[0] == '#') { |
| 124 |
if (is_kas_test_ && seen_non_comment_) { |
| 125 |
// KAS tests have comments after the initial comment block which need |
| 126 |
// to be included in the corresponding place in the output. |
| 127 |
current_test_ += std::string(buf.get()); |
| 128 |
} else if (comment_callback_) { |
| 129 |
comment_callback_(buf.get()); |
| 130 |
} |
| 131 |
// Otherwise ignore comments. |
| 132 |
} else if (strcmp("[B.4.2 Key Pair Generation by Testing Candidates]\r\n", |
| 133 |
buf.get()) == 0) { |
| 134 |
// The above instruction-like line is ignored because the FIPS lab's |
| 135 |
// request files are hopelessly inconsistent. |
| 136 |
} else if (buf[0] == '[') { // Inside an instruction block. |
| 137 |
is_at_new_instruction_block_ = true; |
| 138 |
seen_non_comment_ = true; |
| 139 |
if (start_line_ != 0) { |
| 140 |
// Instructions should be separate blocks. |
| 141 |
fprintf(stderr, "Line %u is an instruction in a test case.\n", line_); |
| 142 |
return kReadError; |
| 143 |
} |
| 144 |
if (!in_instruction_block) { |
| 145 |
ClearInstructions(); |
| 146 |
in_instruction_block = true; |
| 147 |
} |
| 148 |
|
| 149 |
// Parse the line as an instruction ("[key = value]" or "[key]"). |
| 150 |
|
| 151 |
// KAS tests contain invalid syntax. |
| 152 |
std::string kv = buf.get(); |
| 153 |
const bool is_broken_kas_instruction = |
| 154 |
is_kas_test_ && |
| 155 |
(kv == "[SHA(s) supported (Used for hashing Z): SHA512 \r\n"); |
| 156 |
|
| 157 |
if (!is_broken_kas_instruction) { |
| 158 |
kv = StripSpace(buf.get(), len); |
| 159 |
if (kv[kv.size() - 1] != ']') { |
| 160 |
fprintf(stderr, "Line %u, invalid instruction: '%s'\n", line_, |
| 161 |
kv.c_str()); |
| 162 |
return kReadError; |
| 163 |
} |
| 164 |
} else { |
| 165 |
// Just remove the newline for the broken instruction. |
| 166 |
kv = kv.substr(0, kv.size() - 2); |
| 167 |
} |
| 168 |
|
| 169 |
current_test_ += kv + "\r\n"; |
| 170 |
kv = std::string(kv.begin() + 1, kv.end() - 1); |
| 171 |
|
| 172 |
for (;;) { |
| 173 |
size_t idx = kv.find(','); |
| 174 |
if (idx == std::string::npos) { |
| 175 |
idx = kv.size(); |
| 176 |
} |
| 177 |
std::string key, value; |
| 178 |
std::tie(key, value) = ParseKeyValue(kv.c_str(), idx); |
| 179 |
instructions_[key] = value; |
| 180 |
if (idx == kv.size()) |
| 181 |
break; |
| 182 |
kv = kv.substr(idx + 1); |
| 183 |
} |
| 184 |
} else { |
| 185 |
// Parsing a test case. |
| 186 |
if (in_instruction_block) { |
| 187 |
// Some NIST CAVP test files (TDES) have a test case immediately |
| 188 |
// following an instruction block, without a separate blank line, some |
| 189 |
// of the time. |
| 190 |
in_instruction_block = false; |
| 191 |
} |
| 192 |
|
| 193 |
current_test_ += std::string(buf.get(), len); |
| 194 |
std::string key, value; |
| 195 |
std::tie(key, value) = ParseKeyValue(buf.get(), len); |
| 196 |
|
| 197 |
// Duplicate keys are rewritten to have “/2”, “/3”, … suffixes. |
| 198 |
std::string mapped_key = key; |
| 199 |
// If absent, the value will be zero-initialized. |
| 200 |
const size_t num_occurrences = ++attribute_count_[key]; |
| 201 |
if (num_occurrences > 1) { |
| 202 |
mapped_key += "/" + std::to_string(num_occurrences); |
| 203 |
} |
| 204 |
|
| 205 |
unused_attributes_.insert(mapped_key); |
| 206 |
attributes_[mapped_key] = value; |
| 207 |
if (start_line_ == 0) { |
| 208 |
// This is the start of a test. |
| 209 |
type_ = mapped_key; |
| 210 |
parameter_ = value; |
| 211 |
start_line_ = line_; |
| 212 |
for (const auto &kv : instructions_) { |
| 213 |
unused_instructions_.insert(kv.first); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
void FileTest::PrintLine(const char *format, ...) { |
| 221 |
va_list args; |
| 222 |
va_start(args, format); |
| 223 |
|
| 224 |
fprintf(stderr, "Line %u: ", start_line_); |
| 225 |
vfprintf(stderr, format, args); |
| 226 |
fprintf(stderr, "\n"); |
| 227 |
|
| 228 |
va_end(args); |
| 229 |
} |
| 230 |
|
| 231 |
const std::string &FileTest::GetType() { |
| 232 |
OnKeyUsed(type_); |
| 233 |
return type_; |
| 234 |
} |
| 235 |
|
| 236 |
const std::string &FileTest::GetParameter() { |
| 237 |
OnKeyUsed(type_); |
| 238 |
return parameter_; |
| 239 |
} |
| 240 |
|
| 241 |
bool FileTest::HasAttribute(const std::string &key) { |
| 242 |
OnKeyUsed(key); |
| 243 |
return attributes_.count(key) > 0; |
| 244 |
} |
| 245 |
|
| 246 |
bool FileTest::GetAttribute(std::string *out_value, const std::string &key) { |
| 247 |
OnKeyUsed(key); |
| 248 |
auto iter = attributes_.find(key); |
| 249 |
if (iter == attributes_.end()) { |
| 250 |
PrintLine("Missing attribute '%s'.", key.c_str()); |
| 251 |
return false; |
| 252 |
} |
| 253 |
*out_value = iter->second; |
| 254 |
return true; |
| 255 |
} |
| 256 |
|
| 257 |
const std::string &FileTest::GetAttributeOrDie(const std::string &key) { |
| 258 |
if (!HasAttribute(key)) { |
| 259 |
abort(); |
| 260 |
} |
| 261 |
return attributes_[key]; |
| 262 |
} |
| 263 |
|
| 264 |
bool FileTest::HasInstruction(const std::string &key) { |
| 265 |
OnInstructionUsed(key); |
| 266 |
return instructions_.count(key) > 0; |
| 267 |
} |
| 268 |
|
| 269 |
bool FileTest::GetInstruction(std::string *out_value, const std::string &key) { |
| 270 |
OnInstructionUsed(key); |
| 271 |
auto iter = instructions_.find(key); |
| 272 |
if (iter == instructions_.end()) { |
| 273 |
PrintLine("Missing instruction '%s'.", key.c_str()); |
| 274 |
return false; |
| 275 |
} |
| 276 |
*out_value = iter->second; |
| 277 |
return true; |
| 278 |
} |
| 279 |
|
| 280 |
void FileTest::IgnoreAllUnusedInstructions() { |
| 281 |
unused_instructions_.clear(); |
| 282 |
} |
| 283 |
|
| 284 |
const std::string &FileTest::GetInstructionOrDie(const std::string &key) { |
| 285 |
if (!HasInstruction(key)) { |
| 286 |
abort(); |
| 287 |
} |
| 288 |
return instructions_[key]; |
| 289 |
} |
| 290 |
|
| 291 |
bool FileTest::GetInstructionBytes(std::vector<uint8_t> *out, |
| 292 |
const std::string &key) { |
| 293 |
std::string value; |
| 294 |
return GetInstruction(&value, key) && ConvertToBytes(out, value); |
| 295 |
} |
| 296 |
|
| 297 |
const std::string &FileTest::CurrentTestToString() const { |
| 298 |
return current_test_; |
| 299 |
} |
| 300 |
|
| 301 |
bool FileTest::GetBytes(std::vector<uint8_t> *out, const std::string &key) { |
| 302 |
std::string value; |
| 303 |
return GetAttribute(&value, key) && ConvertToBytes(out, value); |
| 304 |
} |
| 305 |
|
| 306 |
void FileTest::ClearTest() { |
| 307 |
start_line_ = 0; |
| 308 |
type_.clear(); |
| 309 |
parameter_.clear(); |
| 310 |
attribute_count_.clear(); |
| 311 |
attributes_.clear(); |
| 312 |
unused_attributes_.clear(); |
| 313 |
unused_instructions_.clear(); |
| 314 |
current_test_ = ""; |
| 315 |
} |
| 316 |
|
| 317 |
void FileTest::ClearInstructions() { |
| 318 |
instructions_.clear(); |
| 319 |
unused_attributes_.clear(); |
| 320 |
} |
| 321 |
|
| 322 |
void FileTest::OnKeyUsed(const std::string &key) { |
| 323 |
unused_attributes_.erase(key); |
| 324 |
} |
| 325 |
|
| 326 |
void FileTest::OnInstructionUsed(const std::string &key) { |
| 327 |
unused_instructions_.erase(key); |
| 328 |
} |
| 329 |
|
| 330 |
bool FileTest::ConvertToBytes(std::vector<uint8_t> *out, |
| 331 |
const std::string &value) { |
| 332 |
if (value.size() >= 2 && value[0] == '"' && value[value.size() - 1] == '"') { |
| 333 |
out->assign(value.begin() + 1, value.end() - 1); |
| 334 |
return true; |
| 335 |
} |
| 336 |
|
| 337 |
if (!DecodeHex(out, value)) { |
| 338 |
PrintLine("Error decoding value: %s", value.c_str()); |
| 339 |
return false; |
| 340 |
} |
| 341 |
return true; |
| 342 |
} |
| 343 |
|
| 344 |
bool FileTest::IsAtNewInstructionBlock() const { |
| 345 |
return is_at_new_instruction_block_; |
| 346 |
} |
| 347 |
|
| 348 |
void FileTest::InjectInstruction(const std::string &key, |
| 349 |
const std::string &value) { |
| 350 |
instructions_[key] = value; |
| 351 |
} |
| 352 |
|
| 353 |
class FileLineReader : public FileTest::LineReader { |
| 354 |
public: |
| 355 |
explicit FileLineReader(const char *path) : file_(fopen(path, "r")) {} |
| 356 |
~FileLineReader() override { |
| 357 |
if (file_ != nullptr) { |
| 358 |
fclose(file_); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
// is_open returns true if the file was successfully opened. |
| 363 |
bool is_open() const { return file_ != nullptr; } |
| 364 |
|
| 365 |
FileTest::ReadResult ReadLine(char *out, size_t len) override { |
| 366 |
assert(len > 0); |
| 367 |
if (file_ == nullptr) { |
| 368 |
return FileTest::kReadError; |
| 369 |
} |
| 370 |
|
| 371 |
len = std::min(len, size_t{INT_MAX}); |
| 372 |
if (fgets(out, static_cast<int>(len), file_) == nullptr) { |
| 373 |
return feof(file_) ? FileTest::kReadEOF : FileTest::kReadError; |
| 374 |
} |
| 375 |
|
| 376 |
if (strlen(out) == len - 1 && out[len - 2] != '\n' && !feof(file_)) { |
| 377 |
fprintf(stderr, "Line too long.\n"); |
| 378 |
return FileTest::kReadError; |
| 379 |
} |
| 380 |
|
| 381 |
return FileTest::kReadSuccess; |
| 382 |
} |
| 383 |
|
| 384 |
private: |
| 385 |
FILE *file_; |
| 386 |
|
| 387 |
FileLineReader(const FileLineReader &) = delete; |
| 388 |
FileLineReader &operator=(const FileLineReader &) = delete; |
| 389 |
}; |
| 390 |
|
| 391 |
int FileTestMain(FileTestFunc run_test, void *arg, const char *path) { |
| 392 |
FileTest::Options opts; |
| 393 |
opts.callback = run_test; |
| 394 |
opts.arg = arg; |
| 395 |
opts.path = path; |
| 396 |
|
| 397 |
return FileTestMain(opts); |
| 398 |
} |
| 399 |
|
| 400 |
int FileTestMain(const FileTest::Options &opts) { |
| 401 |
std::unique_ptr<FileLineReader> reader( |
| 402 |
new FileLineReader(opts.path)); |
| 403 |
if (!reader->is_open()) { |
| 404 |
fprintf(stderr, "Could not open file %s: %s.\n", opts.path, |
| 405 |
strerror(errno)); |
| 406 |
return 1; |
| 407 |
} |
| 408 |
|
| 409 |
FileTest t(std::move(reader), opts.comment_callback, opts.is_kas_test); |
| 410 |
|
| 411 |
bool failed = false; |
| 412 |
while (true) { |
| 413 |
FileTest::ReadResult ret = t.ReadNext(); |
| 414 |
if (ret == FileTest::kReadError) { |
| 415 |
return 1; |
| 416 |
} else if (ret == FileTest::kReadEOF) { |
| 417 |
break; |
| 418 |
} |
| 419 |
|
| 420 |
bool result = opts.callback(&t, opts.arg); |
| 421 |
if (t.HasAttribute("Error")) { |
| 422 |
if (result) { |
| 423 |
t.PrintLine("Operation unexpectedly succeeded."); |
| 424 |
failed = true; |
| 425 |
continue; |
| 426 |
} |
| 427 |
uint32_t err = ERR_peek_error(); |
| 428 |
if (ERR_reason_error_string(err) != t.GetAttributeOrDie("Error")) { |
| 429 |
t.PrintLine("Unexpected error; wanted '%s', got '%s'.", |
| 430 |
t.GetAttributeOrDie("Error").c_str(), |
| 431 |
ERR_reason_error_string(err)); |
| 432 |
failed = true; |
| 433 |
ERR_clear_error(); |
| 434 |
continue; |
| 435 |
} |
| 436 |
ERR_clear_error(); |
| 437 |
} else if (!result) { |
| 438 |
// In case the test itself doesn't print output, print something so the |
| 439 |
// line number is reported. |
| 440 |
t.PrintLine("Test failed"); |
| 441 |
ERR_print_errors_fp(stderr); |
| 442 |
failed = true; |
| 443 |
continue; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
if (!opts.silent && !failed) { |
| 448 |
printf("PASS\n"); |
| 449 |
} |
| 450 |
|
| 451 |
return failed ? 1 : 0; |
| 452 |
} |
| 453 |
|
| 454 |
void FileTest::SkipCurrent() { |
| 455 |
ClearTest(); |
| 456 |
} |