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  *
  36  * @library /test/lib
  37  * @modules java.base/jdk.internal.misc
  38  *          java.management
  39  *          jdk.jfr
  40  *
  41  * @run main jdk.jfr.startupargs.TestBadOptionValues
  42  */
  43 public class TestBadOptionValues {
  44 
  45     private static final String START_FLIGHT_RECORDING = "-XX:StartFlightRecording=";
  46     private static final String FLIGHT_RECORDER_OPTIONS = "-XX:FlightRecorderOptions=";
  47 
  48     private static void test(String prepend, String expectedOutput, String... options) throws Exception {
  49         ProcessBuilder pb;
  50         OutputAnalyzer output;
  51 
  52         Asserts.assertGreaterThan(options.length, 0);
  53 
  54         for (String option : options) {
  55             pb = ProcessTools.createJavaProcessBuilder(prepend + option, "-version");
  56             output = new OutputAnalyzer(pb.start());
  57             output.shouldContain(expectedOutput);
  58         }
  59     }
  60 
  61     private static void testBoolean(String prepend, String... options) throws Exception {
  62         String[] splitOption;
  63 
  64         Asserts.assertGreaterThan(options.length, 0);
  65 
  66         for (String option : options) {
  67             splitOption = option.split("=", 2);
  68             test(prepend, String.format("Boolean parsing error in command argument '%s'. Could not parse: %s.", splitOption[0], splitOption[1]), option);
  69         }
  70     }
  71 
  72     private static void testJlong(String prepend, String... options) throws Exception {
  73         String[] splitOption;
  74 
  75         Asserts.assertGreaterThan(options.length, 0);
  76 
  77         for (String option : options) {
  78             splitOption = option.split("=", 2);
  79             test(prepend, String.format("Integer parsing error in command argument '%s'. Could not parse: %s.",
  80                 splitOption[0], splitOption.length > 1 ? splitOption[1]: ""), option);
  81         }
  82     }
  83 
  84     public static void main(String[] args) throws Exception {
  85         // Nanotime options
  86         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: syntax error",
  87             "delay=ms",
  88             "duration=",
  89             "maxage=q");
  90         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: syntax error, value is null",
  91             "duration");
  92         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: illegal unit",
  93             "delay=1000mq",
  94             "duration=2000mss",
  95             "maxage=-1000");
  96         test(START_FLIGHT_RECORDING, "Integer parsing error nanotime value: unit required",
  97             "delay=3037",
  98             "maxage=1");
  99 
 100         // Memory size options
 101         test(START_FLIGHT_RECORDING, "Parsing error memory size value: negative values not allowed",
 102             "maxsize=-1",
 103             "maxsize=-10k");
 104 
 105         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: negative values not allowed",
 106             "threadbuffersize=-1M",
 107             "memorysize=-1g",
 108             "globalbuffersize=-0",
 109             "maxchunksize=-");
 110 
 111         test(START_FLIGHT_RECORDING, "Parsing error memory size value: syntax error, value is null",
 112             "maxsize");
 113 
 114         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: syntax error, value is null",
 115             "threadbuffersize",
 116             "memorysize",
 117             "globalbuffersize",
 118             "maxchunksize");
 119 
 120         test(START_FLIGHT_RECORDING, "Parsing error memory size value: invalid value",
 121             "maxsize=");
 122 
 123         test(FLIGHT_RECORDER_OPTIONS, "Parsing error memory size value: invalid value",
 124             "threadbuffersize=a",
 125             "globalbuffersize=G",
 126             "maxchunksize=M10");
 127 
 128         // Jlong options
 129         testJlong(FLIGHT_RECORDER_OPTIONS,
 130             "stackdepth=q",
 131             "stackdepth=",
 132             "numglobalbuffers=10m",
 133             "numglobalbuffers",
 134             "numglobalbuffers=0x15");
 135 
 136         // Boolean options
 137         testBoolean(START_FLIGHT_RECORDING,
 138             "disk=on",
 139             "dumponexit=truee");
 140 
 141         testBoolean(FLIGHT_RECORDER_OPTIONS,
 142             "samplethreads=falseq",
 143             "retransform=0");
 144 
 145         // Not existing options
 146         test(START_FLIGHT_RECORDING, "Unknown argument 'dumponexitt' in diagnostic command.",
 147             "dumponexitt=true");
 148         test(FLIGHT_RECORDER_OPTIONS, "Unknown argument 'notexistoption' in diagnostic command.",
 149             "notexistoption");
 150     }
 151 }