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