1 /*
  2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
  3  * Copyright (c) 2020, 2021, Huawei Technologies Co., Ltd. All rights reserved.
  4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  5  *
  6  * This code is free software; you can redistribute it and/or modify it
  7  * under the terms of the GNU General Public License version 2 only, as
  8  * published by the Free Software Foundation.
  9  *
 10  * This code is distributed in the hope that it will be useful, but WITHOUT
 11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 13  * version 2 for more details (a copy is included in the LICENSE file that
 14  * accompanied this code).
 15  *
 16  * You should have received a copy of the GNU General Public License version
 17  * 2 along with this work; if not, write to the Free Software Foundation,
 18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 19  *
 20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 21  * or visit www.oracle.com if you need additional information or have any
 22  * questions.
 23  */
 24 
 25 package compiler.intrinsics.sha.cli.testcases;
 26 
 27 import compiler.intrinsics.sha.cli.DigestOptionsBase;
 28 import jdk.test.lib.process.ExitCode;
 29 import jdk.test.lib.Platform;
 30 import jdk.test.lib.cli.CommandLineOptionTest;
 31 import jdk.test.lib.cli.predicate.AndPredicate;
 32 import jdk.test.lib.cli.predicate.NotPredicate;
 33 
 34 /**
 35  * Generic test case for SHA-related options targeted to RISCV64 CPUs
 36  * which don't support instruction required by the tested option.
 37  */
 38 public class GenericTestCaseForUnsupportedRISCV64CPU extends
 39         DigestOptionsBase.TestCase {
 40 
 41     final private boolean checkUseSHA;
 42 
 43     public GenericTestCaseForUnsupportedRISCV64CPU(String optionName) {
 44         this(optionName, true);
 45     }
 46 
 47     public GenericTestCaseForUnsupportedRISCV64CPU(String optionName, boolean checkUseSHA) {
 48         super(optionName, new AndPredicate(Platform::isRISCV64,
 49                 new NotPredicate(DigestOptionsBase.getPredicateForOption(
 50                         optionName))));
 51 
 52         this.checkUseSHA = checkUseSHA;
 53     }
 54 
 55     @Override
 56     protected void verifyWarnings() throws Throwable {
 57         String shouldPassMessage = String.format("JVM startup should pass with"
 58                 + "option '-XX:-%s' without any warnings", optionName);
 59         //Verify that option could be disabled without any warnings.
 60         CommandLineOptionTest.verifySameJVMStartup(null, new String[] {
 61                         DigestOptionsBase.getWarningForUnsupportedCPU(optionName)
 62                 }, shouldPassMessage, shouldPassMessage, ExitCode.OK,
 63                 DigestOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 64                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
 65 
 66         if (checkUseSHA) {
 67             shouldPassMessage = String.format("If JVM is started with '-XX:-"
 68                     + "%s' '-XX:+%s', output should contain warning.",
 69                     DigestOptionsBase.USE_SHA_OPTION, optionName);
 70 
 71             // Verify that when the tested option is enabled, then
 72             // a warning will occur in VM output if UseSHA is disabled.
 73             if (!optionName.equals(DigestOptionsBase.USE_SHA_OPTION)) {
 74                 CommandLineOptionTest.verifySameJVMStartup(
 75                         new String[] { DigestOptionsBase.getWarningForUnsupportedCPU(optionName) },
 76                         null,
 77                         shouldPassMessage,
 78                         shouldPassMessage,
 79                         ExitCode.OK,
 80                         DigestOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 81                         CommandLineOptionTest.prepareBooleanFlag(DigestOptionsBase.USE_SHA_OPTION, false),
 82                         CommandLineOptionTest.prepareBooleanFlag(optionName, true));
 83             }
 84         }
 85     }
 86 
 87     @Override
 88     protected void verifyOptionValues() throws Throwable {
 89         // Verify that option is disabled by default.
 90         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 91                 String.format("Option '%s' should be disabled by default",
 92                         optionName),
 93                 DigestOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS);
 94 
 95         if (checkUseSHA) {
 96             // Verify that option is disabled even if it was explicitly enabled
 97             // using CLI options.
 98             CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 99                     String.format("Option '%s' should be off on unsupported "
100                             + "RISCV64CPU even if set to true directly", optionName),
101                     DigestOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
102                     CommandLineOptionTest.prepareBooleanFlag(optionName, true));
103 
104             // Verify that option is disabled when +UseSHA was passed to JVM.
105             CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
106                     String.format("Option '%s' should be off on unsupported "
107                             + "RISCV64CPU even if %s flag set to JVM",
108                             optionName, CommandLineOptionTest.prepareBooleanFlag(
109                                 DigestOptionsBase.USE_SHA_OPTION, true)),
110                     DigestOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
111                     CommandLineOptionTest.prepareBooleanFlag(
112                             DigestOptionsBase.USE_SHA_OPTION, true));
113         }
114     }
115 }