test/jdk/sun/security/ssl/SSLContextImpl/DefaultEnabledProtocols.java
changeset 50768 68fa3d4026ea
parent 47216 71c04702a3d5
equal deleted inserted replaced
50767:356eaea05bf0 50768:68fa3d4026ea
     1 /*
     1 /*
     2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    29  * @bug 7093640
    29  * @bug 7093640
    30  * @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE
    30  * @summary Enable TLS 1.1 and TLS 1.2 by default in client side of SunJSSE
    31  * @run main/othervm DefaultEnabledProtocols
    31  * @run main/othervm DefaultEnabledProtocols
    32  */
    32  */
    33 
    33 
    34 import javax.net.*;
    34 import java.security.Security;
    35 import javax.net.ssl.*;
       
    36 import java.util.Arrays;
    35 import java.util.Arrays;
    37 import java.security.Security;
    36 import java.util.HashSet;
       
    37 import java.util.Set;
       
    38 
       
    39 import javax.net.SocketFactory;
       
    40 import javax.net.ssl.KeyManager;
       
    41 import javax.net.ssl.SSLContext;
       
    42 import javax.net.ssl.SSLEngine;
       
    43 import javax.net.ssl.SSLParameters;
       
    44 import javax.net.ssl.SSLServerSocket;
       
    45 import javax.net.ssl.SSLServerSocketFactory;
       
    46 import javax.net.ssl.SSLSocket;
       
    47 import javax.net.ssl.TrustManager;
    38 
    48 
    39 public class DefaultEnabledProtocols {
    49 public class DefaultEnabledProtocols {
    40     static enum ContextVersion {
    50     enum ContextVersion {
    41         TLS_CV_01("SSL",
    51         TLS_CV_01("SSL",
    42                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),
    52                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}),
    43         TLS_CV_02("TLS",
    53         TLS_CV_02("TLS",
    44                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),
    54                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}),
    45         TLS_CV_03("SSLv3",
    55         TLS_CV_03("SSLv3",
    46                 new String[] {"SSLv3", "TLSv1"}),
    56                 new String[] {"SSLv3", "TLSv1"}),
    47         TLS_CV_04("TLSv1",
    57         TLS_CV_04("TLSv1",
    48                 new String[] {"SSLv3", "TLSv1"}),
    58                 new String[] {"SSLv3", "TLSv1"}),
    49         TLS_CV_05("TLSv1.1",
    59         TLS_CV_05("TLSv1.1",
    50                 new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),
    60                 new String[] {"SSLv3", "TLSv1", "TLSv1.1"}),
    51         TLS_CV_06("TLSv1.2",
    61         TLS_CV_06("TLSv1.2",
    52                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),
    62                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"}),
    53         TLS_CV_07("Default",
    63         TLS_CV_07("TLSv1.3",
    54                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"});
    64                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"}),
       
    65         TLS_CV_08("Default",
       
    66                 new String[] {"SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"});
    55 
    67 
    56         final String contextVersion;
    68         final String contextVersion;
    57         final String[] enabledProtocols;
    69         final String[] enabledProtocols;
    58         final static String[] supportedProtocols = new String[] {
    70         final static String[] supportedProtocols = new String[] {
    59                 "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"};
    71                 "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2", "TLSv1.3"};
    60 
    72 
    61         ContextVersion(String contextVersion, String[] enabledProtocols) {
    73         ContextVersion(String contextVersion, String[] enabledProtocols) {
    62             this.contextVersion = contextVersion;
    74             this.contextVersion = contextVersion;
    63             this.enabledProtocols = enabledProtocols;
    75             this.enabledProtocols = enabledProtocols;
    64         }
    76         }
    69         if (target.length == 0) {
    81         if (target.length == 0) {
    70             System.out.println("\tError: No protocols");
    82             System.out.println("\tError: No protocols");
    71             success = false;
    83             success = false;
    72         }
    84         }
    73 
    85 
    74         if (!Arrays.equals(target, expected)) {
    86         if (!protocolEquals(target, expected)) {
    75             System.out.println("\tError: Expected to get protocols " +
    87             System.out.println("\tError: Expected to get protocols " +
    76                     Arrays.toString(expected));
    88                     Arrays.toString(expected));
    77             System.out.println("\tError: The actual protocols " +
       
    78                     Arrays.toString(target));
       
    79             success = false;
    89             success = false;
    80         }
    90         }
       
    91         System.out.println("\t  Protocols found " + Arrays.toString(target));
    81 
    92 
    82         return success;
    93         return success;
       
    94     }
       
    95 
       
    96     private static boolean protocolEquals(
       
    97             String[] actualProtocols,
       
    98             String[] expectedProtocols) {
       
    99         if (actualProtocols.length != expectedProtocols.length) {
       
   100             return false;
       
   101         }
       
   102 
       
   103         Set<String> set = new HashSet<>(Arrays.asList(expectedProtocols));
       
   104         for (String actual : actualProtocols) {
       
   105             if (set.add(actual)) {
       
   106                 return false;
       
   107             }
       
   108         }
       
   109 
       
   110         return true;
    83     }
   111     }
    84 
   112 
    85     private static boolean checkCipherSuites(String[] target) {
   113     private static boolean checkCipherSuites(String[] target) {
    86         boolean success = true;
   114         boolean success = true;
    87         if (target.length == 0) {
   115         if (target.length == 0) {