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.mock;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
18 import io.netty.channel.Channel;
19 import io.netty.channel.ChannelFuture;
20 import io.netty.channel.ChannelPipeline;
21 import io.netty.channel.EventLoopGroup;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 import java.security.PublicKey;
25 import org.apache.sshd.ClientChannel;
26 import org.apache.sshd.ClientChannel.Streaming;
27 import org.apache.sshd.ClientSession;
28 import org.apache.sshd.client.channel.ChannelSubsystem;
29 import org.apache.sshd.client.future.OpenFuture;
30 import org.apache.sshd.client.session.ClientSessionImpl;
31 import org.apache.sshd.common.KeyExchange;
32 import org.apache.sshd.common.Session.AttributeKey;
33 import org.apache.sshd.common.future.SshFutureListener;
34 import org.apache.sshd.common.io.IoInputStream;
35 import org.apache.sshd.common.io.IoOutputStream;
36 import org.apache.sshd.common.io.IoReadFuture;
37 import org.apache.sshd.common.io.IoSession;
38 import org.apache.sshd.common.util.Buffer;
39 import org.junit.Before;
40 import org.junit.Ignore;
41 import org.junit.Test;
42 import org.mockito.Mockito;
43 import org.opendaylight.netconf.client.NetconfClientSessionListener;
44 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
46 public class CallHomeSessionContextTest {
47 private ClientSessionImpl mockSession;
48 private CallHomeAuthorization mockAuth;
49 private ClientChannel mockChannel;
50 private InetSocketAddress address;
52 private ReverseSshChannelInitializer mockChannelInitializer;
53 private CallHomeNetconfSubsystemListener subListener;
54 private EventLoopGroup mockNettyGroup;
55 private CallHomeSessionContext.Factory realFactory;
56 private CallHomeSessionContext instance;
57 private NetconfClientSessionNegotiatorFactory mockNegotiatior;
61 mockSession = mock(ClientSessionImpl.class);
62 mockAuth = mock(CallHomeAuthorization.class);
63 mockChannel = mock(ClientChannel.class);
64 address = mock(InetSocketAddress.class);
66 mockNegotiatior = mock(NetconfClientSessionNegotiatorFactory.class);
67 subListener = mock(CallHomeNetconfSubsystemListener.class);
68 mockNettyGroup = mock(EventLoopGroup.class);
70 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 Mockito.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 mockChannel = mock(ChannelSubsystem.class);
126 Mockito.doReturn(mockFuture).when(mockChannel).open();
127 Mockito.doReturn(mockChannel).when(mockSession).createSubsystemChannel(anyString());
129 Mockito.doReturn(null).when(mockFuture).addListener(any(SshFutureListener.class));
130 Mockito.doNothing().when(mockChannel).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 public TestableContext(ClientSession sshSession, CallHomeAuthorization authorization, InetSocketAddress address,
142 CallHomeSessionContext.Factory factory, MinaSshNettyChannel minaMock) {
143 super(sshSession, authorization, address, factory);
144 this.minaMock = minaMock;
148 protected MinaSshNettyChannel newMinaSshNettyChannel(ClientChannel netconfChannel) {
155 public void openingTheChannelSuccessfullyShouldFireActiveChannel() {
157 MinaSshNettyChannel mockMinaChannel = mock(MinaSshNettyChannel.class);
158 CallHomeSessionContext.Factory mockFactory = mock(CallHomeSessionContext.Factory.class);
160 ChannelFuture mockChanFuture = mock(ChannelFuture.class);
161 Mockito.doReturn(mockChanFuture).when(mockNettyGroup).register(any(Channel.class));
163 Mockito.doReturn(mockNettyGroup).when(mockFactory).getNettyGroup();
164 Mockito.doReturn(mockChannelInitializer).when(mockFactory)
165 .getChannelInitializer(any(NetconfClientSessionListener.class));
167 ChannelPipeline mockPipeline = mock(ChannelPipeline.class);
168 Mockito.doReturn(mockPipeline).when(mockMinaChannel).pipeline();
170 OpenFuture mockFuture = mock(OpenFuture.class);
171 Mockito.doReturn(true).when(mockFuture).isOpened();
173 instance = new TestableContext(mockSession, mockAuth, address, mockFactory, mockMinaChannel);
174 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
176 listener.operationComplete(mockFuture);
178 verify(mockPipeline, times(1)).fireChannelActive();
183 public void failureToOpenTheChannelShouldCauseTheSessionToClose() {
185 SshFutureListener<OpenFuture> listener = instance.newSshFutureListener(mockChannel);
186 OpenFuture mockFuture = mock(OpenFuture.class);
187 Mockito.doReturn(false).when(mockFuture).isOpened();
188 Mockito.doReturn(new RuntimeException("test")).when(mockFuture).getException();
190 listener.operationComplete(mockFuture);
192 verify(mockSession, times(1)).close(anyBoolean());