Merge "Bug 8153: Enforce check-style rules for netconf - aaa-authn-odl-plugin"
[netconf.git] / netconf / netconf-netty-util / src / test / java / org / opendaylight / netconf / nettyutil / AbstractNetconfSessionNegotiatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.nettyutil;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.timeout;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_AGGREGATOR;
20 import static org.opendaylight.netconf.nettyutil.AbstractChannelInitializer.NETCONF_MESSAGE_FRAME_ENCODER;
21
22 import com.google.common.base.Optional;
23 import io.netty.buffer.ByteBuf;
24 import io.netty.buffer.Unpooled;
25 import io.netty.channel.Channel;
26 import io.netty.channel.ChannelInboundHandlerAdapter;
27 import io.netty.channel.ChannelOutboundHandler;
28 import io.netty.channel.ChannelOutboundHandlerAdapter;
29 import io.netty.channel.embedded.EmbeddedChannel;
30 import io.netty.handler.ssl.SslHandler;
31 import io.netty.util.HashedWheelTimer;
32 import io.netty.util.Timer;
33 import io.netty.util.concurrent.Future;
34 import io.netty.util.concurrent.Promise;
35 import java.util.ArrayList;
36 import java.util.Collections;
37 import java.util.List;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.MockitoAnnotations;
44 import org.opendaylight.controller.config.util.xml.XmlUtil;
45 import org.opendaylight.netconf.api.NetconfDocumentedException;
46 import org.opendaylight.netconf.api.NetconfSessionListener;
47 import org.opendaylight.netconf.api.NetconfSessionPreferences;
48 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
49 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
50 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
51 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
52 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
53 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
54 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
55 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
56 import org.opendaylight.netconf.util.messages.FramingMechanism;
57
58 public class AbstractNetconfSessionNegotiatorTest {
59
60     @Mock
61     private NetconfSessionListener<TestingNetconfSession> listener;
62     @Mock
63     private Promise<TestingNetconfSession> promise;
64     @Mock
65     private SslHandler sslHandler;
66     private EmbeddedChannel channel;
67     private AbstractNetconfSessionNegotiator negotiator;
68     private NetconfHelloMessage hello;
69     private NetconfHelloMessage helloBase11;
70     private NetconfXMLToHelloMessageDecoder xmlToHello;
71     private NetconfSessionPreferences prefs;
72
73     @Before
74     public void setUp() throws Exception {
75         MockitoAnnotations.initMocks(this);
76         channel = new EmbeddedChannel();
77         xmlToHello = new NetconfXMLToHelloMessageDecoder();
78         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
79                 new ChannelInboundHandlerAdapter());
80         channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, xmlToHello);
81         channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
82                 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
83         channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
84         hello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.absent());
85         helloBase11 = NetconfHelloMessage.createClientHello(Collections
86                 .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent());
87         prefs = new NetconfSessionPreferences(helloBase11);
88         doReturn(promise).when(promise).setFailure(any());
89         doReturn(promise).when(promise).setSuccess(any());
90         negotiator = new TestSessionNegotiator(prefs, promise, channel, new HashedWheelTimer(), listener, 100L);
91     }
92
93     @Test
94     public void testStartNegotiation() throws Exception {
95         negotiator.startNegotiation();
96         Assert.assertEquals(helloBase11, channel.readOutbound());
97     }
98
99     @Test
100     public void testStartNegotiationSsl() throws Exception {
101         doReturn(true).when(sslHandler).isSharable();
102         doNothing().when(sslHandler).handlerAdded(any());
103         doNothing().when(sslHandler).write(any(), any(), any());
104         final Future<EmbeddedChannel> handshakeFuture = channel.eventLoop().newSucceededFuture(channel);
105         doReturn(handshakeFuture).when(sslHandler).handshakeFuture();
106         channel.pipeline().addLast(sslHandler);
107         negotiator.startNegotiation();
108         verify(sslHandler, timeout(1000)).write(any(), eq(helloBase11), any());
109
110     }
111
112     @Test
113     public void testStartNegotiationNotEstablished() throws Exception {
114         final ChannelOutboundHandler closedDetector = Mockito.spy(new ChannelOutboundHandlerAdapter());
115         channel.pipeline().addLast("closedDetector", closedDetector);
116         doReturn(false).when(promise).isDone();
117         doReturn(false).when(promise).isCancelled();
118         negotiator.startNegotiation();
119         verify(closedDetector, timeout(2000)).close(any(), any());
120     }
121
122     @Test
123     public void testGetSessionPreferences() throws Exception {
124         Assert.assertEquals(prefs, negotiator.getSessionPreferences());
125     }
126
127     @Test
128     public void testGetSessionForHelloMessage() throws Exception {
129         negotiator.startNegotiation();
130         final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(hello);
131         Assert.assertNotNull(session);
132         Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfEOMAggregator);
133         Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof EOMFramingMechanismEncoder);
134     }
135
136     @Test
137     public void testGetSessionForHelloMessageBase11() throws Exception {
138         negotiator.startNegotiation();
139         final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloBase11);
140         Assert.assertNotNull(session);
141         Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
142         Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER)
143                 instanceof ChunkedFramingMechanismEncoder);
144     }
145
146     @Test
147     public void testReplaceHelloMessageInboundHandler() throws Exception {
148         final List<Object> out = new ArrayList<>();
149         final byte[] msg = "<rpc/>".getBytes();
150         final ByteBuf msgBuf = Unpooled.wrappedBuffer(msg);
151         final ByteBuf helloBuf = Unpooled.wrappedBuffer(XmlUtil.toString(hello.getDocument()).getBytes());
152         negotiator.startNegotiation();
153         xmlToHello.decode(null, helloBuf, out);
154         xmlToHello.decode(null, msgBuf, out);
155         final AbstractNetconfSession session = mock(AbstractNetconfSession.class);
156         doNothing().when(session).handleMessage(any());
157         negotiator.replaceHelloMessageInboundHandler(session);
158         verify(session, times(1)).handleMessage(any());
159     }
160
161     @Test
162     public void testNegotiationFail() throws Exception {
163         negotiator.startNegotiation();
164         final RuntimeException cause = new RuntimeException("failure cause");
165         channel.pipeline().fireExceptionCaught(cause);
166         verify(promise).setFailure(cause);
167     }
168
169     private static class TestSessionNegotiator extends
170             AbstractNetconfSessionNegotiator<NetconfSessionPreferences,
171                     TestingNetconfSession, NetconfSessionListener<TestingNetconfSession>> {
172
173
174         TestSessionNegotiator(final NetconfSessionPreferences sessionPreferences,
175                               final Promise<TestingNetconfSession> promise, final Channel channel,
176                               final Timer timer,
177                               final NetconfSessionListener<TestingNetconfSession> sessionListener,
178                               final long connectionTimeoutMillis) {
179             super(sessionPreferences, promise, channel, timer, sessionListener, connectionTimeoutMillis);
180         }
181
182         @Override
183         protected TestingNetconfSession getSession(final NetconfSessionListener sessionListener, final Channel channel,
184                                                final NetconfHelloMessage message) throws NetconfDocumentedException {
185             return new TestingNetconfSession(sessionListener, channel, 0L);
186         }
187
188         @Override
189         protected void handleMessage(final NetconfHelloMessage netconfHelloMessage) throws Exception {
190
191         }
192     }
193
194
195 }