BUG-938 Fix negotiation error when using exi
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITSecureTest.java
1 /*
2  * Copyright (c) 2013 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.it;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyString;
13 import static org.mockito.Mockito.doAnswer;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.lang.management.ManagementFactory;
21 import java.net.InetSocketAddress;
22 import java.nio.file.Files;
23 import java.util.Collection;
24 import java.util.List;
25
26 import junit.framework.Assert;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
34 import org.opendaylight.controller.config.spi.ModuleFactory;
35 import org.opendaylight.controller.netconf.api.NetconfMessage;
36 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
37 import org.opendaylight.controller.netconf.client.NetconfSshClientDispatcher;
38 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
39 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
40 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
41 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
42 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
43 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
44 import org.opendaylight.controller.netconf.ssh.NetconfSSHServer;
45 import org.opendaylight.controller.netconf.ssh.authentication.AuthProvider;
46 import org.opendaylight.controller.netconf.ssh.authentication.PEMGenerator;
47 import org.opendaylight.controller.netconf.util.handler.ssh.authentication.AuthenticationHandler;
48 import org.opendaylight.controller.netconf.util.messages.NetconfMessageUtil;
49 import org.opendaylight.controller.netconf.util.test.XmlFileLoader;
50 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
51 import org.opendaylight.controller.sal.authorization.AuthResultEnum;
52 import org.opendaylight.controller.usermanager.IUserManager;
53
54 import ch.ethz.ssh2.Connection;
55 import io.netty.channel.ChannelFuture;
56
57 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
58
59     private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
60     private static final InetSocketAddress tcpAddress = new InetSocketAddress("127.0.0.1", 12023);
61
62     private DefaultCommitNotificationProducer commitNot;
63     private NetconfServerDispatcher dispatchS;
64     private NetconfSSHServer sshServer;
65     private NetconfMessage getConfig;
66
67     @Before
68     public void setUp() throws Exception {
69         this.getConfig = XmlFileLoader.xmlFileToNetconfMessage("netconfMessages/getConfig.xml");
70
71         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext, getModuleFactories().toArray(
72                 new ModuleFactory[0])));
73
74         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
75         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
76
77         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
78
79
80         dispatchS = createDispatcher(factoriesListener);
81         ChannelFuture s = dispatchS.createServer(tcpAddress);
82         s.await();
83
84         sshServer = NetconfSSHServer.start(tlsAddress.getPort(), tcpAddress, getAuthProvider());
85         Thread thread = new Thread(sshServer);
86         thread.setDaemon(true);
87         thread.start();
88     }
89
90     private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
91         return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
92     }
93
94     @After
95     public void tearDown() throws Exception {
96         sshServer.stop();
97         commitNot.close();
98     }
99
100     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
101         final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
102         return new HardcodedYangStoreService(yangDependencies);
103     }
104
105     protected List<ModuleFactory> getModuleFactories() {
106         return NetconfITTest.getModuleFactoriesS();
107     }
108
109     @Test
110     public void testSecure() throws Exception {
111         NetconfClientDispatcher dispatch = new NetconfSshClientDispatcher(getAuthHandler(), nettyThreadgroup, nettyThreadgroup, 5000);
112         try (TestingNetconfClient netconfClient = new TestingNetconfClient("tls-client", tlsAddress, 4000, dispatch)) {
113             NetconfMessage response = netconfClient.sendMessage(getConfig);
114             Assert.assertFalse("Unexpected error message " + XmlUtil.toString(response.getDocument()),
115                     NetconfMessageUtil.isErrorMessage(response));
116         }
117
118         dispatch.close();
119     }
120
121     public AuthProvider getAuthProvider() throws Exception {
122         final IUserManager userManager = mock(IUserManager.class);
123         doReturn(AuthResultEnum.AUTH_ACCEPT).when(userManager).authenticate(anyString(), anyString());
124
125         final File privateKeyFile = Files.createTempFile("tmp-netconf-test", "pk").toFile();
126         privateKeyFile.deleteOnExit();
127         String privateKeyPEMString = PEMGenerator.generateTo(privateKeyFile);
128         return new AuthProvider(userManager, privateKeyPEMString);
129     }
130
131     public AuthenticationHandler getAuthHandler() throws IOException {
132         final AuthenticationHandler authHandler = mock(AuthenticationHandler.class);
133         doAnswer(new Answer() {
134             @Override
135             public Object answer(final InvocationOnMock invocation) throws Throwable {
136                 Connection conn = (Connection) invocation.getArguments()[0];
137                 conn.authenticateWithPassword("user", "pwd");
138                 return null;
139             }
140         }).when(authHandler).authenticate(any(Connection.class));
141         return authHandler;
142     }
143 }