jdk/test/javax/net/ssl/TLS/TLSClientPropertyTest.java
author wetmore
Fri, 23 Jan 2015 10:31:15 -0800
changeset 28570 b00a1e9a9238
parent 27939 0a39982a7c6a
child 40949 be7a612613ae
permissions -rw-r--r--
8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210 Reviewed-by: xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     1
/*
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
     2
 * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     4
 *
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     8
 *
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    13
 * accompanied this code).
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    14
 *
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    18
 *
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    21
 * questions.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    22
 */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    23
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    24
import java.security.KeyManagementException;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    25
import java.security.NoSuchAlgorithmException;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    26
import java.util.Arrays;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    27
import java.util.List;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    28
import javax.net.ssl.SSLContext;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    29
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    30
/*
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    31
 * @test
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    32
 * @bug 8049432 8069038
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    33
 * @summary New tests for TLS property jdk.tls.client.protocols
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    34
 * @summary javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    35
 *     updated for JDK-8061210
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    36
 * @run main/othervm TLSClientPropertyTest NoProperty
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    37
 * @run main/othervm TLSClientPropertyTest SSLv3
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    38
 * @run main/othervm TLSClientPropertyTest TLSv1
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    39
 * @run main/othervm TLSClientPropertyTest TLSv11
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    40
 * @run main/othervm TLSClientPropertyTest TLSv12
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    41
 * @run main/othervm TLSClientPropertyTest WrongProperty
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    42
 */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    43
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    44
/**
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    45
 * Sets the property jdk.tls.client.protocols to one of this protocols:
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    46
 * SSLv3,TLSv1,TLSv1.1,TLSv1.2 and TLSV(invalid) or removes this
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    47
 * property (if any),then validates the default, supported and current
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    48
 * protocols in the SSLContext.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    49
 */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    50
