1 /*
   2  * Copyright (c) 2011, 2019, 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         if (isSolaris) {
  93             String envValue = getValue("LD_LIBRARY_PATH_64", tr.testOutput);
  94            /*
  95             * the envValue can never be null, since the test code should always
  96             * print a "null" string.
  97             */
  98             if (envValue == null) {
  99                 throw new RuntimeException("NPE, likely a program crash ??");
 100             }
 101             boolean noLLP64 = envValue.equals("null");
 102             if (nLLPComponents == 0 && noLLP64) {
 103                 System.out.println("FAIL: test7029048, " + caseID);
 104                 System.out.println(" Missing LD_LIBRARY_PATH_64");
 105                 errors++;
 106                 return;
 107             } else if (nLLPComponents > 3 && !noLLP64) {
 108                 System.out.println("FAIL: test7029048, " + caseID);
 109                 System.out.println(" Unexpected LD_LIBRARY_PATH_64: " + envValue);
 110                 errors++;
 111                 return;
 112             }
 113         }
 114 
 115         String envValue = getValue(LD_LIBRARY_PATH, tr.testOutput);
 116        /*
 117         * the envValue can never be null, since the test code should always
 118         * print a "null" string.
 119         */
 120         if (envValue == null) {
 121             throw new RuntimeException("NPE, likely a program crash ??");
 122         }
 123         int len = (envValue.equals("null")
 124                    ? 0 : envValue.split(File.pathSeparator).length);
 125         if (len == nLLPComponents) {
 126             System.out.println(caseID + ": OK");
 127             passes++;
 128         } else {
 129             System.out.println("FAIL: test7029048, " + caseID);
 130             System.out.println(" expected " + nLLPComponents
 131                                + " but got " + len);
 132             System.out.println(envValue);
 133             errors++;
 134         }
 135     }
 136 
 137     /*
 138      * Describe the cases that we test.  Each case sets the environment
 139      * variable LD_LIBRARY_PATH to a different value.  The value associated
 140      * with a case is the number of path elements that we expect the launcher
 141      * to add to that variable.
 142      */
 143     private static enum TestCase {
 144         NO_DIR(0),                      // Directory does not exist
 145         NO_LIBJVM(0),                   // Directory exists, but no libjvm.so
 146         LIBJVM(3);                      // Directory exists, with a libjvm.so
 147         private final int value;
 148         TestCase(int i) {
 149             this.value = i;
 150         }
 151     }
 152 
 153     /*
 154      * test for 7029048
 155      */
 156     static void test7029048() throws IOException {
 157         String desc = null;
 158         for (TestCase v : TestCase.values()) {
 159             switch (v) {
 160                 case LIBJVM:
 161                     // copy the files into the directory structures
 162                     copyFile(srcLibjvmSo, dstServerLibjvm);
 163                     // does not matter if it is client or a server
 164                     copyFile(srcLibjvmSo, dstClientLibjvm);
 165                     desc = "LD_LIBRARY_PATH should be set";
 166                     break;
 167                 case NO_LIBJVM:
 168                     if (!dstClientDir.exists()) {
 169                         Files.createDirectories(dstClientDir.toPath());
 170                     } else {
 171                         Files.deleteIfExists(dstClientLibjvm.toPath());
 172                     }
 173 
 174                     if (!dstServerDir.exists()) {
 175                         Files.createDirectories(dstServerDir.toPath());
 176                     } else {
 177                         Files.deleteIfExists(dstServerLibjvm.toPath());
 178                     }
 179 
 180                     desc = "LD_LIBRARY_PATH should not be set (no libjvm.so)";
 181                     if (TestHelper.isAIX) {
 182                         System.out.println("Skipping test case \"" + desc +
 183                                            "\" because the Aix launcher adds the paths in any case.");
 184                         continue;
 185                     }
 186                     break;
 187                 case NO_DIR:
 188                     if (dstLibDir.exists()) {
 189                         recursiveDelete(dstLibDir);
 190                     }
 191                     desc = "LD_LIBRARY_PATH should not be set (no directory)";
 192                     if (TestHelper.isAIX) {
 193                         System.out.println("Skipping test case \"" + desc +
 194                                            "\" because the Aix launcher adds the paths in any case.");
 195                         continue;
 196                     }
 197                     break;
 198                 default:
 199                     throw new RuntimeException("unknown case");
 200             }
 201 
 202             /*
 203              * Case 1: set the server path
 204              */
 205             env.clear();
 206             env.put(LD_LIBRARY_PATH, dstServerDir.getAbsolutePath());
 207             run(env,
 208                 v.value + 1,            // Add one to account for our setting
 209                 "Case 1: " + desc);
 210 
 211             /*
 212              * Case 2: repeat with client path
 213              */
 214             env.clear();
 215             env.put(LD_LIBRARY_PATH, dstClientDir.getAbsolutePath());
 216             run(env,
 217                 v.value + 1,            // Add one to account for our setting
 218                 "Case 2: " + desc);
 219 
 220             if (isSolaris) {
 221                 /*
 222                  * Case 3: set the appropriate LLP_XX flag,
 223                  * java64 LLP_64 is relevant, LLP_32 is ignored
 224                  */
 225                 env.clear();
 226                 env.put(LD_LIBRARY_PATH_64, dstServerDir.getAbsolutePath());
 227                 run(env,
 228                     // LD_LIBRARY_PATH_64 is copied into LD_LIBRARY_PATH for LIBJVM case
 229                     v.value == 0 ? 0 : v.value + 1,
 230                     "Case 3: " + desc);
 231             }
 232         }
 233         return;
 234     }
 235 
 236     public static void main(String... args) throws Exception {
 237         if (TestHelper.isWindows || TestHelper.isMacOSX) {
 238             System.out.println("Note: applicable on neither Windows nor MacOSX");
 239             return;
 240         }
 241         if (!TestHelper.haveServerVM) {
 242             System.out.println("Note: test relies on server vm, not found, exiting");
 243             return;
 244         }
 245         // create our test jar first
 246         ExecutionEnvironment.createTestJar();
 247 
 248         // run the tests
 249         test7029048();
 250         if (errors > 0) {
 251             throw new Exception("Test7029048: FAIL: with "
 252                     + errors + " errors and passes " + passes);
 253         } else if (isSolaris && passes < 9) {
 254             throw new Exception("Test7029048: FAIL: " +
 255                     "all tests did not run, expected " + 9 + " got " + passes);
 256         } else if (isLinux && passes < 6) {
 257              throw new Exception("Test7029048: FAIL: " +
 258                     "all tests did not run, expected " + 6 + " got " + passes);
 259         } else {
 260             System.out.println("Test7029048: PASS " + passes);
 261         }
 262     }
 263 }