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