X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-netty-util%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fnettyutil%2Fhandler%2FNetconfXMLToMessageDecoderTest.java;h=aaa92e8050d332e3c2b1c8f7b78f62d23114cc55;hb=81441c1681143050118195459a1a731cf9be21bb;hp=f85a38769f1f5b451690f63c718d4804c7767719;hpb=12d79e66e26bc7a521b0380e158dca5dd2b98abc;p=controller.git diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java index f85a38769f..aaa92e8050 100644 --- a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java @@ -14,6 +14,7 @@ import com.google.common.collect.Lists; import io.netty.buffer.Unpooled; import java.util.ArrayList; import org.junit.Test; +import org.xml.sax.SAXParseException; public class NetconfXMLToMessageDecoderTest { @@ -30,4 +31,61 @@ public class NetconfXMLToMessageDecoderTest { new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("".getBytes()), out); assertEquals(1, out.size()); } -} \ No newline at end of file + + @Test + public void testDecodeWithLeadingLFAndXmlDecl() throws Exception { + /* Test that we accept XML documents with a line feed (0x0a) before the + * XML declaration in the XML prologue. + * A leading LF is the case reported in BUG-2838. + */ + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\n".getBytes()), out); + assertEquals(1, out.size()); + } + + @Test + public void testDecodeWithLeadingCRLFAndXmlDecl() throws Exception { + /* Test that we accept XML documents with both a carriage return and + * line feed (0x0d 0x0a) before the XML declaration in the XML prologue. + * Leading CRLF can be seen with some Cisco routers + * (eg CSR1000V running IOS 15.4(1)S) + */ + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out); + assertEquals(1, out.size()); + } + + @Test(expected=SAXParseException.class) + public void testDecodeGibberish() throws Exception { + /* Test that we reject inputs where we cannot find the xml start '<' character */ + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n?xml version>".getBytes()), out); + assertEquals(1, out.size()); + } + + @Test + public void testDecodeOnlyWhitespaces() throws Exception { + /* Test that we handle properly a bunch of whitespaces. + */ + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out); + assertEquals(0, out.size()); + } + + @Test + public void testDecodeWithAllWhitespaces() throws Exception { + /* Test that every whitespace we want to skip is actually skipped. + */ + + final ArrayList out = Lists.newArrayList(); + byte whitespaces[] = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */}; + new NetconfXMLToMessageDecoder().decode( + null, + Unpooled.copiedBuffer( + Unpooled.wrappedBuffer(whitespaces), + Unpooled.wrappedBuffer("".getBytes())), + out); + assertEquals(1, out.size()); + } +} +