1 /*
   2  * Copyright (c) 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 package optionsvalidation;
  24 
  25 import com.sun.tools.attach.VirtualMachine;
  26 import com.sun.tools.attach.AttachOperationFailedException;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 import jdk.test.lib.DynamicVMOption;
  30 import jdk.test.lib.OutputAnalyzer;
  31 import jdk.test.lib.ProcessTools;
  32 import jdk.test.lib.dcmd.CommandExecutor;
  33 import jdk.test.lib.dcmd.JMXExecutor;
  34 import sun.tools.attach.HotSpotVirtualMachine;
  35 
  36 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  37 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  38 import static optionsvalidation.JVMOptionsUtils.VMType;
  39 
  40 public abstract class JVMOption {
  41 
  42     /**
  43      * Executor for JCMD
  44      */
  45     private final static CommandExecutor executor = new JMXExecutor();
  46 
  47     /**
  48      * Name of the tested parameter
  49      */
  50     protected String name;
  51 
  52     /**
  53      * Range is defined for option inside VM
  54      */
  55     protected boolean withRange;
  56 
  57     /**
  58      * Prepend string which added before testing option to the command line
  59      */
  60     private final List<String> prepend;
  61     private final StringBuilder prependString;
  62 
  63     protected JVMOption() {
  64         this.prepend = new ArrayList<>();
  65         prependString = new StringBuilder();
  66         withRange = false;
  67     }
  68 
  69     /**
  70      * Create JVM Option with given type and name.
  71      *
  72      * @param type type: "intx", "size_t", "uintx", "uint64_t" or "double"
  73      * @param name name of the option
  74      * @return created JVMOption
  75      */
  76     static JVMOption createVMOption(String type, String name) {
  77         JVMOption parameter;
  78 
  79         switch (type) {
  80             case "int":
  81             case "intx":
  82             case "size_t":
  83             case "uint":
  84             case "uintx":
  85             case "uint64_t":
  86                 parameter = new IntJVMOption(name, type);
  87                 break;
  88             case "double":
  89                 parameter = new DoubleJVMOption(name);
  90                 break;
  91             default:
  92                 throw new Error("Expected only \"int\", \"intx\", \"size_t\", "
  93                         + "\"uint\", \"uintx\", \"uint64_t\", or \"double\" "
  94                         + "option types! Got " + type + " type!");
  95         }
  96 
  97         return parameter;
  98     }
  99 
 100     /**
 101      * Add passed options to the prepend options of the option. Prepend options
 102      * will be added before testing option to the command line.
 103      *
 104      * @param options array of prepend options
 105      */
 106     public final void addPrepend(String... options) {
 107         String toAdd;
 108 
 109         for (String option : options) {
 110             if (option.startsWith("-")) {
 111                 toAdd = option;
 112             } else {
 113                 /* Add "-" before parameter name */
 114                 toAdd = "-" + option;
 115 
 116             }
 117             prepend.add(toAdd);
 118             prependString.append(toAdd).append(" ");
 119         }
 120     }
 121 
 122     /**
 123      * Get name of the option
 124      *
 125      * @return name of the option
 126      */
 127     final String getName() {
 128         return name;
 129     }
 130 
 131     /**
 132      * Mark this option as option which range is defined inside VM
 133      */
 134     final void optionWithRange() {
 135         withRange = true;
 136     }
 137 
 138     /**
 139      * Set new minimum option value
 140      *
 141      * @param min new minimum value
 142      */
 143     abstract void setMin(String min);
 144 
 145     /**
 146      * Get string with minimum value of the option
 147      *
 148      * @return string with minimum value of the option
 149      */
 150     abstract String getMin();
 151 
 152     /**
 153      * Set new maximum option value
 154      *
 155      * @param max new maximum value
 156      */
 157     abstract void setMax(String min);
 158 
 159     /**
 160      * Get string with maximum value of the option
 161      *
 162      * @return string with maximum value of the option
 163      */
 164     abstract String getMax();
 165 
 166     /**
 167      * Return list of strings with valid option values which used for testing
 168      * using jcmd, attach and etc.
 169      *
 170      * @return list of strings which contain valid values for option
 171      */
 172     protected abstract List<String> getValidValues();
 173 
 174     /**
 175      * Return list of strings with invalid option values which used for testing
 176      * using jcmd, attach and etc.
 177      *
 178      * @return list of strings which contain invalid values for option
 179      */
 180     protected abstract List<String> getInvalidValues();
 181 
 182     /**
 183      * Return expected error message for option with value "value" when it used
 184      * on command line with passed value
 185      *
 186      * @param value option value
 187      * @return expected error message
 188      */
 189     protected abstract String getErrorMessageCommandLine(String value);
 190 
 191     /**
 192      * Testing writeable option using DynamicVMOption isValidValue and
 193      * isInvalidValue methods
 194      *
 195      * @return number of failed tests
 196      */
 197     public int testDynamic() {
 198         DynamicVMOption option = new DynamicVMOption(name);
 199         int failedTests = 0;
 200         String origValue;
 201 
 202         if (option.isWriteable()) {
 203 
 204             System.out.println("Testing " + name + " option dynamically by DynamicVMOption");
 205 
 206             origValue = option.getValue();
 207 
 208             for (String value : getValidValues()) {
 209                 if (!option.isValidValue(value)) {
 210                     failedMessage(String.format("Option %s: Valid value \"%s\" is invalid", name, value));
 211                     failedTests++;
 212                 }
 213             }
 214 
 215             for (String value : getInvalidValues()) {
 216                 if (option.isValidValue(value)) {
 217                     failedMessage(String.format("Option %s: Invalid value \"%s\" is valid", name, value));
 218                     failedTests++;
 219                 }
 220             }
 221 
 222             option.setValue(origValue);
 223         }
 224 
 225         return failedTests;
 226     }
 227 
 228     /**
 229      * Testing writeable option using Jcmd
 230      *
 231      * @return number of failed tests
 232      */
 233     public int testJcmd() {
 234         DynamicVMOption option = new DynamicVMOption(name);
 235         int failedTests = 0;
 236         OutputAnalyzer out;
 237         String origValue;
 238 
 239         if (option.isWriteable()) {
 240 
 241             System.out.println("Testing " + name + " option dynamically by jcmd");
 242 
 243             origValue = option.getValue();
 244 
 245             for (String value : getValidValues()) {
 246                 out = executor.execute(String.format("VM.set_flag %s %s", name, value), true);
 247 
 248                 if (out.getOutput().contains(name + " error")) {
 249                     failedMessage(String.format("Option %s: Can not change "
 250                             + "option to valid value \"%s\" via jcmd", name, value));
 251                     printOutputContent(out);
 252                     failedTests++;
 253                 }
 254             }
 255 
 256             for (String value : getInvalidValues()) {
 257                 out = executor.execute(String.format("VM.set_flag %s %s", name, value), true);
 258 
 259                 if (!out.getOutput().contains(name + " error")) {
 260                     failedMessage(String.format("Option %s: Error not reported for "
 261                             + "option when it chagned to invalid value \"%s\" via jcmd", name, value));
 262                     printOutputContent(out);
 263                     failedTests++;
 264                 }
 265             }
 266 
 267             option.setValue(origValue);
 268         }
 269 
 270         return failedTests;
 271     }
 272 
 273     private boolean setFlagAttach(HotSpotVirtualMachine vm, String flagName, String flagValue) throws Exception {
 274         boolean result;
 275 
 276         try {
 277             vm.setFlag(flagName, flagValue);
 278             result = true;
 279         } catch (AttachOperationFailedException e) {
 280             result = false;
 281         }
 282 
 283         return result;
 284     }
 285 
 286     /**
 287      * Testing writeable option using attach method
 288      *
 289      * @return number of failed tests
 290      * @throws Exception if an error occurred while attaching to the target JVM
 291      */
 292     public int testAttach() throws Exception {
 293         DynamicVMOption option = new DynamicVMOption(name);
 294         int failedTests = 0;
 295         String origValue;
 296 
 297         if (option.isWriteable()) {
 298 
 299             System.out.println("Testing " + name + " option dynamically via attach");
 300 
 301             origValue = option.getValue();
 302 
 303             HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(String.valueOf(ProcessTools.getProcessId()));
 304 
 305             for (String value : getValidValues()) {
 306                 if (!setFlagAttach(vm, name, value)) {
 307                     failedMessage(String.format("Option %s: Can not change option to valid value \"%s\" via attach", name, value));
 308                     failedTests++;
 309                 }
 310             }
 311 
 312             for (String value : getInvalidValues()) {
 313                 if (setFlagAttach(vm, name, value)) {
 314                     failedMessage(String.format("Option %s: Option changed to invalid value \"%s\" via attach", name, value));
 315                     failedTests++;
 316                 }
 317             }
 318 
 319             vm.detach();
 320 
 321             option.setValue(origValue);
 322         }
 323 
 324         return failedTests;
 325     }
 326 
 327     /**
 328      * Run java with passed parameter and check the result depending on the
 329      * 'valid' parameter
 330      *
 331      * @param param tested parameter passed to the JVM
 332      * @param valid indicates whether the JVM should fail or not
 333      * @return true - if test passed
 334      * @throws Exception if java process can not be started
 335      */
 336     private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception {
 337         int exitCode;
 338         boolean result = true;
 339         String value = optionValue.substring(optionValue.lastIndexOf("=") + 1);
 340         String fullOptionString = prependString.toString() + optionValue;
 341         List<String> runJava = new ArrayList<>();
 342         OutputAnalyzer out;
 343 
 344         if (VMType != null) {
 345             runJava.add(VMType);
 346         }
 347         runJava.addAll(prepend);
 348         runJava.add(optionValue);
 349         runJava.add(JVMOptionsUtils.class.getName());
 350 
 351         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
 352 
 353         exitCode = out.getExitValue();
 354 
 355         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 356             /* Always consider "fatal error" in output as fail */
 357             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
 358             printOutputContent(out);
 359             result = false;
 360         } else if (valid == true) {
 361             if ((exitCode != 0) && (exitCode != 1)) {
 362                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
 363                 printOutputContent(out);
 364                 result = false;
 365             } else if ((exitCode == 1) && (out.getOutput().isEmpty() == true)) {
 366                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == 1)"
 367                         + ", but with empty stdout and stderr. Description of error is needed!");
 368                 result = false;
 369             } else if (out.getOutput().contains("is outside the allowed range")) {
 370                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 371                 printOutputContent(out);
 372                 result = false;
 373             }
 374         } else {
 375             // valid == false
 376             if (exitCode == 0) {
 377                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 378                 result = false;
 379             } else if (exitCode != 1) {
 380                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 381                         + exitCode + " which not equal to 1");
 382                 result = false;
 383             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 384                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 385                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 386                 printOutputContent(out);
 387                 result = false;
 388             }
 389         }
 390 
 391         System.out.println("");
 392 
 393         return result;
 394     }
 395 
 396     /**
 397      * Construct option string with passed value
 398      *
 399      * @param value parameter value
 400      * @return string containing option with passed value
 401      */
 402     private String constructOption(String value) {
 403         return "-XX:" + name + "=" + value;
 404     }
 405 
 406     /**
 407      * Return list of strings which contain options with valid values which can
 408      * be used for testing on command line
 409      *
 410      * @return list of strings which contain options with valid values
 411      */
 412     private List<String> getValidCommandLineOptions() {
 413         List<String> validParameters = new ArrayList<>();
 414 
 415         for (String value : getValidValues()) {
 416             validParameters.add(constructOption(value));
 417         }
 418 
 419         return validParameters;
 420     }
 421 
 422     /**
 423      * Return list of strings which contain options with invalid values which
 424      * can be used for testing on command line
 425      *
 426      * @return list of strings which contain options with invalid values
 427      */
 428     private List<String> getInvalidCommandLineOptions() {
 429         List<String> invalidParameters = new ArrayList<>();
 430 
 431         for (String value : getInvalidValues()) {
 432             invalidParameters.add(constructOption(value));
 433         }
 434 
 435         return invalidParameters;
 436     }
 437 
 438     /**
 439      * Perform test of the parameter. Call java with valid option values and
 440      * with invalid option values.
 441      *
 442      * @return number of failed tests
 443      * @throws Exception if java process can not be started
 444      */
 445     public int testCommandLine() throws Exception {
 446         ProcessBuilder pb;
 447         int failed = 0;
 448         List<String> optionValuesList;
 449 
 450         optionValuesList = getValidCommandLineOptions();
 451 
 452         if (optionValuesList.isEmpty() != true) {
 453             System.out.println("Testing valid " + name + " values.");
 454             for (String optionValid : optionValuesList) {
 455                 if (runJavaWithParam(optionValid, true) == false) {
 456                     failed++;
 457                 }
 458             }
 459         }
 460 
 461         optionValuesList = getInvalidCommandLineOptions();
 462 
 463         if (optionValuesList.isEmpty() != true) {
 464             System.out.println("Testing invalid " + name + " values.");
 465 
 466             for (String optionInvalid : optionValuesList) {
 467                 if (runJavaWithParam(optionInvalid, false) == false) {
 468                     failed++;
 469                 }
 470             }
 471         }
 472 
 473         /* return number of failed tests for this option */
 474         return failed;
 475     }
 476 
 477 }