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.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.spy;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.net.SocketAddress;
25 import java.security.PublicKey;
26 import java.util.HashMap;
28 import org.apache.sshd.client.SshClient;
29 import org.apache.sshd.client.future.AuthFuture;
30 import org.apache.sshd.client.session.ClientSession;
31 import org.apache.sshd.client.session.ClientSessionImpl;
32 import org.apache.sshd.common.future.SshFutureListener;
33 import org.apache.sshd.common.io.IoAcceptor;
34 import org.apache.sshd.common.io.IoHandler;
35 import org.apache.sshd.common.io.IoServiceFactory;
36 import org.apache.sshd.common.session.Session;
37 import org.apache.sshd.common.session.SessionListener;
38 import org.junit.AfterClass;
39 import org.junit.Before;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.junit.MockitoJUnitRunner;
47 @RunWith(MockitoJUnitRunner.class)
48 public class NetconfCallHomeServerTest {
49 private static EventLoopGroup EVENT_LOOP_GROUP;
50 private static InetSocketAddress MOCK_ADDRESS;
52 private SshClient mockSshClient;
54 private CallHomeAuthorizationProvider mockCallHomeAuthProv;
56 private CallHomeAuthorization mockAuth;
58 private CallHomeSessionContext.Factory mockFactory;
60 private ClientSession mockSession;
62 private StatusRecorder mockStatusRecorder;
64 private NetconfCallHomeServer instance;
67 public static void beforeClass() {
68 EVENT_LOOP_GROUP = new NioEventLoopGroup();
69 MOCK_ADDRESS = InetSocketAddress.createUnresolved("127.0.0.1", 123);
73 public static void afterClass() {
74 EVENT_LOOP_GROUP.shutdownGracefully();
75 EVENT_LOOP_GROUP = null;
81 mockSshClient = spy(SshClient.setUpDefaultClient());
82 mockCallHomeAuthProv = mock(CallHomeAuthorizationProvider.class);
83 mockAuth = mock(CallHomeAuthorization.class);
84 mockFactory = mock(CallHomeSessionContext.Factory.class);
85 mockSession = mock(ClientSession.class);
86 mockStatusRecorder = mock(StatusRecorder.class);
88 Map<String, String> props = new HashMap<>();
89 props.put("nio-workers", "1");
90 doReturn(EVENT_LOOP_GROUP).when(mockFactory).getNettyGroup();
91 instance = new NetconfCallHomeServer(
92 mockSshClient, mockCallHomeAuthProv, mockFactory, MOCK_ADDRESS, mockStatusRecorder);
96 public void sessionListenerShouldHandleEventsOfKeyEstablishedAndAuthenticated() throws IOException {
97 // Weird - IJ was ok but command line compile failed using the usual array initializer syntax ????
98 SessionListener.Event[] evt = new SessionListener.Event[2];
99 evt[0] = SessionListener.Event.KeyEstablished;
100 evt[1] = SessionListener.Event.Authenticated;
102 int[] hitOpen = new int[2];
106 int[] hitAuth = new int[2];
110 for (int pass = 0; pass < evt.length; pass++) {
112 AuthFuture mockAuthFuture = mock(AuthFuture.class);
113 doReturn(null).when(mockAuthFuture).addListener(any(SshFutureListener.class));
114 CallHomeSessionContext mockContext = mock(CallHomeSessionContext.class);
115 doNothing().when(mockContext).openNetconfChannel();
116 doReturn(mockContext).when(mockSession).getAttribute(any(Session.AttributeKey.class));
117 SessionListener listener = instance.createSessionListener();
118 doReturn(mockAuthFuture).when(mockContext).authorize();
120 listener.sessionEvent(mockSession, evt[pass]);
122 verify(mockContext, times(hitOpen[pass])).openNetconfChannel();
123 verify(mockContext, times(hitAuth[pass])).authorize();
128 public void verificationOfTheServerKeyShouldBeSuccessfulForServerIsAllowed() {
131 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
132 Mockito.doReturn("test").when(mockClientSession).toString();
133 SocketAddress mockSocketAddr = mock(SocketAddress.class);
134 PublicKey mockPublicKey = mock(PublicKey.class);
136 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
137 Mockito.doReturn("some-session-name").when(mockAuth).getSessionName();
139 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
141 Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth, mockSocketAddr);
144 instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey);
148 public void verificationOfTheServerKeyShouldFailIfTheServerIsNotAllowed() {
151 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
152 SocketAddress mockSocketAddr = mock(SocketAddress.class);
153 PublicKey mockPublicKey = mock(PublicKey.class);
155 Mockito.doReturn(false).when(mockAuth).isServerAllowed();
156 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
157 Mockito.doReturn("").when(mockClientSession).toString();
160 assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey));
164 public void bindShouldStartTheClientAndBindTheAddress() throws IOException {
166 IoAcceptor mockAcceptor = mock(IoAcceptor.class);
167 IoServiceFactory mockMinaFactory = mock(IoServiceFactory.class);
169 Mockito.doReturn(mockAcceptor).when(mockMinaFactory).createAcceptor(any(IoHandler.class));
170 Mockito.doNothing().when(mockAcceptor).bind(any(SocketAddress.class));
171 instance = new NetconfCallHomeServer(
172 mockSshClient, mockCallHomeAuthProv, mockFactory, MOCK_ADDRESS, mockStatusRecorder, mockMinaFactory);
176 verify(mockSshClient, times(1)).start();
177 verify(mockAcceptor, times(1)).bind(MOCK_ADDRESS);