Path: blob/master/test/jdk/tools/launcher/ExecutionEnvironment.java
41144 views
/*1* Copyright (c) 2009, 2020, 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 4780570 4731671 6354700 6367077 6670965 488297426* @summary Checks for LD_LIBRARY_PATH and execution on *nixes27* @requires os.family != "windows" & !vm.musl & os.family != "aix"28* @library /test/lib29* @modules jdk.compiler30* jdk.zipfs31* @compile -XDignore.symbol.file ExecutionEnvironment.java32* @run main/othervm -DexpandedLdLibraryPath=false ExecutionEnvironment33*/3435/*36* @test37* @bug 4780570 4731671 6354700 6367077 6670965 488297438* @summary Checks for LD_LIBRARY_PATH and execution on *nixes39* @requires os.family == "aix" | vm.musl40* @library /test/lib41* @modules jdk.compiler42* jdk.zipfs43* @compile -XDignore.symbol.file ExecutionEnvironment.java44* @run main/othervm -DexpandedLdLibraryPath=true ExecutionEnvironment45*/4647/*48* This tests for various things as follows:49* Ensures that:50* 1. uneccessary execs do not occur51* 2. the environment is pristine, users environment variable wrt.52* LD_LIBRARY_PATH if set are not modified in any way.53* 3. the correct vm is chosen with -server and -client options54* 4. the VM on Solaris correctly interprets the LD_LIBRARY_PATH3255* and LD_LIBRARY_PATH64 variables if set by the user, ie.56* i. on 32 bit systems:57* a. if LD_LIBRARY_PATH32 is set it will override LD_LIBRARY_PATH58* b. LD_LIBRARY_PATH64 is ignored if set59* ii. on 64 bit systems:60* a. if LD_LIBRARY_PATH64 is set it will override LD_LIBRARY_PATH61* b. LD_LIBRARY_PATH32 is ignored if set62* 5. no extra symlink exists on Solaris ie.63* lib/$arch/libjvm.so -> client/libjvm.so64* TODO:65* a. perhaps we need to add a test to audit all environment variables are66* in pristine condition after the launch, there may be a few that the67* launcher may add as implementation details.68* b. add a pldd for solaris to ensure only one libjvm.so is linked69*/7071import jdk.test.lib.Platform;7273import java.io.File;74import java.io.FileNotFoundException;75import java.util.ArrayList;76import java.util.HashMap;77import java.util.List;78import java.util.Map;7980public class ExecutionEnvironment extends TestHelper {81static final String LD_LIBRARY_PATH = Platform.sharedLibraryPathVariableName();82static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32";83static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64";8485// Note: these paths need not exist on the filesytem86static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai";87static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia";88static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India";8990static final String[] LD_PATH_STRINGS = {91LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE,92LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE,93LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE94};9596static final File testJarFile = new File("EcoFriendly.jar");9798static final boolean IS_EXPANDED_LD_LIBRARY_PATH =99Boolean.getBoolean("expandedLdLibraryPath");100101public ExecutionEnvironment() {102createTestJar();103}104105static void createTestJar() {106try {107List<String> codeList = new ArrayList<>();108codeList.add("static void printValue(String name, boolean property) {\n");109codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n");110codeList.add(" System.out.println(name + \"=\" + value);\n");111codeList.add("}\n");112codeList.add("public static void main(String... args) {\n");113codeList.add(" System.out.println(\"Execute test:\");\n");114codeList.add(" printValue(\"os.name\", true);\n");115codeList.add(" printValue(\"os.arch\", true);\n");116codeList.add(" printValue(\"os.version\", true);\n");117codeList.add(" printValue(\"sun.arch.data.model\", true);\n");118codeList.add(" printValue(\"java.library.path\", true);\n");119codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n");120codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n");121codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n");122codeList.add("}\n");123String[] clist = new String[codeList.size()];124createJar(testJarFile, codeList.toArray(clist));125} catch (FileNotFoundException fnfe) {126throw new RuntimeException(fnfe);127}128}129private void flagError(TestResult tr, String message) {130System.err.println(tr);131throw new RuntimeException(message);132}133/*134* tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there135* should not be any new variables or pollution/mutations of any kind, the136* environment should be pristine.137*/138@Test139void testEcoFriendly() {140Map<String, String> env = new HashMap<>();141for (String x : LD_PATH_STRINGS) {142String pairs[] = x.split("=");143env.put(pairs[0], pairs[1]);144}145146TestResult tr =147doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());148149if (!tr.isNotZeroOutput()) {150flagError(tr, "Error: No output at all. Did the test execute ?");151}152153for (String x : LD_PATH_STRINGS) {154if (!tr.contains(x)) {155if (IS_EXPANDED_LD_LIBRARY_PATH && x.startsWith(LD_LIBRARY_PATH)) {156// AIX does not support the '-rpath' linker options so the157// launchers have to prepend the jdk library path to 'LIBPATH'.158// The musl library loader requires LD_LIBRARY_PATH to be set in159// order to correctly resolve the dependency libjava.so has on libjvm.so.160String libPath = LD_LIBRARY_PATH + "=" +161System.getenv(LD_LIBRARY_PATH) +162System.getProperty("path.separator") + LD_LIBRARY_PATH_VALUE;163if (!tr.matches(libPath)) {164flagError(tr, "FAIL: did not get <" + libPath + ">");165}166}167else {168flagError(tr, "FAIL: did not get <" + x + ">");169}170}171}172}173174/*175* ensures that there are no execs as long as we are in the same176* data model177*/178@Test179void testNoExec() {180Map<String, String> env = new HashMap<>();181env.put(JLDEBUG_KEY, "true");182TestResult tr = doExec(env, javaCmd, "-version");183if (tr.testOutput.contains(EXPECTED_MARKER)) {184flagError(tr, "testNoExec: found warning <" + EXPECTED_MARKER +185"> the process execing ?");186}187}188189/*190* This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM191* and the expected java.library.path behaviour.192* For Generic platforms (All *nixes):193* * All LD_LIBRARY_PATH variable should be on java.library.path194*/195@Test196void testJavaLibraryPath() {197TestResult tr;198199Map<String, String> env = new HashMap<>();200201for (String x : LD_PATH_STRINGS) {202String pairs[] = x.split("=");203env.put(pairs[0], pairs[1]);204}205206tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath());207verifyJavaLibraryPathGeneric(tr);208}209210private void verifyJavaLibraryPathGeneric(TestResult tr) {211if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {212flagError(tr, "testJavaLibraryPath: java.library.path does not contain " +213LD_LIBRARY_PATH_VALUE);214}215}216217private void verifyJavaLibraryPathOverride(TestResult tr,218boolean is32Bit) {219// make sure the 32/64 bit value exists220if (!tr.matches("java.library.path=.*" +221(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) {222flagError(tr, "verifyJavaLibraryPathOverride: " +223" java.library.path does not contain " +224(is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE));225226}227// make sure the generic value is absent228if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) {229flagError(tr, "verifyJavaLibraryPathOverride: " +230" java.library.path contains " + LD_LIBRARY_PATH_VALUE);231}232}233234/*235* ensures we have indeed exec'ed the correct vm of choice if it exists236*/237@Test238void testVmSelection() {239boolean haveSomeVM = false;240if (haveClientVM) {241tryVmOption("-client", ".*Client VM.*");242haveSomeVM = true;243}244if (haveServerVM) {245tryVmOption("-server", ".*Server VM.*");246haveSomeVM = true;247}248if (!haveSomeVM) {249String msg = "Don't have a known VM";250System.err.println(msg);251throw new RuntimeException(msg);252}253}254255private void tryVmOption(String opt, String expected) {256TestResult tr = doExec(javaCmd, opt, "-version");257if (!tr.matches(expected)) {258flagError(tr, "the expected vm " + opt + " did not launch");259}260}261262/*263* checks to see there is no extra libjvm.so than needed264*/265@Test266void testNoSymLink() {267if (is64Bit) {268return;269}270271File symLink = null;272String libPathPrefix = "/lib";273symLink = new File(JAVAHOME, libPathPrefix +274getJreArch() + "/" + LIBJVM);275if (symLink.exists()) {276throw new RuntimeException("symlink exists " + symLink.getAbsolutePath());277}278}279public static void main(String... args) throws Exception {280ExecutionEnvironment ee = new ExecutionEnvironment();281ee.run(args);282}283}284285286