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.spy;
19 import static org.mockito.Mockito.times;
20 import static org.mockito.Mockito.verify;
22 import io.netty.channel.Channel;
23 import io.netty.channel.ChannelFuture;
24 import io.netty.channel.ChannelPipeline;
25 import io.netty.channel.DefaultChannelPipeline;
26 import io.netty.channel.EventLoopGroup;
27 import java.net.InetSocketAddress;
28 import java.security.PublicKey;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.opendaylight.netconf.callhome.protocol.CallHomeSessionContext.Factory;
32 import org.opendaylight.netconf.client.NetconfClientSessionListener;
33 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
34 import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfClientSessionImpl;
35 import org.opendaylight.netconf.nettyutil.handler.ssh.client.NettyPipelineAwareChannelSubsystem;
36 import org.opendaylight.netconf.shaded.sshd.client.channel.ClientChannel;
37 import org.opendaylight.netconf.shaded.sshd.client.future.OpenFuture;
38 import org.opendaylight.netconf.shaded.sshd.common.AttributeRepository.AttributeKey;
39 import org.opendaylight.netconf.shaded.sshd.common.channel.StreamingChannel;
40 import org.opendaylight.netconf.shaded.sshd.common.future.SshFutureListener;
41 import org.opendaylight.netconf.shaded.sshd.common.io.IoInputStream;
42 import org.opendaylight.netconf.shaded.sshd.common.io.IoOutputStream;
43 import org.opendaylight.netconf.shaded.sshd.common.io.IoReadFuture;
44 import org.opendaylight.netconf.shaded.sshd.common.io.IoSession;
45 import org.opendaylight.netconf.shaded.sshd.common.kex.KeyExchange;
46 import org.opendaylight.netconf.shaded.sshd.common.util.buffer.Buffer;
48 public class CallHomeSessionContextTest {
49 private NetconfClientSessionImpl mockSession;
50 private CallHomeAuthorization mockAuth;
51 private ClientChannel mockChannel;
52 private EventLoopGroup mockNettyGroup;
53 private Factory realFactory;
54 private CallHomeSessionContext instance;
58 mockSession = mock(NetconfClientSessionImpl.class);
59 mockAuth = mock(CallHomeAuthorization.class);
60 mockChannel = mock(ClientChannel.class);
62 final var mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
63 final var subListener = mock(CallHomeNetconfSubsystemListener.class);
64 mockNettyGroup = mock(EventLoopGroup.class);
65 realFactory = new Factory(mockNettyGroup, mockNegotiatior, subListener);
67 final var kexMock = mock(KeyExchange.class);
68 doReturn(kexMock).when(mockSession).getKex();
69 final var keyMock = mock(PublicKey.class);
70 doReturn(keyMock).when(mockSession).getServerKey();
72 final var mockFuture = mock(IoReadFuture.class);
73 final var mockIn = mock(IoInputStream.class);
74 doReturn(mockFuture).when(mockIn).read(any(Buffer.class));
75 final var mockOut = mock(IoOutputStream.class);
76 doReturn(mockIn).when(mockChannel).getAsyncOut();
77 doReturn(mockOut).when(mockChannel).getAsyncIn();
79 final var ioSession = mock(IoSession.class);
80 doReturn(ioSession).when(mockSession).getIoSession();
81 final var address = mock(InetSocketAddress.class);
82 doReturn(address).when(ioSession).getRemoteAddress();
83 doReturn(null).when(mockSession).setAttribute(any(AttributeKey.class), any());
84 doReturn(null).when(mockSession).getAttribute(any(AttributeKey.class));
85 doReturn("testSession").when(mockSession).toString();
87 doReturn(true).when(mockAuth).isServerAllowed();
88 doNothing().when(mockAuth).applyTo(mockSession);
89 doReturn("test").when(mockAuth).getSessionName();
93 public void theContextShouldBeSettableAndRetrievableAsASessionAttribute() {
95 instance = realFactory.createIfNotExists(mockSession, mockAuth);
97 assertNotNull(instance);
98 verify(mockSession, times(1)).setAttribute(CallHomeSessionContext.SESSION_KEY, instance);
99 verify(mockSession, times(0)).getAttribute(any());
102 CallHomeSessionContext.getFrom(mockSession);
104 verify(mockSession, times(1)).getAttribute(CallHomeSessionContext.SESSION_KEY);
108 public void anAuthorizeActionShouldApplyToTheBoundSession() throws Exception {
109 instance = realFactory.createIfNotExists(mockSession, mockAuth);
111 doReturn(null).when(mockSession).auth();
112 instance.authorize();
114 verify(mockAuth, times(1)).applyTo(mockSession);
118 public void creatingAChannelSuccessfullyShouldResultInAnAttachedListener() throws Exception {
120 final var mockFuture = mock(OpenFuture.class);
121 final var mockChannelSubsystem = mock(NettyPipelineAwareChannelSubsystem.class);
122 doReturn(mockFuture).when(mockChannelSubsystem).open();
123 doReturn(mockChannelSubsystem).when(mockSession).createSubsystemChannel(anyString(),
124 any(DefaultChannelPipeline.class));
126 doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
127 doNothing().when(mockChannelSubsystem).setStreaming(any(StreamingChannel.Streaming.class));
128 instance = realFactory.createIfNotExists(mockSession, mockAuth);
130 instance.openNetconfChannel();
132 verify(mockFuture, times(1)).addListener(any(SshFutureListener.class));
136 public void openingTheChannelSuccessfullyNotifyTheChannelListener() {
138 final var mockListener = mock(CallHomeNetconfSubsystemListener.class);
139 doNothing().when(mockListener).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
140 any(CallHomeChannelActivator.class));
142 final var mockChanFuture = mock(ChannelFuture.class);
143 doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
145 final var mockFactory = mock(Factory.class);
146 doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
147 final var mockChannelInitializer = mock(ReverseSshChannelInitializer.class);
148 doReturn(mockChannelInitializer).when(mockFactory)
149 .getChannelInitializer(any(NetconfClientSessionListener.class));
150 doReturn(mockListener).when(mockFactory).getChannelOpenListener();
152 final var mockPipeline = mock(ChannelPipeline.class);
153 final var mockMinaChannel = mock(MinaSshNettyChannel.class);
154 doReturn(mockPipeline).when(mockMinaChannel).pipeline();
156 final var mockFuture = mock(OpenFuture.class);
157 doReturn(true).when(mockFuture).isOpened();
159 instance = spy(new CallHomeSessionContext(mockSession, mockAuth, mockFactory));
160 doReturn(mockMinaChannel).when(instance).newMinaSshNettyChannel();
161 final var listener = instance.newSshFutureListener(mockChannel, mockMinaChannel);
164 listener.operationComplete(mockFuture);
166 verify(mockListener, times(1)).onNetconfSubsystemOpened(any(CallHomeProtocolSessionContext.class),
167 any(CallHomeChannelActivator.class));
171 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
173 instance = realFactory.createIfNotExists(mockSession, mockAuth);
174 final var mockFuture = mock(OpenFuture.class);
175 doReturn(false).when(mockFuture).isOpened();
176 doReturn(new RuntimeException("test")).when(mockFuture).getException();
177 doReturn(null).when(mockSession).close(anyBoolean());
180 final var listener = instance.newSshFutureListener(mockChannel, null);
181 listener.operationComplete(mockFuture);
183 // You'll see an error message logged to the console - it is expected.
184 verify(mockSession, times(1)).close(anyBoolean());
188 public void theContextConstructorShouldNotModifySession() {
189 instance = new CallHomeSessionContext(mockSession, mockAuth, realFactory);
190 verify(mockSession, times(0)).setAttribute(any(), any());
191 assertNull(CallHomeSessionContext.getFrom(mockSession));