1 /*
   2  * Copyright (c) 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 8064667
  27  * @summary Sanity test for -XX:+CheckEndorsedAndExtDirs
  28  * @library /testlibrary
  29  * @run main/othervm EndorsedExtDirs
  30  */
  31 
  32 import com.oracle.java.testlibrary.*;
  33 import java.io.File;
  34 import java.io.IOException;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.nio.file.Files;
  37 import java.nio.file.SimpleFileVisitor;
  38 import java.nio.file.FileVisitOption;
  39 import java.nio.file.FileVisitResult;
  40 import java.nio.file.Path;
  41 import java.nio.file.Paths;
  42 import java.util.ArrayList;
  43 import java.util.EnumSet;
  44 import java.util.List;
  45 
  46 public class EndorsedExtDirs {
  47     static final String cpath = System.getProperty("test.classes", ".");
  48     public static void main(String arg[]) throws Exception {
  49         fatalError("-XX:+CheckEndorsedAndExtDirs", "-Djava.endorsed.dirs=foo");
  50         fatalError("-XX:+CheckEndorsedAndExtDirs", "-Djava.ext.dirs=bar");
  51         testNonEmptySystemExtDirs();
  52     }
  53 
  54     static void testNonEmptySystemExtDirs() throws Exception {
  55         String home = System.getProperty("java.home");
  56         Path ext = Paths.get(home, "lib", "ext");
  57         String extDirs = System.getProperty("java.ext.dirs");
  58         String[] dirs = extDirs.split(File.pathSeparator);
  59         long count = 0;
  60         for (String d : dirs) {
  61             Path path = Paths.get(d);
  62             if (Files.notExists(path) || path.equals(ext)) continue;
  63             final long[] cnt = new long[1];
  64             Files.walkFileTree(path, EnumSet.noneOf(FileVisitOption.class), 1,
  65                                new SimpleFileVisitor<Path>() {
  66                                    @Override
  67                                    public FileVisitResult visitFile(Path p,
  68                                            BasicFileAttributes attrs)
  69                                    {
  70                                        if (p.getFileName().toString()
  71                                                           .endsWith(".jar")) {
  72                                            cnt[0]++;
  73                                        }
  74                                        return FileVisitResult.CONTINUE;
  75                                    }
  76                                });
  77             count += cnt[0];
  78         }
  79         if (count > 0) {
  80             fatalError("-XX:+CheckEndorsedAndExtDirs");
  81         }
  82     }
  83 
  84     static ProcessBuilder newProcessBuilder(String... args) {
  85         List<String> commands = new ArrayList<>();
  86         String java = System.getProperty("java.home") + "/bin/java";
  87         commands.add(java);
  88         for (String s : args) {
  89             commands.add(s);
  90         }
  91         commands.add("-cp");
  92         commands.add(cpath);
  93         commands.add("EndorsedExtDirs");
  94 
  95         System.out.println("Process " + commands);
  96         return new ProcessBuilder(commands);
  97     }
  98 
  99     static void fatalError(String... args) throws Exception {
 100         fatalError(newProcessBuilder(args));
 101     }
 102 
 103     static void fatalError(ProcessBuilder pb) throws Exception {
 104         OutputAnalyzer output = new OutputAnalyzer(pb.start());
 105         output.shouldContain("Could not create the Java Virtual Machine");
 106         output.shouldHaveExitValue(1);
 107     }
 108 }