aboutsummaryrefslogtreecommitdiff
path: root/javanano/src/test
diff options
context:
space:
mode:
authorCharles Munger <clm@google.com>2015-04-21 14:35:46 -0700
committerBrian Duff <bduff@google.com>2015-04-28 13:10:49 -0700
commit6732dd7e58e3adaf217f20f227e6ff7e72cb6b22 (patch)
tree20e64ef7649aa52aeee3d49bf764566b63617cfb /javanano/src/test
parenta69b461e1eee43a277839825f1153b8260a28e87 (diff)
downloadprotobuf-6732dd7e58e3adaf217f20f227e6ff7e72cb6b22.tar.gz
protobuf-6732dd7e58e3adaf217f20f227e6ff7e72cb6b22.tar.bz2
protobuf-6732dd7e58e3adaf217f20f227e6ff7e72cb6b22.zip
Throw OutOfSpaceException instead of IllegalArgumentException.
When a MessageNano containing a String is serialized into a buffer that is too small to contain it, and the buffer's boundary happens to be where the string field's length delimiting varint is serialized, and the string's length and 3*length have the same length when encoded as a varint, an IllegalArgumentException is thrown rather than an OutOfSpaceException. Github issue: https://github.com/google/protobuf/issues/292 Change-Id: If478d68cf15bfd0662252d008e42b2bf1ff1c75e
Diffstat (limited to 'javanano/src/test')
-rw-r--r--javanano/src/test/java/com/google/protobuf/nano/NanoTest.java18
1 files changed, 18 insertions, 0 deletions
diff --git a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
index 91cc385f..3a75777a 100644
--- a/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
+++ b/javanano/src/test/java/com/google/protobuf/nano/NanoTest.java
@@ -31,6 +31,7 @@
package com.google.protobuf.nano;
import com.google.protobuf.nano.MapTestProto.TestMap;
+import com.google.protobuf.nano.CodedOutputByteBufferNano;
import com.google.protobuf.nano.MapTestProto.TestMap.MessageValue;
import com.google.protobuf.nano.NanoAccessorsOuterClass.TestNanoAccessors;
import com.google.protobuf.nano.NanoHasOuterClass.TestAllTypesNanoHas;
@@ -2322,6 +2323,23 @@ public class NanoTest extends TestCase {
}
}
+ /** Regression test for https://github.com/google/protobuf/issues/292 */
+ public void testCorrectExceptionThrowWhenEncodingStringsWithoutEnoughSpace() throws Exception {
+ String testCase = "Foooooooo";
+ assertEquals(CodedOutputByteBufferNano.computeRawVarint32Size(testCase.length()),
+ CodedOutputByteBufferNano.computeRawVarint32Size(testCase.length() * 3));
+ assertEquals(11, CodedOutputByteBufferNano.computeStringSize(1, 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 (int i = 0; i < 11; i++) {
+ CodedOutputByteBufferNano bufferNano = CodedOutputByteBufferNano.newInstance(new byte[i]);
+ try {
+ bufferNano.writeString(1, testCase);
+ fail("Should have thrown an out of space exception");
+ } catch (CodedOutputByteBufferNano.OutOfSpaceException expected) {}
+ }
+ }
+
private void testEncodingOfString(char c, int length) throws InvalidProtocolBufferNanoException {
TestAllTypesNano testAllTypesNano = new TestAllTypesNano();
final String fullString = fullString(c, length);