fuzzer_util_test.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "explorer/fuzzing/fuzzer_util.h"
  5. #include <gmock/gmock.h>
  6. #include <google/protobuf/text_format.h>
  7. #include <gtest/gtest.h>
  8. #include <fstream>
  9. #include "llvm/ADT/StringRef.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. static std::vector<llvm::StringRef>* carbon_files = nullptr;
  14. // A workaround for https://github.com/carbon-language/carbon-lang/issues/1208.
  15. TEST(FuzzerUtilTest, RunFuzzerOnCorpus) {
  16. int file_count = 0;
  17. for (const llvm::StringRef f : *carbon_files) {
  18. llvm::outs() << "Processing " << f << "\n";
  19. std::ifstream file(f.str(), std::ios::in);
  20. ASSERT_TRUE(file.is_open());
  21. std::stringstream contents;
  22. contents << file.rdbuf();
  23. Fuzzing::Carbon carbon_proto;
  24. ASSERT_TRUE(google::protobuf::TextFormat::ParseFromString(contents.str(),
  25. &carbon_proto));
  26. ParseAndExecute(carbon_proto.compilation_unit());
  27. ++file_count;
  28. }
  29. EXPECT_GT(file_count, 0);
  30. }
  31. TEST(FuzzerUtilTest, GetRunfilesFile) {
  32. EXPECT_THAT(*Internal::GetRunfilesFile(
  33. "carbon/explorer/fuzzing/fuzzer_corpus/empty.textproto"),
  34. testing::EndsWith("/empty.textproto"));
  35. EXPECT_THAT(Internal::GetRunfilesFile("nonexistent-file").error().message(),
  36. testing::EndsWith("doesn't exist"));
  37. }
  38. } // namespace
  39. } // namespace Carbon::Testing
  40. auto main(int argc, char** argv) -> int {
  41. ::testing::InitGoogleTest(&argc, argv);
  42. Carbon::Testing::carbon_files =
  43. new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
  44. return RUN_ALL_TESTS();
  45. }