Home | History | Annotate | Download | only in ssl
      1 /*
      2  * Copyright (C) 2007 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package tests.api.javax.net.ssl;
     17 
     18 import java.io.IOException;
     19 import java.net.ServerSocket;
     20 import java.net.Socket;
     21 import java.net.UnknownHostException;
     22 
     23 import javax.net.SocketFactory;
     24 import javax.net.ssl.SSLSocketFactory;
     25 
     26 import junit.framework.TestCase;
     27 
     28 import tests.support.Support_PortManager;
     29 
     30 public class SSLSocketFactoryTest extends TestCase {
     31 
     32     private ServerSocket ss;
     33 
     34     protected int startServer(String name) {
     35         int portNumber = Support_PortManager.getNextPort();
     36         try {
     37             ss = new ServerSocket(portNumber);
     38         } catch (IOException e) {
     39             fail(name + ": " + e);
     40         }
     41         return ss.getLocalPort();
     42     }
     43 
     44     /**
     45      * javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
     46      */
     47     public void test_Constructor() {
     48         try {
     49             SocketFactory sf = SSLSocketFactory.getDefault();
     50             assertTrue(sf instanceof SSLSocketFactory);
     51         } catch (Exception e) {
     52             fail("Unexpected exception " + e.toString());
     53         }
     54     }
     55 
     56     /**
     57      * javax.net.ssl.SSLSocketFactory#getDefault()
     58      */
     59     public void test_getDefault() {
     60         assertNotNull("Incorrect default socket factory",
     61                 SSLSocketFactory.getDefault());
     62     }
     63 
     64     /**
     65      * javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose)
     66      */
     67     public void test_createSocket() {
     68         SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
     69         int sport = startServer("test_createSocket()");
     70         int[] invalid = {
     71                 Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE
     72         };
     73         try {
     74             Socket st = new Socket("localhost", sport);
     75             Socket s = sf.createSocket(st, "localhost", sport, false);
     76             assertFalse(s.isClosed());
     77         } catch (Exception ex) {
     78             fail("Unexpected exception " + ex);
     79         }
     80         try {
     81             Socket st = new Socket("localhost", sport);
     82             Socket s = sf.createSocket(st, "localhost", sport, true);
     83             s.close();
     84             assertTrue(st.isClosed());
     85         } catch (Exception ex) {
     86             fail("Unexpected exception " + ex);
     87         }
     88         try {
     89             sf.createSocket(null, "localhost", sport, true);
     90             fail("IOException wasn't thrown");
     91         } catch (IOException ioe) {
     92             // expected
     93         } catch (NullPointerException e) {
     94             // expected
     95         }
     96         for (int i = 0; i < invalid.length; i++) {
     97             try {
     98                 Socket s = sf.createSocket(new Socket(), "localhost", 1080, false);
     99                 fail("IOException wasn't thrown");
    100             } catch (IOException ioe) {
    101                 // expected
    102             }
    103         }
    104 
    105         try {
    106             Socket st = new Socket("bla-bla", sport);
    107             Socket s = sf.createSocket(st, "bla-bla", sport, false);
    108             fail("UnknownHostException wasn't thrown: " + "bla-bla");
    109         } catch (UnknownHostException uhe) {
    110             // expected
    111         } catch (Exception e) {
    112             fail(e + " was thrown instead of UnknownHostException");
    113         }
    114     }
    115 
    116     /**
    117      * javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
    118      */
    119     public void test_getDefaultCipherSuites() {
    120         try {
    121             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    122             assertTrue("no default cipher suites returned",
    123                     sf.getDefaultCipherSuites().length > 0);
    124         } catch (Exception e) {
    125             fail("Unexpected exception " + e.toString());
    126         }
    127     }
    128 
    129     /**
    130      * javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
    131      */
    132     public void test_getSupportedCipherSuites() {
    133         try {
    134             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    135             assertTrue("no supported cipher suites returned",
    136                     sf.getSupportedCipherSuites().length > 0);
    137         } catch (Exception e) {
    138             fail("Unexpected exception " + e.toString());
    139         }
    140     }
    141 
    142 }
    143