28 import jdk.test.lib.cli.CommandLineOptionTest;
29
30 import java.util.function.BooleanSupplier;
31
32 /**
33 * Base class for all CLI tests on SHA-related options.
34 *
35 * Instead of using huge complex tests for each option, each test is constructed
36 * from several test cases shared among different tests.
37 */
38 public class DigestOptionsBase extends CommandLineOptionTest {
39 public static final String USE_MD5_INTRINSICS_OPTION
40 = "UseMD5Intrinsics";
41 public static final String USE_SHA_OPTION = "UseSHA";
42 public static final String USE_SHA1_INTRINSICS_OPTION
43 = "UseSHA1Intrinsics";
44 public static final String USE_SHA256_INTRINSICS_OPTION
45 = "UseSHA256Intrinsics";
46 public static final String USE_SHA512_INTRINSICS_OPTION
47 = "UseSHA512Intrinsics";
48
49 // Intrinsics flags are of diagnostic type
50 // and must be preceded by UnlockDiagnosticVMOptions.
51 public static final String UNLOCK_DIAGNOSTIC_VM_OPTIONS
52 = "-XX:+UnlockDiagnosticVMOptions";
53
54 // Note that strings below will be passed to
55 // CommandLineOptionTest.verifySameJVMStartup and thus are regular
56 // expressions, not just a plain strings.
57 protected static final String MD5_INTRINSICS_ARE_NOT_AVAILABLE
58 = "Intrinsics for MD5 crypto hash functions not available on this CPU.";
59 protected static final String SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE
60 = "SHA instructions are not available on this CPU";
61 protected static final String SHA1_INTRINSICS_ARE_NOT_AVAILABLE
62 = "Intrinsics for SHA-1 crypto hash functions not available on this CPU.";
63 protected static final String SHA256_INTRINSICS_ARE_NOT_AVAILABLE
64 = "Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.";
65 protected static final String SHA512_INTRINSICS_ARE_NOT_AVAILABLE
66 = "Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.";
67
68 private final TestCase[] testCases;
69
70 /**
71 * Returns warning message that should occur in VM output if an option with
72 * the name {@code optionName} was turned on and CPU does not support
73 * required instructions.
74 *
75 * @param optionName The name of the option for which warning message should
76 * be returned.
77 * @return A warning message that will be printed out to VM output if CPU
78 * instructions required by the option are not supported.
79 */
80 public static String getWarningForUnsupportedCPU(String optionName) {
81 switch (optionName) {
82 case DigestOptionsBase.USE_MD5_INTRINSICS_OPTION:
83 return DigestOptionsBase.MD5_INTRINSICS_ARE_NOT_AVAILABLE;
84 case DigestOptionsBase.USE_SHA_OPTION:
85 return DigestOptionsBase.SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE;
86 case DigestOptionsBase.USE_SHA1_INTRINSICS_OPTION:
87 return DigestOptionsBase.SHA1_INTRINSICS_ARE_NOT_AVAILABLE;
88 case DigestOptionsBase.USE_SHA256_INTRINSICS_OPTION:
89 return DigestOptionsBase.SHA256_INTRINSICS_ARE_NOT_AVAILABLE;
90 case DigestOptionsBase.USE_SHA512_INTRINSICS_OPTION:
91 return DigestOptionsBase.SHA512_INTRINSICS_ARE_NOT_AVAILABLE;
92 default:
93 throw new Error("Unexpected option " + optionName);
94 }
95 }
96
97 /**
98 * Returns the predicate indicating whether or not CPU instructions required
99 * by the option with name {@code optionName} are available.
100 *
101 * @param optionName The name of the option for which a predicate should be
102 * returned.
103 * @return The predicate on availability of CPU instructions required by the
104 * option.
105 */
106 public static BooleanSupplier getPredicateForOption(String optionName) {
107 switch (optionName) {
108 case DigestOptionsBase.USE_MD5_INTRINSICS_OPTION:
109 return IntrinsicPredicates.MD5_INSTRUCTION_AVAILABLE;
110 case DigestOptionsBase.USE_SHA_OPTION:
111 return IntrinsicPredicates.ANY_SHA_INSTRUCTION_AVAILABLE;
112 case DigestOptionsBase.USE_SHA1_INTRINSICS_OPTION:
113 return IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE;
114 case DigestOptionsBase.USE_SHA256_INTRINSICS_OPTION:
115 return IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE;
116 case DigestOptionsBase.USE_SHA512_INTRINSICS_OPTION:
117 return IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE;
118 default:
119 throw new Error("Unexpected option " + optionName);
120 }
121 }
122
123 public DigestOptionsBase(TestCase... testCases) {
124 super(Boolean.TRUE::booleanValue);
125 this.testCases = testCases;
126 }
127
128 @Override
129 protected void runTestCases() throws Throwable {
130 for (TestCase testCase : testCases) {
131 testCase.test();
132 }
133 }
134
135 public static abstract class TestCase {
136 protected final String optionName;
137 private final BooleanSupplier predicate;
|
28 import jdk.test.lib.cli.CommandLineOptionTest;
29
30 import java.util.function.BooleanSupplier;
31
32 /**
33 * Base class for all CLI tests on SHA-related options.
34 *
35 * Instead of using huge complex tests for each option, each test is constructed
36 * from several test cases shared among different tests.
37 */
38 public class DigestOptionsBase extends CommandLineOptionTest {
39 public static final String USE_MD5_INTRINSICS_OPTION
40 = "UseMD5Intrinsics";
41 public static final String USE_SHA_OPTION = "UseSHA";
42 public static final String USE_SHA1_INTRINSICS_OPTION
43 = "UseSHA1Intrinsics";
44 public static final String USE_SHA256_INTRINSICS_OPTION
45 = "UseSHA256Intrinsics";
46 public static final String USE_SHA512_INTRINSICS_OPTION
47 = "UseSHA512Intrinsics";
48 public static final String USE_SHA3_INTRINSICS_OPTION
49 = "UseSHA3Intrinsics";
50
51 // Intrinsics flags are of diagnostic type
52 // and must be preceded by UnlockDiagnosticVMOptions.
53 public static final String UNLOCK_DIAGNOSTIC_VM_OPTIONS
54 = "-XX:+UnlockDiagnosticVMOptions";
55
56 // Note that strings below will be passed to
57 // CommandLineOptionTest.verifySameJVMStartup and thus are regular
58 // expressions, not just a plain strings.
59 protected static final String MD5_INTRINSICS_ARE_NOT_AVAILABLE
60 = "Intrinsics for MD5 crypto hash functions not available on this CPU.";
61 protected static final String SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE
62 = "SHA instructions are not available on this CPU";
63 protected static final String SHA1_INTRINSICS_ARE_NOT_AVAILABLE
64 = "Intrinsics for SHA-1 crypto hash functions not available on this CPU.";
65 protected static final String SHA256_INTRINSICS_ARE_NOT_AVAILABLE
66 = "Intrinsics for SHA-224 and SHA-256 crypto hash functions not available on this CPU.";
67 protected static final String SHA512_INTRINSICS_ARE_NOT_AVAILABLE
68 = "Intrinsics for SHA-384 and SHA-512 crypto hash functions not available on this CPU.";
69 protected static final String SHA3_INTRINSICS_ARE_NOT_AVAILABLE
70 = "Intrinsics for SHA3-224, SHA3-256, SHA3-384 and SHA3-512 crypto hash functions not available on this CPU.";
71
72 private final TestCase[] testCases;
73
74 /**
75 * Returns warning message that should occur in VM output if an option with
76 * the name {@code optionName} was turned on and CPU does not support
77 * required instructions.
78 *
79 * @param optionName The name of the option for which warning message should
80 * be returned.
81 * @return A warning message that will be printed out to VM output if CPU
82 * instructions required by the option are not supported.
83 */
84 public static String getWarningForUnsupportedCPU(String optionName) {
85 switch (optionName) {
86 case DigestOptionsBase.USE_MD5_INTRINSICS_OPTION:
87 return DigestOptionsBase.MD5_INTRINSICS_ARE_NOT_AVAILABLE;
88 case DigestOptionsBase.USE_SHA_OPTION:
89 return DigestOptionsBase.SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE;
90 case DigestOptionsBase.USE_SHA1_INTRINSICS_OPTION:
91 return DigestOptionsBase.SHA1_INTRINSICS_ARE_NOT_AVAILABLE;
92 case DigestOptionsBase.USE_SHA256_INTRINSICS_OPTION:
93 return DigestOptionsBase.SHA256_INTRINSICS_ARE_NOT_AVAILABLE;
94 case DigestOptionsBase.USE_SHA512_INTRINSICS_OPTION:
95 return DigestOptionsBase.SHA512_INTRINSICS_ARE_NOT_AVAILABLE;
96 case DigestOptionsBase.USE_SHA3_INTRINSICS_OPTION:
97 return DigestOptionsBase.SHA3_INTRINSICS_ARE_NOT_AVAILABLE;
98 default:
99 throw new Error("Unexpected option " + optionName);
100 }
101 }
102
103 /**
104 * Returns the predicate indicating whether or not CPU instructions required
105 * by the option with name {@code optionName} are available.
106 *
107 * @param optionName The name of the option for which a predicate should be
108 * returned.
109 * @return The predicate on availability of CPU instructions required by the
110 * option.
111 */
112 public static BooleanSupplier getPredicateForOption(String optionName) {
113 switch (optionName) {
114 case DigestOptionsBase.USE_MD5_INTRINSICS_OPTION:
115 return IntrinsicPredicates.MD5_INSTRUCTION_AVAILABLE;
116 case DigestOptionsBase.USE_SHA_OPTION:
117 return IntrinsicPredicates.ANY_SHA_INSTRUCTION_AVAILABLE;
118 case DigestOptionsBase.USE_SHA1_INTRINSICS_OPTION:
119 return IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE;
120 case DigestOptionsBase.USE_SHA256_INTRINSICS_OPTION:
121 return IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE;
122 case DigestOptionsBase.USE_SHA512_INTRINSICS_OPTION:
123 return IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE;
124 case DigestOptionsBase.USE_SHA3_INTRINSICS_OPTION:
125 return IntrinsicPredicates.SHA3_INSTRUCTION_AVAILABLE;
126 default:
127 throw new Error("Unexpected option " + optionName);
128 }
129 }
130
131 public DigestOptionsBase(TestCase... testCases) {
132 super(Boolean.TRUE::booleanValue);
133 this.testCases = testCases;
134 }
135
136 @Override
137 protected void runTestCases() throws Throwable {
138 for (TestCase testCase : testCases) {
139 testCase.test();
140 }
141 }
142
143 public static abstract class TestCase {
144 protected final String optionName;
145 private final BooleanSupplier predicate;
|