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 
  24 package optionsvalidation;
  25 
  26 import com.sun.tools.attach.VirtualMachine;
  27 import com.sun.tools.attach.AttachOperationFailedException;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import jdk.test.lib.DynamicVMOption;
  31 import jdk.test.lib.OutputAnalyzer;
  32 import jdk.test.lib.ProcessTools;
  33 import jdk.test.lib.dcmd.CommandExecutor;
  34 import jdk.test.lib.dcmd.JMXExecutor;
  35 import sun.tools.attach.HotSpotVirtualMachine;
  36 
  37 import static optionsvalidation.JVMOptionsUtils.failedMessage;
  38 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
  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         runJava.addAll(prepend);
 345         runJava.add(optionValue);
 346         runJava.add(JVMOptionsUtils.class.getName());
 347 
 348         out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(true, runJava.toArray(new String[0])).start());
 349 
 350         exitCode = out.getExitValue();
 351 
 352         if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
 353             /* Always consider "fatal error" in output as fail */
 354             failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
 355             printOutputContent(out);
 356             result = false;
 357         } else if (valid == true) {
 358             if ((exitCode != 0) && (exitCode != 1)) {
 359                 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
 360                 printOutputContent(out);
 361                 result = false;
 362             } else if ((exitCode == 1) && (out.getOutput().isEmpty() == true)) {
 363                 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == 1)"
 364                         + ", but with empty stdout and stderr. Description of error is needed!");
 365                 result = false;
 366             } else if (out.getOutput().contains("is outside the allowed range")) {
 367                 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
 368                 printOutputContent(out);
 369                 result = false;
 370             }
 371         } else {
 372             // valid == false
 373             if (exitCode == 0) {
 374                 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
 375                 result = false;
 376             } else if (exitCode != 1) {
 377                 failedMessage(name, fullOptionString, valid, "JVM exited with code "
 378                         + exitCode + " which not equal to 1");
 379                 result = false;
 380             } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
 381                 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
 382                         + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
 383                 printOutputContent(out);
 384                 result = false;
 385             }
 386         }
 387 
 388         System.out.println("");
 389 
 390         return result;
 391     }
 392 
 393     /**
 394      * Construct option string with passed value
 395      *
 396      * @param value parameter value
 397      * @return string containing option with passed value
 398      */
 399     private String constructOption(String value) {
 400         return "-XX:" + name + "=" + value;
 401     }
 402 
 403     /**
 404      * Return list of strings which contain options with valid values which can
 405      * be used for testing on command line
 406      *
 407      * @return list of strings which contain options with valid values
 408      */
 409     private List<String> getValidCommandLineOptions() {
 410         List<String> validParameters = new ArrayList<>();
 411 
 412         for (String value : getValidValues()) {
 413             validParameters.add(constructOption(value));
 414         }
 415 
 416         return validParameters;
 417     }
 418 
 419     /**
 420      * Return list of strings which contain options with invalid values which
 421      * can be used for testing on command line
 422      *
 423      * @return list of strings which contain options with invalid values
 424      */
 425     private List<String> getInvalidCommandLineOptions() {
 426         List<String> invalidParameters = new ArrayList<>();
 427 
 428         for (String value : getInvalidValues()) {
 429             invalidParameters.add(constructOption(value));
 430         }
 431 
 432         return invalidParameters;
 433     }
 434 
 435     /**
 436      * Perform test of the parameter. Call java with valid option values and
 437      * with invalid option values.
 438      *
 439      * @return number of failed tests
 440      * @throws Exception if java process can not be started
 441      */
 442     public int testCommandLine() throws Exception {
 443         ProcessBuilder pb;
 444         int failed = 0;
 445         List<String> optionValuesList;
 446 
 447         optionValuesList = getValidCommandLineOptions();
 448 
 449         if (optionValuesList.isEmpty() != true) {
 450             System.out.println("Testing valid " + name + " values.");
 451             for (String optionValid : optionValuesList) {
 452                 if (runJavaWithParam(optionValid, true) == false) {
 453                     failed++;
 454                 }
 455             }
 456         }
 457 
 458         optionValuesList = getInvalidCommandLineOptions();
 459 
 460         if (optionValuesList.isEmpty() != true) {
 461             System.out.println("Testing invalid " + name + " values.");
 462 
 463             for (String optionInvalid : optionValuesList) {
 464                 if (runJavaWithParam(optionInvalid, false) == false) {
 465                     failed++;
 466                 }
 467             }
 468         }
 469 
 470         /* return number of failed tests for this option */
 471         return failed;
 472     }
 473 
 474 }