<groupId>org.openexi</groupId>
<artifactId>nagasena-rta</artifactId>
</dependency>
-
<dependency>
<groupId>org.osgi</groupId>
<artifactId>org.osgi.core</artifactId>
<groupId>xmlunit</groupId>
<artifactId>xmlunit</artifactId>
</dependency>
-
+ <dependency>
+ <groupId>org.opendaylight.yangtools</groupId>
+ <artifactId>mockito-configuration</artifactId>
+ </dependency>
</dependencies>
<build>
Optional<NetconfHelloMessageAdditionalHeader> 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));
}
*/
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<Object> out) throws Exception {
if (in.readableBytes() != 0) {
package org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication;
-import ch.ethz.ssh2.Connection;
import java.io.IOException;
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();
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;
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;
--- /dev/null
+/*
+ * 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<ByteBuf> 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;
+ }
+}
--- /dev/null
+/*
+ * 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
--- /dev/null
+/*
+ * 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
*/
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" +
@Test
public void testMultipleChunks() throws Exception {
- List<Object> output = Lists.newArrayList();
- ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE.getBytes(Charsets.UTF_8));
+ final List<Object> 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<Object> output = Lists.newArrayList();
- ByteBuf input = Unpooled.copiedBuffer(CHUNKED_MESSAGE_ONE.getBytes(Charsets.UTF_8));
+ final List<Object> 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));
}
--- /dev/null
+/*
+ * 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("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"),
+ 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("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+ }
+
+ @Test
+ public void testEncodeNoHeader() throws Exception {
+ final NetconfMessage msg = new NetconfHelloMessage(XmlUtil.readXmlToDocument("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+ 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("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+ }
+
+ @Test(expected = IllegalStateException.class)
+ public void testEncodeNotHello() throws Exception {
+ final NetconfMessage msg = new NetconfMessage(XmlUtil.readXmlToDocument("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>"));
+ new NetconfHelloMessageToXMLEncoder().encode(ctx, msg, null);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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;]", "<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>").getBytes());
+ final List<Object> 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("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\""));
+ }
+
+ @Test
+ public void testDecodeNoHeader() throws Exception {
+ final ByteBuf src = Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
+ final List<Object> 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("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
+ final ByteBuf msg2 = Unpooled.wrappedBuffer("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
+ final ByteBuf src = Unpooled.wrappedBuffer("<hello xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
+ final List<Object> 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("<rpc-reply xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"/>".getBytes());
+ final List<Object> out = Lists.newArrayList();
+ NetconfXMLToHelloMessageDecoder decoder = new NetconfXMLToHelloMessageDecoder();
+ decoder.decode(null, msg1, out);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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<Object> out = Lists.newArrayList();
+ new NetconfXMLToMessageDecoder().decode(null, Unpooled.buffer(), out);
+ assertEquals(0, out.size());
+ }
+
+ @Test
+ public void testDecode() throws Exception {
+ final ArrayList<Object> out = Lists.newArrayList();
+ new NetconfXMLToMessageDecoder().decode(null, Unpooled.wrappedBuffer("<msg/>".getBytes()), out);
+ assertEquals(1, out.size());
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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