Path: blob/master/test/jdk/java/io/FilePermission/Correctness.java
41149 views
/*1* Copyright (c) 2016, 2017, 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*25* @test26* @bug 816470527* @modules java.base/java.io:open28* @summary Remove pathname canonicalization from FilePermission29*/3031import java.io.FilePermission;32import java.lang.reflect.Method;33import java.nio.file.Path;34import java.nio.file.Paths;3536public class Correctness {3738static boolean err = false;39static Method containsMethod;40static boolean isWindows =41System.getProperty("os.name").contains("Windows");42public static void main(String args[]) throws Exception {43check("/", "/");44checkNo("/", "/x");45checkNo("/", "/../x");4647checkNo("/", "x");4849check("/-", "/*");50checkNo("/*", "/-");5152check("/*", "/x");53check("/-", "/x");54check("/-", "/x/*");55check("/-", "/x/-");56check("/-", "/x/y");57checkNo("/*", "/x/y");58check("/x/*", "/x/x");59checkNo("/x/-", "/x");60checkNo("/x/*", "/x");61check("/x/-", "/x/x");62check("/x/-", "/x/x/y");63checkNo("/x/*", "/x/x/y");64checkNo("/x/*", "/x");6566check("*", "x");67checkNo("", "x");68check("-", "x");69check("-", "*");70check("-", "a/-");71check("-", "a/*");72checkNo("*", "a/b");73check("a/*", "a/b");74check("a/-", "a/*");75check("a/-", "a/b/c");76checkNo("a/*", "a/b/c");7778check("../", "../");79check("../-", "../*");80check("../../*", "../../a");8182// If we allow .. and abs/rel checks83check("../-", "a");84check("../../-", "-");85checkNo("../../*", "a");86//check("/-", "a");87//checkNo("/*", "a");88//check("/-", "-");8990try {91containsMethod = FilePermission.class.getDeclaredMethod(92"containsPath", Path.class, Path.class);93containsMethod.setAccessible(true);94System.out.println();9596// The 1st 2 args of contains() must be normalized paths.97// When FilePermission::containsPath is called by implies,98// paths have already been normalized.99contains("x", "x", 0);100contains("x", "x/y", 1);101contains("x", "x/y/z", 2);102contains("x", "y", -1);103contains("x", "", -1);104contains("", "", 0);105contains("", "x", 1);106contains("", "x/y", 2);107contains("/", "/", 0);108contains("/", "/x", 1);109contains("/", "/x/y", 2);110contains("/x", "/x/y", 1);111contains("/x", "/y", -1);112//contains("/", "..", Integer.MAX_VALUE);113//contains("/", "x", Integer.MAX_VALUE);114//contains("/", "x/y", Integer.MAX_VALUE);115//contains("/", "../x", Integer.MAX_VALUE);116contains("/x", "y", -1);117contains("x", "/y", -1);118119contains("", "..", -1);120contains("", "../x", -1);121contains("..", "", 1);122contains("..", "x", 2);123contains("..", "x/y", 3);124contains("../x", "x", -1);125contains("../x", "y", -1);126contains("../x", "../x/y", 1);127contains("../../x", "../../x/y", 1);128contains("../../../x", "../../../x/y", 1);129contains("../x", "../y", -1);130} catch (NoSuchMethodException e) {131// Ignored132}133if (err) throw new Exception("Failed.");134}135136// Checks if s2 is inside s1 and depth is expected.137static void contains(String s1, String s2, int expected) throws Exception {138contains0(s1, s2, expected);139if (isWindows) {140contains0("C:" + s1, s2, -1);141contains0(s1, "C:" + s2, -1);142contains0("C:" + s1, "D:" + s2, -1);143contains0("C:" + s1, "C:" + s2, expected);144}145}146147static void contains0(String s1, String s2, int expected) throws Exception {148Path p1 = Paths.get(s1);149Path p2 = Paths.get(s2);150int d = (int)containsMethod.invoke(null, p1, p2);151Path p;152try {153p = p2.relativize(p1);154} catch (Exception e) {155p = null;156}157System.out.printf("%-20s -> %-20s: %20s %5d %5d %s\n", s1, s2, p,158d, expected, d==expected?"":" WRONG");159if (d != expected) {160err = true;161}162}163164static void check0(String s1, String s2, boolean expected) {165FilePermission fp1 = new FilePermission(s1, "read");166FilePermission fp2 = new FilePermission(s2, "read");167boolean b = fp1.implies(fp2);168System.out.printf("%-30s -> %-30s: %5b %s\n",169s1, s2, b, b==expected?"":" WRONG");170if (b != expected) {171err = true;172System.out.println(fp1);173System.out.println(fp2);174}175}176177static void check(String s1, String s2, boolean expected) {178check0(s1, s2, expected);179if (isWindows) {180check0("C:" + s1, s2, false);181check0(s1, "C:" + s2, false);182check0("C:" + s1, "D:" + s2, false);183check0("C:" + s1, "C:" + s2, expected);184}185}186187static void check(String s1, String s2) {188check(s1, s2, true);189}190191static void checkNo(String s1, String s2) {192check(s1, s2, false);193}194}195196197