test/jdk/java/lang/ProcessBuilder/SecurityManagerClinit.java
author kvn
Fri, 21 Jun 2019 13:04:14 -0700
changeset 55462 6dfdcd31463d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8185139: [Graal] Tests which set too restrictive security manager fail with Graal Summary: tests should also check default policy Reviewed-by: mchung, dfuchs, alanb, mullan
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.
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
     3
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
6516
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     4
 * 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
     5
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     6
 * 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
     7
 * 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
     8
 * published by the Free Software Foundation.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
     9
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    10
 * 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
    11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    12
 * 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
    13
 * 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
    14
 * accompanied this code).
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    15
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    16
 * 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
    17
 * 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
    18
 * 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
    19
 *
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    20
 * 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
    21
 * 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
    22
 * questions.
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
/*
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    26
 * @test
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    27
 * @bug 6980747
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    28
 * @summary Check that Process-related classes have the proper
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    29
 *     doPrivileged blocks, and can be initialized with an adversarial
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    30
 *     security manager.
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    31
 * @run main/othervm SecurityManagerClinit
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    32
 * @author Martin Buchholz
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
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    35
import java.io.*;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    36
import java.security.*;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    37
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    38
public class SecurityManagerClinit {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    39
    private static class SimplePolicy extends Policy {
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    40
        static final Policy DEFAULT_POLICY = Policy.getPolicy();
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    41
6516
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    42
        private Permissions perms;
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    43
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    44
        public SimplePolicy(Permission... permissions) {
6516
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    45
            perms = new Permissions();
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    46
            for (Permission permission : permissions)
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    47
                perms.add(permission);
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    48
        }
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
        public boolean implies(ProtectionDomain pd, Permission p) {
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    51
            return perms.implies(p) || DEFAULT_POLICY.implies(pd, p);
6516
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    52
        }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    53
    }
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    54
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    55
    public static void main(String[] args) throws Throwable {
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    56
        String javaExe =
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    57
            System.getProperty("java.home") +
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    58
            File.separator + "bin" + File.separator + "java";
8c52bb671f3e 6980747: Runtime.exec can fail due to SecurityException (lnx)
martin
parents:
diff changeset
    59
55462
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    60
        final SimplePolicy policy =
6dfdcd31463d 8185139: [Graal] Tests which set too restrictive security manager fail with Graal
kvn
parents: 47216
diff changeset
    61
            new SimplePolicy
6516
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
}