< prev index next >

test/jdk/tools/launcher/Test7029048.java

Print this page
rev 53150 : 8216532: tools/launcher/Test7029048.java fails (Solaris)


   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  * @compile -XDignore.symbol.file ExecutionEnvironment.java Test7029048.java
  29  * @run main Test7029048
  30  */
  31 
  32 /*
  33  * 7029048: test for LD_LIBRARY_PATH set to different paths which may or
  34  * may not contain a libjvm.so, but we test to ensure that the launcher
  35  * behaves correctly in all cases.
  36  */
  37 import java.io.File;
  38 import java.io.IOException;
  39 import java.nio.file.Files;
  40 import java.util.ArrayList;
  41 import java.util.HashMap;
  42 import java.util.List;
  43 import java.util.Map;
  44 
  45 public class Test7029048 extends TestHelper {
  46 
  47     static int passes = 0;
  48     static int errors = 0;
  49 
  50     private static final String LIBJVM = ExecutionEnvironment.LIBJVM;
  51     private static final String LD_LIBRARY_PATH =
  52             ExecutionEnvironment.LD_LIBRARY_PATH;
  53     private static final String LD_LIBRARY_PATH_64 =
  54             ExecutionEnvironment.LD_LIBRARY_PATH_64;
  55 
  56     private static final File libDir =
  57             new File(System.getProperty("sun.boot.library.path"));
  58     private static final File srcServerDir = new File(libDir, "server");
  59     private static final File srcLibjvmSo = new File(srcServerDir, LIBJVM);
  60 
  61     private static final File dstLibDir = new File("lib");
  62     private static final File dstServerDir = new File(dstLibDir, "server");
  63     private static final File dstServerLibjvm = new File(dstServerDir, LIBJVM);
  64 
  65     private static final File dstClientDir = new File(dstLibDir, "client");
  66     private static final File dstClientLibjvm = new File(dstClientDir, LIBJVM);
  67 
  68     private static final Map<String, String> env = new HashMap<>();
  69 
  70 
  71     static String getValue(String name, List<String> in) {
  72         for (String x : in) {
  73             String[] s = x.split("=");
  74             if (name.equals(s[0].trim())) {
  75                 return s[1].trim();
  76             }
  77         }
  78         return null;
  79     }
  80 
  81     static void run(Map<String, String> env,
  82             int nLLPComponents, String caseID) {
  83         env.put(ExecutionEnvironment.JLDEBUG_KEY, "true");
  84         List<String> cmdsList = new ArrayList<>();
  85         cmdsList.add(javaCmd);
  86         cmdsList.add("-server");
  87         cmdsList.add("-jar");
  88         cmdsList.add(ExecutionEnvironment.testJarFile.getAbsolutePath());
  89         String[] cmds = new String[cmdsList.size()];
  90         TestResult tr = doExec(env, cmdsList.toArray(cmds));
  91         System.out.println(tr);
  92         analyze(tr, nLLPComponents, caseID);
  93     }
  94 
  95     static void analyze(TestResult tr, int nLLPComponents, String caseID) {
  96         String envValue = getValue(LD_LIBRARY_PATH, tr.testOutput);
  97        /*
  98         * the envValue can never be null, since the test code should always
  99         * print a "null" string.
 100         */
 101         if (envValue == null) {
 102             System.out.println(tr);
 103             throw new RuntimeException("NPE, likely a program crash ??");
 104         }
 105         String values[] = envValue.split(File.pathSeparator);
 106         if (values.length == nLLPComponents) {
 107             System.out.println(caseID + " :OK");

 108             passes++;
 109         } else {
 110             System.out.println("FAIL: test7029048, " + caseID);
 111             System.out.println(" expected " + nLLPComponents
 112                     + " but got " + values.length);
 113             System.out.println(envValue);
 114             System.out.println(tr);
 115             errors++;
 116         }
 117     }
 118 
 119     /*
 120      * A crucial piece, specifies what we should expect, given the conditions.
 121      * That is for a given enum type, the value indicates how many absolute
 122      * environment variables that can be expected. This value is used to base
 123      * the actual expected values by adding the set environment variable usually
 124      * it is 1, but it could be more if the test wishes to set more paths in
 125      * the future.
 126      */
 127     private static enum LLP_VAR {
 128         LLP_SET_NON_EXISTENT_PATH(0),   // env set, but the path does not exist
 129         LLP_SET_EMPTY_PATH(0),          // env set, with a path but no libjvm.so
 130         LLP_SET_WITH_JVM(3);            // env set, with a libjvm.so
 131         private final int value;
 132         LLP_VAR(int i) {
 133             this.value = i;
 134         }
 135     }
 136 
 137     /*
 138      * test for 7029048
 139      */
 140     static void test7029048() throws IOException {
 141         String desc = null;
 142         for (LLP_VAR v : LLP_VAR.values()) {
 143             switch (v) {
 144                 case LLP_SET_WITH_JVM:
 145                     // copy the files into the directory structures
 146                     copyFile(srcLibjvmSo, dstServerLibjvm);
 147                     // does not matter if it is client or a server
 148                     copyFile(srcLibjvmSo, dstClientLibjvm);
 149                     desc = "LD_LIBRARY_PATH should be set";
 150                     break;
 151                 case LLP_SET_EMPTY_PATH:
 152                     if (!dstClientDir.exists()) {
 153                         Files.createDirectories(dstClientDir.toPath());
 154                     } else {
 155                         Files.deleteIfExists(dstClientLibjvm.toPath());
 156                     }
 157 
 158                     if (!dstServerDir.exists()) {
 159                         Files.createDirectories(dstServerDir.toPath());
 160                     } else {
 161                         Files.deleteIfExists(dstServerLibjvm.toPath());
 162                     }
 163 
 164                     desc = "LD_LIBRARY_PATH should not be set";
 165                     break;
 166                 case LLP_SET_NON_EXISTENT_PATH:
 167                     if (dstLibDir.exists()) {
 168                         recursiveDelete(dstLibDir);
 169                     }
 170                     desc = "LD_LIBRARY_PATH should not be set";
 171                     break;
 172                 default:
 173                     throw new RuntimeException("unknown case");
 174             }
 175 
 176             /*
 177              * Case 1: set the server path
 178              */
 179             env.clear();
 180             env.put(LD_LIBRARY_PATH, dstServerDir.getAbsolutePath());
 181             run(env, v.value + 1, "Case 1: " + desc);


 182 
 183             /*
 184              * Case 2: repeat with client path
 185              */
 186             env.clear();
 187             env.put(LD_LIBRARY_PATH, dstClientDir.getAbsolutePath());
 188             run(env, v.value + 1, "Case 2: " + desc);


 189 
 190             if (isSolaris) {
 191                 /*
 192                  * Case 3: set the appropriate LLP_XX flag,
 193                  * java64 LLP_64 is relevant, LLP_32 is ignored
 194                  */
 195                 env.clear();
 196                 env.put(LD_LIBRARY_PATH_64, dstServerDir.getAbsolutePath());
 197                 run(env, v.value + 1, "Case 3: " + desc);



 198             }
 199         }
 200         return;
 201     }
 202 
 203     public static void main(String... args) throws Exception {
 204         if (TestHelper.isWindows || TestHelper.isMacOSX) {
 205             System.out.println("Note: applicable on neither Windows nor MacOSX");
 206             return;
 207         }
 208         if (!TestHelper.haveServerVM) {
 209             System.out.println("Note: test relies on server vm, not found, exiting");
 210             return;
 211         }
 212         // create our test jar first
 213         ExecutionEnvironment.createTestJar();
 214 
 215         // run the tests
 216         test7029048();
 217         if (errors > 0) {
 218             throw new Exception("Test7029048: FAIL: with "
 219                     + errors + " errors and passes " + passes);
 220         } else if (isSolaris && passes < 9) {
 221             throw new Exception("Test7029048: FAIL: " +
 222                     "all tests did not run, expected " + 9 + " got " + passes);
 223         } else if (isLinux && passes < 6) {
 224              throw new Exception("Test7029048: FAIL: " +
 225                     "all tests did not run, expected " + 6 + " got " + passes);
 226         } else {
 227             System.out.println("Test7029048: PASS " + passes);
 228         }
 229     }

 230 }


   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 Ensure that the launcher defends against user settings of the
  28  *          LD_LIBRARY_PATH environment variable on Unixes
  29  * @compile -XDignore.symbol.file ExecutionEnvironment.java Test7029048.java
  30  * @run main Test7029048
  31  */
  32 





  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.nio.file.Files;
  36 import java.util.ArrayList;
  37 import java.util.HashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 
  41 public class Test7029048 extends TestHelper {
  42 
  43     static int passes = 0;
  44     static int errors = 0;
  45 
  46     private static final String LIBJVM = ExecutionEnvironment.LIBJVM;
  47     private static final String LD_LIBRARY_PATH =
  48             ExecutionEnvironment.LD_LIBRARY_PATH;
  49     private static final String LD_LIBRARY_PATH_64 =
  50             ExecutionEnvironment.LD_LIBRARY_PATH_64;
  51 
  52     private static final File libDir =
  53             new File(System.getProperty("sun.boot.library.path"));
  54     private static final File srcServerDir = new File(libDir, "server");
  55     private static final File srcLibjvmSo = new File(srcServerDir, LIBJVM);
  56 
  57     private static final File dstLibDir = new File("lib");
  58     private static final File dstServerDir = new File(dstLibDir, "server");
  59     private static final File dstServerLibjvm = new File(dstServerDir, LIBJVM);
  60 
  61     private static final File dstClientDir = new File(dstLibDir, "client");
  62     private static final File dstClientLibjvm = new File(dstClientDir, LIBJVM);
  63 
  64     private static final Map<String, String> env = new HashMap<>();
  65 

  66     static String getValue(String name, List<String> in) {
  67         for (String x : in) {
  68             String[] s = x.split("=");
  69             if (name.equals(s[0].trim())) {
  70                 return s[1].trim();
  71             }
  72         }
  73         return null;
  74     }
  75 
  76     static void run(Map<String, String> env,
  77             int nLLPComponents, String caseID) {
  78         env.put(ExecutionEnvironment.JLDEBUG_KEY, "true");
  79         List<String> cmdsList = new ArrayList<>();
  80         cmdsList.add(javaCmd);
  81         cmdsList.add("-server");
  82         cmdsList.add("-jar");
  83         cmdsList.add(ExecutionEnvironment.testJarFile.getAbsolutePath());
  84         String[] cmds = new String[cmdsList.size()];
  85         TestResult tr = doExec(env, cmdsList.toArray(cmds));
  86         System.out.println(tr);
  87         analyze(tr, nLLPComponents, caseID);
  88     }
  89 
  90     static void analyze(TestResult tr, int nLLPComponents, String caseID) {
  91         String envValue = getValue(LD_LIBRARY_PATH, tr.testOutput);
  92        /*
  93         * the envValue can never be null, since the test code should always
  94         * print a "null" string.
  95         */
  96         if (envValue == null) {

  97             throw new RuntimeException("NPE, likely a program crash ??");
  98         }
  99         int len = (envValue.equals("null")
 100                    ? 0 : envValue.split(File.pathSeparator).length);
 101         if (len == nLLPComponents) {
 102             System.out.println(caseID + ": OK");
 103             passes++;
 104         } else {
 105             System.out.println("FAIL: test7029048, " + caseID);
 106             System.out.println(" expected " + nLLPComponents
 107                                + " but got " + len);
 108             System.out.println(envValue);

 109             errors++;
 110         }
 111     }
 112 
 113     /*
 114      * Describe the cases that we test.  Each case sets the environment
 115      * variable LD_LIBRARY_PATH to a different value.  The value associated
 116      * with a case is the number of path elements that we expect the launcher
 117      * to add to that variable.
 118      */
 119     private static enum CASE {
 120         NO_DIR(0),                      // Directory does not exist
 121         NO_LIBJVM(0),                   // Directory exists, but no libjvm.so
 122         LIBJVM(3);                      // Directory exists, with a libjvm.so


 123         private final int value;
 124         CASE(int i) {
 125             this.value = i;
 126         }
 127     }
 128 
 129     /*
 130      * test for 7029048
 131      */
 132     static void test7029048() throws IOException {
 133         String desc = null;
 134         for (CASE v : CASE.values()) {
 135             switch (v) {
 136                 case LIBJVM:
 137                     // copy the files into the directory structures
 138                     copyFile(srcLibjvmSo, dstServerLibjvm);
 139                     // does not matter if it is client or a server
 140                     copyFile(srcLibjvmSo, dstClientLibjvm);
 141                     desc = "LD_LIBRARY_PATH should be set";
 142                     break;
 143                 case NO_LIBJVM:
 144                     if (!dstClientDir.exists()) {
 145                         Files.createDirectories(dstClientDir.toPath());
 146                     } else {
 147                         Files.deleteIfExists(dstClientLibjvm.toPath());
 148                     }
 149 
 150                     if (!dstServerDir.exists()) {
 151                         Files.createDirectories(dstServerDir.toPath());
 152                     } else {
 153                         Files.deleteIfExists(dstServerLibjvm.toPath());
 154                     }
 155 
 156                     desc = "LD_LIBRARY_PATH should not be set (no libjvm.so)";
 157                     break;
 158                 case NO_DIR:
 159                     if (dstLibDir.exists()) {
 160                         recursiveDelete(dstLibDir);
 161                     }
 162                     desc = "LD_LIBRARY_PATH should not be set (no directory)";
 163                     break;
 164                 default:
 165                     throw new RuntimeException("unknown case");
 166             }
 167 
 168             /*
 169              * Case 1: set the server path
 170              */
 171             env.clear();
 172             env.put(LD_LIBRARY_PATH, dstServerDir.getAbsolutePath());
 173             run(env,
 174                 v.value + 1,            // Add one to account for our setting
 175                 "Case 1: " + desc);
 176 
 177             /*
 178              * Case 2: repeat with client path
 179              */
 180             env.clear();
 181             env.put(LD_LIBRARY_PATH, dstClientDir.getAbsolutePath());
 182             run(env,
 183                 v.value + 1,            // Add one to account for our setting
 184                 "Case 2: " + desc);
 185 
 186             if (isSolaris) {
 187                 /*
 188                  * Case 3: set the appropriate LLP_XX flag,
 189                  * java64 LLP_64 is relevant, LLP_32 is ignored
 190                  */
 191                 env.clear();
 192                 env.put(LD_LIBRARY_PATH_64, dstServerDir.getAbsolutePath());
 193                 run(env,
 194                     v.value,            // Do not add one, since we didn't set
 195                                         // LD_LIBRARY_PATH here
 196                     "Case 3: " + desc);
 197             }
 198         }
 199         return;
 200     }
 201 
 202     public static void main(String... args) throws Exception {
 203         if (TestHelper.isWindows || TestHelper.isMacOSX) {
 204             System.out.println("Note: applicable on neither Windows nor MacOSX");
 205             return;
 206         }
 207         if (!TestHelper.haveServerVM) {
 208             System.out.println("Note: test relies on server vm, not found, exiting");
 209             return;
 210         }
 211         // create our test jar first
 212         ExecutionEnvironment.createTestJar();
 213 
 214         // run the tests
 215         test7029048();
 216         if (errors > 0) {
 217             throw new Exception("Test7029048: FAIL: with "
 218                     + errors + " errors and passes " + passes);
 219         } else if (isSolaris && passes < 9) {
 220             throw new Exception("Test7029048: FAIL: " +
 221                     "all tests did not run, expected " + 9 + " got " + passes);
 222         } else if (isLinux && passes < 6) {
 223              throw new Exception("Test7029048: FAIL: " +
 224                     "all tests did not run, expected " + 6 + " got " + passes);
 225         } else {
 226             System.out.println("Test7029048: PASS " + passes);
 227         }
 228     }
 229 
 230 }
< prev index next >