• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 public class SSLSocketFactoryTest extends TestCase {
35 
36     private SSLSocketFactory customSocketFactory;
37 
38     /*
39     * @see TestCase#setUp()
40     */
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44         String defaultName = Security.getProperty("ssl.SocketFactory.provider");
45         if (defaultName != null) {
46             try {
47                 customSocketFactory = (SSLSocketFactory) Class.forName(
48                         defaultName, true, ClassLoader.getSystemClassLoader())
49                         .newInstance();
50             } catch (Exception e) {
51             }
52         }
53         if (customSocketFactory == null) {
54             SSLContext context = DefaultSSLContext.getContext();
55             if (context != null) {
56                 customSocketFactory = context.getSocketFactory();
57             }
58         }
59     }
60 
61     /*
62      * @see TestCase#tearDown()
63      */
64     @Override
tearDown()65     protected void tearDown() throws Exception {
66         super.tearDown();
67     }
68 
testGetDefault()69     public final void testGetDefault() {
70         SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
71         if (customSocketFactory != null) {
72             if (!factory.getClass().getName().equals(customSocketFactory.getClass().getName())) {
73                 fail("incorrect instance: " + factory.getClass() +
74                         " expected: " + customSocketFactory.getClass().getName());
75             }
76         } else {
77             if (!(factory instanceof DefaultSSLSocketFactory)) {
78                 fail("incorrect instance " + factory.getClass());
79             }
80             if (factory.getDefaultCipherSuites().length != 0) {
81                 fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
82             }
83             if (factory.getSupportedCipherSuites().length != 0) {
84                 fail("incorrect result: DefaultSSLSocketFactory.getDefaultCipherSuites()");
85             }
86             try {
87                 factory.createSocket("localhost", 2021);
88                 fail("No expected SocketException");
89             } catch (SocketException e) {
90             } catch (IOException e) {
91                 fail(e.toString());
92             }
93         }
94     }
95 
96 }
97