2 * Copyright (c) 2016 Brocade Communication Systems 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.callhome.protocol;
11 import static org.junit.Assert.assertFalse;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.times;
15 import static org.mockito.Mockito.verify;
17 import java.io.IOException;
18 import java.net.InetSocketAddress;
19 import java.net.SocketAddress;
20 import java.security.PublicKey;
21 import java.util.HashMap;
23 import org.apache.sshd.client.SshClient;
24 import org.apache.sshd.client.future.AuthFuture;
25 import org.apache.sshd.client.session.ClientSession;
26 import org.apache.sshd.client.session.ClientSessionImpl;
27 import org.apache.sshd.common.future.SshFutureListener;
28 import org.apache.sshd.common.io.IoAcceptor;
29 import org.apache.sshd.common.io.IoHandler;
30 import org.apache.sshd.common.io.IoServiceFactory;
31 import org.apache.sshd.common.session.Session;
32 import org.apache.sshd.common.session.SessionListener;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
37 public class NetconfCallHomeServerTest {
38 private SshClient mockSshClient;
39 private CallHomeAuthorizationProvider mockCallHomeAuthProv;
40 private CallHomeAuthorization mockAuth;
41 private CallHomeSessionContext.Factory mockFactory;
42 private InetSocketAddress mockAddress;
43 private ClientSession mockSession;
44 private StatusRecorder mockStatusRecorder;
46 private NetconfCallHomeServer instance;
50 mockSshClient = Mockito.spy(SshClient.setUpDefaultClient());
51 mockCallHomeAuthProv = mock(CallHomeAuthorizationProvider.class);
52 mockAuth = mock(CallHomeAuthorization.class);
53 mockFactory = mock(CallHomeSessionContext.Factory.class);
54 mockAddress = InetSocketAddress.createUnresolved("1.2.3.4", 123);
55 mockSession = mock(ClientSession.class);
56 mockStatusRecorder = mock(StatusRecorder.class);
58 Map<String, String> props = new HashMap<>();
59 props.put("nio-workers", "1");
60 Mockito.doReturn(props).when(mockSshClient).getProperties();
61 Mockito.doReturn("test").when(mockSession).toString();
62 instance = new NetconfCallHomeServer(
63 mockSshClient, mockCallHomeAuthProv, mockFactory, mockAddress, mockStatusRecorder);
67 public void sessionListenerShouldHandleEventsOfKeyEstablishedAndAuthenticated() throws IOException {
68 // Weird - IJ was ok but command line compile failed using the usual array initializer syntax ????
69 SessionListener.Event[] evt = new SessionListener.Event[2];
70 evt[0] = SessionListener.Event.KeyEstablished;
71 evt[1] = SessionListener.Event.Authenticated;
73 int[] hitOpen = new int[2];
77 int[] hitAuth = new int[2];
81 for (int pass = 0; pass < evt.length; pass++) {
83 AuthFuture mockAuthFuture = mock(AuthFuture.class);
84 Mockito.doReturn(null).when(mockAuthFuture).addListener(any(SshFutureListener.class));
85 CallHomeSessionContext mockContext = mock(CallHomeSessionContext.class);
86 Mockito.doNothing().when(mockContext).openNetconfChannel();
87 Mockito.doReturn(mockContext).when(mockSession).getAttribute(any(Session.AttributeKey.class));
88 SessionListener listener = instance.createSessionListener();
89 Mockito.doReturn(mockAuthFuture).when(mockContext).authorize();
91 listener.sessionEvent(mockSession, evt[pass]);
93 verify(mockContext, times(hitOpen[pass])).openNetconfChannel();
94 verify(mockContext, times(hitAuth[pass])).authorize();
99 public void verificationOfTheServerKeyShouldBeSuccessfulForServerIsAllowed() {
102 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
103 Mockito.doReturn("test").when(mockClientSession).toString();
104 SocketAddress mockSocketAddr = mock(SocketAddress.class);
105 Mockito.doReturn("testAddr").when(mockSocketAddr).toString();
106 PublicKey mockPublicKey = mock(PublicKey.class);
108 Mockito.doReturn("test").when(mockAuth).toString();
109 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
110 Mockito.doReturn("some-session-name").when(mockAuth).getSessionName();
112 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
114 Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth, mockSocketAddr);
117 instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey);
121 public void verificationOfTheServerKeyShouldFailIfTheServerIsNotAllowed() {
124 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
125 SocketAddress mockSocketAddr = mock(SocketAddress.class);
126 PublicKey mockPublicKey = mock(PublicKey.class);
128 Mockito.doReturn(false).when(mockAuth).isServerAllowed();
129 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
130 Mockito.doReturn("").when(mockClientSession).toString();
133 assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey));
136 static class TestableCallHomeServer extends NetconfCallHomeServer {
137 static IoServiceFactory minaServiceFactory;
139 static SshClient factoryHook(final SshClient client, final IoServiceFactory minaFactory) {
140 minaServiceFactory = minaFactory;
146 TestableCallHomeServer(final SshClient sshClient, final CallHomeAuthorizationProvider authProvider,
147 final CallHomeSessionContext.Factory factory, final InetSocketAddress socketAddress,
148 final IoServiceFactory minaFactory, final StatusRecorder recorder) {
149 super(factoryHook(sshClient, minaFactory), authProvider, factory, socketAddress, recorder);
154 protected IoServiceFactory createMinaServiceFactory(final SshClient sshClient) {
155 return minaServiceFactory;
160 public void bindShouldStartTheClientAndBindTheAddress() throws IOException {
162 IoAcceptor mockAcceptor = mock(IoAcceptor.class);
163 IoServiceFactory mockMinaFactory = mock(IoServiceFactory.class);
165 Mockito.doReturn(mockAcceptor).when(mockMinaFactory).createAcceptor(any(IoHandler.class));
166 Mockito.doReturn(mockAcceptor).when(mockMinaFactory).createAcceptor(any(IoHandler.class));
167 Mockito.doNothing().when(mockAcceptor).bind(mockAddress);
168 instance = new TestableCallHomeServer(
169 mockSshClient, mockCallHomeAuthProv, mockFactory, mockAddress, mockMinaFactory, mockStatusRecorder);
173 verify(mockSshClient, times(1)).start();
174 verify(mockAcceptor, times(1)).bind(mockAddress);