UDP support implementation
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandlerTest.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 io.netty.channel.ChannelHandlerContext;
13
14 import java.io.IOException;
15 import java.net.BindException;
16 import java.net.InetAddress;
17 import java.net.InetSocketAddress;
18 import java.net.Socket;
19 import java.util.concurrent.ExecutionException;
20
21 import org.junit.Assert;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
26 import org.opendaylight.openflowjava.protocol.impl.deserialization.DeserializationFactory;
27 import org.opendaylight.openflowjava.protocol.impl.serialization.SerializationFactory;
28
29 import com.google.common.util.concurrent.ListenableFuture;
30
31 /**
32  *
33  * @author jameshall
34  */
35 public class TcpHandlerTest {
36
37     private InetAddress serverAddress = InetAddress.getLoopbackAddress() ;
38     @Mock ChannelHandlerContext mockChHndlrCtx ;
39     @Mock TcpChannelInitializer mockChannelInitializer;
40     @Mock SwitchConnectionHandler mockSwitchConnHndler ;
41     @Mock SerializationFactory mockSerializationFactory ;
42     @Mock DeserializationFactory mockDeserializationFactory ;
43
44     TcpHandler tcpHandler ;
45
46     /**
47      * Initialize mocks
48      */
49     public TcpHandlerTest() {
50         MockitoAnnotations.initMocks(this);
51     }
52
53     /**
54      * Test run with null address set
55      * @throws IOException
56      * @throws InterruptedException
57      * @throws ExecutionException
58      */
59     @Test
60     public void testRunWithNullAddress() throws IOException, InterruptedException, ExecutionException  {
61
62         tcpHandler = new TcpHandler(null, 0);
63         tcpHandler.setChannelInitializer(mockChannelInitializer);
64
65         assertEquals("failed to start server", true, startupServer()) ;
66         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
67         shutdownServer();
68     }
69
70     /**
71      * Test run with address set
72      * @throws IOException
73      * @throws InterruptedException
74      * @throws ExecutionException
75      */
76     @Test
77     public void testRunWithAddress() throws IOException, InterruptedException, ExecutionException  {
78
79         tcpHandler = new TcpHandler(serverAddress, 0);
80         tcpHandler.setChannelInitializer(mockChannelInitializer);
81
82         assertEquals("failed to start server", true, startupServer()) ;
83         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
84         shutdownServer();
85     }
86
87     /**
88      * Test run with encryption
89      * @throws InterruptedException
90      * @throws IOException
91      * @throws ExecutionException
92      */
93     @Test
94     public void testRunWithEncryption () throws InterruptedException, IOException, ExecutionException {
95         int serverPort = 28001;
96         tcpHandler = new TcpHandler(serverAddress, serverPort);
97         tcpHandler.setChannelInitializer(mockChannelInitializer);
98
99         assertEquals( "failed to start server", true, startupServer()) ;
100         assertEquals( "wrong connection count", 0, tcpHandler.getNumberOfConnections() );
101         assertEquals( "wrong port", serverPort, tcpHandler.getPort() );
102         assertEquals( "wrong address", serverAddress.getHostAddress(), tcpHandler.getAddress()) ;
103
104         assertEquals("failed to connect client", true, clientConnection(tcpHandler.getPort())) ;
105
106         shutdownServer();
107     }
108
109     /**
110      * Test run on already used port
111      * @throws IOException
112      */
113     @Test
114     public void testSocketAlreadyInUse() throws IOException {
115         int serverPort = 28001;
116         Socket firstBinder = new Socket();
117         boolean exceptionThrown = false;
118         try {
119             firstBinder.bind(new InetSocketAddress(serverAddress, serverPort));
120         } catch (Exception e) {
121             Assert.fail("Test precondition failed - not able to bind socket to port " + serverPort);
122         }
123         try {
124             tcpHandler = new TcpHandler(serverAddress, serverPort);
125             tcpHandler.setChannelInitializer(mockChannelInitializer);
126             tcpHandler.run();
127         } catch (Exception e) {
128             if (e instanceof BindException) {
129                 exceptionThrown = true;
130             }
131         }
132         firstBinder.close();
133         Assert.assertTrue("Expected BindException has not been thrown", exceptionThrown == true);
134     }
135
136     /**
137      * Trigger the server shutdown and wait 2 seconds for completion
138      */
139     private void shutdownServer() throws InterruptedException, ExecutionException {
140         ListenableFuture<Boolean> shutdownRet = tcpHandler.shutdown() ;
141         while ( shutdownRet.isDone() != true )
142             Thread.sleep(100) ;
143         assertEquals("shutdown failed", true, shutdownRet.get());
144     }
145
146     /**
147      * @throws InterruptedException
148      * @throws IOException
149      * @throws ExecutionException
150      */
151     private Boolean startupServer() throws InterruptedException, IOException, ExecutionException {
152         ListenableFuture<Boolean> online = tcpHandler.getIsOnlineFuture();
153
154             (new Thread(tcpHandler)).start();
155             int retry = 0;
156             while (online.isDone() != true && retry++ < 20) {
157                 Thread.sleep(100);
158             }
159         return online.isDone() ;
160     }
161     /**
162      * @throws IOException
163      */
164     private static Boolean clientConnection(int port) throws IOException {
165         // Connect, and disconnect
166         Socket socket = new Socket(InetAddress.getLoopbackAddress(), port );
167         Boolean result = socket.isConnected();
168         socket.close() ;
169         return result ;
170     }
171 }