OldServerSocketTest.java revision 04e25f30524dc7b51f280912d6ca97d991ae84b0
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package libcore.java.net; 19 20import java.io.IOException; 21import java.io.InputStream; 22import java.io.OutputStream; 23import java.net.InetAddress; 24import java.net.InetSocketAddress; 25import java.net.ServerSocket; 26import java.net.Socket; 27import java.net.SocketAddress; 28import java.net.SocketException; 29import java.net.SocketImpl; 30import java.net.SocketImplFactory; 31import java.net.SocketTimeoutException; 32import java.nio.channels.IllegalBlockingModeException; 33import java.nio.channels.ServerSocketChannel; 34import java.security.Permission; 35import java.util.Properties; 36import tests.support.Support_PortManager; 37 38public class OldServerSocketTest extends OldSocketTestCase { 39 40 boolean isCreateCalled = false; 41 ServerSocket s; 42 Socket sconn; 43 Thread t; 44 45 public void test_setPerformancePreference_Int_Int_Int() throws Exception { 46 performancePreferenceTest(1, 0, 0); 47 performancePreferenceTest(1, 1, 1); 48 performancePreferenceTest(0, 1, 2); 49 performancePreferenceTest(Integer.MAX_VALUE, Integer.MAX_VALUE, 50 Integer.MAX_VALUE); 51 } 52 53 void performancePreferenceTest(int connectionTime, int latency, 54 int bandwidth) throws Exception { 55 ServerSocket theSocket = new ServerSocket(); 56 theSocket.setPerformancePreferences(connectionTime, latency, bandwidth); 57 58 InetSocketAddress theAddress = new InetSocketAddress(InetAddress 59 .getLocalHost(), 0); 60 theSocket.bind(theAddress); 61 int portNumber = theSocket.getLocalPort(); 62 assertTrue( 63 "Returned incorrect InetSocketAddress(2):" 64 + theSocket.getLocalSocketAddress().toString() 65 + "Expected: " 66 + (new InetSocketAddress(InetAddress.getLocalHost(), 67 portNumber)).toString(), theSocket 68 .getLocalSocketAddress().equals( 69 new InetSocketAddress(InetAddress 70 .getLocalHost(), portNumber))); 71 assertTrue("Server socket not bound when it should be:", theSocket 72 .isBound()); 73 74 // now make sure that it is actually bound and listening on the 75 // address we provided 76 Socket clientSocket = new Socket(); 77 InetSocketAddress clAddress = new InetSocketAddress(InetAddress 78 .getLocalHost(), portNumber); 79 clientSocket.connect(clAddress); 80 Socket servSock = theSocket.accept(); 81 82 assertEquals(clAddress, clientSocket.getRemoteSocketAddress()); 83 theSocket.close(); 84 servSock.close(); 85 clientSocket.close(); 86 } 87 88 public void test_ConstructorII() throws IOException { 89 int freePortNumber = Support_PortManager.getNextPort(); 90 s = new ServerSocket(freePortNumber, 1); 91 s.setSoTimeout(2000); 92 startClient(freePortNumber); 93 sconn = s.accept(); 94 sconn.close(); 95 s.close(); 96 } 97 98 static class SSClient implements Runnable { 99 Socket cs; 100 101 int port; 102 103 public SSClient(int prt) { 104 port = prt; 105 } 106 107 public void run() { 108 try { 109 // Go to sleep so the server can setup and wait for connection 110 Thread.sleep(1000); 111 cs = new Socket(InetAddress.getLocalHost().getHostName(), port); 112 // Sleep again to allow server side processing. Thread is 113 // stopped by server. 114 Thread.sleep(10000); 115 } catch (InterruptedException e) { 116 return; 117 } catch (Throwable e) { 118 System.out.println("Error establishing client: " + e.toString()); 119 } finally { 120 try { 121 if (cs != null) 122 cs.close(); 123 } catch (Exception e) { 124 } 125 } 126 } 127 } 128 129 public void test_Constructor() throws IOException { 130 ServerSocket ss = new ServerSocket(); 131 assertEquals(-1, ss.getLocalPort()); 132 ss.close(); 133 } 134 135 public void test_ConstructorI() throws Exception { 136 int portNumber = Support_PortManager.getNextPort(); 137 s = new ServerSocket(portNumber); 138 try { 139 new ServerSocket(portNumber); 140 fail("IOException was not thrown."); 141 } catch(IOException ioe) { 142 //expected 143 } 144 try { 145 startClient(s.getLocalPort()); 146 sconn = s.accept(); 147 assertNotNull("Was unable to accept connection", sconn); 148 sconn.close(); 149 } finally { 150 s.close(); 151 } 152 153 s = new ServerSocket(0); 154 try { 155 startClient(s.getLocalPort()); 156 sconn = s.accept(); 157 assertNotNull("Was unable to accept connection", sconn); 158 sconn.close(); 159 } finally { 160 s.close(); 161 } 162 } 163 164 public void test_ConstructorIILjava_net_InetAddress() throws IOException { 165 int freePortNumber = Support_PortManager.getNextPort(); 166 167 ServerSocket ss = new ServerSocket(freePortNumber, 10, InetAddress.getLocalHost()); 168 try { 169 new ServerSocket(freePortNumber, 10, InetAddress.getLocalHost()); 170 fail("IOException was not thrown."); 171 } catch(IOException expected) { 172 } 173 ss.close(); 174 175 try { 176 new ServerSocket(65536, 10, InetAddress.getLocalHost()); 177 fail("IllegalArgumentException was not thrown."); 178 } catch(IllegalArgumentException expected) { 179 } 180 } 181 182 public void test_LocalPort() throws IOException { 183 ServerSocket ss1 = new ServerSocket(4242); 184 assertEquals(ss1.getLocalPort(), 4242); 185 ss1.close(); 186 187 ServerSocket ss2 = new ServerSocket(); 188 ss2.bind(new InetSocketAddress("127.0.0.1", 4343)); 189 assertEquals(ss2.getLocalPort(), 4343); 190 ss2.close(); 191 192 ServerSocket ss3 = new ServerSocket(0); 193 assertTrue(ss3.getLocalPort() != 0); 194 ss3.close(); 195 } 196 197 class MockSocketFactory implements SocketImplFactory { 198 public SocketImpl createSocketImpl() { 199 return new MockSocketImpl(); 200 } 201 } 202 203 public void test_ConstructorI_SocksSet() throws IOException { 204 // Harmony-623 regression test 205 ServerSocket ss = null; 206 Properties props = (Properties) System.getProperties().clone(); 207 try { 208 System.setProperty("socksProxyHost", "127.0.0.1"); 209 System.setProperty("socksProxyPort", "12345"); 210 ss = new ServerSocket(0); 211 } finally { 212 System.setProperties(props); 213 if (null != ss) { 214 ss.close(); 215 } 216 } 217 } 218 219 public void test_accept() throws IOException { 220 int portNumber = Support_PortManager.getNextPort(); 221 222 ServerSocket newSocket = new ServerSocket(portNumber); 223 newSocket.setSoTimeout(500); 224 try { 225 Socket accepted = newSocket.accept(); 226 fail("SocketTimeoutException was not thrown: " + accepted); 227 } catch(SocketTimeoutException expected) { 228 } 229 newSocket.close(); 230 231 ServerSocketChannel ssc = ServerSocketChannel.open(); 232 ServerSocket ss = ssc.socket(); 233 234 try { 235 ss.accept(); 236 fail("IllegalBlockingModeException was not thrown."); 237 } catch(IllegalBlockingModeException ibme) { 238 //expected 239 } finally { 240 ss.close(); 241 ssc.close(); 242 } 243 } 244 245 public void test_getSoTimeout_setSoTimeout() throws Exception { 246 // TODO: a useful test would check that setSoTimeout actually causes timeouts! 247 ServerSocket s = new ServerSocket(); 248 s.setSoTimeout(1500); 249 int ms = s.getSoTimeout(); 250 if (ms < 1500-10 || ms > 1500+10) { 251 fail("suspicious timeout: " + ms); 252 } 253 s.close(); 254 try { 255 s.getSoTimeout(); 256 fail("SocketException was not thrown."); 257 } catch (SocketException expected) { 258 } 259 try { 260 s.setSoTimeout(1000); 261 fail("SocketException was not thrown."); 262 } catch (SocketException expected) { 263 } 264 } 265 266 public void test_toString() throws Exception { 267 s = new ServerSocket(0); 268 int portNumber = s.getLocalPort(); 269 assertTrue(s.toString().contains("" + portNumber)); 270 s.close(); 271 } 272 273 public void test_setReuseAddressZ() throws IOException { 274 ServerSocket newSocket = new ServerSocket(); 275 newSocket.close(); 276 try { 277 newSocket.setReuseAddress(true); 278 fail("SocketException was not thrown."); 279 } catch(SocketException expected) { 280 } 281 } 282 283 public void test_getReuseAddress() throws IOException { 284 ServerSocket newSocket = new ServerSocket(); 285 newSocket.close(); 286 try { 287 newSocket.getReuseAddress(); 288 fail("SocketException was not thrown."); 289 } catch(SocketException e) { 290 //expected 291 } 292 } 293 294 public void test_setReceiveBufferSizeI() throws IOException { 295 ServerSocket newSocket = new ServerSocket(); 296 newSocket.close(); 297 try { 298 newSocket.setReceiveBufferSize(10); 299 fail("SocketException was not thrown."); 300 } catch(SocketException se) { 301 //expected 302 } 303 } 304 305 public void test_getReceiveBufferSize() throws IOException { 306 ServerSocket newSocket = new ServerSocket(); 307 newSocket.close(); 308 try { 309 newSocket.getReceiveBufferSize(); 310 fail("SocketException was not thrown."); 311 } catch (SocketException e) { 312 //expected 313 } 314 } 315 316 protected void tearDown() { 317 try { 318 if (s != null) 319 s.close(); 320 if (sconn != null) 321 sconn.close(); 322 if (t != null) 323 t.interrupt(); 324 } catch (Exception e) { 325 } 326 } 327 328 /** 329 * Sets up the fixture, for example, open a network connection. This method 330 * is called before a test is executed. 331 */ 332 protected void startClient(int port) { 333 t = new Thread(new SSClient(port), "SSClient"); 334 t.start(); 335 try { 336 Thread.sleep(1000); 337 } catch (InterruptedException e) { 338 System.out.println("Exception during startClinet()" + e.toString()); 339 } 340 } 341 342 class MockSocketImpl extends SocketImpl { 343 public MockSocketImpl() { 344 isCreateCalled = true; 345 } 346 347 protected void create(boolean arg0) throws IOException { 348 } 349 350 protected void connect(String arg0, int arg1) throws IOException { 351 } 352 353 protected void connect(InetAddress arg0, int arg1) throws IOException { 354 } 355 356 protected void connect(SocketAddress arg0, int arg1) throws IOException { 357 } 358 359 protected void bind(InetAddress arg0, int arg1) throws IOException { 360 } 361 362 protected void listen(int arg0) throws IOException { 363 } 364 365 protected void accept(SocketImpl arg0) throws IOException { 366 } 367 368 protected InputStream getInputStream() throws IOException { 369 return null; 370 } 371 372 protected OutputStream getOutputStream() throws IOException { 373 return null; 374 } 375 376 protected int available() throws IOException { 377 return 0; 378 } 379 380 protected void close() throws IOException { 381 } 382 383 protected void sendUrgentData(int arg0) throws IOException { 384 } 385 386 public void setOption(int arg0, Object arg1) throws SocketException { 387 } 388 389 public Object getOption(int arg0) throws SocketException { 390 return null; 391 } 392 } 393} 394