1 | /* |
2 | * @(#) $Id: AcceptorTest.java 357871 2005-12-20 01:56:40Z trustin $ |
3 | * |
4 | * Copyright 2004 The Apache Software Foundation |
5 | * |
6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
7 | * you may not use this file except in compliance with the License. |
8 | * 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, software |
13 | * distributed under the License is distributed on an "AS IS" BASIS, |
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15 | * See the License for the specific language governing permissions and |
16 | * limitations under the License. |
17 | * |
18 | */ |
19 | package org.apache.mina.examples.echoserver; |
20 | |
21 | import java.io.IOException; |
22 | import java.net.InetAddress; |
23 | import java.net.ServerSocket; |
24 | import java.net.Socket; |
25 | import java.net.SocketTimeoutException; |
26 | import java.net.UnknownHostException; |
27 | |
28 | import javax.net.ServerSocketFactory; |
29 | import javax.net.SocketFactory; |
30 | |
31 | import org.apache.commons.net.EchoTCPClient; |
32 | import org.apache.commons.net.EchoUDPClient; |
33 | import org.apache.mina.common.TransportType; |
34 | import org.apache.mina.examples.echoserver.ssl.BogusSSLContextFactory; |
35 | import org.apache.mina.examples.echoserver.ssl.SSLServerSocketFactory; |
36 | import org.apache.mina.examples.echoserver.ssl.SSLSocketFactory; |
37 | import org.apache.mina.io.IoAcceptor; |
38 | import org.apache.mina.io.filter.SSLFilter; |
39 | |
40 | /** |
41 | * Tests echo server example. |
42 | * |
43 | * @author The Apache Directory Project (dev@directory.apache.org) |
44 | * @version $Rev: 357871 $, $Date: 2005-12-20 10:56:40 +0900 (Tue, 20 Dec 2005) $ |
45 | */ |
46 | public class AcceptorTest extends AbstractTest |
47 | { |
48 | public AcceptorTest() |
49 | { |
50 | } |
51 | |
52 | public void testTCP() throws Exception |
53 | { |
54 | EchoTCPClient client = new EchoTCPClient(); |
55 | testTCP0( client ); |
56 | } |
57 | |
58 | public void testTCPWithSSL() throws Exception |
59 | { |
60 | // Add an SSL filter |
61 | SSLFilter sslFilter = |
62 | new SSLFilter( BogusSSLContextFactory.getInstance( true ) ); |
63 | IoAcceptor acceptor = registry.getIoAcceptor( TransportType.SOCKET ); |
64 | acceptor.getFilterChain().addLast( "SSL", sslFilter ); |
65 | |
66 | // Create a commons-net socket factory |
67 | SSLSocketFactory.setSslEnabled(true); |
68 | SSLServerSocketFactory.setSslEnabled(true); |
69 | org.apache.commons.net.SocketFactory factory = new org.apache.commons.net.SocketFactory() { |
70 | |
71 | private SocketFactory f = SSLSocketFactory.getSocketFactory(); |
72 | private ServerSocketFactory ssf = SSLServerSocketFactory.getServerSocketFactory(); |
73 | |
74 | public Socket createSocket( String arg0, int arg1 ) throws UnknownHostException, IOException |
75 | { |
76 | return f.createSocket(arg0, arg1); |
77 | } |
78 | |
79 | public Socket createSocket( InetAddress arg0, int arg1 ) throws IOException |
80 | { |
81 | return f.createSocket(arg0, arg1); |
82 | } |
83 | |
84 | public Socket createSocket( String arg0, int arg1, InetAddress arg2, int arg3 ) throws UnknownHostException, IOException |
85 | { |
86 | return f.createSocket(arg0, arg1, arg2, arg3); |
87 | } |
88 | |
89 | public Socket createSocket( InetAddress arg0, int arg1, InetAddress arg2, int arg3 ) throws IOException |
90 | { |
91 | return f.createSocket(arg0, arg1, arg2, arg3); |
92 | } |
93 | |
94 | public ServerSocket createServerSocket( int arg0 ) throws IOException |
95 | { |
96 | return ssf.createServerSocket(arg0); |
97 | } |
98 | |
99 | public ServerSocket createServerSocket( int arg0, int arg1 ) throws IOException |
100 | { |
101 | return ssf.createServerSocket(arg0, arg1); |
102 | } |
103 | |
104 | public ServerSocket createServerSocket( int arg0, int arg1, InetAddress arg2 ) throws IOException |
105 | { |
106 | return ssf.createServerSocket(arg0, arg1, arg2); |
107 | } |
108 | |
109 | }; |
110 | |
111 | // Create a echo client with SSL factory and test it. |
112 | EchoTCPClient client = new EchoTCPClient(); |
113 | client.setSocketFactory( factory ); |
114 | testTCP0( client ); |
115 | } |
116 | |
117 | private void testTCP0( EchoTCPClient client ) throws Exception |
118 | { |
119 | client.connect( InetAddress.getLocalHost(), port ); |
120 | byte[] writeBuf = new byte[ 16 ]; |
121 | |
122 | for( int i = 0; i < 10; i ++ ) |
123 | { |
124 | fillWriteBuffer( writeBuf, i ); |
125 | client.getOutputStream().write( writeBuf ); |
126 | } |
127 | |
128 | client.setSoTimeout( 30000 ); |
129 | |
130 | byte[] readBuf = new byte[ writeBuf.length ]; |
131 | |
132 | for( int i = 0; i < 10; i ++ ) |
133 | { |
134 | fillWriteBuffer( writeBuf, i ); |
135 | |
136 | int readBytes = 0; |
137 | while( readBytes < readBuf.length ) |
138 | { |
139 | int nBytes = client.getInputStream().read( readBuf, |
140 | readBytes, readBuf.length - readBytes ); |
141 | |
142 | if( nBytes < 0 ) |
143 | fail( "Unexpected disconnection." ); |
144 | |
145 | readBytes += nBytes; |
146 | } |
147 | |
148 | assertEquals( writeBuf, readBuf ); |
149 | } |
150 | |
151 | client.setSoTimeout( 500 ); |
152 | |
153 | try |
154 | { |
155 | client.getInputStream().read(); |
156 | fail( "Unexpected incoming data." ); |
157 | } |
158 | catch( SocketTimeoutException e ) |
159 | { |
160 | } |
161 | |
162 | client.disconnect(); |
163 | } |
164 | |
165 | public void testUDP() throws Exception |
166 | { |
167 | EchoUDPClient client = new EchoUDPClient(); |
168 | client.open(); |
169 | client.setSoTimeout( 3000 ); |
170 | |
171 | byte[] writeBuf = new byte[ 16 ]; |
172 | byte[] readBuf = new byte[ writeBuf.length ]; |
173 | |
174 | client.setSoTimeout( 500 ); |
175 | |
176 | for( int i = 0; i < 10; i ++ ) |
177 | { |
178 | fillWriteBuffer( writeBuf, i ); |
179 | client.send( writeBuf, writeBuf.length, InetAddress |
180 | .getLocalHost(), port ); |
181 | |
182 | assertEquals( readBuf.length, client.receive( readBuf, |
183 | readBuf.length ) ); |
184 | assertEquals( writeBuf, readBuf ); |
185 | } |
186 | |
187 | try |
188 | { |
189 | client.receive( readBuf ); |
190 | fail( "Unexpected incoming data." ); |
191 | } |
192 | catch( SocketTimeoutException e ) |
193 | { |
194 | } |
195 | |
196 | client.close(); |
197 | } |
198 | |
199 | private void fillWriteBuffer( byte[] writeBuf, int i ) |
200 | { |
201 | for( int j = writeBuf.length - 1; j >= 0; j -- ) |
202 | { |
203 | writeBuf[ j ] = ( byte ) ( j + i ); |
204 | } |
205 | } |
206 | |
207 | public static void main( String[] args ) |
208 | { |
209 | junit.textui.TestRunner.run( AcceptorTest.class ); |
210 | } |
211 | } |