1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
40
41
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
55 useSSL = true;
56
57
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 }