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