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 dalvik.annotation.BrokenTest;
     19 import dalvik.annotation.TestTargetClass;
     20 import dalvik.annotation.TestLevel;
     21 import dalvik.annotation.TestTargetNew;
     22 
     23 import java.io.IOException;
     24 import java.net.ServerSocket;
     25 import java.net.Socket;
     26 import java.net.UnknownHostException;
     27 
     28 import javax.net.SocketFactory;
     29 import javax.net.ssl.SSLSocketFactory;
     30 
     31 import junit.framework.TestCase;
     32 
     33 import tests.support.Support_PortManager;
     34 
     35 @TestTargetClass(SSLSocketFactory.class)
     36 public class SSLSocketFactoryTest extends TestCase {
     37 
     38     private ServerSocket ss;
     39 
     40     protected int startServer(String name) {
     41         int portNumber = Support_PortManager.getNextPort();
     42         try {
     43             ss = new ServerSocket(portNumber);
     44         } catch (IOException e) {
     45             fail(name + ": " + e);
     46         }
     47         return ss.getLocalPort();
     48     }
     49 
     50     /**
     51      * @tests javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
     52      */
     53     @TestTargetNew(
     54         level = TestLevel.COMPLETE,
     55         notes = "",
     56         method = "SSLSocketFactory",
     57         args = {}
     58     )
     59     public void test_Constructor() {
     60         try {
     61             SocketFactory sf = SSLSocketFactory.getDefault();
     62             assertTrue(sf instanceof SSLSocketFactory);
     63         } catch (Exception e) {
     64             fail("Unexpected exception " + e.toString());
     65         }
     66     }
     67 
     68     /**
     69      * @tests javax.net.ssl.SSLSocketFactory#getDefault()
     70      */
     71     @TestTargetNew(
     72         level = TestLevel.COMPLETE,
     73         notes = "",
     74         method = "getDefault",
     75         args = {}
     76     )
     77     public void test_getDefault() {
     78         assertNotNull("Incorrect default socket factory",
     79                 SSLSocketFactory.getDefault());
     80     }
     81 
     82     /**
     83      * @tests javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose)
     84      */
     85     @TestTargetNew(
     86         level = TestLevel.COMPLETE,
     87         notes = "",
     88         method = "createSocket",
     89         args = {java.net.Socket.class, java.lang.String.class, int.class, boolean.class}
     90     )
     91     public void test_createSocket() {
     92         SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault();
     93         int sport = startServer("test_createSocket()");
     94         int[] invalid = {
     95                 Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE
     96         };
     97         try {
     98             Socket st = new Socket("localhost", sport);
     99             Socket s = sf.createSocket(st, "localhost", sport, false);
    100             assertFalse(s.isClosed());
    101         } catch (Exception ex) {
    102             fail("Unexpected exception " + ex);
    103         }
    104         try {
    105             Socket st = new Socket("localhost", sport);
    106             Socket s = sf.createSocket(st, "localhost", sport, true);
    107             s.close();
    108             assertTrue(st.isClosed());
    109         } catch (Exception ex) {
    110             fail("Unexpected exception " + ex);
    111         }
    112         try {
    113             sf.createSocket(null, "localhost", sport, true);
    114             fail("IOException wasn't thrown");
    115         } catch (IOException ioe) {
    116             // expected
    117         } catch (NullPointerException e) {
    118             // expected
    119         }
    120         for (int i = 0; i < invalid.length; i++) {
    121             try {
    122                 Socket s = sf.createSocket(new Socket(), "localhost", 1080, false);
    123                 fail("IOException wasn't thrown");
    124             } catch (IOException ioe) {
    125                 // expected
    126             }
    127         }
    128 
    129         try {
    130             Socket st = new Socket("bla-bla", sport);
    131             Socket s = sf.createSocket(st, "bla-bla", sport, false);
    132             fail("UnknownHostException wasn't thrown: " + "bla-bla");
    133         } catch (UnknownHostException uhe) {
    134             // expected
    135         } catch (Exception e) {
    136             fail(e + " was thrown instead of UnknownHostException");
    137         }
    138     }
    139 
    140     /**
    141      * @tests javax.net.ssl.SSLSocketFactory#getDefaultCipherSuites()
    142      */
    143     @TestTargetNew(
    144         level = TestLevel.COMPLETE,
    145         notes = "",
    146         method = "getDefaultCipherSuites",
    147         args = {}
    148     )
    149     public void test_getDefaultCipherSuites() {
    150         try {
    151             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    152             assertTrue("no default cipher suites returned",
    153                     sf.getDefaultCipherSuites().length > 0);
    154         } catch (Exception e) {
    155             fail("Unexpected exception " + e.toString());
    156         }
    157     }
    158 
    159     /**
    160      * @tests javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
    161      */
    162     @TestTargetNew(
    163         level = TestLevel.COMPLETE,
    164         notes = "",
    165         method = "getSupportedCipherSuites",
    166         args = {}
    167     )
    168     public void test_getSupportedCipherSuites() {
    169         try {
    170             SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
    171             assertTrue("no supported cipher suites returned",
    172                     sf.getSupportedCipherSuites().length > 0);
    173         } catch (Exception e) {
    174             fail("Unexpected exception " + e.toString());
    175         }
    176     }
    177 
    178 }
    179