2015-05-10 03:46:47 +01:00
|
|
|
/* 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"
|
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
#include <algorithm>
|
2017-05-19 00:39:06 +01:00
|
|
|
#include <utility>
|
2016-02-24 18:04:31 +00:00
|
|
|
|
2017-05-19 00:39:06 +01:00
|
|
|
#include <assert.h>
|
2015-05-10 03:46:47 +01:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdarg.h>
|
2017-05-19 00:39:06 +01:00
|
|
|
#include <stdio.h>
|
2015-05-12 23:01:17 +01:00
|
|
|
#include <stdlib.h>
|
2015-05-10 03:46:47 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-05-11 20:58:08 +01:00
|
|
|
#include <openssl/err.h>
|
|
|
|
|
2016-12-13 06:07:13 +00:00
|
|
|
#include "../internal.h"
|
|
|
|
|
2015-05-10 03:46:47 +01:00
|
|
|
|
2017-05-30 23:37:27 +01:00
|
|
|
FileTest::FileTest(std::unique_ptr<FileTest::LineReader> reader,
|
2018-01-05 17:28:00 +00:00
|
|
|
std::function<void(const std::string &)> comment_callback,
|
|
|
|
bool is_kas_test)
|
2017-08-01 00:09:42 +01:00
|
|
|
: reader_(std::move(reader)),
|
2018-01-05 17:28:00 +00:00
|
|
|
is_kas_test_(is_kas_test),
|
2017-08-01 00:09:42 +01:00
|
|
|
comment_callback_(std::move(comment_callback)) {}
|
2015-05-10 03:46:47 +01:00
|
|
|
|
2017-05-19 00:39:06 +01:00
|
|
|
FileTest::~FileTest() {}
|
2015-05-10 03:46:47 +01:00
|
|
|
|
|
|
|
// 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--;
|
|
|
|
}
|
2017-04-24 21:29:11 +01:00
|
|
|
while (len > 0 && isspace(str[len - 1])) {
|
2015-05-10 03:46:47 +01:00
|
|
|
len--;
|
|
|
|
}
|
|
|
|
return std::string(str, len);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
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};
|
|
|
|
}
|
|
|
|
|
2015-05-10 03:46:47 +01:00
|
|
|
FileTest::ReadResult FileTest::ReadNext() {
|
2017-04-24 21:29:11 +01:00
|
|
|
// If the previous test had unused attributes or instructions, it is an error.
|
2017-05-19 00:02:21 +01:00
|
|
|
if (!unused_attributes_.empty()) {
|
2015-05-10 03:46:47 +01:00
|
|
|
for (const std::string &key : unused_attributes_) {
|
|
|
|
PrintLine("Unused attribute: %s", key.c_str());
|
|
|
|
}
|
|
|
|
return kReadError;
|
|
|
|
}
|
2017-05-19 00:02:21 +01:00
|
|
|
if (!unused_instructions_.empty()) {
|
2017-04-24 21:29:11 +01:00
|
|
|
for (const std::string &key : unused_instructions_) {
|
|
|
|
PrintLine("Unused instruction: %s", key.c_str());
|
|
|
|
}
|
|
|
|
return kReadError;
|
|
|
|
}
|
2015-05-10 03:46:47 +01:00
|
|
|
|
|
|
|
ClearTest();
|
|
|
|
|
2017-04-28 21:17:54 +01:00
|
|
|
static const size_t kBufLen = 8192 * 4;
|
2016-02-24 18:04:31 +00:00
|
|
|
std::unique_ptr<char[]> buf(new char[kBufLen]);
|
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
bool in_instruction_block = false;
|
2017-05-01 19:56:43 +01:00
|
|
|
is_at_new_instruction_block_ = false;
|
2017-04-24 21:29:11 +01:00
|
|
|
|
2015-05-10 03:46:47 +01:00
|
|
|
while (true) {
|
|
|
|
// Read the next line.
|
2017-05-19 00:39:06 +01:00
|
|
|
switch (reader_->ReadLine(buf.get(), kBufLen)) {
|
|
|
|
case kReadError:
|
|
|
|
fprintf(stderr, "Error reading from input at line %u.\n", line_ + 1);
|
|
|
|
return kReadError;
|
|
|
|
case kReadEOF:
|
2015-05-10 03:46:47 +01:00
|
|
|
// EOF is a valid terminator for a test.
|
|
|
|
return start_line_ > 0 ? kReadSuccess : kReadEOF;
|
2017-05-19 00:39:06 +01:00
|
|
|
case kReadSuccess:
|
|
|
|
break;
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
line_++;
|
2016-02-24 18:04:31 +00:00
|
|
|
size_t len = strlen(buf.get());
|
2017-04-24 21:29:11 +01:00
|
|
|
if (buf[0] == '\n' || buf[0] == '\r' || buf[0] == '\0') {
|
2015-05-10 03:46:47 +01:00
|
|
|
// Empty lines delimit tests.
|
|
|
|
if (start_line_ > 0) {
|
|
|
|
return kReadSuccess;
|
|
|
|
}
|
2017-04-24 21:29:11 +01:00
|
|
|
if (in_instruction_block) {
|
|
|
|
in_instruction_block = false;
|
|
|
|
// Delimit instruction block from test with a blank line.
|
|
|
|
current_test_ += "\r\n";
|
2018-01-05 17:28:00 +00:00
|
|
|
} else if (is_kas_test_) {
|
|
|
|
// KAS tests have random blank lines scattered around.
|
|
|
|
current_test_ += "\r\n";
|
2017-04-24 21:29:11 +01:00
|
|
|
}
|
2017-05-30 23:37:27 +01:00
|
|
|
} else if (buf[0] == '#') {
|
2018-01-05 17:28:00 +00:00
|
|
|
if (is_kas_test_ && seen_non_comment_) {
|
|
|
|
// KAS tests have comments after the initial comment block which need
|
|
|
|
// to be included in the corresponding place in the output.
|
|
|
|
current_test_ += std::string(buf.get());
|
|
|
|
} else if (comment_callback_) {
|
2017-05-30 23:37:27 +01:00
|
|
|
comment_callback_(buf.get());
|
|
|
|
}
|
|
|
|
// Otherwise ignore comments.
|
|
|
|
} else if (strcmp("[B.4.2 Key Pair Generation by Testing Candidates]\r\n",
|
2017-05-01 19:56:22 +01:00
|
|
|
buf.get()) == 0) {
|
2017-05-30 23:37:27 +01:00
|
|
|
// The above instruction-like line is ignored because the FIPS lab's
|
|
|
|
// request files are hopelessly inconsistent.
|
2017-04-24 21:29:11 +01:00
|
|
|
} else if (buf[0] == '[') { // Inside an instruction block.
|
2017-05-01 19:56:43 +01:00
|
|
|
is_at_new_instruction_block_ = true;
|
2018-01-05 17:28:00 +00:00
|
|
|
seen_non_comment_ = true;
|
2017-04-24 21:29:11 +01:00
|
|
|
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]").
|
2018-01-05 17:28:00 +00:00
|
|
|
|
|
|
|
// KAS tests contain invalid syntax.
|
|
|
|
std::string kv = buf.get();
|
|
|
|
const bool is_broken_kas_instruction =
|
|
|
|
is_kas_test_ &&
|
|
|
|
(kv == "[SHA(s) supported (Used for hashing Z): SHA512 \r\n");
|
|
|
|
|
|
|
|
if (!is_broken_kas_instruction) {
|
|
|
|
kv = StripSpace(buf.get(), len);
|
|
|
|
if (kv[kv.size() - 1] != ']') {
|
|
|
|
fprintf(stderr, "Line %u, invalid instruction: '%s'\n", line_,
|
|
|
|
kv.c_str());
|
|
|
|
return kReadError;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Just remove the newline for the broken instruction.
|
|
|
|
kv = kv.substr(0, kv.size() - 2);
|
2017-04-24 21:29:11 +01:00
|
|
|
}
|
2018-01-05 17:28:00 +00:00
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
current_test_ += kv + "\r\n";
|
|
|
|
kv = std::string(kv.begin() + 1, kv.end() - 1);
|
|
|
|
|
2017-04-28 22:41:28 +01:00
|
|
|
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);
|
|
|
|
}
|
2017-05-01 19:56:22 +01:00
|
|
|
} else {
|
2017-05-01 19:56:43 +01:00
|
|
|
// Parsing a test case.
|
2017-04-24 21:29:11 +01:00
|
|
|
if (in_instruction_block) {
|
2017-05-01 19:56:43 +01:00
|
|
|
// 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;
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
2017-04-24 21:29:11 +01:00
|
|
|
|
|
|
|
current_test_ += std::string(buf.get(), len);
|
|
|
|
std::string key, value;
|
|
|
|
std::tie(key, value) = ParseKeyValue(buf.get(), len);
|
2015-05-10 03:46:47 +01:00
|
|
|
|
2017-05-01 18:54:03 +01:00
|
|
|
// 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;
|
2015-05-10 03:46:47 +01:00
|
|
|
if (start_line_ == 0) {
|
|
|
|
// This is the start of a test.
|
2017-05-01 18:54:03 +01:00
|
|
|
type_ = mapped_key;
|
2015-05-10 03:46:47 +01:00
|
|
|
parameter_ = value;
|
|
|
|
start_line_ = line_;
|
2017-04-24 21:29:11 +01:00
|
|
|
for (const auto &kv : instructions_) {
|
|
|
|
unused_instructions_.insert(kv.first);
|
|
|
|
}
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-05-11 20:58:08 +01:00
|
|
|
const std::string &FileTest::GetAttributeOrDie(const std::string &key) {
|
|
|
|
if (!HasAttribute(key)) {
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
return attributes_[key];
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2015-05-10 03:46:47 +01:00
|
|
|
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;
|
|
|
|
}
|
2016-01-01 06:17:30 +00:00
|
|
|
out->clear();
|
2015-05-10 03:46:47 +01:00
|
|
|
out->reserve(value.size() / 2);
|
|
|
|
for (size_t i = 0; i < value.size(); i += 2) {
|
|
|
|
uint8_t hi, lo;
|
2017-04-24 21:29:11 +01:00
|
|
|
if (!FromHexDigit(&hi, value[i]) || !FromHexDigit(&lo, value[i + 1])) {
|
2015-05-10 03:46:47 +01:00
|
|
|
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 &&
|
2016-12-13 06:07:13 +00:00
|
|
|
OPENSSL_memcmp(expected, actual, expected_len) == 0) {
|
2015-05-10 03:46:47 +01:00
|
|
|
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();
|
2017-04-24 21:29:11 +01:00
|
|
|
current_test_ = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileTest::ClearInstructions() {
|
|
|
|
instructions_.clear();
|
|
|
|
unused_attributes_.clear();
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FileTest::OnKeyUsed(const std::string &key) {
|
|
|
|
unused_attributes_.erase(key);
|
|
|
|
}
|
|
|
|
|
2017-04-24 21:29:11 +01:00
|
|
|
void FileTest::OnInstructionUsed(const std::string &key) {
|
|
|
|
unused_instructions_.erase(key);
|
|
|
|
}
|
|
|
|
|
2017-05-01 19:56:43 +01:00
|
|
|
bool FileTest::IsAtNewInstructionBlock() const {
|
|
|
|
return is_at_new_instruction_block_;
|
|
|
|
}
|
|
|
|
|
2017-05-01 23:31:43 +01:00
|
|
|
void FileTest::InjectInstruction(const std::string &key,
|
|
|
|
const std::string &value) {
|
|
|
|
instructions_[key] = value;
|
|
|
|
}
|
|
|
|
|
2017-05-19 00:39:06 +01:00
|
|
|
class FileLineReader : public FileTest::LineReader {
|
|
|
|
public:
|
|
|
|
explicit FileLineReader(const char *path) : file_(fopen(path, "r")) {}
|
|
|
|
~FileLineReader() override {
|
|
|
|
if (file_ != nullptr) {
|
|
|
|
fclose(file_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// is_open returns true if the file was successfully opened.
|
|
|
|
bool is_open() const { return file_ != nullptr; }
|
|
|
|
|
|
|
|
FileTest::ReadResult ReadLine(char *out, size_t len) override {
|
|
|
|
assert(len > 0);
|
|
|
|
if (file_ == nullptr) {
|
|
|
|
return FileTest::kReadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fgets(out, len, file_) == nullptr) {
|
|
|
|
return feof(file_) ? FileTest::kReadEOF : FileTest::kReadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(out) == len - 1 && out[len - 2] != '\n' && !feof(file_)) {
|
|
|
|
fprintf(stderr, "Line too long.\n");
|
|
|
|
return FileTest::kReadError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FileTest::kReadSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FILE *file_;
|
|
|
|
|
|
|
|
FileLineReader(const FileLineReader &) = delete;
|
|
|
|
FileLineReader &operator=(const FileLineReader &) = delete;
|
|
|
|
};
|
|
|
|
|
2017-05-30 23:37:27 +01:00
|
|
|
int FileTestMain(FileTestFunc run_test, void *arg, const char *path) {
|
|
|
|
FileTest::Options opts;
|
|
|
|
opts.callback = run_test;
|
|
|
|
opts.arg = arg;
|
|
|
|
opts.path = path;
|
|
|
|
|
|
|
|
return FileTestMain(opts);
|
|
|
|
}
|
|
|
|
|
|
|
|
int FileTestMain(const FileTest::Options &opts) {
|
|
|
|
std::unique_ptr<FileLineReader> reader(
|
|
|
|
new FileLineReader(opts.path));
|
2017-05-19 00:39:06 +01:00
|
|
|
if (!reader->is_open()) {
|
2017-05-30 23:37:27 +01:00
|
|
|
fprintf(stderr, "Could not open file %s: %s.\n", opts.path,
|
|
|
|
strerror(errno));
|
2015-05-10 03:46:47 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2018-01-05 17:28:00 +00:00
|
|
|
FileTest t(std::move(reader), opts.comment_callback, opts.is_kas_test);
|
2017-05-30 23:37:27 +01:00
|
|
|
|
2015-05-10 03:46:47 +01:00
|
|
|
bool failed = false;
|
|
|
|
while (true) {
|
|
|
|
FileTest::ReadResult ret = t.ReadNext();
|
|
|
|
if (ret == FileTest::kReadError) {
|
|
|
|
return 1;
|
|
|
|
} else if (ret == FileTest::kReadEOF) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:37:27 +01:00
|
|
|
bool result = opts.callback(&t, opts.arg);
|
2015-05-11 20:58:08 +01:00
|
|
|
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'.",
|
2017-04-24 21:29:11 +01:00
|
|
|
t.GetAttributeOrDie("Error").c_str(),
|
|
|
|
ERR_reason_error_string(err));
|
2015-05-11 20:58:08 +01:00
|
|
|
failed = true;
|
2016-01-01 06:17:30 +00:00
|
|
|
ERR_clear_error();
|
2015-05-11 20:58:08 +01:00
|
|
|
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);
|
2015-05-10 03:46:47 +01:00
|
|
|
failed = true;
|
2015-05-11 20:58:08 +01:00
|
|
|
continue;
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-30 23:37:27 +01:00
|
|
|
if (!opts.silent && !failed) {
|
2017-04-24 21:29:11 +01:00
|
|
|
printf("PASS\n");
|
|
|
|
}
|
2017-05-30 23:37:27 +01:00
|
|
|
|
|
|
|
return failed ? 1 : 0;
|
2015-05-10 03:46:47 +01:00
|
|
|
}
|
2017-06-26 18:16:50 +01:00
|
|
|
|
|
|
|
void FileTest::SkipCurrent() {
|
|
|
|
ClearTest();
|
|
|
|
}
|