1 /* 2 * Copyright (c) 2009, 2013, 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 * jre/lib/$arch/libjvm.so -> client/libjvm.so 49 * 6. Since 32-bit Solaris is no longer supported we continue to ensure that 50 * the appropriate paths are ignored or used, additionally we also test to 51 * ensure the 64-bit isadir exists and contains appropriate links. 52 * TODO: 53 * a. perhaps we need to add a test to audit all environment variables are 54 * in pristine condition after the launch, there may be a few that the 55 * launcher may add as implementation details. 56 * b. add a pldd for solaris to ensure only one libjvm.so is linked 57 */ 58 import java.io.File; 59 import java.io.FileNotFoundException; 60 import java.io.IOException; 61 import java.nio.file.DirectoryStream; 62 import java.nio.file.Files; 63 import java.nio.file.Path; 64 import java.util.ArrayList; 65 import java.util.HashMap; 66 import java.util.List; 67 import java.util.Map; 68 import static java.nio.file.LinkOption.*; 69 import java.util.regex.Pattern; 70 71 72 public class ExecutionEnvironment extends TestHelper { 73 static final String LD_LIBRARY_PATH = TestHelper.isMacOSX 74 ? "DYLD_LIBRARY_PATH" 75 : "LD_LIBRARY_PATH"; 76 static final String LD_LIBRARY_PATH_32 = LD_LIBRARY_PATH + "_32"; 77 static final String LD_LIBRARY_PATH_64 = LD_LIBRARY_PATH + "_64"; 78 79 // Note: these paths need not exist on the filesytem 80 static final String LD_LIBRARY_PATH_VALUE = "/Bridge/On/The/River/Kwai"; 81 static final String LD_LIBRARY_PATH_32_VALUE = "/Lawrence/Of/Arabia"; 82 static final String LD_LIBRARY_PATH_64_VALUE = "/A/Passage/To/India"; 83 84 static final String[] LD_PATH_STRINGS = { 85 LD_LIBRARY_PATH + "=" + LD_LIBRARY_PATH_VALUE, 86 LD_LIBRARY_PATH_32 + "=" + LD_LIBRARY_PATH_32_VALUE, 87 LD_LIBRARY_PATH_64 + "=" + LD_LIBRARY_PATH_64_VALUE 88 }; 89 90 static final File testJarFile = new File("EcoFriendly.jar"); 91 92 static final String LIBJVM = TestHelper.isWindows 93 ? "jvm.dll" 94 : "libjvm" + (TestHelper.isMacOSX ? ".dylib" : ".so"); 95 96 public ExecutionEnvironment() { 97 createTestJar(); 98 } 99 100 static void createTestJar() { 101 try { 102 List<String> codeList = new ArrayList<>(); 103 codeList.add("static void printValue(String name, boolean property) {\n"); 104 codeList.add(" String value = (property) ? System.getProperty(name) : System.getenv(name);\n"); 105 codeList.add(" System.out.println(name + \"=\" + value);\n"); 106 codeList.add("}\n"); 107 codeList.add("public static void main(String... args) {\n"); 108 codeList.add(" System.out.println(\"Execute test:\");\n"); 109 codeList.add(" printValue(\"os.name\", true);\n"); 110 codeList.add(" printValue(\"os.arch\", true);\n"); 111 codeList.add(" printValue(\"os.version\", true);\n"); 112 codeList.add(" printValue(\"sun.arch.data.model\", true);\n"); 113 codeList.add(" printValue(\"java.library.path\", true);\n"); 114 codeList.add(" printValue(\"" + LD_LIBRARY_PATH + "\", false);\n"); 115 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_32 + "\", false);\n"); 116 codeList.add(" printValue(\"" + LD_LIBRARY_PATH_64 + "\", false);\n"); 117 codeList.add("}\n"); 118 String[] clist = new String[codeList.size()]; 119 createJar(testJarFile, codeList.toArray(clist)); 120 } catch (FileNotFoundException fnfe) { 121 throw new RuntimeException(fnfe); 122 } 123 } 124 private void flagError(TestResult tr, String message) { 125 System.err.println(tr); 126 throw new RuntimeException(message); 127 } 128 /* 129 * tests if the launcher pollutes the LD_LIBRARY_PATH variables ie. there 130 * should not be any new variables or pollution/mutations of any kind, the 131 * environment should be pristine. 132 */ 133 @Test 134 void testEcoFriendly() { 135 TestResult tr = null; 136 137 Map<String, String> env = new HashMap<>(); 138 for (String x : LD_PATH_STRINGS) { 139 String pairs[] = x.split("="); 140 env.put(pairs[0], pairs[1]); 141 } 142 143 tr = doExec(env, javaCmd, haveServerVM ? "-server" : "-client", 144 "-jar", testJarFile.getAbsolutePath()); 145 146 if (!tr.isNotZeroOutput()) { 147 flagError(tr, "Error: No output at all. Did the test execute ?"); 148 } 149 150 for (String x : LD_PATH_STRINGS) { 151 if (!tr.contains(x)) { 152 flagError(tr, "FAIL: did not get <" + x + ">"); 153 } 154 } 155 } 156 157 /* 158 * ensures that there are no execs as long as we are in the same 159 * data model 160 */ 161 @Test 162 void testNoExec() { 163 Map<String, String> env = new HashMap<>(); 164 env.put(JLDEBUG_KEY, "true"); 165 TestResult tr = doExec(env, javaCmd, 166 haveServerVM ? "-server" : "-client", 167 "-version"); 168 if (tr.testOutput.contains(EXPECTED_MARKER)) { 169 flagError(tr, "testNoExec: found warning <" + EXPECTED_MARKER + 170 "> the process execing ?"); 171 } 172 } 173 174 /* 175 * This test ensures that LD_LIBRARY_PATH* values are interpreted by the VM 176 * and the expected java.library.path behaviour. 177 * For Generic platforms (All *nixes): 178 * * All LD_LIBRARY_PATH variable should be on java.library.path 179 * For Solaris 32-bit 180 * * The LD_LIBRARY_PATH_32 should override LD_LIBRARY_PATH if specified 181 * For Solaris 64-bit 182 * * The LD_LIBRARY_PATH_64 should override LD_LIBRARY_PATH if specified 183 */ 184 @Test 185 void testJavaLibraryPath() { 186 TestResult tr = null; 187 188 Map<String, String> env = new HashMap<>(); 189 190 if (TestHelper.isLinux || TestHelper.isMacOSX) { 191 for (String x : LD_PATH_STRINGS) { 192 String pairs[] = x.split("="); 193 env.put(pairs[0], pairs[1]); 194 } 195 196 tr = doExec(env, javaCmd, haveServerVM ? "-server" : "-client", 197 "-jar", testJarFile.getAbsolutePath()); 198 verifyJavaLibraryPathGeneric(tr); 199 } else { // Solaris 200 // no override 201 env.clear(); 202 env.put(LD_LIBRARY_PATH, LD_LIBRARY_PATH_VALUE); 203 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 204 verifyJavaLibraryPathGeneric(tr); 205 206 env.clear(); 207 for (String x : LD_PATH_STRINGS) { 208 String pairs[] = x.split("="); 209 env.put(pairs[0], pairs[1]); 210 } 211 212 // verify the override occurs for 64-bit system 213 tr = doExec(env, javaCmd, "-jar", testJarFile.getAbsolutePath()); 214 verifyJavaLibraryPathOverride(tr, false); 215 } 216 } 217 218 private void verifyJavaLibraryPathGeneric(TestResult tr) { 219 if (!tr.matches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 220 flagError(tr, "testJavaLibraryPath: java.library.path does not contain " + 221 LD_LIBRARY_PATH_VALUE); 222 } 223 } 224 225 private void verifyJavaLibraryPathOverride(TestResult tr, 226 boolean is32Bit) { 227 // make sure the 32/64 bit value exists 228 if (!tr.matches("java.library.path=.*" + 229 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE) + ".*")) { 230 flagError(tr, "verifyJavaLibraryPathOverride: " + 231 " java.library.path does not contain " + 232 (is32Bit ? LD_LIBRARY_PATH_32_VALUE : LD_LIBRARY_PATH_64_VALUE)); 233 234 } 235 // make sure the generic value is absent 236 if (!tr.notMatches("java.library.path=.*" + LD_LIBRARY_PATH_VALUE + ".*")) { 237 flagError(tr, "verifyJavaLibraryPathOverride: " + 238 " java.library.path contains " + LD_LIBRARY_PATH_VALUE); 239 } 240 } 241 242 /* 243 * ensures we have indeed exec'ed the correct vm of choice, all VMs support 244 * -server, however 32-bit VMs support -client and -server. 245 */ 246 @Test 247 void testVmSelection() { 248 249 TestResult tr = null; 250 251 if (haveClientVM) { 252 tr = doExec(javaCmd, "-client", "-version"); 253 if (!tr.matches(".*Client VM.*")) { 254 flagError(tr, "the expected vm -client did not launch"); 255 } 256 } 257 if (haveServerVM) { 258 tr = doExec(javaCmd, "-server", "-version"); 259 if (!tr.matches(".*Server VM.*")) { 260 flagError(tr, "the expected vm -server did not launch"); 261 } 262 } 263 } 264 265 /* 266 * checks to see there is no extra libjvm.so than needed 267 */ 268 @Test 269 void testNoSymLink() { 270 if (is64Bit) { 271 return; 272 } 273 274 File symLink = null; 275 String libPathPrefix = isSDK ? "jre/lib" : "/lib"; 276 symLink = new File(JAVAHOME, libPathPrefix + 277 getJreArch() + "/" + LIBJVM); 278 if (symLink.exists()) { 279 throw new RuntimeException("symlink exists " + symLink.getAbsolutePath()); 280 } 281 } 282 283 /* 284 * verify if all the symlinks in the images are created correctly, 285 * only on solaris, this test works only on images. 286 */ 287 @Test 288 void testSymLinks() throws Exception { 289 if (!isSolaris) 290 return; 291 verifySymLinks(JAVA_BIN); 292 verifySymLinks(JAVA_JRE_BIN); 293 } 294 // exclude non-consequential binaries or scripts co-packaged in other 295 // build phases 296 private final String excludeRE = 297 ".*jvisualvm.*" + 298 "|.*javaws.*" + 299 "|.*ControlPanel.*" + 300 "|.*java-rmi.cgi" + 301 "|.*jcontrol.*"; 302 private final Pattern symlinkExcludes = Pattern.compile(excludeRE); 303 304 private void verifySymLinks(String bindir) throws IOException { 305 File binDir = new File(bindir); 306 System.err.println("verifying links in: " + bindir); 307 File isaDir = new File(binDir, getArch()).getAbsoluteFile(); 308 if (!isaDir.exists()) { 309 throw new RuntimeException("dir: " + isaDir + " does not exist"); 310 } 311 try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) { 312 for (Path p : ds) { 313 if (symlinkExcludes.matcher(p.toString()).matches() || 314 Files.isDirectory(p, NOFOLLOW_LINKS)) { 315 continue; 316 } 317 Path link = new File(isaDir, p.getFileName().toString()).toPath(); 318 if (Files.isSymbolicLink(link)) { 319 Path target = Files.readSymbolicLink(link); 320 if (target.startsWith("..") && p.endsWith(target.getFileName())) { 321 // System.out.println(target + " OK"); 322 continue; 323 } 324 System.err.println("target:" + target); 325 System.err.println("file:" + p); 326 } 327 throw new RuntimeException("could not find link to " + p); 328 } 329 } 330 331 } 332 public static void main(String... args) throws Exception { 333 if (isWindows) { 334 System.err.println("Warning: test not applicable to windows"); 335 return; 336 } 337 ExecutionEnvironment ee = new ExecutionEnvironment(); 338 ee.run(args); 339 } 340 }