001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *
019: */
020: package org.apache.mina.example.echoserver;
021:
022: import java.io.IOException;
023: import java.net.InetSocketAddress;
024: import java.net.SocketAddress;
025:
026: import junit.framework.TestCase;
027:
028: import org.apache.mina.common.IoAcceptor;
029: import org.apache.mina.common.IoBuffer;
030: import org.apache.mina.common.IoSession;
031: import org.apache.mina.example.echoserver.ssl.BogusSslContextFactory;
032: import org.apache.mina.filter.ssl.SslFilter;
033: import org.apache.mina.transport.socket.DatagramSessionConfig;
034: import org.apache.mina.transport.socket.nio.NioDatagramAcceptor;
035: import org.apache.mina.transport.socket.nio.NioSocketAcceptor;
036: import org.slf4j.Logger;
037: import org.slf4j.LoggerFactory;
038:
039: /**
040: * Tests echo server example.
041: *
042: * @author The Apache MINA Project (dev@mina.apache.org)
043: * @version $Rev:448075 $, $Date:2006-09-20 05:26:53Z $
044: */
045: public abstract class AbstractTest extends TestCase {
046: private final Logger logger = LoggerFactory.getLogger(getClass());
047:
048: protected boolean useSSL;
049:
050: protected int port;
051:
052: protected SocketAddress boundAddress;
053:
054: protected IoAcceptor datagramAcceptor;
055:
056: protected IoAcceptor socketAcceptor;
057:
058: protected AbstractTest() {
059: }
060:
061: protected static void assertEquals(byte[] expected, byte[] actual) {
062: assertEquals(toString(expected), toString(actual));
063: }
064:
065: protected static void assertEquals(IoBuffer expected,
066: IoBuffer actual) {
067: assertEquals(toString(expected), toString(actual));
068: }
069:
070: protected static String toString(byte[] buf) {
071: StringBuffer str = new StringBuffer(buf.length * 4);
072: for (byte element : buf) {
073: str.append(element);
074: str.append(' ');
075: }
076: return str.toString();
077: }
078:
079: protected static String toString(IoBuffer buf) {
080: return buf.getHexDump();
081: }
082:
083: @Override
084: protected void setUp() throws Exception {
085: // Disable SSL by default
086: useSSL = false;
087: final SslFilter sslFilter = new SslFilter(
088: BogusSslContextFactory.getInstance(true));
089:
090: boundAddress = null;
091: datagramAcceptor = new NioDatagramAcceptor();
092: socketAcceptor = new NioSocketAcceptor();
093:
094: ((DatagramSessionConfig) datagramAcceptor.getSessionConfig())
095: .setReuseAddress(true);
096: ((NioSocketAcceptor) socketAcceptor).setReuseAddress(true);
097:
098: // Find an availble test port and bind to it.
099: boolean socketBound = false;
100: boolean datagramBound = false;
101:
102: // Let's start from port #1 to detect possible resource leak
103: // because test will fail in port 1-1023 if user run this test
104: // as a normal user.
105:
106: SocketAddress address = null;
107:
108: for (port = 1; port <= 65535; port++) {
109: socketBound = false;
110: datagramBound = false;
111:
112: address = new InetSocketAddress(port);
113:
114: try {
115: socketAcceptor.setHandler(new EchoProtocolHandler() {
116: @Override
117: public void sessionCreated(IoSession session) {
118: if (useSSL) {
119: session.getFilterChain().addFirst("SSL",
120: sslFilter);
121: }
122: }
123:
124: // This is for TLS reentrance test
125: @Override
126: public void messageReceived(IoSession session,
127: Object message) throws Exception {
128: if (!(message instanceof IoBuffer)) {
129: return;
130: }
131:
132: IoBuffer buf = (IoBuffer) message;
133: if (session.getFilterChain().contains("SSL")
134: && buf.remaining() == 1
135: && buf.get() == (byte) '.') {
136: logger.info("TLS Reentrance");
137: ((SslFilter) session.getFilterChain().get(
138: "SSL")).startSsl(session);
139:
140: // Send a response
141: buf = IoBuffer.allocate(1);
142: buf.put((byte) '.');
143: buf.flip();
144: session
145: .setAttribute(SslFilter.DISABLE_ENCRYPTION_ONCE);
146: session.write(buf);
147: } else {
148: super .messageReceived(session, message);
149: }
150: }
151: });
152: socketAcceptor.bind(address);
153: socketBound = true;
154:
155: datagramAcceptor.setHandler(new EchoProtocolHandler());
156: datagramAcceptor.bind(address);
157: datagramBound = true;
158:
159: break;
160: } catch (IOException e) {
161: } finally {
162: if (socketBound && !datagramBound) {
163: socketAcceptor.unbind();
164: }
165: if (datagramBound && !socketBound) {
166: datagramAcceptor.unbind();
167: }
168: }
169: }
170:
171: // If there is no port available, test fails.
172: if (!socketBound || !datagramBound) {
173: throw new IOException("Cannot bind any test port.");
174: }
175:
176: boundAddress = address;
177: System.out.println("Using port " + port + " for testing.");
178: }
179:
180: @Override
181: protected void tearDown() throws Exception {
182: if (boundAddress != null) {
183: socketAcceptor.dispose();
184: datagramAcceptor.dispose();
185: }
186: }
187: }
|