jdk/test/javax/net/ssl/GetInstance.java
changeset 23052 241885315119
parent 8791 f5106bbf577d
child 30820 0d4717a011d3
equal deleted inserted replaced
23051:501d8479f798 23052:241885315119
       
     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 4898428 7022855
       
    27  * @summary verify getInstance() works using Provider.getService()
       
    28  *          Export "PKIX" as the standard algorithm name of KeyManagerFactory
       
    29  * @author Andreas Sterbenz
       
    30  */
       
    31 
       
    32 import java.security.*;
       
    33 
       
    34 import javax.net.ssl.*;
       
    35 
       
    36 public class GetInstance {
       
    37 
       
    38     private static void same(Provider p1, Provider p2) throws Exception {
       
    39         if (p1 != p2) {
       
    40             throw new Exception("not same object");
       
    41         }
       
    42     }
       
    43 
       
    44     public static void main(String[] args) throws Exception {
       
    45         long start = System.currentTimeMillis();
       
    46 
       
    47         Provider p = Security.getProvider("SunJSSE");
       
    48 
       
    49         SSLContext context;
       
    50         context = SSLContext.getInstance("SSL");
       
    51         same(p, context.getProvider());
       
    52         context = SSLContext.getInstance("SSL", "SunJSSE");
       
    53         same(p, context.getProvider());
       
    54         context = SSLContext.getInstance("SSL", p);
       
    55         same(p, context.getProvider());
       
    56 
       
    57         KeyManagerFactory kmf;
       
    58         kmf = KeyManagerFactory.getInstance("SunX509");
       
    59         same(p, kmf.getProvider());
       
    60         kmf = KeyManagerFactory.getInstance("SunX509", "SunJSSE");
       
    61         same(p, kmf.getProvider());
       
    62         kmf = KeyManagerFactory.getInstance("SunX509", p);
       
    63         same(p, kmf.getProvider());
       
    64 
       
    65         kmf = KeyManagerFactory.getInstance("NewSunX509");
       
    66         same(p, kmf.getProvider());
       
    67         kmf = KeyManagerFactory.getInstance("NewSunX509", "SunJSSE");
       
    68         same(p, kmf.getProvider());
       
    69         kmf = KeyManagerFactory.getInstance("NewSunX509", p);
       
    70         same(p, kmf.getProvider());
       
    71 
       
    72         kmf = KeyManagerFactory.getInstance("PKIX");
       
    73         same(p, kmf.getProvider());
       
    74         kmf = KeyManagerFactory.getInstance("PKIX", "SunJSSE");
       
    75         same(p, kmf.getProvider());
       
    76         kmf = KeyManagerFactory.getInstance("PKIX", p);
       
    77         same(p, kmf.getProvider());
       
    78 
       
    79         TrustManagerFactory tmf;
       
    80         tmf = TrustManagerFactory.getInstance("SunX509");
       
    81         same(p, tmf.getProvider());
       
    82         tmf = TrustManagerFactory.getInstance("SunX509", "SunJSSE");
       
    83         same(p, tmf.getProvider());
       
    84         tmf = TrustManagerFactory.getInstance("SunX509", p);
       
    85         same(p, tmf.getProvider());
       
    86 
       
    87         tmf = TrustManagerFactory.getInstance("PKIX");
       
    88         same(p, tmf.getProvider());
       
    89         tmf = TrustManagerFactory.getInstance("PKIX", "SunJSSE");
       
    90         same(p, tmf.getProvider());
       
    91         tmf = TrustManagerFactory.getInstance("PKIX", p);
       
    92         same(p, tmf.getProvider());
       
    93 
       
    94         tmf = TrustManagerFactory.getInstance("SunPKIX");
       
    95         same(p, tmf.getProvider());
       
    96         tmf = TrustManagerFactory.getInstance("SunPKIX", "SunJSSE");
       
    97         same(p, tmf.getProvider());
       
    98         tmf = TrustManagerFactory.getInstance("SunPKIX", p);
       
    99         same(p, tmf.getProvider());
       
   100 
       
   101         tmf = TrustManagerFactory.getInstance("X509");
       
   102         same(p, tmf.getProvider());
       
   103         tmf = TrustManagerFactory.getInstance("X509", "SunJSSE");
       
   104         same(p, tmf.getProvider());
       
   105         tmf = TrustManagerFactory.getInstance("X509", p);
       
   106         same(p, tmf.getProvider());
       
   107 
       
   108         tmf = TrustManagerFactory.getInstance("X.509");
       
   109         same(p, tmf.getProvider());
       
   110         tmf = TrustManagerFactory.getInstance("X.509", "SunJSSE");
       
   111         same(p, tmf.getProvider());
       
   112         tmf = TrustManagerFactory.getInstance("X.509", p);
       
   113         same(p, tmf.getProvider());
       
   114 
       
   115         testComSun();
       
   116 
       
   117         long stop = System.currentTimeMillis();
       
   118         System.out.println("Done (" + (stop - start) + " ms).");
       
   119     }
       
   120 
       
   121     private static void testComSun() throws Exception {
       
   122         Provider p = Security.getProvider("SunJSSE");
       
   123 
       
   124         com.sun.net.ssl.SSLContext context;
       
   125         context = com.sun.net.ssl.SSLContext.getInstance("SSL");
       
   126         same(p, context.getProvider());
       
   127         context = com.sun.net.ssl.SSLContext.getInstance("SSL", "SunJSSE");
       
   128         same(p, context.getProvider());
       
   129         context = com.sun.net.ssl.SSLContext.getInstance("SSL", p);
       
   130         same(p, context.getProvider());
       
   131 
       
   132         com.sun.net.ssl.KeyManagerFactory kmf;
       
   133         kmf = com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509");
       
   134         same(p, kmf.getProvider());
       
   135         kmf = com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509", "SunJSSE");
       
   136         same(p, kmf.getProvider());
       
   137         kmf = com.sun.net.ssl.KeyManagerFactory.getInstance("SunX509", p);
       
   138         same(p, kmf.getProvider());
       
   139 
       
   140         com.sun.net.ssl.TrustManagerFactory tmf;
       
   141         tmf = com.sun.net.ssl.TrustManagerFactory.getInstance("SunX509");
       
   142         same(p, tmf.getProvider());
       
   143         tmf = com.sun.net.ssl.TrustManagerFactory.getInstance("SunX509", "SunJSSE");
       
   144         same(p, tmf.getProvider());
       
   145         tmf = com.sun.net.ssl.TrustManagerFactory.getInstance("SunX509", p);
       
   146         same(p, tmf.getProvider());
       
   147     }
       
   148 
       
   149 }