1 /*
   2  * Copyright (c) 2011, 2020, 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 7029048 8217340 8217216
  27  * @summary Ensure that the launcher defends against user settings of the
  28  *          LD_LIBRARY_PATH environment variable on Unixes
  29  * @library /test/lib
  30  * @compile -XDignore.symbol.file ExecutionEnvironment.java Test7029048.java
  31  * @run main Test7029048
  32  */
  33 
  34 import java.io.File;
  35 import java.io.IOException;
  36 import java.nio.file.Files;
  37 import java.util.ArrayList;
  38 import java.util.HashMap;
  39 import java.util.List;
  40 import java.util.Map;
  41 
  42 public class Test7029048 extends TestHelper {
  43 
  44     static int passes = 0;
  45     static int errors = 0;
  46 
  47     private static final String LIBJVM = ExecutionEnvironment.LIBJVM;
  48     private static final String LD_LIBRARY_PATH =
  49             ExecutionEnvironment.LD_LIBRARY_PATH;
  50     private static final String LD_LIBRARY_PATH_64 =
  51             ExecutionEnvironment.LD_LIBRARY_PATH_64;
  52 
  53     private static final File libDir =
  54             new File(System.getProperty("sun.boot.library.path"));
  55     private static final File srcServerDir = new File(libDir, "server");
  56     private static final File srcLibjvmSo = new File(srcServerDir, LIBJVM);
  57 
  58     private static final File dstLibDir = new File("lib");
  59     private static final File dstServerDir = new File(dstLibDir, "server");
  60     private static final File dstServerLibjvm = new File(dstServerDir, LIBJVM);
  61 
  62     private static final File dstClientDir = new File(dstLibDir, "client");
  63     private static final File dstClientLibjvm = new File(dstClientDir, LIBJVM);
  64 
  65     private static final Map<String, String> env = new HashMap<>();
  66 
  67     static String getValue(String name, List<String> in) {
  68         for (String x : in) {
  69             String[] s = x.split("=");
  70             if (name.equals(s[0].trim())) {
  71                 return s[1].trim();
  72             }
  73         }
  74         return null;
  75     }
  76 
  77     static void run(Map<String, String> env,
  78             int nLLPComponents, String caseID) {
  79         env.put(ExecutionEnvironment.JLDEBUG_KEY, "true");
  80         List<String> cmdsList = new ArrayList<>();
  81         cmdsList.add(javaCmd);
  82         cmdsList.add("-server");
  83         cmdsList.add("-jar");
  84         cmdsList.add(ExecutionEnvironment.testJarFile.getAbsolutePath());
  85         String[] cmds = new String[cmdsList.size()];
  86         TestResult tr = doExec(env, cmdsList.toArray(cmds));
  87         System.out.println(tr);
  88         analyze(tr, nLLPComponents, caseID);
  89     }
  90 
  91     static void analyze(TestResult tr, int nLLPComponents, String caseID) {
  92         String envValue = getValue(LD_LIBRARY_PATH, tr.testOutput);
  93        /*
  94         * the envValue can never be null, since the test code should always
  95         * print a "null" string.
  96         */
  97         if (envValue == null) {
  98             throw new RuntimeException("NPE, likely a program crash ??");
  99         }
 100         int len = (envValue.equals("null")
 101                    ? 0 : envValue.split(File.pathSeparator).length);
 102         if (len == nLLPComponents) {
 103             System.out.println(caseID + ": OK");
 104             passes++;
 105         } else {
 106             System.out.println("FAIL: test7029048, " + caseID);
 107             System.out.println(" expected " + nLLPComponents
 108                                + " but got " + len);
 109             System.out.println(envValue);
 110             errors++;
 111         }
 112     }
 113 
 114     /*
 115      * Describe the cases that we test.  Each case sets the environment
 116      * variable LD_LIBRARY_PATH to a different value.  The value associated
 117      * with a case is the number of path elements that we expect the launcher
 118      * to add to that variable.
 119      */
 120     private static enum TestCase {
 121         NO_DIR(0),                      // Directory does not exist
 122         NO_LIBJVM(0),                   // Directory exists, but no libjvm.so
 123         LIBJVM(3);                      // Directory exists, with a libjvm.so
 124         private final int value;
 125         TestCase(int i) {
 126             this.value = i;
 127         }
 128     }
 129 
 130     /*
 131      * test for 7029048
 132      */
 133     static void test7029048() throws IOException {
 134         String desc = null;
 135         for (TestCase v : TestCase.values()) {
 136             switch (v) {
 137                 case LIBJVM:
 138                     // copy the files into the directory structures
 139                     copyFile(srcLibjvmSo, dstServerLibjvm);
 140                     // does not matter if it is client or a server
 141                     copyFile(srcLibjvmSo, dstClientLibjvm);
 142                     desc = "LD_LIBRARY_PATH should be set";
 143                     break;
 144                 case NO_LIBJVM:
 145                     if (!dstClientDir.exists()) {
 146                         Files.createDirectories(dstClientDir.toPath());
 147                     } else {
 148                         Files.deleteIfExists(dstClientLibjvm.toPath());
 149                     }
 150 
 151                     if (!dstServerDir.exists()) {
 152                         Files.createDirectories(dstServerDir.toPath());
 153                     } else {
 154                         Files.deleteIfExists(dstServerLibjvm.toPath());
 155                     }
 156 
 157                     desc = "LD_LIBRARY_PATH should not be set (no libjvm.so)";
 158                     if (TestHelper.isAIX) {
 159                         System.out.println("Skipping test case \"" + desc +
 160                                            "\" because the Aix launcher adds the paths in any case.");
 161                         continue;
 162                     }
 163                     break;
 164                 case NO_DIR:
 165                     if (dstLibDir.exists()) {
 166                         recursiveDelete(dstLibDir);
 167                     }
 168                     desc = "LD_LIBRARY_PATH should not be set (no directory)";
 169                     if (TestHelper.isAIX) {
 170                         System.out.println("Skipping test case \"" + desc +
 171                                            "\" because the Aix launcher adds the paths in any case.");
 172                         continue;
 173                     }
 174                     break;
 175                 default:
 176                     throw new RuntimeException("unknown case");
 177             }
 178 
 179             /*
 180              * Case 1: set the server path
 181              */
 182             env.clear();
 183             env.put(LD_LIBRARY_PATH, dstServerDir.getAbsolutePath());
 184             run(env,
 185                 v.value + 1,            // Add one to account for our setting
 186                 "Case 1: " + desc);
 187 
 188             /*
 189              * Case 2: repeat with client path
 190              */
 191             env.clear();
 192             env.put(LD_LIBRARY_PATH, dstClientDir.getAbsolutePath());
 193             run(env,
 194                 v.value + 1,            // Add one to account for our setting
 195                 "Case 2: " + desc);
 196         }
 197         return;
 198     }
 199 
 200     public static void main(String... args) throws Exception {
 201         if (TestHelper.isWindows || TestHelper.isMacOSX) {
 202             System.out.println("Note: applicable on neither Windows nor MacOSX");
 203             return;
 204         }
 205         if (!TestHelper.haveServerVM) {
 206             System.out.println("Note: test relies on server vm, not found, exiting");
 207             return;
 208         }
 209         // create our test jar first
 210         ExecutionEnvironment.createTestJar();
 211 
 212         // run the tests
 213         test7029048();
 214         if (errors > 0) {
 215             throw new Exception("Test7029048: FAIL: with "
 216                     + errors + " errors and passes " + passes);
 217         } else if (isLinux && passes < 6) {
 218              throw new Exception("Test7029048: FAIL: " +
 219                     "all tests did not run, expected " + 6 + " got " + passes);
 220         } else {
 221             System.out.println("Test7029048: PASS " + passes);
 222         }
 223     }
 224 }