Bug 8824 - NETCONF request hangs when rpc-rply has invalid xml
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / handler / NetconfXMLToMessageDecoderTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.netconf.nettyutil.handler;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.Unpooled;
16 import java.util.ArrayList;
17 import org.junit.Test;
18 import org.opendaylight.netconf.api.FailedNetconfMessage;
19 import org.opendaylight.netconf.api.NetconfMessage;
20 import org.xml.sax.SAXParseException;
21
22 public class NetconfXMLToMessageDecoderTest {
23
24     @Test
25     public void testDecodeNoMoreContent() throws Exception {
26         final ArrayList<Object> out = Lists.newArrayList();
27         new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out);
28         assertEquals(0, out.size());
29     }
30
31     @Test
32     public void testDecode() throws Exception {
33         final ArrayList<Object> out = Lists.newArrayList();
34         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("<msg/>".getBytes()), out);
35         assertEquals(1, out.size());
36     }
37
38     @Test
39     public void testDecodeWithLeadingLFAndXmlDecl() throws Exception {
40         /* Test that we accept XML documents with a line feed (0x0a) before the
41          * XML declaration in the XML prologue.
42          * A leading LF is the case reported in BUG-2838.
43          */
44         final ArrayList<Object> out = Lists.newArrayList();
45         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes()), out);
46         assertEquals(1, out.size());
47     }
48
49     @Test
50     public void testDecodeWithLeadingCRLFAndXmlDecl() throws Exception {
51         /* Test that we accept XML documents with both a carriage return and
52          * line feed (0x0d 0x0a) before the XML declaration in the XML prologue.
53          * Leading CRLF can be seen with some Cisco routers
54          * (eg CSR1000V running IOS 15.4(1)S)
55          */
56         final ArrayList<Object> out = Lists.newArrayList();
57         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes()), out);
58         assertEquals(1, out.size());
59     }
60
61     @Test
62     public void testDecodeGibberish() throws Exception {
63         /* Test that we reject inputs where we cannot find the xml start '<' character */
64         final ArrayList<Object> out = Lists.newArrayList();
65         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n?xml version>".getBytes()), out);
66         assertEquals(1, out.size());
67         assertTrue(FailedNetconfMessage.class.isInstance(out.get(0)));
68         assertTrue(((FailedNetconfMessage) out.get(0)).getException().getClass().isAssignableFrom(SAXParseException.class));
69     }
70
71     @Test
72     public void testDecodeOnlyWhitespaces() throws Exception {
73         /* Test that we handle properly a bunch of whitespaces.
74          */
75         final ArrayList<Object> out = Lists.newArrayList();
76         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out);
77         assertEquals(0, out.size());
78     }
79
80     @Test
81     public void testDecodeWithAllWhitespaces() throws Exception {
82         /* Test that every whitespace we want to skip is actually skipped.
83          */
84
85         final ArrayList<Object> out = Lists.newArrayList();
86         byte whitespaces[] = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */};
87         new NetconfXMLToMessageDecoder().decode(
88                 null,
89                 Unpooled.copiedBuffer(
90                         Unpooled.wrappedBuffer(whitespaces),
91                         Unpooled.wrappedBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes())),
92                 out);
93         assertEquals(1, out.size());
94     }
95 }
96