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.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyBoolean;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.times;
19 import static org.mockito.Mockito.verify;
21 import io.netty.channel.Channel;
22 import io.netty.channel.ChannelFuture;
23 import io.netty.channel.ChannelPipeline;
24 import io.netty.channel.EventLoopGroup;
25 import java.io.IOException;
26 import java.net.InetSocketAddress;
27 import java.security.PublicKey;
28 import org.junit.Before;
29 import org.junit.Ignore;
30 import org.junit.Test;
31 import org.mockito.Mockito;
32 import org.opendaylight.netconf.client.NetconfClientSessionListener;
33 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
34 import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
35 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
36 import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
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.AttributeRepository.AttributeKey;
40 import org.opendaylight.netconf.shaded.sshd.common.channel.StreamingChannel;
41 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
42 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
43 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
44 import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
45 import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
46 import org.opendaylight.netconf.shaded.sshd.common.kex.KeyExchange;
47 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
49 public class CallHomeSessionContextTest {
50 private ClientSessionImpl mockSession;
51 private CallHomeAuthorization mockAuth;
52 private ClientChannel mockChannel;
53 private InetSocketAddress address;
55 private ReverseSshChannelInitializer mockChannelInitializer;
56 private CallHomeNetconfSubsystemListener subListener;
57 private EventLoopGroup mockNettyGroup;
58 private CallHomeSessionContext.Factory realFactory;
59 private CallHomeSessionContext instance;
60 private NetconfClientSessionNegotiatorFactory mockNegotiatior;
64 mockSession = mock(ClientSessionImpl.class);
65 mockAuth = mock(CallHomeAuthorization.class);
66 mockChannel = mock(ClientChannel.class);
67 address = mock(InetSocketAddress.class);
69 mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
70 subListener = mock(CallHomeNetconfSubsystemListener.class);
71 mockNettyGroup = mock(EventLoopGroup.class);
73 realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
75 KeyExchange kexMock = Mockito.mock(KeyExchange.class);
76 Mockito.doReturn(kexMock).when(mockSession).getKex();
78 PublicKey keyMock = Mockito.mock(PublicKey.class);
79 Mockito.doReturn(keyMock).when(mockSession).getServerKey();
80 IoReadFuture mockFuture = mock(IoReadFuture.class);
81 IoInputStream mockIn = mock(IoInputStream.class);
82 Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
83 IoOutputStream mockOut = mock(IoOutputStream.class);
85 Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
86 Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
88 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
90 IoSession ioSession = mock(IoSession.class);
91 Mockito.doReturn(ioSession).when(mockSession).getIoSession();
92 Mockito.doReturn(address).when(ioSession).getRemoteAddress();
93 Mockito.doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
94 Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
95 Mockito.doReturn("testSession").when(mockSession).toString();
97 doNothing().when(mockAuth).applyTo(mockSession);
98 Mockito.doReturn("test").when(mockAuth).getSessionName();
102 public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
104 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
106 assertNotNull(instance);
107 verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
108 verify(mockSession, times(0)).getAttribute(any());
111 CallHomeSessionContext.getFrom(mockSession);
113 verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
117 public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
118 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
120 Mockito.doReturn(null).when(mockSession).auth();
121 instance.authorize();
123 verify(mockAuth, times(1)).applyTo(mockSession);
127 public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
129 OpenFuture mockFuture = mock(OpenFuture.class);
130 ChannelSubsystem mockChannelSubsystem = mock(ChannelSubsystem.class);
131 Mockito.doReturn(mockFuture).when(mockChannelSubsystem).open();
132 Mockito.doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
134 Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
135 doNothing().when(mockChannelSubsystem).setStreaming(any(StreamingChannel.Streaming.class));
136 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
138 instance.openNetconfChannel();
140 verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
143 static class TestableContext extends CallHomeSessionContext {
144 MinaSshNettyChannel minaMock;
146 TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
147 final InetSocketAddress address, final CallHomeSessionContext.Factory factory,
148 final MinaSshNettyChannel minaMock) {
149 super(sshSession, authorization, address, factory);
150 this.minaMock = minaMock;
154 protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
160 public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
162 MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
163 CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
165 CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
166 doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
167 any(CallHomeChannelActivator.class));
169 ChannelFuture mockChanFuture = mock(ChannelFuture.class);
170 Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
172 Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
173 Mockito.doReturn(mockChannelInitializer).when(mockFactory)
174 .getChannelInitializer(any(NetconfClientSessionListener.class));
175 Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
177 ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
178 Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
180 OpenFuture mockFuture = mock(OpenFuture.class);
181 Mockito.doReturn(true).when(mockFuture).isOpened();
183 instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
184 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
186 listener.operationComplete(mockFuture);
188 verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
189 any(CallHomeChannelActivator.class));
194 // FIXME: enable this test
195 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
197 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
198 OpenFuture mockFuture = mock(OpenFuture.class);
199 Mockito.doReturn(false).when(mockFuture).isOpened();
200 Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
202 doReturn(null).when(mockSession).close(anyBoolean());
205 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
206 listener.operationComplete(mockFuture);
208 // You'll see an error message logged to the console - it is expected.
209 verify(mockSession, times(1)).close(anyBoolean());
213 public void theContextConstructorShouldNotModifySession() {
214 instance = new CallHomeSessionContext(mockSession, mockAuth, address, realFactory);
215 verify(mockSession, times(0)).setAttribute(any(), any());
216 assertNull(CallHomeSessionContext.getFrom(mockSession));