Bump odlparent to 4.0.10
[netconf.git] / netconf / callhome-protocol / src / test / java / org / opendaylight / netconf / callhome / protocol / NetconfCallHomeServerTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.netconf.callhome.protocol;
10
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;
19
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;
27 import java.util.Map;
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;
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class NetconfCallHomeServerTest {
49     private static EventLoopGroup EVENT_LOOP_GROUP;
50     private static InetSocketAddress MOCK_ADDRESS;
51
52     private SshClient mockSshClient;
53     @Mock
54     private CallHomeAuthorizationProvider mockCallHomeAuthProv;
55     @Mock
56     private CallHomeAuthorization mockAuth;
57     @Mock
58     private CallHomeSessionContext.Factory mockFactory;
59     @Mock
60     private ClientSession mockSession;
61     @Mock
62     private StatusRecorder mockStatusRecorder;
63
64     private NetconfCallHomeServer instance;
65
66     @BeforeClass
67     public static void beforeClass() {
68         EVENT_LOOP_GROUP = new NioEventLoopGroup();
69         MOCK_ADDRESS = InetSocketAddress.createUnresolved("127.0.0.1", 123);
70     }
71
72     @AfterClass
73     public static void afterClass() {
74         EVENT_LOOP_GROUP.shutdownGracefully();
75         EVENT_LOOP_GROUP = null;
76         MOCK_ADDRESS = null;
77     }
78
79     @Before
80     public void setup() {
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);
87
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);
93     }
94
95     @Test
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;
101
102         int[] hitOpen = new int[2];
103         hitOpen[0] = 0;
104         hitOpen[1] = 1;
105
106         int[] hitAuth = new int[2];
107         hitAuth[0] = 1;
108         hitAuth[1] = 0;
109
110         for (int pass = 0; pass < evt.length; pass++) {
111             // given
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();
119             // when
120             listener.sessionEvent(mockSession, evt[pass]);
121             // then
122             verify(mockContext, times(hitOpen[pass])).openNetconfChannel();
123             verify(mockContext, times(hitAuth[pass])).authorize();
124         }
125     }
126
127     @Test
128     public void verificationOfTheServerKeyShouldBeSuccessfulForServerIsAllowed() {
129         // given
130
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);
135
136         Mockito.doReturn(true).when(mockAuth).isServerAllowed();
137         Mockito.doReturn("some-session-name").when(mockAuth).getSessionName();
138
139         Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
140
141         Mockito.doReturn(null).when(mockFactory).createIfNotExists(mockClientSession, mockAuth, mockSocketAddr);
142
143         // expect
144         instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey);
145     }
146
147     @Test
148     public void verificationOfTheServerKeyShouldFailIfTheServerIsNotAllowed() {
149         // given
150
151         ClientSessionImpl mockClientSession = mock(ClientSessionImpl.class);
152         SocketAddress mockSocketAddr = mock(SocketAddress.class);
153         PublicKey mockPublicKey = mock(PublicKey.class);
154
155         Mockito.doReturn(false).when(mockAuth).isServerAllowed();
156         Mockito.doReturn(mockAuth).when(mockCallHomeAuthProv).provideAuth(mockSocketAddr, mockPublicKey);
157         Mockito.doReturn("").when(mockClientSession).toString();
158
159         // expect
160         assertFalse(instance.verifyServerKey(mockClientSession, mockSocketAddr, mockPublicKey));
161     }
162
163     @Test
164     public void bindShouldStartTheClientAndBindTheAddress() throws IOException {
165         // given
166         IoAcceptor mockAcceptor = mock(IoAcceptor.class);
167         IoServiceFactory mockMinaFactory = mock(IoServiceFactory.class);
168
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);
173         // when
174         instance.bind();
175         // then
176         verify(mockSshClient, times(1)).start();
177         verify(mockAcceptor, times(1)).bind(MOCK_ADDRESS);
178     }
179 }