Merge "BUG-9048 Fix actor crash when writing incorrect data"
[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.xml.sax.SAXParseException;
20
21 public class NetconfXMLToMessageDecoderTest {
22
23     @Test
24     public void testDecodeNoMoreContent() throws Exception {
25         final ArrayList<Object> out = Lists.newArrayList();
26         new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out);
27         assertEquals(0, out.size());
28     }
29
30     @Test
31     public void testDecode() throws Exception {
32         final ArrayList<Object> out = Lists.newArrayList();
33         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("<msg/>".getBytes()), out);
34         assertEquals(1, out.size());
35     }
36
37     @Test
38     public void testDecodeWithLeadingLFAndXmlDecl() throws Exception {
39         /* Test that we accept XML documents with a line feed (0x0a) before the
40          * XML declaration in the XML prologue.
41          * A leading LF is the case reported in BUG-2838.
42          */
43         final ArrayList<Object> out = Lists.newArrayList();
44         new NetconfXMLToMessageDecoder().decode(null,
45                 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,
58                 Unpooled.wrappedBuffer("\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes()), out);
59         assertEquals(1, out.size());
60     }
61
62     @Test
63     public void testDecodeGibberish() throws Exception {
64         /* Test that we reject inputs where we cannot find the xml start '<' character */
65         final ArrayList<Object> out = Lists.newArrayList();
66         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n?xml version>".getBytes()), out);
67         assertEquals(1, out.size());
68         assertTrue(FailedNetconfMessage.class.isInstance(out.get(0)));
69         assertTrue(((FailedNetconfMessage) out.get(0))
70                 .getException().getClass().isAssignableFrom(SAXParseException.class));
71     }
72
73     @Test
74     public void testDecodeOnlyWhitespaces() throws Exception {
75         /* Test that we handle properly a bunch of whitespaces.
76          */
77         final ArrayList<Object> out = Lists.newArrayList();
78         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out);
79         assertEquals(0, out.size());
80     }
81
82     @Test
83     public void testDecodeWithAllWhitespaces() throws Exception {
84         /* Test that every whitespace we want to skip is actually skipped.
85          */
86
87         final ArrayList<Object> out = Lists.newArrayList();
88         byte[] whitespaces = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */};
89         new NetconfXMLToMessageDecoder().decode(
90                 null,
91                 Unpooled.copiedBuffer(
92                         Unpooled.wrappedBuffer(whitespaces),
93                         Unpooled.wrappedBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes())),
94                 out);
95         assertEquals(1, out.size());
96     }
97 }
98