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.Test;
30 import org.opendaylight.netconf.client.NetconfClientSessionListener;
31 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
32 import org.opendaylight.netconf.shaded.sshd.client.channel.ChannelSubsystem;
33 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
34 import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
35 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSession;
36 import org.opendaylight.netconf.shaded.sshd.client.session.ClientSessionImpl;
37 import org.opendaylight.netconf.shaded.sshd.common.AttributeRepository.AttributeKey;
38 import org.opendaylight.netconf.shaded.sshd.common.channel.StreamingChannel;
39 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
40 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
41 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
42 import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
43 import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
44 import org.opendaylight.netconf.shaded.sshd.common.kex.KeyExchange;
45 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
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 = mock(KeyExchange.class);
74 doReturn(kexMock).when(mockSession).getKex();
76 PublicKey keyMock = mock(PublicKey.class);
77 doReturn(keyMock).when(mockSession).getServerKey();
78 IoReadFuture mockFuture = mock(IoReadFuture.class);
79 IoInputStream mockIn = mock(IoInputStream.class);
80 doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
81 IoOutputStream mockOut = mock(IoOutputStream.class);
83 doReturn(mockIn).when(mockChannel).getAsyncOut();
84 doReturn(mockOut).when(mockChannel).getAsyncIn();
86 doReturn(true).when(mockAuth).isServerAllowed();
88 IoSession ioSession = mock(IoSession.class);
89 doReturn(ioSession).when(mockSession).getIoSession();
90 doReturn(address).when(ioSession).getRemoteAddress();
91 doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
92 doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
93 doReturn("testSession").when(mockSession).toString();
95 doNothing().when(mockAuth).applyTo(mockSession);
96 doReturn("test").when(mockAuth).getSessionName();
100 public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
102 instance = realFactory.createIfNotExists(mockSession, mockAuth);
104 assertNotNull(instance);
105 verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
106 verify(mockSession, times(0)).getAttribute(any());
109 CallHomeSessionContext.getFrom(mockSession);
111 verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
115 public void anAuthorizeActionShouldApplyToTheBoundSession() throws IOException {
116 instance = realFactory.createIfNotExists(mockSession, mockAuth);
118 doReturn(null).when(mockSession).auth();
119 instance.authorize();
121 verify(mockAuth, times(1)).applyTo(mockSession);
125 public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws IOException {
127 OpenFuture mockFuture = mock(OpenFuture.class);
128 ChannelSubsystem mockChannelSubsystem = mock(ChannelSubsystem.class);
129 doReturn(mockFuture).when(mockChannelSubsystem).open();
130 doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString());
132 doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
133 doNothing().when(mockChannelSubsystem).setStreaming(any(StreamingChannel.Streaming.class));
134 instance = realFactory.createIfNotExists(mockSession, mockAuth);
136 instance.openNetconfChannel();
138 verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
142 public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
144 MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
145 CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
147 CallHomeNetconfSubsystemListener mockListener = mock(CallHomeNetconfSubsystemListener.class);
148 doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
149 any(CallHomeChannelActivator.class));
151 ChannelFuture mockChanFuture = mock(ChannelFuture.class);
152 doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
154 doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
155 doReturn(mockChannelInitializer).when(mockFactory)
156 .getChannelInitializer(any(NetconfClientSessionListener.class));
157 doReturn(mockListener).when(mockFactory).getChannelOpenListener();
159 ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
160 doReturn(mockPipeline).when(mockMinaChannel).pipeline();
162 OpenFuture mockFuture = mock(OpenFuture.class);
163 doReturn(true).when(mockFuture).isOpened();
165 instance = new TestableContext(mockSession, mockAuth, mockFactory, mockMinaChannel);
166 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
168 listener.operationComplete(mockFuture);
170 verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
171 any(CallHomeChannelActivator.class));
175 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
177 instance = realFactory.createIfNotExists(mockSession, mockAuth);
178 OpenFuture mockFuture = mock(OpenFuture.class);
179 doReturn(false).when(mockFuture).isOpened();
180 doReturn(new RuntimeException("test")).when(mockFuture).getException();
182 doReturn(null).when(mockSession).close(anyBoolean());
185 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
186 listener.operationComplete(mockFuture);
188 // You'll see an error message logged to the console - it is expected.
189 verify(mockSession, times(1)).close(anyBoolean());
193 public void theContextConstructorShouldNotModifySession() {
194 instance = new CallHomeSessionContext(mockSession, mockAuth, realFactory);
195 verify(mockSession, times(0)).setAttribute(any(), any());
196 assertNull(CallHomeSessionContext.getFrom(mockSession));
199 static class TestableContext extends CallHomeSessionContext {
200 MinaSshNettyChannel minaMock;
202 TestableContext(final ClientSession sshSession, final CallHomeAuthorization authorization,
203 final CallHomeSessionContext.Factory factory, final MinaSshNettyChannel minaMock) {
204 super(sshSession, authorization, factory);
205 this.minaMock = minaMock;
209 protected MinaSshNettyChannel newMinaSshNettyChannel(final ClientChannel netconfChannel) {