Internal change

PiperOrigin-RevId: 633679764
pull/16848/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent bb68eb22dd
commit dde03553c9
  1. 67
      java/core/src/main/java/com/google/protobuf/TextFormat.java
  2. 52
      java/core/src/test/java/com/google/protobuf/DebugFormatTest.java

@ -60,11 +60,12 @@ public final class TextFormat {
*/ */
public static void printUnknownFieldValue( public static void printUnknownFieldValue(
final int tag, final Object value, final Appendable output) throws IOException { final int tag, final Object value, final Appendable output) throws IOException {
printUnknownFieldValue(tag, value, setSingleLineOutput(output, false)); printUnknownFieldValue(tag, value, setSingleLineOutput(output, false), false);
} }
private static void printUnknownFieldValue( private static void printUnknownFieldValue(
final int tag, final Object value, final TextGenerator generator) throws IOException { final int tag, final Object value, final TextGenerator generator, boolean redact)
throws IOException {
switch (WireFormat.getTagWireType(tag)) { switch (WireFormat.getTagWireType(tag)) {
case WireFormat.WIRETYPE_VARINT: case WireFormat.WIRETYPE_VARINT:
generator.print(unsignedToString((Long) value)); generator.print(unsignedToString((Long) value));
@ -82,7 +83,7 @@ public final class TextFormat {
generator.print("{"); generator.print("{");
generator.eol(); generator.eol();
generator.indent(); generator.indent();
Printer.printUnknownFields(message, generator); Printer.printUnknownFields(message, generator, redact);
generator.outdent(); generator.outdent();
generator.print("}"); generator.print("}");
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
@ -93,7 +94,7 @@ public final class TextFormat {
} }
break; break;
case WireFormat.WIRETYPE_START_GROUP: case WireFormat.WIRETYPE_START_GROUP:
Printer.printUnknownFields((UnknownFieldSet) value, generator); Printer.printUnknownFields((UnknownFieldSet) value, generator, redact);
break; break;
default: default:
throw new IllegalArgumentException("Bad tag: " + tag); throw new IllegalArgumentException("Bad tag: " + tag);
@ -223,7 +224,8 @@ public final class TextFormat {
/** Outputs a textual representation of {@code fields} to {@code output}. */ /** Outputs a textual representation of {@code fields} to {@code output}. */
public void print(final UnknownFieldSet fields, final Appendable output) throws IOException { public void print(final UnknownFieldSet fields, final Appendable output) throws IOException {
printUnknownFields(fields, setSingleLineOutput(output, this.singleLine)); printUnknownFields(
fields, setSingleLineOutput(output, this.singleLine), this.enablingSafeDebugFormat);
} }
private void print(final MessageOrBuilder message, final TextGenerator generator) private void print(final MessageOrBuilder message, final TextGenerator generator)
@ -597,7 +599,7 @@ public final class TextFormat {
public String shortDebugString(final UnknownFieldSet fields) { public String shortDebugString(final UnknownFieldSet fields) {
try { try {
final StringBuilder text = new StringBuilder(); final StringBuilder text = new StringBuilder();
printUnknownFields(fields, setSingleLineOutput(text, true)); printUnknownFields(fields, setSingleLineOutput(text, true), this.enablingSafeDebugFormat);
return text.toString(); return text.toString();
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e); throw new IllegalStateException(e);
@ -605,16 +607,26 @@ public final class TextFormat {
} }
private static void printUnknownFieldValue( private static void printUnknownFieldValue(
final int tag, final Object value, final TextGenerator generator) throws IOException { final int tag, final Object value, final TextGenerator generator, boolean redact)
throws IOException {
switch (WireFormat.getTagWireType(tag)) { switch (WireFormat.getTagWireType(tag)) {
case WireFormat.WIRETYPE_VARINT: case WireFormat.WIRETYPE_VARINT:
generator.print(unsignedToString((Long) value)); generator.print(
redact
? String.format("UNKNOWN_VARINT %s", REDACTED_MARKER)
: unsignedToString((Long) value));
break; break;
case WireFormat.WIRETYPE_FIXED32: case WireFormat.WIRETYPE_FIXED32:
generator.print(String.format((Locale) null, "0x%08x", (Integer) value)); generator.print(
redact
? String.format("UNKNOWN_FIXED32 %s", REDACTED_MARKER)
: String.format((Locale) null, "0x%08x", (Integer) value));
break; break;
case WireFormat.WIRETYPE_FIXED64: case WireFormat.WIRETYPE_FIXED64:
generator.print(String.format((Locale) null, "0x%016x", (Long) value)); generator.print(
redact
? String.format("UNKNOWN_FIXED64 %s", REDACTED_MARKER)
: String.format((Locale) null, "0x%016x", (Long) value));
break; break;
case WireFormat.WIRETYPE_LENGTH_DELIMITED: case WireFormat.WIRETYPE_LENGTH_DELIMITED:
try { try {
@ -623,18 +635,22 @@ public final class TextFormat {
generator.print("{"); generator.print("{");
generator.eol(); generator.eol();
generator.indent(); generator.indent();
printUnknownFields(message, generator); printUnknownFields(message, generator, redact);
generator.outdent(); generator.outdent();
generator.print("}"); generator.print("}");
} catch (InvalidProtocolBufferException e) { } catch (InvalidProtocolBufferException e) {
// If not parseable as a message, print as a String // If not parseable as a message, print as a String
if (redact) {
generator.print(String.format("UNKNOWN_STRING %s", REDACTED_MARKER));
break;
}
generator.print("\""); generator.print("\"");
generator.print(escapeBytes((ByteString) value)); generator.print(escapeBytes((ByteString) value));
generator.print("\""); generator.print("\"");
} }
break; break;
case WireFormat.WIRETYPE_START_GROUP: case WireFormat.WIRETYPE_START_GROUP:
printUnknownFields((UnknownFieldSet) value, generator); printUnknownFields((UnknownFieldSet) value, generator, redact);
break; break;
default: default:
throw new IllegalArgumentException("Bad tag: " + tag); throw new IllegalArgumentException("Bad tag: " + tag);
@ -646,7 +662,7 @@ public final class TextFormat {
for (Map.Entry<FieldDescriptor, Object> field : message.getAllFields().entrySet()) { for (Map.Entry<FieldDescriptor, Object> field : message.getAllFields().entrySet()) {
printField(field.getKey(), field.getValue(), generator); printField(field.getKey(), field.getValue(), generator);
} }
printUnknownFields(message.getUnknownFields(), generator); printUnknownFields(message.getUnknownFields(), generator, this.enablingSafeDebugFormat);
} }
private void printSingleField( private void printSingleField(
@ -692,27 +708,32 @@ public final class TextFormat {
} }
private static void printUnknownFields( private static void printUnknownFields(
final UnknownFieldSet unknownFields, final TextGenerator generator) throws IOException { final UnknownFieldSet unknownFields, final TextGenerator generator, boolean redact)
throws IOException {
if (unknownFields.isEmpty()) { if (unknownFields.isEmpty()) {
return; return;
} }
for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) { for (Map.Entry<Integer, UnknownFieldSet.Field> entry : unknownFields.asMap().entrySet()) {
final int number = entry.getKey(); final int number = entry.getKey();
final UnknownFieldSet.Field field = entry.getValue(); final UnknownFieldSet.Field field = entry.getValue();
printUnknownField(number, WireFormat.WIRETYPE_VARINT, field.getVarintList(), generator); printUnknownField(
printUnknownField(number, WireFormat.WIRETYPE_FIXED32, field.getFixed32List(), generator); number, WireFormat.WIRETYPE_VARINT, field.getVarintList(), generator, redact);
printUnknownField(number, WireFormat.WIRETYPE_FIXED64, field.getFixed64List(), generator); printUnknownField(
number, WireFormat.WIRETYPE_FIXED32, field.getFixed32List(), generator, redact);
printUnknownField(
number, WireFormat.WIRETYPE_FIXED64, field.getFixed64List(), generator, redact);
printUnknownField( printUnknownField(
number, number,
WireFormat.WIRETYPE_LENGTH_DELIMITED, WireFormat.WIRETYPE_LENGTH_DELIMITED,
field.getLengthDelimitedList(), field.getLengthDelimitedList(),
generator); generator,
redact);
for (final UnknownFieldSet value : field.getGroupList()) { for (final UnknownFieldSet value : field.getGroupList()) {
generator.print(entry.getKey().toString()); generator.print(entry.getKey().toString());
generator.print(" {"); generator.print(" {");
generator.eol(); generator.eol();
generator.indent(); generator.indent();
printUnknownFields(value, generator); printUnknownFields(value, generator, redact);
generator.outdent(); generator.outdent();
generator.print("}"); generator.print("}");
generator.eol(); generator.eol();
@ -721,12 +742,16 @@ public final class TextFormat {
} }
private static void printUnknownField( private static void printUnknownField(
final int number, final int wireType, final List<?> values, final TextGenerator generator) final int number,
final int wireType,
final List<?> values,
final TextGenerator generator,
boolean redact)
throws IOException { throws IOException {
for (final Object value : values) { for (final Object value : values) {
generator.print(String.valueOf(number)); generator.print(String.valueOf(number));
generator.print(": "); generator.print(": ");
printUnknownFieldValue(wireType, value, generator); printUnknownFieldValue(wireType, value, generator, redact);
generator.eol(); generator.eol();
} }
} }

@ -5,6 +5,7 @@ import static protobuf_unittest.UnittestProto.redactedExtension;
import com.google.protobuf.Descriptors.FieldDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor;
import protobuf_unittest.UnittestProto.RedactedFields; import protobuf_unittest.UnittestProto.RedactedFields;
import protobuf_unittest.UnittestProto.TestEmptyMessage;
import protobuf_unittest.UnittestProto.TestNestedMessageRedaction; import protobuf_unittest.UnittestProto.TestNestedMessageRedaction;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -155,4 +156,55 @@ public final class DebugFormatTest {
String.format("%soptional_unredacted_string: \"foo\"", UNSTABLE_PREFIX_SINGLE_LINE)); String.format("%soptional_unredacted_string: \"foo\"", UNSTABLE_PREFIX_SINGLE_LINE));
} }
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 unknownFieldsDebugFormat_returnsExpectedFormat() {
TestEmptyMessage unknownFields =
TestEmptyMessage.newBuilder().setUnknownFields(makeUnknownFieldSet()).build();
assertThat(DebugFormat.multiline().toString(unknownFields))
.matches(
String.format("%s5: UNKNOWN_VARINT %s\n", UNSTABLE_PREFIX_MULTILINE, REDACTED_REGEX)
+ String.format("5: UNKNOWN_FIXED32 %s\n", REDACTED_REGEX)
+ String.format("5: UNKNOWN_FIXED64 %s\n", REDACTED_REGEX)
+ String.format("5: UNKNOWN_STRING %s\n", REDACTED_REGEX)
+ String.format("5: \\{\n 12: UNKNOWN_VARINT %s\n\\}\n", REDACTED_REGEX)
+ String.format("5 \\{\n 10: UNKNOWN_VARINT %s\n\\}\n", REDACTED_REGEX)
+ String.format("8: UNKNOWN_VARINT %s\n", REDACTED_REGEX)
+ String.format("8: UNKNOWN_VARINT %s\n", REDACTED_REGEX)
+ String.format("8: UNKNOWN_VARINT %s\n", REDACTED_REGEX)
+ String.format("15: UNKNOWN_VARINT %s\n", REDACTED_REGEX)
+ String.format("15: UNKNOWN_FIXED32 %s\n", REDACTED_REGEX)
+ String.format("15: UNKNOWN_FIXED64 %s\n", REDACTED_REGEX));
}
} }

Loading…
Cancel
Save