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