@@ -76,7 +76,6 @@ class UnsetFieldsMetadataTextFormatTestUtil {
7676// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
7777namespace text_format_unittest {
7878
79- using ::google::protobuf::internal::kDebugStringSilentMarker ;
8079using ::google::protobuf::internal::UnsetFieldsMetadataTextFormatTestUtil;
8180using ::testing::AllOf;
8281using ::testing::HasSubstr;
@@ -94,10 +93,16 @@ constexpr absl::string_view kEscapeTestStringEscaped =
9493
9594constexpr absl::string_view value_replacement = " \\ [REDACTED\\ ]" ;
9695
96+ constexpr absl::string_view kTextMarkerRegex = " goo\\ .gle/.+ +" ;
97+
9798class TextFormatTestBase : public testing ::Test {
9899 public:
99100 void SetUp () override {
100- single_line_debug_format_prefix_ = " " ;
101+ // DebugString APIs insert a per-process randomized
102+ // prefix. Here we obtain the prefixes by calling DebugString APIs on an
103+ // empty proto. Note that Message::ShortDebugString() trims the last empty
104+ // space so we have to add it back.
105+ single_line_debug_format_prefix_ = proto_.ShortDebugString () + " " ;
101106 multi_line_debug_format_prefix_ = proto_.DebugString ();
102107 }
103108
@@ -210,6 +215,7 @@ TEST_F(TextFormatTest, ShortFormat) {
210215 std::string value_replacement = " \\ [REDACTED\\ ]" ;
211216 EXPECT_THAT (google::protobuf::ShortFormat (proto),
212217 testing::MatchesRegex (absl::Substitute (
218+ " $1"
213219 " optional_redacted_string: $0 "
214220 " optional_unredacted_string: \" bar\" "
215221 " repeated_redacted_string: $0 "
@@ -226,7 +232,7 @@ TEST_F(TextFormatTest, ShortFormat) {
226232 " \\ { optional_unredacted_nested_string: \" 8\" \\ } "
227233 " map_redacted_string: $0 "
228234 " map_unredacted_string \\ { key: \" ghi\" value: \" jkl\" \\ }" ,
229- value_replacement)));
235+ value_replacement, kTextMarkerRegex )));
230236}
231237
232238TEST_F (TextFormatTest, Utf8Format) {
@@ -262,6 +268,7 @@ TEST_F(TextFormatTest, Utf8Format) {
262268
263269 EXPECT_THAT (google::protobuf::Utf8Format (proto),
264270 testing::MatchesRegex (absl::Substitute (
271+ " $1\n "
265272 " optional_redacted_string: $0\n "
266273 " optional_unredacted_string: \" bar\"\n "
267274 " repeated_redacted_string: $0\n "
@@ -280,7 +287,7 @@ TEST_F(TextFormatTest, Utf8Format) {
280287 " map_redacted_string: $0\n "
281288 " map_unredacted_string \\ {\n "
282289 " key: \" ghi\"\n value: \" jkl\"\n\\ }\n " ,
283- value_replacement)));
290+ value_replacement, kTextMarkerRegex )));
284291}
285292
286293TEST_F (TextFormatTest, ShortPrimitiveRepeateds) {
@@ -433,6 +440,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) {
433440 message_text);
434441
435442 EXPECT_THAT (absl::StrCat (message), testing::MatchesRegex (absl::Substitute (
443+ " $1\n "
436444 " 5: UNKNOWN_VARINT $0\n "
437445 " 5: UNKNOWN_FIXED32 $0\n "
438446 " 5: UNKNOWN_FIXED64 $0\n "
@@ -441,7 +449,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) {
441449 " 8: UNKNOWN_VARINT $0\n "
442450 " 8: UNKNOWN_VARINT $0\n "
443451 " 8: UNKNOWN_VARINT $0\n " ,
444- value_replacement)));
452+ value_replacement, kTextMarkerRegex )));
445453}
446454
447455TEST_F (TextFormatTest, PrintUnknownFieldsDeepestStackWorks) {
@@ -2679,6 +2687,13 @@ TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
26792687 EXPECT_FALSE (parser.ParseFromString (" unknown_field: 1" , &proto));
26802688}
26812689
2690+ TEST (AbslStringifyTest, DebugStringIsTheSame) {
2691+ unittest::TestAllTypes proto;
2692+ proto.set_optional_int32 (1 );
2693+ proto.set_optional_string (" foo" );
2694+
2695+ EXPECT_THAT (proto.DebugString (), absl::StrCat (proto));
2696+ }
26822697
26832698TEST (AbslStringifyTest, TextFormatIsUnchanged) {
26842699 unittest::TestAllTypes proto;
@@ -2698,34 +2713,40 @@ TEST(AbslStringifyTest, StringifyHasRedactionMarker) {
26982713 proto.set_optional_int32 (1 );
26992714 proto.set_optional_string (" foo" );
27002715
2701- EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (
2716+ EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2717+ " $0\n "
27022718 " optional_int32: 1\n "
2703- " optional_string: \" foo\"\n " ));
2719+ " optional_string: \" foo\"\n " ,
2720+ kTextMarkerRegex )));
27042721}
27052722
27062723
27072724TEST (AbslStringifyTest, StringifyMetaAnnotatedIsRedacted) {
27082725 unittest::TestRedactedMessage proto;
27092726 proto.set_meta_annotated (" foo" );
27102727 EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2711- " meta_annotated: $0\n " ,
2712- value_replacement)));
2728+ " $0\n "
2729+ " meta_annotated: $1\n " ,
2730+ kTextMarkerRegex , value_replacement)));
27132731}
27142732
27152733TEST (AbslStringifyTest, StringifyRepeatedMetaAnnotatedIsRedacted) {
27162734 unittest::TestRedactedMessage proto;
27172735 proto.set_repeated_meta_annotated (" foo" );
27182736 EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2719- " repeated_meta_annotated: $0\n " ,
2720- value_replacement)));
2737+ " $0\n "
2738+ " repeated_meta_annotated: $1\n " ,
2739+ kTextMarkerRegex , value_replacement)));
27212740}
27222741
27232742TEST (AbslStringifyTest, StringifyRepeatedMetaAnnotatedIsNotRedacted) {
27242743 unittest::TestRedactedMessage proto;
27252744 proto.set_unredacted_repeated_annotations (" foo" );
27262745 EXPECT_THAT (absl::StrCat (proto),
27272746 testing::MatchesRegex (
2728- " unredacted_repeated_annotations: \" foo\"\n " ));
2747+ absl::Substitute (" $0\n "
2748+ " unredacted_repeated_annotations: \" foo\"\n " ,
2749+ kTextMarkerRegex )));
27292750}
27302751
27312752TEST (AbslStringifyTest, TextFormatMetaAnnotatedIsNotRedacted) {
@@ -2739,23 +2760,26 @@ TEST(AbslStringifyTest, StringifyDirectMessageEnumIsRedacted) {
27392760 unittest::TestRedactedMessage proto;
27402761 proto.set_test_direct_message_enum (" foo" );
27412762 EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2742- " test_direct_message_enum: $0\n " ,
2743- value_replacement)));
2763+ " $0\n "
2764+ " test_direct_message_enum: $1\n " ,
2765+ kTextMarkerRegex , value_replacement)));
27442766}
27452767TEST (AbslStringifyTest, StringifyNestedMessageEnumIsRedacted) {
27462768 unittest::TestRedactedMessage proto;
27472769 proto.set_test_nested_message_enum (" foo" );
27482770 EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2749- " test_nested_message_enum: $0\n " ,
2750- value_replacement)));
2771+ " $0\n "
2772+ " test_nested_message_enum: $1\n " ,
2773+ kTextMarkerRegex , value_replacement)));
27512774}
27522775
27532776TEST (AbslStringifyTest, StringifyRedactedOptionDoesNotRedact) {
27542777 unittest::TestRedactedMessage proto;
27552778 proto.set_test_redacted_message_enum (" foo" );
2756- EXPECT_THAT (absl::StrCat (proto),
2757- testing::MatchesRegex (
2758- " test_redacted_message_enum: \" foo\"\n " ));
2779+ EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2780+ " $0\n "
2781+ " test_redacted_message_enum: \" foo\"\n " ,
2782+ kTextMarkerRegex )));
27592783}
27602784
27612785
0 commit comments