fuzzer_util_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. TEST(FuzzerUtilTest, ParseAndExecute) {
  13. const ErrorOr<Fuzzing::Carbon> carbon_proto = ParseCarbonTextProto(R"(
  14. compilation_unit {
  15. package_statement { package_name: "P" }
  16. is_api: true
  17. declarations {
  18. function {
  19. name {
  20. name: "Main"
  21. }
  22. param_pattern {}
  23. return_term {
  24. kind: Expression
  25. type { int_type_literal {} }
  26. }
  27. body {
  28. statements {
  29. return_expression_statement {
  30. expression { int_literal { value: 0 } }
  31. }
  32. }
  33. }
  34. }
  35. }
  36. })");
  37. ASSERT_TRUE(carbon_proto.ok());
  38. const ErrorOr<int> result = ParseAndExecute(carbon_proto->compilation_unit());
  39. ASSERT_TRUE(result.ok()) << "Execution failed: " << result.error();
  40. EXPECT_EQ(*result, 0);
  41. }
  42. TEST(FuzzerUtilTest, GetRunfilesFile) {
  43. EXPECT_THAT(*Internal::GetRunfilesFile("carbon/explorer/data/prelude.carbon"),
  44. testing::EndsWith("/prelude.carbon"));
  45. EXPECT_THAT(Internal::GetRunfilesFile("nonexistent-file").error().message(),
  46. testing::EndsWith("doesn't exist"));
  47. }
  48. TEST(FuzzerUtilTest, ParseCarbonTextProtoWithUnknownField) {
  49. const ErrorOr<Fuzzing::Carbon> carbon_proto =
  50. ParseCarbonTextProto(R"(
  51. compilation_unit {
  52. garbage: "value"
  53. declarations {
  54. choice {
  55. name {
  56. name: "Ch"
  57. }
  58. }
  59. }
  60. })",
  61. /*allow_unknown=*/true);
  62. ASSERT_TRUE(carbon_proto.ok());
  63. // No EqualsProto in gmock - https://github.com/google/googletest/issues/1761.
  64. EXPECT_EQ(
  65. carbon_proto->compilation_unit().declarations(0).choice().name().name(),
  66. "Ch");
  67. }
  68. } // namespace
  69. } // namespace Carbon::Testing