fuzzer_util_test.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. << " couldn't parse text proto in " << f;
  27. ParseAndExecute(carbon_proto.compilation_unit());
  28. ++file_count;
  29. }
  30. EXPECT_GT(file_count, 0);
  31. }
  32. TEST(FuzzerUtilTest, GetRunfilesFile) {
  33. EXPECT_THAT(*Internal::GetRunfilesFile(
  34. "carbon/explorer/fuzzing/fuzzer_corpus/empty.textproto"),
  35. testing::EndsWith("/empty.textproto"));
  36. EXPECT_THAT(Internal::GetRunfilesFile("nonexistent-file").error().message(),
  37. testing::EndsWith("doesn't exist"));
  38. }
  39. } // namespace
  40. } // namespace Carbon::Testing
  41. auto main(int argc, char** argv) -> int {
  42. ::testing::InitGoogleTest(&argc, argv);
  43. Carbon::Testing::carbon_files =
  44. new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
  45. return RUN_ALL_TESTS();
  46. }