Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/TestSpecialArgs.java
38833 views
/*1* Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 7124089 7131021 8042469 8066185 807437326* @summary Checks for Launcher special flags, such as MacOSX specific flags,27* and JVM NativeMemoryTracking flags.28* @compile -XDignore.symbol.file TestSpecialArgs.java EnvironmentVariables.java29* @run main TestSpecialArgs30*/31import java.io.File;32import java.io.FileNotFoundException;33import java.util.HashMap;34import java.util.HashSet;35import java.util.Map;36import java.util.Set;3738public class TestSpecialArgs extends TestHelper {3940public static void main(String... args) throws Exception {41new TestSpecialArgs().run(args);42}4344@Test45void testDocking() {46final Map<String, String> envMap = new HashMap<>();47envMap.put("_JAVA_LAUNCHER_DEBUG", "true");48TestResult tr = doExec(envMap, javaCmd, "-XstartOnFirstThread", "-version");49if (isMacOSX) {50if (!tr.contains("In same thread")) {51System.out.println(tr);52throw new RuntimeException("Error: not running in the same thread ?");53}54if (!tr.isOK()) {55System.out.println(tr);56throw new RuntimeException("Error: arg was rejected ????");57}58} else {59if (tr.isOK()) {60System.out.println(tr);61throw new RuntimeException("Error: argument was accepted ????");62}63}6465tr = doExec(javaCmd, "-Xdock:/tmp/not-available", "-version");66if (isMacOSX) {67if (!tr.isOK()) {68System.out.println(tr);69throw new RuntimeException("Error: arg was rejected ????");70}71} else {72if (tr.isOK()) {73System.out.println(tr);74throw new RuntimeException("Error: argument was accepted ????");75}76}77// MacOSX specific tests ensue......78if (!isMacOSX) {79return;80}81Set<String> envToRemove = new HashSet<>();82Map<String, String> map = System.getenv();83for (String s : map.keySet()) {84if (s.startsWith("JAVA_MAIN_CLASS_")85|| s.startsWith("APP_NAME_")86|| s.startsWith("APP_ICON_")) {87envToRemove.add(s);88}89}90runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),91"EnvironmentVariables", "JAVA_MAIN_CLASS_*",92"EnvironmentVariables");9394runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),95"-Xdock:name=TestAppName", "EnvironmentVariables",96"APP_NAME_*", "TestAppName");9798runTest(envToRemove, javaCmd, "-cp", TEST_CLASSES_DIR.getAbsolutePath(),99"-Xdock:icon=TestAppIcon", "EnvironmentVariables",100"APP_ICON_*", "TestAppIcon");101}102103void runTest(Set<String> envToRemove, String... args) {104TestResult tr = doExec(null, envToRemove, args);105if (!tr.isOK()) {106System.err.println(tr.toString());107throw new RuntimeException("Test Fails");108}109}110111@Test112void testNativeMemoryTracking() {113final Map<String, String> envMap = new HashMap<>();114envMap.put("_JAVA_LAUNCHER_DEBUG", "true");115TestResult tr;116/*117* test argument : -XX:NativeMemoryTracking=value118* A JVM flag, comsumed by the JVM, but requiring launcher119* to set an environmental variable if and only if value is supplied.120* Test and order:121* 1) execute with valid parameter: -XX:NativeMemoryTracking=MyValue122* a) check for correct env variable name: "NMT_LEVEL_" + pid123* b) check that "MyValue" was found in local env.124* 2) execute with invalid parameter: -XX:NativeMemoryTracking=125* !) Won't find "NativeMemoryTracking:"126* Code to create env variable not executed.127* 3) execute with invalid parameter: -XX:NativeMemoryTracking128* !) Won't find "NativeMemoryTracking:"129* Code to create env variable not executed.130* 4) give and invalid value and check to make sure JVM commented131*/132String launcherPidString = "launcher.pid=";133String envVarPidString = "TRACER_MARKER: NativeMemoryTracking: env var is NMT_LEVEL_";134String NMT_Option_Value = "off";135String myClassName = "helloworld";136boolean haveLauncherPid = false;137138// === Run the tests ===139// ---Test 1a140tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=" + NMT_Option_Value,141"-version");142143// get the PID from the env var we set for the JVM144String envVarPid = null;145for (String line : tr.testOutput) {146if (line.contains(envVarPidString)) {147int sindex = envVarPidString.length();148envVarPid = line.substring(sindex);149break;150}151}152// did we find envVarPid?153if (envVarPid == null) {154System.out.println(tr);155throw new RuntimeException("Error: failed to find env Var Pid in tracking info");156}157// we think we found the pid string. min test, not "".158if (envVarPid.length() < 1) {159System.out.println(tr);160throw new RuntimeException("Error: env Var Pid in tracking info is empty string");161}162163/*164* On Linux, Launcher Tracking will print the PID. Use this info165* to validate what we got as the PID in the Launcher itself.166* Linux is the only one that prints this, and trying to get it167* here for win is awful. So let the linux test make sure we get168* the valid pid, and for non-linux, just make sure pid string is169* non-zero.170*/171if (isLinux) {172// get what the test says is the launcher pid173String launcherPid = null;174for (String line : tr.testOutput) {175int index = line.indexOf(launcherPidString);176if (index >= 0) {177int sindex = index + launcherPidString.length();178int tindex = sindex + line.substring(sindex).indexOf("'");179System.out.println("DEBUG INFO: sindex = " + sindex);180System.out.println("DEBUG INFO: searching substring: " + line.substring(sindex));181System.out.println("DEBUG INFO: tindex = " + tindex);182// DEBUG INFO183System.out.println(tr);184launcherPid = line.substring(sindex, tindex);185break;186}187}188if (launcherPid == null) {189System.out.println(tr);190throw new RuntimeException("Error: failed to find launcher Pid in launcher tracking info");191}192193// did we create the env var with the correct pid?194if (!launcherPid.equals(envVarPid)) {195System.out.println(tr);196System.out.println("Error: wrong pid in creating env var");197System.out.println("Error Info: launcherPid = " + launcherPid);198System.out.println("Error Info: envVarPid = " + envVarPid);199throw new RuntimeException("Error: wrong pid in creating env var");200}201}202203// --- Test 1b204if (!tr.contains("NativeMemoryTracking: got value " + NMT_Option_Value)) {205System.out.println(tr);206throw new RuntimeException("Error: Valid param failed to set env variable");207}208209// --- Test 2210tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=",211"-version");212if (tr.contains("NativeMemoryTracking:")) {213System.out.println(tr);214throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");215}216if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {217System.out.println(tr);218throw new RuntimeException("Error: invalid param not checked by JVM");219}220221// --- Test 3222tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking",223"-version");224if (tr.contains("NativeMemoryTracking:")) {225System.out.println(tr);226throw new RuntimeException("Error: invalid param caused env variable to be erroneously created");227}228if (!tr.contains("Syntax error, expecting -XX:NativeMemoryTracking=")) {229System.out.println(tr);230throw new RuntimeException("Error: invalid param not checked by JVM");231}232// --- Test 4233tr = doExec(envMap, javaCmd, "-XX:NativeMemoryTracking=BADVALUE",234"-version");235if (!tr.contains("expecting -XX:NativeMemoryTracking")) {236System.out.println(tr);237throw new RuntimeException("Error: invalid param did not get JVM Syntax error message");238}239}240241@Test242void testNMArgumentProcessing() throws FileNotFoundException {243TestResult tr = null;244// the direct invokers of the VM245String options[] = {246"-version", "-fullversion", "-help", "-?", "-X"247};248for (String option : options) {249tr = doExec(javaCmd, option, "-XX:NativeMemoryTracking=summary");250checkTestResult(tr);251}252253// create a test jar254File jarFile = new File("test.jar");255createJar(jarFile, "public static void main(String... args){}");256257// ones that involve main-class of some sort258tr = doExec(javaCmd, "-jar", jarFile.getName(),259"-XX:NativeMemoryTracking=summary");260checkTestResult(tr);261262tr = doExec(javaCmd, "-cp", jarFile.getName(), "Foo",263"-XX:NativeMemoryTracking=summary");264checkTestResult(tr);265266final Map<String, String> envMap = new HashMap<>();267// checkwith CLASSPATH set ie. no -cp or -classpath268envMap.put("CLASSPATH", ".");269tr = doExec(envMap, javaCmd, "Foo", "-XX:NativeMemoryTracking=summary");270checkTestResult(tr);271272// should accept with no warnings273tr = doExec(javaCmd, "-cp", jarFile.getName(),274"-XX:NativeMemoryTracking=summary", "Foo");275ensureNoWarnings(tr);276277// should accept with no warnings278tr = doExec(javaCmd, "-classpath", jarFile.getName(),279"-XX:NativeMemoryTracking=summary", "Foo");280ensureNoWarnings(tr);281282// make sure a missing class is handled correctly, because the class283// resolution is performed by the JVM.284tr = doExec(javaCmd, "AbsentClass", "-XX:NativeMemoryTracking=summary");285if (!tr.contains("Error: Could not find or load main class AbsentClass")) {286throw new RuntimeException("Test Fails");287}288}289290void ensureNoWarnings(TestResult tr) {291checkTestResult(tr);292if (tr.contains("warning: Native Memory Tracking")) {293System.err.println(tr.toString());294throw new RuntimeException("Test Fails");295}296}297298void checkTestResult(TestResult tr) {299if (!tr.isOK()) {300System.err.println(tr.toString());301throw new RuntimeException("Test Fails");302}303}304}305306307