3ecd0a5fca
This introduces machinery to start embedding the test data files into the crypto_test binary. Figuring out every CI's test data story is more trouble than is worth it. The GTest FileTest runner is considerably different from the old one: - It returns void and expects failures to use the GTest EXPECT_* and ASSERT_* macros, rather than ExpectBytesEqual. This is more monkey work to convert, but ultimately less work to add new tests. I think it's also valuable for our FileTest and normal test patterns to align as much as possible. The line number is emitted via SCOPED_TRACE. - I've intentionally omitted the Error attribute handling, since that doesn't work very well with the new callback. This means evp_test.cc will take a little more work to convert, but this is again to keep our two test patterns aligned. - The callback takes a std::function rather than a C-style void pointer. This means we can go nuts with lambdas. It also places the path first so clang-format doesn't go nuts. BUG=129 Change-Id: I0d1920a342b00e64043e3ea05f5f5af57bfe77b3 Reviewed-on: https://boringssl-review.googlesource.com/16507 Reviewed-by: Adam Langley <agl@google.com>
86 lines
2.4 KiB
C++
86 lines
2.4 KiB
C++
/* Copyright (c) 2017, 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 <assert.h>
|
|
#include <string.h>
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
std::string GetTestData(const char *path);
|
|
|
|
class StringLineReader : public FileTest::LineReader {
|
|
public:
|
|
explicit StringLineReader(const std::string &data)
|
|
: data_(data), offset_(0) {}
|
|
|
|
FileTest::ReadResult ReadLine(char *out, size_t len) override {
|
|
assert(len > 0);
|
|
if (offset_ == data_.size()) {
|
|
return FileTest::kReadEOF;
|
|
}
|
|
|
|
size_t idx = data_.find('\n', offset_);
|
|
if (idx == std::string::npos) {
|
|
idx = data_.size();
|
|
} else {
|
|
idx++; // Include the newline.
|
|
}
|
|
|
|
if (idx - offset_ > len - 1) {
|
|
ADD_FAILURE() << "Line too long.";
|
|
return FileTest::kReadError;
|
|
}
|
|
|
|
memcpy(out, data_.data() + offset_, idx - offset_);
|
|
out[idx - offset_] = '\0';
|
|
offset_ = idx;
|
|
return FileTest::kReadSuccess;
|
|
}
|
|
|
|
private:
|
|
std::string data_;
|
|
size_t offset_;
|
|
|
|
StringLineReader(const StringLineReader &) = delete;
|
|
StringLineReader &operator=(const StringLineReader &) = delete;
|
|
};
|
|
|
|
void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test) {
|
|
std::unique_ptr<StringLineReader> reader(
|
|
new StringLineReader(GetTestData(path)));
|
|
FileTest t(std::move(reader));
|
|
|
|
while (true) {
|
|
switch (t.ReadNext()) {
|
|
case FileTest::kReadError:
|
|
ADD_FAILURE() << "Error reading test.";
|
|
return;
|
|
case FileTest::kReadEOF:
|
|
return;
|
|
case FileTest::kReadSuccess:
|
|
break;
|
|
}
|
|
|
|
SCOPED_TRACE(testing::Message() << path << ", line " << t.start_line());
|
|
run_test(&t);
|
|
}
|
|
}
|