From b6f3481696ab9355b5e27a488fa6e595a7ed8bcb Mon Sep 17 00:00:00 2001 From: Xufei Tan Date: Tue, 21 Jan 2025 14:44:06 -0800 Subject: [PATCH] Internal change PiperOrigin-RevId: 718078095 --- java/core/BUILD.bazel | 2 + .../UnredactedDebugFormatForTest.java | 39 ++++++ .../UnredactedDebugFormatForTestTest.java | 132 ++++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 java/core/src/main/java/com/google/protobuf/UnredactedDebugFormatForTest.java create mode 100644 java/core/src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java diff --git a/java/core/BUILD.bazel b/java/core/BUILD.bazel index 6fa9ae9e91..7db37081b0 100644 --- a/java/core/BUILD.bazel +++ b/java/core/BUILD.bazel @@ -571,6 +571,7 @@ LITE_TEST_EXCLUSIONS = [ "src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java", "src/test/java/com/google/protobuf/UnknownFieldSetPerformanceTest.java", "src/test/java/com/google/protobuf/UnknownFieldSetTest.java", + "src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java", "src/test/java/com/google/protobuf/WellKnownTypesTest.java", "src/test/java/com/google/protobuf/WireFormatTest.java", ] @@ -626,6 +627,7 @@ junit_tests( "src/test/java/com/google/protobuf/CodedInputStreamTest.java", "src/test/java/com/google/protobuf/ProtobufToStringOutputTest.java", "src/test/java/com/google/protobuf/FieldPresenceTest.java", + "src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java", # Excluded in core_tests "src/test/java/com/google/protobuf/DecodeUtf8Test.java", "src/test/java/com/google/protobuf/IsValidUtf8Test.java", diff --git a/java/core/src/main/java/com/google/protobuf/UnredactedDebugFormatForTest.java b/java/core/src/main/java/com/google/protobuf/UnredactedDebugFormatForTest.java new file mode 100644 index 0000000000..32c49679c2 --- /dev/null +++ b/java/core/src/main/java/com/google/protobuf/UnredactedDebugFormatForTest.java @@ -0,0 +1,39 @@ +package com.google.protobuf; + +/** + * These APIs are restricted to test-only targets, and are suitable for test purposes where it is + * impractical to compare protos directly via ProtoTruth (e.g. log output). + */ +public final class UnredactedDebugFormatForTest { + + private UnredactedDebugFormatForTest() {} + + /** Like {@code TextFormat.printer().printToString(message)}, but for test assertion purposes. */ + public static String unredactedMultilineString(MessageOrBuilder message) { + return TextFormat.printer() + .printToString(message, TextFormat.Printer.FieldReporterLevel.NO_REPORT); + } + + /** Like {@code TextFormat.printer().printToString(fields)}, but for test assertion purposes. */ + public static String unredactedMultilineString(UnknownFieldSet fields) { + return TextFormat.printer().printToString(fields); + } + + /** + * Like {@code TextFormat.printer().emittingSingleLine(true).printToString(message)}, but for test + * assertion purposes. + */ + public static String unredactedSingleLineString(MessageOrBuilder message) { + return TextFormat.printer() + .emittingSingleLine(true) + .printToString(message, TextFormat.Printer.FieldReporterLevel.NO_REPORT); + } + + /** + * Like {@code TextFormat.printer().emittingSingleLine(true).printToString(fields)}, but for test + * assertion purposes. + */ + public static String unredactedSingleLineString(UnknownFieldSet fields) { + return TextFormat.printer().emittingSingleLine(true).printToString(fields); + } +} diff --git a/java/core/src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java b/java/core/src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java new file mode 100644 index 0000000000..1ffeb554a9 --- /dev/null +++ b/java/core/src/test/java/com/google/protobuf/UnredactedDebugFormatForTestTest.java @@ -0,0 +1,132 @@ +package com.google.protobuf; + +import static com.google.common.truth.Truth.assertThat; + +import protobuf_unittest.UnittestProto; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public final class UnredactedDebugFormatForTestTest { + + @Test + public void unredactedDebugFormatForTestMessage_defaultMessageIsEmpty() { + UnittestProto.TestEmptyMessage message = UnittestProto.TestEmptyMessage.getDefaultInstance(); + assertThat(UnredactedDebugFormatForTest.unredactedMultilineString(message)).isEmpty(); + + UnittestProto.TestEmptyMessage singleLineMessage = + UnittestProto.TestEmptyMessage.getDefaultInstance(); + assertThat(UnredactedDebugFormatForTest.unredactedSingleLineString(singleLineMessage)) + .isEmpty(); + } + + @Test + public void unredactedDebugFormatForTestMessage_hasExpectedFormats() { + UnittestProto.RedactedFields message = + UnittestProto.RedactedFields.newBuilder() + .setOptionalRedactedString("redacted") + .setOptionalUnredactedString("hello") + .setOptionalRedactedMessage( + UnittestProto.TestNestedMessageRedaction.newBuilder() + .setOptionalRedactedNestedString("nested") + .build()) + .setOptionalUnredactedMessage( + UnittestProto.TestNestedMessageRedaction.newBuilder() + .setOptionalUnredactedNestedString("unredacted") + .build()) + .build(); + assertThat(UnredactedDebugFormatForTest.unredactedMultilineString(message)) + .isEqualTo( + "optional_redacted_string: \"redacted\"\n" + + "optional_unredacted_string: \"hello\"\n" + + "optional_redacted_message {\n" + + " optional_redacted_nested_string: \"nested\"\n" + + "}\n" + + "optional_unredacted_message {\n" + + " optional_unredacted_nested_string: \"unredacted\"\n" + + "}\n"); + assertThat(UnredactedDebugFormatForTest.unredactedSingleLineString(message)) + .isEqualTo( + "optional_redacted_string: \"redacted\"" + + " optional_unredacted_string: \"hello\"" + + " optional_redacted_message {" + + " optional_redacted_nested_string: \"nested\"" + + " }" + + " optional_unredacted_message {" + + " optional_unredacted_nested_string: \"unredacted\"" + + " }"); + } + + private UnknownFieldSet makeUnknownFieldSet() { + return UnknownFieldSet.newBuilder() + .addField( + 5, + UnknownFieldSet.Field.newBuilder() + .addVarint(1) + .addFixed32(2) + .addFixed64(3) + .addLengthDelimited(ByteString.copyFromUtf8("4")) + .addLengthDelimited( + UnknownFieldSet.newBuilder() + .addField(12, UnknownFieldSet.Field.newBuilder().addVarint(6).build()) + .build() + .toByteString()) + .addGroup( + UnknownFieldSet.newBuilder() + .addField(10, UnknownFieldSet.Field.newBuilder().addVarint(5).build()) + .build()) + .build()) + .addField( + 8, UnknownFieldSet.Field.newBuilder().addVarint(1).addVarint(2).addVarint(3).build()) + .addField( + 15, + UnknownFieldSet.Field.newBuilder() + .addVarint(0xABCDEF1234567890L) + .addFixed32(0xABCD1234) + .addFixed64(0xABCDEF1234567890L) + .build()) + .build(); + } + + @Test + public void unredactedDebugFormatForTestUnknownFields_hasExpectedFormats() { + UnknownFieldSet unknownFields = makeUnknownFieldSet(); + assertThat(UnredactedDebugFormatForTest.unredactedMultilineString(unknownFields)) + .isEqualTo( + "5: 1\n" + + "5: 0x00000002\n" + + "5: 0x0000000000000003\n" + + "5: \"4\"\n" + + "5: {\n" + + " 12: 6\n" + + "}\n" + + "5 {\n" + + " 10: 5\n" + + "}\n" + + "8: 1\n" + + "8: 2\n" + + "8: 3\n" + + "15: 12379813812177893520\n" + + "15: 0xabcd1234\n" + + "15: 0xabcdef1234567890\n"); + assertThat(UnredactedDebugFormatForTest.unredactedSingleLineString(unknownFields)) + .isEqualTo( + "5: 1" + + " 5: 0x00000002" + + " 5: 0x0000000000000003" + + " 5: \"4\"" + + " 5: {" + + " 12: 6" + + " }" + + " 5 {" + + " 10: 5" + + " }" + + " 8: 1" + + " 8: 2" + + " 8: 3" + + " 15: 12379813812177893520" + + " 15: 0xabcd1234" + + " 15: 0xabcdef1234567890"); + } +}