fuzzer_util_test.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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("/explorer/fuzzing/fuzzer_corpus/"
  35. "empty.textproto"));
  36. EXPECT_THAT(Internal::GetRunfilesFile(
  37. "explorer/fuzzing/fuzzer_corpus/empty.textproto")
  38. .error()
  39. .message(),
  40. testing::EndsWith("fuzzer_util_test.runfiles/explorer/fuzzing/"
  41. "fuzzer_corpus/empty.textproto doesn't exist"));
  42. }
  43. } // namespace
  44. } // namespace Carbon::Testing
  45. auto main(int argc, char** argv) -> int {
  46. ::testing::InitGoogleTest(&argc, argv);
  47. Carbon::Testing::carbon_files =
  48. new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
  49. return RUN_ALL_TESTS();
  50. }