fuzzer_util_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <gtest/gtest.h>
  7. #include <fstream>
  8. #include "llvm/ADT/StringRef.h"
  9. #include "llvm/Support/raw_ostream.h"
  10. namespace Carbon::Testing {
  11. namespace {
  12. static std::vector<llvm::StringRef>* carbon_files = nullptr;
  13. // A workaround for https://github.com/carbon-language/carbon-lang/issues/1208.
  14. TEST(FuzzerUtilTest, RunFuzzerOnCorpus) {
  15. int file_count = 0;
  16. for (const llvm::StringRef f : *carbon_files) {
  17. llvm::outs() << "Processing " << f << "\n";
  18. std::ifstream file(f.str(), std::ios::in);
  19. ASSERT_TRUE(file.is_open());
  20. std::stringstream contents;
  21. contents << file.rdbuf();
  22. const ErrorOr<Fuzzing::Carbon> carbon_proto =
  23. ParseCarbonTextProto(contents.str());
  24. ASSERT_TRUE(carbon_proto.ok()) << "couldn't parse text proto in " << f;
  25. ParseAndExecute(carbon_proto->compilation_unit());
  26. ++file_count;
  27. }
  28. EXPECT_GT(file_count, 0);
  29. }
  30. TEST(FuzzerUtilTest, GetRunfilesFile) {
  31. EXPECT_THAT(*Internal::GetRunfilesFile(
  32. "carbon/explorer/fuzzing/fuzzer_corpus/empty.textproto"),
  33. testing::EndsWith("/empty.textproto"));
  34. EXPECT_THAT(Internal::GetRunfilesFile("nonexistent-file").error().message(),
  35. testing::EndsWith("doesn't exist"));
  36. }
  37. TEST(FuzzerUtilTest, ParseCarbonTextProtoWithUnknownField) {
  38. const ErrorOr<Fuzzing::Carbon> carbon_proto =
  39. ParseCarbonTextProto(R"(
  40. compilation_unit {
  41. garbage: "value"
  42. declarations {
  43. choice {
  44. name: "Ch"
  45. }
  46. }
  47. })",
  48. /*allow_unknown=*/true);
  49. ASSERT_TRUE(carbon_proto.ok());
  50. // No EqualsProto in gmock - https://github.com/google/googletest/issues/1761.
  51. EXPECT_EQ(carbon_proto->compilation_unit().declarations(0).choice().name(),
  52. "Ch");
  53. }
  54. } // namespace
  55. } // namespace Carbon::Testing
  56. auto main(int argc, char** argv) -> int {
  57. ::testing::InitGoogleTest(&argc, argv);
  58. Carbon::Testing::carbon_files =
  59. new std::vector<llvm::StringRef>(&argv[1], &argv[argc]);
  60. return RUN_ALL_TESTS();
  61. }