1 /*
   2  * Copyright 2015 SAP SE.  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 import java.nio.file.DirectoryStream;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.nio.file.attribute.PosixFilePermission;
  29 import static java.nio.file.attribute.PosixFilePermission.*;
  30 import java.util.Arrays;
  31 import java.util.EnumSet;
  32 import java.util.HashSet;
  33 import java.util.Set;
  34 
  35 /*
  36  * @test
  37  * @bug 8132475
  38  * @summary Check that the executables in the current JDK image
  39  *          are executable by all users.
  40  * @run main ExecutableTest
  41  * @author Volker Simonis
  42  */
  43 
  44 public class ExecutableTest {
  45 
  46     // The bin/ directory may contain non-executable files (see 8132704)
  47     private static final String[] exclude = { "jmc.ini" };
  48     private static final Set<String> excludeSet =
  49         new HashSet<String>(Arrays.asList(exclude));
  50 
  51     public static void main(String args[]) throws Throwable {
  52         String JAVA_HOME = System.getProperty("java.home");
  53         Path binPath = Paths.get(JAVA_HOME, "bin");
  54         DirectoryStream<Path> stream = Files.newDirectoryStream(binPath);
  55         EnumSet<PosixFilePermission> execPerms =
  56             EnumSet.of(GROUP_EXECUTE, OTHERS_EXECUTE, OWNER_EXECUTE);
  57         for (Path entry : stream) {
  58             if (excludeSet.contains(entry.getFileName().toString())) continue;
  59             if (Files.isRegularFile(entry)) {
  60                 if (!Files.isExecutable(entry)) {
  61                     throw new Error(entry + " is not executable!");
  62                 }
  63                 try {
  64                     Set<PosixFilePermission> perm = Files.getPosixFilePermissions(entry);
  65                     if (!perm.containsAll(execPerms)) {
  66                         throw new Error(entry + " has not all executable permissions!\n" +
  67                                         "Should have: " + execPerms + "\nbut has: " + perm);
  68                     }
  69                 } catch (UnsupportedOperationException e) {}
  70             }
  71         }
  72     }
  73 }