1/*
2 * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 5070632
27 * @summary Default SSLSockeFactory override createSocket() now
28 * @run main/othervm Fix5070632
29 *
30 *     SunJSSE does not support dynamic system properties, no way to re-use
31 *     system properties in samevm/agentvm mode.
32 * @author Weijun Wang
33 */
34
35import javax.net.ssl.SSLSocketFactory;
36import java.net.SocketException;
37import javax.net.SocketFactory;
38import java.security.*;
39
40public class Fix5070632 {
41    public static void main(String[] args) throws Exception {
42        // reserve the security properties
43        String reservedSFacProvider =
44            Security.getProperty("ssl.SocketFactory.provider");
45
46        // use a non-existing provider so that the DefaultSSLSocketFactory
47        // will be used, and then test against it.
48
49        Security.setProperty("ssl.SocketFactory.provider", "foo.NonExistant");
50        SSLSocketFactory fac = (SSLSocketFactory)SSLSocketFactory.getDefault();
51        try {
52            fac.createSocket();
53        } catch(SocketException se) {
54            // if exception caught, then it's ok
55            System.out.println("Throw SocketException");
56            se.printStackTrace();
57            return;
58        } finally {
59            // restore the security properties
60            if (reservedSFacProvider == null) {
61                reservedSFacProvider = "";
62            }
63            Security.setProperty("ssl.SocketFactory.provider",
64                                                reservedSFacProvider);
65        }
66
67        // if not caught, or other exception caught, then it's error
68        throw new Exception("should throw SocketException");
69    }
70}
71