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