Merge "OPNFLWPLUG-972: Point to openflowplugin liblldp"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / PublishingChannelInitializerTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Mockito.doThrow;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.collect.Lists;
20 import io.netty.channel.ChannelHandler;
21 import io.netty.channel.ChannelPipeline;
22 import io.netty.channel.group.DefaultChannelGroup;
23 import io.netty.channel.socket.SocketChannel;
24 import io.netty.handler.ssl.SslHandler;
25 import java.net.InetAddress;
26 import java.net.InetSocketAddress;
27 import java.net.UnknownHostException;
28 import javax.net.ssl.SSLEngine;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
34 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfiguration;
35 import org.opendaylight.openflowjava.protocol.api.connection.TlsConfigurationImpl;
36 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionAdapterFactory;
37 import org.opendaylight.openflowjava.protocol.impl.core.connection.ConnectionFacade;
38 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
39 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.KeystoreType;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.config.rev140630.PathType;
42
43 /**
44  * Unit tests for ChannelInitializer.
45  *
46  * @author james.hall
47  */
48 public class PublishingChannelInitializerTest {
49
50     @Mock SocketChannel mockSocketCh ;
51     @Mock ChannelPipeline mockChPipeline ;
52     @Mock SwitchConnectionHandler mockSwConnHandler ;
53     @Mock ConnectionAdapterFactory mockConnAdaptorFactory;
54     @Mock DefaultChannelGroup mockChGrp ;
55     @Mock ConnectionFacade mockConnFacade ;
56     @Mock
57     SSLEngine sslEngine ;
58
59     @Mock SerializationFactory mockSerializationFactory ;
60     @Mock DeserializationFactory mockDeserializationFactory ;
61
62     TlsConfiguration tlsConfiguration ;
63     InetSocketAddress inetSockAddr;
64     TcpChannelInitializer pubChInitializer  ;
65
66     /**
67      * Sets up test environment.
68      */
69     @Before
70     public void setUp() throws Exception {
71         MockitoAnnotations.initMocks(this);
72         pubChInitializer = new TcpChannelInitializer(mockChGrp, mockConnAdaptorFactory) ;
73         pubChInitializer.setSerializationFactory(mockSerializationFactory);
74         pubChInitializer.setDeserializationFactory(mockDeserializationFactory);
75         pubChInitializer.setSwitchIdleTimeout(1) ;
76         pubChInitializer.getConnectionIterator() ;
77         pubChInitializer.setUseBarrier(true);
78
79         when(mockChGrp.size()).thenReturn(1);
80         pubChInitializer.setSwitchConnectionHandler(mockSwConnHandler);
81
82         inetSockAddr = new InetSocketAddress(InetAddress.getLocalHost(), 8675);
83
84         when(mockConnAdaptorFactory.createConnectionFacade(mockSocketCh, null, true)).thenReturn(mockConnFacade);
85         when(mockSocketCh.remoteAddress()).thenReturn(inetSockAddr) ;
86         when(mockSocketCh.localAddress()).thenReturn(inetSockAddr) ;
87         when(mockSocketCh.remoteAddress()).thenReturn(inetSockAddr) ;
88         when(mockSwConnHandler.accept(eq(InetAddress.getLocalHost()))).thenReturn(true) ;
89         when(mockSocketCh.pipeline()).thenReturn(mockChPipeline) ;
90
91         tlsConfiguration = new TlsConfigurationImpl(KeystoreType.JKS, "/selfSignedSwitch", PathType.CLASSPATH,
92                 KeystoreType.JKS, "/selfSignedController", PathType.CLASSPATH,
93                 Lists.newArrayList("TLS_RSA_WITH_AES_128_CBC_SHA", "TLS_RSA_WITH_AES_128_CBC_SHA256"));
94     }
95
96
97     /**
98      * Test channel initialization with encryption config set.
99      */
100     @Test
101     public void testinitChannelEncryptionSet()  {
102         pubChInitializer.setTlsConfiguration(tlsConfiguration);
103         pubChInitializer.initChannel(mockSocketCh) ;
104
105         verifyCommonHandlers();
106         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.SSL_HANDLER.name()),any(SslHandler.class)) ;
107     }
108
109     /**
110      * Test channel initialization with null encryption config.
111      */
112     @Test
113     public void testinitChannelEncryptionSetNullTls()  {
114         pubChInitializer.setTlsConfiguration(null);
115         pubChInitializer.initChannel(mockSocketCh) ;
116
117         verifyCommonHandlers();
118         verify(mockChPipeline, times(0)).addLast(eq(PipelineHandlers.SSL_HANDLER.name()),any(SslHandler.class)) ;
119     }
120
121     /**
122      * Test channel initialization without setting the encryption.
123      */
124     @Test
125     public void testinitChannelEncryptionNotSet()  {
126         // Without encryption, only the common
127         pubChInitializer.initChannel(mockSocketCh) ;
128
129         verifyCommonHandlers();
130     }
131
132     /**
133      * Test disconnect on new connection rejected.
134      */
135     @Test
136     public void testinitChannelNoEncryptionAcceptFails() throws UnknownHostException  {
137         when(mockSwConnHandler.accept(eq(InetAddress.getLocalHost()))).thenReturn(false) ;
138         pubChInitializer.initChannel(mockSocketCh) ;
139
140         verify(mockSocketCh, times(1)).disconnect();
141         verify(mockChPipeline, times(0)).addLast(any(String.class), any(ChannelHandler.class));
142     }
143
144     /**
145      * Test channel close on exception during initialization.
146      */
147     @Test
148     public void testExceptionThrown() {
149         doThrow(new IllegalArgumentException()).when(mockSocketCh).pipeline() ;
150         pubChInitializer.initChannel(mockSocketCh);
151
152         verify(mockSocketCh, times(1)).close();
153     }
154
155     /**
156      * All paths should install these six handlers.
157      */
158     private void verifyCommonHandlers() {
159         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.IDLE_HANDLER.name()),any(IdleHandler.class)) ;
160         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.OF_DECODER.name()),any(OFDecoder.class)) ;
161         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.OF_ENCODER.name()),any(OFEncoder.class)) ;
162         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.OF_FRAME_DECODER.name()),
163                 any(OFFrameDecoder.class));
164         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.OF_VERSION_DETECTOR.name()),
165                 any(OFVersionDetector.class));
166         verify(mockChPipeline, times(1)).addLast(eq(PipelineHandlers.DELEGATING_INBOUND_HANDLER.name()),
167                 any(DelegatingInboundHandler.class));
168         assertEquals(1, pubChInitializer.size()) ;
169     }
170 }