Remove netconf from commons/opendaylight pom
[controller.git] / opendaylight / netconf / netconf-netty-util / src / test / java / org / opendaylight / controller / 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.controller.netconf.nettyutil.handler;
10
11 import static org.junit.Assert.assertEquals;
12
13 import com.google.common.collect.Lists;
14 import io.netty.buffer.Unpooled;
15 import java.util.ArrayList;
16 import org.junit.Test;
17 import org.xml.sax.SAXParseException;
18
19 public class NetconfXMLToMessageDecoderTest {
20
21     @Test
22     public void testDecodeNoMoreContent() throws Exception {
23         final ArrayList<Object> out = Lists.newArrayList();
24         new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out);
25         assertEquals(0, out.size());
26     }
27
28     @Test
29     public void testDecode() throws Exception {
30         final ArrayList<Object> out = Lists.newArrayList();
31         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("<msg/>".getBytes()), out);
32         assertEquals(1, out.size());
33     }
34
35     @Test
36     public void testDecodeWithLeadingLFAndXmlDecl() throws Exception {
37         /* Test that we accept XML documents with a line feed (0x0a) before the
38          * XML declaration in the XML prologue.
39          * A leading LF is the case reported in BUG-2838.
40          */
41         final ArrayList<Object> out = Lists.newArrayList();
42         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes()), out);
43         assertEquals(1, out.size());
44     }
45
46     @Test
47     public void testDecodeWithLeadingCRLFAndXmlDecl() throws Exception {
48         /* Test that we accept XML documents with both a carriage return and
49          * line feed (0x0d 0x0a) before the XML declaration in the XML prologue.
50          * Leading CRLF can be seen with some Cisco routers
51          * (eg CSR1000V running IOS 15.4(1)S)
52          */
53         final ArrayList<Object> out = Lists.newArrayList();
54         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes()), out);
55         assertEquals(1, out.size());
56     }
57
58     @Test(expected=SAXParseException.class)
59     public void testDecodeGibberish() throws Exception {
60         /* Test that we reject inputs where we cannot find the xml start '<' character */
61         final ArrayList<Object> out = Lists.newArrayList();
62         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n?xml version>".getBytes()), out);
63         assertEquals(1, out.size());
64     }
65
66     @Test
67     public void testDecodeOnlyWhitespaces() throws Exception {
68         /* Test that we handle properly a bunch of whitespaces.
69          */
70         final ArrayList<Object> out = Lists.newArrayList();
71         new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out);
72         assertEquals(0, out.size());
73     }
74
75     @Test
76     public void testDecodeWithAllWhitespaces() throws Exception {
77         /* Test that every whitespace we want to skip is actually skipped.
78          */
79
80         final ArrayList<Object> out = Lists.newArrayList();
81         byte whitespaces[] = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */};
82         new NetconfXMLToMessageDecoder().decode(
83                 null,
84                 Unpooled.copiedBuffer(
85                         Unpooled.wrappedBuffer(whitespaces),
86                         Unpooled.wrappedBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes())),
87                 out);
88         assertEquals(1, out.size());
89     }
90 }
91