X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-it%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fit%2FNetconfITSecureTest.java;h=67ccf0c02ca4a2531986000bfc66db5bddce29f4;hb=f71a2c712690ecfd1260543ab58d8e16453f7918;hp=a9e8dbe86b06b76087c902521022ff06e239b870;hpb=2bee5a4b37bc6e2f7331df63757e5dc4e0236b29;p=controller.git diff --git a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java index a9e8dbe86b..67ccf0c02c 100644 --- a/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java +++ b/opendaylight/netconf/netconf-it/src/test/java/org/opendaylight/controller/netconf/it/NetconfITSecureTest.java @@ -8,108 +8,71 @@ package org.opendaylight.controller.netconf.it; -import static java.util.Arrays.asList; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; import com.google.common.collect.Lists; -import io.netty.channel.EventLoopGroup; -import io.netty.channel.nio.NioEventLoopGroup; +import io.netty.channel.local.LocalAddress; import io.netty.util.concurrent.Future; import io.netty.util.concurrent.GenericFutureListener; import io.netty.util.concurrent.GlobalEventExecutor; import java.io.IOException; -import java.io.InputStream; -import java.lang.management.ManagementFactory; import java.net.InetSocketAddress; -import java.util.Collection; import java.util.List; import java.util.concurrent.atomic.AtomicInteger; -import junit.framework.Assert; import org.junit.After; import org.junit.Before; import org.junit.Test; -import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver; -import org.opendaylight.controller.config.spi.ModuleFactory; import org.opendaylight.controller.netconf.api.NetconfMessage; +import org.opendaylight.controller.netconf.auth.AuthProvider; import org.opendaylight.controller.netconf.client.NetconfClientDispatcher; import org.opendaylight.controller.netconf.client.NetconfClientDispatcherImpl; import org.opendaylight.controller.netconf.client.SimpleNetconfClientSessionListener; import org.opendaylight.controller.netconf.client.conf.NetconfClientConfiguration; import org.opendaylight.controller.netconf.client.conf.NetconfClientConfigurationBuilder; import org.opendaylight.controller.netconf.client.test.TestingNetconfClient; -import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl; -import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException; -import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; -import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher; -import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl; import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.AuthenticationHandler; import org.opendaylight.controller.netconf.nettyutil.handler.ssh.authentication.LoginPassword; import org.opendaylight.controller.netconf.ssh.NetconfSSHServer; -import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider; -import org.opendaylight.controller.netconf.ssh.authentication.AuthProviderImpl; import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator; import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil; import org.opendaylight.controller.netconf.util.osgi.NetconfConfigUtil; -import org.opendaylight.controller.netconf.util.test.XmlFileLoader; import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.opendaylight.protocol.framework.NeverReconnectStrategy; public class NetconfITSecureTest extends AbstractNetconfConfigTest { - private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024); + public static final int PORT = 12024; + private static final InetSocketAddress TLS_ADDRESS = new InetSocketAddress("127.0.0.1", PORT); + + public static final String USERNAME = "user"; + public static final String PASSWORD = "pwd"; - private DefaultCommitNotificationProducer commitNot; private NetconfSSHServer sshServer; - private NetconfMessage getConfig; @Before public void setUp() throws Exception { - this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml"); - - super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray( - new ModuleFactory[0]))); - - final NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl(); - factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore())); - - commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer()); - - - final NetconfServerDispatcher dispatchS = createDispatcher(factoriesListener); - dispatchS.createLocalServer(NetconfConfigUtil.getNetconfLocalAddress()).await(); - final EventLoopGroup bossGroup = new NioEventLoopGroup(); - sshServer = NetconfSSHServer.start(tlsAddress.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getAuthProvider(), bossGroup); - } - - private NetconfServerDispatcher createDispatcher(final NetconfOperationServiceFactoryListenerImpl factoriesListener) { - return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot); + final char[] pem = PEMGenerator.generate().toCharArray(); + sshServer = NetconfSSHServer.start(TLS_ADDRESS.getPort(), NetconfConfigUtil.getNetconfLocalAddress(), getNettyThreadgroup(), pem); + sshServer.setAuthProvider(getAuthProvider()); } @After public void tearDown() throws Exception { sshServer.close(); - commitNot.close(); sshServer.join(); } - private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException { - final Collection yangDependencies = NetconfITTest.getBasicYangs(); - return new HardcodedYangStoreService(yangDependencies); - } - - protected List getModuleFactories() { - return asList(NetconfITTest.FACTORIES); - } - @Test public void testSecure() throws Exception { final NetconfClientDispatcher dispatch = new NetconfClientDispatcherImpl(getNettyThreadgroup(), getNettyThreadgroup(), getHashedWheelTimer()); try (TestingNetconfClient netconfClient = new TestingNetconfClient("testing-ssh-client", dispatch, getClientConfiguration())) { - NetconfMessage response = netconfClient.sendMessage(getConfig); - Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), + NetconfMessage response = netconfClient.sendMessage(getGetConfig()); + assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), NetconfMessageUtil.isErrorMessage(response)); final NetconfMessage gs = new NetconfMessage(XmlUtil.readXmlToDocument("\n")); response = netconfClient.sendMessage(gs); - Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), + assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()), NetconfMessageUtil.isErrorMessage(response)); } } @@ -138,7 +101,7 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { final int requests = 1000; for (int i = 0; i < requests; i++) { - final Future netconfMessageFuture = netconfClient.sendRequest(getConfig); + final Future netconfMessageFuture = netconfClient.sendRequest(getGetConfig()); futures.add(netconfMessageFuture); netconfMessageFuture.addListener(new GenericFutureListener>() { @Override @@ -156,13 +119,13 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { // Give future listeners some time to finish counter incrementation Thread.sleep(5000); - org.junit.Assert.assertEquals(requests, responseCounter.get()); + assertEquals(requests, responseCounter.get()); } } public NetconfClientConfiguration getClientConfiguration() throws IOException { final NetconfClientConfigurationBuilder b = NetconfClientConfigurationBuilder.create(); - b.withAddress(tlsAddress); + b.withAddress(TLS_ADDRESS); b.withSessionListener(new SimpleNetconfClientSessionListener()); b.withReconnectStrategy(new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, 5000)); b.withProtocol(NetconfClientConfiguration.NetconfClientProtocol.SSH); @@ -172,13 +135,18 @@ public class NetconfITSecureTest extends AbstractNetconfConfigTest { } public AuthProvider getAuthProvider() throws Exception { - final AuthProvider mock = mock(AuthProviderImpl.class); - doReturn(true).when(mock).authenticated(anyString(), anyString()); - doReturn(PEMGenerator.generate().toCharArray()).when(mock).getPEMAsCharArray(); - return mock; + final AuthProvider mockAuth = mock(AuthProvider.class); + doReturn("mockedAuth").when(mockAuth).toString(); + doReturn(true).when(mockAuth).authenticated(anyString(), anyString()); + return mockAuth; } public AuthenticationHandler getAuthHandler() throws IOException { - return new LoginPassword("user", "pwd"); + return new LoginPassword(USERNAME, PASSWORD); + } + + @Override + protected LocalAddress getTcpServerAddress() { + return NetconfConfigUtil.getNetconfLocalAddress(); } }