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 haveLauncherPid = false; 104 String envVarPid = null; 105 for ( String line : tr.testOutput) { 106 int index; 107 if ((index = line.indexOf(envVarPidString)) >= 0) { 108 int sindex = envVarPidString.length(); 109 envVarPid = line.substring(sindex); 110 haveLauncherPid = true; 111 break; 112 } 113 } 114 // did we find envVarPid? 115 if (!haveLauncherPid) { 116 System.out.println(tr); 117 throw new RuntimeException("Error: failed to find env Var Pid in tracking info"); 118 } 119 // we think we found the pid string. min test, not "". 120 if (envVarPid.length() < 1) { 121 System.out.println(tr); 122 throw new RuntimeException("Error: env Var Pid in tracking info is empty string"); 123 } 124 125 /* 126 * On Linux, Launcher Tracking will print the PID. Use this info 127 * to validate what we got as the PID in the Launcher itself. 128 * Linux is the only one that prints this, and trying to get it 129 * here for win is awful. So let the linux test make sure we get 130 * the valid pid, and for non-linux, just make sure pid string is 131 * non-zero. 132 */ 133 if (isLinux) { 134 // get what the test says is the launcher pid 135 String launcherPid = null; 136 for (String line : tr.testOutput) { 137 int index; 138 if ((index = line.indexOf(launcherPidString)) >= 0) { 139 int sindex = index + launcherPidString.length(); 140 int tindex = sindex + line.substring(sindex).indexOf("'"); 141 System.out.println("DEBUG INFO: sindex = " + sindex); 142 System.out.println("DEBUG INFO: searching substring: " + line.substring(sindex)); 143 System.out.println("DEBUG INFO: tindex = " + tindex); 144 // DEBUG INFO 145 System.out.println(tr); 146 launcherPid = line.substring(sindex,tindex); 147 haveLauncherPid = true; 148 break; 149 } 150 } 151 if (!haveLauncherPid) { 152 System.out.println(tr); 153 throw new RuntimeException("Error: failed to find launcher Pid in launcher tracking info"); 154 } 155 156 // did we create the env var with the correct pid? 157 if (!launcherPid.equals(envVarPid)) { 158 System.out.println(tr); 159 System.out.println("Error: wrong pid in creating env var"); 160 System.out.println("Error Info: launcherPid = " + launcherPid); 161 System.out.println("Error Info: envVarPid = " + envVarPid); 162 throw new RuntimeException("Error: wrong pid in creating env var"); 163 } 164 } 165 166 167 // --- Test 1b 168 if (!tr.contains("NativeMemoryTracking: got value "+NMT_Option_Value)) { 169 System.out.println(tr); 170 throw new RuntimeException("Error: Valid param failed to set env variable"); 171 } 172 173 // --- Test 2 174 tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking=", 175 "-version"); 176 if (tr.contains("NativeMemoryTracking:")) { 177 System.out.println(tr); 178 throw new RuntimeException("Error: invalid param caused env variable to be erroneously created"); 179 } 180 if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) { 181 System.out.println(tr); 182 throw new RuntimeException("Error: invalid param not checked by JVM"); 183 } 184 185 // --- Test 3 186 tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking", 187 "-version"); 188 if (tr.contains("NativeMemoryTracking:")) { 189 System.out.println(tr); 190 throw new RuntimeException("Error: invalid param caused env variable to be erroneously created"); 191 } 192 if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) { 193 System.out.println(tr); 194 throw new RuntimeException("Error: invalid param not checked by JVM"); 195 } 196 // --- Test 4 197 tr = doExec(envMap,javaCmd, "-XX:NativeMemoryTracking=BADVALUE", 198 "-version"); 199 if (!tr.contains("expecting -XX:NativeMemoryTracking")) { 200 System.out.println(tr); 201 throw new RuntimeException("Error: invalid param did not get JVM Syntax error message"); 202 } 203 204 } // NativeMemoryTracking 205 206 207 // MacOSX specific tests ensue...... 208 if (!isMacOSX) 209 return; 210 Set<String> envToRemove = new HashSet<>(); 211 Map<String, String> map = System.getenv(); 212 for (String s : map.keySet()) { 213 if (s.startsWith("JAVA_MAIN_CLASS_") 214 || s.startsWith("APP_NAME_") 215 || s.startsWith("APP_ICON_")) { 216 envToRemove.add(s); 217 } 218 } 219 runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(), 220 "EnvironmentVariables", "JAVA_MAIN_CLASS_*", 221 "EnvironmentVariables"); 222 223 runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(), 224 "-Xdock:name=TestAppName", "EnvironmentVariables", 225 "APP_NAME_*", "TestAppName"); 226 227 runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(), 228 "-Xdock:icon=TestAppIcon", "EnvironmentVariables", 229 "APP_ICON_*", "TestAppIcon"); 230 } 231 232 static void runTest(Set<String> envToRemove, String... args) { 233 TestResult tr = doExec(null, envToRemove, args); 234 if (!tr.isOK()) { 235 System.err.println(tr.toString()); 236 throw new RuntimeException("Test Fails"); 237 } 238 } 239 }