jdk/test/sun/security/ssl/sanity/pluggability/CheckSSLContextExport.java
author weijun
Fri, 12 Aug 2011 12:26:31 +0800
changeset 10328 06c93c42bca0
parent 5506 202f599c92aa
child 14342 8435a30053c1
permissions -rw-r--r--
7055363: jdk_security3 test target cleanup Reviewed-by: alanb, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4635454 6208022
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Check pluggability of SSLContext class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class CheckSSLContextExport extends Provider {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static String info = "test provider for JSSE pluggability";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public CheckSSLContextExport(String protocols[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        super("TestJSSEPluggability", 1.0, info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        for (int i=0; i<protocols.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
            put("SSLContext." + protocols[i], "MySSLContextImpl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public static void test(String protocol) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        SSLContext mySSLContext = SSLContext.getInstance(protocol);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        String providerName = mySSLContext.getProvider().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (!providerName.equals("TestJSSEPluggability")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            System.out.println(providerName + "'s SSLContext is used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            throw new Exception("...used the wrong provider: " + providerName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        for (int i = 0; i < 2; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            boolean standardCiphers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            switch (i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                // non-standard cipher suites
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                standardCiphers = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                MySSLContextImpl.useCustomCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                standardCiphers = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                MySSLContextImpl.useStandardCipherSuites();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                throw new Exception("Internal Test Error!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            }
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    67
            System.out.println("Testing with " +
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
    68
                (standardCiphers ? "standard" : "custom") + " cipher suites");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            for (int j = 0; j < 4; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                String clsName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    switch (j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                        SSLSocketFactory sf = mySSLContext.getSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                        clsName = sf.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                        SSLServerSocketFactory ssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                            mySSLContext.getServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                        clsName = ssf.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                    case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                        SSLEngine se = mySSLContext.createSSLEngine();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                        clsName = se.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                        SSLEngine se2 = mySSLContext.createSSLEngine(null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                        clsName = se2.getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        throw new Exception("Internal Test Error!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    if (!clsName.startsWith("MySSL")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                        throw new Exception("test#" + j +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                                             ": wrong impl is used");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                        System.out.println("test#" + j +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                           ": accepted valid impl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                } catch (RuntimeException re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                    // pass it on
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                    throw re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void main(String[] argv) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        String protocols[] = { "SSL", "TLS" };
10328
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   110
        Provider extraProvider = new CheckSSLContextExport(protocols);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   111
        Security.insertProviderAt(extraProvider, 1);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   112
        try {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   113
            for (int i = 0; i < protocols.length; i++) {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   114
                System.out.println("Testing " + protocols[i] + "'s SSLContext");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   115
                test(protocols[i]);
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   116
            }
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   117
            System.out.println("Test Passed");
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   118
        } finally {
06c93c42bca0 7055363: jdk_security3 test target cleanup
weijun
parents: 5506
diff changeset
   119
            Security.removeProvider(extraProvider.getName());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
}