CheckSSLContextExport.java revision 9372:171791e63397
1/*
2 * Copyright (c) 2002, 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 SSLContext class.
28 */
29import java.security.*;
30import java.net.*;
31import javax.net.ssl.*;
32
33public class CheckSSLContextExport extends Provider {
34    private static String info = "test provider for JSSE pluggability";
35
36    public CheckSSLContextExport(String protocols[]) {
37        super("TestJSSEPluggability", 1.0, info);
38        for (int i=0; i<protocols.length; i++) {
39            put("SSLContext." + protocols[i], "MySSLContextImpl");
40        }
41    }
42
43    public static void test(String protocol) throws Exception {
44        SSLContext mySSLContext = SSLContext.getInstance(protocol);
45
46        String providerName = mySSLContext.getProvider().getName();
47        if (!providerName.equals("TestJSSEPluggability")) {
48            System.out.println(providerName + "'s SSLContext is used");
49            throw new Exception("...used the wrong provider: " + providerName);
50        }
51        for (int i = 0; i < 2; i++) {
52            boolean standardCiphers = true;
53
54            switch (i) {
55            case 0:
56                // non-standard cipher suites
57                standardCiphers = false;
58                MySSLContextImpl.useCustomCipherSuites();
59                break;
60            case 1:
61                standardCiphers = true;
62                MySSLContextImpl.useStandardCipherSuites();
63                break;
64            default:
65                throw new Exception("Internal Test Error!");
66            }
67            System.out.println("Testing with " +
68                (standardCiphers ? "standard" : "custom") + " cipher suites");
69            for (int j = 0; j < 4; j++) {
70                String clsName = null;
71                try {
72                    switch (j) {
73                    case 0:
74                        SSLSocketFactory sf = mySSLContext.getSocketFactory();
75                        clsName = sf.getClass().getName();
76                        break;
77                    case 1:
78                        SSLServerSocketFactory ssf =
79                            mySSLContext.getServerSocketFactory();
80                        clsName = ssf.getClass().getName();
81                        break;
82                    case 2:
83                        SSLEngine se = mySSLContext.createSSLEngine();
84                        clsName = se.getClass().getName();
85                        break;
86                    case 3:
87                        SSLEngine se2 = mySSLContext.createSSLEngine(null, 0);
88                        clsName = se2.getClass().getName();
89                        break;
90                    default:
91                        throw new Exception("Internal Test Error!");
92                    }
93                    if (!clsName.startsWith("MySSL")) {
94                        throw new Exception("test#" + j +
95                                             ": wrong impl is used");
96                    } else {
97                        System.out.println("test#" + j +
98                                           ": accepted valid impl");
99                    }
100                } catch (RuntimeException re) {
101                    // pass it on
102                    throw re;
103                }
104            }
105        }
106    }
107
108    public static void main(String[] argv) throws Exception {
109        String protocols[] = { "SSL", "TLS" };
110        Provider extraProvider = new CheckSSLContextExport(protocols);
111        Security.insertProviderAt(extraProvider, 1);
112        try {
113            for (int i = 0; i < protocols.length; i++) {
114                System.out.println("Testing " + protocols[i] + "'s SSLContext");
115                test(protocols[i]);
116            }
117            System.out.println("Test Passed");
118        } finally {
119            Security.removeProvider(extraProvider.getName());
120        }
121    }
122}
123