1 /*
   2  * Copyright (c) 2014, 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 import java.io.PrintWriter;
  25 import java.io.File;
  26 
  27 import com.oracle.java.testlibrary.*;
  28 
  29 /*
  30  * @test CheckCompileCommandOption
  31  * @bug 8055286 8056964 8059847 8069035
  32  * @summary "Checks parsing of -XX:CompileCommand=option"
  33  * @library /testlibrary
  34  * @modules java.base/sun.misc
  35  *          java.management
  36  * @run main CheckCompileCommandOption
  37  */
  38 
  39 public class CheckCompileCommandOption {
  40 
  41     // Currently, two types of trailing options can be used with
  42     // -XX:CompileCommand=option
  43     //
  44     // (1) CompileCommand=option,Klass::method,flag
  45     // (2) CompileCommand=option,Klass::method,type,flag,value
  46     //
  47     // Type (1) is used to enable a boolean flag for a method.
  48     //
  49     // Type (2) is used to support flags with a value. Values can
  50     // have the the following types: intx, uintx, bool, ccstr,
  51     // ccstrlist, and double.
  52 
  53     private static final String[][] FILE_ARGUMENTS = {
  54         {
  55             "-XX:CompileCommandFile=" + new File(System.getProperty("test.src", "."), "command1.txt"),
  56             "-version"
  57         },
  58         {
  59             "-XX:CompileCommandFile=" + new File(System.getProperty("test.src", "."), "command2.txt"),
  60             "-version"
  61         }
  62     };
  63 
  64     private static final String[][] FILE_EXPECTED_OUTPUT = {
  65         {
  66             "CompileCommand: option com/oracle/Test.test bool MyBoolOption1 = true",
  67             "CompileCommand: option com/oracle/Test.test bool MyBoolOption2 = true",
  68             "CompileCommand: option com/oracle/Test.test bool MyBoolOption3 = true",
  69             "CompileCommand: option com/oracle/Test.test bool MyBoolOption4 = true",
  70             "CompileCommand: option com/oracle/Test.test bool MyBoolOption5 = true",
  71             "CompileCommand: option com/oracle/Test.test bool MyBoolOption6 = true",
  72             "CompileCommand: option com/oracle/Test.test bool MyBoolOption7 = true",
  73             "CompileCommand: option com/oracle/Test.test bool MyBoolOption8 = true",
  74             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption9 = true",
  75             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption10 = true",
  76             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption11 = true",
  77             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption12 = true",
  78             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption13 = true",
  79             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption14 = true",
  80             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption15 = true",
  81             "CompileCommand: option com/oracle/Test.test(I) bool MyBoolOption16 = true"
  82         },
  83         {
  84             "CompileCommand: option Test.test const char* MyListOption = '_foo _bar'",
  85             "CompileCommand: option Test.test const char* MyStrOption = '_foo'",
  86             "CompileCommand: option Test.test bool MyBoolOption = false",
  87             "CompileCommand: option Test.test intx MyIntxOption = -1",
  88             "CompileCommand: option Test.test uintx MyUintxOption = 1",
  89             "CompileCommand: option Test.test bool MyFlag = true",
  90             "CompileCommand: option Test.test double MyDoubleOption = 1.123000"
  91         }
  92     };
  93 
  94     private static final String[][] TYPE_1_ARGUMENTS = {
  95         {
  96             "-XX:CompileCommand=option,com/oracle/Test.test,MyBoolOption1",
  97             "-XX:CompileCommand=option,com/oracle/Test,test,MyBoolOption2",
  98             "-XX:CompileCommand=option,com.oracle.Test::test,MyBoolOption3",
  99             "-XX:CompileCommand=option,com/oracle/Test::test,MyBoolOption4",
 100             "-XX:CompileCommand=option,com/oracle/Test.test,MyBoolOption5,MyBoolOption6",
 101             "-XX:CompileCommand=option,com/oracle/Test,test,MyBoolOption7,MyBoolOption8",
 102             "-version"
 103         }
 104     };
 105 
 106     private static final String[][] TYPE_1_EXPECTED_OUTPUTS = {
 107         {
 108             "CompileCommand: option com/oracle/Test.test bool MyBoolOption1 = true",
 109             "CompileCommand: option com/oracle/Test.test bool MyBoolOption2 = true",
 110             "CompileCommand: option com/oracle/Test.test bool MyBoolOption3 = true",
 111             "CompileCommand: option com/oracle/Test.test bool MyBoolOption4 = true",
 112             "CompileCommand: option com/oracle/Test.test bool MyBoolOption5 = true",
 113             "CompileCommand: option com/oracle/Test.test bool MyBoolOption6 = true",
 114             "CompileCommand: option com/oracle/Test.test bool MyBoolOption7 = true",
 115             "CompileCommand: option com/oracle/Test.test bool MyBoolOption8 = true"
 116         }
 117     };
 118 
 119     private static final String[][] TYPE_2_ARGUMENTS = {
 120         {
 121             "-XX:CompileCommand=option,Test::test,ccstrlist,MyListOption,_foo,_bar",
 122             "-XX:CompileCommand=option,Test::test,ccstr,MyStrOption,_foo",
 123             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption,false",
 124             "-XX:CompileCommand=option,Test::test,intx,MyIntxOption,-1",
 125             "-XX:CompileCommand=option,Test::test,uintx,MyUintxOption,1",
 126             "-XX:CompileCommand=option,Test::test,MyFlag",
 127             "-XX:CompileCommand=option,Test::test,double,MyDoubleOption1,1.123",
 128             "-XX:CompileCommand=option,Test.test,double,MyDoubleOption2,1.123",
 129             "-XX:CompileCommand=option,Test::test,bool,MyBoolOptionX,false,intx,MyIntxOptionX,-1,uintx,MyUintxOptionX,1,MyFlagX,double,MyDoubleOptionX,1.123",
 130             "-version"
 131         }
 132     };
 133 
 134     private static final String[][] TYPE_2_EXPECTED_OUTPUTS = {
 135         {
 136             "CompileCommand: option Test.test const char* MyListOption = '_foo _bar'",
 137             "CompileCommand: option Test.test const char* MyStrOption = '_foo'",
 138             "CompileCommand: option Test.test bool MyBoolOption = false",
 139             "CompileCommand: option Test.test intx MyIntxOption = -1",
 140             "CompileCommand: option Test.test uintx MyUintxOption = 1",
 141             "CompileCommand: option Test.test bool MyFlag = true",
 142             "CompileCommand: option Test.test double MyDoubleOption1 = 1.123000",
 143             "CompileCommand: option Test.test double MyDoubleOption2 = 1.123000",
 144             "CompileCommand: option Test.test bool MyBoolOptionX = false",
 145             "CompileCommand: option Test.test intx MyIntxOptionX = -1",
 146             "CompileCommand: option Test.test uintx MyUintxOptionX = 1",
 147             "CompileCommand: option Test.test bool MyFlagX = true",
 148             "CompileCommand: option Test.test double MyDoubleOptionX = 1.123000",
 149         }
 150     };
 151 
 152     private static final String[][] TYPE_2_INVALID_ARGUMENTS = {
 153         {
 154             // bool flag name missing
 155             "-XX:CompileCommand=option,Test::test,bool",
 156             "-version"
 157         },
 158         {
 159             // bool flag value missing
 160             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption",
 161             "-version"
 162         },
 163         {
 164             // wrong value for bool flag
 165             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption,100",
 166             "-version"
 167         },
 168         {
 169             // intx flag name missing
 170             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption,false,intx",
 171             "-version"
 172         },
 173         {
 174             // intx flag value missing
 175             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption,false,intx,MyIntOption",
 176             "-version"
 177         },
 178         {
 179             // wrong value for intx flag
 180             "-XX:CompileCommand=option,Test::test,bool,MyBoolOption,false,intx,MyIntOption,true",
 181             "-version"
 182         },
 183         {
 184             // wrong value for flag double flag
 185             "-XX:CompileCommand=option,Test::test,double,MyDoubleOption,1",
 186             "-version"
 187         }
 188     };
 189 
 190     private static void verifyValidOption(String[] arguments, String[] expected_outputs) throws Exception {
 191         ProcessBuilder pb;
 192         OutputAnalyzer out;
 193 
 194         pb = ProcessTools.createJavaProcessBuilder(arguments);
 195         out = new OutputAnalyzer(pb.start());
 196 
 197         for (String expected_output : expected_outputs) {
 198             out.shouldContain(expected_output);
 199         }
 200 
 201         out.shouldNotContain("CompileCommand: An error occured during parsing");
 202         out.shouldHaveExitValue(0);
 203     }
 204 
 205     private static void verifyInvalidOption(String[] arguments) throws Exception {
 206         ProcessBuilder pb;
 207         OutputAnalyzer out;
 208 
 209         pb = ProcessTools.createJavaProcessBuilder(arguments);
 210         out = new OutputAnalyzer(pb.start());
 211 
 212         out.shouldContain("CompileCommand: An error occured during parsing");
 213         out.shouldHaveExitValue(0);
 214     }
 215 
 216     public static void main(String[] args) throws Exception {
 217 
 218         if (TYPE_1_ARGUMENTS.length != TYPE_1_EXPECTED_OUTPUTS.length) {
 219             throw new RuntimeException("Test is set up incorrectly: length of arguments and expected outputs for type (1) options does not match.");
 220         }
 221 
 222         if (TYPE_2_ARGUMENTS.length != TYPE_2_EXPECTED_OUTPUTS.length) {
 223             throw new RuntimeException("Test is set up incorrectly: length of arguments and expected outputs for type (2) options does not match.");
 224         }
 225 
 226         // Check if type (1) options are parsed correctly
 227         for (int i = 0; i < TYPE_1_ARGUMENTS.length; i++) {
 228             verifyValidOption(TYPE_1_ARGUMENTS[i], TYPE_1_EXPECTED_OUTPUTS[i]);
 229         }
 230 
 231         // Check if type (2) options are parsed correctly
 232         for (int i = 0; i < TYPE_2_ARGUMENTS.length; i++) {
 233             verifyValidOption(TYPE_2_ARGUMENTS[i], TYPE_2_EXPECTED_OUTPUTS[i]);
 234         }
 235 
 236         // Check if error is reported for invalid type (2) options
 237         // (flags with type information specified)
 238         for (String[] arguments: TYPE_2_INVALID_ARGUMENTS) {
 239             verifyInvalidOption(arguments);
 240         }
 241 
 242         // Check if commands in command file are parsed correctly
 243         for (int i = 0; i < FILE_ARGUMENTS.length; i++) {
 244             verifyValidOption(FILE_ARGUMENTS[i], FILE_EXPECTED_OUTPUT[i]);
 245         }
 246     }
 247 }