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