1 20 package org.apache.mina.example.echoserver; 21 22 import java.io.IOException ; 23 import java.net.InetAddress ; 24 import java.net.ServerSocket ; 25 import java.net.Socket ; 26 import java.net.SocketTimeoutException ; 27 import java.net.UnknownHostException ; 28 29 import javax.net.ServerSocketFactory; 30 import javax.net.SocketFactory; 31 32 import org.apache.commons.net.EchoTCPClient; 33 import org.apache.commons.net.EchoUDPClient; 34 import org.apache.mina.example.echoserver.ssl.SSLServerSocketFactory; 35 import org.apache.mina.example.echoserver.ssl.SSLSocketFactory; 36 37 43 public class AcceptorTest extends AbstractTest { 44 public AcceptorTest() { 45 } 46 47 public void testTCP() throws Exception { 48 EchoTCPClient client = new EchoTCPClient(); 49 testTCP0(client); 50 } 51 52 public void testTCPWithSSL() throws Exception { 53 useSSL = true; 55 56 SSLSocketFactory.setSslEnabled(true); 58 SSLServerSocketFactory.setSslEnabled(true); 59 org.apache.commons.net.SocketFactory factory = new org.apache.commons.net.SocketFactory() { 60 61 private SocketFactory f = SSLSocketFactory.getSocketFactory(); 62 63 private ServerSocketFactory ssf = SSLServerSocketFactory 64 .getServerSocketFactory(); 65 66 public Socket createSocket(String arg0, int arg1) 67 throws UnknownHostException , IOException { 68 return f.createSocket(arg0, arg1); 69 } 70 71 public Socket createSocket(InetAddress arg0, int arg1) 72 throws IOException { 73 return f.createSocket(arg0, arg1); 74 } 75 76 public Socket createSocket(String arg0, int arg1, InetAddress arg2, 77 int arg3) throws UnknownHostException , IOException { 78 return f.createSocket(arg0, arg1, arg2, arg3); 79 } 80 81 public Socket createSocket(InetAddress arg0, int arg1, 82 InetAddress arg2, int arg3) throws IOException { 83 return f.createSocket(arg0, arg1, arg2, arg3); 84 } 85 86 public ServerSocket createServerSocket(int arg0) throws IOException { 87 return ssf.createServerSocket(arg0); 88 } 89 90 public ServerSocket createServerSocket(int arg0, int arg1) 91 throws IOException { 92 return ssf.createServerSocket(arg0, arg1); 93 } 94 95 public ServerSocket createServerSocket(int arg0, int arg1, 96 InetAddress arg2) throws IOException { 97 return ssf.createServerSocket(arg0, arg1, arg2); 98 } 99 100 }; 101 102 EchoTCPClient client = new EchoTCPClient(); 104 client.setSocketFactory(factory); 105 testTCP0(client); 106 } 107 108 private void testTCP0(EchoTCPClient client) throws Exception { 109 client.connect("localhost", port); 110 byte[] writeBuf = new byte[16]; 111 112 for (int i = 0; i < 10; i++) { 113 fillWriteBuffer(writeBuf, i); 114 client.getOutputStream().write(writeBuf); 115 } 116 117 client.setSoTimeout(30000); 118 119 byte[] readBuf = new byte[writeBuf.length]; 120 121 for (int i = 0; i < 10; i++) { 122 fillWriteBuffer(writeBuf, i); 123 124 int readBytes = 0; 125 while (readBytes < readBuf.length) { 126 int nBytes = client.getInputStream().read(readBuf, readBytes, 127 readBuf.length - readBytes); 128 129 if (nBytes < 0) 130 fail("Unexpected disconnection."); 131 132 readBytes += nBytes; 133 } 134 135 assertEquals(writeBuf, readBuf); 136 } 137 138 client.setSoTimeout(500); 139 140 try { 141 client.getInputStream().read(); 142 fail("Unexpected incoming data."); 143 } catch (SocketTimeoutException e) { 144 } 145 146 client.disconnect(); 147 } 148 149 public void testUDP() throws Exception { 150 EchoUDPClient client = new EchoUDPClient(); 151 client.open(); 152 client.setSoTimeout(3000); 153 154 byte[] writeBuf = new byte[16]; 155 byte[] readBuf = new byte[writeBuf.length]; 156 157 client.setSoTimeout(500); 158 159 for (int i = 0; i < 10; i++) { 160 fillWriteBuffer(writeBuf, i); 161 client.send(writeBuf, writeBuf.length, InetAddress.getByName(null), 162 port); 163 164 assertEquals(readBuf.length, client 165 .receive(readBuf, readBuf.length)); 166 assertEquals(writeBuf, readBuf); 167 } 168 169 try { 170 client.receive(readBuf); 171 fail("Unexpected incoming data."); 172 } catch (SocketTimeoutException e) { 173 } 174 175 client.close(); 176 } 177 178 private void fillWriteBuffer(byte[] writeBuf, int i) { 179 for (int j = writeBuf.length - 1; j >= 0; j--) { 180 writeBuf[j] = (byte) (j + i); 181 } 182 } 183 184 public static void main(String [] args) { 185 junit.textui.TestRunner.run(AcceptorTest.class); 186 } 187 } 188 | Popular Tags |