jdk/test/java/lang/ProcessBuilder/SecurityManagerClinit.java
author robm
Mon, 15 Oct 2012 03:26:11 +0100
changeset 14183 26e97eede839
parent 6516 8c52bb671f3e
child 24369 9355f02a63a3
permissions -rw-r--r--
8000817: Reinstate accidentally removed sleep() from ProcessBuilder/Basic.java Reviewed-by: alanb, martin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6516
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     1
/*
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     2
 * Copyright 2010 Google Inc.  All Rights Reserved.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     4
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     8
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    13
 * accompanied this code).
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    14
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    18
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    21
 * questions.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    22
 */
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    23
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    24
/*
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    25
 * @test
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    26
 * @bug 6980747
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    27
 * @summary Check that Process-related classes have the proper
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    28
 *     doPrivileged blocks, and can be initialized with an adversarial
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    29
 *     security manager.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    30
 * @run main/othervm SecurityManagerClinit
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    31
 * @author Martin Buchholz
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    32
 */
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    33
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    34
import java.io.*;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    35
import java.security.*;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    36
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    37
public class SecurityManagerClinit {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    38
    private static class Policy extends java.security.Policy {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    39
        private Permissions perms;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    40
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    41
        public Policy(Permission... permissions) {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    42
            perms = new Permissions();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    43
            for (Permission permission : permissions)
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    44
                perms.add(permission);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    45
        }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    46
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    47
        public boolean implies(ProtectionDomain pd, Permission p) {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    48
            return perms.implies(p);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    49
        }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    50
    }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    51
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    52
    public static void main(String[] args) throws Throwable {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    53
        String javaExe =
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    54
            System.getProperty("java.home") +
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    55
            File.separator + "bin" + File.separator + "java";
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    56
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    57
        // A funky contrived security setup, just for bug repro purposes.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    58
        java.security.Security.setProperty("package.access", "java.util");
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    59
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    60
        final Policy policy =
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    61
            new Policy
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    62
            (new FilePermission("<<ALL FILES>>", "execute"),
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    63
             new RuntimePermission("setSecurityManager"));
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    64
        Policy.setPolicy(policy);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    65
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    66
        System.setSecurityManager(new SecurityManager());
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    67
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    68
        try {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    69
            String[] cmd = { javaExe, "-version" };
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    70
            Process p = Runtime.getRuntime().exec(cmd);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    71
            p.getOutputStream().close();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    72
            p.getInputStream().close();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    73
            p.getErrorStream().close();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    74
            p.waitFor();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    75
        } finally {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    76
            System.setSecurityManager(null);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    77
        }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    78
    }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    79
}