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.net.DatagramPacket;
023: import java.net.DatagramSocket;
024: import java.net.InetSocketAddress;
025: import java.net.Socket;
026: import java.net.SocketTimeoutException;
027:
028: import org.apache.mina.example.echoserver.ssl.SslServerSocketFactory;
029: import org.apache.mina.example.echoserver.ssl.SslSocketFactory;
030:
031: /**
032: * Tests echo server example.
033: *
034: * @author The Apache MINA Project (dev@mina.apache.org)
035: * @version $Rev: 594505 $, $Date: 2007-11-13 05:17:27 -0700 (Tue, 13 Nov 2007) $
036: */
037: public class AcceptorTest extends AbstractTest {
038: public AcceptorTest() {
039: }
040:
041: public void testTCP() throws Exception {
042: testTCP0(new Socket("127.0.0.1", port));
043: }
044:
045: public void testTCPWithSSL() throws Exception {
046: // Add an SSL filter
047: useSSL = true;
048:
049: // Create a echo client with SSL factory and test it.
050: SslSocketFactory.setSslEnabled(true);
051: SslServerSocketFactory.setSslEnabled(true);
052: testTCP0(SslSocketFactory.getSocketFactory().createSocket(
053: "localhost", port));
054: }
055:
056: private void testTCP0(Socket client) throws Exception {
057: client.setSoTimeout(3000);
058: byte[] writeBuf = new byte[16];
059:
060: for (int i = 0; i < 10; i++) {
061: fillWriteBuffer(writeBuf, i);
062: client.getOutputStream().write(writeBuf);
063: }
064:
065: byte[] readBuf = new byte[writeBuf.length];
066:
067: for (int i = 0; i < 10; i++) {
068: fillWriteBuffer(writeBuf, i);
069:
070: int readBytes = 0;
071: while (readBytes < readBuf.length) {
072: int nBytes = client.getInputStream().read(readBuf,
073: readBytes, readBuf.length - readBytes);
074:
075: if (nBytes < 0) {
076: fail("Unexpected disconnection.");
077: }
078:
079: readBytes += nBytes;
080: }
081:
082: assertEquals(writeBuf, readBuf);
083: }
084:
085: client.setSoTimeout(500);
086:
087: try {
088: client.getInputStream().read();
089: fail("Unexpected incoming data.");
090: } catch (SocketTimeoutException e) {
091: }
092:
093: client.getInputStream().close();
094: client.close();
095: }
096:
097: public void testUDP() throws Exception {
098: DatagramSocket client = new DatagramSocket();
099: client.connect(new InetSocketAddress("127.0.0.1", port));
100: client.setSoTimeout(500);
101:
102: byte[] writeBuf = new byte[16];
103: byte[] readBuf = new byte[writeBuf.length];
104: DatagramPacket wp = new DatagramPacket(writeBuf,
105: writeBuf.length);
106: DatagramPacket rp = new DatagramPacket(readBuf, readBuf.length);
107:
108: for (int i = 0; i < 10; i++) {
109: fillWriteBuffer(writeBuf, i);
110: client.send(wp);
111:
112: client.receive(rp);
113: assertEquals(writeBuf.length, rp.getLength());
114: assertEquals(writeBuf, readBuf);
115: }
116:
117: try {
118: client.receive(rp);
119: fail("Unexpected incoming data.");
120: } catch (SocketTimeoutException e) {
121: }
122:
123: client.close();
124: }
125:
126: private void fillWriteBuffer(byte[] writeBuf, int i) {
127: for (int j = writeBuf.length - 1; j >= 0; j--) {
128: writeBuf[j] = (byte) (j + i);
129: }
130: }
131:
132: public static void main(String[] args) {
133: junit.textui.TestRunner.run(AcceptorTest.class);
134: }
135: }
|