1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import com.oracle.java.testlibrary.Platform;
  25 import com.oracle.java.testlibrary.cli.CommandLineOptionTest;
  26 import sha.predicate.IntrinsicPredicates;
  27 
  28 import java.util.function.BooleanSupplier;
  29 
  30 /**
  31  * Base class for all CLI tests on SHA-related options.
  32  *
  33  * Instead of using huge complex tests for each option, each test is constructed
  34  * from several test cases shared among different tests.
  35  */
  36 public class SHAOptionsBase extends CommandLineOptionTest {
  37     protected static final String USE_SHA_OPTION = "UseSHA";
  38     protected static final String USE_SHA1_INTRINSICS_OPTION
  39             = "UseSHA1Intrinsics";
  40     protected static final String USE_SHA256_INTRINSICS_OPTION
  41             = "UseSHA256Intrinsics";
  42     protected static final String USE_SHA512_INTRINSICS_OPTION
  43             = "UseSHA512Intrinsics";
  44 
  45     protected static final String SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE
  46             = "SHA instructions are not available on this CPU";
  47     protected static final String SHA1_INSTRUCTION_IS_NOT_AVAILABLE
  48             = "SHA1 instruction is not available on this CPU.";
  49     protected static final String SHA256_INSTRUCTION_IS_NOT_AVAILABLE
  50             = "SHA256 instruction.*is not available on this CPU.";
  51     protected static final String SHA512_INSTRUCTION_IS_NOT_AVAILABLE
  52             = "SHA512 instruction.*is not available on this CPU.";
  53     protected static final String SHA_INTRINSICS_ARE_NOT_AVAILABLE
  54             = "SHA intrinsics are not available on this CPU";
  55 
  56     private final TestCase[] testCases;
  57 
  58     /**
  59      * Returns warning message that should occur in VM output if an option with
  60      * the name {@code optionName} was turned on and CPU does not support
  61      * required instructions.
  62      *
  63      * @param optionName The name of the option for which warning message should
  64      *                   be returned.
  65      * @return A warning message that will be printed out to VM output if CPU
  66      *         instructions required by the option are not supported.
  67      */
  68     protected static String getWarningForUnsupportedCPU(String optionName) {
  69         if (Platform.isSparc()) {
  70             switch (optionName) {
  71                 case SHAOptionsBase.USE_SHA_OPTION:
  72                     return SHAOptionsBase.SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE;
  73                 case SHAOptionsBase.USE_SHA1_INTRINSICS_OPTION:
  74                     return SHAOptionsBase.SHA1_INSTRUCTION_IS_NOT_AVAILABLE;
  75                 case SHAOptionsBase.USE_SHA256_INTRINSICS_OPTION:
  76                     return SHAOptionsBase.SHA256_INSTRUCTION_IS_NOT_AVAILABLE;
  77                 case SHAOptionsBase.USE_SHA512_INTRINSICS_OPTION:
  78                     return SHAOptionsBase.SHA512_INSTRUCTION_IS_NOT_AVAILABLE;
  79                 default:
  80                     throw new Error("Unexpected option " + optionName);
  81             }
  82         } else if (Platform.isX64() || Platform.isX86()) {
  83             switch (optionName) {
  84                 case SHAOptionsBase.USE_SHA_OPTION:
  85                     return SHAOptionsBase.SHA_INSTRUCTIONS_ARE_NOT_AVAILABLE;
  86                 case SHAOptionsBase.USE_SHA1_INTRINSICS_OPTION:
  87                 case SHAOptionsBase.USE_SHA256_INTRINSICS_OPTION:
  88                 case SHAOptionsBase.USE_SHA512_INTRINSICS_OPTION:
  89                     return SHAOptionsBase.SHA_INTRINSICS_ARE_NOT_AVAILABLE;
  90                 default:
  91                     throw new Error("Unexpected option " + optionName);
  92             }
  93         } else {
  94             throw new Error("Support for CPUs other then X86 or SPARC is not "
  95                     + "implemented.");
  96         }
  97     }
  98 
  99     /**
 100      * Returns the predicate indicating whether or not CPU instructions required
 101      * by the option with name {@code optionName} are available.
 102      *
 103      * @param optionName The name of the option for which a predicate should be
 104      *                   returned.
 105      * @return The predicate on availability of CPU instructions required by the
 106      *         option.
 107      */
 108     protected static BooleanSupplier getPredicateForOption(String optionName) {
 109         switch (optionName) {
 110             case SHAOptionsBase.USE_SHA_OPTION:
 111                 return IntrinsicPredicates.ANY_SHA_INSTRUCTION_AVAILABLE;
 112             case SHAOptionsBase.USE_SHA1_INTRINSICS_OPTION:
 113                 return IntrinsicPredicates.SHA1_INSTRUCTION_AVAILABLE;
 114             case SHAOptionsBase.USE_SHA256_INTRINSICS_OPTION:
 115                 return IntrinsicPredicates.SHA256_INSTRUCTION_AVAILABLE;
 116             case SHAOptionsBase.USE_SHA512_INTRINSICS_OPTION:
 117                 return IntrinsicPredicates.SHA512_INSTRUCTION_AVAILABLE;
 118             default:
 119                 throw new Error("Unexpected option " + optionName);
 120         }
 121     }
 122 
 123     public SHAOptionsBase(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;
 138 
 139         protected TestCase(String optionName, BooleanSupplier predicate) {
 140             this.optionName = optionName;
 141             this.predicate = predicate;
 142         }
 143 
 144         protected final void test() throws Throwable {
 145             String testCaseName = this.getClass().getName();
 146             if (!predicate.getAsBoolean()) {
 147                 System.out.println("Skipping " + testCaseName
 148                         + " due to predicate failure.");
 149                 return;
 150             } else {
 151                 System.out.println("Running " + testCaseName);
 152             }
 153 
 154             verifyWarnings();
 155             verifyOptionValues();
 156         }
 157 
 158         protected void verifyWarnings() throws Throwable {
 159         }
 160 
 161         protected void verifyOptionValues() throws Throwable {
 162         }
 163     }
 164 }