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 /**
     19 * @author Boris V. Kuznetsov
     20 */
     21 
     22 package javax.net.ssl;
     23 
     24 import java.io.IOException;
     25 import java.security.Security;
     26 import java.net.SocketException;
     27 
     28 import junit.framework.TestCase;
     29 
     30 
     31 /**
     32  * Tests for <code>SSLSocketFactory</code> class methods.
     33  *
     34  */
     35 public class SSLSocketFactoryTest extends TestCase {
     36 
     37     private SSLSocketFactory customSocketFactory;
     38 
     39     /*
     40      * @see TestCase#setUp()
     41      */
     42     @Override
     43     protected void setUp() throws Exception {
     44         super.setUp();
     45         String defaultName = Security.getProperty("ssl.SocketFactory.provider");
     46         if (defaultName != null) {
     47             try {
     48                 customSocketFactory = (SSLSocketFactory) Class.forName(
     49                         defaultName, true, ClassLoader.getSystemClassLoader())
     50                         .newInstance();
     51              } catch (Exception e) {
     52              }
     53         }
     54         if (customSocketFactory == null) {
     55             SSLContext context = DefaultSSLContext.getContext();
     56             if (context != null) {
     57                 customSocketFactory = context.getSocketFactory();
     58             }
     59         }
     60     }
     61 
     62     /*
     63      * @see TestCase#tearDown()
     64      */
     65     @Override
     66     protected void tearDown() throws Exception {
     67         super.tearDown();
     68     }
     69 
     70     public final void testGetDefault() {
     71         SSLSocketFactory factory = (SSLSocketFactory)SSLSocketFactory.getDefault();
     72         if (customSocketFactory != null) {
     73             if (!factory.getClass().getName().equals(customSocketFactory.getClass().getName())) {
     74                 fail("incorrect instance: " + factory.getClass()+
     75                         " expected: " + customSocketFactory.getClass().getName());
     76             }
     77         } else {
     78             if (!(factory instanceof DefaultSSLSocketFactory)) {
     79                 fail("incorrect instance " + factory.getClass());
     80             }
     81             if (factory.getDefaultCipherSuites().length != 0) {
     82                 fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
     83             }
     84             if (factory.getSupportedCipherSuites().length != 0) {
     85                 fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
     86             }
     87             try {
     88                 factory.createSocket("localhost", 2021);
     89                 fail("No expected SocketException");
     90             } catch (SocketException e) {
     91             } catch (IOException e) {
     92                 fail(e.toString());
     93             }
     94         }
     95     }
     96 
     97 }
     98