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.*;
  25 import jdk.test.lib.cli.*;
  26 
  27 /**
  28  * Test on bit manipulation related command line options,
  29  * that should be executed on CPU that does not support
  30  * required features.
  31  *
  32  * Note that this test intended to verify that VM could be launched with
  33  * specific options and that values of these options processed correctly.
  34  * In order to do that test launch a new VM with tested options, the same
  35  * flavor-specific flag as one that was used for parent VM (-client, -server,
  36  * -minimal, -graal) and '-version'.
  37  */
  38 public class BMIUnsupportedCPUTest extends BMICommandLineOptionTestBase {
  39 
  40     /**
  41      * Construct new test on {@code optionName} option.
  42      *
  43      * @param optionName Name of the option to be tested
  44      *                   without -XX:[+-] prefix.
  45      * @param warningMessage Message that can occur in VM output
  46      *                       if CPU on test box does not support
  47      *                       features required by the option.
  48      * @param cpuFeatures CPU features requires by the option.
  49      */
  50     public BMIUnsupportedCPUTest(String optionName,
  51                                  String warningMessage,
  52                                  String... cpuFeatures) {
  53         super(optionName, warningMessage, null, cpuFeatures);
  54     }
  55 
  56     @Override
  57     public void runTestCases() throws Throwable {
  58         if (Platform.isX86() || Platform.isX64()) {
  59             unsupportedX86CPUTestCases();
  60         } else {
  61             unsupportedNonX86CPUTestCases();
  62         }
  63     }
  64 
  65     /**
  66      * Run test cases common for all bit manipulation related VM options
  67      * targeted to X86 CPU that does not support required features.
  68      *
  69      * @throws Throwable if test failed.
  70      */
  71     public void unsupportedX86CPUTestCases() throws Throwable {
  72 
  73         /*
  74           Verify that VM will successfully start up, but output will contain a
  75           warning. VM will be launched with following options:
  76           -XX:+<tested option> -version
  77         */
  78         String errorString = String.format("JVM should start with '-XX:+%s' "
  79                 + "flag, but output should contain warning.", optionName);
  80         CommandLineOptionTest.verifySameJVMStartup(
  81                 new String[] { warningMessage }, new String[] { errorMessage },
  82                 errorString, String.format("Option '%s' is unsupported.%n"
  83                 + "Warning expected to be shown.", optionName), ExitCode.OK,
  84                 CommandLineOptionTest.prepareBooleanFlag(
  85                         optionName, true));
  86 
  87         /*
  88           Verify that VM will successfully startup without any warnings.
  89           VM will be launched with following options:
  90           -XX:-<tested option> -version
  91         */
  92         errorString = String.format("JVM should start with '-XX:-%s' flag "
  93                         + "without any warnings", optionName);
  94         CommandLineOptionTest.verifySameJVMStartup(null,
  95                 new String[] { warningMessage, errorMessage },
  96                 errorString, errorString, ExitCode.OK,
  97                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
  98 
  99         /*
 100          * Verify that on unsupported CPUs option is off by default. VM will be
 101          * launched with following options: -version
 102          */
 103         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 104                 String.format("Option '%s' is expected to have default value "
 105                         + "'false' since feature required is not supported "
 106                         + "on CPU", optionName));
 107 
 108         /*
 109           Verify that on unsupported CPUs option will be off even if
 110           it was explicitly turned on by user. VM will be launched with
 111           following options: -XX:+<tested option> -version
 112         */
 113         CommandLineOptionTest.verifyOptionValueForSameVM(optionName, "false",
 114                 String.format("Option '%s' is expected to have default value"
 115                         + " 'false' since feature required is not supported on"
 116                         + " CPU even if user set another value.", optionName),
 117                 CommandLineOptionTest.prepareBooleanFlag(optionName, true));
 118 
 119     }
 120 
 121     /**
 122      * Run test cases common for all bit manipulation related VM options
 123      * targeted to non-X86 CPU that does not support required features.
 124      *
 125      * @throws Throwable if test failed.
 126      */
 127     public void unsupportedNonX86CPUTestCases() throws Throwable {
 128 
 129         /*
 130           Verify that VM known nothing about tested option. VM will be launched
 131           with following options: -XX:[+-]<tested option> -version
 132         */
 133         CommandLineOptionTest.verifySameJVMStartup(
 134                 new String[] { errorMessage }, null,
 135                 String.format("JVM startup should fail with '-XX:+%s' flag."
 136                         + "%nOption should be unknown (non-X86CPU).",
 137                         optionName), "", ExitCode.FAIL,
 138                 CommandLineOptionTest.prepareBooleanFlag(optionName, true));
 139 
 140         CommandLineOptionTest.verifySameJVMStartup(
 141                 new String[] { errorMessage }, null,
 142                 String.format("JVM startup should fail with '-XX:-%s' flag."
 143                         + "%nOption should be unknown (non-X86CPU)",
 144                         optionName), "", ExitCode.FAIL,
 145                 CommandLineOptionTest.prepareBooleanFlag(optionName, false));
 146     }
 147 }
 148