1 /*
   2  * Copyright (c) 2012, 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 /*
  25  * @test
  26  * @bug 7124089 7131021 8042469
  27  * @summary Checks for MacOSX specific flags are accepted or rejected, and
  28  *          MacOSX platforms specific environment is consistent.
  29  * @compile -XDignore.symbol.file TestSpecialArgs.java EnvironmentVariables.java
  30  * @run main TestSpecialArgs
  31  */
  32 import java.util.HashMap;
  33 import java.util.HashSet;
  34 import java.util.Map;
  35 import java.util.Set;
  36 
  37 public class TestSpecialArgs extends TestHelper {
  38 
  39     public static void main(String... args) {
  40         final Map<String, String> envMap = new HashMap<>();
  41         envMap.put("_JAVA_LAUNCHER_DEBUG", "true");
  42 
  43         TestResult tr = doExec(envMap, javaCmd, "-XstartOnFirstThread", "-version");
  44         if (isMacOSX) {
  45             if (!tr.contains("In same thread")) {
  46                 System.out.println(tr);
  47                 throw new RuntimeException("Error: not running in the same thread ?");
  48             }
  49             if (!tr.isOK()) {
  50                 System.out.println(tr);
  51                 throw new RuntimeException("Error: arg was rejected ????");
  52             }
  53         } else {
  54             if (tr.isOK()) {
  55                 System.out.println(tr);
  56                 throw new RuntimeException("Error: argument was accepted ????");
  57             }
  58         }
  59 
  60         tr = doExec(javaCmd, "-Xdock:/tmp/not-available", "-version");
  61         if (isMacOSX) {
  62             if (!tr.isOK()) {
  63                 System.out.println(tr);
  64                 throw new RuntimeException("Error: arg was rejected ????");
  65             }
  66         } else {
  67             if (tr.isOK()) {
  68                 System.out.println(tr);
  69                 throw new RuntimeException("Error: argument was accepted ????");
  70             }
  71         }
  72 
  73         /*
  74          * test argument : -XX:NativeMemoryTracking=value
  75          * A JVM flag, comsumed by the JVM, but requiring launcher
  76          * to set an environmental variable if and only if value is supplied.
  77          * Test and order:
  78          * 1) execute with valid parameter: -XX:NativeMemoryTracking=MyValue
  79          *    a) check for correct env variable name: "NMT_LEVEL_" + pid
  80          *    b) check that "MyValue" was found in local env.
  81          * 2) execute with invalid parameter: -XX:NativeMemoryTracking=
  82          *    !) Won't find "NativeMemoryTracking:"
  83          *       Code to create env variable not executed.
  84          * 3) execute with invalid parameter: -XX:NativeMemoryTracking
  85          *    !) Won't find "NativeMemoryTracking:"
  86          *       Code to create env variable not executed.
  87          * 4) give and invalid value and check to make sure JVM commented
  88          */
  89         { // NativeMemoryTracking
  90             String launcherPidString = "launcher.pid=";
  91             String envVarPidString = "TRACER_MARKER: NativeMemoryTracking: env var is NMT_LEVEL_";
  92             String NMT_Option_Value = "off";
  93             String myClassName = "helloworld";
  94             boolean haveLauncherPid = false;
  95 
  96             // === Run the tests ===
  97 
  98             // ---Test 1a
  99             tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking=" + NMT_Option_Value,
 100                         "-version");
 101 
 102             // get the PID from the env var we set for the JVM
 103             String envVarPid = null;
 104             for (String line : tr.testOutput) {
 105                 if (line.contains(envVarPidString)) {
 106                     int sindex = envVarPidString.length();
 107                     envVarPid = line.substring(sindex);
 108                     break;
 109                 }
 110             }
 111             // did we find envVarPid?
 112             if (envVarPid == null) {
 113                 System.out.println(tr);
 114                 throw new RuntimeException("Error: failed to find env Var Pid in tracking info");
 115             }
 116             // we think we found the pid string.  min test, not "".
 117             if (envVarPid.length() < 1) {
 118                 System.out.println(tr);
 119                 throw new RuntimeException("Error: env Var Pid in tracking info is empty string");
 120             }
 121 
 122             /*
 123              * On Linux, Launcher Tracking will print the PID.  Use this info
 124              * to validate what we got as the PID in the Launcher itself.
 125              * Linux is the only one that prints this, and trying to get it
 126              * here for win is awful.  So let the linux test make sure we get
 127              * the valid pid, and for non-linux, just make sure pid string is
 128              * non-zero.
 129              */
 130             if (isLinux) {
 131                 // get what the test says is the launcher pid
 132                 String launcherPid = null;
 133                 for (String line : tr.testOutput) {
 134                     int index;
 135                     if ((index = line.indexOf(launcherPidString)) >= 0) {
 136                         int sindex = index + launcherPidString.length();
 137                         int tindex = sindex + line.substring(sindex).indexOf("'");
 138                         System.out.println("DEBUG INFO: sindex = " + sindex);
 139                         System.out.println("DEBUG INFO: searching substring: " + line.substring(sindex));
 140                         System.out.println("DEBUG INFO: tindex = " + tindex);
 141                         // DEBUG INFO
 142                         System.out.println(tr);
 143                         launcherPid = line.substring(sindex, tindex);
 144                         break;
 145                     }
 146                 }
 147                 if (launcherPid == null) {
 148                     System.out.println(tr);
 149                     throw new RuntimeException("Error: failed to find launcher Pid in launcher tracking info");
 150                 }
 151 
 152                // did we create the env var with the correct pid?
 153                 if (!launcherPid.equals(envVarPid)) {
 154                     System.out.println(tr);
 155                     System.out.println("Error: wrong pid in creating env var");
 156                     System.out.println("Error Info: launcherPid = " + launcherPid);
 157                     System.out.println("Error Info: envVarPid   = " + envVarPid);
 158                     throw new RuntimeException("Error: wrong pid in creating env var");
 159                 }
 160             }
 161                 
 162 
 163             // --- Test 1b
 164             if (!tr.contains("NativeMemoryTracking: got value " + NMT_Option_Value)) {
 165                 System.out.println(tr);
 166                 throw new RuntimeException("Error: Valid param failed to set env variable");
 167             }
 168 
 169             // --- Test 2
 170             tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking=",
 171                         "-version");
 172             if (tr.contains("NativeMemoryTracking:")) {
 173                 System.out.println(tr);
 174                 throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");
 175             }
 176             if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {
 177                 System.out.println(tr);
 178                 throw new RuntimeException("Error: invalid param not checked by JVM");
 179             }
 180 
 181             // --- Test 3
 182             tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking",
 183                         "-version");
 184             if (tr.contains("NativeMemoryTracking:")) {
 185                 System.out.println(tr);
 186                 throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");
 187             }
 188             if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {
 189                 System.out.println(tr);
 190                 throw new RuntimeException("Error: invalid param not checked by JVM");
 191             }
 192             // --- Test 4
 193             tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking=BADVALUE",
 194                         "-version");
 195             if (!tr.contains("expecting -XX:NativeMemoryTracking")) {
 196                 System.out.println(tr);
 197                 throw new RuntimeException("Error: invalid param did not get JVM Syntax error message");
 198             }
 199 
 200         } // NativeMemoryTracking
 201         
 202 
 203         // MacOSX specific tests ensue......
 204         if (!isMacOSX)
 205             return;
 206         Set<String> envToRemove = new HashSet<>();
 207         Map<String, String> map = System.getenv();
 208         for (String s : map.keySet()) {
 209             if (s.startsWith("JAVA_MAIN_CLASS_")
 210                     || s.startsWith("APP_NAME_")
 211                     || s.startsWith("APP_ICON_")) {
 212                 envToRemove.add(s);
 213             }
 214         }
 215         runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),
 216                 "EnvironmentVariables", "JAVA_MAIN_CLASS_*",
 217                 "EnvironmentVariables");
 218 
 219         runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),
 220                 "-Xdock:name=TestAppName", "EnvironmentVariables",
 221                 "APP_NAME_*", "TestAppName");
 222 
 223         runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),
 224                 "-Xdock:icon=TestAppIcon", "EnvironmentVariables",
 225                 "APP_ICON_*", "TestAppIcon");
 226     }
 227 
 228     static void runTest(Set<String> envToRemove, String... args) {
 229         TestResult tr = doExec(null, envToRemove, args);
 230         if (!tr.isOK()) {
 231             System.err.println(tr.toString());
 232             throw new RuntimeException("Test Fails");
 233         }
 234     }
 235 }