From 454a780602c7c372d17be5032b25041013456642 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Wed, 29 Nov 2023 14:21:40 -0800 Subject: [PATCH] Internal change. PiperOrigin-RevId: 586452605 --- pkg/BUILD.bazel | 1 + src/google/protobuf/BUILD.bazel | 24 +++++ src/google/protobuf/text_format.h | 5 +- .../unredacted_debug_format_for_test.cc | 74 ++++++++++++++++ .../unredacted_debug_format_for_test.h | 33 +++++++ .../unredacted_debug_format_for_test_test.cc | 88 +++++++++++++++++++ 6 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 src/google/protobuf/unredacted_debug_format_for_test.cc create mode 100644 src/google/protobuf/unredacted_debug_format_for_test.h create mode 100644 src/google/protobuf/unredacted_debug_format_for_test_test.cc diff --git a/pkg/BUILD.bazel b/pkg/BUILD.bazel index 340fbaf453..55079a67e2 100644 --- a/pkg/BUILD.bazel +++ b/pkg/BUILD.bazel @@ -220,6 +220,7 @@ cc_dist_library( "//src/google/protobuf:lite_test_util", "//src/google/protobuf:test_util", "//src/google/protobuf:test_util2", + "//src/google/protobuf:unredacted_debug_format_for_test", "//src/google/protobuf/compiler:annotation_test_util", "//src/google/protobuf/compiler/cpp:unittest_lib", "//src/google/protobuf/io:test_zero_copy_stream", diff --git a/src/google/protobuf/BUILD.bazel b/src/google/protobuf/BUILD.bazel index 55eca1fe1d..03e8913c4a 100644 --- a/src/google/protobuf/BUILD.bazel +++ b/src/google/protobuf/BUILD.bazel @@ -1570,6 +1570,30 @@ cc_test( ], ) +cc_library( + name = "unredacted_debug_format_for_test", + testonly = True, + srcs = ["unredacted_debug_format_for_test.cc"], + hdrs = ["unredacted_debug_format_for_test.h"], + strip_include_prefix = "/src", + visibility = ["//visibility:public"], + deps = [ + ":protobuf", + ], +) + +cc_test( + name = "unredacted_debug_format_for_test_test", + srcs = ["unredacted_debug_format_for_test_test.cc"], + deps = [ + ":cc_lite_test_protos", + ":cc_test_protos", + ":protobuf", + ":unredacted_debug_format_for_test", + "@com_google_googletest//:gtest_main", + ], +) + ################################################################################ # Helper targets for Kotlin tests ################################################################################ diff --git a/src/google/protobuf/text_format.h b/src/google/protobuf/text_format.h index e16ed985d0..f80aefb713 100644 --- a/src/google/protobuf/text_format.h +++ b/src/google/protobuf/text_format.h @@ -66,7 +66,10 @@ PROTOBUF_EXPORT enum class FieldReporterLevel { kUtf8Format = 8, kDebugString = 12, kShortDebugString = 13, - kUtf8DebugString = 14 + kUtf8DebugString = 14, + kUnredactedDebugFormatForTest = 15, + kUnredactedShortDebugFormatForTest = 16, + kUnredactedUtf8DebugFormatForTest = 17 }; } // namespace internal diff --git a/src/google/protobuf/unredacted_debug_format_for_test.cc b/src/google/protobuf/unredacted_debug_format_for_test.cc new file mode 100644 index 0000000000..c598effd5f --- /dev/null +++ b/src/google/protobuf/unredacted_debug_format_for_test.cc @@ -0,0 +1,74 @@ +#include "google/protobuf/unredacted_debug_format_for_test.h" + +#include + +#include "google/protobuf/message.h" +#include "google/protobuf/message_lite.h" +#include "google/protobuf/text_format.h" + +namespace google { +namespace protobuf { +namespace util { + +std::string UnredactedDebugFormatForTest(const google::protobuf::Message& message) { + std::string debug_string; + + google::protobuf::TextFormat::Printer printer; + printer.SetExpandAny(true); + printer.SetReportSensitiveFields( + internal::FieldReporterLevel::kUnredactedDebugFormatForTest); + + printer.PrintToString(message, &debug_string); + + return debug_string; +} + +std::string UnredactedShortDebugFormatForTest(const google::protobuf::Message& message) { + std::string debug_string; + + google::protobuf::TextFormat::Printer printer; + printer.SetSingleLineMode(true); + printer.SetExpandAny(true); + printer.SetReportSensitiveFields( + internal::FieldReporterLevel::kUnredactedShortDebugFormatForTest); + + printer.PrintToString(message, &debug_string); + // Single line mode currently might have an extra space at the end. + if (!debug_string.empty() && debug_string[debug_string.size() - 1] == ' ') { + debug_string.resize(debug_string.size() - 1); + } + + return debug_string; +} + +std::string UnredactedUtf8DebugFormatForTest(const google::protobuf::Message& message) { + std::string debug_string; + + google::protobuf::TextFormat::Printer printer; + printer.SetUseUtf8StringEscaping(true); + printer.SetExpandAny(true); + printer.SetReportSensitiveFields( + internal::FieldReporterLevel::kUnredactedUtf8DebugFormatForTest); + + printer.PrintToString(message, &debug_string); + + return debug_string; +} + +std::string UnredactedDebugFormatForTest(const google::protobuf::MessageLite& message) { + return message.DebugString(); +} + +std::string UnredactedShortDebugFormatForTest( + const google::protobuf::MessageLite& message) { + return message.ShortDebugString(); +} + +std::string UnredactedUtf8DebugFormatForTest( + const google::protobuf::MessageLite& message) { + return message.Utf8DebugString(); +} + +} // namespace util +} // namespace protobuf +} // namespace google diff --git a/src/google/protobuf/unredacted_debug_format_for_test.h b/src/google/protobuf/unredacted_debug_format_for_test.h new file mode 100644 index 0000000000..252438ef9b --- /dev/null +++ b/src/google/protobuf/unredacted_debug_format_for_test.h @@ -0,0 +1,33 @@ +#ifndef GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__ +#define GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__ + +#include "google/protobuf/message.h" +#include "google/protobuf/message_lite.h" + +namespace google { +namespace protobuf { +namespace util { + +// Generates a human-readable form of this message for debugging purposes in +// test-only code. This API does not redact any fields in the message. +std::string UnredactedDebugFormatForTest(const google::protobuf::Message& message); +// Like UnredactedDebugFormatForTest(), but prints the message in a single line. +std::string UnredactedShortDebugFormatForTest(const google::protobuf::Message& message); +// Like UnredactedDebugFormatForTest(), but does not escape UTF-8 byte +// sequences. +std::string UnredactedUtf8DebugFormatForTest(const google::protobuf::Message& message); + +// The following APIs are added just to work with code that interoperates with +// `Message` and `MessageLite`. + +std::string UnredactedDebugFormatForTest(const google::protobuf::MessageLite& message); +std::string UnredactedShortDebugFormatForTest( + const google::protobuf::MessageLite& message); +std::string UnredactedUtf8DebugFormatForTest( + const google::protobuf::MessageLite& message); + +} // namespace util +} // namespace protobuf +} // namespace google + +#endif // GOOGLE_PROTOBUF_UNREDACTED_DEBUG_FORMAT_FOR_TEST_H__ diff --git a/src/google/protobuf/unredacted_debug_format_for_test_test.cc b/src/google/protobuf/unredacted_debug_format_for_test_test.cc new file mode 100644 index 0000000000..ceff5ba058 --- /dev/null +++ b/src/google/protobuf/unredacted_debug_format_for_test_test.cc @@ -0,0 +1,88 @@ +#include "google/protobuf/unredacted_debug_format_for_test.h" + +#include +#include +#include "google/protobuf/unittest.pb.h" +#include "google/protobuf/unittest_lite.pb.h" + +namespace { + +using ::google::protobuf::util::UnredactedDebugFormatForTest; +using ::google::protobuf::util::UnredactedShortDebugFormatForTest; +using ::google::protobuf::util::UnredactedUtf8DebugFormatForTest; +using ::testing::StrEq; + +TEST(UnredactedDebugFormatAPITest, MessageUnredactedDebugFormat) { + protobuf_unittest::RedactedFields proto; + protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; + protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; + redacted_nested_proto.set_optional_unredacted_nested_string( + "\350\260\267\346\255\214"); + unredacted_nested_proto.set_optional_unredacted_nested_string( + "\350\260\267\346\255\214"); + *proto.mutable_optional_redacted_message() = redacted_nested_proto; + *proto.mutable_optional_unredacted_message() = unredacted_nested_proto; + + EXPECT_THAT(UnredactedDebugFormatForTest(proto), + StrEq("optional_redacted_message {\n " + "optional_unredacted_nested_string: " + "\"\\350\\260\\267\\346\\255\\214\"\n}\n" + "optional_unredacted_message {\n " + "optional_unredacted_nested_string: " + "\"\\350\\260\\267\\346\\255\\214\"\n}\n")); +} + +TEST(UnredactedDebugFormatAPITest, MessageUnredactedShortDebugFormat) { + protobuf_unittest::RedactedFields proto; + protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; + protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; + redacted_nested_proto.set_optional_unredacted_nested_string("hello"); + unredacted_nested_proto.set_optional_unredacted_nested_string("world"); + *proto.mutable_optional_redacted_message() = redacted_nested_proto; + *proto.mutable_optional_unredacted_message() = unredacted_nested_proto; + + EXPECT_THAT(UnredactedShortDebugFormatForTest(proto), + StrEq("optional_redacted_message { " + "optional_unredacted_nested_string: \"hello\" } " + "optional_unredacted_message { " + "optional_unredacted_nested_string: \"world\" }")); +} + +TEST(UnredactedDebugFormatAPITest, MessageUnredactedUtf8DebugFormat) { + protobuf_unittest::RedactedFields proto; + protobuf_unittest::TestNestedMessageRedaction redacted_nested_proto; + protobuf_unittest::TestNestedMessageRedaction unredacted_nested_proto; + redacted_nested_proto.set_optional_unredacted_nested_string( + "\350\260\267\346\255\214"); + unredacted_nested_proto.set_optional_unredacted_nested_string( + "\350\260\267\346\255\214"); + *proto.mutable_optional_redacted_message() = redacted_nested_proto; + *proto.mutable_optional_unredacted_message() = unredacted_nested_proto; + + EXPECT_THAT(UnredactedUtf8DebugFormatForTest(proto), + StrEq("optional_redacted_message {\n " + "optional_unredacted_nested_string: " + "\"\xE8\xB0\xB7\xE6\xAD\x8C\"\n}\n" + "optional_unredacted_message {\n " + "optional_unredacted_nested_string: " + "\"\xE8\xB0\xB7\xE6\xAD\x8C\"\n}\n")); +} + +TEST(UnredactedDebugFormatAPITest, LiteUnredactedDebugFormat) { + protobuf_unittest::TestAllTypesLite message; + EXPECT_EQ(UnredactedDebugFormatForTest(message), message.DebugString()); +} + +TEST(UnredactedDebugFormatAPITest, LiteUnredactedShortDebugFormat) { + protobuf_unittest::TestAllTypesLite message; + EXPECT_EQ(UnredactedShortDebugFormatForTest(message), + message.ShortDebugString()); +} + +TEST(UnredactedDebugFormatAPITest, LiteUnredactedUtf8DebugFormat) { + protobuf_unittest::TestAllTypesLite message; + EXPECT_EQ(UnredactedUtf8DebugFormatForTest(message), + message.Utf8DebugString()); +} + +} // namespace