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 public class SSLSocketFactoryTest extends TestCase {
     29 
     30     private ServerSocket ss;
     31 
     32     protected int startServer(String name) {
     33         try {
     34             ss = new ServerSocket(0);
     35         } catch (IOException e) {
     36             fail(name + ": " + e);
     37         }
     38         return ss.getLocalPort();
     39     }
     40 
     41     /**
     42      * javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
     43      */
     44     public void test_Constructor() {
     45         try {
     46             SocketFactory sf = SSLSocketFactory.getDefault();
     47             assertTrue(sf instanceof SSLSocketFactory);
     48         } catch (Exception e) {
     49             fail("Unexpected exception " + e.toString());
     50         }
     51     }
     52 
     53     /**
     54      * javax.net.ssl.SSLSocketFactory#getDefault()
     55      */
     56     public void test_getDefault() {
     57         assertNotNull("Incorrect default socket factory",
     58                 SSLSocketFactory.getDefault());
     59     }
     60 
     61     /**
     62      * javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose)
     63      */
     64     public void test_createSocket() {
     65         SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
     66         int sport = startServer("test_createSocket()");
     67         int[] invalid = {
     68                 Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE
     69         };
     70         try {
     71             Socket st = new Socket("localhost", sport);
     72             Socket s = sf.createSocket(st, "localhost", sport, false);
     73             assertFalse(s.isClosed());
     74         } catch (Exception ex) {
     75             fail("Unexpected exception " + ex);
     76         }
     77         try {
     78             Socket st = new Socket("localhost", sport);
     79             Socket s = sf.createSocket(st, "localhost", sport, true);
     80             s.close();
     81             assertTrue(st.isClosed());
     82         } catch (Exception ex) {
     83             fail("Unexpected exception " + ex);
     84         }
     85         try {
     86             sf.createSocket(null, "localhost", sport, true);
     87             fail("IOException wasn't thrown");
     88         } catch (IOException ioe) {
     89             // expected
     90         } catch (NullPointerException e) {
     91             // expected
     92         }
     93         for (int i = 0; i < invalid.length; i++) {
     94             try {
     95                 Socket s = sf.createSocket(new Socket(), "localhost", 1080, false);
     96                 fail("IOException wasn't thrown");
     97             } catch (IOException ioe) {
     98                 // expected
     99             }
    100         }
    101 
    102         try {
    103             Socket st = new Socket("bla-bla", sport);
    104             Socket s = sf.createSocket(st, "bla-bla", sport, false);
    105             fail("UnknownHostException wasn't thrown: " + "bla-bla");
    106         } catch (UnknownHostException uhe) {
    107             // expected
    108         } catch (Exception e) {
    109             fail(e + " was thrown instead of UnknownHostException");
    110         }
    111     }
    112 
    113     /**
    114      * javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
    115      */
    116     public void test_getDefaultCipherSuites() {
    117         try {
    118             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    119             assertTrue("no default cipher suites returned",
    120                     sf.getDefaultCipherSuites().length > 0);
    121         } catch (Exception e) {
    122             fail("Unexpected exception " + e.toString());
    123         }
    124     }
    125 
    126     /**
    127      * javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
    128      */
    129     public void test_getSupportedCipherSuites() {
    130         try {
    131             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    132             assertTrue("no supported cipher suites returned",
    133                     sf.getSupportedCipherSuites().length > 0);
    134         } catch (Exception e) {
    135             fail("Unexpected exception " + e.toString());
    136         }
    137     }
    138 
    139 }
    140