Home | History | Annotate | Download | only in ssl
      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 package org.apache.harmony.xnet.tests.javax.net.ssl;
     19 
     20 import java.security.KeyManagementException;
     21 import java.security.NoSuchAlgorithmException;
     22 import java.security.NoSuchProviderException;
     23 import java.security.Provider;
     24 import java.security.SecureRandom;
     25 import java.security.Security;
     26 
     27 import javax.net.ssl.SSLContext;
     28 import javax.net.ssl.SSLEngine;
     29 import javax.net.ssl.KeyManager;
     30 import javax.net.ssl.TrustManager;
     31 
     32 import org.apache.harmony.xnet.tests.support.SpiEngUtils;
     33 import org.apache.harmony.xnet.tests.support.MySSLContextSpi;
     34 import junit.framework.TestCase;
     35 
     36 /**
     37  * Tests for SSLContext class constructors and methods
     38  *
     39  */
     40 
     41 public class SSLContext2Test extends TestCase {
     42 
     43     private static String srvSSLContext = "SSLContext";
     44 
     45     private static final String defaultProtocol = "S+S+L";
     46 
     47     public static final String SSLContextProviderClass = "org.apache.harmony.xnet.tests.support.MySSLContextSpi";
     48 
     49     private static final String[] invalidValues = SpiEngUtils.invalidValues;
     50 
     51     private static final String[] validValues;
     52     static {
     53         validValues = new String[4];
     54         validValues[0] = defaultProtocol;
     55         validValues[1] = defaultProtocol.toLowerCase();
     56         validValues[2] = "s+S+L";
     57         validValues[3] = "S+s+L";
     58     }
     59 
     60     Provider mProv;
     61 
     62     @Override
     63     protected void setUp() throws Exception {
     64         super.setUp();
     65         mProv = (new SpiEngUtils()).new MyProvider("MySSLContextProvider", "Provider for testing",
     66                 srvSSLContext.concat(".").concat(defaultProtocol),
     67                 SSLContextProviderClass);
     68         Security.insertProviderAt(mProv, 1);
     69     }
     70 
     71     /*
     72      * @see TestCase#tearDown()
     73      */
     74     @Override
     75     protected void tearDown() throws Exception {
     76         super.tearDown();
     77         Security.removeProvider(mProv.getName());
     78     }
     79 
     80     private void checkSSLContext(SSLContext sslC)
     81             throws KeyManagementException {
     82 
     83         try {
     84             sslC.getSocketFactory();
     85             fail("RuntimeException must be thrown");
     86         } catch (RuntimeException e) {
     87             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
     88         }
     89         try {
     90             sslC.getServerSocketFactory();
     91             fail("RuntimeException must be thrown");
     92         } catch (RuntimeException e) {
     93             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
     94         }
     95         try {
     96             sslC.getServerSessionContext();
     97             fail("RuntimeException must be thrown");
     98         } catch (RuntimeException e) {
     99             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
    100         }
    101         try {
    102             sslC.getClientSessionContext();
    103             fail("RuntimeException must be thrown");
    104         } catch (RuntimeException e) {
    105             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
    106         }
    107         try {
    108             sslC.createSSLEngine();
    109             fail("RuntimeException must be thrown");
    110         } catch (RuntimeException e) {
    111             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
    112         }
    113         try {
    114             sslC.createSSLEngine("host",1);
    115             fail("RuntimeException must be thrown");
    116         } catch (RuntimeException e) {
    117             assertEquals("Incorrect message", "Not initialiazed", e.getMessage());
    118         }
    119         TrustManager [] tm = new TManager[10];
    120         KeyManager [] km = new KManager[5];
    121         try {
    122             sslC.init(km, tm, null);
    123             fail("KeyManagementException must be thrown");
    124         } catch (KeyManagementException e) {
    125         }
    126         sslC.init(km, tm, new SecureRandom());
    127 
    128         SSLEngine sslE = sslC.createSSLEngine();
    129         assertTrue("Not null result",sslE instanceof SSLEngine);
    130         assertNull("Incorrect host", sslE.getPeerHost());
    131         assertEquals("Incorrect port", 0, sslE.getPeerPort());
    132         String host = "ZZZ";
    133         int port = 8080;
    134         sslE = sslC.createSSLEngine(host, port);
    135         assertTrue("Not null result",sslE instanceof SSLEngine);
    136         assertEquals("Incorrect host", sslE.getPeerHost(), host);
    137         assertEquals("Incorrect port", sslE.getPeerPort(), port);
    138         try {
    139             assertNull("Not null result", sslC.getServerSessionContext());
    140         } catch (NullPointerException e) {
    141         }
    142         try {
    143             assertNull("Not null result", sslC.getClientSessionContext());
    144         } catch (NullPointerException e) {
    145         }
    146     }
    147 
    148     /**
    149      * Test for <code>getInstance(String protocol)</code> method
    150      * Assertions:
    151      * throws NullPointerException when protocol is null;
    152      * throws NoSuchAlgorithmException when protocol is not correct;
    153      * returns SSLContext object
    154      */
    155     public void testGetInstance01() throws NoSuchAlgorithmException,
    156             KeyManagementException {
    157         try {
    158             SSLContext.getInstance(null);
    159             fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
    160         } catch (NoSuchAlgorithmException e) {
    161         } catch (NullPointerException e) {
    162         }
    163         for (int i = 0; i < invalidValues.length; i++) {
    164             try {
    165                 SSLContext.getInstance(invalidValues[i]);
    166                 fail("NoSuchAlgorithmException must be thrown (protocol: "
    167                         .concat(invalidValues[i]).concat(")"));
    168             } catch (NoSuchAlgorithmException e) {
    169             }
    170         }
    171         SSLContext sslC;
    172         for (int i = 0; i < validValues.length; i++) {
    173             sslC = SSLContext.getInstance(validValues[i]);
    174             assertTrue("Not instanceof SSLContext object",
    175                     sslC instanceof SSLContext);
    176             assertEquals("Incorrect protocol", sslC.getProtocol(),
    177                     validValues[i]);
    178             assertEquals("Incorrect provider", sslC.getProvider(), mProv);
    179             checkSSLContext(sslC);
    180         }
    181     }
    182 
    183     /**
    184      * Test for <code>getInstance(String protocol, String provider)</code>
    185      * method
    186      * Assertions:
    187      * throws NullPointerException when protocol is null;
    188      * throws NoSuchAlgorithmException when protocol is not correct;
    189      * throws IllegalArgumentException when provider is null or empty;
    190      * throws NoSuchProviderException when provider is available;
    191      * returns SSLContext object
    192      */
    193     public void testGetInstance02() throws NoSuchAlgorithmException,
    194             NoSuchProviderException, IllegalArgumentException,
    195             KeyManagementException {
    196         try {
    197             SSLContext.getInstance(null, mProv.getName());
    198             fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
    199         } catch (NoSuchAlgorithmException e) {
    200         } catch (NullPointerException e) {
    201         }
    202         for (int i = 0; i < invalidValues.length; i++) {
    203             try {
    204                 SSLContext.getInstance(invalidValues[i], mProv.getName());
    205                 fail("NoSuchAlgorithmException must be thrown (protocol: "
    206                         .concat(invalidValues[i]).concat(")"));
    207             } catch (NoSuchAlgorithmException e) {
    208             }
    209         }
    210         String prov = null;
    211         for (int i = 0; i < validValues.length; i++) {
    212             try {
    213                 SSLContext.getInstance(validValues[i], prov);
    214                 fail("IllegalArgumentException must be thrown when provider is null (protocol: "
    215                         .concat(invalidValues[i]).concat(")"));
    216             } catch (IllegalArgumentException e) {
    217             }
    218             try {
    219                 SSLContext.getInstance(validValues[i], "");
    220                 fail("IllegalArgumentException must be thrown when provider is empty (protocol: "
    221                         .concat(invalidValues[i]).concat(")"));
    222             } catch (IllegalArgumentException e) {
    223             }
    224         }
    225         for (int i = 0; i < validValues.length; i++) {
    226             for (int j = 1; j < invalidValues.length; j++) {
    227                 try {
    228                     SSLContext.getInstance(validValues[i], invalidValues[j]);
    229                     fail("NoSuchProviderException must be thrown (protocol: "
    230                             .concat(invalidValues[i]).concat(" provider: ")
    231                             .concat(invalidValues[j]).concat(")"));
    232                 } catch (NoSuchProviderException e) {
    233                 }
    234             }
    235         }
    236         SSLContext sslC;
    237         for (int i = 0; i < validValues.length; i++) {
    238             sslC = SSLContext.getInstance(validValues[i], mProv.getName());
    239             assertTrue("Not instanceof SSLContext object",
    240                     sslC instanceof SSLContext);
    241             assertEquals("Incorrect protocol", sslC.getProtocol(),
    242                     validValues[i]);
    243             assertEquals("Incorrect provider", sslC.getProvider().getName(),
    244                     mProv.getName());
    245             checkSSLContext(sslC);
    246         }
    247     }
    248 
    249     /**
    250      * Test for <code>getInstance(String protocol, Provider provider)</code>
    251      * method
    252      * Assertions:
    253      * throws NullPointerException when protocol is null;
    254      * throws NoSuchAlgorithmException when protocol is not correct;
    255      * throws IllegalArgumentException when provider is null;
    256      * returns SSLContext object
    257      */
    258     public void testGetInstance03() throws NoSuchAlgorithmException,
    259             IllegalArgumentException, KeyManagementException {
    260         try {
    261             SSLContext.getInstance(null, mProv);
    262             fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
    263         } catch (NoSuchAlgorithmException e) {
    264         } catch (NullPointerException e) {
    265         }
    266         for (int i = 0; i < invalidValues.length; i++) {
    267             try {
    268                 SSLContext.getInstance(invalidValues[i], mProv);
    269                 fail("NoSuchAlgorithmException must be thrown (protocol: "
    270                         .concat(invalidValues[i]).concat(")"));
    271             } catch (NoSuchAlgorithmException e) {
    272             }
    273         }
    274         Provider prov = null;
    275         for (int i = 0; i < validValues.length; i++) {
    276             try {
    277                 SSLContext.getInstance(validValues[i], prov);
    278                 fail("IllegalArgumentException must be thrown when provider is null (protocol: "
    279                         .concat(invalidValues[i]).concat(")"));
    280             } catch (IllegalArgumentException e) {
    281             }
    282         }
    283         SSLContext sslC;
    284         for (int i = 0; i < validValues.length; i++) {
    285             sslC = SSLContext.getInstance(validValues[i], mProv);
    286             assertTrue("Not instanceof SSLContext object",
    287                     sslC instanceof SSLContext);
    288             assertEquals("Incorrect protocol", sslC.getProtocol(),
    289                     validValues[i]);
    290             assertEquals("Incorrect provider", sslC.getProvider(), mProv);
    291             checkSSLContext(sslC);
    292        }
    293     }
    294 
    295     class TManager implements TrustManager {
    296 
    297     }
    298     class KManager implements KeyManager {
    299 
    300     }
    301 }