File: SHAOptionsBase.java

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.15%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 221,420 kB
  • sloc: java: 1,363,561; cpp: 967,919; ansic: 69,480; xml: 62,475; sh: 8,106; asm: 3,475; python: 1,667; javascript: 943; makefile: 397; sed: 172; perl: 114
file content (156 lines) | stat: -rw-r--r-- 6,471 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package compiler.intrinsics.sha.cli;

import compiler.testlibrary.sha.predicate.IntrinsicPredicates;
import jdk.test.lib.Platform;
import jdk.test.lib.cli.CommandLineOptionTest;

import java.util.function.BooleanSupplier;

/**
 * Base class for all CLI tests on SHA-related options.
 *
 * Instead of using huge complex tests for each option, each test is constructed
 * from several test cases shared among different tests.
 */
public class SHAOptionsBase extends CommandLineOptionTest {
    public static final String USE_SHA_OPTION = "UseSHA";
    public static final String USE_SHA1_INTRINSICS_OPTION
            = "UseSHA1Intrinsics";
    public static final String USE_SHA256_INTRINSICS_OPTION
            = "UseSHA256Intrinsics";
    public static final String USE_SHA512_INTRINSICS_OPTION
            = "UseSHA512Intrinsics";

    // Intrinsics flags are of diagnostic type
    // and must be preceded by UnlockDiagnosticVMOptions.
    public static final String UNLOCK_DIAGNOSTIC_VM_OPTIONS
            = "-XX:+UnlockDiagnosticVMOptions";

    // Note that strings below will be passed to
    // CommandLineOptionTest.verifySameJVMStartup and thus are regular
    // expressions, not just a plain strings.
    protected static final String SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE
            = "SHA instructions are not available on this CPU";
    protected static final String SHA1_INTRINSICS_ARE_NOT_AVAILABLE
            = "Intrinsics for SHA-1 crypto hash functions not available on this CPU.";
    protected static final String SHA256_INTRINSICS_ARE_NOT_AVAILABLE
            = "Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.";
    protected static final String SHA512_INTRINSICS_ARE_NOT_AVAILABLE
            = "Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.";

    private final TestCase[] testCases;

    /**
     * Returns warning message that should occur in VM output if an option with
     * the name {@code optionName} was turned on and CPU does not support
     * required instructions.
     *
     * @param optionName The name of the option for which warning message should
     *                   be returned.
     * @return A warning message that will be printed out to VM output if CPU
     *         instructions required by the option are not supported.
     */
    public static String getWarningForUnsupportedCPU(String optionName) {
        switch (optionName) {
        case SHAOptionsBase.USE_SHA_OPTION:
            return SHAOptionsBase.SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE;
        case SHAOptionsBase.USE_SHA1_INTRINSICS_OPTION:
            return SHAOptionsBase.SHA1_INTRINSICS_ARE_NOT_AVAILABLE;
        case SHAOptionsBase.USE_SHA256_INTRINSICS_OPTION:
            return SHAOptionsBase.SHA256_INTRINSICS_ARE_NOT_AVAILABLE;
        case SHAOptionsBase.USE_SHA512_INTRINSICS_OPTION:
            return SHAOptionsBase.SHA512_INTRINSICS_ARE_NOT_AVAILABLE;
        default:
            throw new Error("Unexpected option " + optionName);
        }
    }

    /**
     * Returns the predicate indicating whether or not CPU instructions required
     * by the option with name {@code optionName} are available.
     *
     * @param optionName The name of the option for which a predicate should be
     *                   returned.
     * @return The predicate on availability of CPU instructions required by the
     *         option.
     */
    public static BooleanSupplier getPredicateForOption(String optionName) {
        switch (optionName) {
            case SHAOptionsBase.USE_SHA_OPTION:
                return IntrinsicPredicates.ANY_SHA_INSTRUCTION_AVAILABLE;
            case SHAOptionsBase.USE_SHA1_INTRINSICS_OPTION:
                return IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE;
            case SHAOptionsBase.USE_SHA256_INTRINSICS_OPTION:
                return IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE;
            case SHAOptionsBase.USE_SHA512_INTRINSICS_OPTION:
                return IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE;
            default:
                throw new Error("Unexpected option " + optionName);
        }
    }

    public SHAOptionsBase(TestCase... testCases) {
        super(Boolean.TRUE::booleanValue);
        this.testCases = testCases;
    }

    @Override
    protected void runTestCases() throws Throwable {
        for (TestCase testCase : testCases) {
            testCase.test();
        }
    }

    public static abstract class TestCase {
        protected final String optionName;
        private final BooleanSupplier predicate;

        protected TestCase(String optionName, BooleanSupplier predicate) {
            this.optionName = optionName;
            this.predicate = predicate;
        }

        protected final void test() throws Throwable {
            String testCaseName = this.getClass().getName();
            if (!predicate.getAsBoolean()) {
                System.out.println("Skipping " + testCaseName
                        + " due to predicate failure.");
                return;
            } else {
                System.out.println("Running " + testCaseName);
            }

            verifyWarnings();
            verifyOptionValues();
        }

        protected void verifyWarnings() throws Throwable {
        }

        protected void verifyOptionValues() throws Throwable {
        }
    }
}