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 org.apache.harmony.tests.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 public class SocketFactoryTest extends TestCase {
     37 
     38     public void test_Constructor() throws Exception {
     39         new MySocketFactory();
     40     }
     41 
     42     public final void test_createSocket() throws Exception {
     43         SocketFactory sf = SocketFactory.getDefault();
     44 
     45         Socket s = sf.createSocket();
     46         assertNotNull(s);
     47         assertEquals(-1, s.getLocalPort());
     48         assertEquals(0, s.getPort());
     49 
     50         MySocketFactory msf = new MySocketFactory();
     51         try {
     52             msf.createSocket();
     53             fail("No expected SocketException");
     54         } catch (SocketException expected) {
     55         }
     56     }
     57 
     58     public final void test_createSocket_StringI() throws Exception {
     59         SocketFactory sf = SocketFactory.getDefault();
     60         int sport = new ServerSocket(0).getLocalPort();
     61         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
     62 
     63         Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
     64         assertNotNull(s);
     65         assertTrue("Failed to create socket", s.getPort() == sport);
     66 
     67         try {
     68             sf.createSocket("1.2.3.4hello", sport);
     69             fail("UnknownHostException wasn't thrown");
     70         } catch (UnknownHostException expected) {
     71         }
     72 
     73         for (int i = 0; i < invalidPorts.length; i++) {
     74             try {
     75                 sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
     76                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
     77             } catch (IllegalArgumentException expected) {
     78             }
     79         }
     80 
     81         try {
     82             sf.createSocket(InetAddress.getLocalHost().getHostName(), s.getLocalPort());
     83             fail("IOException wasn't thrown");
     84         } catch (IOException expected) {
     85         }
     86 
     87         SocketFactory f = SocketFactory.getDefault();
     88         try {
     89             f.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
     90             fail("IOException wasn't thrown ...");
     91         } catch (IOException expected) {
     92         }
     93     }
     94 
     95     public final void test_createSocket_InetAddressI() throws Exception {
     96         SocketFactory sf = SocketFactory.getDefault();
     97         int sport = new ServerSocket(0).getLocalPort();
     98         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
     99 
    100         Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
    101         assertNotNull(s);
    102         assertTrue("Failed to create socket", s.getPort() == sport);
    103 
    104         for (int i = 0; i < invalidPorts.length; i++) {
    105             try {
    106                 sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
    107                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    108             } catch (IllegalArgumentException expected) {
    109             }
    110         }
    111 
    112         try {
    113             sf.createSocket(InetAddress.getLocalHost(), s.getLocalPort());
    114             fail("IOException wasn't thrown");
    115         } catch (IOException expected) {
    116         }
    117 
    118         SocketFactory f = SocketFactory.getDefault();
    119         try {
    120             f.createSocket(InetAddress.getLocalHost(), 8081);
    121             fail("IOException wasn't thrown ...");
    122         } catch (IOException expected) {
    123         }
    124     }
    125 
    126     public final void test_createSocket_InetAddressIInetAddressI() throws Exception {
    127         SocketFactory sf = SocketFactory.getDefault();
    128         int sport = new ServerSocket(0).getLocalPort();
    129 
    130         Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
    131                 InetAddress.getLocalHost(), 0);
    132         assertNotNull(s);
    133         assertTrue("1: Failed to create socket", s.getPort() == sport);
    134         int portNumber = s.getLocalPort();
    135         try {
    136             sf.createSocket(InetAddress.getLocalHost(), sport,
    137                             InetAddress.getLocalHost(), portNumber);
    138             fail("IOException wasn't thrown");
    139         } catch (IOException expected) {
    140         }
    141 
    142         SocketFactory f = SocketFactory.getDefault();
    143         try {
    144             f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
    145             fail("IOException wasn't thrown ...");
    146         } catch (IOException expected) {
    147         }
    148     }
    149 
    150     // Checks the behavior of createSocket(InetAddress, int, InetAddress, int) when the
    151     // ports are invalid.
    152     public void test_createSocket_InetAddressIInetAddressI_IllegalArgumentException()
    153             throws Exception {
    154         SocketFactory sf = SocketFactory.getDefault();
    155         int validPort = new ServerSocket(0).getLocalPort();
    156         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
    157 
    158         for (int i = 0; i < invalidPorts.length; i++) {
    159             // Check invalid server port.
    160             try (Socket s = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */,
    161                     invalidPorts[i] /* ServerPort */,
    162                     InetAddress.getLocalHost() /* ClientAddress */,
    163                     validPort /* ClientPort */)) {
    164                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    165             } catch (IllegalArgumentException expected) {
    166             }
    167 
    168             // Check invalid client port.
    169             try (Socket s = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */,
    170                     validPort /* ServerPort */,
    171                     InetAddress.getLocalHost() /* ClientAddress */,
    172                     invalidPorts[i]) /* ClientPort */){
    173                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    174             } catch (IllegalArgumentException expected) {
    175             }
    176         }
    177     }
    178 
    179     // b/31019685
    180     // Checks the ordering of port number validation (IllegalArgumentException) and binding error.
    181     public void test_createSocket_InetAddressIInetAddressI_ExceptionOrder() throws IOException {
    182         int invalidPort = Integer.MAX_VALUE;
    183         SocketFactory sf = SocketFactory.getDefault();
    184         int validServerPortNumber = new ServerSocket(0).getLocalPort();
    185 
    186         // Create a socket with localhost as the client address so that another attempt to bind
    187         // would fail.
    188         Socket s = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */,
    189                 validServerPortNumber /* ServerPortNumber */,
    190                 InetAddress.getLocalHost() /* ClientAddress */,
    191                 0 /* ClientPortNumber */);
    192 
    193         int assignedLocalPortNumber = s.getLocalPort();
    194 
    195         // Create a socket with an invalid port and localhost as the client address. Both
    196         // BindException and IllegalArgumentException are expected in this case as the address is
    197         // already bound to the socket above and port is invalid, however, to preserve the
    198         // precedence order, IllegalArgumentException should be thrown.
    199         try (Socket s1 = sf.createSocket(InetAddress.getLocalHost() /* ServerAddress */,
    200                 invalidPort /* ServerPortNumber */,
    201                 InetAddress.getLocalHost() /* ClientAddress */,
    202                 assignedLocalPortNumber /* ClientPortNumber */)) {
    203             fail("IllegalArgumentException wasn't thrown for " + invalidPort);
    204         } catch (IllegalArgumentException expected) {
    205         }
    206     }
    207 
    208     /**
    209      * javax.net.SocketFactory#createSocket(String host, int port,
    210      *                                             InetAddress localHost, int localPort)
    211      */
    212     public final void test_createSocket_05() throws Exception {
    213         SocketFactory sf = SocketFactory.getDefault();
    214         int sport = new ServerSocket(0).getLocalPort();
    215         int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
    216 
    217         Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
    218                                    InetAddress.getLocalHost(), 0);
    219         assertNotNull(s);
    220         assertTrue("1: Failed to create socket", s.getPort() == sport);
    221 
    222         try {
    223             sf.createSocket("1.2.3.4hello", sport, InetAddress.getLocalHost(), 0);
    224             fail("UnknownHostException wasn't thrown");
    225         } catch (UnknownHostException expected) {
    226         }
    227 
    228         for (int i = 0; i < invalidPorts.length; i++) {
    229             try {
    230                 sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i],
    231                                 InetAddress.getLocalHost(), 0);
    232                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    233             } catch (IllegalArgumentException expected) {
    234             }
    235             try {
    236                 sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
    237                                 InetAddress.getLocalHost(), invalidPorts[i]);
    238                 fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
    239             } catch (IllegalArgumentException expected) {
    240             }
    241         }
    242 
    243         try {
    244             sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
    245             fail("IOException wasn't thrown ...");
    246         } catch (IOException expected) {
    247         }
    248     }
    249 
    250     /**
    251      * javax.net.SocketFactory#getDefault()
    252      */
    253     public final void test_getDefault() {
    254         SocketFactory sf = SocketFactory.getDefault();
    255         Socket s;
    256         try {
    257             s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8082);
    258             s.close();
    259         } catch (IOException e) {
    260         }
    261         try {
    262             s = sf.createSocket(InetAddress.getLocalHost().getHostName(), 8081, InetAddress.getLocalHost(), 8082);
    263             s.close();
    264         } catch (IOException e) {
    265         }
    266         try {
    267             s = sf.createSocket(InetAddress.getLocalHost(), 8081);
    268             s.close();
    269         } catch (IOException e) {
    270         }
    271         try {
    272             s = sf.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
    273             s.close();
    274         } catch (IOException e) {
    275         }
    276     }
    277 }
    278 
    279 class MySocketFactory extends SocketFactory {
    280 
    281     public MySocketFactory() {
    282         super();
    283     }
    284 
    285     @Override
    286     public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
    287         return null;
    288     }
    289 
    290     @Override
    291     public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
    292             throws IOException, UnknownHostException {
    293         return null;
    294     }
    295 
    296     @Override
    297     public Socket createSocket(InetAddress host, int port) throws IOException {
    298         return null;
    299      }
    300 
    301     @Override
    302     public Socket createSocket(InetAddress address, int port,
    303                                InetAddress localAddress, int localPort) throws IOException {
    304         return null;
    305      }
    306 
    307 }
    308