|
@@ -1142,6 +1142,39 @@ public class CodedInputStreamTest extends TestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ public void testIterableByteBufferInputStreamReadBytesWithAlias() throws Exception {
|
|
|
+ ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
|
|
|
+ CodedOutputStream output = CodedOutputStream.newInstance(byteArrayStream);
|
|
|
+ // A bytes field large enough that won't fit into the default block buffer.
|
|
|
+ // 4.5 is to test the case where the total size of input is not aligned with DEFAULT_BLOCK_SIZE.
|
|
|
+ final int bytesLength = DEFAULT_BLOCK_SIZE * 4 + (DEFAULT_BLOCK_SIZE / 2);
|
|
|
+ byte[] bytes = new byte[bytesLength];
|
|
|
+ for (int i = 0; i < bytesLength; i++) {
|
|
|
+ bytes[i] = (byte) (i % 256);
|
|
|
+ }
|
|
|
+ output.writeByteArrayNoTag(bytes);
|
|
|
+ output.flush();
|
|
|
+
|
|
|
+ // Input data is split into multiple ByteBuffers so that a single bytes spans across them.
|
|
|
+ // CodedInputStream with aliasing will decode it as a consequent rope by wrapping ByteBuffers.
|
|
|
+ byte[] data = byteArrayStream.toByteArray();
|
|
|
+ ArrayList<ByteBuffer> input = new ArrayList<>();
|
|
|
+ for (int i = 0; i < data.length; i += DEFAULT_BLOCK_SIZE) {
|
|
|
+ int rl = Math.min(DEFAULT_BLOCK_SIZE, data.length - i);
|
|
|
+ ByteBuffer rb = ByteBuffer.allocateDirect(rl);
|
|
|
+ rb.put(data, i, rl);
|
|
|
+ rb.flip();
|
|
|
+ input.add(rb);
|
|
|
+ }
|
|
|
+ final CodedInputStream inputStream = CodedInputStream.newInstance(input, true);
|
|
|
+ inputStream.enableAliasing(true);
|
|
|
+
|
|
|
+ ByteString result = inputStream.readBytes();
|
|
|
+ for (int i = 0; i < bytesLength; i++) {
|
|
|
+ assertEquals((byte) (i % 256), result.byteAt(i));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testCompatibleTypes() throws Exception {
|
|
|
long data = 0x100000000L;
|
|
|
Int64Message message = Int64Message.newBuilder().setData(data).build();
|
|
@@ -1196,7 +1229,7 @@ public class CodedInputStreamTest extends TestCase {
|
|
|
// Expected
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
public void testMaliciousInputStream() throws Exception {
|
|
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
|
|
CodedOutputStream codedOutputStream = CodedOutputStream.newInstance(outputStream);
|
|
@@ -1210,17 +1243,17 @@ public class CodedInputStreamTest extends TestCase {
|
|
|
return super.read(b, off, len);
|
|
|
}
|
|
|
};
|
|
|
-
|
|
|
+
|
|
|
// test ByteString
|
|
|
-
|
|
|
+
|
|
|
CodedInputStream codedInputStream = CodedInputStream.newInstance(inputStream, 1);
|
|
|
ByteString byteString = codedInputStream.readBytes();
|
|
|
assertEquals(0x0, byteString.byteAt(0));
|
|
|
maliciousCapture.get(1)[0] = 0x9;
|
|
|
assertEquals(0x0, byteString.byteAt(0));
|
|
|
-
|
|
|
+
|
|
|
// test ByteBuffer
|
|
|
-
|
|
|
+
|
|
|
inputStream.reset();
|
|
|
maliciousCapture.clear();
|
|
|
codedInputStream = CodedInputStream.newInstance(inputStream, 1);
|
|
@@ -1228,10 +1261,10 @@ public class CodedInputStreamTest extends TestCase {
|
|
|
assertEquals(0x0, byteBuffer.get(0));
|
|
|
maliciousCapture.get(1)[0] = 0x9;
|
|
|
assertEquals(0x0, byteBuffer.get(0));
|
|
|
-
|
|
|
+
|
|
|
|
|
|
// test byte[]
|
|
|
-
|
|
|
+
|
|
|
inputStream.reset();
|
|
|
maliciousCapture.clear();
|
|
|
codedInputStream = CodedInputStream.newInstance(inputStream, 1);
|
|
@@ -1241,7 +1274,7 @@ public class CodedInputStreamTest extends TestCase {
|
|
|
assertEquals(0x9, byteArray[0]); // MODIFICATION! Should we fix?
|
|
|
|
|
|
// test rawBytes
|
|
|
-
|
|
|
+
|
|
|
inputStream.reset();
|
|
|
maliciousCapture.clear();
|
|
|
codedInputStream = CodedInputStream.newInstance(inputStream, 1);
|