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 ServerSocketChannel ssc = ServerSocketChannel.open(); 216 ServerSocket ss = ssc.socket(); 217 218 try { 219 ss.accept(); 220 fail("IllegalBlockingModeException was not thrown."); 221 } catch(IllegalBlockingModeException ibme) { 222 //expected 223 } finally { 224 ss.close(); 225 ssc.close(); 226 } 227 } 228 229 public void test_getSoTimeout_setSoTimeout() throws Exception { 230 // TODO: a useful test would check that setSoTimeout actually causes timeouts! 231 ServerSocket s = new ServerSocket(); 232 final int timeoutSet = 1500; 233 s.setSoTimeout(timeoutSet); 234 int actualTimeout = s.getSoTimeout(); 235 // The kernel can round the requested value based on the HZ setting. We allow up to 10ms. 236 assertTrue("suspicious timeout: " + actualTimeout, Math.abs(actualTimeout - timeoutSet) <= 10); 237 s.close(); 238 try { 239 s.getSoTimeout(); 240 fail("SocketException was not thrown."); 241 } catch (SocketException expected) { 242 } 243 try { 244 s.setSoTimeout(1000); 245 fail("SocketException was not thrown."); 246 } catch (SocketException expected) { 247 } 248 } 249 250 public void test_toString() throws Exception { 251 s = new ServerSocket(0); 252 int portNumber = s.getLocalPort(); 253 assertTrue(s.toString().contains("" + portNumber)); 254 s.close(); 255 } 256 257 public void test_setReuseAddressZ() throws IOException { 258 ServerSocket newSocket = new ServerSocket(); 259 newSocket.close(); 260 try { 261 newSocket.setReuseAddress(true); 262 fail("SocketException was not thrown."); 263 } catch(SocketException expected) { 264 } 265 } 266 267 public void test_getReuseAddress() throws IOException { 268 ServerSocket newSocket = new ServerSocket(); 269 newSocket.close(); 270 try { 271 newSocket.getReuseAddress(); 272 fail("SocketException was not thrown."); 273 } catch(SocketException e) { 274 //expected 275 } 276 } 277 278 public void test_setReceiveBufferSizeI() throws IOException { 279 ServerSocket newSocket = new ServerSocket(); 280 newSocket.close(); 281 try { 282 newSocket.setReceiveBufferSize(10); 283 fail("SocketException was not thrown."); 284 } catch(SocketException se) { 285 //expected 286 } 287 } 288 289 public void test_getReceiveBufferSize() throws IOException { 290 ServerSocket newSocket = new ServerSocket(); 291 newSocket.close(); 292 try { 293 newSocket.getReceiveBufferSize(); 294 fail("SocketException was not thrown."); 295 } catch (SocketException e) { 296 //expected 297 } 298 } 299 300 protected void tearDown() { 301 try { 302 if (s != null) 303 s.close(); 304 if (sconn != null) 305 sconn.close(); 306 if (t != null) 307 t.interrupt(); 308 } catch (Exception e) { 309 } 310 } 311 312 /** 313 * Sets up the fixture, for example, open a network connection. This method 314 * is called before a test is executed. 315 */ 316 protected void startClient(int port) { 317 t = new Thread(new SSClient(port), "SSClient"); 318 t.start(); 319 try { 320 Thread.sleep(1000); 321 } catch (InterruptedException e) { 322 System.out.println("Exception during startClinet()" + e.toString()); 323 } 324 } 325 326 class MockSocketImpl extends SocketImpl { 327 public MockSocketImpl() { 328 isCreateCalled = true; 329 } 330 331 protected void create(boolean arg0) throws IOException { 332 } 333 334 protected void connect(String arg0, int arg1) throws IOException { 335 } 336 337 protected void connect(InetAddress arg0, int arg1) throws IOException { 338 } 339 340 protected void connect(SocketAddress arg0, int arg1) throws IOException { 341 } 342 343 protected void bind(InetAddress arg0, int arg1) throws IOException { 344 } 345 346 protected void listen(int arg0) throws IOException { 347 } 348 349 protected void accept(SocketImpl arg0) throws IOException { 350 } 351 352 protected InputStream getInputStream() throws IOException { 353 return null; 354 } 355 356 protected OutputStream getOutputStream() throws IOException { 357 return null; 358 } 359 360 protected int available() throws IOException { 361 return 0; 362 } 363 364 protected void close() throws IOException { 365 } 366 367 protected void sendUrgentData(int arg0) throws IOException { 368 } 369 370 public void setOption(int arg0, Object arg1) throws SocketException { 371 } 372 373 public Object getOption(int arg0) throws SocketException { 374 return null; 375 } 376 } 377} 378