2 * Copyright (c) 2014 Brocade Communications 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.openflowjava.protocol.impl.core;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.fail;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import io.netty.channel.ChannelHandlerContext;
16 import io.netty.channel.unix.Errors;
17 import java.io.IOException;
18 import java.net.BindException;
19 import java.net.InetAddress;
20 import java.net.InetSocketAddress;
21 import java.net.Socket;
22 import java.util.concurrent.ExecutionException;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
28 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
29 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
32 * Unit tests for TcpHandler.
36 @RunWith(MockitoJUnitRunner.class)
37 public class TcpHandlerTest {
39 private final InetAddress serverAddress = InetAddress.getLoopbackAddress() ;
40 @Mock ChannelHandlerContext mockChHndlrCtx ;
41 @Mock TcpChannelInitializer mockChannelInitializer;
42 @Mock SwitchConnectionHandler mockSwitchConnHndler ;
43 @Mock SerializationFactory mockSerializationFactory ;
44 @Mock DeserializationFactory mockDeserializationFactory ;
46 TcpHandler tcpHandler ;
49 * Test run with null address set.
52 public void testRunWithNullAddress() throws IOException, InterruptedException, ExecutionException {
53 tcpHandler = new TcpHandler(null, 0, () -> { });
54 tcpHandler.setChannelInitializer(mockChannelInitializer);
56 assertEquals("failed to start server", true, startupServer(false)) ;
57 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
62 * Test run with null address set on Epoll native transport.
65 public void testRunWithNullAddressOnEpoll() throws IOException, InterruptedException, ExecutionException {
66 tcpHandler = new TcpHandler(null, 0, () -> { });
67 tcpHandler.setChannelInitializer(mockChannelInitializer);
69 //Use Epoll native transport
70 assertEquals("failed to start server", true, startupServer(true)) ;
71 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
76 * Test run with address set.
79 public void testRunWithAddress() throws IOException, InterruptedException, ExecutionException {
80 tcpHandler = new TcpHandler(serverAddress, 0, () -> { });
81 tcpHandler.setChannelInitializer(mockChannelInitializer);
83 assertEquals("failed to start server", true, startupServer(false)) ;
84 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
89 * Test run with address set on Epoll native transport.
92 public void testRunWithAddressOnEpoll() throws IOException, InterruptedException, ExecutionException {
93 tcpHandler = new TcpHandler(serverAddress, 0, () -> { });
94 tcpHandler.setChannelInitializer(mockChannelInitializer);
96 //Use Epoll native transport
97 assertEquals("failed to start server", true, startupServer(true));
98 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
103 * Test run with encryption.
106 public void testRunWithEncryption() throws InterruptedException, IOException, ExecutionException {
107 int serverPort = 28001;
108 tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
109 tcpHandler.setChannelInitializer(mockChannelInitializer);
111 assertEquals("failed to start server", true, startupServer(false));
112 assertEquals("wrong connection count", 0, tcpHandler.getNumberOfConnections());
113 assertEquals("wrong port", serverPort, tcpHandler.getPort());
114 assertEquals("wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
116 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
122 * Test run with encryption on Epoll native transport.
125 public void testRunWithEncryptionOnEpoll() throws InterruptedException, IOException, ExecutionException {
126 int serverPort = 28001;
127 tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
128 tcpHandler.setChannelInitializer(mockChannelInitializer);
130 //Use Epoll native transport
131 assertEquals("failed to start server", true, startupServer(true));
132 assertEquals("wrong connection count", 0, tcpHandler.getNumberOfConnections());
133 assertEquals("wrong port", serverPort, tcpHandler.getPort());
134 assertEquals("wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress());
136 assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort()));
142 * Test run on already used port.
144 @Test(expected = BindException.class)
145 public void testSocketAlreadyInUse() throws IOException {
146 int serverPort = 28001;
147 Socket firstBinder = new Socket();
150 firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
151 tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
152 tcpHandler.setChannelInitializer(mockChannelInitializer);
153 tcpHandler.initiateEventLoopGroups(null, false);
159 * Test run on already used port.
162 public void testSocketAlreadyInUseOnEpoll() throws IOException {
163 int serverPort = 28001;
164 Socket firstBinder = new Socket();
167 firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
169 tcpHandler = new TcpHandler(serverAddress, serverPort, () -> { });
170 tcpHandler.setChannelInitializer(mockChannelInitializer);
171 //Use Epoll native transport
172 tcpHandler.initiateEventLoopGroups(null, true);
174 fail("Expected BindException or Errors.NativeIoException");
175 } catch (BindException | Errors.NativeIoException e) {
181 * Trigger the server shutdown and wait 2 seconds for completion.
183 private void shutdownServer() throws InterruptedException, ExecutionException {
184 ListenableFuture<Boolean> shutdownRet = tcpHandler.shutdown() ;
185 while (shutdownRet.isDone() != true) {
188 assertEquals("shutdown failed", true, shutdownRet.get());
191 private Boolean startupServer(final boolean isEpollEnabled) throws InterruptedException {
192 final var online = tcpHandler.getIsOnlineFuture();
194 * Test EPoll based native transport if isEpollEnabled is true.
195 * Else use Nio based transport.
197 tcpHandler.initiateEventLoopGroups(null, isEpollEnabled);
198 new Thread(tcpHandler).start();
200 while (online.isDone() != true && retry++ < 20) {
203 return online.isDone();
206 private static Boolean clientConnection(final int port) throws IOException {
207 // Connect, and disconnect
208 Socket socket = new Socket(InetAddress.getLoopbackAddress(), port);
209 Boolean result = socket.isConnected();