diff options
Diffstat (limited to 'java/core/src/test')
45 files changed, 884 insertions, 499 deletions
diff --git a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java index 7dc9fc15..622e36a4 100644 --- a/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/AbstractMessageTest.java @@ -40,9 +40,8 @@ import protobuf_unittest.UnittestProto.TestPackedTypes; import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestRequiredForeign; import protobuf_unittest.UnittestProto.TestUnpackedTypes; -import junit.framework.TestCase; - import java.util.Map; +import junit.framework.TestCase; /** * Unit test for {@link AbstractMessage}. @@ -492,7 +491,6 @@ public class AbstractMessageTest extends TestCase { checkEqualsIsConsistent(eUnknownFields, eUnknownFields2); } - /** * Asserts that the given proto has symmetric equals and hashCode methods. */ diff --git a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java index ec139225..18132e9e 100644 --- a/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/BooleanArrayListTest.java @@ -32,11 +32,10 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; +import junit.framework.TestCase; /** * Tests for {@link BooleanArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java b/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java index cbe742e5..6b1cfe78 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteBufferWriterTest.java @@ -30,13 +30,12 @@ package com.google.protobuf; -import junit.framework.TestCase; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; import java.util.Random; +import junit.framework.TestCase; /** * Tests for {@link ByteBufferWriter}. diff --git a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java index ad9f266e..be71f1f5 100644 --- a/java/core/src/test/java/com/google/protobuf/ByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/ByteStringTest.java @@ -31,9 +31,6 @@ package com.google.protobuf; import com.google.protobuf.ByteString.Output; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -48,6 +45,7 @@ import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Random; +import junit.framework.TestCase; /** * Test methods with implementations in {@link ByteString}, plus do some top-level "integration" diff --git a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java index ca940ced..e2ab0df9 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedInputStreamTest.java @@ -30,20 +30,20 @@ package com.google.protobuf; +import static org.junit.Assert.assertArrayEquals; + 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 junit.framework.TestCase; /** * Unit test for {@link CodedInputStream}. @@ -51,10 +51,45 @@ import java.nio.ByteBuffer; * @author kenton@google.com Kenton Varda */ public class CodedInputStreamTest extends TestCase { + private enum InputType { + ARRAY { + @Override + CodedInputStream newDecoder(byte[] data, int blockSize) { + return CodedInputStream.newInstance(data); + } + }, + NIO_HEAP { + @Override + CodedInputStream newDecoder(byte[] data, int blockSize) { + return CodedInputStream.newInstance(ByteBuffer.wrap(data)); + } + }, + NIO_DIRECT { + @Override + CodedInputStream newDecoder(byte[] data, int blockSize) { + ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); + buffer.put(data); + buffer.flip(); + return CodedInputStream.newInstance(buffer); + } + }, + STREAM { + @Override + CodedInputStream newDecoder(byte[] data, int blockSize) { + return CodedInputStream.newInstance(new SmallBlockInputStream(data, blockSize)); + } + }; + + CodedInputStream newDecoder(byte[] data) { + return newDecoder(data, data.length); + } + + abstract CodedInputStream newDecoder(byte[] data, int blockSize); + } + /** - * Helper to construct a byte array from a bunch of bytes. The inputs are - * actually ints so that I can use hex notation and not get stupid errors - * about precision. + * Helper to construct a byte array from a bunch of bytes. The inputs are actually ints so that I + * can use hex notation and not get stupid errors about precision. */ private byte[] bytes(int... bytesAsInts) { byte[] bytes = new byte[bytesAsInts.length]; @@ -65,19 +100,14 @@ public class CodedInputStreamTest extends TestCase { } /** - * An InputStream which limits the number of bytes it reads at a time. - * We use this to make sure that CodedInputStream doesn't screw up when - * reading in small blocks. + * An InputStream which limits the number of bytes it reads at a time. We use this to make sure + * that CodedInputStream doesn't screw up when reading in small blocks. */ private static final class SmallBlockInputStream extends FilterInputStream { private final int blockSize; public SmallBlockInputStream(byte[] data, int blockSize) { - this(new ByteArrayInputStream(data), blockSize); - } - - public SmallBlockInputStream(InputStream in, int blockSize) { - super(in); + super(new ByteArrayInputStream(data)); this.blockSize = blockSize; } @@ -92,54 +122,36 @@ public class CodedInputStreamTest extends TestCase { } } - private void assertDataConsumed(byte[] data, CodedInputStream input) + private void assertDataConsumed(String msg, byte[] data, CodedInputStream input) throws IOException { - assertEquals(data.length, input.getTotalBytesRead()); - assertTrue(input.isAtEnd()); + assertEquals(msg, data.length, input.getTotalBytesRead()); + assertTrue(msg, input.isAtEnd()); } /** - * Parses the given bytes using readRawVarint32() and readRawVarint64() and - * checks that the result matches the given value. + * 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); - - 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); - - // 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); - - 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); + for (InputType inputType : InputType.values()) { + // Try different block sizes. + for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { + CodedInputStream input = inputType.newDecoder(data, blockSize); + assertEquals(inputType.name(), (int) value, input.readRawVarint32()); + assertDataConsumed(inputType.name(), data, input); + + input = inputType.newDecoder(data, blockSize); + assertEquals(inputType.name(), value, input.readRawVarint64()); + assertDataConsumed(inputType.name(), data, input); + + input = inputType.newDecoder(data, blockSize); + assertEquals(inputType.name(), value, input.readRawVarint64SlowPath()); + assertDataConsumed(inputType.name(), data, input); + + input = inputType.newDecoder(data, blockSize); + assertTrue(inputType.name(), input.skipField(WireFormat.WIRETYPE_VARINT)); + assertDataConsumed(inputType.name(), data, input); + } } // Try reading direct from an InputStream. We want to verify that it @@ -153,35 +165,26 @@ public class CodedInputStreamTest extends TestCase { } /** - * Parses the given bytes using readRawVarint32() and readRawVarint64() and - * expects them to fail with an InvalidProtocolBufferException whose - * description matches the given one. + * Parses the given bytes using readRawVarint32() and readRawVarint64() and expects them to fail + * with an InvalidProtocolBufferException whose description matches the given one. */ - private void assertReadVarintFailure( - InvalidProtocolBufferException expected, byte[] data) + private void assertReadVarintFailure(InvalidProtocolBufferException expected, byte[] data) throws Exception { - CodedInputStream input = CodedInputStream.newInstance(data); - try { - input.readRawVarint32(); - fail("Should have thrown an exception."); - } catch (InvalidProtocolBufferException e) { - assertEquals(expected.getMessage(), e.getMessage()); - } - - input = CodedInputStream.newInstance(data); - try { - input.readRawVarint64(); - fail("Should have thrown an exception."); - } 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()); + for (InputType inputType : InputType.values()) { + try { + CodedInputStream input = inputType.newDecoder(data); + input.readRawVarint32(); + fail(inputType.name() + ": Should have thrown an exception."); + } catch (InvalidProtocolBufferException e) { + assertEquals(inputType.name(), expected.getMessage(), e.getMessage()); + } + try { + CodedInputStream input = inputType.newDecoder(data); + input.readRawVarint64(); + fail(inputType.name() + ": Should have thrown an exception."); + } catch (InvalidProtocolBufferException e) { + assertEquals(inputType.name(), expected.getMessage(), e.getMessage()); + } } // Make sure we get the same error when reading direct from an InputStream. @@ -201,72 +204,74 @@ public class CodedInputStreamTest extends TestCase { // 14882 assertReadVarint(bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7)); // 2961488830 - assertReadVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b), - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (0x0bL << 28)); + assertReadVarint( + bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b), + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | (0x0bL << 28)); // 64-bit // 7256456126 - assertReadVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b), - (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | - (0x1bL << 28)); + assertReadVarint( + bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b), + (0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) | (0x1bL << 28)); // 41256202580718336 assertReadVarint( - bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49), - (0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) | - (0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x49L << 49)); + bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49), + (0x00 << 0) + | (0x66 << 7) + | (0x6b << 14) + | (0x1c << 21) + | (0x43L << 28) + | (0x49L << 35) + | (0x24L << 42) + | (0x49L << 49)); // 11964378330978735131 assertReadVarint( - bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01), - (0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) | - (0x3bL << 28) | (0x56L << 35) | (0x00L << 42) | - (0x05L << 49) | (0x26L << 56) | (0x01L << 63)); + bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01), + (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)); + InvalidProtocolBufferException.malformedVarint(), + bytes(0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00)); + assertReadVarintFailure(InvalidProtocolBufferException.truncatedMessage(), bytes(0x80)); } /** - * Parses the given bytes using readRawLittleEndian32() and checks - * that the result matches the given value. + * Parses the given bytes using readRawLittleEndian32() and checks that the result matches the + * given value. */ - private void assertReadLittleEndian32(byte[] data, int value) - throws Exception { - CodedInputStream input = CodedInputStream.newInstance(data); - assertEquals(value, input.readRawLittleEndian32()); - assertTrue(input.isAtEnd()); - - // Try different block sizes. - for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { - input = CodedInputStream.newInstance( - new SmallBlockInputStream(data, blockSize)); - assertEquals(value, input.readRawLittleEndian32()); - assertTrue(input.isAtEnd()); + private void assertReadLittleEndian32(byte[] data, int value) throws Exception { + for (InputType inputType : InputType.values()) { + // Try different block sizes. + for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { + CodedInputStream input = inputType.newDecoder(data, blockSize); + assertEquals(inputType.name(), value, input.readRawLittleEndian32()); + assertTrue(inputType.name(), input.isAtEnd()); + } } } /** - * Parses the given bytes using readRawLittleEndian64() and checks - * that the result matches the given value. + * Parses the given bytes using readRawLittleEndian64() and checks that the result matches the + * given value. */ - private void assertReadLittleEndian64(byte[] data, long value) - throws Exception { - CodedInputStream input = CodedInputStream.newInstance(data); - assertEquals(value, input.readRawLittleEndian64()); - assertTrue(input.isAtEnd()); - - // Try different block sizes. - for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { - input = CodedInputStream.newInstance( - new SmallBlockInputStream(data, blockSize)); - assertEquals(value, input.readRawLittleEndian64()); - assertTrue(input.isAtEnd()); + private void assertReadLittleEndian64(byte[] data, long value) throws Exception { + for (InputType inputType : InputType.values()) { + // Try different block sizes. + for (int blockSize = 1; blockSize <= 16; blockSize *= 2) { + CodedInputStream input = inputType.newDecoder(data, blockSize); + assertEquals(inputType.name(), value, input.readRawLittleEndian64()); + assertTrue(inputType.name(), input.isAtEnd()); + } } } @@ -276,40 +281,32 @@ public class CodedInputStreamTest extends TestCase { assertReadLittleEndian32(bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0); assertReadLittleEndian64( - bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12), - 0x123456789abcdef0L); + bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12), 0x123456789abcdef0L); assertReadLittleEndian64( - bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a), - 0x9abcdef012345678L); + bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef012345678L); } /** Test decodeZigZag32() and decodeZigZag64(). */ public void testDecodeZigZag() throws Exception { - assertEquals( 0, CodedInputStream.decodeZigZag32(0)); + assertEquals(0, CodedInputStream.decodeZigZag32(0)); assertEquals(-1, CodedInputStream.decodeZigZag32(1)); - assertEquals( 1, CodedInputStream.decodeZigZag32(2)); + assertEquals(1, CodedInputStream.decodeZigZag32(2)); assertEquals(-2, CodedInputStream.decodeZigZag32(3)); assertEquals(0x3FFFFFFF, CodedInputStream.decodeZigZag32(0x7FFFFFFE)); assertEquals(0xC0000000, CodedInputStream.decodeZigZag32(0x7FFFFFFF)); assertEquals(0x7FFFFFFF, CodedInputStream.decodeZigZag32(0xFFFFFFFE)); assertEquals(0x80000000, CodedInputStream.decodeZigZag32(0xFFFFFFFF)); - assertEquals( 0, CodedInputStream.decodeZigZag64(0)); + assertEquals(0, CodedInputStream.decodeZigZag64(0)); assertEquals(-1, CodedInputStream.decodeZigZag64(1)); - assertEquals( 1, CodedInputStream.decodeZigZag64(2)); + assertEquals(1, CodedInputStream.decodeZigZag64(2)); assertEquals(-2, CodedInputStream.decodeZigZag64(3)); - assertEquals(0x000000003FFFFFFFL, - CodedInputStream.decodeZigZag64(0x000000007FFFFFFEL)); - assertEquals(0xFFFFFFFFC0000000L, - CodedInputStream.decodeZigZag64(0x000000007FFFFFFFL)); - assertEquals(0x000000007FFFFFFFL, - CodedInputStream.decodeZigZag64(0x00000000FFFFFFFEL)); - assertEquals(0xFFFFFFFF80000000L, - CodedInputStream.decodeZigZag64(0x00000000FFFFFFFFL)); - assertEquals(0x7FFFFFFFFFFFFFFFL, - CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFEL)); - assertEquals(0x8000000000000000L, - CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFFL)); + assertEquals(0x000000003FFFFFFFL, CodedInputStream.decodeZigZag64(0x000000007FFFFFFEL)); + assertEquals(0xFFFFFFFFC0000000L, CodedInputStream.decodeZigZag64(0x000000007FFFFFFFL)); + assertEquals(0x000000007FFFFFFFL, CodedInputStream.decodeZigZag64(0x00000000FFFFFFFEL)); + assertEquals(0xFFFFFFFF80000000L, CodedInputStream.decodeZigZag64(0x00000000FFFFFFFFL)); + assertEquals(0x7FFFFFFFFFFFFFFFL, CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFEL)); + assertEquals(0x8000000000000000L, CodedInputStream.decodeZigZag64(0xFFFFFFFFFFFFFFFFL)); } /** Tests reading and parsing a whole message with every field type. */ @@ -319,14 +316,12 @@ public class CodedInputStreamTest extends TestCase { byte[] rawBytes = message.toByteArray(); assertEquals(rawBytes.length, message.getSerializedSize()); - TestAllTypes message2 = TestAllTypes.parseFrom(rawBytes); - TestUtil.assertAllFieldsSet(message2); - - // Try different block sizes. - for (int blockSize = 1; blockSize < 256; blockSize *= 2) { - message2 = TestAllTypes.parseFrom( - new SmallBlockInputStream(rawBytes, blockSize)); - TestUtil.assertAllFieldsSet(message2); + for (InputType inputType : InputType.values()) { + // Try different block sizes. + for (int blockSize = 1; blockSize < 256; blockSize *= 2) { + TestAllTypes message2 = TestAllTypes.parseFrom(inputType.newDecoder(rawBytes, blockSize)); + TestUtil.assertAllFieldsSet(message2); + } } } @@ -335,57 +330,65 @@ public class CodedInputStreamTest extends TestCase { TestAllTypes message = TestUtil.getAllSet(); byte[] rawBytes = message.toByteArray(); - // Create two parallel inputs. Parse one as unknown fields while using - // skipField() to skip each field on the other. Expect the same tags. - CodedInputStream input1 = CodedInputStream.newInstance(rawBytes); - CodedInputStream input2 = CodedInputStream.newInstance(rawBytes); + InputType[] inputTypes = InputType.values(); + CodedInputStream[] inputs = new CodedInputStream[inputTypes.length]; + for (int i = 0; i < inputs.length; ++i) { + inputs[i] = inputTypes[i].newDecoder(rawBytes); + } UnknownFieldSet.Builder unknownFields = UnknownFieldSet.newBuilder(); while (true) { + CodedInputStream input1 = inputs[0]; int tag = input1.readTag(); - assertEquals(tag, input2.readTag()); + // Ensure that the rest match. + for (int i = 1; i < inputs.length; ++i) { + assertEquals(inputTypes[i].name(), tag, inputs[i].readTag()); + } if (tag == 0) { break; } unknownFields.mergeFieldFrom(tag, input1); - input2.skipField(tag); + // Skip the field for the rest of the inputs. + for (int i = 1; i < inputs.length; ++i) { + inputs[i].skipField(tag); + } } } /** - * Test that a bug in skipRawBytes() has been fixed: if the skip skips - * exactly up to a limit, this should not break things. + * Test that a bug in skipRawBytes() has been fixed: if the skip skips exactly up to a limit, this + * should not break things. */ public void testSkipRawBytesBug() throws Exception { - byte[] rawBytes = new byte[] { 1, 2 }; - CodedInputStream input = CodedInputStream.newInstance(rawBytes); - - int limit = input.pushLimit(1); - input.skipRawBytes(1); - input.popLimit(limit); - assertEquals(2, input.readRawByte()); + byte[] rawBytes = new byte[] {1, 2}; + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawBytes); + int limit = input.pushLimit(1); + input.skipRawBytes(1); + input.popLimit(limit); + assertEquals(inputType.name(), 2, input.readRawByte()); + } } /** - * Test that a bug in skipRawBytes() has been fixed: if the skip skips - * past the end of a buffer with a limit that has been set past the end of - * that buffer, this should not break things. + * Test that a bug in skipRawBytes() has been fixed: if the skip skips past the end of a buffer + * with a limit that has been set past the end of that buffer, this should not break things. */ public void testSkipRawBytesPastEndOfBufferWithLimit() throws Exception { - byte[] rawBytes = new byte[] { 1, 2, 3, 4, 5 }; - CodedInputStream input = CodedInputStream.newInstance( - new SmallBlockInputStream(rawBytes, 3)); - - int limit = input.pushLimit(4); - // In order to expose the bug we need to read at least one byte to prime the - // buffer inside the CodedInputStream. - assertEquals(1, input.readRawByte()); - // Skip to the end of the limit. - input.skipRawBytes(3); - assertTrue(input.isAtEnd()); - input.popLimit(limit); - assertEquals(5, input.readRawByte()); + byte[] rawBytes = new byte[] {1, 2, 3, 4, 5}; + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawBytes); + int limit = input.pushLimit(4); + // In order to expose the bug we need to read at least one byte to prime the + // buffer inside the CodedInputStream. + assertEquals(inputType.name(), 1, input.readRawByte()); + // Skip to the end of the limit. + input.skipRawBytes(3); + assertTrue(inputType.name(), input.isAtEnd()); + input.popLimit(limit); + assertEquals(inputType.name(), 5, input.readRawByte()); + } } public void testReadHugeBlob() throws Exception { @@ -401,19 +404,22 @@ public class CodedInputStreamTest extends TestCase { builder.setOptionalBytes(ByteString.copyFrom(blob)); TestAllTypes message = builder.build(); - // Serialize and parse it. Make sure to parse from an InputStream, not - // directly from a ByteString, so that CodedInputStream uses buffered - // reading. - TestAllTypes message2 = - TestAllTypes.parseFrom(message.toByteString().newInput()); - - assertEquals(message.getOptionalBytes(), message2.getOptionalBytes()); - - // Make sure all the other fields were parsed correctly. - TestAllTypes message3 = TestAllTypes.newBuilder(message2) - .setOptionalBytes(TestUtil.getAllSet().getOptionalBytes()) - .build(); - TestUtil.assertAllFieldsSet(message3); + byte[] data = message.toByteArray(); + for (InputType inputType : InputType.values()) { + // Serialize and parse it. Make sure to parse from an InputStream, not + // directly from a ByteString, so that CodedInputStream uses buffered + // reading. + TestAllTypes message2 = TestAllTypes.parseFrom(inputType.newDecoder(data)); + + assertEquals(inputType.name(), message.getOptionalBytes(), message2.getOptionalBytes()); + + // Make sure all the other fields were parsed correctly. + TestAllTypes message3 = + TestAllTypes.newBuilder(message2) + .setOptionalBytes(TestUtil.getAllSet().getOptionalBytes()) + .build(); + TestUtil.assertAllFieldsSet(message3); + } } public void testReadMaliciouslyLargeBlob() throws Exception { @@ -423,17 +429,19 @@ public class CodedInputStreamTest extends TestCase { int tag = WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED); output.writeRawVarint32(tag); output.writeRawVarint32(0x7FFFFFFF); - output.writeRawBytes(new byte[32]); // Pad with a few random bytes. + output.writeRawBytes(new byte[32]); // Pad with a few random bytes. output.flush(); - CodedInputStream input = rawOutput.toByteString().newCodedInput(); - assertEquals(tag, input.readTag()); - - try { - input.readBytes(); - fail("Should have thrown an exception!"); - } catch (InvalidProtocolBufferException e) { - // success. + byte[] data = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(data); + assertEquals(tag, input.readTag()); + try { + input.readBytes(); + fail(inputType.name() + ": Should have thrown an exception!"); + } catch (InvalidProtocolBufferException e) { + // success. + } } } @@ -441,54 +449,55 @@ public class CodedInputStreamTest extends TestCase { if (depth == 0) { return TestRecursiveMessage.newBuilder().setI(5).build(); } else { - return TestRecursiveMessage.newBuilder() - .setA(makeRecursiveMessage(depth - 1)).build(); + return TestRecursiveMessage.newBuilder().setA(makeRecursiveMessage(depth - 1)).build(); } } - private void assertMessageDepth(TestRecursiveMessage message, int depth) { + private void assertMessageDepth(String msg, TestRecursiveMessage message, int depth) { if (depth == 0) { - assertFalse(message.hasA()); - assertEquals(5, message.getI()); + assertFalse(msg, message.hasA()); + assertEquals(msg, 5, message.getI()); } else { - assertTrue(message.hasA()); - assertMessageDepth(message.getA(), depth - 1); + assertTrue(msg, message.hasA()); + assertMessageDepth(msg, message.getA(), depth - 1); } } public void testMaliciousRecursion() throws Exception { - ByteString data100 = makeRecursiveMessage(100).toByteString(); - ByteString data101 = makeRecursiveMessage(101).toByteString(); + byte[] data100 = makeRecursiveMessage(100).toByteArray(); + byte[] data101 = makeRecursiveMessage(101).toByteArray(); - assertMessageDepth(TestRecursiveMessage.parseFrom(data100), 100); + for (InputType inputType : InputType.values()) { + assertMessageDepth( + inputType.name(), TestRecursiveMessage.parseFrom(inputType.newDecoder(data100)), 100); - try { - TestRecursiveMessage.parseFrom(data101); - fail("Should have thrown an exception!"); - } catch (InvalidProtocolBufferException e) { - // success. - } + try { + TestRecursiveMessage.parseFrom(inputType.newDecoder(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. + CodedInputStream input = inputType.newDecoder(data100); + input.setRecursionLimit(8); + try { + TestRecursiveMessage.parseFrom(input); + fail(inputType.name() + ": Should have thrown an exception!"); + } catch (InvalidProtocolBufferException e) { + // success. + } } } private void checkSizeLimitExceeded(InvalidProtocolBufferException e) { - assertEquals( - InvalidProtocolBufferException.sizeLimitExceeded().getMessage(), - e.getMessage()); + assertEquals(InvalidProtocolBufferException.sizeLimitExceeded().getMessage(), e.getMessage()); } public void testSizeLimit() throws Exception { - CodedInputStream input = CodedInputStream.newInstance( - new SmallBlockInputStream( - TestUtil.getAllSet().toByteString().newInput(), 16)); + // NOTE: Size limit only applies to the stream-backed CIS. + CodedInputStream input = + CodedInputStream.newInstance( + new SmallBlockInputStream(TestUtil.getAllSet().toByteArray(), 16)); input.setSizeLimit(16); try { @@ -500,8 +509,9 @@ public class CodedInputStreamTest extends TestCase { } public void testResetSizeCounter() throws Exception { - CodedInputStream input = CodedInputStream.newInstance( - new SmallBlockInputStream(new byte[256], 8)); + // NOTE: Size limit only applies to the stream-backed CIS. + CodedInputStream input = + CodedInputStream.newInstance(new SmallBlockInputStream(new byte[256], 8)); input.setSizeLimit(16); input.readRawBytes(16); assertEquals(16, input.getTotalBytesRead()); @@ -515,7 +525,7 @@ public class CodedInputStreamTest extends TestCase { input.resetSizeCounter(); assertEquals(0, input.getTotalBytesRead()); - input.readRawByte(); // No exception thrown. + input.readRawByte(); // No exception thrown. input.resetSizeCounter(); assertEquals(0, input.getTotalBytesRead()); input.readRawBytes(16); @@ -523,7 +533,7 @@ public class CodedInputStreamTest extends TestCase { input.resetSizeCounter(); try { - input.readRawBytes(17); // Hits limit again. + input.readRawBytes(17); // Hits limit again. fail("Should have thrown an exception!"); } catch (InvalidProtocolBufferException expected) { checkSizeLimitExceeded(expected); @@ -531,12 +541,12 @@ public class CodedInputStreamTest extends TestCase { } public void testSizeLimitMultipleMessages() throws Exception { + // NOTE: Size limit only applies to the stream-backed CIS. 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)); + CodedInputStream input = CodedInputStream.newInstance(new SmallBlockInputStream(bytes, 7)); input.setSizeLimit(16); for (int i = 0; i < 256 / 16; i++) { byte[] message = input.readRawBytes(16); @@ -566,12 +576,13 @@ public class CodedInputStreamTest extends TestCase { 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); + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawInput); + assertEquals(inputType.name(), tag, input.readTag()); + String text = input.readString(); + assertEquals(inputType.name(), lorem, text); + } } public void testReadStringRequireUtf8() throws Exception { @@ -591,18 +602,18 @@ public class CodedInputStreamTest extends TestCase { 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); + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawInput); + assertEquals(inputType.name(), tag, input.readTag()); + String text = input.readStringRequireUtf8(); + assertEquals(inputType.name(), lorem, text); + } } /** - * Tests that if we readString invalid UTF-8 bytes, no exception - * is thrown. Instead, the invalid bytes are replaced with the Unicode - * "replacement character" U+FFFD. + * Tests that if we readString invalid UTF-8 bytes, no exception is thrown. Instead, the invalid + * bytes are replaced with the Unicode "replacement character" U+FFFD. */ public void testReadStringInvalidUtf8() throws Exception { ByteString.Output rawOutput = ByteString.newOutput(); @@ -611,18 +622,21 @@ public class CodedInputStreamTest extends TestCase { int tag = WireFormat.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(); - assertEquals(tag, input.readTag()); - String text = input.readString(); - assertEquals(0xfffd, text.charAt(0)); + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawInput); + assertEquals(inputType.name(), tag, input.readTag()); + String text = input.readString(); + assertEquals(inputType.name(), 0xfffd, text.charAt(0)); + } } /** - * Tests that if we readStringRequireUtf8 invalid UTF-8 bytes, an - * InvalidProtocolBufferException is thrown. + * Tests that if we readStringRequireUtf8 invalid UTF-8 bytes, an InvalidProtocolBufferException + * is thrown. */ public void testReadStringRequireUtf8InvalidUtf8() throws Exception { ByteString.Output rawOutput = ByteString.newOutput(); @@ -631,16 +645,20 @@ public class CodedInputStreamTest extends TestCase { int tag = WireFormat.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(); - 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()); + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream input = inputType.newDecoder(rawInput); + assertEquals(tag, input.readTag()); + try { + input.readStringRequireUtf8(); + fail(inputType.name() + ": Expected invalid UTF-8 exception."); + } catch (InvalidProtocolBufferException exception) { + assertEquals( + inputType.name(), "Protocol message had invalid UTF-8.", exception.getMessage()); + } } } @@ -660,13 +678,17 @@ public class CodedInputStreamTest extends TestCase { public void testInvalidTag() throws Exception { // Any tag number which corresponds to field number zero is invalid and // should throw InvalidProtocolBufferException. - for (int i = 0; i < 8; i++) { - try { - CodedInputStream.newInstance(bytes(i)).readTag(); - fail("Should have thrown an exception."); - } catch (InvalidProtocolBufferException e) { - assertEquals(InvalidProtocolBufferException.invalidTag().getMessage(), - e.getMessage()); + for (InputType inputType : InputType.values()) { + for (int i = 0; i < 8; i++) { + try { + inputType.newDecoder(bytes(i)).readTag(); + fail(inputType.name() + ": Should have thrown an exception."); + } catch (InvalidProtocolBufferException e) { + assertEquals( + inputType.name(), + InvalidProtocolBufferException.invalidTag().getMessage(), + e.getMessage()); + } } } } @@ -678,10 +700,10 @@ public class CodedInputStreamTest extends TestCase { output.writeRawVarint32(0); // One one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[] { (byte) 23 }); + output.writeRawBytes(new byte[] {(byte) 23}); // Another one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[] { (byte) 45 }); + 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]; @@ -691,20 +713,24 @@ public class CodedInputStreamTest extends TestCase { 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]); + + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream inputStream = inputType.newDecoder(rawInput); + + byte[] result = inputStream.readByteArray(); + assertEquals(inputType.name(), 0, result.length); + result = inputStream.readByteArray(); + assertEquals(inputType.name(), 1, result.length); + assertEquals(inputType.name(), (byte) 23, result[0]); + result = inputStream.readByteArray(); + assertEquals(inputType.name(), 1, result.length); + assertEquals(inputType.name(), (byte) 45, result[0]); + result = inputStream.readByteArray(); + assertEquals(inputType.name(), bytesLength, result.length); + assertEquals(inputType.name(), (byte) 67, result[0]); + assertEquals(inputType.name(), (byte) 89, result[bytesLength - 1]); + } } public void testReadByteBuffer() throws Exception { @@ -714,10 +740,10 @@ public class CodedInputStreamTest extends TestCase { output.writeRawVarint32(0); // One one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[]{(byte) 23}); + output.writeRawBytes(new byte[] {(byte) 23}); // Another one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[]{(byte) 45}); + 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]; @@ -727,21 +753,25 @@ public class CodedInputStreamTest extends TestCase { 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()); + + byte[] rawInput = rawOutput.toByteString().toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream inputStream = inputType.newDecoder(rawInput); + + ByteBuffer result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), 0, result.capacity()); + result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 23, result.get()); + result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 45, result.get()); + result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), bytesLength, result.capacity()); + assertEquals(inputType.name(), (byte) 67, result.get()); + result.position(bytesLength - 1); + assertEquals(inputType.name(), (byte) 89, result.get()); + } } public void testReadByteBufferAliasing() throws Exception { @@ -751,10 +781,10 @@ public class CodedInputStreamTest extends TestCase { output.writeRawVarint32(0); // One one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[]{(byte) 23}); + output.writeRawBytes(new byte[] {(byte) 23}); // Another one-byte bytes field output.writeRawVarint32(1); - output.writeRawBytes(new byte[]{(byte) 45}); + 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]; @@ -763,59 +793,105 @@ public class CodedInputStreamTest extends TestCase { 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()); + for (InputType inputType : InputType.values()) { + if (inputType == InputType.STREAM) { + // Aliasing doesn't apply to stream-backed CIS. + continue; + } + + // Without aliasing + CodedInputStream inputStream = inputType.newDecoder(data); + ByteBuffer result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), 0, result.capacity()); + result = inputStream.readByteBuffer(); + assertTrue(inputType.name(), result.array() != data); + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 23, result.get()); + result = inputStream.readByteBuffer(); + assertTrue(inputType.name(), result.array() != data); + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 45, result.get()); + result = inputStream.readByteBuffer(); + assertTrue(inputType.name(), result.array() != data); + assertEquals(inputType.name(), bytesLength, result.capacity()); + assertEquals(inputType.name(), (byte) 67, result.get()); + result.position(bytesLength - 1); + assertEquals(inputType.name(), (byte) 89, result.get()); + + // Enable aliasing + inputStream = inputType.newDecoder(data); + inputStream.enableAliasing(true); + result = inputStream.readByteBuffer(); + assertEquals(inputType.name(), 0, result.capacity()); + result = inputStream.readByteBuffer(); + if (result.hasArray()) { + assertTrue(inputType.name(), result.array() == data); + } + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 23, result.get()); + result = inputStream.readByteBuffer(); + if (result.hasArray()) { + assertTrue(inputType.name(), result.array() == data); + } + assertEquals(inputType.name(), 1, result.capacity()); + assertEquals(inputType.name(), (byte) 45, result.get()); + result = inputStream.readByteBuffer(); + if (result.hasArray()) { + assertTrue(inputType.name(), result.array() == data); + } + assertEquals(inputType.name(), bytesLength, result.capacity()); + assertEquals(inputType.name(), (byte) 67, result.get()); + result.position(bytesLength - 1); + assertEquals(inputType.name(), (byte) 89, result.get()); + } } public void testCompatibleTypes() throws Exception { long data = 0x100000000L; Int64Message message = Int64Message.newBuilder().setData(data).build(); - ByteString serialized = message.toByteString(); + byte[] serialized = message.toByteArray(); + for (InputType inputType : InputType.values()) { + CodedInputStream inputStream = inputType.newDecoder(serialized); + + // Test int64(long) is compatible with bool(boolean) + BoolMessage msg2 = BoolMessage.parseFrom(inputStream); + assertTrue(msg2.getData()); + + // Test int64(long) is compatible with int32(int) + inputStream = inputType.newDecoder(serialized); + Int32Message msg3 = Int32Message.parseFrom(inputStream); + assertEquals((int) data, msg3.getData()); + } + } - // Test int64(long) is compatible with bool(boolean) - BoolMessage msg2 = BoolMessage.parseFrom(serialized); - assertTrue(msg2.getData()); + public void testSkipInvalidVarint_FastPath() throws Exception { + // Fast path: We have >= 10 bytes available. Ensure we properly recognize a non-ending varint. + byte[] data = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0}; + for (InputType inputType : InputType.values()) { + try { + CodedInputStream input = inputType.newDecoder(data); + input.skipField(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT)); + fail(inputType.name() + ": Should have thrown an exception."); + } catch (InvalidProtocolBufferException e) { + // Expected + } + } + } - // Test int64(long) is compatible with int32(int) - Int32Message msg3 = Int32Message.parseFrom(serialized); - assertEquals((int) data, msg3.getData()); + public void testSkipInvalidVarint_SlowPath() throws Exception { + // Slow path: < 10 bytes available. Ensure we properly recognize a non-ending varint. + byte[] data = new byte[] {-1, -1, -1, -1, -1, -1, -1, -1, -1}; + for (InputType inputType : InputType.values()) { + try { + CodedInputStream input = inputType.newDecoder(data); + input.skipField(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT)); + fail(inputType.name() + ": Should have thrown an exception."); + } catch (InvalidProtocolBufferException e) { + // Expected + } + } } } diff --git a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java index 33aa4357..78f415c2 100644 --- a/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java +++ b/java/core/src/test/java/com/google/protobuf/CodedOutputStreamTest.java @@ -35,15 +35,13 @@ import protobuf_unittest.UnittestProto.SparseEnumMessage; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestPackedTypes; import protobuf_unittest.UnittestProto.TestSparseEnum; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import junit.framework.TestCase; /** * Unit test for {@link CodedOutputStream}. @@ -151,16 +149,21 @@ public class CodedOutputStreamTest extends TestCase { private final int initialPosition; private final CodedOutputStream stream; private final ByteBuffer buffer; + private final boolean unsafe; - NioDirectCoder(int size) { - this(size, 0); + NioDirectCoder(int size, boolean unsafe) { + this(size, 0, unsafe); } - NioDirectCoder(int size, int initialPosition) { + NioDirectCoder(int size, int initialPosition, boolean unsafe) { + this.unsafe = unsafe; this.initialPosition = initialPosition; buffer = ByteBuffer.allocateDirect(size); buffer.position(initialPosition); - stream = CodedOutputStream.newInstance(buffer); + stream = + unsafe + ? CodedOutputStream.newUnsafeInstance(buffer) + : CodedOutputStream.newSafeInstance(buffer); } @Override @@ -181,7 +184,7 @@ public class CodedOutputStreamTest extends TestCase { @Override public OutputType getOutputType() { - return OutputType.NIO_DIRECT; + return unsafe ? OutputType.NIO_DIRECT_SAFE : OutputType.NIO_DIRECT_UNSAFE; } } @@ -198,10 +201,16 @@ public class CodedOutputStreamTest extends TestCase { return new NioHeapCoder(size); } }, - NIO_DIRECT() { + NIO_DIRECT_SAFE() { @Override Coder newCoder(int size) { - return new NioDirectCoder(size); + return new NioDirectCoder(size, false); + } + }, + NIO_DIRECT_UNSAFE() { + @Override + Coder newCoder(int size) { + return new NioDirectCoder(size, true); } }, STREAM() { @@ -389,6 +398,7 @@ public class CodedOutputStreamTest extends TestCase { != CodedOutputStream.computeUInt32SizeNoTag(string.length() * Utf8.MAX_BYTES_PER_CHAR)); coder.stream().writeStringNoTag(string); + coder.stream().flush(); int stringSize = CodedOutputStream.computeStringSizeNoTag(string); // Verify that the total bytes written is correct @@ -478,11 +488,12 @@ public class CodedOutputStreamTest extends TestCase { public void testWriteByteArrayWithOffsets() throws Exception { byte[] fullArray = bytes(0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88); - byte[] destination = new byte[4]; - CodedOutputStream codedStream = CodedOutputStream.newInstance(destination); - codedStream.writeByteArrayNoTag(fullArray, 2, 2); - assertEqualBytes(OutputType.ARRAY, bytes(0x02, 0x33, 0x44, 0x00), destination); - assertEquals(3, codedStream.getTotalBytesWritten()); + for (OutputType type : new OutputType[] {OutputType.ARRAY}) { + Coder coder = type.newCoder(4); + coder.stream().writeByteArrayNoTag(fullArray, 2, 2); + assertEqualBytes(type, bytes(0x02, 0x33, 0x44), coder.toByteArray()); + assertEquals(3, coder.stream().getTotalBytesWritten()); + } } public void testSerializeUtf8_MultipleSmallWrites() throws Exception { @@ -561,7 +572,12 @@ public class CodedOutputStreamTest extends TestCase { // Tag is one byte, varint describing string length is 1 byte, string length is 9 bytes. // An array of size 1 will cause a failure when trying to write the varint. for (OutputType outputType : - new OutputType[] {OutputType.ARRAY, OutputType.NIO_HEAP, OutputType.NIO_DIRECT}) { + new OutputType[] { + OutputType.ARRAY, + OutputType.NIO_HEAP, + OutputType.NIO_DIRECT_SAFE, + OutputType.NIO_DIRECT_UNSAFE + }) { for (int i = 0; i < 11; i++) { Coder coder = outputType.newCoder(i); try { @@ -599,10 +615,13 @@ public class CodedOutputStreamTest extends TestCase { public void testNioEncodersWithInitialOffsets() throws Exception { String value = "abc"; - for (Coder coder : new Coder[] {new NioHeapCoder(10, 2), new NioDirectCoder(10, 2)}) { + for (Coder coder : + new Coder[] { + new NioHeapCoder(10, 2), new NioDirectCoder(10, 2, false), new NioDirectCoder(10, 2, true) + }) { coder.stream().writeStringNoTag(value); coder.stream().flush(); - assertEqualBytes(coder.getOutputType(), new byte[]{3, 'a', 'b', 'c'}, coder.toByteArray()); + assertEqualBytes(coder.getOutputType(), new byte[] {3, 'a', 'b', 'c'}, coder.toByteArray()); } } diff --git a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java index ce85ae2e..2c272a73 100644 --- a/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java +++ b/java/core/src/test/java/com/google/protobuf/DeprecatedFieldTest.java @@ -31,11 +31,9 @@ package com.google.protobuf; import protobuf_unittest.UnittestProto.TestDeprecatedFields; - -import junit.framework.TestCase; - import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Method; +import junit.framework.TestCase; /** * Test field deprecation diff --git a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java index b3302441..b60cd620 100644 --- a/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java +++ b/java/core/src/test/java/com/google/protobuf/DescriptorsTest.java @@ -55,15 +55,15 @@ import protobuf_unittest.UnittestProto.ForeignMessage; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; +import protobuf_unittest.UnittestProto.TestJsonName; import protobuf_unittest.UnittestProto.TestMultipleExtensionRanges; import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestReservedFields; import protobuf_unittest.UnittestProto.TestService; -import junit.framework.TestCase; - import java.util.Arrays; import java.util.Collections; import java.util.List; +import junit.framework.TestCase; /** * Unit test for {@link Descriptors}. @@ -805,4 +805,15 @@ public class DescriptorsTest extends TestCase { Descriptors.FileDescriptor.buildFrom( fileDescriptorProto, new FileDescriptor[0]); } + + public void testFieldJsonName() throws Exception { + Descriptor d = TestJsonName.getDescriptor(); + assertEquals(6, d.getFields().size()); + assertEquals("fieldName1", d.getFields().get(0).getJsonName()); + assertEquals("fieldName2", d.getFields().get(1).getJsonName()); + assertEquals("FieldName3", d.getFields().get(2).getJsonName()); + assertEquals("FieldName4", d.getFields().get(3).getJsonName()); + assertEquals("FIELDNAME5", d.getFields().get(4).getJsonName()); + assertEquals("@type", d.getFields().get(5).getJsonName()); + } } diff --git a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java index 8e8e4fe2..d8942792 100644 --- a/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/DoubleArrayListTest.java @@ -32,11 +32,10 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; +import junit.framework.TestCase; /** * Tests for {@link DoubleArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java index 8f3a74c1..77d14f6b 100644 --- a/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/DynamicMessageTest.java @@ -37,10 +37,8 @@ import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestEmptyMessage; import protobuf_unittest.UnittestProto.TestPackedTypes; - -import junit.framework.TestCase; - import java.util.Arrays; +import junit.framework.TestCase; /** * Unit test for {@link DynamicMessage}. See also {@link MessageTest}, which diff --git a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java index c1246782..6157e589 100644 --- a/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java +++ b/java/core/src/test/java/com/google/protobuf/ExtensionRegistryFactoryTest.java @@ -32,26 +32,24 @@ package com.google.protobuf; import protobuf_unittest.NonNestedExtension; import protobuf_unittest.NonNestedExtensionLite; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - import java.lang.reflect.Method; import java.net.URLClassLoader; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.Set; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; /** * Tests for {@link ExtensionRegistryFactory} and the {@link ExtensionRegistry} instances it * creates. - * + * * <p>This test simulates the runtime behaviour of the ExtensionRegistryFactory by delegating test * definitions to two inner classes {@link InnerTest} and {@link InnerLiteTest}, the latter of * which is executed using a custom ClassLoader, simulating the ProtoLite environment. - * + * * <p>The test mechanism employed here is based on the pattern in * {@code com.google.common.util.concurrent.AbstractFutureFallbackAtomicHelperTest} */ @@ -68,6 +66,7 @@ public class ExtensionRegistryFactoryTest extends TestCase { void testEmpty(); void testIsFullRegistry(); void testAdd(); + void testAdd_immutable(); } /** @@ -125,6 +124,29 @@ public class ExtensionRegistryFactoryTest extends TestCase { assertNotNull("Extension is registered in masqueraded full registry", fullRegistry2.findImmutableExtensionByName("protobuf_unittest.nonNestedExtension")); } + + @Override + public void testAdd_immutable() { + ExtensionRegistryLite registry1 = ExtensionRegistryLite.newInstance().getUnmodifiable(); + try { + NonNestedExtensionLite.registerAllExtensions(registry1); + fail(); + } catch (UnsupportedOperationException expected) {} + try { + registry1.add(NonNestedExtensionLite.nonNestedExtensionLite); + fail(); + } catch (UnsupportedOperationException expected) {} + + ExtensionRegistryLite registry2 = ExtensionRegistryLite.newInstance().getUnmodifiable(); + try { + NonNestedExtension.registerAllExtensions((ExtensionRegistry) registry2); + fail(); + } catch (IllegalArgumentException expected) {} + try { + registry2.add(NonNestedExtension.nonNestedExtension); + fail(); + } catch (IllegalArgumentException expected) {} + } } /** @@ -162,12 +184,21 @@ public class ExtensionRegistryFactoryTest extends TestCase { NonNestedExtensionLite.MessageLiteToBeExtended.getDefaultInstance(), 1); assertNotNull("Extension is registered in Lite registry", extension); } + + @Override + public void testAdd_immutable() { + ExtensionRegistryLite registry = ExtensionRegistryLite.newInstance().getUnmodifiable(); + try { + NonNestedExtensionLite.registerAllExtensions(registry); + fail(); + } catch (UnsupportedOperationException expected) {} + } } /** * Defines a suite of tests which the JUnit3 runner retrieves by reflection. */ - public static Test suite() { + public static Test suite() { TestSuite suite = new TestSuite(); for (Method method : RegistryTests.class.getMethods()) { suite.addTest(TestSuite.createTest(ExtensionRegistryFactoryTest.class, method.getName())); diff --git a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java index 0e13a598..aa36be49 100644 --- a/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/FloatArrayListTest.java @@ -32,11 +32,10 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; +import junit.framework.TestCase; /** * Tests for {@link FloatArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java index 127e06fd..3eece26a 100644 --- a/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/GeneratedMessageTest.java @@ -65,9 +65,6 @@ import protobuf_unittest.UnittestProto.TestExtremeDefaultValues; import protobuf_unittest.UnittestProto.TestOneof2; import protobuf_unittest.UnittestProto.TestPackedTypes; import protobuf_unittest.UnittestProto.TestUnpackedTypes; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; @@ -76,6 +73,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.Iterator; import java.util.List; +import junit.framework.TestCase; /** * Unit test for generated messages and generated code. See also diff --git a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java index e59e3c6e..60c85450 100644 --- a/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/IntArrayListTest.java @@ -32,11 +32,10 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; +import junit.framework.TestCase; /** * Tests for {@link IntArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java index 8d1de6dc..efdfdfbe 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyFieldLiteTest.java @@ -35,10 +35,8 @@ import static protobuf_unittest.UnittestProto.optionalInt64Extension; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; - -import junit.framework.TestCase; - import java.io.IOException; +import junit.framework.TestCase; /** * Unit test for {@link LazyFieldLite}. diff --git a/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java b/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java index ce473ae9..5f013f3c 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyFieldTest.java @@ -32,10 +32,8 @@ package com.google.protobuf; import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; - -import junit.framework.TestCase; - import java.io.IOException; +import junit.framework.TestCase; /** * Unit test for {@link LazyField}. diff --git a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java index e5b11cf1..c8ca9b6a 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyMessageLiteTest.java @@ -34,10 +34,8 @@ import protobuf_unittest.LazyFieldsLite.LazyExtension; import protobuf_unittest.LazyFieldsLite.LazyInnerMessageLite; import protobuf_unittest.LazyFieldsLite.LazyMessageLite; import protobuf_unittest.LazyFieldsLite.LazyNestedInnerMessageLite; - -import junit.framework.TestCase; - import java.util.ArrayList; +import junit.framework.TestCase; /** * Unit test for messages with lazy fields. diff --git a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java index 497c4df2..d2bee2a4 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyStringArrayListTest.java @@ -32,13 +32,12 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.ArrayList; import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; +import junit.framework.TestCase; /** * Tests for {@link LazyStringArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java b/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java index 0ef414aa..006e4933 100644 --- a/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java +++ b/java/core/src/test/java/com/google/protobuf/LazyStringEndToEndTest.java @@ -32,10 +32,8 @@ package com.google.protobuf; import protobuf_unittest.UnittestProto; - -import junit.framework.TestCase; - import java.io.IOException; +import junit.framework.TestCase; /** * Tests to make sure the lazy conversion of UTF8-encoded byte arrays to diff --git a/java/core/src/test/java/com/google/protobuf/LiteTest.java b/java/core/src/test/java/com/google/protobuf/LiteTest.java index b3a246dc..254beba1 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteTest.java @@ -35,7 +35,6 @@ import static java.util.Collections.singletonList; import com.google.protobuf.UnittestImportLite.ImportEnumLite; import com.google.protobuf.UnittestImportPublicLite.PublicImportMessageLite; -import com.google.protobuf.UnittestLite; import com.google.protobuf.UnittestLite.ForeignEnumLite; import com.google.protobuf.UnittestLite.ForeignMessageLite; import com.google.protobuf.UnittestLite.TestAllExtensionsLite; @@ -52,15 +51,8 @@ import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.BarPrime; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestOneofEquals; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.TestRecursiveOneof; - import junit.framework.TestCase; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.NotSerializableException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; - /** * Test lite runtime. * @@ -139,7 +131,6 @@ public class LiteTest extends TestCase { UnittestLite.optionalNestedMessageExtensionLite).getBb()); } - public void testClone() { TestAllTypesLite.Builder expected = TestAllTypesLite.newBuilder() .setOptionalInt32(123); diff --git a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java index 2e7792a8..eac47448 100644 --- a/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java @@ -30,8 +30,6 @@ package com.google.protobuf; -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.EOFException; @@ -45,6 +43,7 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import junit.framework.TestCase; /** * Test {@code LiteralByteString} by setting up a reference string in {@link #setUp()}. diff --git a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java index 6aaf85d7..6bbdfcaa 100644 --- a/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/LongArrayListTest.java @@ -32,11 +32,10 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; +import junit.framework.TestCase; /** * Tests for {@link LongArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java index 04d58006..8b64fa27 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2LiteTest.java @@ -35,15 +35,13 @@ import map_lite_test.MapForProto2TestProto.TestMap; import map_lite_test.MapForProto2TestProto.TestMap.MessageValue; import map_lite_test.MapForProto2TestProto.TestMapOrBuilder; import map_lite_test.MapForProto2TestProto.TestUnknownEnumValue; - -import junit.framework.TestCase; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Map; +import junit.framework.TestCase; /** * Unit tests for map fields. diff --git a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java index e8246bf6..76bd7bd9 100644 --- a/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java +++ b/java/core/src/test/java/com/google/protobuf/MapForProto2Test.java @@ -32,14 +32,14 @@ package com.google.protobuf; import com.google.protobuf.Descriptors.FieldDescriptor; import map_test.MapForProto2TestProto.BizarroTestMap; +import map_test.MapForProto2TestProto.ReservedAsMapField; +import map_test.MapForProto2TestProto.ReservedAsMapFieldWithEnumValue; import map_test.MapForProto2TestProto.TestMap; import map_test.MapForProto2TestProto.TestMap.MessageValue; import map_test.MapForProto2TestProto.TestMap.MessageWithRequiredFields; import map_test.MapForProto2TestProto.TestMapOrBuilder; import map_test.MapForProto2TestProto.TestRecursiveMap; import map_test.MapForProto2TestProto.TestUnknownEnumValue; -import junit.framework.TestCase; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -47,6 +47,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import junit.framework.TestCase; /** * Unit tests for map fields in proto2 protos. @@ -1143,6 +1144,11 @@ public class MapForProto2Test extends TestCase { assertEquals(2, mapEntry.getAllFields().size()); } + public void testReservedWordsFieldNames() { + ReservedAsMapField.newBuilder().build(); + ReservedAsMapFieldWithEnumValue.newBuilder().build(); + } + private static <K, V> Map<K, V> newMap(K key1, V value1) { Map<K, V> map = new HashMap<K, V>(); map.put(key1, value1); diff --git a/java/core/src/test/java/com/google/protobuf/MapTest.java b/java/core/src/test/java/com/google/protobuf/MapTest.java index caef246b..035a8335 100644 --- a/java/core/src/test/java/com/google/protobuf/MapTest.java +++ b/java/core/src/test/java/com/google/protobuf/MapTest.java @@ -36,12 +36,12 @@ import com.google.protobuf.Descriptors.EnumDescriptor; import com.google.protobuf.Descriptors.EnumValueDescriptor; import com.google.protobuf.Descriptors.FieldDescriptor; import map_test.MapTestProto.BizarroTestMap; +import map_test.MapTestProto.ReservedAsMapField; +import map_test.MapTestProto.ReservedAsMapFieldWithEnumValue; import map_test.MapTestProto.TestMap; import map_test.MapTestProto.TestMap.MessageValue; import map_test.MapTestProto.TestMapOrBuilder; import map_test.MapTestProto.TestOnChangeEventPropagation; -import junit.framework.TestCase; - import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.ArrayList; @@ -49,6 +49,7 @@ import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import junit.framework.TestCase; /** * Unit tests for map fields. @@ -1307,6 +1308,171 @@ public class MapTest extends TestCase { } } + public void testReservedWordsFieldNames() { + ReservedAsMapField.newBuilder().build(); + ReservedAsMapFieldWithEnumValue.newBuilder().build(); + } + + public void testDeterministicSerialziation() throws Exception { + TestMap.Builder builder = TestMap.newBuilder(); + // int32->int32 + builder.putInt32ToInt32Field(5, 1); + builder.putInt32ToInt32Field(1, 1); + builder.putInt32ToInt32Field(4, 1); + builder.putInt32ToInt32Field(-2, 1); + builder.putInt32ToInt32Field(0, 1); + + // uint32->int32 + builder.putUint32ToInt32Field(5, 1); + builder.putUint32ToInt32Field(1, 1); + builder.putUint32ToInt32Field(4, 1); + builder.putUint32ToInt32Field(-2, 1); + builder.putUint32ToInt32Field(0, 1); + + // int64->int32 + builder.putInt64ToInt32Field(5L, 1); + builder.putInt64ToInt32Field(1L, 1); + builder.putInt64ToInt32Field(4L, 1); + builder.putInt64ToInt32Field(-2L, 1); + builder.putInt64ToInt32Field(0L, 1); + + // string->int32 + builder.putStringToInt32Field("baz", 1); + builder.putStringToInt32Field("foo", 1); + builder.putStringToInt32Field("bar", 1); + builder.putStringToInt32Field("", 1); + builder.putStringToInt32Field("hello", 1); + builder.putStringToInt32Field("world", 1); + + TestMap message = builder.build(); + byte[] serialized = new byte[message.getSerializedSize()]; + CodedOutputStream output = CodedOutputStream.newInstance(serialized); + output.useDeterministicSerialization(); + message.writeTo(output); + output.flush(); + + CodedInputStream input = CodedInputStream.newInstance(serialized); + List<Integer> int32Keys = new ArrayList<Integer>(); + List<Integer> uint32Keys = new ArrayList<Integer>(); + List<Long> int64Keys = new ArrayList<Long>(); + List<String> stringKeys = new ArrayList<String>(); + int tag; + while (true) { + tag = input.readTag(); + if (tag == 0) { + break; + } + int length = input.readRawVarint32(); + int oldLimit = input.pushLimit(length); + switch (WireFormat.getTagFieldNumber(tag)) { + case TestMap.STRING_TO_INT32_FIELD_FIELD_NUMBER: + stringKeys.add(readMapStringKey(input)); + break; + case TestMap.INT32_TO_INT32_FIELD_FIELD_NUMBER: + int32Keys.add(readMapIntegerKey(input)); + break; + case TestMap.UINT32_TO_INT32_FIELD_FIELD_NUMBER: + uint32Keys.add(readMapIntegerKey(input)); + break; + case TestMap.INT64_TO_INT32_FIELD_FIELD_NUMBER: + int64Keys.add(readMapLongKey(input)); + break; + default: + fail("Unexpected fields."); + } + input.popLimit(oldLimit); + } + assertEquals( + Arrays.asList(-2, 0, 1, 4, 5), + int32Keys); + assertEquals( + Arrays.asList(-2, 0, 1, 4, 5), + uint32Keys); + assertEquals( + Arrays.asList(-2L, 0L, 1L, 4L, 5L), + int64Keys); + assertEquals( + Arrays.asList("", "bar", "baz", "foo", "hello", "world"), + stringKeys); + } + + public void testInitFromPartialDynamicMessage() { + FieldDescriptor fieldDescriptor = + TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD_FIELD_NUMBER); + Descriptor mapEntryType = fieldDescriptor.getMessageType(); + FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); + FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); + DynamicMessage dynamicMessage = + DynamicMessage.newBuilder(TestMap.getDescriptor()) + .addRepeatedField( + fieldDescriptor, + DynamicMessage.newBuilder(mapEntryType) + .setField(keyField, 10) + .setField(valueField, TestMap.MessageValue.newBuilder().setValue(10).build()) + .build()) + .build(); + TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); + assertEquals( + TestMap.MessageValue.newBuilder().setValue(10).build(), + message.getInt32ToMessageFieldMap().get(10)); + } + + public void testInitFromFullyDynamicMessage() { + FieldDescriptor fieldDescriptor = + TestMap.getDescriptor().findFieldByNumber(TestMap.INT32_TO_MESSAGE_FIELD_FIELD_NUMBER); + Descriptor mapEntryType = fieldDescriptor.getMessageType(); + FieldDescriptor keyField = mapEntryType.findFieldByNumber(1); + FieldDescriptor valueField = mapEntryType.findFieldByNumber(2); + DynamicMessage dynamicMessage = + DynamicMessage.newBuilder(TestMap.getDescriptor()) + .addRepeatedField( + fieldDescriptor, + DynamicMessage.newBuilder(mapEntryType) + .setField(keyField, 10) + .setField( + valueField, + DynamicMessage.newBuilder(TestMap.MessageValue.getDescriptor()) + .setField( + TestMap.MessageValue.getDescriptor().findFieldByName("value"), 10) + .build()) + .build()) + .build(); + TestMap message = TestMap.newBuilder().mergeFrom(dynamicMessage).build(); + assertEquals( + TestMap.MessageValue.newBuilder().setValue(10).build(), + message.getInt32ToMessageFieldMap().get(10)); + } + + private int readMapIntegerKey(CodedInputStream input) throws IOException { + int tag = input.readTag(); + assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); + int ret = input.readInt32(); + // skip the value field. + input.skipField(input.readTag()); + assertTrue(input.isAtEnd()); + return ret; + } + + private long readMapLongKey(CodedInputStream input) throws IOException { + int tag = input.readTag(); + assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_VARINT), tag); + long ret = input.readInt64(); + // skip the value field. + input.skipField(input.readTag()); + assertTrue(input.isAtEnd()); + return ret; + } + + private String readMapStringKey(CodedInputStream input) throws IOException { + int tag = input.readTag(); + assertEquals(WireFormat.makeTag(1, WireFormat.WIRETYPE_LENGTH_DELIMITED), tag); + String ret = input.readString(); + // skip the value field. + input.skipField(input.readTag()); + assertTrue(input.isAtEnd()); + return ret; + } + private static <K, V> Map<K, V> newMap(K key1, V value1) { Map<K, V> map = new HashMap<K, V>(); map.put(key1, value1); diff --git a/java/core/src/test/java/com/google/protobuf/MessageTest.java b/java/core/src/test/java/com/google/protobuf/MessageTest.java index dcd1aba7..75b79a34 100644 --- a/java/core/src/test/java/com/google/protobuf/MessageTest.java +++ b/java/core/src/test/java/com/google/protobuf/MessageTest.java @@ -35,10 +35,8 @@ import protobuf_unittest.UnittestProto.TestAllExtensions; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestRequired; import protobuf_unittest.UnittestProto.TestRequiredForeign; - -import junit.framework.TestCase; - import java.util.List; +import junit.framework.TestCase; /** * Misc. unit tests for message operations that apply to both generated diff --git a/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java b/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java index 542e28c0..03ed65a5 100644 --- a/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java +++ b/java/core/src/test/java/com/google/protobuf/NestedBuildersTest.java @@ -32,11 +32,9 @@ package com.google.protobuf; import protobuf_unittest.Vehicle; import protobuf_unittest.Wheel; - -import junit.framework.TestCase; - import java.util.ArrayList; import java.util.List; +import junit.framework.TestCase; /** * Test cases that exercise end-to-end use cases involving diff --git a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java index 6be5b93c..c388bd05 100644 --- a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java +++ b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java @@ -32,8 +32,6 @@ package com.google.protobuf; import static com.google.protobuf.Internal.UTF_8; -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.EOFException; @@ -48,6 +46,7 @@ import java.nio.ByteBuffer; import java.util.Arrays; import java.util.List; import java.util.NoSuchElementException; +import junit.framework.TestCase; /** * Tests for {@link NioByteString}. diff --git a/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java b/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java index bf1f1d71..e376b1cd 100644 --- a/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParseExceptionsTest.java @@ -36,16 +36,14 @@ import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import com.google.protobuf.DescriptorProtos.DescriptorProto; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FilterInputStream; import java.io.IOException; import java.io.InputStream; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; /** * Tests the exceptions thrown when parsing from a stream. The methods on the {@link Parser} diff --git a/java/core/src/test/java/com/google/protobuf/ParserTest.java b/java/core/src/test/java/com/google/protobuf/ParserTest.java index 30842d2c..7086912b 100644 --- a/java/core/src/test/java/com/google/protobuf/ParserTest.java +++ b/java/core/src/test/java/com/google/protobuf/ParserTest.java @@ -42,13 +42,11 @@ import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestEmptyMessage; import protobuf_unittest.UnittestProto.TestParsingMerge; import protobuf_unittest.UnittestProto.TestRequired; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import junit.framework.TestCase; /** * Unit test for {@link Parser}. diff --git a/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java index 3f45e226..af717bfd 100644 --- a/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java +++ b/java/core/src/test/java/com/google/protobuf/ProtobufArrayListTest.java @@ -32,12 +32,11 @@ package com.google.protobuf; import static java.util.Arrays.asList; -import junit.framework.TestCase; - import java.util.Collections; import java.util.ConcurrentModificationException; import java.util.Iterator; import java.util.List; +import junit.framework.TestCase; /** * Tests for {@link ProtobufArrayList}. diff --git a/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java b/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java index 241a4354..edbd0afd 100644 --- a/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java +++ b/java/core/src/test/java/com/google/protobuf/RepeatedFieldBuilderV3Test.java @@ -32,11 +32,9 @@ package com.google.protobuf; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestAllTypesOrBuilder; - -import junit.framework.TestCase; - import java.util.Collections; import java.util.List; +import junit.framework.TestCase; /** * Tests for {@link RepeatedFieldBuilderV3}. This tests basic functionality. diff --git a/java/core/src/test/java/com/google/protobuf/ServiceTest.java b/java/core/src/test/java/com/google/protobuf/ServiceTest.java index b902737d..b895ad8d 100644 --- a/java/core/src/test/java/com/google/protobuf/ServiceTest.java +++ b/java/core/src/test/java/com/google/protobuf/ServiceTest.java @@ -42,15 +42,13 @@ import protobuf_unittest.UnittestProto.FooResponse; import protobuf_unittest.UnittestProto.TestAllTypes; import protobuf_unittest.UnittestProto.TestService; +import java.util.HashSet; +import java.util.Set; import junit.framework.TestCase; - import org.easymock.classextension.EasyMock; import org.easymock.IArgumentMatcher; import org.easymock.classextension.IMocksControl; -import java.util.HashSet; -import java.util.Set; - /** * Tests services and stubs. * @@ -271,6 +269,8 @@ public class ServiceTest extends TestCase { file.getServices().get(0).getMethods().get(0).getName()); } + + // ================================================================= /** diff --git a/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java b/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java index e96ecd65..a7f8342d 100644 --- a/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java +++ b/java/core/src/test/java/com/google/protobuf/SmallSortedMapTest.java @@ -30,8 +30,6 @@ package com.google.protobuf; -import junit.framework.TestCase; - import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -40,6 +38,7 @@ import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; +import junit.framework.TestCase; /** * @author darick@google.com Darick Tong diff --git a/java/core/src/test/java/com/google/protobuf/TestUtil.java b/java/core/src/test/java/com/google/protobuf/TestUtil.java index d94f99e3..d4a18a22 100644 --- a/java/core/src/test/java/com/google/protobuf/TestUtil.java +++ b/java/core/src/test/java/com/google/protobuf/TestUtil.java @@ -232,12 +232,10 @@ import protobuf_unittest.UnittestProto.TestOneof2; import protobuf_unittest.UnittestProto.TestPackedExtensions; import protobuf_unittest.UnittestProto.TestPackedTypes; import protobuf_unittest.UnittestProto.TestUnpackedTypes; - -import junit.framework.Assert; - import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; +import junit.framework.Assert; /** * Contains methods for setting all fields of {@code TestAllTypes} to @@ -260,6 +258,13 @@ public final class TestUtil { } /** + * Dirties the message by resetting the momoized serialized size. + */ + public static void resetMemoizedSize(AbstractMessage message) { + message.memoizedSize = -1; + } + + /** * Get a {@code TestAllTypes} with all fields set as they would be by * {@link #setAllFields(TestAllTypes.Builder)}. */ diff --git a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java index 14783b0a..6a91b02f 100644 --- a/java/core/src/test/java/com/google/protobuf/TextFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/TextFormatTest.java @@ -42,11 +42,9 @@ import protobuf_unittest.UnittestProto.TestAllTypes.NestedMessage; import protobuf_unittest.UnittestProto.TestEmptyMessage; import protobuf_unittest.UnittestProto.TestOneof2; import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; - -import junit.framework.TestCase; - import java.io.StringReader; import java.util.List; +import junit.framework.TestCase; /** * Test case for {@link TextFormat}. @@ -992,10 +990,35 @@ public class TextFormatTest extends TestCase { assertParseSuccessWithOverwriteForbidden("repeated_nested_message [{ bb: 1 }, { bb: 2 }]\n"); } + public void testParseShortRepeatedFormOfEmptyRepeatedFields() throws Exception { + assertParseSuccessWithOverwriteForbidden("repeated_foreign_enum: []"); + assertParseSuccessWithOverwriteForbidden("repeated_int32: []\n"); + assertParseSuccessWithOverwriteForbidden("RepeatedGroup []\n"); + assertParseSuccessWithOverwriteForbidden("repeated_nested_message []\n"); + } + + public void testParseShortRepeatedFormWithTrailingComma() throws Exception { + assertParseErrorWithOverwriteForbidden( + "1:38: Expected identifier. Found \']\'", + "repeated_foreign_enum: [FOREIGN_FOO, ]\n"); + assertParseErrorWithOverwriteForbidden( + "1:22: Couldn't parse integer: For input string: \"]\"", + "repeated_int32: [ 1, ]\n"); + assertParseErrorWithOverwriteForbidden( + "1:25: Expected \"{\".", + "RepeatedGroup [{ a: 1 },]\n"); + assertParseErrorWithOverwriteForbidden( + "1:37: Expected \"{\".", + "repeated_nested_message [{ bb: 1 }, ]\n"); + } + public void testParseShortRepeatedFormOfNonRepeatedFields() throws Exception { assertParseErrorWithOverwriteForbidden( "1:17: Couldn't parse integer: For input string: \"[\"", "optional_int32: [1]\n"); + assertParseErrorWithOverwriteForbidden( + "1:17: Couldn't parse integer: For input string: \"[\"", + "optional_int32: []\n"); } // ======================================================================= diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java index 573cd665..26ea850e 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetLiteTest.java @@ -35,11 +35,9 @@ import com.google.protobuf.UnittestLite.TestAllTypesLite; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Bar; import protobuf_unittest.lite_equals_and_hash.LiteEqualsAndHash.Foo; - -import junit.framework.TestCase; - import java.io.ByteArrayOutputStream; import java.io.IOException; +import junit.framework.TestCase; /** * Tests for {@link UnknownFieldSetLite}. diff --git a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java index 32380f70..f81e90b4 100644 --- a/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnknownFieldSetTest.java @@ -38,11 +38,9 @@ import protobuf_unittest.UnittestProto.TestEmptyMessage; import protobuf_unittest.UnittestProto.TestEmptyMessageWithExtensions; import protobuf_unittest.UnittestProto.TestPackedExtensions; import protobuf_unittest.UnittestProto.TestPackedTypes; - -import junit.framework.TestCase; - import java.util.Arrays; import java.util.Map; +import junit.framework.TestCase; /** * Tests related to unknown field handling. diff --git a/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java b/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java index b1c75fc3..00f201ca 100644 --- a/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java +++ b/java/core/src/test/java/com/google/protobuf/UnmodifiableLazyStringListTest.java @@ -30,11 +30,10 @@ package com.google.protobuf; -import junit.framework.TestCase; - import java.util.Iterator; import java.util.List; import java.util.ListIterator; +import junit.framework.TestCase; /** * Tests for {@link UnmodifiableLazyStringList}. diff --git a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java index e66b371c..370860c2 100644 --- a/java/core/src/test/java/com/google/protobuf/WireFormatTest.java +++ b/java/core/src/test/java/com/google/protobuf/WireFormatTest.java @@ -44,12 +44,10 @@ import protobuf_unittest.UnittestProto.TestOneofBackwardsCompatible; import protobuf_unittest.UnittestProto.TestPackedExtensions; import protobuf_unittest.UnittestProto.TestPackedTypes; import proto2_wireformat_unittest.UnittestMsetWireFormat.TestMessageSet; - -import junit.framework.TestCase; - import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.util.List; +import junit.framework.TestCase; /** * Tests related to parsing and serialization. diff --git a/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto b/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto index 86cdd286..2367bd8b 100644 --- a/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/field_presence_test.proto @@ -36,7 +36,6 @@ import "google/protobuf/unittest.proto"; option java_package = "com.google.protobuf"; option java_outer_classname = "FieldPresenceTestProto"; -option java_generate_equals_and_hash = true; message TestAllTypes { enum NestedEnum { diff --git a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto index de3336a5..2ca0251c 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_lite_test.proto @@ -32,7 +32,6 @@ syntax = "proto2"; option java_outer_classname = "MapForProto2TestProto"; -option java_generate_equals_and_hash = true; message TestMap { message MessageValue { @@ -81,6 +80,42 @@ message BizarroTestMap { map<string, bytes> int32_to_message_field = 5; // different key and value types map<string, bytes> string_to_int32_field = 6; // same key type, different value } + +// Used to test that java reserved words can be used as protobuf field names +// Not all reserved words are tested (to avoid bloat) but instead an arbitrary +// subset of them chosen to cover various keyword categories like +// type, modifier, declaration, etc. +message ReservedAsMapField { + map<string, uint32> if = 1; + map<string, uint32> const = 2; + map<string, uint32> private = 3; + map<string, uint32> class = 4; + map<string, uint32> int = 5; + map<string, uint32> void = 6; + map<string, uint32> string = 7; // These are also proto keywords + map<string, uint32> package = 8; + map<string, uint32> enum = 9; // Most recent Java reserved word + map<string, uint32> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} + +message ReservedAsMapFieldWithEnumValue { + enum SampleEnum { + A = 0; + B = 1; + } + map<string, SampleEnum> if = 1; + map<string, SampleEnum> const = 2; + map<string, SampleEnum> private = 3; + map<string, SampleEnum> class = 4; + map<string, SampleEnum> int = 5; + map<string, SampleEnum> void = 6; + map<string, SampleEnum> string = 7; // These are also proto keywords + map<string, SampleEnum> package = 8; + map<string, SampleEnum> enum = 9; // Most recent Java reserved word + map<string, SampleEnum> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} package map_for_proto2_lite_test; option java_package = "map_lite_test"; option optimize_for = LITE_RUNTIME; diff --git a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto index 0c92b0ae..974f8a2c 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_for_proto2_test.proto @@ -34,7 +34,6 @@ package map_for_proto2_test; option java_package = "map_test"; option java_outer_classname = "MapForProto2TestProto"; -option java_generate_equals_and_hash = true; message TestMap { message MessageValue { @@ -83,3 +82,39 @@ message BizarroTestMap { map<string, bytes> int32_to_message_field = 5; // different key and value types map<string, bytes> string_to_int32_field = 6; // same key type, different value } + +// Used to test that java reserved words can be used as protobuf field names +// Not all reserved words are tested (to avoid bloat) but instead an arbitrary +// subset of them chosen to cover various keyword categories like +// type, modifier, declaration, etc. +message ReservedAsMapField { + map<string, uint32> if = 1; + map<string, uint32> const = 2; + map<string, uint32> private = 3; + map<string, uint32> class = 4; + map<string, uint32> int = 5; + map<string, uint32> void = 6; + map<string, uint32> string = 7; // These are also proto keywords + map<string, uint32> package = 8; + map<string, uint32> enum = 9; // Most recent Java reserved word + map<string, uint32> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} + +message ReservedAsMapFieldWithEnumValue { + enum SampleEnum { + A = 0; + B = 1; + } + map<string, SampleEnum> if = 1; + map<string, SampleEnum> const = 2; + map<string, SampleEnum> private = 3; + map<string, SampleEnum> class = 4; + map<string, SampleEnum> int = 5; + map<string, SampleEnum> void = 6; + map<string, SampleEnum> string = 7; // These are also proto keywords + map<string, SampleEnum> package = 8; + map<string, SampleEnum> enum = 9; // Most recent Java reserved word + map<string, SampleEnum> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} diff --git a/java/core/src/test/proto/com/google/protobuf/map_test.proto b/java/core/src/test/proto/com/google/protobuf/map_test.proto index 9eb63fc3..bc2105e5 100644 --- a/java/core/src/test/proto/com/google/protobuf/map_test.proto +++ b/java/core/src/test/proto/com/google/protobuf/map_test.proto @@ -34,7 +34,6 @@ package map_test; option java_package = "map_test"; option java_outer_classname = "MapTestProto"; -option java_generate_equals_and_hash = true; message TestMap { message MessageValue { @@ -53,6 +52,8 @@ message TestMap { map<int32, EnumValue> int32_to_enum_field = 4; map<int32, MessageValue> int32_to_message_field = 5; map<string, int32> string_to_int32_field = 6; + map<uint32, int32> uint32_to_int32_field = 7; + map<int64, int32> int64_to_int32_field = 8; } // Used to test that a nested builder containing map fields will properly @@ -71,3 +72,39 @@ message BizarroTestMap { map<string, bytes> int32_to_message_field = 5; // different key and value types map<string, bytes> string_to_int32_field = 6; // same key type, different value } + +// Used to test that java reserved words can be used as protobuf field names +// Not all reserved words are tested (to avoid bloat) but instead an arbitrary +// subset of them chosen to cover various keyword categories like +// type, modifier, declaration, etc. +message ReservedAsMapField { + map<string, uint32> if = 1; + map<string, uint32> const = 2; + map<string, uint32> private = 3; + map<string, uint32> class = 4; + map<string, uint32> int = 5; + map<string, uint32> void = 6; + map<string, uint32> string = 7; // These are also proto keywords + map<string, uint32> package = 8; + map<string, uint32> enum = 9; // Most recent Java reserved word + map<string, uint32> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} + +message ReservedAsMapFieldWithEnumValue { + enum SampleEnum { + A = 0; + B = 1; + } + map<string, SampleEnum> if = 1; + map<string, SampleEnum> const = 2; + map<string, SampleEnum> private = 3; + map<string, SampleEnum> class = 4; + map<string, SampleEnum> int = 5; + map<string, SampleEnum> void = 6; + map<string, SampleEnum> string = 7; // These are also proto keywords + map<string, SampleEnum> package = 8; + map<string, SampleEnum> enum = 9; // Most recent Java reserved word + map<string, SampleEnum> null = 10; + // null is not a 'reserved word' per se but as a literal needs similar care +} diff --git a/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto b/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto index 2b1f65e4..d2c77936 100644 --- a/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto +++ b/java/core/src/test/proto/com/google/protobuf/test_bad_identifiers.proto @@ -43,7 +43,6 @@ package io_protocol_tests; option java_package = "com.google.protobuf"; option java_outer_classname = "TestBadIdentifiersProto"; -option java_generate_equals_and_hash = true; message TestMessage { optional string cached_size = 1; |