BUG-2838: Trim leading whitespaces from incoming NETCONF messages.
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / netconf / nettyutil / handler / NetconfXMLToMessageDecoderTest.java
index f85a38769f1f5b451690f63c718d4804c7767719..aaa92e8050d332e3c2b1c8f7b78f62d23114cc55 100644 (file)
@@ -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("<msg/>".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<Object> out = Lists.newArrayList();
+        new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".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<Object> out = Lists.newArrayList();
+        new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".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<Object> 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<Object> 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<Object> out = Lists.newArrayList();
+        byte whitespaces[] = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */};
+        new NetconfXMLToMessageDecoder().decode(
+                null,
+                Unpooled.copiedBuffer(
+                        Unpooled.wrappedBuffer(whitespaces),
+                        Unpooled.wrappedBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes())),
+                out);
+        assertEquals(1, out.size());
+    }
+}
+