diff options
Diffstat (limited to 'protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java')
-rw-r--r-- | protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java | 161 |
1 files changed, 8 insertions, 153 deletions
diff --git a/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java b/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java index dc9ede460..8281da89c 100644 --- a/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java +++ b/protobuf/src/test/java/io/grpc/protobuf/ProtoUtilsTest.java @@ -31,166 +31,42 @@ package io.grpc.protobuf; -import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.fail; import com.google.common.io.ByteStreams; -import com.google.protobuf.ByteString; -import com.google.protobuf.Empty; -import com.google.protobuf.Enum; -import com.google.protobuf.InvalidProtocolBufferException; +import com.google.protobuf.MessageLite; import com.google.protobuf.Type; -import io.grpc.Drainable; -import io.grpc.Metadata; import io.grpc.MethodDescriptor.Marshaller; -import io.grpc.Status; -import io.grpc.StatusRuntimeException; 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.IOException; import java.io.InputStream; -import java.util.Arrays; /** Unit tests for {@link ProtoUtils}. */ @RunWith(JUnit4.class) public class ProtoUtilsTest { - private Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance()); private Type proto = Type.newBuilder().setName("name").build(); @Test - public void testPassthrough() { - assertSame(proto, marshaller.parse(marshaller.stream(proto))); - } - - @Test public void testRoundtrip() throws Exception { + Marshaller<Type> marshaller = ProtoUtils.marshaller(Type.getDefaultInstance()); InputStream is = marshaller.stream(proto); is = new ByteArrayInputStream(ByteStreams.toByteArray(is)); assertEquals(proto, marshaller.parse(is)); } @Test - public void testInvalidatedMessage() throws Exception { - InputStream is = marshaller.stream(proto); - // Invalidates message, and drains all bytes - ByteStreams.toByteArray(is); - try { - ((ProtoInputStream) is).message(); - fail("Expected exception"); - } catch (IllegalStateException ex) { - // expected - } - // Zero bytes is the default message - assertEquals(Type.getDefaultInstance(), marshaller.parse(is)); - } - - @Test - public void parseInvalid() throws Exception { - InputStream is = new ByteArrayInputStream(new byte[] {-127}); - try { - marshaller.parse(is); - fail("Expected exception"); - } catch (StatusRuntimeException ex) { - assertEquals(Status.Code.INTERNAL, ex.getStatus().getCode()); - assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage()); - } - } - - @Test - public void testMismatch() throws Exception { - Marshaller<Enum> enumMarshaller = ProtoUtils.marshaller(Enum.getDefaultInstance()); - // Enum's name and Type's name are both strings with tag 1. - Enum altProto = Enum.newBuilder().setName(proto.getName()).build(); - assertEquals(proto, marshaller.parse(enumMarshaller.stream(altProto))); - } - - @Test - public void marshallerShouldNotLimitProtoSize() throws Exception { - // The default limit is 64MB. Using a larger proto to verify that the limit is not enforced. - byte[] bigName = new byte[70 * 1024 * 1024]; - Arrays.fill(bigName, (byte) 32); - - proto = Type.newBuilder().setNameBytes(ByteString.copyFrom(bigName)).build(); - - // Just perform a round trip to verify that it works. - testRoundtrip(); - } - - @Test - public void testAvailable() throws Exception { + @Deprecated + public void testRoundtripLite() throws Exception { + Marshaller<MessageLite> marshaller + = ProtoUtils.marshaller((MessageLite) Type.getDefaultInstance()); InputStream is = marshaller.stream(proto); - assertEquals(proto.getSerializedSize(), is.available()); - is.read(); - assertEquals(proto.getSerializedSize() - 1, is.available()); - while (is.read() != -1) {} - assertEquals(-1, is.read()); - assertEquals(0, is.available()); - } - - @Test - public void testEmpty() throws IOException { - Marshaller<Empty> marshaller = ProtoUtils.marshaller(Empty.getDefaultInstance()); - InputStream is = marshaller.stream(Empty.getDefaultInstance()); - assertEquals(0, is.available()); - byte[] b = new byte[10]; - assertEquals(-1, is.read(b)); - assertArrayEquals(new byte[10], b); - // Do the same thing again, because the internal state may be different - assertEquals(-1, is.read(b)); - assertArrayEquals(new byte[10], b); - assertEquals(-1, is.read()); - assertEquals(0, is.available()); - } - - @Test - public void testDrainTo_all() throws Exception { - byte[] golden = ByteStreams.toByteArray(marshaller.stream(proto)); - InputStream is = marshaller.stream(proto); - Drainable d = (Drainable) is; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int drained = d.drainTo(baos); - assertEquals(baos.size(), drained); - assertArrayEquals(golden, baos.toByteArray()); - assertEquals(0, is.available()); - } - - @Test - public void testDrainTo_partial() throws Exception { - final byte[] golden; - { - InputStream is = marshaller.stream(proto); - is.read(); - golden = ByteStreams.toByteArray(is); - } - InputStream is = marshaller.stream(proto); - is.read(); - Drainable d = (Drainable) is; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - int drained = d.drainTo(baos); - assertEquals(baos.size(), drained); - assertArrayEquals(golden, baos.toByteArray()); - assertEquals(0, is.available()); - } - - @Test - public void testDrainTo_none() throws Exception { - byte[] golden = ByteStreams.toByteArray(marshaller.stream(proto)); - InputStream is = marshaller.stream(proto); - ByteStreams.toByteArray(is); - Drainable d = (Drainable) is; - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - assertEquals(0, d.drainTo(baos)); - assertArrayEquals(new byte[0], baos.toByteArray()); - assertEquals(0, is.available()); + is = new ByteArrayInputStream(ByteStreams.toByteArray(is)); + assertEquals(proto, marshaller.parse(is)); } @Test @@ -198,25 +74,4 @@ public class ProtoUtilsTest { assertEquals("google.protobuf.Type-bin", ProtoUtils.keyForProto(Type.getDefaultInstance()).originalName()); } - - @Test - public void keyMarshaller_roundtrip() { - Metadata.BinaryMarshaller<Type> keyMarshaller = - ProtoUtils.keyMarshaller(Type.getDefaultInstance()); - assertEquals(proto, keyMarshaller.parseBytes(keyMarshaller.toBytes(proto))); - } - - @Test - public void keyMarshaller_invalid() { - Metadata.BinaryMarshaller<Type> keyMarshaller = - ProtoUtils.keyMarshaller(Type.getDefaultInstance()); - try { - keyMarshaller.parseBytes(new byte[] {-127}); - fail("Expected exception"); - } catch (IllegalArgumentException ex) { - assertNotNull(((InvalidProtocolBufferException) ex.getCause()).getUnfinishedMessage()); - } - } - - } |