2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netconf.nettyutil.handler;
11 import static org.junit.Assert.assertEquals;
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;
19 public class NetconfXMLToMessageDecoderTest {
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());
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());
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.
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());
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)
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());
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());
67 public void testDecodeOnlyWhitespaces() throws Exception {
68 /* Test that we handle properly a bunch of whitespaces.
70 final ArrayList<Object> out = Lists.newArrayList();
71 new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("\r\n".getBytes()), out);
72 assertEquals(0, out.size());
76 public void testDecodeWithAllWhitespaces() throws Exception {
77 /* Test that every whitespace we want to skip is actually skipped.
80 final ArrayList<Object> out = Lists.newArrayList();
81 byte whitespaces[] = {' ', '\t', '\n', '\r', '\f', 0x0b /* vertical tab */};
82 new NetconfXMLToMessageDecoder().decode(
84 Unpooled.copiedBuffer(
85 Unpooled.wrappedBuffer(whitespaces),
86 Unpooled.wrappedBuffer("<?xml version=\"1.0\" encoding=\"UTF-8\"?><msg/>".getBytes())),
88 assertEquals(1, out.size());