Path: blob/master/test/jdk/sun/security/util/FilePermCompat/CompatImpact.java
41152 views
/*1* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8164705 816841026* @summary check compatibility after FilePermission change27* @library /test/lib28* @run main CompatImpact prepare29* @run main CompatImpact builtin30* @run main/othervm -Djdk.security.filePermCompat=true CompatImpact mine31* @run main/fail CompatImpact mine32* @run main CompatImpact dopriv33*/3435import jdk.test.lib.process.Proc;3637import java.io.File;38import java.io.FilePermission;39import java.nio.file.Files;40import java.nio.file.Paths;41import java.security.AccessController;42import java.security.AllPermission;43import java.security.CodeSource;44import java.security.Permission;45import java.security.PermissionCollection;46import java.security.Policy;47import java.security.PrivilegedAction;48import java.security.ProtectionDomain;49import java.security.SecurityPermission;5051public class CompatImpact {5253public static void main(String[] args) throws Exception {54switch (args[0]) {55// copy class files to future classpath56case "prepare":57// cp in .58String cp = System.getProperty("test.classes");59Files.copy(Paths.get(cp, "CompatImpact.class"),60Paths.get("CompatImpact.class"));61Files.copy(Paths.get(cp, "CompatImpact$MP.class"),62Paths.get("CompatImpact$MP.class"));63Files.write(Paths.get("f"), new byte[10]);64// cp in ./sub65Files.createDirectory(Paths.get("sub"));66Files.copy(Paths.get(cp, "CompatImpact.class"),67Paths.get("sub", "CompatImpact.class"));68Files.copy(Paths.get(cp, "CompatImpact$MP.class"),69Paths.get("sub", "CompatImpact$MP.class"));70Files.write(Paths.get("sub", "f"), new byte[10]);71// cp in ./inner72Files.createDirectory(Paths.get("inner"));73Files.copy(Paths.get(cp, "CompatImpact$DoPrivInner.class"),74Paths.get("inner", "CompatImpact$DoPrivInner.class"));75break;76// default policy always covered, user-defined depends on77// system property jdk.security.filePermCompact.78case "builtin":79case "mine":80cp = System.getProperty("test.classes");81Proc p;82String failed = "";83String testcase = "";84String cwd = System.getProperty("user.dir");8586// Granting a FilePermission on an absolute path87testcase = "PonA";88p = p(args[0], cwd + "/f")89.args("f", cwd + "/f")90.debug(testcase)91.start();92if (p.waitFor() != 0) {93Files.copy(Paths.get(testcase + ".stderr"), System.out);94failed += testcase + " ";95}9697// Granting a FilePermission on a relative path98testcase = "PonR";99p = p(args[0], "f")100.args("f", cwd + "/f")101.debug(testcase)102.start();103if (p.waitFor() != 0) {104Files.copy(Paths.get(testcase + ".stderr"), System.out);105failed += testcase + " ";106}107108// Reading file on classpath, not cwd109testcase = "cp";110String cprel = Paths.get(cwd).relativize(Paths.get(cp))111.normalize().toString();112p = p(args[0], "x")113.args(cp + "/f", cprel + "/f")114.debug(testcase)115.start();116if (p.waitFor() != 0) {117Files.copy(Paths.get(testcase + ".stderr"), System.out);118failed += testcase + " ";119}120121// Reading file on classpath, cwd122testcase = "cpHere";123p = p(args[0], "x")124.args(cwd + "/f", "f", "RES")125.cp(".") // Must! cancel the old CLASSPATH.126.debug(testcase)127.start();128if (p.waitFor() != 0) {129Files.copy(Paths.get(testcase + ".stderr"), System.out);130failed += testcase + " ";131}132133// Reading file on classpath, cwd134testcase = "cpSub";135p = p(args[0], "x")136.args(cwd + "/sub/f", "sub/f", "RES")137.cp("sub") // Must! There's CLASSPATH.138.debug(testcase)139.start();140if (p.waitFor() != 0) {141Files.copy(Paths.get(testcase + ".stderr"), System.out);142failed += testcase + " ";143}144145if (!failed.isEmpty()) {146throw new Exception(failed + "failed");147}148break;149// test <policy_type> <grant> <read...>150case "test":151if (args[1].equals("mine")) {152Policy.setPolicy(new MP(args[2]));153}154Exception e = null;155for (int i = 3; i < args.length; i++) {156try {157System.out.println(args[i]);158if (args[i].equals("RES")) {159CompatImpact.class.getResourceAsStream("f")160.close();161} else {162new File(args[i]).exists();163}164} catch (Exception e2) {165e = e2;166e2.printStackTrace(System.out);167}168}169if (e != null) {170System.err.println("====================");171throw e;172}173break;174// doPrivWithPerm test launcher175case "dopriv":176cwd = System.getProperty("user.dir");177// caller (CompatImpact doprivouter, no permission) in sub,178// executor (DoPrivInner, AllPermission) in inner.179p = Proc.create("CompatImpact")180.args("doprivouter")181.prop("java.security.manager", "")182.grant(new File("inner"))183.perm(new AllPermission())184.cp("sub", "inner")185.debug("doPriv")186.args(cwd)187.start();188if (p.waitFor() != 0) {189throw new Exception("dopriv test fails");190}191break;192// doprivouter <cwd>193case "doprivouter":194DoPrivInner.main(args);195break;196default:197throw new Exception("unknown " + args[0]);198}199}200201// Call by CompatImpact doprivouter, with AllPermission202public static class DoPrivInner {203public static void main(String[] args) throws Exception {204AccessController.doPrivileged((PrivilegedAction<Boolean>)205() -> new File("x").exists(),206null,207new FilePermission(args[1] + "/x", "read"));208AccessController.doPrivileged((PrivilegedAction<Boolean>)209() -> new File(args[1] + "/x").exists(),210null,211new FilePermission("x", "read"));212try {213AccessController.doPrivileged((PrivilegedAction<Boolean>)214() -> new File("x").exists(),215null,216new FilePermission("y", "read"));217throw new Exception("Should not read");218} catch (SecurityException se) {219// Expected220}221}222}223224// Return a Proc object for different policy types225private static Proc p(String type, String f) throws Exception {226Proc p = Proc.create("CompatImpact")227.prop("java.security.manager", "")228.inheritProp("jdk.security.filePermCompat");229p.args("test", type);230switch (type) {231case "builtin":232// For builtin policy, reading access to f can be233// granted as a permission234p.perm(new FilePermission(f, "read"));235p.args("-");236break;237case "mine":238// For my policy, f is passed into test and new MP(f)239// will be set as new policy240p.perm(new SecurityPermission("setPolicy"));241p.perm(new SecurityPermission("getPolicy"));242p.args(f);243break;244default:245throw new Exception("unknown " + type);246}247return p;248}249250// My own Policy impl, with only one granted permission, also not smart251// enough to know whether ProtectionDomain grants any permission252static class MP extends Policy {253static final Policy DEFAULT_POLICY = Policy.getPolicy();254final PermissionCollection pc;255256MP(String f) {257FilePermission p = new FilePermission(f, "read");258pc = p.newPermissionCollection();259pc.add(p);260}261@Override262public PermissionCollection getPermissions(CodeSource codesource) {263return pc;264}265266@Override267public PermissionCollection getPermissions(ProtectionDomain domain) {268return pc;269}270271@Override272public boolean implies(ProtectionDomain domain, Permission permission) {273return pc.implies(permission) || DEFAULT_POLICY.implies(domain, permission);274}275}276}277278279