Path: blob/master/test/jdk/tools/launcher/Test7029048.java
41144 views
/*1* Copyright (c) 2011, 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 7029048 8217340 821721626* @summary Ensure that the launcher defends against user settings of the27* LD_LIBRARY_PATH environment variable on Unixes28* @requires os.family != "windows" & os.family != "mac" & !vm.musl & os.family != "aix"29* @library /test/lib30* @compile -XDignore.symbol.file ExecutionEnvironment.java Test7029048.java31* @run main/othervm -DexpandedLdLibraryPath=false Test702904832*/3334/**35* @test36* @bug 7029048 8217340 821721637* @summary Ensure that the launcher defends against user settings of the38* LD_LIBRARY_PATH environment variable on Unixes39* @requires os.family == "aix" | vm.musl40* @library /test/lib41* @compile -XDignore.symbol.file ExecutionEnvironment.java Test7029048.java42* @run main/othervm -DexpandedLdLibraryPath=true Test702904843*/4445import java.io.File;46import java.io.IOException;47import java.nio.file.Files;48import java.util.ArrayList;49import java.util.HashMap;50import java.util.List;51import java.util.Map;5253public class Test7029048 extends TestHelper {5455private static final String LIBJVM = ExecutionEnvironment.LIBJVM;56private static final String LD_LIBRARY_PATH =57ExecutionEnvironment.LD_LIBRARY_PATH;58private static final String LD_LIBRARY_PATH_64 =59ExecutionEnvironment.LD_LIBRARY_PATH_64;6061private static final File libDir =62new File(System.getProperty("sun.boot.library.path"));63private static final File srcServerDir = new File(libDir, "server");64private static final File srcLibjvmSo = new File(srcServerDir, LIBJVM);6566private static final File dstLibDir = new File("lib");67private static final File dstServerDir = new File(dstLibDir, "server");68private static final File dstServerLibjvm = new File(dstServerDir, LIBJVM);6970private static final File dstClientDir = new File(dstLibDir, "client");71private static final File dstClientLibjvm = new File(dstClientDir, LIBJVM);7273static final boolean IS_EXPANDED_LD_LIBRARY_PATH =74Boolean.getBoolean("expandedLdLibraryPath");7576static String getValue(String name, List<String> in) {77for (String x : in) {78String[] s = x.split("=");79if (name.equals(s[0].trim())) {80return s[1].trim();81}82}83return null;84}8586static boolean run(int nLLPComponents, File variantDir, String caseID) {8788Map<String, String> env = new HashMap<>();89env.put(LD_LIBRARY_PATH, variantDir.getAbsolutePath());90env.put(ExecutionEnvironment.JLDEBUG_KEY, "true");91List<String> cmdsList = new ArrayList<>();92cmdsList.add(javaCmd);93cmdsList.add("-server");94cmdsList.add("-jar");95cmdsList.add(ExecutionEnvironment.testJarFile.getAbsolutePath());96String[] cmds = new String[cmdsList.size()];97TestResult tr = doExec(env, cmdsList.toArray(cmds));98System.out.println(tr);99int len = getLLPComponents(tr);100if (len == nLLPComponents) {101System.out.printf("Test7029048 OK %s%n", caseID);102return true;103} else {104System.out.printf("Test7029048 FAIL %s: expected %d but got %d%n",105caseID, nLLPComponents, len);106return false;107}108}109110static int getLLPComponents(TestResult tr) {111String envValue = getValue(LD_LIBRARY_PATH, tr.testOutput);112/*113* the envValue can never be null, since the test code should always114* print a "null" string.115*/116if (envValue == null) {117throw new RuntimeException("NPE, likely a program crash ??");118}119120if (envValue.equals("null")) {121return 0;122}123124return envValue.split(File.pathSeparator).length;125}126127/*128* Describe the cases that we test. Each case sets the environment129* variable LD_LIBRARY_PATH to a different value. The value associated130* with a case is the number of path elements that we expect the launcher131* to add to that variable.132*/133private static enum TestCase {134NO_DIR(0), // Directory does not exist135NO_LIBJVM(0), // Directory exists, but no libjvm.so136LIBJVM(3); // Directory exists, with a libjvm.so137private final int value;138TestCase(int i) {139this.value = i;140}141}142143/*144* test for 7029048145*/146static boolean runTest() throws IOException {147String desc = null;148boolean pass = true;149for (TestCase v : TestCase.values()) {150switch (v) {151case LIBJVM:152// copy the files into the directory structures153copyFile(srcLibjvmSo, dstServerLibjvm);154// does not matter if it is client or a server155copyFile(srcLibjvmSo, dstClientLibjvm);156desc = "LD_LIBRARY_PATH should be set";157break;158case NO_LIBJVM:159if (!dstClientDir.exists()) {160Files.createDirectories(dstClientDir.toPath());161} else {162Files.deleteIfExists(dstClientLibjvm.toPath());163}164165if (!dstServerDir.exists()) {166Files.createDirectories(dstServerDir.toPath());167} else {168Files.deleteIfExists(dstServerLibjvm.toPath());169}170171desc = "LD_LIBRARY_PATH should not be set (no libjvm.so)";172if (IS_EXPANDED_LD_LIBRARY_PATH) {173printSkipMessage(desc);174continue;175}176break;177case NO_DIR:178if (dstLibDir.exists()) {179recursiveDelete(dstLibDir);180}181desc = "LD_LIBRARY_PATH should not be set (no directory)";182if (IS_EXPANDED_LD_LIBRARY_PATH) {183printSkipMessage(desc);184continue;185}186break;187default:188throw new RuntimeException("unknown case");189}190191// Add one to account for our setting192int nLLPComponents = v.value + 1;193194/*195* Case 1: set the server path196*/197boolean pass1 = run(nLLPComponents, dstServerDir, "Case 1: " + desc);198199/*200* Case 2: repeat with client path201*/202boolean pass2 = run(nLLPComponents, dstClientDir, "Case 2: " + desc);203204pass &= pass1 && pass2;205}206return pass;207}208209private static void printSkipMessage(String description) {210System.out.printf("Skipping test case '%s' because the Aix and musl launchers" +211" add the paths in any case.%n", description);212}213214public static void main(String... args) throws Exception {215if (!TestHelper.haveServerVM) {216System.out.println("Note: test relies on server vm, not found, exiting");217return;218}219// create our test jar first220ExecutionEnvironment.createTestJar();221222if (!runTest()) {223throw new Exception("Test7029048 fails");224}225}226}227228229