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