View Javadoc
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 static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.fail;
25  
26  import java.net.DatagramPacket;
27  import java.net.DatagramSocket;
28  import java.net.InetAddress;
29  import java.net.InetSocketAddress;
30  import java.net.Socket;
31  import java.net.SocketTimeoutException;
32  import java.util.Arrays;
33  
34  import org.apache.mina.example.echoserver.ssl.SslServerSocketFactory;
35  import org.apache.mina.example.echoserver.ssl.SslSocketFactory;
36  import org.junit.Test;
37  
38  /**
39   * Tests echo server example.
40   *
41   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
42   */
43  public class AcceptorTest extends AbstractTest {
44      public AcceptorTest() {
45      }
46  
47      @Test
48      public void testTCP() throws Exception {
49          testTCP0(new Socket(InetAddress.getByName(null), port));
50      }
51  
52      @Test
53      public void testTCPWithSSL() throws Exception {
54          // Add an SSL filter
55          useSSL = true;
56  
57          // Create a echo client with SSL factory and test it.
58          SslSocketFactory.setSslEnabled(true);
59          SslServerSocketFactory.setSslEnabled(true);
60          testTCP0(SslSocketFactory.getSocketFactory().createSocket(
61                  "localhost", port));
62      }
63  
64      private void testTCP0(Socket client) throws Exception {
65          client.setSoTimeout(300000);
66          byte[] writeBuf = new byte[16];
67  
68          for (int i = 0; i < 10; i++) {
69              fillWriteBuffer(writeBuf, i);
70              client.getOutputStream().write(writeBuf);
71          }
72  
73          byte[] readBuf = new byte[writeBuf.length];
74  
75          for (int i = 0; i < 10; i++) {
76              fillWriteBuffer(writeBuf, i);
77  
78              int readBytes = 0;
79              while (readBytes < readBuf.length) {
80                  int nBytes = client.getInputStream().read(readBuf, readBytes,
81                          readBuf.length - readBytes);
82  
83                  if (nBytes < 0) {
84                      fail("Unexpected disconnection.");
85                  }
86  
87                  readBytes += nBytes;
88              }
89  
90              assertTrue(Arrays.equals( writeBuf, readBuf));
91          }
92  
93          client.setSoTimeout(500);
94  
95          try {
96              client.getInputStream().read();
97              fail("Unexpected incoming data.");
98          } catch (SocketTimeoutException e) {
99          }
100 
101         client.getInputStream().close();
102         client.close();
103     }
104 
105     public void testUDP() throws Exception {
106         DatagramSocket client = new DatagramSocket();
107         client.connect(new InetSocketAddress(InetAddress.getByName(null), port));
108         client.setSoTimeout(500);
109 
110         byte[] writeBuf = new byte[16];
111         byte[] readBuf = new byte[writeBuf.length];
112         DatagramPacket wp = new DatagramPacket(writeBuf, writeBuf.length);
113         DatagramPacket rp = new DatagramPacket(readBuf, readBuf.length);
114 
115         for (int i = 0; i < 10; i++) {
116             fillWriteBuffer(writeBuf, i);
117             client.send(wp);
118 
119             client.receive(rp);
120             assertEquals(writeBuf.length, rp.getLength());
121             assertTrue(Arrays.equals(writeBuf, readBuf));
122         }
123 
124         try {
125             client.receive(rp);
126             fail("Unexpected incoming data.");
127         } catch (SocketTimeoutException e) {
128         }
129 
130         client.close();
131     }
132 
133     private void fillWriteBuffer(byte[] writeBuf, int i) {
134         for (int j = writeBuf.length - 1; j >= 0; j--) {
135             writeBuf[j] = (byte) (j + i);
136         }
137     }
138 }