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.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyBoolean;
13 import static org.mockito.ArgumentMatchers.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.client.channel.ChannelSubsystem;
28 import org.apache.sshd.client.channel.ClientChannel;
29 import org.apache.sshd.client.channel.ClientChannel.Streaming;
30 import org.apache.sshd.client.future.OpenFuture;
31 import org.apache.sshd.client.session.ClientSession;
32 import org.apache.sshd.client.session.ClientSessionImpl;
33 import org.apache.sshd.common.AttributeRepository.AttributeKey;
34 import org.apache.sshd.common.future.SshFutureListener;
35 import org.apache.sshd.common.io.IoInputStream;
36 import org.apache.sshd.common.io.IoOutputStream;
37 import org.apache.sshd.common.io.IoReadFuture;
38 import org.apache.sshd.common.io.IoSession;
39 import org.apache.sshd.common.kex.KeyExchange;
40 import org.apache.sshd.common.util.buffer.Buffer;
41 import org.junit.Before;
42 import org.junit.Ignore;
43 import org.junit.Test;
44 import org.mockito.Mockito;
45 import org.opendaylight.netconf.client.NetconfClientSessionListener;
46 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
48 public class CallHomeSessionContextTest {
49 private ClientSessionImpl mockSession;
50 private CallHomeAuthorization mockAuth;
51 private ClientChannel mockChannel;
52 private InetSocketAddress address;
54 private ReverseSshChannelInitializer mockChannelInitializer;
55 private CallHomeNetconfSubsystemListener subListener;
56 private EventLoopGroup mockNettyGroup;
57 private CallHomeSessionContext.Factory realFactory;
58 private CallHomeSessionContext instance;
59 private NetconfClientSessionNegotiatorFactory mockNegotiatior;
63 mockSession = mock(ClientSessionImpl.class);
64 mockAuth = mock(CallHomeAuthorization.class);
65 mockChannel = mock(ClientChannel.class);
66 address = mock(InetSocketAddress.class);
68 mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
69 subListener = mock(CallHomeNetconfSubsystemListener.class);
70 mockNettyGroup = mock(EventLoopGroup.class);
72 realFactory = new CallHomeSessionContext.Factory(mockNettyGroup, mockNegotiatior, subListener);
74 KeyExchange kexMock = Mockito.mock(KeyExchange.class);
75 Mockito.doReturn(kexMock).when(mockSession).getKex();
77 PublicKey keyMock = Mockito.mock(PublicKey.class);
78 Mockito.doReturn(keyMock).when(kexMock).getServerKey();
79 IoReadFuture mockFuture = mock(IoReadFuture.class);
80 IoInputStream mockIn = mock(IoInputStream.class);
81 Mockito.doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
82 IoOutputStream mockOut = mock(IoOutputStream.class);
84 Mockito.doReturn(mockIn).when(mockChannel).getAsyncOut();
85 Mockito.doReturn(mockOut).when(mockChannel).getAsyncIn();
87 Mockito.doReturn(true).when(mockAuth).isServerAllowed();
89 IoSession ioSession = mock(IoSession.class);
90 Mockito.doReturn(ioSession).when(mockSession).getIoSession();
91 Mockito.doReturn(address).when(ioSession).getRemoteAddress();
92 Mockito.doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
93 Mockito.doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
94 Mockito.doReturn("testSession").when(mockSession).toString();
96 doNothing().when(mockAuth).applyTo(mockSession);
97 Mockito.doReturn("test").when(mockAuth).getSessionName();
101 public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
102 // redo instance below because previous constructor happened too early to capture behavior
103 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
105 CallHomeSessionContext.getFrom(mockSession);
107 verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
108 verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
112 public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
113 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
115 Mockito.doReturn(null).when(mockSession).auth();
116 instance.authorize();
118 verify(mockAuth, times(1)).applyTo(mockSession);
122 public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
124 OpenFuture mockFuture = mock(OpenFuture.class);
125 ChannelSubsystem mockChannelSubsystem = mock(ChannelSubsystem.class);
126 Mockito.doReturn(mockFuture).when(mockChannelSubsystem).open();
127 Mockito.doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
129 Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
130 doNothing().when(mockChannelSubsystem).setStreaming(any(Streaming.class));
131 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
133 instance.openNetconfChannel();
135 verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
138 static class TestableContext extends CallHomeSessionContext {
139 MinaSshNettyChannel minaMock;
141 TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
142 final InetSocketAddress address, final CallHomeSessionContext.Factory factory,
143 final MinaSshNettyChannel minaMock) {
144 super(sshSession, authorization, address, factory);
145 this.minaMock = minaMock;
149 protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {
155 public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
157 MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
158 CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
160 CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
161 doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
162 any(CallHomeChannelActivator.class));
164 ChannelFuture mockChanFuture = mock(ChannelFuture.class);
165 Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
167 Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
168 Mockito.doReturn(mockChannelInitializer).when(mockFactory)
169 .getChannelInitializer(any(NetconfClientSessionListener.class));
170 Mockito.doReturn(mockListener).when(mockFactory).getChannelOpenListener();
172 ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
173 Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
175 OpenFuture mockFuture = mock(OpenFuture.class);
176 Mockito.doReturn(true).when(mockFuture).isOpened();
178 instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
179 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
181 listener.operationComplete(mockFuture);
183 verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
184 any(CallHomeChannelActivator.class));
189 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
191 instance = realFactory.createIfNotExists(mockSession, mockAuth, address);
193 OpenFuture mockFuture = mock(OpenFuture.class);
194 Mockito.doReturn(false).when(mockFuture).isOpened();
195 Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
197 doReturn(null).when(mockSession).close(anyBoolean());
200 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
201 listener.operationComplete(mockFuture);
203 // You'll see an error message logged to the console - it is expected.
204 verify(mockSession, times(1)).close(anyBoolean());