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
8 package org.opendaylight.netconf.callhome.protocol;
10 import static org.junit.Assert.assertFalse;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doNothing;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
15 import static org.mockito.Mockito.spy;
16 import static org.mockito.Mockito.times;
17 import static org.mockito.Mockito.verify;
19 import io.netty.channel.EventLoopGroup;
20 import io.netty.channel.nio.NioEventLoopGroup;
21 import java.io.IOException;
22 import java.net.InetSocketAddress;
23 import java.net.SocketAddress;
24 import java.security.PublicKey;
25 import java.util.HashMap;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.opendaylight.netconf.shaded.sshd.client.SshClient;
36 import org.opendaylight.netconf.shaded.sshd.client.future.AuthFuture;
37 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
38 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
39 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
40 import org.opendaylight.netconf.shaded.sshd.common.io.IoAcceptor;
41 import org.opendaylight.netconf.shaded.sshd.common.io.IoHandler;
42 import org.opendaylight.netconf.shaded.sshd.common.io.IoServiceFactory;
43 import org.opendaylight.netconf.shaded.sshd.common.session.Session;
44 import org.opendaylight.netconf.shaded.sshd.common.session.SessionListener;
46 @RunWith(MockitoJUnitRunner.class)
47 public class NetconfCallHomeServerTest {
48 private static EventLoopGroup EVENT_LOOP_GROUP;
49 private static InetSocketAddress MOCK_ADDRESS;
51 private SshClient mockSshClient;
53 private CallHomeAuthorizationProvider mockCallHomeAuthProv;
55 private CallHomeAuthorization mockAuth;
57 private CallHomeSessionContext.Factory mockFactory;
59 private ClientSession mockSession;
61 private StatusRecorder mockStatusRecorder;
63 private NetconfCallHomeServer instance;
66 public static void beforeClass() {
67 EVENT_LOOP_GROUP = new NioEventLoopGroup();
68 MOCK_ADDRESS = InetSocketAddress.createUnresolved("127.0.0.1", 123);
72 public static void afterClass() {
73 EVENT_LOOP_GROUP.shutdownGracefully();
74 EVENT_LOOP_GROUP = null;
80 mockSshClient = spy(SshClient.setUpDefaultClient());
81 mockCallHomeAuthProv = mock(CallHomeAuthorizationProvider.class);
82 mockAuth = mock(CallHomeAuthorization.class);
83 mockFactory = mock(CallHomeSessionContext.Factory.class);
84 mockSession = mock(ClientSession.class);
85 mockStatusRecorder = mock(StatusRecorder.class);
87 Map<String, String> props = new HashMap<>();
88 props.put("nio-workers", "1");
89 doReturn(EVENT_LOOP_GROUP).when(mockFactory).getNettyGroup();
90 instance = new NetconfCallHomeServer(
91 mockSshClient, mockCallHomeAuthProv, mockFactory, MOCK_ADDRESS, mockStatusRecorder);
95 public void sessionListenerShouldHandleEventsOfKeyEstablishedAndAuthenticated() throws IOException {
96 // Weird - IJ was ok but command line compile failed using the usual array initializer syntax ????
97 SessionListener.Event[] evt = new SessionListener.Event[2];
98 evt[0] = SessionListener.Event.KeyEstablished;
99 evt[1] = SessionListener.Event.Authenticated;
101 int[] hitOpen = new int[2];
105 int[] hitAuth = new int[2];
109 for (int pass = 0; pass < evt.length; pass++) {
111 AuthFuture mockAuthFuture = mock(AuthFuture.class);
112 doReturn(null).when(mockAuthFuture).addListener(any(SshFutureListener.class));
113 CallHomeSessionContext mockContext = mock(CallHomeSessionContext.class);
114 doNothing().when(mockContext).openNetconfChannel();
115 doReturn(mockContext).when(mockSession).getAttribute(any(Session.AttributeKey.class));
117 final PublicKey serverKey = mock(PublicKey.class);
118 doReturn(serverKey).when(mockSession).getServerKey();
120 final SessionListener listener = instance.createSessionListener();
121 doReturn(mockAuthFuture).when(mockContext).authorize();
122 doReturn(false).when(mockSession).isAuthenticated();
124 listener.sessionEvent(mockSession, evt[pass]);
126 verify(mockContext, times(hitOpen[pass])).openNetconfChannel();
127 verify(mockContext, times(hitAuth[pass])).authorize();
132 public void verificationOfTheServerKeyShouldBeSuccessfulForServerIsAllowed() {
134 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
135 Mockito.doReturn("test").when(mockClientSession).toString();
136 SocketAddress mockSocketAddr = mock(SocketAddress.class);
137 PublicKey mockPublicKey = mock(PublicKey.class);
139 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
140 Mockito.doReturn("some-session-name").when(mockAuth).getSessionName();
141 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
142 Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth);
145 assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey));
149 public void verificationOfTheServerKeyShouldFailIfTheServerIsNotAllowed() {
152 ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
153 SocketAddress mockSocketAddr = mock(SocketAddress.class);
154 PublicKey mockPublicKey = mock(PublicKey.class);
156 Mockito.doReturn(false).when(mockAuth).isServerAllowed();
157 Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
158 Mockito.doReturn("").when(mockClientSession).toString();
161 assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey));
165 public void bindShouldStartTheClientAndBindTheAddress() throws IOException {
167 IoAcceptor mockAcceptor = mock(IoAcceptor.class);
168 IoServiceFactory mockMinaFactory = mock(IoServiceFactory.class);
170 Mockito.doReturn(mockAcceptor).when(mockMinaFactory).createAcceptor(any(IoHandler.class));
171 Mockito.doNothing().when(mockAcceptor).bind(any(SocketAddress.class));
172 instance = new NetconfCallHomeServer(
173 mockSshClient, mockCallHomeAuthProv, mockFactory, MOCK_ADDRESS, mockStatusRecorder, mockMinaFactory);
177 verify(mockSshClient, times(1)).start();
178 verify(mockAcceptor, times(1)).bind(MOCK_ADDRESS);