1 /* 2 * Copyright (c) 2009, 2014, 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 4780570 4731671 6354700 6367077 6670965 4882974 27 * @summary Checks for LD_LIBRARY_PATH and execution on *nixes 28 * @compile -XDignore.symbol.file ExecutionEnvironment.java 29 * @run main/othervm ExecutionEnvironment 30 */ 31 32 /* 33 * This tests for various things as follows: 34 * Ensures that: 35 * 1. uneccessary execs do not occur 36 * 2. the environment is pristine, users environment variable wrt. 37 * LD_LIBRARY_PATH if set are not modified in any way. 38 * 3. the correct vm is chosen with -server and -client options 39 * 4. the VM on Solaris correctly interprets the LD_LIBRARY_PATH32 40 * and LD_LIBRARY_PATH64 variables if set by the user, ie. 41 * i. on 32 bit systems: 42 * a. if LD_LIBRARY_PATH32 is set it will override LD_LIBRARY_PATH 43 * b. LD_LIBRARY_PATH64 is ignored if set 44 * ii. on 64 bit systems: 45 * a. if LD_LIBRARY_PATH64 is set it will override LD_LIBRARY_PATH 46 * b. LD_LIBRARY_PATH32 is ignored if set 47 * 5. no extra symlink exists on Solaris ie. 48 * lib/$arch/libjvm.so -> client/libjvm.so 49 * TODO: 50 * a. perhaps we need to add a test to audit all environment variables are 51 * in pristine condition after the launch, there may be a few that the 52 * launcher may add as implementation details. 53 * b. add a pldd for solaris to ensure only one libjvm.so is linked 54 */ 55 import java.io.File; 56 import java.io.FileNotFoundException; 57 import java.util.ArrayList; 58 import java.util.HashMap; 59 import java.util.List; 60 import java.util.Map; 61 62 public class ExecutionEnvironment extends TestHelper { 63 static final String LD_LIBRARY_PATH = TestHelper.isMacOSX 64 ? "DYLD_LIBRARY_PATH" 65 : TestHelper.isAIX 66 ? "LIBPATH" 67 : "LD_LIBRARY_PATH"; 68 static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32"; 69 static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64"; 70 71 // Note: these paths need not exist on the filesytem 72 static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai"; 73 static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia"; 74 static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India"; 75 76 static final String[] LD_PATH_STRINGS = { 77 LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE, 78 LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE, 79 LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE 80 }; 81 82 static final File testJarFile = new File("EcoFriendly.jar"); 83 84 public ExecutionEnvironment() { 85 createTestJar(); 86 } 87 88 static void createTestJar() { 89 try { 90 List<String> codeList = new ArrayList<>(); 91 codeList.add("static void printValue(String name, boolean property) {\n"); 92 codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n"); 93 codeList.add(" System.out.println(name + \"=\" + value);\n"); 94 codeList.add("}\n"); 95 codeList.add("public static void main(String... args) {\n"); 96 codeList.add(" System.out.println(\"Execute test:\");\n"); 97 codeList.add(" printValue(\"os.name\", true);\n"); 98 codeList.add(" printValue(\"os.arch\", true);\n"); 99 codeList.add(" printValue(\"os.version\", true);\n"); 100 codeList.add(" printValue(\"sun.arch.data.model\", true);\n"); 101 codeList.add(" printValue(\"java.library.path\", true);\n"); 102 codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n"); 103 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n"); 104 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n"); 105 codeList.add("}\n"); 106 String[] clist = new String[codeList.size()]; 107 createJar(testJarFile, codeList.toArray(clist)); 108 } catch (FileNotFoundException fnfe) { 109 throw new RuntimeException(fnfe); 110 } 111 } 112 private void flagError(TestResult tr, String message) { 113 System.err.println(tr); 114 throw new RuntimeException(message); 115 } 116 /* 117 * tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there 118 * should not be any new variables or pollution/mutations of any kind, the 119 * environment should be pristine. 120 */ 121 @Test 122 void testEcoFriendly() { 123 TestResult tr = null; 124 125 Map<String, String> env = new HashMap<>(); 126 for (String x : LD_PATH_STRINGS) { 127 String pairs[] = x.split("="); 128 env.put(pairs[0], pairs[1]); 129 } 130 131 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 132 133 if (!tr.isNotZeroOutput()) { 134 flagError(tr, "Error: No output at all. Did the test execute ?"); 135 } 136 137 for (String x : LD_PATH_STRINGS) { 138 if (!tr.contains(x)) { 139 if (TestHelper.isAIX && x.startsWith(LD_LIBRARY_PATH)) { 140 // AIX does not support the '-rpath' linker options so the 141 // launchers have to prepend the jdk library path to 'LIBPATH'. 142 String aixLibPath = LD_LIBRARY_PATH + "=" + 143 System.getenv(LD_LIBRARY_PATH) + 144 System.getProperty("path.separator") + LD_LIBRARY_PATH_VALUE; 145 if (!tr.matches(aixLibPath)) { 146 flagError(tr, "FAIL: did not get <" + aixLibPath + ">"); 147 } 148 } 149 else { 150 flagError(tr, "FAIL: did not get <" + x + ">"); 151 } 152 } 153 } 154 } 155 156 /* 157 * ensures that there are no execs as long as we are in the same 158 * data model 159 */ 160 @Test 161 void testNoExec() { 162 Map<String, String> env = new HashMap<>(); 163 env.put(JLDEBUG_KEY, "true"); 164 TestResult tr = doExec(env, javaCmd, "-version"); 165 if (tr.testOutput.contains(EXPECTED_MARKER)) { 166 flagError(tr, "testNoExec: found warning <" + EXPECTED_MARKER + 167 "> the process execing ?"); 168 } 169 } 170 171 /* 172 * This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM 173 * and the expected java.library.path behaviour. 174 * For Generic platforms (All *nixes): 175 * * All LD_LIBRARY_PATH variable should be on java.library.path 176 * For Solaris 32-bit 177 * * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified 178 * For Solaris 64-bit 179 * * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified 180 */ 181 @Test 182 void testJavaLibraryPath() { 183 TestResult tr = null; 184 185 Map<String, String> env = new HashMap<>(); 186 187 if (TestHelper.isLinux || TestHelper.isMacOSX || TestHelper.isAIX) { 188 for (String x : LD_PATH_STRINGS) { 189 String pairs[] = x.split("="); 190 env.put(pairs[0], pairs[1]); 191 } 192 193 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 194 verifyJavaLibraryPathGeneric(tr); 195 } else { // Solaris 196 // no override 197 env.clear(); 198 env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE); 199 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 200 verifyJavaLibraryPathGeneric(tr); 201 202 env.clear(); 203 for (String x : LD_PATH_STRINGS) { 204 String pairs[] = x.split("="); 205 env.put(pairs[0], pairs[1]); 206 } 207 208 // verify the override occurs for 64-bit system 209 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 210 verifyJavaLibraryPathOverride(tr, false); 211 } 212 } 213 214 private void verifyJavaLibraryPathGeneric(TestResult tr) { 215 if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 216 flagError(tr, "testJavaLibraryPath: java.library.path does not contain " + 217 LD_LIBRARY_PATH_VALUE); 218 } 219 } 220 221 private void verifyJavaLibraryPathOverride(TestResult tr, 222 boolean is32Bit) { 223 // make sure the 32/64 bit value exists 224 if (!tr.matches("java.library.path=.*" + 225 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) { 226 flagError(tr, "verifyJavaLibraryPathOverride: " + 227 " java.library.path does not contain " + 228 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE)); 229 230 } 231 // make sure the generic value is absent 232 if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 233 flagError(tr, "verifyJavaLibraryPathOverride: " + 234 " java.library.path contains " + LD_LIBRARY_PATH_VALUE); 235 } 236 } 237 238 /* 239 * ensures we have indeed exec'ed the correct vm of choice if it exists 240 */ 241 @Test 242 void testVmSelection() { 243 244 TestResult tr = null; 245 246 if (haveClientVM) { 247 tr = doExec(javaCmd, "-client", "-version"); 248 if (!tr.matches(".*Client VM.*")) { 249 flagError(tr, "the expected vm -client did not launch"); 250 } 251 } 252 if (haveServerVM) { 253 tr = doExec(javaCmd, "-server", "-version"); 254 if (!tr.matches(".*Server VM.*")) { 255 flagError(tr, "the expected vm -server did not launch"); 256 } 257 } 258 } 259 260 /* 261 * checks to see there is no extra libjvm.so than needed 262 */ 263 @Test 264 void testNoSymLink() { 265 if (is64Bit) { 266 return; 267 } 268 269 File symLink = null; 270 String libPathPrefix = "/lib"; 271 symLink = new File(JAVAHOME, libPathPrefix + 272 getJreArch() + "/" + LIBJVM); 273 if (symLink.exists()) { 274 throw new RuntimeException("symlink exists " + symLink.getAbsolutePath()); 275 } 276 } 277 public static void main(String... args) throws Exception { 278 if (isWindows) { 279 System.err.println("Warning: test not applicable to windows"); 280 return; 281 } 282 ExecutionEnvironment ee = new ExecutionEnvironment(); 283 ee.run(args); 284 } 285 }