Properly convert proto to text `ProtobufPrinter` used to `absl::StrCat` protos into code snippets, which is intentionally unparseable. This CL replaces that with a proper proto -> text conversion. PiperOrigin-RevId: 625803909
diff --git a/fuzztest/BUILD b/fuzztest/BUILD index 38990c3..4ffc4f3 100644 --- a/fuzztest/BUILD +++ b/fuzztest/BUILD
@@ -721,6 +721,7 @@ "@com_google_absl//absl/strings", "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/time", + "@com_google_protobuf//:protobuf", ], ) @@ -738,5 +739,6 @@ "@com_google_absl//absl/strings:str_format", "@com_google_absl//absl/time", "@com_google_googletest//:gtest_main", + "@com_google_protobuf//:protobuf", ], )
diff --git a/fuzztest/internal/type_support.h b/fuzztest/internal/type_support.h index d068992..88804af 100644 --- a/fuzztest/internal/type_support.h +++ b/fuzztest/internal/type_support.h
@@ -31,6 +31,7 @@ #include "absl/strings/escaping.h" #include "absl/strings/has_absl_stringify.h" #include "absl/strings/match.h" +#include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" #include "absl/strings/strip.h" @@ -38,6 +39,7 @@ #include "./fuzztest/internal/domains/absl_helpers.h" #include "./fuzztest/internal/meta.h" #include "./fuzztest/internal/printer.h" +#include "google/protobuf/text_format.h" namespace fuzztest::internal { @@ -255,13 +257,18 @@ return PrintUserValue(*val, out, mode); } else { static constexpr absl::string_view kProtoParser = "ParseTestProto"; + std::string textproto; + if (!google::protobuf::TextFormat::PrintToString(val, &textproto)) { + // Fall-back to debug printing, which is on purpose not parseable but at + // least gives some idea about the contents of the proto. + textproto = absl::StrCat(val); + } switch (mode) { case domain_implementor::PrintMode::kHumanReadable: - absl::Format(out, "(%s)", absl::StrCat(val)); + absl::Format(out, "(%s)", textproto); break; case domain_implementor::PrintMode::kSourceCode: - absl::Format(out, "%s(R\"pb(%s)pb\")", kProtoParser, - absl::StrCat(val)); + absl::Format(out, "%s(R\"pb(%s)pb\")", kProtoParser, textproto); break; } }
diff --git a/fuzztest/internal/type_support_test.cc b/fuzztest/internal/type_support_test.cc index 354c6a3..286d1b6 100644 --- a/fuzztest/internal/type_support_test.cc +++ b/fuzztest/internal/type_support_test.cc
@@ -45,6 +45,7 @@ #include "./fuzztest/internal/meta.h" #include "./fuzztest/internal/printer.h" #include "./fuzztest/internal/test_protobuf.pb.h" +#include "google/protobuf/text_format.h" namespace fuzztest::internal { namespace { @@ -228,11 +229,13 @@ internal::TestProtobuf proto; proto.set_b(true); proto.add_rep_subproto()->set_subproto_i32(17); + std::string proto_text; + ASSERT_TRUE(google::protobuf::TextFormat::PrintToString(proto, &proto_text)); EXPECT_THAT(TestPrintValue(proto), - ElementsAre(absl::StrCat("(", proto, ")"), + ElementsAre(absl::StrCat("(", proto_text, ")"), MatchesRegex(absl::StrCat( - R"re(.*ParseTe[sx]tProto.*\(R"pb\()re", proto, - R"re(\)pb"\))re")))); + R"re(.*ParseTe[sx]tProto.*\(R"pb\()re", + proto_text, R"re(\)pb"\))re")))); } TEST(ProtobufEnumTest, Printer) {