1 /*
   2  * Copyright (c) 2014, 2015, 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 jdk.test.lib.ExitCode;
  25 import jdk.test.lib.Platform;
  26 import jdk.test.lib.cli.CommandLineOptionTest;
  27 import jdk.test.lib.cli.predicate.AndPredicate;
  28 import jdk.test.lib.cli.predicate.OrPredicate;
  29 
  30 /**
  31  * Generic test case for SHA-related options targeted to CPUs which
  32  * support instructions required by the tested option.
  33  */
  34 public class GenericTestCaseForSupportedCPU extends
  35         SHAOptionsBase.TestCase {
  36     public GenericTestCaseForSupportedCPU(String optionName) {
  37         super(optionName,
  38                 new AndPredicate(
  39                     new OrPredicate(Platform::isSparc, Platform::isAArch64),
  40                     SHAOptionsBase.getPredicateForOption(optionName)));
  41     }
  42 
  43     @Override
  44     protected void verifyWarnings() throws Throwable {
  45 
  46         String shouldPassMessage = String.format("JVM should start with option"
  47                 + " '%s' without any warnings", optionName);
  48         // Verify that there are no warning when option is explicitly enabled.
  49         CommandLineOptionTest.verifySameJVMStartup(null, new String[] {
  50                         SHAOptionsBase.getWarningForUnsupportedCPU(optionName)
  51                 }, shouldPassMessage, shouldPassMessage, ExitCode.OK,
  52                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
  53                 CommandLineOptionTest.prepareBooleanFlag(optionName, true));
  54 
  55         // Verify that option could be disabled even if +UseSHA was passed to
  56         // JVM.
  57         CommandLineOptionTest.verifySameJVMStartup(null, new String[] {
  58                         SHAOptionsBase.getWarningForUnsupportedCPU(optionName)
  59                 }, shouldPassMessage, String.format("It should be able to "
  60                         + "disable option '%s' even if %s was passed to JVM",
  61                         optionName, CommandLineOptionTest.prepareBooleanFlag(
  62                             SHAOptionsBase.USE_SHA_OPTION, true)),
  63                 ExitCode.OK,
  64                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
  65                 CommandLineOptionTest.prepareBooleanFlag(
  66                         SHAOptionsBase.USE_SHA_OPTION, true),
  67                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
  68 
  69         if (!optionName.equals(SHAOptionsBase.USE_SHA_OPTION)) {
  70             // Verify that if -XX:-UseSHA is passed to the JVM, it is not possible
  71             // to enable the tested option and a warning is printed.
  72             CommandLineOptionTest.verifySameJVMStartup(
  73                     new String[] { SHAOptionsBase.getWarningForUnsupportedCPU(optionName) },
  74                     null,
  75                     shouldPassMessage,
  76                     String.format("Enabling option '%s' should not be possible and should result in a warning if %s was passed to JVM",
  77                                   optionName,
  78                                   CommandLineOptionTest.prepareBooleanFlag(SHAOptionsBase.USE_SHA_OPTION, false)),
  79                     ExitCode.OK,
  80                     SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
  81                     CommandLineOptionTest.prepareBooleanFlag(SHAOptionsBase.USE_SHA_OPTION, false),
  82                     CommandLineOptionTest.prepareBooleanFlag(optionName, true));
  83         }
  84     }
  85 
  86     @Override
  87     protected void verifyOptionValues() throws Throwable {
  88         // Verify that "It should be able to disable option "
  89 
  90         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "true",
  91                 String.format("Option '%s' should be enabled by default",
  92                         optionName),
  93                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS);
  94 
  95         // Verify that it is possible to explicitly enable the option.
  96         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "true",
  97                 String.format("Option '%s' was set to have value 'true'",
  98                         optionName),
  99                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 100                 CommandLineOptionTest.prepareBooleanFlag(optionName, true));
 101 
 102         // Verify that it is possible to explicitly disable the option.
 103         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 104                 String.format("Option '%s' was set to have value 'false'",
 105                         optionName),
 106                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 107                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
 108 
 109         // verify that option is disabled when -UseSHA was passed to JVM.
 110         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 111                 String.format("Option '%s' should have value 'false' when %s"
 112                         + " flag set to JVM", optionName,
 113                         CommandLineOptionTest.prepareBooleanFlag(
 114                             SHAOptionsBase.USE_SHA_OPTION, false)),
 115                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 116                 CommandLineOptionTest.prepareBooleanFlag(optionName, true),
 117                 CommandLineOptionTest.prepareBooleanFlag(
 118                         SHAOptionsBase.USE_SHA_OPTION, false));
 119 
 120         // Verify that it is possible to explicitly disable the tested option
 121         // even if +UseSHA was passed to JVM.
 122         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 123                 String.format("Option '%s' should have value 'false' if set so"
 124                         + " even if %s flag set to JVM", optionName,
 125                         CommandLineOptionTest.prepareBooleanFlag(
 126                             SHAOptionsBase.USE_SHA_OPTION, true)),
 127                 SHAOptionsBase.UNLOCK_DIAGNOSTIC_VM_OPTIONS,
 128                 CommandLineOptionTest.prepareBooleanFlag(
 129                         SHAOptionsBase.USE_SHA_OPTION, true),
 130                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
 131     }
 132 }