test/jdk/sun/security/ssl/SSLContextImpl/DefaultDTLSEnabledProtocols.java
changeset 50768 68fa3d4026ea
child 51771 1f805481d8de
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
       
     1 /*
       
     2  * Copyright (c) 2013, 2018, 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 // SunJSSE does not support dynamic system properties, no way to re-use
       
    25 // system properties in samevm/agentvm mode.
       
    26 
       
    27 /*
       
    28  * @test
       
    29  * @summary Test jdk.tls.client.protocols with DTLS
       
    30  * @run main/othervm DefaultDTLSEnabledProtocols
       
    31  */
       
    32 
       
    33 import java.security.Security;
       
    34 import java.util.Arrays;
       
    35 import java.util.HashSet;
       
    36 import java.util.Set;
       
    37 
       
    38 import javax.net.SocketFactory;
       
    39 import javax.net.ssl.KeyManager;
       
    40 import javax.net.ssl.SSLContext;
       
    41 import javax.net.ssl.SSLEngine;
       
    42 import javax.net.ssl.SSLParameters;
       
    43 import javax.net.ssl.SSLServerSocket;
       
    44 import javax.net.ssl.SSLServerSocketFactory;
       
    45 import javax.net.ssl.SSLSocket;
       
    46 import javax.net.ssl.TrustManager;
       
    47 
       
    48 public class DefaultDTLSEnabledProtocols {
       
    49     static enum ContextVersion {
       
    50         TLS_CV_01("DTLS",
       
    51                 new String[] {"DTLSv1.0", "DTLSv1.2"}),
       
    52         TLS_CV_02("DTLSv1.0",
       
    53                 new String[] {"DTLSv1.0"}),
       
    54         TLS_CV_03("DTLSv1.2",
       
    55                 new String[] {"DTLSv1.0", "DTLSv1.2"});
       
    56 
       
    57         final String contextVersion;
       
    58         final String[] enabledProtocols;
       
    59         final static String[] supportedProtocols = new String[] {
       
    60                 "DTLSv1.0", "DTLSv1.2"};
       
    61 
       
    62         ContextVersion(String contextVersion, String[] enabledProtocols) {
       
    63             this.contextVersion = contextVersion;
       
    64             this.enabledProtocols = enabledProtocols;
       
    65         }
       
    66     }
       
    67 
       
    68     private static boolean checkProtocols(String[] target, String[] expected) {
       
    69         boolean success = true;
       
    70         if (target.length == 0) {
       
    71             System.out.println("\tError: No protocols");
       
    72             success = false;
       
    73         }
       
    74 
       
    75         if (!protocolEquals(target, expected)) {
       
    76             System.out.println("\tError: Expected to get protocols " +
       
    77                     Arrays.toString(expected));
       
    78             success = false;
       
    79         }
       
    80         System.out.println("\t  Protocols found " + Arrays.toString(target));
       
    81 
       
    82         return success;
       
    83     }
       
    84 
       
    85     private static boolean protocolEquals(
       
    86             String[] actualProtocols,
       
    87             String[] expectedProtocols) {
       
    88         if (actualProtocols.length != expectedProtocols.length) {
       
    89             return false;
       
    90         }
       
    91 
       
    92         Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));
       
    93         for (String actual : actualProtocols) {
       
    94             if (set.add(actual)) {
       
    95                 return false;
       
    96             }
       
    97         }
       
    98 
       
    99         return true;
       
   100     }
       
   101 
       
   102     private static boolean checkCipherSuites(String[] target) {
       
   103         boolean success = true;
       
   104         if (target.length == 0) {
       
   105             System.out.println("\tError: No cipher suites");
       
   106             success = false;
       
   107         }
       
   108 
       
   109         return success;
       
   110     }
       
   111 
       
   112     public static void main(String[] args) throws Exception {
       
   113         // reset the security property to make sure that the algorithms
       
   114         // and keys used in this test are not disabled.
       
   115         Security.setProperty("jdk.tls.disabledAlgorithms", "");
       
   116 
       
   117         boolean failed = false;
       
   118         for (ContextVersion cv : ContextVersion.values()) {
       
   119             System.out.println("Checking SSLContext of " + cv.contextVersion);
       
   120             SSLContext context = SSLContext.getInstance(cv.contextVersion);
       
   121 
       
   122             // Default SSLContext is initialized automatically.
       
   123             if (!cv.contextVersion.equals("Default")) {
       
   124                 // Use default TK, KM and random.
       
   125                 context.init((KeyManager[])null, (TrustManager[])null, null);
       
   126             }
       
   127 
       
   128             //
       
   129             // Check SSLContext
       
   130             //
       
   131             // Check default SSLParameters of SSLContext
       
   132             System.out.println("\tChecking default SSLParameters");
       
   133             SSLParameters parameters = context.getDefaultSSLParameters();
       
   134 
       
   135             String[] protocols = parameters.getProtocols();
       
   136             failed |= !checkProtocols(protocols, cv.enabledProtocols);
       
   137 
       
   138             String[] ciphers = parameters.getCipherSuites();
       
   139             failed |= !checkCipherSuites(ciphers);
       
   140 
       
   141             // Check supported SSLParameters of SSLContext
       
   142             System.out.println("\tChecking supported SSLParameters");
       
   143             parameters = context.getSupportedSSLParameters();
       
   144 
       
   145             protocols = parameters.getProtocols();
       
   146             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   147 
       
   148             ciphers = parameters.getCipherSuites();
       
   149             failed |= !checkCipherSuites(ciphers);
       
   150 
       
   151             //
       
   152             // Check SSLEngine
       
   153             //
       
   154             // Check SSLParameters of SSLEngine
       
   155             System.out.println();
       
   156             System.out.println("\tChecking SSLEngine of this SSLContext");
       
   157             System.out.println("\tChecking SSLEngine.getSSLParameters()");
       
   158             SSLEngine engine = context.createSSLEngine();
       
   159             engine.setUseClientMode(true);
       
   160             parameters = engine.getSSLParameters();
       
   161 
       
   162             protocols = parameters.getProtocols();
       
   163             failed |= !checkProtocols(protocols, cv.enabledProtocols);
       
   164 
       
   165             ciphers = parameters.getCipherSuites();
       
   166             failed |= !checkCipherSuites(ciphers);
       
   167 
       
   168             System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
       
   169             protocols = engine.getEnabledProtocols();
       
   170             failed |= !checkProtocols(protocols, cv.enabledProtocols);
       
   171 
       
   172             System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
       
   173             ciphers = engine.getEnabledCipherSuites();
       
   174             failed |= !checkCipherSuites(ciphers);
       
   175 
       
   176             System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
       
   177             protocols = engine.getSupportedProtocols();
       
   178             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   179 
       
   180             System.out.println(
       
   181                     "\tChecking SSLEngine.getSupportedCipherSuites()");
       
   182             ciphers = engine.getSupportedCipherSuites();
       
   183             failed |= !checkCipherSuites(ciphers);
       
   184 
       
   185             //
       
   186             // Check SSLSocket
       
   187             //
       
   188             // Check SSLParameters of SSLSocket
       
   189             System.out.println();
       
   190             System.out.println("\tChecking SSLSocket of this SSLContext");
       
   191             System.out.println("\tChecking SSLSocket.getSSLParameters()");
       
   192             SocketFactory fac = context.getSocketFactory();
       
   193             SSLSocket socket = (SSLSocket)fac.createSocket();
       
   194             parameters = socket.getSSLParameters();
       
   195 
       
   196             protocols = parameters.getProtocols();
       
   197             failed |= !checkProtocols(protocols, cv.enabledProtocols);
       
   198 
       
   199             ciphers = parameters.getCipherSuites();
       
   200             failed |= !checkCipherSuites(ciphers);
       
   201 
       
   202             System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
       
   203             protocols = socket.getEnabledProtocols();
       
   204             failed |= !checkProtocols(protocols, cv.enabledProtocols);
       
   205 
       
   206             System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
       
   207             ciphers = socket.getEnabledCipherSuites();
       
   208             failed |= !checkCipherSuites(ciphers);
       
   209 
       
   210             System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
       
   211             protocols = socket.getSupportedProtocols();
       
   212             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   213 
       
   214             System.out.println(
       
   215                     "\tChecking SSLEngine.getSupportedCipherSuites()");
       
   216             ciphers = socket.getSupportedCipherSuites();
       
   217             failed |= !checkCipherSuites(ciphers);
       
   218 
       
   219             //
       
   220             // Check SSLServerSocket
       
   221             //
       
   222             // Check SSLParameters of SSLServerSocket
       
   223             System.out.println();
       
   224             System.out.println("\tChecking SSLServerSocket of this SSLContext");
       
   225             System.out.println("\tChecking SSLServerSocket.getSSLParameters()");
       
   226             SSLServerSocketFactory sf = context.getServerSocketFactory();
       
   227             SSLServerSocket ssocket = (SSLServerSocket)sf.createServerSocket();
       
   228             parameters = ssocket.getSSLParameters();
       
   229 
       
   230             protocols = parameters.getProtocols();
       
   231             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   232 
       
   233             ciphers = parameters.getCipherSuites();
       
   234             failed |= !checkCipherSuites(ciphers);
       
   235 
       
   236             System.out.println("\tChecking SSLEngine.getEnabledProtocols()");
       
   237             protocols = ssocket.getEnabledProtocols();
       
   238             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   239 
       
   240             System.out.println("\tChecking SSLEngine.getEnabledCipherSuites()");
       
   241             ciphers = ssocket.getEnabledCipherSuites();
       
   242             failed |= !checkCipherSuites(ciphers);
       
   243 
       
   244             System.out.println("\tChecking SSLEngine.getSupportedProtocols()");
       
   245             protocols = ssocket.getSupportedProtocols();
       
   246             failed |= !checkProtocols(protocols, cv.supportedProtocols);
       
   247 
       
   248             System.out.println(
       
   249                     "\tChecking SSLEngine.getSupportedCipherSuites()");
       
   250             ciphers = ssocket.getSupportedCipherSuites();
       
   251             failed |= !checkCipherSuites(ciphers);
       
   252         }
       
   253 
       
   254         if (failed) {
       
   255             throw new Exception("Run into problems, see log for more details");
       
   256         } else {
       
   257             System.out.println("\t... Success");
       
   258         }
       
   259     }
       
   260 }