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.Arrays;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32 import jdk.test.lib.DynamicVMOption;
33 import jdk.test.lib.OutputAnalyzer;
34 import jdk.test.lib.ProcessTools;
35 import jdk.test.lib.dcmd.CommandExecutor;
36 import jdk.test.lib.dcmd.JMXExecutor;
37 import sun.tools.attach.HotSpotVirtualMachine;
38
39 import static optionsvalidation.JVMOptionsUtils.failedMessage;
40 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
41 import static optionsvalidation.JVMOptionsUtils.VMType;
42
43 public abstract class JVMOption {
44
45 /**
46 * Executor for JCMD
47 */
48 private final static CommandExecutor executor = new JMXExecutor();
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
357 vm.detach();
358
359 option.setValue(origValue);
360 }
361
362 return failedTests;
363 }
364
365 /**
366 * Run java with passed parameter and check the result depending on the
367 * 'valid' parameter
368 *
369 * @param param tested parameter passed to the JVM
370 * @param valid indicates whether the JVM should fail or not
371 * @return true - if test passed
372 * @throws Exception if java process can not be started
373 */
374 private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception {
375 int exitCode;
376 boolean result = true;
377 String value = optionValue.substring(optionValue.lastIndexOf("=") + 1);
378 String fullOptionString = prependString.toString() + optionValue;
379 List<String> runJava = new ArrayList<>();
380 OutputAnalyzer out;
381
382 if (VMType != null) {
383 runJava.add(VMType);
384 }
385 runJava.addAll(prepend);
386 runJava.add(optionValue);
387 runJava.add(JVMOptionsUtils.class.getName());
388
389 out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
390
391 exitCode = out.getExitValue();
392
393 if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
394 /* Always consider "fatal error" in output as fail */
395 failedMessage(name, fullOptionString, valid, "JVM output reports a fatal error. JVM exited with code " + exitCode + "!");
396 printOutputContent(out);
397 result = false;
398 } else if (valid == true) {
399 if (!allowedExitCodes.contains(exitCode)) {
400 failedMessage(name, fullOptionString, valid, "JVM exited with unexpected error code = " + exitCode);
401 printOutputContent(out);
402 result = false;
403 } else if ((exitCode != 0) && (out.getOutput().isEmpty() == true)) {
404 failedMessage(name, fullOptionString, valid, "JVM exited with error(exitcode == " + exitCode +
405 "), but with empty stdout and stderr. Description of error is needed!");
406 result = false;
407 } else if (out.getOutput().contains("is outside the allowed range")) {
408 failedMessage(name, fullOptionString, valid, "JVM output contains \"is outside the allowed range\"");
409 printOutputContent(out);
410 result = false;
411 }
412 } else {
413 // valid == false
414 if (exitCode == 0) {
415 failedMessage(name, fullOptionString, valid, "JVM successfully exit");
416 result = false;
417 } else if (exitCode != 1) {
418 failedMessage(name, fullOptionString, valid, "JVM exited with code "
419 + exitCode + " which not equal to 1");
420 result = false;
421 } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
422 failedMessage(name, fullOptionString, valid, "JVM output does not contain "
423 + "expected output \"" + getErrorMessageCommandLine(value) + "\"");
424 printOutputContent(out);
425 result = false;
426 }
427 }
428
429 System.out.println("");
430
431 return result;
432 }
433
434 /**
435 * Construct option string with passed value
436 *
437 * @param value parameter value
438 * @return string containing option with passed value
439 */
440 private String constructOption(String value) {
441 return "-XX:" + name + "=" + value;
442 }
443
444 /**
445 * Return list of strings which contain options with valid values which can
446 * be used for testing on command line
|
1 /*
2 * Copyright (c) 2015, 2016, 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.Arrays;
29 import java.util.HashSet;
30 import java.util.List;
31 import java.util.Set;
32 import jdk.test.lib.DynamicVMOption;
33 import jdk.test.lib.OutputAnalyzer;
34 import jdk.test.lib.ProcessTools;
35 import jdk.test.lib.dcmd.CommandExecutor;
36 import jdk.test.lib.dcmd.JMXExecutor;
37 import sun.tools.attach.HotSpotVirtualMachine;
38
39 import static optionsvalidation.JVMOptionsUtils.failedMessage;
40 import static optionsvalidation.JVMOptionsUtils.GCType;
41 import static optionsvalidation.JVMOptionsUtils.printOutputContent;
42 import static optionsvalidation.JVMOptionsUtils.VMType;
43
44 public abstract class JVMOption {
45
46 /**
47 * Executor for JCMD
48 */
49 private final static CommandExecutor executor = new JMXExecutor();
50
51 /**
52 * Name of the tested parameter
53 */
54 protected String name;
55
56 /**
57 * Range is defined for option inside VM
58 */
59 protected boolean withRange;
60
358 vm.detach();
359
360 option.setValue(origValue);
361 }
362
363 return failedTests;
364 }
365
366 /**
367 * Run java with passed parameter and check the result depending on the
368 * 'valid' parameter
369 *
370 * @param param tested parameter passed to the JVM
371 * @param valid indicates whether the JVM should fail or not
372 * @return true - if test passed
373 * @throws Exception if java process can not be started
374 */
375 private boolean runJavaWithParam(String optionValue, boolean valid) throws Exception {
376 int exitCode;
377 boolean result = true;
378 String errorMessage = null;
379 List<String> runJava = new ArrayList<>();
380 OutputAnalyzer out;
381
382 if (VMType != null) {
383 runJava.add(VMType);
384 }
385
386 if (GCType != null) {
387 runJava.add(GCType);
388 }
389
390 runJava.addAll(prepend);
391 runJava.add(optionValue);
392 runJava.add(JVMStartup.class.getName());
393
394 out = new OutputAnalyzer(ProcessTools.createJavaProcessBuilder(runJava.toArray(new String[0])).start());
395
396 exitCode = out.getExitValue();
397
398 if (out.getOutput().contains("A fatal error has been detected by the Java Runtime Environment")) {
399 /* Always consider "fatal error" in output as fail */
400 errorMessage = "JVM output reports a fatal error. JVM exited with code " + exitCode + "!";
401 } else if (valid == true) {
402 if (!allowedExitCodes.contains(exitCode)) {
403 errorMessage = "JVM exited with unexpected error code = " + exitCode;
404 } else if ((exitCode != 0) && (out.getOutput().isEmpty() == true)) {
405 errorMessage = "JVM exited with error(exitcode == " + exitCode + "), but with empty stdout and stderr. " +
406 "Description of error is needed!";
407 } else if (out.getOutput().contains("is outside the allowed range")) {
408 errorMessage = "JVM output contains \"is outside the allowed range\"";
409 }
410 } else {
411 // valid == false
412 String value = optionValue.substring(optionValue.lastIndexOf("=") + 1);
413 if (exitCode == 0) {
414 errorMessage = "JVM successfully exit";
415 } else if (exitCode != 1) {
416 errorMessage = "JVM exited with code " + exitCode + " which not equal to 1";
417 } else if (!out.getOutput().contains(getErrorMessageCommandLine(value))) {
418 errorMessage = "JVM output does not contain expected output \"" + getErrorMessageCommandLine(value) + "\"";
419 }
420 }
421
422 if (errorMessage != null) {
423 String fullOptionString = String.format("%s %s %s %s",
424 VMType == null ? "" : VMType, GCType == null ? "" : GCType, prependString.toString(), optionValue).trim().replaceAll(" +", " ");
425 failedMessage(name, fullOptionString, valid, errorMessage);
426 printOutputContent(out);
427 result = false;
428 }
429
430 System.out.println("");
431
432 return result;
433 }
434
435 /**
436 * Construct option string with passed value
437 *
438 * @param value parameter value
439 * @return string containing option with passed value
440 */
441 private String constructOption(String value) {
442 return "-XX:" + name + "=" + value;
443 }
444
445 /**
446 * Return list of strings which contain options with valid values which can
447 * be used for testing on command line
|