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 18 /** 19 * @author Boris V. Kuznetsov 20 * @version $Revision$ 21 */ 22 23 package tests.api.javax.net; 24 25 import dalvik.annotation.TestTargetClass; 26 import dalvik.annotation.TestLevel; 27 import dalvik.annotation.TestTargetNew; 28 29 import java.io.IOException; 30 import java.net.InetAddress; 31 import java.net.ServerSocket; 32 import java.net.Socket; 33 import java.net.SocketException; 34 import java.net.UnknownHostException; 35 36 import javax.net.SocketFactory; 37 38 import junit.framework.TestCase; 39 40 import tests.support.Support_PortManager; 41 42 43 /** 44 * Tests for <code>SocketFactory</code> class methods. 45 */ 46 @TestTargetClass(SocketFactory.class) 47 public class SocketFactoryTest extends TestCase { 48 49 /** 50 * @tests javax.net.SocketFactory#SocketFactory() 51 */ 52 @TestTargetNew( 53 level = TestLevel.COMPLETE, 54 notes = "", 55 method = "SocketFactory", 56 args = {} 57 ) 58 public void test_Constructor() { 59 try { 60 MySocketFactory sf = new MySocketFactory(); 61 } catch (Exception e) { 62 fail("Unexpected exception " + e.toString()); 63 } 64 } 65 66 /** 67 * @tests javax.net.SocketFactory#createSocket() 68 */ 69 @TestTargetNew( 70 level = TestLevel.SUFFICIENT, 71 notes = "IOException check missed", 72 method = "createSocket", 73 args = {} 74 ) 75 public final void test_createSocket_01() { 76 SocketFactory sf = SocketFactory.getDefault(); 77 78 try { 79 Socket s = sf.createSocket(); 80 assertNotNull(s); 81 assertEquals(-1, s.getLocalPort()); 82 assertEquals(0, s.getPort()); 83 } catch (Exception e) { 84 fail("Unexpected exception: " + e); 85 } 86 87 MySocketFactory msf = new MySocketFactory(); 88 try { 89 msf.createSocket(); 90 fail("No expected SocketException"); 91 } catch (SocketException e) { 92 } catch (IOException e) { 93 fail(e.toString()); 94 } 95 } 96 97 /** 98 * @tests javax.net.SocketFactory#createSocket(String host, int port) 99 */ 100 @TestTargetNew( 101 level = TestLevel.COMPLETE, 102 notes = "", 103 method = "createSocket", 104 args = {String.class, int.class} 105 ) 106 public final void test_createSocket_02() { 107 SocketFactory sf = SocketFactory.getDefault(); 108 int portNumber = Support_PortManager.getNextPort(); 109 int sport = startServer("Cons String,I"); 110 int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE}; 111 112 try { 113 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport); 114 assertNotNull(s); 115 assertTrue("Failed to create socket", s.getPort() == sport); 116 } catch (Exception e) { 117 fail("Unexpected exception: " + e); 118 } 119 120 try { 121 Socket s = sf.createSocket("bla-bla", sport); 122 fail("UnknownHostException wasn't thrown"); 123 } catch (UnknownHostException uhe) { 124 //expected 125 } catch (Exception e) { 126 fail(e + " was thrown instead of UnknownHostException"); 127 } 128 129 for (int i = 0; i < invalidPorts.length; i++) { 130 try { 131 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]); 132 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 133 } catch (IllegalArgumentException iae) { 134 //expected 135 } catch (Exception e) { 136 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 137 } 138 } 139 140 try { 141 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), portNumber); 142 fail("IOException wasn't thrown"); 143 } catch (IOException ioe) { 144 //expected 145 } 146 147 SocketFactory f = SocketFactory.getDefault(); 148 try { 149 Socket s = f.createSocket("localhost", 8082); 150 fail("IOException wasn't thrown ..."); 151 } catch (IOException e) { 152 } 153 } 154 155 /** 156 * @tests javax.net.SocketFactory#createSocket(InetAddress host, int port) 157 */ 158 @TestTargetNew( 159 level = TestLevel.COMPLETE, 160 notes = "", 161 method = "createSocket", 162 args = {InetAddress.class, int.class} 163 ) 164 public final void test_createSocket_03() { 165 SocketFactory sf = SocketFactory.getDefault(); 166 int portNumber = Support_PortManager.getNextPort(); 167 int sport = startServer("Cons InetAddress,I"); 168 int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE}; 169 170 try { 171 Socket s = sf.createSocket(InetAddress.getLocalHost(), sport); 172 assertNotNull(s); 173 assertTrue("Failed to create socket", s.getPort() == sport); 174 } catch (Exception e) { 175 fail("Unexpected exception: " + e); 176 } 177 178 for (int i = 0; i < invalidPorts.length; i++) { 179 try { 180 Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]); 181 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 182 } catch (IllegalArgumentException iae) { 183 //expected 184 } catch (Exception e) { 185 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 186 } 187 } 188 189 try { 190 Socket s = sf.createSocket(InetAddress.getLocalHost(), portNumber); 191 fail("IOException wasn't thrown"); 192 } catch (IOException ioe) { 193 //expected 194 } 195 196 SocketFactory f = SocketFactory.getDefault(); 197 try { 198 Socket s = f.createSocket(InetAddress.getLocalHost(), 8081); 199 fail("IOException wasn't thrown ..."); 200 } catch (IOException e) { 201 } 202 } 203 204 /** 205 * @tests javax.net.SocketFactory#createSocket(InetAddress address, int port, 206 * InetAddress localAddress, int localPort) 207 */ 208 @TestTargetNew( 209 level = TestLevel.COMPLETE, 210 notes = "", 211 method = "createSocket", 212 args = {InetAddress.class, int.class, InetAddress.class, int.class} 213 ) 214 public final void test_createSocket_04() { 215 SocketFactory sf = SocketFactory.getDefault(); 216 int portNumber = Support_PortManager.getNextPort(); 217 int sport = startServer("Cons InetAddress,I,InetAddress,I"); 218 int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE}; 219 220 try { 221 Socket s = sf.createSocket(InetAddress.getLocalHost(), sport, 222 InetAddress.getLocalHost(), portNumber); 223 assertNotNull(s); 224 assertTrue("1: Failed to create socket", s.getPort() == sport); 225 assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber); 226 } catch (Exception e) { 227 fail("Unexpected exception: " + e); 228 } 229 230 for (int i = 0; i < invalidPorts.length; i++) { 231 try { 232 Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i], 233 InetAddress.getLocalHost(), portNumber); 234 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 235 } catch (IllegalArgumentException iae) { 236 //expected 237 } catch (Exception e) { 238 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 239 } 240 241 try { 242 Socket s = sf.createSocket(InetAddress.getLocalHost(), sport, 243 InetAddress.getLocalHost(), invalidPorts[i]); 244 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 245 } catch (IllegalArgumentException iae) { 246 //expected 247 } catch (Exception e) { 248 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 249 } 250 } 251 252 try { 253 Socket s = sf.createSocket(InetAddress.getLocalHost(), sport, 254 InetAddress.getLocalHost(), portNumber); 255 fail("IOException wasn't thrown"); 256 } catch (IOException ioe) { 257 //expected 258 } 259 260 SocketFactory f = SocketFactory.getDefault(); 261 try { 262 Socket s = f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082); 263 fail("IOException wasn't thrown ..."); 264 } catch (IOException e) { 265 } 266 } 267 268 /** 269 * @tests javax.net.SocketFactory#createSocket(String host, int port, 270 * InetAddress localHost, int localPort) 271 */ 272 @TestTargetNew( 273 level = TestLevel.COMPLETE, 274 notes = "", 275 method = "createSocket", 276 args = {String.class, int.class, InetAddress.class, int.class} 277 ) 278 public final void test_createSocket_05() { 279 SocketFactory sf = SocketFactory.getDefault(); 280 int portNumber = Support_PortManager.getNextPort(); 281 int sport = startServer("Cons String,I,InetAddress,I"); 282 int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE}; 283 284 try { 285 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport, 286 InetAddress.getLocalHost(), portNumber); 287 assertNotNull(s); 288 assertTrue("1: Failed to create socket", s.getPort() == sport); 289 assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber); 290 } catch (Exception e) { 291 fail("Unexpected exception: " + e); 292 } 293 294 portNumber = Support_PortManager.getNextPort(); 295 try { 296 Socket s = sf.createSocket("bla-bla", sport, InetAddress.getLocalHost(), portNumber); 297 fail("UnknownHostException wasn't thrown"); 298 } catch (UnknownHostException uhe) { 299 //expected 300 } catch (Exception e) { 301 fail(e + " was thrown instead of UnknownHostException"); 302 } 303 304 for (int i = 0; i < invalidPorts.length; i++) { 305 portNumber = Support_PortManager.getNextPort(); 306 try { 307 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i], 308 InetAddress.getLocalHost(), portNumber); 309 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 310 } catch (IllegalArgumentException iae) { 311 //expected 312 } catch (Exception e) { 313 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 314 } 315 try { 316 Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport, 317 InetAddress.getLocalHost(), invalidPorts[i]); 318 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]); 319 } catch (IllegalArgumentException iae) { 320 //expected 321 } catch (Exception e) { 322 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]); 323 } 324 } 325 326 SocketFactory f = SocketFactory.getDefault(); 327 try { 328 Socket s = f.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082); 329 fail("IOException wasn't thrown ..."); 330 } catch (IOException e) { 331 } 332 } 333 334 /** 335 * @tests javax.net.SocketFactory#getDefault() 336 */ 337 @TestTargetNew( 338 level = TestLevel.COMPLETE, 339 notes = "", 340 method = "getDefault", 341 args = {} 342 ) 343 public final void test_getDefault() { 344 SocketFactory sf = SocketFactory.getDefault(); 345 Socket s; 346 try { 347 s = sf.createSocket("localhost", 8082); 348 s.close(); 349 } catch (IOException e) { 350 } 351 try { 352 s = sf.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082); 353 s.close(); 354 } catch (IOException e) { 355 } 356 try { 357 s = sf.createSocket(InetAddress.getLocalHost(), 8081); 358 s.close(); 359 } catch (IOException e) { 360 } 361 try { 362 s = sf.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082); 363 s.close(); 364 } catch (IOException e) { 365 } 366 } 367 368 protected int startServer(String name) { 369 int portNumber = Support_PortManager.getNextPort(); 370 ServerSocket ss = null; 371 try { 372 ss = new ServerSocket(portNumber); 373 } catch (IOException e) { 374 fail(name + ": " + e); 375 } 376 return ss.getLocalPort(); 377 } 378 } 379 380 class MySocketFactory extends SocketFactory { 381 382 public MySocketFactory() { 383 super(); 384 } 385 386 @Override 387 public Socket createSocket(String host, int port) throws IOException, UnknownHostException { 388 return null; 389 } 390 391 @Override 392 public Socket createSocket(String host, int port, InetAddress localHost, int localPort) 393 throws IOException, UnknownHostException { 394 return null; 395 } 396 397 @Override 398 public Socket createSocket(InetAddress host, int port) throws IOException { 399 return null; 400 } 401 402 @Override 403 public Socket createSocket(InetAddress address, int port, 404 InetAddress localAddress, int localPort) throws IOException { 405 return null; 406 } 407 408 } 409