1/*
2 * Copyright (c) 2003, 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 4635454 6208022
27 * @summary Check pluggability of SSLSocketFactory and
28 * SSLServerSocketFactory classes.
29 * @run main/othervm CheckSockFacExport1
30 *
31 *     SunJSSE does not support dynamic system properties, no way to re-use
32 *     system properties in samevm/agentvm mode.
33 */
34
35import java.util.*;
36
37import java.security.*;
38import java.net.*;
39import javax.net.ssl.*;
40
41public class CheckSockFacExport1 {
42
43    public static void main(String argv[]) throws Exception {
44        // reserve the security properties
45        String reservedSFacAlg =
46            Security.getProperty("ssl.SocketFactory.provider");
47        String reservedSSFacAlg =
48            Security.getProperty("ssl.ServerSocketFactory.provider");
49
50        try {
51            Security.setProperty("ssl.SocketFactory.provider",
52                                 "MySSLSocketFacImpl");
53            MySSLSocketFacImpl.useCustomCipherSuites();
54            Security.setProperty("ssl.ServerSocketFactory.provider",
55                "MySSLServerSocketFacImpl");
56            MySSLServerSocketFacImpl.useCustomCipherSuites();
57
58            String[] supportedCS = null;
59            for (int i = 0; i < 2; i++) {
60                switch (i) {
61                case 0:
62                    System.out.println("Testing SSLSocketFactory:");
63                    SSLSocketFactory sf = (SSLSocketFactory)
64                        SSLSocketFactory.getDefault();
65                    supportedCS = sf.getSupportedCipherSuites();
66                    break;
67                case 1:
68                    System.out.println("Testing SSLServerSocketFactory:");
69                    SSLServerSocketFactory ssf = (SSLServerSocketFactory)
70                        SSLServerSocketFactory.getDefault();
71                    supportedCS = ssf.getSupportedCipherSuites();
72                    break;
73                default:
74                    throw new Exception("Internal Test Error");
75                }
76                System.out.println(Arrays.asList(supportedCS));
77                if (supportedCS.length == 0) {
78                    throw new Exception("supported ciphersuites are empty");
79                }
80            }
81            System.out.println("Test Passed");
82        } finally {
83            // restore the security properties
84            if (reservedSFacAlg == null) {
85                reservedSFacAlg = "";
86            }
87
88            if (reservedSSFacAlg == null) {
89                reservedSSFacAlg = "";
90            }
91            Security.setProperty("ssl.SocketFactory.provider",
92                                                            reservedSFacAlg);
93            Security.setProperty("ssl.ServerSocketFactory.provider",
94                                                            reservedSSFacAlg);
95        }
96    }
97}
98