From 2f097c374a16b818f50f945cafc2461b9c5b8321 Mon Sep 17 00:00:00 2001 From: Maros Marsalek Date: Mon, 25 Aug 2014 17:14:11 +0200 Subject: [PATCH] BUG-1521 Increase test coverage of netconf-netty-util Change-Id: I962e16da8d4112b4ec6a1c90170be3036041eddd Signed-off-by: Maros Marsalek --- .../netconf/netconf-netty-util/pom.xml | 6 +- .../NetconfHelloMessageToXMLEncoder.java | 3 +- .../handler/NetconfXMLToMessageDecoder.java | 15 ++-- .../authentication/AuthenticationHandler.java | 3 - .../ssh/authentication/LoginPassword.java | 14 +-- .../ChunkedFramingMechanismEncoderTest.java | 85 +++++++++++++++++++ .../EOMFramingMechanismEncoderTest.java | 29 +++++++ .../FramingMechanismHandlerFactoryTest.java | 27 ++++++ .../handler/NetconfChunkAggregatorTest.java | 26 +++--- .../NetconfHelloMessageToXMLEncoderTest.java | 65 ++++++++++++++ .../NetconfXMLToHelloMessageDecoderTest.java | 78 +++++++++++++++++ .../NetconfXMLToMessageDecoderTest.java | 33 +++++++ .../ssh/authentication/LoginPasswordTest.java | 36 ++++++++ 13 files changed, 377 insertions(+), 43 deletions(-) create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ChunkedFramingMechanismEncoderTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/EOMFramingMechanismEncoderTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/FramingMechanismHandlerFactoryTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoderTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoderTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java create mode 100644 opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPasswordTest.java diff --git a/opendaylight/netconf/netconf-netty-util/pom.xml b/opendaylight/netconf/netconf-netty-util/pom.xml index 80dc1ae0fd..cb8461a299 100644 --- a/opendaylight/netconf/netconf-netty-util/pom.xml +++ b/opendaylight/netconf/netconf-netty-util/pom.xml @@ -64,7 +64,6 @@ org.openexi nagasena-rta - org.osgi org.osgi.core @@ -77,7 +76,10 @@ xmlunit xmlunit - + + org.opendaylight.yangtools + mockito-configuration + diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoder.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoder.java index d765ca8b25..f39e2c425d 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoder.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoder.java @@ -53,8 +53,7 @@ public final class NetconfHelloMessageToXMLEncoder extends NetconfMessageToXMLEn Optional headerOptional = ((NetconfHelloMessage) msg) .getAdditionalHeader(); - // If additional header present, serialize it along with netconf hello - // message + // If additional header present, serialize it along with netconf hello message if (headerOptional.isPresent()) { out.writeBytes(headerOptional.get().toFormattedString().getBytes(Charsets.UTF_8)); } diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java index 69c0d53fc1..bfc8d77e17 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoder.java @@ -7,26 +7,21 @@ */ package org.opendaylight.controller.netconf.nettyutil.handler; -import java.util.List; - -import org.opendaylight.controller.netconf.api.NetconfMessage; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.VisibleForTesting; - import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufInputStream; import io.netty.buffer.ByteBufUtil; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.ByteToMessageDecoder; +import java.util.List; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class NetconfXMLToMessageDecoder extends ByteToMessageDecoder { private static final Logger LOG = LoggerFactory.getLogger(NetconfXMLToMessageDecoder.class); @Override - @VisibleForTesting public void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { if (in.readableBytes() != 0) { diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/AuthenticationHandler.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/AuthenticationHandler.java index eea2b8693a..0548b1d371 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/AuthenticationHandler.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/AuthenticationHandler.java @@ -8,7 +8,6 @@ package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication; -import ch.ethz.ssh2.Connection; import java.io.IOException; import org.apache.sshd.ClientSession; @@ -16,8 +15,6 @@ import org.apache.sshd.ClientSession; * Class providing authentication facility to SSH handler. */ public abstract class AuthenticationHandler { - public abstract void authenticate(Connection connection) throws IOException; - public abstract String getUsername(); diff --git a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPassword.java b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPassword.java index 553e5359ff..ab94e59a93 100644 --- a/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPassword.java +++ b/opendaylight/netconf/netconf-netty-util/src/main/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPassword.java @@ -9,15 +9,12 @@ package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication; import java.io.IOException; - import org.apache.sshd.ClientSession; import org.apache.sshd.client.future.AuthFuture; -import ch.ethz.ssh2.Connection; - /** * Class Providing username/password authentication option to - * {@link org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.SshHandler} + * {@link org.opendaylight.controller.netconf.nettyutil.handler.ssh.client.AsyncSshHandler} */ public class LoginPassword extends AuthenticationHandler { private final String username; @@ -28,15 +25,6 @@ public class LoginPassword extends AuthenticationHandler { this.password = password; } - @Override - public void authenticate(Connection connection) throws IOException { - final boolean isAuthenticated = connection.authenticateWithPassword(username, password); - - if (!isAuthenticated) { - throw new IOException("Authentication failed."); - } - } - @Override public String getUsername() { return username; diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ChunkedFramingMechanismEncoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ChunkedFramingMechanismEncoderTest.java new file mode 100644 index 0000000000..93475129d2 --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ChunkedFramingMechanismEncoderTest.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Mockito.doAnswer; + +import com.google.common.collect.Lists; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants; + +public class ChunkedFramingMechanismEncoderTest { + + private int chunkSize; + @Mock + private ChannelHandlerContext ctx; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + chunkSize = 256; + } + + @Test(expected = IllegalArgumentException.class) + public void testIllegalSize() throws Exception { + new ChunkedFramingMechanismEncoder(10); + } + + @Test(expected = IllegalArgumentException.class) + public void testIllegalSizeMax() throws Exception { + new ChunkedFramingMechanismEncoder(Integer.MAX_VALUE); + } + + @Test + public void testEncode() throws Exception { + final List chunks = Lists.newArrayList(); + doAnswer(new Answer() { + @Override + public Object answer(final InvocationOnMock invocation) throws Throwable { + chunks.add((ByteBuf) invocation.getArguments()[0]); + return null; + } + }).when(ctx).write(anyObject()); + + final ChunkedFramingMechanismEncoder encoder = new ChunkedFramingMechanismEncoder(chunkSize); + final int lastChunkSize = 20; + final ByteBuf src = Unpooled.wrappedBuffer(getByteArray(chunkSize * 4 + lastChunkSize)); + final ByteBuf destination = Unpooled.buffer(); + encoder.encode(ctx, src, destination); + assertEquals(4, chunks.size()); + + final int framingSize = "#256\n".getBytes().length + 1/* new line at end */; + + for (final ByteBuf chunk : chunks) { + assertEquals(chunkSize + framingSize, chunk.readableBytes()); + } + + final int lastFramingSize = "#20\n".length() + NetconfMessageConstants.END_OF_CHUNK.length + 1/* new line at end */; + assertEquals(lastChunkSize + lastFramingSize, destination.readableBytes()); + } + + private byte[] getByteArray(final int size) { + final byte[] bytes = new byte[size]; + for (int i = 0; i < size; i++) { + bytes[i] = 'a'; + } + return bytes; + } +} diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/EOMFramingMechanismEncoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/EOMFramingMechanismEncoderTest.java new file mode 100644 index 0000000000..158f3a8701 --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/EOMFramingMechanismEncoderTest.java @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import static org.junit.Assert.assertEquals; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import org.junit.Test; +import org.opendaylight.controller.netconf.util.messages.NetconfMessageConstants; + +public class EOMFramingMechanismEncoderTest { + + @Test + public void testEncode() throws Exception { + final byte[] content = new byte[50]; + final ByteBuf source = Unpooled.wrappedBuffer(content); + final ByteBuf destination = Unpooled.buffer(); + new EOMFramingMechanismEncoder().encode(null, source, destination); + + assertEquals(Unpooled.wrappedBuffer(source.array(), NetconfMessageConstants.END_OF_MESSAGE), destination); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/FramingMechanismHandlerFactoryTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/FramingMechanismHandlerFactoryTest.java new file mode 100644 index 0000000000..4f123f0f09 --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/FramingMechanismHandlerFactoryTest.java @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import org.hamcrest.CoreMatchers; +import org.hamcrest.MatcherAssert; +import org.junit.Test; +import org.opendaylight.controller.netconf.util.messages.FramingMechanism; + +public class FramingMechanismHandlerFactoryTest { + + @Test + public void testCreate() throws Exception { + MatcherAssert.assertThat(FramingMechanismHandlerFactory + .createHandler(FramingMechanism.CHUNK), CoreMatchers + .instanceOf(ChunkedFramingMechanismEncoder.class)); + MatcherAssert.assertThat(FramingMechanismHandlerFactory + .createHandler(FramingMechanism.EOM), CoreMatchers + .instanceOf(EOMFramingMechanismEncoder.class)); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfChunkAggregatorTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfChunkAggregatorTest.java index e088859e82..a647b9ee17 100644 --- a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfChunkAggregatorTest.java +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfChunkAggregatorTest.java @@ -7,16 +7,16 @@ */ package org.opendaylight.controller.netconf.nettyutil.handler; +import static org.junit.Assert.assertEquals; + import com.google.common.base.Charsets; import com.google.common.collect.Lists; import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; -import junit.framework.Assert; +import java.util.List; import org.junit.BeforeClass; import org.junit.Test; -import java.util.List; - public class NetconfChunkAggregatorTest { private static final String CHUNKED_MESSAGE = "\n#4\n" + @@ -45,26 +45,26 @@ public class NetconfChunkAggregatorTest { @Test public void testMultipleChunks() throws Exception { - List output = Lists.newArrayList(); - ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(Charsets.UTF_8)); + final List output = Lists.newArrayList(); + final ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(Charsets.UTF_8)); agr.decode(null, input, output); - Assert.assertEquals(1, output.size()); - ByteBuf chunk = (ByteBuf) output.get(0); + assertEquals(1, output.size()); + final ByteBuf chunk = (ByteBuf) output.get(0); - Assert.assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8)); + assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8)); } @Test public void testOneChunks() throws Exception { - List output = Lists.newArrayList(); - ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(Charsets.UTF_8)); + final List output = Lists.newArrayList(); + final ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(Charsets.UTF_8)); agr.decode(null, input, output); - Assert.assertEquals(1, output.size()); - ByteBuf chunk = (ByteBuf) output.get(0); + assertEquals(1, output.size()); + final ByteBuf chunk = (ByteBuf) output.get(0); - Assert.assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8)); + assertEquals(EXPECTED_MESSAGE, chunk.toString(Charsets.UTF_8)); } diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoderTest.java new file mode 100644 index 0000000000..00d95df423 --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfHelloMessageToXMLEncoderTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.not; +import static org.junit.Assert.assertThat; + +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import io.netty.channel.ChannelHandlerContext; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage; +import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessageAdditionalHeader; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; + +public class NetconfHelloMessageToXMLEncoderTest { + + @Mock + private ChannelHandlerContext ctx; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testEncode() throws Exception { + final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument(""), + NetconfHelloMessageAdditionalHeader.fromString("[tomas;10.0.0.0:10000;tcp;client;]")); + final ByteBuf destination = Unpooled.buffer(); + new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination); + + final String encoded = new String(destination.array()); + assertThat(encoded, containsString("[tomas;10.0.0.0:10000;tcp;client;]")); + assertThat(encoded, containsString("")); + } + + @Test + public void testEncodeNoHeader() throws Exception { + final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument("")); + final ByteBuf destination = Unpooled.buffer(); + new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, destination); + + final String encoded = new String(destination.array()); + assertThat(encoded, not(containsString("[tomas;10.0.0.0:10000;tcp;client;]"))); + assertThat(encoded, containsString("")); + } + + @Test(expected = IllegalStateException.class) + public void testEncodeNotHello() throws Exception { + final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("")); + new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoderTest.java new file mode 100644 index 0000000000..f0c0d6341b --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToHelloMessageDecoderTest.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +import com.google.common.collect.Iterables; +import com.google.common.collect.Lists; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; +import java.util.List; +import org.hamcrest.CoreMatchers; +import org.junit.Test; +import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage; +import org.opendaylight.controller.netconf.util.xml.XmlUtil; + +public class NetconfXMLToHelloMessageDecoderTest { + + @Test + public void testDecodeWithHeader() throws Exception { + final ByteBuf src = Unpooled.wrappedBuffer(String.format("%s\n%s", + "[tomas;10.0.0.0:10000;tcp;client;]", "").getBytes()); + final List out = Lists.newArrayList(); + new NetconfXMLToHelloMessageDecoder().decode(null, src, out); + + assertEquals(1, out.size()); + assertThat(out.get(0), CoreMatchers.instanceOf(NetconfHelloMessage.class)); + final NetconfHelloMessage hello = (NetconfHelloMessage) out.get(0); + assertTrue(hello.getAdditionalHeader().isPresent()); + assertEquals("[tomas;10.0.0.0:10000;tcp;client;]\n", hello.getAdditionalHeader().get().toFormattedString()); + assertThat(XmlUtil.toString(hello.getDocument()), CoreMatchers.containsString("".getBytes()); + final List out = Lists.newArrayList(); + new NetconfXMLToHelloMessageDecoder().decode(null, src, out); + + assertEquals(1, out.size()); + assertThat(out.get(0), CoreMatchers.instanceOf(NetconfHelloMessage.class)); + final NetconfHelloMessage hello = (NetconfHelloMessage) out.get(0); + assertFalse(hello.getAdditionalHeader().isPresent()); + } + + @Test + public void testDecodeCaching() throws Exception { + final ByteBuf msg1 = Unpooled.wrappedBuffer("".getBytes()); + final ByteBuf msg2 = Unpooled.wrappedBuffer("".getBytes()); + final ByteBuf src = Unpooled.wrappedBuffer("".getBytes()); + final List out = Lists.newArrayList(); + final NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder(); + decoder.decode(null, src, out); + decoder.decode(null, msg1, out); + decoder.decode(null, msg2, out); + + assertEquals(1, out.size()); + + assertEquals(2, Iterables.size(decoder.getPostHelloNetconfMessages())); + } + + @Test(expected = IllegalStateException.class) + public void testDecodeNotHelloReceived() throws Exception { + final ByteBuf msg1 = Unpooled.wrappedBuffer("".getBytes()); + final List out = Lists.newArrayList(); + NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder(); + decoder.decode(null, msg1, out); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java new file mode 100644 index 0000000000..f85a38769f --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/NetconfXMLToMessageDecoderTest.java @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler; + +import static org.junit.Assert.assertEquals; + +import com.google.common.collect.Lists; +import io.netty.buffer.Unpooled; +import java.util.ArrayList; +import org.junit.Test; + +public class NetconfXMLToMessageDecoderTest { + + @Test + public void testDecodeNoMoreContent() throws Exception { + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out); + assertEquals(0, out.size()); + } + + @Test + public void testDecode() throws Exception { + final ArrayList out = Lists.newArrayList(); + new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("".getBytes()), out); + assertEquals(1, out.size()); + } +} \ No newline at end of file diff --git a/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPasswordTest.java b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPasswordTest.java new file mode 100644 index 0000000000..01df1e3fce --- /dev/null +++ b/opendaylight/netconf/netconf-netty-util/src/test/java/org/opendaylight/controller/netconf/nettyutil/handler/ssh/authentication/LoginPasswordTest.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; + +import org.apache.sshd.ClientSession; +import org.apache.sshd.client.future.AuthFuture; +import org.junit.Test; + +public class LoginPasswordTest { + + @Test + public void testLoginPassword() throws Exception { + final LoginPassword loginPassword = new LoginPassword("user", "pwd"); + assertEquals("user", loginPassword.getUsername()); + + final ClientSession session = mock(ClientSession.class); + doNothing().when(session).addPasswordIdentity("pwd"); + doReturn(mock(AuthFuture.class)).when(session).auth(); + loginPassword.authenticate(session); + + verify(session).addPasswordIdentity("pwd"); + verify(session).auth(); + } +} \ No newline at end of file -- 2.36.6