1945N/A/*
4651N/A * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
1945N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1945N/A *
1945N/A * This code is free software; you can redistribute it and/or modify it
1945N/A * under the terms of the GNU General Public License version 2 only, as
1945N/A * published by the Free Software Foundation.
1945N/A *
1945N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1945N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1945N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1945N/A * version 2 for more details (a copy is included in the LICENSE file that
1945N/A * accompanied this code).
1945N/A *
1945N/A * You should have received a copy of the GNU General Public License version
1945N/A * 2 along with this work; if not, write to the Free Software Foundation,
1945N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1945N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1945N/A */
1945N/A
1945N/A/*
1945N/A * @test
1945N/A * @bug 4780570 4731671 6354700 6367077 6670965 4882974
1945N/A * @summary Checks for LD_LIBRARY_PATH and execution on *nixes
1945N/A * @compile -XDignore.symbol.file ExecutionEnvironment.java TestHelper.java
1945N/A * @run main ExecutionEnvironment
1945N/A */
1945N/A
1945N/A/*
3986N/A * This tests for various things as follows:
1945N/A * Ensures that:
1945N/A * 1. uneccessary execs do not occur
1945N/A * 2. the environment is pristine, users environment variable wrt.
1945N/A * LD_LIBRARY_PATH if set are not modified in any way.
1945N/A * 3. the correct vm is chosen with -server and -client options
1945N/A * 4. the VM on Solaris correctly interprets the LD_LIBRARY_PATH32
1945N/A * and LD_LIBRARY_PATH64 variables if set by the user, ie.
1945N/A * i. on 32 bit systems:
1945N/A * a. if LD_LIBRARY_PATH32 is set it will override LD_LIBRARY_PATH
1945N/A * b. LD_LIBRARY_PATH64 is ignored if set
1945N/A * ii. on 64 bit systems:
1945N/A * a. if LD_LIBRARY_PATH64 is set it will override LD_LIBRARY_PATH
1945N/A * b. LD_LIBRARY_PATH32 is ignored if set
1945N/A * 5. no extra symlink exists on Solaris ie.
1945N/A * jre/lib/$arch/libjvm.so -> client/libjvm.so
1945N/A * TODO:
1945N/A * a. perhaps we need to add a test to audit all environment variables are
1945N/A * in pristine condition after the launch, there may be a few that the
1945N/A * launcher may add as implementation details.
1945N/A * b. add a pldd for solaris to ensure only one libjvm.so is linked
1945N/A */
1945N/Aimport java.io.File;
1945N/Aimport java.io.FileNotFoundException;
1945N/Aimport java.util.ArrayList;
1945N/Aimport java.util.HashMap;
1945N/Aimport java.util.List;
1945N/Aimport java.util.Map;
1945N/A
1945N/A
1945N/Apublic class ExecutionEnvironment {
4651N/A static final String LD_LIBRARY_PATH = TestHelper.isMacOSX
4651N/A ? "DYLD_LIBRARY_PATH"
4651N/A : "LD_LIBRARY_PATH";
1945N/A static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32";
1945N/A static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64";
1945N/A
1945N/A // Note: these paths need not exist on the filesytem
1945N/A static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai";
1945N/A static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia";
1945N/A static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India";
1945N/A
1945N/A static final String JLDEBUG_KEY = "_JAVA_LAUNCHER_DEBUG";
1945N/A static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC";
1945N/A
1945N/A static final String[] LD_PATH_STRINGS = {
1945N/A LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE,
1945N/A LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE,
1945N/A LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE
1945N/A };
1945N/A
1945N/A static final File testJarFile = new File("EcoFriendly.jar");
1945N/A
1945N/A static int errors = 0;
1945N/A static int passes = 0;
1945N/A
4651N/A static final String LIBJVM = TestHelper.isWindows
4651N/A ? "jvm.dll"
4651N/A : "libjvm" + (TestHelper.isMacOSX ? ".dylib" : ".so");
3986N/A
3986N/A static void createTestJar() {
1945N/A try {
1945N/A List<String> codeList = new ArrayList<String>();
1945N/A codeList.add("static void printValue(String name, boolean property) {\n");
1945N/A codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n");
1945N/A codeList.add(" System.out.println(name + \"=\" + value);\n");
1945N/A codeList.add("}\n");
1945N/A codeList.add("public static void main(String... args) {\n");
1945N/A codeList.add(" System.out.println(\"Execute test:\");\n");
1945N/A codeList.add(" printValue(\"os.name\", true);\n");
1945N/A codeList.add(" printValue(\"os.arch\", true);\n");
1945N/A codeList.add(" printValue(\"os.version\", true);\n");
1945N/A codeList.add(" printValue(\"sun.arch.data.model\", true);\n");
1945N/A codeList.add(" printValue(\"java.library.path\", true);\n");
1945N/A codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n");
1945N/A codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n");
1945N/A codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n");
1945N/A codeList.add("}\n");
1945N/A String[] clist = new String[codeList.size()];
1945N/A TestHelper.createJar(testJarFile, codeList.toArray(clist));
1945N/A } catch (FileNotFoundException fnfe) {
1945N/A throw new RuntimeException(fnfe);
1945N/A }
1945N/A }
1945N/A
1945N/A /*
1945N/A * tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there
1945N/A * should not be any new variables or pollution/mutations of any kind, the
1945N/A * environment should be pristine.
1945N/A */
1945N/A private static void ensureEcoFriendly() {
1945N/A TestHelper.TestResult tr = null;
1945N/A
1945N/A Map<String, String> env = new HashMap<String, String>();
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A String pairs[] = x.split("=");
1945N/A env.put(pairs[0], pairs[1]);
1945N/A }
1945N/A
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A
1945N/A if (!tr.isNotZeroOutput()) {
3986N/A System.out.println(tr);
1945N/A throw new RuntimeException("Error: No output at all. Did the test execute ?");
1945N/A }
1945N/A
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A if (!tr.contains(x)) {
1945N/A System.out.println("FAIL: did not get <" + x + ">");
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A }
1945N/A
1945N/A /*
1945N/A * ensures that there are no execs as long as we are in the same
1945N/A * data model
1945N/A */
1945N/A static void ensureNoExec() {
1945N/A Map<String, String> env = new HashMap<String, String>();
1945N/A env.put(JLDEBUG_KEY, "true");
1945N/A TestHelper.TestResult tr =
1945N/A TestHelper.doExec(env, TestHelper.javaCmd, "-version");
1945N/A if (tr.testOutput.contains(EXPECTED_MARKER)) {
1945N/A System.out.println("FAIL: EnsureNoExecs: found expected warning <" +
1945N/A EXPECTED_MARKER +
1945N/A "> the process execing ?");
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A return;
1945N/A }
1945N/A
1945N/A /*
1945N/A * This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM
1945N/A * and the expected java.library.path behaviour.
1945N/A * For Generic platforms (All *nixes):
1945N/A * * All LD_LIBRARY_PATH variable should be on java.library.path
1945N/A * For Solaris 32-bit
1945N/A * * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified
1945N/A * For Solaris 64-bit
1945N/A * * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified
1945N/A */
1945N/A
1945N/A static void verifyJavaLibraryPath() {
1945N/A TestHelper.TestResult tr = null;
1945N/A
1945N/A Map<String, String> env = new HashMap<String, String>();
1945N/A
4651N/A if (TestHelper.isLinux || TestHelper.isMacOSX) {
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A String pairs[] = x.split("=");
1945N/A env.put(pairs[0], pairs[1]);
1945N/A }
1945N/A
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathGeneric(tr);
1945N/A } else {
1945N/A // no override
1945N/A env.clear();
1945N/A env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathGeneric(tr);
1945N/A
1945N/A env.clear();
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A String pairs[] = x.split("=");
1945N/A env.put(pairs[0], pairs[1]);
1945N/A }
1945N/A
1945N/A // verify the override occurs, since we know the invocation always
1945N/A // uses by default is 32-bit, therefore we also set the test
1945N/A // expectation to be the same.
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathOverride(tr, true);
1945N/A
1945N/A // try changing the model from 32 to 64 bit
3986N/A if (TestHelper.dualModePresent() && TestHelper.is32Bit) {
1945N/A // verify the override occurs
1945N/A env.clear();
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A String pairs[] = x.split("=");
1945N/A env.put(pairs[0], pairs[1]);
1945N/A }
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-d64", "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathOverride(tr, false);
1945N/A
1945N/A // no override
1945N/A env.clear();
1945N/A env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);
1945N/A tr = TestHelper.doExec(env, TestHelper.javaCmd, "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathGeneric(tr);
1945N/A }
1945N/A
1945N/A // try changing the model from 64 to 32 bit
1945N/A if (TestHelper.java64Cmd != null && TestHelper.is64Bit) {
1945N/A // verify the override occurs
1945N/A env.clear();
1945N/A for (String x : LD_PATH_STRINGS) {
1945N/A String pairs[] = x.split("=");
1945N/A env.put(pairs[0], pairs[1]);
1945N/A }
1945N/A tr = TestHelper.doExec(env, TestHelper.java64Cmd, "-d32", "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathOverride(tr, true);
1945N/A
1945N/A // no override
1945N/A env.clear();
1945N/A env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE);
1945N/A tr = TestHelper.doExec(env, TestHelper.java64Cmd, "-d32", "-jar",
1945N/A testJarFile.getAbsolutePath());
1945N/A verifyJavaLibraryPathGeneric(tr);
1945N/A }
1945N/A }
1945N/A }
1945N/A
1945N/A private static void verifyJavaLibraryPathGeneric(TestHelper.TestResult tr) {
1945N/A if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
1945N/A System.out.print("FAIL: verifyJavaLibraryPath: ");
1945N/A System.out.println(" java.library.path does not contain " +
1945N/A LD_LIBRARY_PATH_VALUE);
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A
1945N/A private static void verifyJavaLibraryPathOverride(TestHelper.TestResult tr,
1945N/A boolean is32Bit) {
1945N/A // make sure the 32/64 bit value exists
1945N/A if (!tr.matches("java.library.path=.*" +
1945N/A (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) {
1945N/A System.out.print("FAIL: verifyJavaLibraryPathOverride: ");
1945N/A System.out.println(" java.library.path does not contain " +
1945N/A (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE));
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A // make sure the generic value is absent
1945N/A if (tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {
1945N/A System.out.print("FAIL: verifyJavaLibraryPathOverride: ");
1945N/A System.out.println(" java.library.path contains " +
1945N/A LD_LIBRARY_PATH_VALUE);
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A
1945N/A /*
1945N/A * ensures we have indeed exec'ed the correct vm of choice, all VMs support
1945N/A * -server, however 32-bit VMs support -client and -server.
1945N/A */
1945N/A static void verifyVmSelection() {
1945N/A
1945N/A TestHelper.TestResult tr = null;
1945N/A
1945N/A if (TestHelper.is32Bit) {
1945N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-client", "-version");
4651N/A if (!tr.matches(".*Client VM.*")) {
1945N/A System.out.println("FAIL: the expected vm -client did launch");
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A tr = TestHelper.doExec(TestHelper.javaCmd, "-server", "-version");
4651N/A if (!tr.matches(".*Server VM.*")) {
1945N/A System.out.println("FAIL: the expected vm -server did launch");
1945N/A System.out.println(tr);
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A
1945N/A /*
1945N/A * checks to see there is no extra libjvm.so than needed
1945N/A */
1945N/A static void verifyNoSymLink() {
1945N/A if (TestHelper.is64Bit) {
1945N/A return;
1945N/A }
1945N/A
1945N/A File symLink = null;
1945N/A String libPathPrefix = TestHelper.isSDK ? "jre/lib" : "/lib";
1945N/A symLink = new File(TestHelper.JAVAHOME, libPathPrefix +
3986N/A TestHelper.getJreArch() + "/" + LIBJVM);
1945N/A if (symLink.exists()) {
1945N/A System.out.println("FAIL: The symlink exists " +
1945N/A symLink.getAbsolutePath());
1945N/A errors++;
1945N/A } else {
1945N/A passes++;
1945N/A }
1945N/A }
1945N/A
1945N/A public static void main(String... args) throws Exception {
1945N/A if (TestHelper.isWindows) {
1945N/A System.out.println("Warning: noop on windows");
1945N/A return;
1945N/A }
1945N/A // create our test jar first
1945N/A createTestJar();
1945N/A ensureNoExec();
1945N/A verifyVmSelection();
1945N/A ensureEcoFriendly();
1945N/A verifyJavaLibraryPath();
1945N/A verifyNoSymLink();
1945N/A if (errors > 0) {
1945N/A throw new Exception("ExecutionEnvironment: FAIL: with " +
1945N/A errors + " errors and passes " + passes );
1945N/A } else {
1945N/A System.out.println("ExecutionEnvironment: PASS " + passes);
1945N/A }
1945N/A }
1945N/A}