Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/CodedInputStreamTest.java

Issue 2599263002: third_party/protobuf: Update to HEAD (f52e188fe4) (Closed)
Patch Set: Address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/CodedInputStreamTest.java
diff --git a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/CodedInputStreamTest.java
similarity index 54%
copy from third_party/protobuf/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java
copy to third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/CodedInputStreamTest.java
index ca940cedb24535ede3794c36b17cd463f40f1a51..7e67898ea4396a6fa4fecc5c43ffcdc7cec69056 100644
--- a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java
+++ b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/CodedInputStreamTest.java
@@ -1,6 +1,6 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
-// https://developers.google.com/protocol-buffers/
+// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
@@ -28,22 +28,18 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-package com.google.protobuf;
+package com.google.protobuf.test;
+import com.google.protobuf.*;
-import protobuf_unittest.UnittestProto.BoolMessage;
-import protobuf_unittest.UnittestProto.Int32Message;
-import protobuf_unittest.UnittestProto.Int64Message;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestRecursiveMessage;
import junit.framework.TestCase;
import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.FilterInputStream;
-import java.io.IOException;
import java.io.InputStream;
-import java.nio.ByteBuffer;
+import java.io.IOException;
/**
* Unit test for {@link CodedInputStream}.
@@ -81,65 +77,37 @@ public class CodedInputStreamTest extends TestCase {
this.blockSize = blockSize;
}
- @Override
public int read(byte[] b) throws IOException {
return super.read(b, 0, Math.min(b.length, blockSize));
}
- @Override
public int read(byte[] b, int off, int len) throws IOException {
return super.read(b, off, Math.min(len, blockSize));
}
}
- private void assertDataConsumed(byte[] data, CodedInputStream input)
- throws IOException {
- assertEquals(data.length, input.getTotalBytesRead());
- assertTrue(input.isAtEnd());
- }
-
/**
* Parses the given bytes using readRawVarint32() and readRawVarint64() and
* checks that the result matches the given value.
*/
private void assertReadVarint(byte[] data, long value) throws Exception {
CodedInputStream input = CodedInputStream.newInstance(data);
- assertEquals((int) value, input.readRawVarint32());
- assertDataConsumed(data, input);
+ assertEquals((int)value, input.readRawVarint32());
input = CodedInputStream.newInstance(data);
assertEquals(value, input.readRawVarint64());
- assertDataConsumed(data, input);
-
- input = CodedInputStream.newInstance(data);
- assertEquals(value, input.readRawVarint64SlowPath());
- assertDataConsumed(data, input);
-
- input = CodedInputStream.newInstance(data);
- assertTrue(input.skipField(WireFormat.WIRETYPE_VARINT));
- assertDataConsumed(data, input);
+ assertTrue(input.isAtEnd());
// Try different block sizes.
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
input = CodedInputStream.newInstance(
new SmallBlockInputStream(data, blockSize));
- assertEquals((int) value, input.readRawVarint32());
- assertDataConsumed(data, input);
+ assertEquals((int)value, input.readRawVarint32());
input = CodedInputStream.newInstance(
new SmallBlockInputStream(data, blockSize));
assertEquals(value, input.readRawVarint64());
- assertDataConsumed(data, input);
-
- input = CodedInputStream.newInstance(
- new SmallBlockInputStream(data, blockSize));
- assertEquals(value, input.readRawVarint64SlowPath());
- assertDataConsumed(data, input);
-
- input = CodedInputStream.newInstance(
- new SmallBlockInputStream(data, blockSize));
- assertTrue(input.skipField(WireFormat.WIRETYPE_VARINT));
- assertDataConsumed(data, input);
+ assertTrue(input.isAtEnd());
}
// Try reading direct from an InputStream. We want to verify that it
@@ -148,8 +116,6 @@ public class CodedInputStreamTest extends TestCase {
byte[] longerData = new byte[data.length + 1];
System.arraycopy(data, 0, longerData, 0, data.length);
InputStream rawInput = new ByteArrayInputStream(longerData);
- assertEquals((int) value, CodedInputStream.readRawVarint32(rawInput));
- assertEquals(1, rawInput.available());
}
/**
@@ -175,22 +141,6 @@ public class CodedInputStreamTest extends TestCase {
} catch (InvalidProtocolBufferException e) {
assertEquals(expected.getMessage(), e.getMessage());
}
-
- input = CodedInputStream.newInstance(data);
- try {
- input.readRawVarint64SlowPath();
- fail("Should have thrown an exception.");
- } catch (InvalidProtocolBufferException e) {
- assertEquals(expected.getMessage(), e.getMessage());
- }
-
- // Make sure we get the same error when reading direct from an InputStream.
- try {
- CodedInputStream.readRawVarint32(new ByteArrayInputStream(data));
- fail("Should have thrown an exception.");
- } catch (InvalidProtocolBufferException e) {
- assertEquals(expected.getMessage(), e.getMessage());
- }
}
/** Tests readRawVarint32() and readRawVarint64(). */
@@ -221,15 +171,6 @@ public class CodedInputStreamTest extends TestCase {
(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
(0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
(0x05L << 49) | (0x26L << 56) | (0x01L << 63));
-
- // Failures
- assertReadVarintFailure(
- InvalidProtocolBufferException.malformedVarint(),
- bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
- 0x00));
- assertReadVarintFailure(
- InvalidProtocolBufferException.truncatedMessage(),
- bytes(0x80));
}
/**
@@ -352,7 +293,6 @@ public class CodedInputStreamTest extends TestCase {
}
}
-
/**
* Test that a bug in skipRawBytes() has been fixed: if the skip skips
* exactly up to a limit, this should not break things.
@@ -392,7 +332,7 @@ public class CodedInputStreamTest extends TestCase {
// Allocate and initialize a 1MB blob.
byte[] blob = new byte[1 << 20];
for (int i = 0; i < blob.length; i++) {
- blob[i] = (byte) i;
+ blob[i] = (byte)i;
}
// Make a message containing it.
@@ -416,11 +356,15 @@ public class CodedInputStreamTest extends TestCase {
TestUtil.assertAllFieldsSet(message3);
}
+ public int makeTag(int number, int tag) {
+ return (number << 3) + tag;
+ }
+
public void testReadMaliciouslyLargeBlob() throws Exception {
ByteString.Output rawOutput = ByteString.newOutput();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
- int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
+ int tag = makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
output.writeRawVarint32(tag);
output.writeRawVarint32(0x7FFFFFFF);
output.writeRawBytes(new byte[32]); // Pad with a few random bytes.
@@ -456,49 +400,6 @@ public class CodedInputStreamTest extends TestCase {
}
}
- public void testMaliciousRecursion() throws Exception {
- ByteString data100 = makeRecursiveMessage(100).toByteString();
- ByteString data101 = makeRecursiveMessage(101).toByteString();
-
- assertMessageDepth(TestRecursiveMessage.parseFrom(data100), 100);
-
- try {
- TestRecursiveMessage.parseFrom(data101);
- fail("Should have thrown an exception!");
- } catch (InvalidProtocolBufferException e) {
- // success.
- }
-
- CodedInputStream input = data100.newCodedInput();
- input.setRecursionLimit(8);
- try {
- TestRecursiveMessage.parseFrom(input);
- fail("Should have thrown an exception!");
- } catch (InvalidProtocolBufferException e) {
- // success.
- }
- }
-
- private void checkSizeLimitExceeded(InvalidProtocolBufferException e) {
- assertEquals(
- InvalidProtocolBufferException.sizeLimitExceeded().getMessage(),
- e.getMessage());
- }
-
- public void testSizeLimit() throws Exception {
- CodedInputStream input = CodedInputStream.newInstance(
- new SmallBlockInputStream(
- TestUtil.getAllSet().toByteString().newInput(), 16));
- input.setSizeLimit(16);
-
- try {
- TestAllTypes.parseFrom(input);
- fail("Should have thrown an exception!");
- } catch (InvalidProtocolBufferException expected) {
- checkSizeLimitExceeded(expected);
- }
- }
-
public void testResetSizeCounter() throws Exception {
CodedInputStream input = CodedInputStream.newInstance(
new SmallBlockInputStream(new byte[256], 8));
@@ -509,8 +410,8 @@ public class CodedInputStreamTest extends TestCase {
try {
input.readRawByte();
fail("Should have thrown an exception!");
- } catch (InvalidProtocolBufferException expected) {
- checkSizeLimitExceeded(expected);
+ } catch (InvalidProtocolBufferException e) {
+ // success.
}
input.resetSizeCounter();
@@ -518,100 +419,21 @@ public class CodedInputStreamTest extends TestCase {
input.readRawByte(); // No exception thrown.
input.resetSizeCounter();
assertEquals(0, input.getTotalBytesRead());
- input.readRawBytes(16);
- assertEquals(16, input.getTotalBytesRead());
- input.resetSizeCounter();
-
- try {
- input.readRawBytes(17); // Hits limit again.
- fail("Should have thrown an exception!");
- } catch (InvalidProtocolBufferException expected) {
- checkSizeLimitExceeded(expected);
- }
- }
-
- public void testSizeLimitMultipleMessages() throws Exception {
- byte[] bytes = new byte[256];
- for (int i = 0; i < bytes.length; i++) {
- bytes[i] = (byte) i;
- }
- CodedInputStream input = CodedInputStream.newInstance(
- new SmallBlockInputStream(bytes, 7));
- input.setSizeLimit(16);
- for (int i = 0; i < 256 / 16; i++) {
- byte[] message = input.readRawBytes(16);
- for (int j = 0; j < message.length; j++) {
- assertEquals(i * 16 + j, message[j] & 0xff);
- }
- assertEquals(16, input.getTotalBytesRead());
- input.resetSizeCounter();
- assertEquals(0, input.getTotalBytesRead());
- }
- }
-
- public void testReadString() throws Exception {
- String lorem = "Lorem ipsum dolor sit amet ";
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < 4096; i += lorem.length()) {
- builder.append(lorem);
- }
- lorem = builder.toString().substring(0, 4096);
- byte[] bytes = lorem.getBytes("UTF-8");
- ByteString.Output rawOutput = ByteString.newOutput();
- CodedOutputStream output = CodedOutputStream.newInstance(rawOutput, bytes.length);
-
- int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
- output.writeRawVarint32(tag);
- output.writeRawVarint32(bytes.length);
- output.writeRawBytes(bytes);
- output.flush();
-
- CodedInputStream input =
- CodedInputStream.newInstance(
- new ByteArrayInputStream(rawOutput.toByteString().toByteArray()));
- assertEquals(tag, input.readTag());
- String text = input.readString();
- assertEquals(lorem, text);
- }
-
- public void testReadStringRequireUtf8() throws Exception {
- String lorem = "Lorem ipsum dolor sit amet ";
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < 4096; i += lorem.length()) {
- builder.append(lorem);
- }
- lorem = builder.toString().substring(0, 4096);
- byte[] bytes = lorem.getBytes("UTF-8");
- ByteString.Output rawOutput = ByteString.newOutput();
- CodedOutputStream output = CodedOutputStream.newInstance(rawOutput, bytes.length);
-
- int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
- output.writeRawVarint32(tag);
- output.writeRawVarint32(bytes.length);
- output.writeRawBytes(bytes);
- output.flush();
-
- CodedInputStream input =
- CodedInputStream.newInstance(
- new ByteArrayInputStream(rawOutput.toByteString().toByteArray()));
- assertEquals(tag, input.readTag());
- String text = input.readStringRequireUtf8();
- assertEquals(lorem, text);
}
/**
- * Tests that if we readString invalid UTF-8 bytes, no exception
+ * Tests that if we read an string that contains invalid UTF-8, no exception
* is thrown. Instead, the invalid bytes are replaced with the Unicode
* "replacement character" U+FFFD.
*/
- public void testReadStringInvalidUtf8() throws Exception {
+ public void testReadInvalidUtf8() throws Exception {
ByteString.Output rawOutput = ByteString.newOutput();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
- int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
+ int tag = makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
output.writeRawVarint32(tag);
output.writeRawVarint32(1);
- output.writeRawBytes(new byte[] { (byte) 0x80 });
+ output.writeRawBytes(new byte[] { (byte)0x80 });
output.flush();
CodedInputStream input = rawOutput.toByteString().newCodedInput();
@@ -620,37 +442,13 @@ public class CodedInputStreamTest extends TestCase {
assertEquals(0xfffd, text.charAt(0));
}
- /**
- * Tests that if we readStringRequireUtf8 invalid UTF-8 bytes, an
- * InvalidProtocolBufferException is thrown.
- */
- public void testReadStringRequireUtf8InvalidUtf8() throws Exception {
- ByteString.Output rawOutput = ByteString.newOutput();
- CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
-
- int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED);
- output.writeRawVarint32(tag);
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[] { (byte) 0x80 });
- output.flush();
-
- CodedInputStream input = rawOutput.toByteString().newCodedInput();
- assertEquals(tag, input.readTag());
- try {
- input.readStringRequireUtf8();
- fail("Expected invalid UTF-8 exception.");
- } catch (InvalidProtocolBufferException exception) {
- assertEquals("Protocol message had invalid UTF-8.", exception.getMessage());
- }
- }
-
public void testReadFromSlice() throws Exception {
byte[] bytes = bytes(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
CodedInputStream in = CodedInputStream.newInstance(bytes, 3, 5);
assertEquals(0, in.getTotalBytesRead());
for (int i = 3; i < 8; i++) {
assertEquals(i, in.readRawByte());
- assertEquals(i - 2, in.getTotalBytesRead());
+ assertEquals(i-2, in.getTotalBytesRead());
}
// eof
assertEquals(0, in.readTag());
@@ -665,157 +463,7 @@ public class CodedInputStreamTest extends TestCase {
CodedInputStream.newInstance(bytes(i)).readTag();
fail("Should have thrown an exception.");
} catch (InvalidProtocolBufferException e) {
- assertEquals(InvalidProtocolBufferException.invalidTag().getMessage(),
- e.getMessage());
}
}
}
-
- public void testReadByteArray() throws Exception {
- ByteString.Output rawOutput = ByteString.newOutput();
- CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
- // Zero-sized bytes field.
- output.writeRawVarint32(0);
- // One one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[] { (byte) 23 });
- // Another one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[] { (byte) 45 });
- // A bytes field large enough that won't fit into the 4K buffer.
- final int bytesLength = 16 * 1024;
- byte[] bytes = new byte[bytesLength];
- bytes[0] = (byte) 67;
- bytes[bytesLength - 1] = (byte) 89;
- output.writeRawVarint32(bytesLength);
- output.writeRawBytes(bytes);
-
- output.flush();
- CodedInputStream inputStream = rawOutput.toByteString().newCodedInput();
-
- byte[] result = inputStream.readByteArray();
- assertEquals(0, result.length);
- result = inputStream.readByteArray();
- assertEquals(1, result.length);
- assertEquals((byte) 23, result[0]);
- result = inputStream.readByteArray();
- assertEquals(1, result.length);
- assertEquals((byte) 45, result[0]);
- result = inputStream.readByteArray();
- assertEquals(bytesLength, result.length);
- assertEquals((byte) 67, result[0]);
- assertEquals((byte) 89, result[bytesLength - 1]);
- }
-
- public void testReadByteBuffer() throws Exception {
- ByteString.Output rawOutput = ByteString.newOutput();
- CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
- // Zero-sized bytes field.
- output.writeRawVarint32(0);
- // One one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[]{(byte) 23});
- // Another one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[]{(byte) 45});
- // A bytes field large enough that won't fit into the 4K buffer.
- final int bytesLength = 16 * 1024;
- byte[] bytes = new byte[bytesLength];
- bytes[0] = (byte) 67;
- bytes[bytesLength - 1] = (byte) 89;
- output.writeRawVarint32(bytesLength);
- output.writeRawBytes(bytes);
-
- output.flush();
- CodedInputStream inputStream = rawOutput.toByteString().newCodedInput();
-
- ByteBuffer result = inputStream.readByteBuffer();
- assertEquals(0, result.capacity());
- result = inputStream.readByteBuffer();
- assertEquals(1, result.capacity());
- assertEquals((byte) 23, result.get());
- result = inputStream.readByteBuffer();
- assertEquals(1, result.capacity());
- assertEquals((byte) 45, result.get());
- result = inputStream.readByteBuffer();
- assertEquals(bytesLength, result.capacity());
- assertEquals((byte) 67, result.get());
- result.position(bytesLength - 1);
- assertEquals((byte) 89, result.get());
- }
-
- public void testReadByteBufferAliasing() throws Exception {
- ByteArrayOutputStream byteArrayStream = new ByteArrayOutputStream();
- CodedOutputStream output = CodedOutputStream.newInstance(byteArrayStream);
- // Zero-sized bytes field.
- output.writeRawVarint32(0);
- // One one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[]{(byte) 23});
- // Another one-byte bytes field
- output.writeRawVarint32(1);
- output.writeRawBytes(new byte[]{(byte) 45});
- // A bytes field large enough that won't fit into the 4K buffer.
- final int bytesLength = 16 * 1024;
- byte[] bytes = new byte[bytesLength];
- bytes[0] = (byte) 67;
- bytes[bytesLength - 1] = (byte) 89;
- output.writeRawVarint32(bytesLength);
- output.writeRawBytes(bytes);
- output.flush();
- byte[] data = byteArrayStream.toByteArray();
-
- // Without aliasing
- CodedInputStream inputStream = CodedInputStream.newInstance(data);
- ByteBuffer result = inputStream.readByteBuffer();
- assertEquals(0, result.capacity());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() != data);
- assertEquals(1, result.capacity());
- assertEquals((byte) 23, result.get());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() != data);
- assertEquals(1, result.capacity());
- assertEquals((byte) 45, result.get());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() != data);
- assertEquals(bytesLength, result.capacity());
- assertEquals((byte) 67, result.get());
- result.position(bytesLength - 1);
- assertEquals((byte) 89, result.get());
-
- // Enable aliasing
- inputStream = CodedInputStream.newInstance(data);
- inputStream.enableAliasing(true);
- result = inputStream.readByteBuffer();
- assertEquals(0, result.capacity());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() == data);
- assertEquals(1, result.capacity());
- assertEquals((byte) 23, result.get());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() == data);
- assertEquals(1, result.capacity());
- assertEquals((byte) 45, result.get());
- result = inputStream.readByteBuffer();
- assertTrue(result.array() == data);
- assertEquals(bytesLength, result.capacity());
- assertEquals((byte) 67, result.get());
- result.position(bytesLength - 1);
- assertEquals((byte) 89, result.get());
- }
-
- public void testCompatibleTypes() throws Exception {
- long data = 0x100000000L;
- Int64Message message = Int64Message.newBuilder().setData(data).build();
- ByteString serialized = message.toByteString();
-
- // Test int64(long) is compatible with bool(boolean)
- BoolMessage msg2 = BoolMessage.parseFrom(serialized);
- assertTrue(msg2.getData());
-
- // Test int64(long) is compatible with int32(int)
- Int32Message msg3 = Int32Message.parseFrom(serialized);
- assertEquals((int) data, msg3.getData());
- }
}

Powered by Google App Engine
This is Rietveld 408576698