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, "-jar", testJarFile.getAbsolutePath());
 144 
 145         if (!tr.isNotZeroOutput()) {
 146             flagError(tr, "Error: No output at all. Did the test execute ?");
 147         }
 148 
 149         for (String x : LD_PATH_STRINGS) {
 150             if (!tr.contains(x)) {
 151                 flagError(tr, "FAIL: did not get <" + x + ">");
 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) {
 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 {
 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, all VMs support
 240      * -server, however 32-bit VMs support -client and -server.
 241      */
 242     @Test
 243     void testVmSelection() {
 244 
 245         TestResult tr = null;
 246 
 247         if (is32Bit) {
 248             tr = doExec(javaCmd, "-client", "-version");
 249             if (!tr.matches(".*Client VM.*")) {
 250                 flagError(tr, "the expected vm -client did not launch");
 251             }
 252         }
 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      * checks to see there is no extra libjvm.so than needed
 261      */
 262     @Test
 263     void testNoSymLink() {
 264         if (is64Bit) {
 265             return;
 266         }
 267 
 268         File symLink = null;
 269         String libPathPrefix = isSDK ? "jre/lib" : "/lib";
 270         symLink = new File(JAVAHOME, libPathPrefix +
 271                 getJreArch() + "/" + LIBJVM);
 272         if (symLink.exists()) {
 273             throw new RuntimeException("symlink exists " + symLink.getAbsolutePath());
 274         }
 275     }
 276 
 277     /*
 278      * verify if all the symlinks in the images are created correctly,
 279      * only on solaris, this test works only on images.
 280      */
 281     @Test
 282     void testSymLinks() throws Exception {
 283         if (!isSolaris)
 284             return;
 285         verifySymLinks(JAVA_BIN);
 286         verifySymLinks(JAVA_JRE_BIN);
 287     }
 288     // exclude non-consequential binaries or scripts co-packaged in install
 289     private Pattern symlinkExcludes = Pattern.compile(".*jvisualvm.*");
 290 
 291     private void verifySymLinks(String bindir) throws IOException {
 292         File binDir = new File(bindir);
 293         System.err.println("verifying links in: " + bindir);
 294         File isaDir = new File(binDir, getArch()).getAbsoluteFile();
 295         if (!isaDir.exists()) {
 296             throw new RuntimeException("dir: " + isaDir + " does not exist");
 297         }
 298         try (DirectoryStream<Path> ds = Files.newDirectoryStream(binDir.toPath())) {
 299             for (Path p : ds) {
 300                 if (symlinkExcludes.matcher(p.toString()).matches() ||
 301                         Files.isDirectory(p, NOFOLLOW_LINKS)) {
 302                     continue;
 303                 }
 304                 Path link = new File(isaDir, p.getFileName().toString()).toPath();
 305                 if (Files.isSymbolicLink(link)) {
 306                     Path target = Files.readSymbolicLink(link);
 307                     if (target.startsWith("..") && p.endsWith(target.getFileName())) {
 308                         // System.out.println(target + " OK");
 309                         continue;
 310                     }
 311                     System.err.println("target:" + target);
 312                     System.err.println("file:" + p);
 313                 }
 314                 throw new RuntimeException("could not find link to " + p);
 315             }
 316         }
 317 
 318     }
 319     public static void main(String... args) throws Exception {
 320         if (isWindows) {
 321             System.err.println("Warning: test not applicable to windows");
 322             return;
 323         }
 324         ExecutionEnvironment ee = new ExecutionEnvironment();
 325         ee.run(args);
 326     }
 327 }