public class TLSClientPropertyTest {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    51
    private final String[] expectedSupportedProtos = new String[] {
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    52
            "SSLv2Hello", "SSLv3", "TLSv1", "TLSv1.1", "TLSv1.2"
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    53
    };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    54
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    56
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    57
        if (args.length < 1) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    58
            throw new RuntimeException(
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    59
                    "Incorrect arguments,expected arguments: testCase");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    60
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    61
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    62
        String[] expectedDefaultProtos;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    63
        String testCase = args[0];
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    64
        String contextProtocol;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    65
        switch (testCase) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    66
        case "NoProperty":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    67
            if (System.getProperty("jdk.tls.client.protocols") != null) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    68
                System.getProperties().remove("jdk.tls.client.protocols");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    69
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    70
            contextProtocol = null;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    71
            expectedDefaultProtos = new String[] {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    72
                    "TLSv1", "TLSv1.1", "TLSv1.2"
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    73
            };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    74
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    75
        case "SSLv3":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    76
            contextProtocol = "SSLv3";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    77
            expectedDefaultProtos = new String[] {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    78
            };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    79
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    80
        case "TLSv1":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    81
            contextProtocol = "TLSv1";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    82
            expectedDefaultProtos = new String[] {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    83
                    "TLSv1"
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    84
            };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    85
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    86
        case "TLSv11":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    87
            contextProtocol = "TLSv1.1";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    88
            expectedDefaultProtos = new String[] {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    89
                    "TLSv1", "TLSv1.1"
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    90
            };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    91
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    92
        case "TLSv12":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    93
            contextProtocol = "TLSv1.2";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    94
            expectedDefaultProtos = new String[] {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
    95
                    "TLSv1", "TLSv1.1", "TLSv1.2"
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    96
            };
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    97
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    98
        case "WrongProperty":
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
    99
            expectedDefaultProtos = new String[] {};
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   100
            contextProtocol = "TLSV";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   101
            break;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   102
        default:
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   103
            throw new RuntimeException("test case is wrong");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   104
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   105
        if (contextProtocol != null) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   106
            System.setProperty("jdk.tls.client.protocols", contextProtocol);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   107
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   108
        try {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   109
            TLSClientPropertyTest test = new TLSClientPropertyTest();
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   110
            test.test(contextProtocol, expectedDefaultProtos);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   111
            if (testCase.equals("WrongProperty")) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   112
                throw new RuntimeException(
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   113
                        "Test failed: NoSuchAlgorithmException " +
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   114
                        "is expected when input wrong protocol");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   115
            } else {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   116
                System.out.println("Test " + contextProtocol + " passed");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   117
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   118
        } catch (NoSuchAlgorithmException nsae) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   119
            if (testCase.equals("WrongProperty")) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   120
                System.out.println("NoSuchAlgorithmException is expected,"
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   121
                        + contextProtocol + " test passed");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   122
            } else {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   123
                throw nsae;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   124
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   125
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   126
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   127
    }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   128
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   129
    /**
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   130
     * The parameter passed is the user enforced protocol. Does not catch
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   131
     * NoSuchAlgorithmException, WrongProperty test will use it.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   132
     */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   133
    public void test(String expectedContextProto,
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   134
            String[] expectedDefaultProtos) throws NoSuchAlgorithmException {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   135
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   136
        SSLContext context = null;
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   137
        try {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   138
            if (expectedContextProto != null) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   139
                context = SSLContext.getInstance(expectedContextProto);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   140
                context.init(null, null, null);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   141
            } else {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   142
                context = SSLContext.getDefault();
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   143
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   144
            printContextDetails(context);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   145
        } catch (KeyManagementException ex) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   146
            error(null, ex);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   147
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   148
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   149
        validateContext(expectedContextProto, expectedDefaultProtos, context);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   150
    }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   151
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   152
    /**
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   153
     * Simple print utility for SSLContext's protocol details.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   154
     */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   155
    private void printContextDetails(SSLContext context) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   156
        System.out.println("Default   Protocols: "
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   157
                + Arrays.toString(context.getDefaultSSLParameters()
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   158
                        .getProtocols()));
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   159
        System.out.println("Supported Protocols: "
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   160
                + Arrays.toString(context.getSupportedSSLParameters()
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   161
                        .getProtocols()));
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   162
        System.out.println("Current   Protocol : " + context.getProtocol());
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   163
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   164
    }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   165
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   166
    /**
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   167
     * Error handler.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   168
     */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   169
    private void error(String msg, Throwable tble) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   170
        String finalMsg = "FAILED " + (msg != null ? msg : "");
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   171
        if (tble != null) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   172
            throw new RuntimeException(finalMsg, tble);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   173
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   174
        throw new RuntimeException(finalMsg);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   175
    }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   176
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   177
    /**
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   178
     * Validates the SSLContext's protocols against the user enforced protocol.
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   179
     */
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   180
    private void validateContext(String expectedProto,
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   181
            String[] expectedDefaultProtos, SSLContext context) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   182
        if (expectedProto == null) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   183
            expectedProto = "Default";
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   184
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   185
        if (!context.getProtocol().equals(expectedProto)) {
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
   186
            error("Invalid current protocol: " + context.getProtocol()
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   187
                    + ", Expected:" + expectedProto, null);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   188
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   189
        List<String> actualDefaultProtos = Arrays.asList(context
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   190
                .getDefaultSSLParameters().getProtocols());
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
   191
        for (String p : expectedDefaultProtos) {
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   192
            if (!actualDefaultProtos.contains(p)) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   193
                error("Default protocol " + p + "missing", null);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   194
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   195
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   196
        List<String> actualSupportedProtos = Arrays.asList(context
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   197
                .getSupportedSSLParameters().getProtocols());
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   198
28570
b00a1e9a9238 8069038: javax/net/ssl/TLS/TLSClientPropertyTest.java needs to be updated for JDK-8061210
wetmore
parents: 27939
diff changeset
   199
        for (String p : expectedSupportedProtos) {
27939
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   200
            if (!actualSupportedProtos.contains(p)) {
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   201
                error("Expected to support protocol:" + p, null);
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   202
            }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   203
        }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   204
    }
0a39982a7c6a 8049432: New tests for TLS property jdk.tls.client.protocols
xuelei
parents:
diff changeset
   205
}