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 39 public abstract class JVMOption { 40 41 /** 42 * Executor for JCMD 43 */ 44 private final static CommandExecutor executor = new JMXExecutor(); 45 46 /** 47 * Name of the tested parameter 48 */ 49 protected String name; 50 51 /** 52 * Range is defined for option inside VM 53 */ 54 protected boolean withRange; 55 56 /** 57 * Prepend string which added before testing option to the command line 58 */ 59 private final List<String> prepend; 60 private final StringBuilder prependString; 61 62 protected JVMOption() { 63 this.prepend = new ArrayList<>(); 64 prependString = new StringBuilder(); 65 withRange = false; 66 } 67 68 /** 69 * Create JVM Option with given type and name. 70 * 71 * @param type Type: "intx", "size_t", "uintx", "uint64_t" or "double" 72 * @param name Name of the option 73 * @return Created JVMOption 74 */ 75 static JVMOption createVMOption(String type, String name) { 76 JVMOption parameter; 77 78 switch (type) { 79 case "intx": 80 parameter = new IntJVMOption(name, IntJVMOption.IntType.INTX); 81 break; 82 case "size_t": 83 case "uintx": 84 parameter = new IntJVMOption(name, IntJVMOption.IntType.UINTX); 85 break; 86 case "uint64_t": 87 parameter = new IntJVMOption(name, IntJVMOption.IntType.UINT64_T); 88 break; 89 case "double": 90 parameter = new DoubleJVMOption(name); 91 break; 92 default: 93 throw new Error("Expected only \"intx\", \"size_t\", \"uintx\", \"uint64_t\"," 94 + " or \"double\" 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 abstract protected 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 abstract protected 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 abstract protected 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() throws Exception { 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 final 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 */ 291 public int testAttach() throws Exception { 292 DynamicVMOption option = new DynamicVMOption(name); 293 int failedTests = 0; 294 String origValue; 295 296 if (option.isWriteable()) { 297 298 System.out.println("Testing " + name + " option dynamically via attach"); 299 300 origValue = option.getValue(); 301 302 HotSpotVirtualMachine vm = (HotSpotVirtualMachine) VirtualMachine.attach(String.valueOf(ProcessTools.getProcessId())); 303 304 for (String value : getValidValues()) { 305 if (!setFlagAttach(vm, name, value)) { 306 failedMessage(String.format("Option %s: Can not change option to valid value \"%s\" via attach", name, value)); 307 failedTests++; 308 } 309 } 310 311 for (String value : getInvalidValues()) { 312 if (setFlagAttach(vm, name, value)) { 313 failedMessage(String.format("Option %s: Option changed to invalid value \"%s\" via attach", name, value)); 314 failedTests++; 315 } 316 } 317 318 vm.detach(); 319 320 option.setValue(origValue); 321 } 322 323 return failedTests; 324 } 325 326 /** 327 * Run java with passed parameter and check the result depending on the 328 * 'valid' parameter 329 * 330 * @param param Tested parameter passed to the java 331 * @param valid Indicates, should JVM failed or not 332 * @return true - if test passed 333 * @throws Exception Java process can not be started 334 */ 335 private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception { 336 int returnCode; 337 boolean result = true; 338 String value = optionValue.substring(optionValue.lastIndexOf("=") + 1); 339 String fullOptionString = prependString.toString() + optionValue; 340 List<String> runJava = new ArrayList<>(); 341 OutputAnalyzer out; 342 343 runJava.addAll(prepend); 344 runJava.add(optionValue); 345 runJava.add(JVMOptionsUtils.class.getName()); 346 347 out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(true, runJava.toArray(new String[0])).start()); 348 349 returnCode = out.getExitValue(); 350 351 if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) { 352 /* Always consider "fatal error" in output as fail */ 353 failedMessage(name, fullOptionString, valid, "JVM output reports about fatal error. JVM exit with code " + returnCode + "!"); 354 printOutputContent(out); 355 result = false; 356 } else if (valid == true) { 357 if ((returnCode != 0) && (returnCode != 1)) { 358 failedMessage(name, fullOptionString, valid, "JVM exit with unexpected error code = " + returnCode); 359 printOutputContent(out); 360 result = false; 361 } else if (returnCode == 1 && out.getOutput().isEmpty()) { 362 failedMessage(name, fullOptionString, valid, "JVM exit with error(exitcode == 1)" 363 + ", but with empty stdout and stderr. Description of error is needed!"); 364 result = false; 365 } else if (out.getOutput().contains("is outside the allowed range")) { 366 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\""); 367 printOutputContent(out); 368 result = false; 369 } 370 } else { 371 // valid == false 372 if (returnCode == 0) { 373 failedMessage(name, fullOptionString, valid, "JVM successfully exit"); 374 result = false; 375 } else if (returnCode != 1) { 376 failedMessage(name, fullOptionString, valid, "JVM exit with code " 377 + returnCode + " which not equal to 1"); 378 result = false; 379 } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) { 380 failedMessage(name, fullOptionString, valid, "JVM output not contains " 381 + "expected output \"" + getErrorMessageCommandLine(value) + "\""); 382 printOutputContent(out); 383 result = false; 384 } 385 } 386 387 System.out.println(""); 388 389 return result; 390 } 391 392 /** 393 * Construct option string with passed value 394 * 395 * @param value Parameter value 396 * @return String containing option with passed value 397 */ 398 private String constructOption(String value) { 399 return "-XX:" + name + "=" + value; 400 } 401 402 /** 403 * Return list of strings which contain options with valid values which used 404 * for testing on command line 405 * 406 * @return List of strings which contain options with valid values 407 */ 408 private List<String> getValidCommandLineOptions() { 409 List<String> validParameters = new ArrayList<>(); 410 411 for (String value : getValidValues()) { 412 validParameters.add(constructOption(value)); 413 } 414 415 return validParameters; 416 } 417 418 /** 419 * Return list of strings which contain options with invalid values which 420 * used for testing on command line 421 * 422 * @return List of strings which contain options with invalid values 423 */ 424 private List<String> getInvalidCommandLineOptions() { 425 List<String> invalidParameters = new ArrayList<>(); 426 427 for (String value : getInvalidValues()) { 428 invalidParameters.add(constructOption(value)); 429 } 430 431 return invalidParameters; 432 } 433 434 /** 435 * Perform test of the parameter. Call java with valid option values and 436 * with invalid option values. 437 * 438 * @return Number of failed tests 439 * @throws Exception Java process can not be started 440 */ 441 public int testCommandLine() throws Exception { 442 ProcessBuilder pb; 443 int failed = 0; 444 List<String> optionValuesList; 445 446 optionValuesList = getValidCommandLineOptions(); 447 448 if (optionValuesList.isEmpty() != true) { 449 System.out.println("Testing valid " + name + " values."); 450 for (String optionValid : optionValuesList) { 451 if (runJavaWithParam(optionValid, true) == false) { 452 failed++; 453 } 454 } 455 } 456 457 optionValuesList = getInvalidCommandLineOptions(); 458 459 if (optionValuesList.isEmpty() != true) { 460 System.out.println("Testing invalid " + name + " values."); 461 462 for (String optionInvalid : optionValuesList) { 463 if (runJavaWithParam(optionInvalid, false) == false) { 464 failed++; 465 } 466 } 467 } 468 469 /* return number of failed tests for this option */ 470 return failed; 471 } 472 473 }