test/jdk/sun/security/tools/keytool/ExtOptionCamelCase.java
author weijun
Mon, 04 Nov 2019 14:26:18 +0800
changeset 58902 197238c30630
permissions -rw-r--r--
8231950: keytool -ext camel-case shorthand not working Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58902
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     1
/*
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     4
 *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     7
 * published by the Free Software Foundation.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     8
 *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    13
 * accompanied this code).
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    14
 *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    18
 *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    21
 * questions.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    22
 */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    23
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    24
/*
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    25
 * @test
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    26
 * @bug 8231950
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    27
 * @modules java.base/sun.security.tools.keytool
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    28
 *          java.base/sun.security.util
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    29
 *          java.base/sun.security.x509
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    30
 * @compile -XDignore.symbol.file ExtOptionCamelCase.java
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    31
 * @summary keytool -ext camel-case shorthand not working
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    32
 */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    33
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    34
import sun.security.tools.keytool.Main;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    35
import sun.security.util.DerValue;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    36
import sun.security.x509.BasicConstraintsExtension;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    37
import sun.security.x509.CertificateExtensions;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    38
import sun.security.x509.Extension;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    39
import sun.security.x509.KeyUsageExtension;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    40
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    41
import java.io.ByteArrayOutputStream;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    42
import java.lang.reflect.Constructor;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    43
import java.lang.reflect.InvocationTargetException;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    44
import java.lang.reflect.Method;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    45
import java.security.KeyPairGenerator;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    46
import java.security.PublicKey;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    47
import java.util.List;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    48
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    49
public class ExtOptionCamelCase {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    50
    static Method createV3Extensions;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    51
    static Constructor<Main> ctor;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    52
    static PublicKey pk;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    53
    static Method oneOf;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    54
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    55
    public static void main(String[] args) throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    56
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    57
        prepare();
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    58
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    59
        // Unseen ext name
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    60
        testCreateFail("abc");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    61
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    62
        // camelCase match, both cases work
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    63
        testCreate("bc", BasicConstraintsExtension.class);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    64
        testCreate("BC", BasicConstraintsExtension.class);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    65
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    66
        // Prefix match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    67
        testCreate("BasicC", BasicConstraintsExtension.class);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    68
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    69
        // Ambiguous, digitalSignature or dataEncipherment?
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    70
        testCreateFail("ku=d");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    71
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    72
        // prefix match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    73
        testCreate("ku:c=dig", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    74
                x -> x.get(KeyUsageExtension.DIGITAL_SIGNATURE));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    75
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    76
        // camelCase match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    77
        testCreate("ku=kE", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    78
                x -> x.get(KeyUsageExtension.KEY_ENCIPHERMENT));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    79
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    80
        // camelCase match must be only 1st+CAPITALs
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    81
        testCreateFail("ku=KeUs");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    82
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    83
        // camelCase match, must be only 1st + all CAPITALs
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    84
        testCreate("ku=kCS", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    85
                x -> x.get(KeyUsageExtension.KEY_CERTSIGN));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    86
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    87
        // ... not all CAPITALs
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    88
        testCreateFail("ku=kC");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    89
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    90
        // ... has lowercase letters
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    91
        testCreateFail("ku=keCeSi");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    92
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    93
        // Ambiguous, keyAgreement or keyCertSign
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    94
        testCreateFail("ku:c=ke");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    95
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    96
        // camelCase natch
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    97
        testCreate("ku:c=dE", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    98
                x -> x.get(KeyUsageExtension.DATA_ENCIPHERMENT));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
    99
        // prefix match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   100
        testCreate("ku:c=de", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   101
                x -> x.get(KeyUsageExtension.DECIPHER_ONLY));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   102
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   103
        // camelCase match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   104
        testCreate("ku:c=kA", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   105
                x -> x.get(KeyUsageExtension.KEY_AGREEMENT));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   106
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   107
        // camelCase match, fallback
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   108
        testCreate("ku:c=ka", KeyUsageExtension.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   109
                x -> x.get(KeyUsageExtension.KEY_AGREEMENT));
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   110
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   111
        // Testing oneOf() directly
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   112
        testOneOf("a", -1, "b", "c"); // -1 means not found
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   113
        testOneOf("a", -2, "ab", "ac"); // -2 means ambiguous
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   114
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   115
        testOneOf("a", 0, "a", "ac"); //exact match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   116
        testOneOf("a", 0, "a", "b");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   117
        testOneOf("ac", 1, "a", "ac");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   118
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   119
        testOneOf("a", 0, "abc", "bcd");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   120
        testOneOf("ab", 0, "abc", "ABC");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   121
        testOneOf("ab", 0, "abc", "aBC");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   122
        testOneOf("ab", 0, "abc", "Abc");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   123
        testOneOf("AB", 1, "abc", "ABC");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   124
        testOneOf("aB", 0, "abcBcd", "abcDef");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   125
        testOneOf("ab", -2, "abcBcd", "abcDef");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   126
        testOneOf("aB", -2, "abcBcdEfg", "abcDef");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   127
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   128
        testOneOf("ab", 0, "abcDef", "axyBuv");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   129
        testOneOf("aB", 1, "abcDef", "axyBuv");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   130
        testOneOf("a", -2, "abcDef", "axyBuv");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   131
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   132
        testOneOf("aBC", -1, "a12BxyCuvDmn"); // 12 is not removed
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   133
        testOneOf("a12BCD", 0, "a12BxyCuvDmn");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   134
        testOneOf("a12BC", -1, "a12BxyCuvDmn"); // must be full
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   135
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   136
        // Fallback
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   137
        testOneOf("bc", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   138
        testOneOf("BC", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   139
        testOneOf("BasicConstraints", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   140
        testOneOf("basicconstraints", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   141
        testOneOf("Basic", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   142
        testOneOf("basic", 0, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   143
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   144
        testOneOf("BaCo", -1, "BasicConstraints");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   145
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   146
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   147
    // Expose some private methods
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   148
    static void prepare() throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   149
        createV3Extensions = Main.class.getDeclaredMethod(
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   150
                "createV3Extensions",
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   151
                CertificateExtensions.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   152
                CertificateExtensions.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   153
                List.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   154
                PublicKey.class,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   155
                PublicKey.class);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   156
        createV3Extensions.setAccessible(true);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   157
        ctor = Main.class.getDeclaredConstructor();
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   158
        ctor.setAccessible(true);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   159
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   160
        pk = kpg.generateKeyPair().getPublic();
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   161
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   162
        oneOf = Main.class.getDeclaredMethod(
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   163
                "oneOf", String.class, String[].class);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   164
        oneOf.setAccessible(true);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   165
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   166
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   167
    /**
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   168
     * Ensures the given type of extension is created with the option
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   169
     */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   170
    static <T extends Extension> void testCreate(String option, Class<T> clazz)
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   171
            throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   172
        testCreate(option, clazz, null);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   173
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   174
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   175
    /**
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   176
     * Ensures an option is invalid and will be rejected
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   177
     */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   178
    static <T extends Extension> void testCreateFail(String option)
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   179
            throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   180
        testCreate(option, null, null);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   181
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   182
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   183
    /**
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   184
     * Ensures the given type of extension is created and match the rule
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   185
     * with the option.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   186
     *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   187
     * @param option the -ext option provided to keytool
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   188
     * @param clazz the expected extension to create, null means none
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   189
     * @param rule a predicate to check if the extension created is acceptable
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   190
     * @param <T> the extected extension type
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   191
     * @throws Exception if test result is unexpected
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   192
     */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   193
    static <T extends Extension> void testCreate(String option, Class<T> clazz,
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   194
            PredicateWithException<T> rule) throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   195
        try {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   196
            CertificateExtensions exts = (CertificateExtensions)
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   197
                    createV3Extensions.invoke(ctor.newInstance(),
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   198
                            null, null, List.of(option), pk, null);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   199
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   200
            // ATTENTION: the extensions created above might contain raw
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   201
            // extensions (not of a subtype) and we need to store and reload
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   202
            // it to resolve them to subtypes.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   203
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   204
            exts.encode(bout);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   205
            exts = new CertificateExtensions(new DerValue(bout.toByteArray()).data);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   206
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   207
            if (clazz == null) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   208
                throw new Exception("Should fail");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   209
            } else {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   210
                for (Extension e : exts.getAllExtensions()) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   211
                    if (e.getClass() == clazz) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   212
                        if (rule == null || rule.test((T) e)) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   213
                            return;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   214
                        }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   215
                    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   216
                }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   217
                throw new Exception("Unexpected result: " + exts);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   218
            }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   219
        } catch (InvocationTargetException e) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   220
            if (clazz == null) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   221
                return;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   222
            } else {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   223
                throw e;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   224
            }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   225
        }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   226
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   227
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   228
    @FunctionalInterface
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   229
    interface PredicateWithException<T> {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   230
        boolean test(T t) throws Exception;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   231
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   232
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   233
    /**
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   234
     * Ensures oneOf returns the expected result.
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   235
     *
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   236
     * @param s input
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   237
     * @param expected expected value, -2 if ambiguous, -1 if no match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   238
     * @param items existing strings to match
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   239
     * @throws Exception if test result is unexpected
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   240
     */
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   241
    static void testOneOf(String s, int expected, String... items)
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   242
            throws Exception {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   243
        try {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   244
            int res = (int)oneOf.invoke(null, s, items);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   245
            if (expected == -2) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   246
                throw new Exception("Should fail");
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   247
            } else {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   248
                if (expected != res) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   249
                    throw new Exception(
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   250
                            "Expected " + expected + ", actually " + res);
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   251
                }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   252
            }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   253
        } catch (InvocationTargetException e) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   254
            if (expected == -2) {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   255
                return;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   256
            } else {
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   257
                throw e;
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   258
            }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   259
        }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   260
    }
197238c30630 8231950: keytool -ext camel-case shorthand not working
weijun
parents:
diff changeset
   261
}