From 1a21b64021f37d862c503652368bb0d223a451d7 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 19 Sep 2019 21:20:16 +0200 Subject: [PATCH] Add encoding size asserts There is a number of places where we check serialize/deserialize operations through ByteArrayOutputStream. In order to check stability (and evolution) of our serialization formats, it is useful to check the sizes of the intermediate bytes. JIRA: CONTROLLER-1919 Change-Id: I171894ba3791245a53fe39c2d3238d2341d8ffc4 Signed-off-by: Robert Varga --- .../NormalizedNodeStreamReaderWriterTest.java | 44 ++++++++++++------- .../utils/stream/SerializationUtilsTest.java | 32 +++++++++----- .../YangTextSourceSerializationProxyTest.java | 7 ++- 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java index aed87ad355..2f910e649d 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/NormalizedNodeStreamReaderWriterTest.java @@ -69,10 +69,12 @@ public class NormalizedNodeStreamReaderWriterTest { .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).withChild(toasterNode).build(); nnout.writeNormalizedNode(toasterContainer); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(1049950, bytes.length); - NormalizedNode node = nnin.readNormalizedNode(); + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); + + NormalizedNode node = nnin.readNormalizedNode(); Assert.assertEquals(testContainer, node); node = nnin.readNormalizedNode(); @@ -111,8 +113,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(161, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); YangInstanceIdentifier newPath = nnin.readYangInstanceIdentifier(); Assert.assertEquals(path, newPath); @@ -121,9 +125,9 @@ public class NormalizedNodeStreamReaderWriterTest { @Test public void testNormalizedNodeAndYangInstanceIdentifierStreaming() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); NormalizedNodeDataOutput writer = NormalizedNodeInputOutput.newDataOutput( - ByteStreams.newDataOutput(byteArrayOutputStream)); + ByteStreams.newDataOutput(bos)); NormalizedNode testContainer = TestModel.createBaseTestContainerBuilder().build(); writer.writeNormalizedNode(testContainer); @@ -134,8 +138,10 @@ public class NormalizedNodeStreamReaderWriterTest { writer.writeYangInstanceIdentifier(path); - NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( - ByteStreams.newDataInput(byteArrayOutputStream.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(1163, bytes.length); + + NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); NormalizedNode node = reader.readNormalizedNode(); Assert.assertEquals(testContainer, node); @@ -148,7 +154,7 @@ public class NormalizedNodeStreamReaderWriterTest { @Test(expected = InvalidNormalizedNodeStreamException.class, timeout = 10000) public void testInvalidNormalizedNodeStream() throws IOException { - byte[] invalidBytes = {1,2,3}; + byte[] invalidBytes = {1, 2, 3}; NormalizedNodeDataInput reader = NormalizedNodeInputOutput.newDataInput( ByteStreams.newDataInput(invalidBytes)); @@ -195,8 +201,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeNormalizedNode(anyXmlContainer); - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(235, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); ContainerNode deserialized = (ContainerNode)nnin.readNormalizedNode(); @@ -222,8 +230,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writeSchemaPath(expected); } - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(100, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); assertEquals(expected, nnin.readSchemaPath()); } @@ -237,8 +247,10 @@ public class NormalizedNodeStreamReaderWriterTest { nnout.writePathArgument(expected); } - NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput( - bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(105, bytes.length); + + NormalizedNodeDataInput nnin = NormalizedNodeInputOutput.newDataInput(ByteStreams.newDataInput(bytes)); assertEquals(expected, nnin.readPathArgument()); } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java index 1ddabe83da..a3e574c9c0 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/datastore/node/utils/stream/SerializationUtilsTest.java @@ -7,6 +7,9 @@ */ package org.opendaylight.controller.cluster.datastore.node.utils.stream; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + import com.google.common.collect.ImmutableSet; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; @@ -22,7 +25,6 @@ import java.util.stream.Collectors; import javax.xml.transform.dom.DOMSource; import org.custommonkey.xmlunit.Diff; import org.custommonkey.xmlunit.XMLUnit; -import org.junit.Assert; import org.junit.Test; import org.opendaylight.yangtools.util.xml.UntrustedXML; import org.opendaylight.yangtools.yang.common.QName; @@ -51,7 +53,8 @@ public class SerializationUtilsTest { public void testSerializeDeserializeNodes() throws IOException { final NormalizedNode normalizedNode = createNormalizedNode(); final byte[] bytes = serializeNormalizedNode(normalizedNode); - Assert.assertEquals(normalizedNode, deserializeNormalizedNode(bytes)); + assertEquals(10774, bytes.length); + assertEquals(normalizedNode, deserializeNormalizedNode(bytes)); } @Test @@ -64,11 +67,12 @@ public class SerializationUtilsTest { .withValue(new DOMSource(parse)) .build(); final byte[] bytes = serializeNormalizedNode(anyXmlNode); + assertEquals(115, bytes.length); final NormalizedNode deserialized = deserializeNormalizedNode(bytes); final DOMSource value = (DOMSource) deserialized.getValue(); final Diff diff = XMLUnit.compareXML((Document) anyXmlNode.getValue().getNode(), value.getNode().getOwnerDocument()); - Assert.assertTrue(diff.toString(), diff.similar()); + assertTrue(diff.toString(), diff.similar()); } @Test @@ -82,9 +86,13 @@ public class SerializationUtilsTest { .node(leafSetId("leafSer1", "leafSetValue1")) .build(); SerializationUtils.writePath(out, path); + + final byte[] bytes = bos.toByteArray(); + assertEquals(150, bytes.length); + final YangInstanceIdentifier deserialized = - SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bos.toByteArray()))); - Assert.assertEquals(path, deserialized); + SerializationUtils.readPath(new DataInputStream(new ByteArrayInputStream(bytes))); + assertEquals(path, deserialized); } @Test @@ -94,17 +102,21 @@ public class SerializationUtilsTest { final NormalizedNode node = createNormalizedNode(); final YangInstanceIdentifier path = YangInstanceIdentifier.create(id("container1")); SerializationUtils.writeNodeAndPath(out, path, node); - final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bos.toByteArray())); + + final byte[] bytes = bos.toByteArray(); + assertEquals(10783, bytes.length); + + final DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes)); final AtomicBoolean applierCalled = new AtomicBoolean(false); SerializationUtils.readNodeAndPath(in, applierCalled, (instance, deserializedPath, deserializedNode) -> { - Assert.assertEquals(path, deserializedPath); - Assert.assertEquals(node, deserializedNode); + assertEquals(path, deserializedPath); + assertEquals(node, deserializedNode); applierCalled.set(true); }); - Assert.assertTrue(applierCalled.get()); + assertTrue(applierCalled.get()); } - private static NormalizedNode deserializeNormalizedNode(final byte [] bytes) throws IOException { + private static NormalizedNode deserializeNormalizedNode(final byte[] bytes) throws IOException { return SerializationUtils.readNormalizedNode(new DataInputStream(new ByteArrayInputStream(bytes))).get(); } diff --git a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/schema/provider/impl/YangTextSourceSerializationProxyTest.java b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/schema/provider/impl/YangTextSourceSerializationProxyTest.java index b2a86e1c0c..084fd5242f 100644 --- a/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/schema/provider/impl/YangTextSourceSerializationProxyTest.java +++ b/opendaylight/md-sal/sal-clustering-commons/src/test/java/org/opendaylight/controller/cluster/schema/provider/impl/YangTextSourceSerializationProxyTest.java @@ -38,14 +38,17 @@ public class YangTextSourceSerializationProxyTest { @Test - public void serializeAndDesrializeProxy() throws ClassNotFoundException, IOException { + public void serializeAndDeserializeProxy() throws ClassNotFoundException, IOException { YangTextSchemaSourceSerializationProxy proxy = new YangTextSchemaSourceSerializationProxy(schemaSource); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(proxy); - ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bos.toByteArray())); + final byte[] bytes = bos.toByteArray(); + assertEquals(353, bytes.length); + + ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes)); YangTextSchemaSourceSerializationProxy deserializedProxy = (YangTextSchemaSourceSerializationProxy) ois.readObject(); -- 2.36.6