Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2008 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 
     17 package android.net.cts;
     18 
     19 import java.io.IOException;
     20 import java.net.InetAddress;
     21 import java.net.Socket;
     22 
     23 import javax.net.SocketFactory;
     24 import javax.net.ssl.SSLPeerUnverifiedException;
     25 
     26 import android.net.SSLCertificateSocketFactory;
     27 import android.test.AndroidTestCase;
     28 
     29 import libcore.javax.net.ssl.SSLConfigurationAsserts;
     30 
     31 public class SSLCertificateSocketFactoryTest extends AndroidTestCase {
     32     private SSLCertificateSocketFactory mFactory;
     33     private int mTimeout;
     34 
     35     @Override
     36     protected void setUp() throws Exception {
     37         super.setUp();
     38         mTimeout = 1000;
     39         mFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(mTimeout);
     40     }
     41 
     42     public void testDefaultConfiguration() throws Exception {
     43         SSLConfigurationAsserts.assertSSLSocketFactoryDefaultConfiguration(mFactory);
     44     }
     45 
     46     public void testAccessProperties() throws Exception {
     47         mFactory.getSupportedCipherSuites();
     48         mFactory.getDefaultCipherSuites();
     49         SocketFactory sf = SSLCertificateSocketFactory.getDefault(mTimeout);
     50         assertNotNull(sf);
     51     }
     52 
     53     public void testCreateSocket() throws Exception {
     54         new SSLCertificateSocketFactory(100);
     55         int port = 443;
     56         String host = "www.google.com";
     57         InetAddress inetAddress = null;
     58         inetAddress = InetAddress.getLocalHost();
     59         try {
     60             mFactory.createSocket(inetAddress, port);
     61             fail("should throw exception!");
     62         } catch (IOException e) {
     63             // expected
     64         }
     65 
     66         try {
     67             InetAddress inetAddress1 = InetAddress.getLocalHost();
     68             InetAddress inetAddress2 = InetAddress.getLocalHost();
     69             mFactory.createSocket(inetAddress1, port, inetAddress2, port);
     70             fail("should throw exception!");
     71         } catch (IOException e) {
     72             // expected
     73         }
     74 
     75         try {
     76             Socket socket = new Socket();
     77             mFactory.createSocket(socket, host, port, true);
     78             fail("should throw exception!");
     79         } catch (IOException e) {
     80             // expected
     81         }
     82         Socket socket = null;
     83         socket = mFactory.createSocket(host, port);
     84         assertNotNull(socket);
     85         assertNotNull(socket.getOutputStream());
     86         assertNotNull(socket.getInputStream());
     87 
     88         // it throw exception when calling createSocket(String, int, InetAddress, int)
     89         // The socket level is invalid.
     90     }
     91 
     92     // a host and port that are expected to be available but have
     93     // a cert with a different CN, in this case CN=mail.google.com
     94     private static String TEST_CREATE_SOCKET_HOST = "googlemail.com";
     95     private static int TEST_CREATE_SOCKET_PORT = 443;
     96 
     97     /**
     98      * b/2807618 Make sure that hostname verifcation in cases were it
     99      * is documented to be included by various
    100      * SSLCertificateSocketFactory.createSocket messages.
    101      *
    102      * NOTE: Test will fail if external server is not available.
    103      */
    104     public void test_createSocket_simple() throws Exception {
    105         try {
    106             mFactory.createSocket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT);
    107             fail();
    108         } catch (SSLPeerUnverifiedException expected) {
    109             // expected
    110         }
    111     }
    112 
    113     /**
    114      * b/2807618 Make sure that hostname verifcation in cases were it
    115      * is documented to be included by various
    116      * SSLCertificateSocketFactory.createSocket messages.
    117      *
    118      * NOTE: Test will fail if external server is not available.
    119      */
    120     public void test_createSocket_wrapping() throws Exception {
    121         try {
    122             Socket underlying = new Socket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT);
    123             mFactory.createSocket(
    124                     underlying, TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT, true);
    125             fail();
    126         } catch (SSLPeerUnverifiedException expected) {
    127             // expected
    128         }
    129     }
    130 
    131     /**
    132      * b/2807618 Make sure that hostname verifcation in cases were it
    133      * is documented to be included by various
    134      * SSLCertificateSocketFactory.createSocket messages.
    135      *
    136      * NOTE: Test will fail if external server is not available.
    137      */
    138     public void test_createSocket_bind() throws Exception {
    139         try {
    140             mFactory.createSocket(TEST_CREATE_SOCKET_HOST, TEST_CREATE_SOCKET_PORT, null, 0);
    141             fail();
    142         } catch (SSLPeerUnverifiedException expected) {
    143             // expected
    144         }
    145     }
    146 }
    147