1 /*
   2  * Copyright (c) 2016, 2018, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.startupargs;
  27 
  28 import jdk.test.lib.Asserts;
  29 import jdk.test.lib.process.OutputAnalyzer;
  30 import jdk.test.lib.process.ProcessTools;
  31 
  32 /**
  33  * @test
  34  * @key jfr
  35  * @requires vm.hasJFR
  36  *
  37  * @library /test/lib
  38  * @modules java.base/jdk.internal.misc
  39  *          java.management
  40  *          jdk.jfr
  41  *
  42  * @run main jdk.jfr.startupargs.TestBadOptionValues
  43  */
  44 public class TestBadOptionValues {
  45 
  46     private static final String START_FLIGHT_RECORDING = "-XX:StartFlightRecording=";
  47     private static final String FLIGHT_RECORDER_OPTIONS = "-XX:FlightRecorderOptions=";
  48 
  49     private static void test(String prepend, String expectedOutput, String... options) throws Exception {
  50         ProcessBuilder pb;
  51         OutputAnalyzer output;
  52 
  53         Asserts.assertGreaterThan(options.length, 0);
  54 
  55         for (String option : options) {
  56             pb = ProcessTools.createJavaProcessBuilder(prepend + option, "-version");
  57             output = new OutputAnalyzer(pb.start());
  58             output.shouldContain(expectedOutput);
  59         }
  60     }
  61 
  62     private static void testBoolean(String prepend, String... options) throws Exception {
  63         String[] splitOption;
  64 
  65         Asserts.assertGreaterThan(options.length, 0);
  66 
  67         for (String option : options) {
  68             splitOption = option.split("=", 2);
  69             test(prepend, String.format("Boolean parsing error in command argument '%s'. Could not parse: %s.", splitOption[0], splitOption[1]), option);
  70         }
  71     }
  72 
  73     private static void testJlong(String prepend, String... options) throws Exception {
  74         String[] splitOption;
  75 
  76         Asserts.assertGreaterThan(options.length, 0);
  77 
  78         for (String option : options) {
  79             splitOption = option.split("=", 2);
  80             test(prepend, String.format("Integer parsing error in command argument '%s'. Could not parse: %s.",
  81                 splitOption[0], splitOption.length > 1 ? splitOption[1]: ""), option);
  82         }
  83     }
  84 
  85     public static void main(String[] args) throws Exception {
  86         // Nanotime options
  87         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: syntax error",
  88             "delay=ms",
  89             "duration=",
  90             "maxage=q");
  91         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: syntax error, value is null",
  92             "duration");
  93         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: illegal unit",
  94             "delay=1000mq",
  95             "duration=2000mss",
  96             "maxage=-1000");
  97         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: unit required",
  98             "delay=3037",
  99             "maxage=1");
 100 
 101         // Memory size options
 102         test(START_FLIGHT_RECORDING, "Parsing error memory size value: negative values not allowed",
 103             "maxsize=-1",
 104             "maxsize=-10k");
 105 
 106         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: negative values not allowed",
 107             "threadbuffersize=-1M",
 108             "memorysize=-1g",
 109             "globalbuffersize=-0",
 110             "maxchunksize=-");
 111 
 112         test(START_FLIGHT_RECORDING, "Parsing error memory size value: syntax error, value is null",
 113             "maxsize");
 114 
 115         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: syntax error, value is null",
 116             "threadbuffersize",
 117             "memorysize",
 118             "globalbuffersize",
 119             "maxchunksize");
 120 
 121         test(START_FLIGHT_RECORDING, "Parsing error memory size value: invalid value",
 122             "maxsize=");
 123 
 124         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: invalid value",
 125             "threadbuffersize=a",
 126             "globalbuffersize=G",
 127             "maxchunksize=M10");
 128 
 129         // Jlong options
 130         testJlong(FLIGHT_RECORDER_OPTIONS,
 131             "stackdepth=q",
 132             "stackdepth=",
 133             "numglobalbuffers=10m",
 134             "numglobalbuffers",
 135             "numglobalbuffers=0x15");
 136 
 137         // Boolean options
 138         testBoolean(START_FLIGHT_RECORDING,
 139             "disk=on",
 140             "dumponexit=truee");
 141 
 142         testBoolean(FLIGHT_RECORDER_OPTIONS,
 143             "samplethreads=falseq",
 144             "retransform=0");
 145 
 146         // Not existing options
 147         test(START_FLIGHT_RECORDING, "Unknown argument 'dumponexitt' in diagnostic command.",
 148             "dumponexitt=true");
 149         test(FLIGHT_RECORDER_OPTIONS, "Unknown argument 'notexistoption' in diagnostic command.",
 150             "notexistoption");
 151     }
 152 }