You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

108 lines
3.0 KiB

  1. /* Copyright (c) 2017, 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. #include "file_test.h"
  15. #include <assert.h>
  16. #include <string.h>
  17. #include <memory>
  18. #include <string>
  19. #include <utility>
  20. #include <gtest/gtest.h>
  21. #include <openssl/err.h>
  22. std::string GetTestData(const char *path);
  23. class StringLineReader : public FileTest::LineReader {
  24. public:
  25. explicit StringLineReader(const std::string &data)
  26. : data_(data), offset_(0) {}
  27. FileTest::ReadResult ReadLine(char *out, size_t len) override {
  28. assert(len > 0);
  29. if (offset_ == data_.size()) {
  30. return FileTest::kReadEOF;
  31. }
  32. size_t idx = data_.find('\n', offset_);
  33. if (idx == std::string::npos) {
  34. idx = data_.size();
  35. } else {
  36. idx++; // Include the newline.
  37. }
  38. if (idx - offset_ > len - 1) {
  39. ADD_FAILURE() << "Line too long.";
  40. return FileTest::kReadError;
  41. }
  42. memcpy(out, data_.data() + offset_, idx - offset_);
  43. out[idx - offset_] = '\0';
  44. offset_ = idx;
  45. return FileTest::kReadSuccess;
  46. }
  47. private:
  48. std::string data_;
  49. size_t offset_;
  50. StringLineReader(const StringLineReader &) = delete;
  51. StringLineReader &operator=(const StringLineReader &) = delete;
  52. };
  53. void FileTestGTest(const char *path, std::function<void(FileTest *)> run_test) {
  54. std::unique_ptr<StringLineReader> reader(
  55. new StringLineReader(GetTestData(path)));
  56. FileTest t(std::move(reader), nullptr, false);
  57. while (true) {
  58. switch (t.ReadNext()) {
  59. case FileTest::kReadError:
  60. ADD_FAILURE() << "Error reading test.";
  61. return;
  62. case FileTest::kReadEOF:
  63. return;
  64. case FileTest::kReadSuccess:
  65. break;
  66. }
  67. const testing::TestResult *test_result =
  68. testing::UnitTest::GetInstance()->current_test_info()->result();
  69. int before_part_count = test_result->total_part_count();
  70. SCOPED_TRACE(testing::Message() << path << ", line " << t.start_line());
  71. run_test(&t);
  72. // Check for failures from the most recent test.
  73. bool failed = false;
  74. for (int i = before_part_count; i < test_result->total_part_count(); i++) {
  75. if (test_result->GetTestPartResult(i).failed()) {
  76. failed = true;
  77. break;
  78. }
  79. }
  80. // Clean up the error queue for the next test, reporting it on failure.
  81. if (failed) {
  82. ERR_print_errors_fp(stdout);
  83. } else {
  84. ERR_clear_error();
  85. }
  86. }
  87. }