@@ -76,7 +76,6 @@ class UnsetFieldsMetadataTextFormatTestUtil {
76
76
// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
77
77
namespace text_format_unittest {
78
78
79
- using ::google::protobuf::internal::kDebugStringSilentMarker ;
80
79
using ::google::protobuf::internal::UnsetFieldsMetadataTextFormatTestUtil;
81
80
using ::testing::AllOf;
82
81
using ::testing::HasSubstr;
@@ -94,10 +93,16 @@ constexpr absl::string_view kEscapeTestStringEscaped =
94
93
95
94
constexpr absl::string_view value_replacement = " \\ [REDACTED\\ ]" ;
96
95
96
+ constexpr absl::string_view kTextMarkerRegex = " goo\\ .gle/.+ +" ;
97
+
97
98
class TextFormatTestBase : public testing ::Test {
98
99
public:
99
100
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 () + " " ;
101
106
multi_line_debug_format_prefix_ = proto_.DebugString ();
102
107
}
103
108
@@ -210,6 +215,7 @@ TEST_F(TextFormatTest, ShortFormat) {
210
215
std::string value_replacement = " \\ [REDACTED\\ ]" ;
211
216
EXPECT_THAT (google::protobuf::ShortFormat (proto),
212
217
testing::MatchesRegex (absl::Substitute (
218
+ " $1"
213
219
" optional_redacted_string: $0 "
214
220
" optional_unredacted_string: \" bar\" "
215
221
" repeated_redacted_string: $0 "
@@ -226,7 +232,7 @@ TEST_F(TextFormatTest, ShortFormat) {
226
232
" \\ { optional_unredacted_nested_string: \" 8\" \\ } "
227
233
" map_redacted_string: $0 "
228
234
" map_unredacted_string \\ { key: \" ghi\" value: \" jkl\" \\ }" ,
229
- value_replacement)));
235
+ value_replacement, kTextMarkerRegex )));
230
236
}
231
237
232
238
TEST_F (TextFormatTest, Utf8Format) {
@@ -262,6 +268,7 @@ TEST_F(TextFormatTest, Utf8Format) {
262
268
263
269
EXPECT_THAT (google::protobuf::Utf8Format (proto),
264
270
testing::MatchesRegex (absl::Substitute (
271
+ " $1\n "
265
272
" optional_redacted_string: $0\n "
266
273
" optional_unredacted_string: \" bar\"\n "
267
274
" repeated_redacted_string: $0\n "
@@ -280,7 +287,7 @@ TEST_F(TextFormatTest, Utf8Format) {
280
287
" map_redacted_string: $0\n "
281
288
" map_unredacted_string \\ {\n "
282
289
" key: \" ghi\"\n value: \" jkl\"\n\\ }\n " ,
283
- value_replacement)));
290
+ value_replacement, kTextMarkerRegex )));
284
291
}
285
292
286
293
TEST_F (TextFormatTest, ShortPrimitiveRepeateds) {
@@ -433,6 +440,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) {
433
440
message_text);
434
441
435
442
EXPECT_THAT (absl::StrCat (message), testing::MatchesRegex (absl::Substitute (
443
+ " $1\n "
436
444
" 5: UNKNOWN_VARINT $0\n "
437
445
" 5: UNKNOWN_FIXED32 $0\n "
438
446
" 5: UNKNOWN_FIXED64 $0\n "
@@ -441,7 +449,7 @@ TEST_F(TextFormatTest, PrintUnknownFields) {
441
449
" 8: UNKNOWN_VARINT $0\n "
442
450
" 8: UNKNOWN_VARINT $0\n "
443
451
" 8: UNKNOWN_VARINT $0\n " ,
444
- value_replacement)));
452
+ value_replacement, kTextMarkerRegex )));
445
453
}
446
454
447
455
TEST_F (TextFormatTest, PrintUnknownFieldsDeepestStackWorks) {
@@ -2679,6 +2687,13 @@ TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
2679
2687
EXPECT_FALSE (parser.ParseFromString (" unknown_field: 1" , &proto));
2680
2688
}
2681
2689
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
+ }
2682
2697
2683
2698
TEST (AbslStringifyTest, TextFormatIsUnchanged) {
2684
2699
unittest::TestAllTypes proto;
@@ -2698,34 +2713,40 @@ TEST(AbslStringifyTest, StringifyHasRedactionMarker) {
2698
2713
proto.set_optional_int32 (1 );
2699
2714
proto.set_optional_string (" foo" );
2700
2715
2701
- EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (
2716
+ EXPECT_THAT (absl::StrCat (proto), testing::MatchesRegex (absl::Substitute (
2717
+ " $0\n "
2702
2718
" optional_int32: 1\n "
2703
- " optional_string: \" foo\"\n " ));
2719
+ " optional_string: \" foo\"\n " ,
2720
+ kTextMarkerRegex )));
2704
2721
}
2705
2722
2706
2723
2707
2724
TEST (AbslStringifyTest, StringifyMetaAnnotatedIsRedacted) {
2708
2725
unittest::TestRedactedMessage proto;
2709
2726
proto.set_meta_annotated (" foo" );
2710
2727
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)));
2713
2731
}
2714
2732
2715
2733
TEST (AbslStringifyTest, StringifyRepeatedMetaAnnotatedIsRedacted) {
2716
2734
unittest::TestRedactedMessage proto;
2717
2735
proto.set_repeated_meta_annotated (" foo" );
2718
2736
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)));
2721
2740
}
2722
2741
2723
2742
TEST (AbslStringifyTest, StringifyRepeatedMetaAnnotatedIsNotRedacted) {
2724
2743
unittest::TestRedactedMessage proto;
2725
2744
proto.set_unredacted_repeated_annotations (" foo" );
2726
2745
EXPECT_THAT (absl::StrCat (proto),
2727
2746
testing::MatchesRegex (
2728
- " unredacted_repeated_annotations: \" foo\"\n " ));
2747
+ absl::Substitute (" $0\n "
2748
+ " unredacted_repeated_annotations: \" foo\"\n " ,
2749
+ kTextMarkerRegex )));
2729
2750
}
2730
2751
2731
2752
TEST (AbslStringifyTest, TextFormatMetaAnnotatedIsNotRedacted) {
@@ -2739,23 +2760,26 @@ TEST(AbslStringifyTest, StringifyDirectMessageEnumIsRedacted) {
2739
2760
unittest::TestRedactedMessage proto;
2740
2761
proto.set_test_direct_message_enum (" foo" );
2741
2762
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)));
2744
2766
}
2745
2767
TEST (AbslStringifyTest, StringifyNestedMessageEnumIsRedacted) {
2746
2768
unittest::TestRedactedMessage proto;
2747
2769
proto.set_test_nested_message_enum (" foo" );
2748
2770
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)));
2751
2774
}
2752
2775
2753
2776
TEST (AbslStringifyTest, StringifyRedactedOptionDoesNotRedact) {
2754
2777
unittest::TestRedactedMessage proto;
2755
2778
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 )));
2759
2783
}
2760
2784
2761
2785
0 commit comments