Add tests for ArrayDecoders decoding packed negative-sized primitive lists.

I'm just about to edit this code, and I want these tests for my peace of mind.

Also update the other callers to use assertThrows for consistency.

PiperOrigin-RevId: 673590917
pull/18091/head
Mark Hansen 2 months ago committed by Copybara-Service
parent 3f1de2c6e8
commit ae51a89bf3
  1. 277
      java/core/src/test/java/com/google/protobuf/ArrayDecodersTest.java

@ -7,7 +7,7 @@
package com.google.protobuf; package com.google.protobuf;
import static com.google.common.truth.Truth.assertWithMessage; import static org.junit.Assert.assertThrows;
import com.google.protobuf.ArrayDecoders.Registers; import com.google.protobuf.ArrayDecoders.Registers;
import java.io.IOException; import java.io.IOException;
@ -33,134 +33,184 @@ public class ArrayDecodersTest {
@Test @Test
public void testException_decodeString() { public void testException_decodeString() {
try { assertThrows(
ArrayDecoders.decodeString(NEGATIVE_SIZE_0.toByteArray(), 0, registers); InvalidProtocolBufferException.class,
assertWithMessage("should throw exception").fail(); () -> ArrayDecoders.decodeString(NEGATIVE_SIZE_0.toByteArray(), 0, registers));
} catch (InvalidProtocolBufferException expected) {
}
} }
@Test @Test
public void testException_decodeStringRequireUtf8() { public void testException_decodeStringRequireUtf8() {
try { assertThrows(
ArrayDecoders.decodeStringRequireUtf8(NEGATIVE_SIZE_0.toByteArray(), 0, registers); InvalidProtocolBufferException.class,
assertWithMessage("should throw an exception").fail(); () -> ArrayDecoders.decodeStringRequireUtf8(NEGATIVE_SIZE_0.toByteArray(), 0, registers));
} catch (InvalidProtocolBufferException expected) {
}
} }
@Test @Test
public void testException_decodeBytes() { public void testException_decodeBytes() {
try { assertThrows(
ArrayDecoders.decodeBytes(NEGATIVE_SIZE_0.toByteArray(), 0, registers); InvalidProtocolBufferException.class,
assertWithMessage("should throw an exception").fail(); () -> ArrayDecoders.decodeBytes(NEGATIVE_SIZE_0.toByteArray(), 0, registers));
} catch (InvalidProtocolBufferException expected) {
}
} }
@Test @Test
public void testException_decodeStringList_first() { public void testException_decodeStringList_first() {
try { assertThrows(
ArrayDecoders.decodeStringList( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_0.toByteArray(), ArrayDecoders.decodeStringList(
0, TAG,
NEGATIVE_SIZE_0.size(), NEGATIVE_SIZE_0.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_0.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeStringList_second() { public void testException_decodeStringList_second() {
try { assertThrows(
ArrayDecoders.decodeStringList( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_1.toByteArray(), ArrayDecoders.decodeStringList(
0, TAG,
NEGATIVE_SIZE_1.size(), NEGATIVE_SIZE_1.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_1.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeStringListRequireUtf8_first() { public void testException_decodeStringListRequireUtf8_first() {
try { assertThrows(
ArrayDecoders.decodeStringListRequireUtf8( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_0.toByteArray(), ArrayDecoders.decodeStringListRequireUtf8(
0, TAG,
NEGATIVE_SIZE_0.size(), NEGATIVE_SIZE_0.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_0.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeStringListRequireUtf8_second() { public void testException_decodeStringListRequireUtf8_second() {
try { assertThrows(
ArrayDecoders.decodeStringListRequireUtf8( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_1.toByteArray(), ArrayDecoders.decodeStringListRequireUtf8(
0, TAG,
NEGATIVE_SIZE_1.size(), NEGATIVE_SIZE_1.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_1.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeBytesList_first() { public void testException_decodeBytesList_first() {
try { assertThrows(
ArrayDecoders.decodeBytesList( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_0.toByteArray(), ArrayDecoders.decodeBytesList(
0, TAG,
NEGATIVE_SIZE_0.size(), NEGATIVE_SIZE_0.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_0.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeBytesList_second() { public void testException_decodeBytesList_second() {
try { assertThrows(
ArrayDecoders.decodeBytesList( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_1.toByteArray(), ArrayDecoders.decodeBytesList(
0, TAG,
NEGATIVE_SIZE_1.size(), NEGATIVE_SIZE_1.toByteArray(),
new ProtobufArrayList<Object>(), 0,
registers); NEGATIVE_SIZE_1.size(),
assertWithMessage("should throw an exception").fail(); new ProtobufArrayList<Object>(),
} catch (InvalidProtocolBufferException expected) { registers));
}
} }
@Test @Test
public void testException_decodeUnknownField() { public void testException_decodeUnknownField() {
try { assertThrows(
ArrayDecoders.decodeUnknownField( InvalidProtocolBufferException.class,
TAG, () ->
NEGATIVE_SIZE_0.toByteArray(), ArrayDecoders.decodeUnknownField(
0, TAG,
NEGATIVE_SIZE_0.size(), NEGATIVE_SIZE_0.toByteArray(),
UnknownFieldSetLite.newInstance(), 0,
registers); NEGATIVE_SIZE_0.size(),
assertWithMessage("should throw an exception").fail(); UnknownFieldSetLite.newInstance(),
} catch (InvalidProtocolBufferException expected) { registers));
} }
@Test
public void testDecodePackedFixed32List_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedFixed32List(
packedSizeBytesNoTag(-1), 0, new IntArrayList(), registers));
}
@Test
public void testDecodePackedFixed64List_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedFixed64List(
packedSizeBytesNoTag(-1), 0, new LongArrayList(), registers));
}
@Test
public void testDecodePackedFloatList_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedFloatList(
packedSizeBytesNoTag(-1), 0, new FloatArrayList(), registers));
}
@Test
public void testDecodePackedDoubleList_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedDoubleList(
packedSizeBytesNoTag(-1), 0, new DoubleArrayList(), registers));
}
@Test
public void testDecodePackedBoolList_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedBoolList(
packedSizeBytesNoTag(-1), 0, new BooleanArrayList(), registers));
}
@Test
public void testDecodePackedSInt32List_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedSInt32List(
packedSizeBytesNoTag(-1), 0, new IntArrayList(), registers));
}
@Test
public void testDecodePackedSInt64List_negativeSize() {
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodePackedSInt64List(
packedSizeBytesNoTag(-1), 0, new LongArrayList(), registers));
} }
@Test @Test
@ -169,18 +219,15 @@ public class ArrayDecodersTest {
new byte[] { new byte[] {
(byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xEF, 0x73, 0x74, 0x69, 0x6E, 0x67 (byte) 0x80, (byte) 0xFF, (byte) 0xFF, (byte) 0xEF, 0x73, 0x74, 0x69, 0x6E, 0x67
}; };
try { assertThrows(
ArrayDecoders.decodeUnknownField( InvalidProtocolBufferException.class,
TAG, badBytes, 0, badBytes.length, UnknownFieldSetLite.newInstance(), registers); () ->
assertWithMessage("should throw an exception").fail(); ArrayDecoders.decodeUnknownField(
} catch (InvalidProtocolBufferException expected) { TAG, badBytes, 0, badBytes.length, UnknownFieldSetLite.newInstance(), registers));
}
try { assertThrows(
ArrayDecoders.decodeBytes(badBytes, 0, registers); InvalidProtocolBufferException.class,
assertWithMessage("should throw an exception").fail(); () -> ArrayDecoders.decodeBytes(badBytes, 0, registers));
} catch (InvalidProtocolBufferException expected) {
}
byte[] badBytesList = byte[] badBytesList =
new byte[] { new byte[] {
@ -197,11 +244,25 @@ public class ArrayDecodersTest {
0x6E, 0x6E,
0x67 0x67
}; };
assertThrows(
InvalidProtocolBufferException.class,
() ->
ArrayDecoders.decodeBytesList(
TAG, badBytesList, 0, badBytes.length, new ProtobufArrayList<>(), registers));
}
// Encodes a single varint without a tag prefix.
// For use when testing decoding of packed primitive lists.
// e.g. size = -1 is not a proper byte size for a list.
private static byte[] packedSizeBytesNoTag(int size) {
try { try {
ArrayDecoders.decodeBytesList( ByteString.Output byteStringOutput = ByteString.newOutput();
TAG, badBytesList, 0, badBytes.length, new ProtobufArrayList<>(), registers); CodedOutputStream codedOutput = CodedOutputStream.newInstance(byteStringOutput);
assertWithMessage("should throw an exception").fail(); codedOutput.writeInt32NoTag(size);
} catch (InvalidProtocolBufferException expected) { codedOutput.flush();
return byteStringOutput.toByteString().toByteArray();
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }

Loading…
Cancel
Save