2 * Copyright (c) 2016 Cisco Systems, Inc. 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.nettyutil;
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;
22 import com.google.common.base.Optional;
23 import io.netty.buffer.ByteBuf;
24 import io.netty.buffer.Unpooled;
25 import io.netty.channel.ChannelInboundHandlerAdapter;
26 import io.netty.channel.ChannelOutboundHandler;
27 import io.netty.channel.ChannelOutboundHandlerAdapter;
28 import io.netty.channel.embedded.EmbeddedChannel;
29 import io.netty.handler.ssl.SslHandler;
30 import io.netty.util.HashedWheelTimer;
31 import io.netty.util.concurrent.Future;
32 import io.netty.util.concurrent.Promise;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import org.junit.Assert;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.MockitoAnnotations;
42 import org.opendaylight.netconf.api.NetconfSessionListener;
43 import org.opendaylight.netconf.api.NetconfSessionPreferences;
44 import org.opendaylight.netconf.api.messages.NetconfHelloMessage;
45 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
46 import org.opendaylight.netconf.api.xml.XmlUtil;
47 import org.opendaylight.netconf.nettyutil.handler.ChunkedFramingMechanismEncoder;
48 import org.opendaylight.netconf.nettyutil.handler.EOMFramingMechanismEncoder;
49 import org.opendaylight.netconf.nettyutil.handler.FramingMechanismHandlerFactory;
50 import org.opendaylight.netconf.nettyutil.handler.NetconfChunkAggregator;
51 import org.opendaylight.netconf.nettyutil.handler.NetconfEOMAggregator;
52 import org.opendaylight.netconf.nettyutil.handler.NetconfXMLToHelloMessageDecoder;
53 import org.opendaylight.netconf.util.messages.FramingMechanism;
55 public class AbstractNetconfSessionNegotiatorTest {
58 private NetconfSessionListener<TestingNetconfSession> listener;
60 private Promise<TestingNetconfSession> promise;
62 private SslHandler sslHandler;
63 private EmbeddedChannel channel;
64 private AbstractNetconfSessionNegotiator negotiator;
65 private NetconfHelloMessage hello;
66 private NetconfHelloMessage helloBase11;
67 private NetconfXMLToHelloMessageDecoder xmlToHello;
68 private NetconfSessionPreferences prefs;
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
73 channel = new EmbeddedChannel();
74 xmlToHello = new NetconfXMLToHelloMessageDecoder();
75 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_ENCODER,
76 new ChannelInboundHandlerAdapter());
77 channel.pipeline().addLast(AbstractChannelInitializer.NETCONF_MESSAGE_DECODER, xmlToHello);
78 channel.pipeline().addLast(NETCONF_MESSAGE_FRAME_ENCODER,
79 FramingMechanismHandlerFactory.createHandler(FramingMechanism.EOM));
80 channel.pipeline().addLast(NETCONF_MESSAGE_AGGREGATOR, new NetconfEOMAggregator());
81 hello = NetconfHelloMessage.createClientHello(Collections.emptySet(), Optional.absent());
82 helloBase11 = NetconfHelloMessage.createClientHello(Collections
83 .singleton(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1), Optional.absent());
84 prefs = new NetconfSessionPreferences(helloBase11);
85 doReturn(promise).when(promise).setFailure(any());
86 doReturn(promise).when(promise).setSuccess(any());
87 negotiator = new TestSessionNegotiator(prefs, promise, channel, new HashedWheelTimer(), listener, 100L);
91 public void testStartNegotiation() throws Exception {
92 negotiator.startNegotiation();
93 Assert.assertEquals(helloBase11, channel.readOutbound());
97 public void testStartNegotiationSsl() throws Exception {
98 doReturn(true).when(sslHandler).isSharable();
99 doNothing().when(sslHandler).handlerAdded(any());
100 doNothing().when(sslHandler).write(any(), any(), any());
101 final Future<EmbeddedChannel> handshakeFuture = channel.eventLoop().newSucceededFuture(channel);
102 doReturn(handshakeFuture).when(sslHandler).handshakeFuture();
103 channel.pipeline().addLast(sslHandler);
104 negotiator.startNegotiation();
105 verify(sslHandler, timeout(1000)).write(any(), eq(helloBase11), any());
110 public void testStartNegotiationNotEstablished() throws Exception {
111 final ChannelOutboundHandler closedDetector = Mockito.spy(new ChannelOutboundHandlerAdapter());
112 channel.pipeline().addLast("closedDetector", closedDetector);
113 doReturn(false).when(promise).isDone();
114 doReturn(false).when(promise).isCancelled();
115 negotiator.startNegotiation();
116 verify(closedDetector, timeout(2000)).close(any(), any());
120 public void testGetSessionPreferences() throws Exception {
121 Assert.assertEquals(prefs, negotiator.getSessionPreferences());
125 public void testGetSessionForHelloMessage() throws Exception {
126 negotiator.startNegotiation();
127 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(hello);
128 Assert.assertNotNull(session);
129 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfEOMAggregator);
130 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER) instanceof EOMFramingMechanismEncoder);
134 public void testGetSessionForHelloMessageBase11() throws Exception {
135 negotiator.startNegotiation();
136 final AbstractNetconfSession session = negotiator.getSessionForHelloMessage(helloBase11);
137 Assert.assertNotNull(session);
138 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_AGGREGATOR) instanceof NetconfChunkAggregator);
139 Assert.assertTrue(channel.pipeline().get(NETCONF_MESSAGE_FRAME_ENCODER)
140 instanceof ChunkedFramingMechanismEncoder);
144 public void testReplaceHelloMessageInboundHandler() throws Exception {
145 final List<Object> out = new ArrayList<>();
146 final byte[] msg = "<rpc/>".getBytes();
147 final ByteBuf msgBuf = Unpooled.wrappedBuffer(msg);
148 final ByteBuf helloBuf = Unpooled.wrappedBuffer(XmlUtil.toString(hello.getDocument()).getBytes());
149 negotiator.startNegotiation();
150 xmlToHello.decode(null, helloBuf, out);
151 xmlToHello.decode(null, msgBuf, out);
152 final AbstractNetconfSession session = mock(AbstractNetconfSession.class);
153 doNothing().when(session).handleMessage(any());
154 negotiator.replaceHelloMessageInboundHandler(session);
155 verify(session, times(1)).handleMessage(any());
159 public void testNegotiationFail() throws Exception {
160 negotiator.startNegotiation();
161 final RuntimeException cause = new RuntimeException("failure cause");
162 channel.pipeline().fireExceptionCaught(cause);
163 verify(promise).setFailure(cause);