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.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyBoolean;
13 import static org.mockito.Matchers.anyString;
14 import static org.mockito.Mockito.doNothing;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
20 import io.netty.channel.Channel;
21 import io.netty.channel.ChannelFuture;
22 import io.netty.channel.ChannelPipeline;
23 import io.netty.channel.EventLoopGroup;
24 import java.io.IOException;
25 import java.net.InetSocketAddress;
26 import java.security.PublicKey;
27 import org.apache.sshd.ClientChannel;
28 import org.apache.sshd.ClientChannel.Streaming;
29 import org.apache.sshd.ClientSession;
30 import org.apache.sshd.client.channel.ChannelSubsystem;
31 import org.apache.sshd.client.future.OpenFuture;
32 import org.apache.sshd.client.session.ClientSessionImpl;
33 import org.apache.sshd.common.KeyExchange;
34 import org.apache.sshd.common.Session.AttributeKey;
35 import org.apache.sshd.common.future.SshFutureListener;
36 import org.apache.sshd.common.io.IoInputStream;
37 import org.apache.sshd.common.io.IoOutputStream;
38 import org.apache.sshd.common.io.IoReadFuture;
39 import org.apache.sshd.common.io.IoSession;
40 import org.apache.sshd.common.util.Buffer;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.mockito.Mockito;
44 import org.opendaylight.netconf.client.NetconfClientSessionListener;
45 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
47 public class CallHomeSessionContextTest {
48 private ClientSessionImpl mockSession;
49 private CallHomeAuthorization mockAuth;
50 private ClientChannel mockChannel;
51 private InetSocketAddress address;
53 private ReverseSshChannelInitializer mockChannelInitializer;
54 private CallHomeNetconfSubsystemListener subListener;
55 private EventLoopGroup mockNettyGroup;
56 private CallHomeSessionContext.Factory realFactory;
57 private CallHomeSessionContext instance;
58 private NetconfClientSessionNegotiatorFactory mockNegotiatior;
62 mockSession = mock(ClientSessionImpl.class);
63 mockAuth = mock(CallHomeAuthorization.class);
64 mockChannel = mock(ClientChannel.class);
65 address = mock(InetSocketAddress.class);
67 mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
68 subListener = mock(CallHomeNetconfSubsystemListener.class);
69 mockNettyGroup = mock(EventLoopGroup.class);
71 realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
73 KeyExchange kexMock = Mockito.mock(KeyExchange.class);
74 Mockito.doReturn(kexMock).when(mockSession).getKex();
76 PublicKey keyMock = Mockito.mock(PublicKey.class);
77 Mockito.doReturn(keyMock).when(kexMock).getServerKey();
78 IoReadFuture mockFuture = mock(IoReadFuture.class);
79 IoInputStream mockIn = mock(IoInputStream.class);
80 Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
81 IoOutputStream mockOut = mock(IoOutputStream.class);
83 Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
84 Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
86 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
88 IoSession ioSession = mock(IoSession.class);
89 Mockito.doReturn(ioSession).when(mockSession).getIoSession();
90 Mockito.doReturn(address).when(ioSession).getRemoteAddress();
91 Mockito.doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
92 Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
93 Mockito.doReturn("testSession").when(mockSession).toString();
95 doNothing().when(mockAuth).applyTo(mockSession);
96 Mockito.doReturn("test").when(mockAuth).getSessionName();
100 public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
101 // redo instance below because previous constructor happened too early to capture behavior
102 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
104 CallHomeSessionContext.getFrom(mockSession);
106 verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
107 verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
111 public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
112 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
114 Mockito.doReturn(null).when(mockSession).auth();
115 instance.authorize();
117 verify(mockAuth, times(1)).applyTo(mockSession);
121 public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
123 OpenFuture mockFuture = mock(OpenFuture.class);
124 ChannelSubsystem mockChannel = mock(ChannelSubsystem.class);
125 Mockito.doReturn(mockFuture).when(mockChannel).open();
126 Mockito.doReturn(mockChannel).when(mockSession).createSubsystemChannel(anyString());
128 Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
129 doNothing().when(mockChannel).setStreaming(any(Streaming.class));
130 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
132 instance.openNetconfChannel();
134 verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
137 static class TestableContext extends CallHomeSessionContext {
138 MinaSshNettyChannel minaMock;
140 TestableContext(ClientSession sshSession, CallHomeAuthorization authorization,
141 InetSocketAddress address, CallHomeSessionContext.Factory factory,
142 MinaSshNettyChannel minaMock) {
143 super(sshSession, authorization, address, factory);
144 this.minaMock = minaMock;
148 protected MinaSshNettyChannel newMinaSshNettyChannel(ClientChannel netconfChannel) {
154 public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
156 MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
157 CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
159 CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
160 doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
161 any(CallHomeChannelActivator.class));
163 ChannelFuture mockChanFuture = mock(ChannelFuture.class);
164 Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
166 Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
167 Mockito.doReturn(mockChannelInitializer).when(mockFactory)
168 .getChannelInitializer(any(NetconfClientSessionListener.class));
169 Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
171 ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
172 Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
174 OpenFuture mockFuture = mock(OpenFuture.class);
175 Mockito.doReturn(true).when(mockFuture).isOpened();
177 instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
178 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
180 listener.operationComplete(mockFuture);
182 verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
183 any(CallHomeChannelActivator.class));
187 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
189 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
191 OpenFuture mockFuture = mock(OpenFuture.class);
192 Mockito.doReturn(false).when(mockFuture).isOpened();
193 Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
195 doReturn(null).when(mockSession).close(anyBoolean());
198 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
199 listener.operationComplete(mockFuture);
201 // You'll see an error message logged to the console - it is expected.
202 verify(mockSession, times(1)).close(anyBoolean());