1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.example.echoserver;
21  
22  import java.io.IOException;
23  import java.net.InetSocketAddress;
24  import java.net.SocketAddress;
25  import java.security.GeneralSecurityException;
26  
27  import junit.framework.TestCase;
28  
29  import org.apache.mina.core.buffer.IoBuffer;
30  import org.apache.mina.core.service.IoAcceptor;
31  import org.apache.mina.core.session.IoSession;
32  import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
33  import org.apache.mina.filter.ssl.SslFilter;
34  import org.apache.mina.transport.socket.DatagramSessionConfig;
35  import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
36  import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
37  import org.apache.mina.util.AvailablePortFinder;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  /**
42   * Tests echo server example.
43   *
44   * @author The Apache MINA Project (dev@mina.apache.org)
45   * @version $Rev:448075 $, $Date:2006-09-20 05:26:53Z $
46   */
47  public abstract class AbstractTest extends TestCase {
48      private final Logger logger = LoggerFactory.getLogger(getClass());
49  
50      protected boolean useSSL;
51  
52      protected int port;
53  
54      protected SocketAddress boundAddress;
55  
56      protected IoAcceptor datagramAcceptor;
57  
58      protected IoAcceptor socketAcceptor;
59  
60      protected AbstractTest() {
61      }
62  
63      protected static void assertEquals(byte[] expected, byte[] actual) {
64          assertEquals(toString(expected), toString(actual));
65      }
66  
67      protected static void assertEquals(IoBuffer expected, IoBuffer actual) {
68          assertEquals(toString(expected), toString(actual));
69      }
70  
71      protected static String toString(byte[] buf) {
72          StringBuilder str = new StringBuilder(buf.length * 4);
73          for (byte element : buf) {
74              str.append(element);
75              str.append(' ');
76          }
77          return str.toString();
78      }
79  
80      protected static String toString(IoBuffer buf) {
81          return buf.getHexDump();
82      }
83  
84      @Override
85      protected void setUp() throws Exception {
86          // Disable SSL by default
87          useSSL = false;
88  
89          boundAddress = null;
90          datagramAcceptor = new NioDatagramAcceptor();
91          socketAcceptor = new NioSocketAcceptor();
92  
93          ((DatagramSessionConfig) datagramAcceptor.getSessionConfig())
94                  .setReuseAddress(true);
95          ((NioSocketAcceptor) socketAcceptor).setReuseAddress(true);
96  
97          // Find an available test port and bind to it.
98          boolean socketBound = false;
99          boolean datagramBound = false;
100 
101         // Let's start from port #1 to detect possible resource leak
102         // because test will fail in port 1-1023 if user run this test
103         // as a normal user.
104 
105         SocketAddress address = null;
106 
107         // Find the first available port above 1024
108         port = AvailablePortFinder.getNextAvailable(1024);
109 
110         socketBound = false;
111         datagramBound = false;
112 
113         address = new InetSocketAddress(port);
114 
115         try {
116             socketAcceptor.setHandler(new EchoProtocolHandler() {
117                 @Override
118                 public void sessionCreated(IoSession session) {
119                     if (useSSL) {
120                         try {
121                             session.getFilterChain().addFirst(
122                                     "SSL",
123                                     new SslFilter(BogusSslContextFactory
124                                             .getInstance(true)));
125                         } catch (GeneralSecurityException e) {
126                             logger.error("", e);
127                             throw new RuntimeException(e);
128                         }
129                     }
130                 }
131 
132                 // This is for TLS re-entrance test
133                 @Override
134                 public void messageReceived(IoSession session, Object message)
135                         throws Exception {
136                     if (!(message instanceof IoBuffer)) {
137                         return;
138                     }
139 
140                     IoBuffer buf = (IoBuffer) message;
141                     if (session.getFilterChain().contains("SSL")
142                             && buf.remaining() == 1 && buf.get() == (byte) '.') {
143                         logger.info("TLS Reentrance");
144                         ((SslFilter) session.getFilterChain().get("SSL"))
145                                 .startSsl(session);
146 
147                         // Send a response
148                         buf = IoBuffer.allocate(1);
149                         buf.put((byte) '.');
150                         buf.flip();
151                         session.setAttribute(SslFilter.DISABLE_ENCRYPTION_ONCE);
152                         session.write(buf);
153                     } else {
154                         super.messageReceived(session, message);
155                     }
156                 }
157             });
158 
159             socketAcceptor.bind(address);
160             socketBound = true;
161 
162             datagramAcceptor.setHandler(new EchoProtocolHandler());
163             datagramAcceptor.bind(address);
164             datagramBound = true;
165         } catch (IOException e) {
166             // Do nothing
167         } finally {
168             if (socketBound && !datagramBound) {
169                 socketAcceptor.unbind();
170             }
171             if (datagramBound && !socketBound) {
172                 datagramAcceptor.unbind();
173             }
174         }
175 
176         // If there is no port available, test fails.
177         if (!socketBound || !datagramBound) {
178             throw new IOException("Cannot bind any test port.");
179         }
180 
181         boundAddress = address;
182         logger.info("Using port " + port + " for testing.");
183     }
184 
185     @Override
186     protected void tearDown() throws Exception {
187         if (boundAddress != null) {
188             socketAcceptor.dispose();
189             datagramAcceptor.dispose();
190         }
191     }
192 }