Path: blob/master/test/jdk/sun/security/provider/PolicyFile/CanonPath.java
41153 views
/*1* Copyright (c) 2005, 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 620304726* @summary Inconsistency in FilePermission27*/2829import java.io.*;3031public class CanonPath {3233private static boolean windows;3435private static final String WIN_FOOBAR = "\\foo\\bar\\";36private static final String UNIX_FOOBAR = "/foo/bar/";3738private static final String WIN_FOO = "\\foo.txt";39private static final String UNIX_FOO = "/foo.txt";4041private static final String WIN_BAR = "bar\\bar.txt";42private static final String UNIX_BAR = "bar/bar.txt";4344private static final String WIN_SLASH = "\\";45private static final String UNIX_SLASH = "/";4647private static void printCanonPath(String label, String path)48throws Exception {4950File f = new File(path);51System.out.println(label + " path = " + f.getCanonicalPath());52}5354public static void main(String[] args) throws Exception {5556if (System.getProperty("os.name").startsWith("Windows")) {57windows = true;58System.out.println("Testing on Windows");59} else {60System.out.println("Testing on Unix");61}626364System.out.println();65System.out.println("\\\\foo\\\\bar\\\\- versus /foo/bar/-");66FilePermission w = new FilePermission(WIN_FOOBAR + "-", "read");67FilePermission u = new FilePermission(UNIX_FOOBAR + "-", "read");68printCanonPath("WIN_FOOBAR", WIN_FOOBAR);69printCanonPath("UNIX_FOOBAR", UNIX_FOOBAR);70if (windows) {71if (!w.implies(u) || !u.implies(w)) {72throw new Exception("FOOBAR test failed");73}74} else {75if (w.implies(u) || u.implies(w)) {76throw new Exception("FOOBAR test failed");77}78}79808182System.out.println();83System.out.println("\\\\foo.txt versus /foo.txt");84w = new FilePermission(WIN_FOO, "read");85u = new FilePermission(UNIX_FOO, "read");86printCanonPath("WIN_FOO", WIN_FOO);87printCanonPath("UNIX_FOO", UNIX_FOO);88if (windows) {89if (!w.implies(u) || !u.implies(w)) {90throw new Exception("FOO test failed");91}92} else {93if (w.implies(u) || u.implies(w)) {94throw new Exception("FOO test failed");95}96}979899100System.out.println();101System.out.println("bar\\\\bar.txt versus bar/bar.txt");102w = new FilePermission(WIN_BAR, "read");103u = new FilePermission(UNIX_BAR, "read");104printCanonPath("WIN_BAR", WIN_BAR);105printCanonPath("UNIX_BAR", UNIX_BAR);106if (windows) {107if (!w.implies(u) || !u.implies(w)) {108throw new Exception("BAR test failed");109}110} else {111if (w.implies(u) || u.implies(w)) {112throw new Exception("BAR test failed");113}114}115116117118System.out.println();119System.out.println("\\\\ versus /");120w = new FilePermission(WIN_SLASH, "read");121u = new FilePermission(UNIX_SLASH, "read");122printCanonPath("WIN_SLASH", WIN_SLASH);123printCanonPath("UNIX_SLASH", UNIX_SLASH);124if (windows) {125if (!w.implies(u) || !u.implies(w)) {126throw new Exception("SLASH test failed");127}128} else {129if (w.implies(u) || u.implies(w)) {130throw new Exception("SLASH test failed");131}132}133134135136System.out.println();137System.out.println("\\\\- versus /-");138w = new FilePermission(WIN_SLASH + "-", "read");139u = new FilePermission(UNIX_SLASH + "-", "read");140printCanonPath("WIN_SLASH", WIN_SLASH);141printCanonPath("UNIX_SLASH", UNIX_SLASH);142if (windows) {143if (!w.implies(u) || !u.implies(w)) {144throw new Exception("SLASH/- test failed");145}146} else {147148// XXX149//150// on unix, /- implies everything151152//if (w.implies(u) || !u.implies(w)) {153// throw new Exception("SLASH/- test failed");154//}155}156157158159System.out.println();160System.out.println("- versus -");161w = new FilePermission("-", "read");162u = new FilePermission("-", "read");163printCanonPath("WIN_DASH", "");164printCanonPath("UNIX_DASH", "");165if (windows) {166if (!w.implies(u) || !u.implies(w)) {167throw new Exception("- test failed");168}169} else {170if (!w.implies(u) || !u.implies(w)) {171throw new Exception("- test failed");172}173}174175176177System.out.println();178System.out.println("- versus *");179w = new FilePermission("-", "read");180u = new FilePermission("*", "read");181printCanonPath("WIN_DASH", "");182printCanonPath("UNIX_STAR", "");183if (windows) {184185// XXX186//187// - implies *, but not the other way around188189if (!w.implies(u) || u.implies(w)) {190throw new Exception("- test failed");191}192} else {193194// XXX195//196// - implies *, but not the other way around197198if (!w.implies(u) || u.implies(w)) {199throw new Exception("- test failed");200}201}202203204205System.out.println();206System.out.println("\\\\* versus /*");207w = new FilePermission(WIN_SLASH + "*", "read");208u = new FilePermission(UNIX_SLASH + "*", "read");209printCanonPath("WIN_SLASH", WIN_SLASH);210printCanonPath("UNIX_SLASH", UNIX_SLASH);211if (windows) {212if (!w.implies(u) || !u.implies(w)) {213throw new Exception("SLASH/* test failed");214}215} else {216if (w.implies(u) || u.implies(w)) {217throw new Exception("SLASH/* test failed");218}219}220221222223System.out.println();224System.out.println("\\\\foo\\\\bar\\\\- versus /foo/bar/foobar/w.txt");225w = new FilePermission(WIN_FOOBAR + "-", "read");226u = new FilePermission("/foo/bar/foobar/w.txt", "read");227printCanonPath("FOOBAR", WIN_FOOBAR);228printCanonPath("W.TXT", "/foo/bar/foobar/w.txt");229if (windows) {230if (!w.implies(u) || u.implies(w)) {231throw new Exception("w.txt (-) test failed");232}233} else {234if (w.implies(u) || u.implies(w)) {235throw new Exception("w.txt (-) test failed");236}237}238239240241System.out.println();242System.out.println("\\\\foo\\\\bar\\\\* versus /foo/bar/w.txt");243w = new FilePermission(WIN_FOOBAR + "*", "read");244u = new FilePermission("/foo/bar/w.txt", "read");245printCanonPath("FOOBAR", WIN_FOOBAR);246printCanonPath("W.TXT", "/foo/bar/w.txt");247if (windows) {248if (!w.implies(u) || u.implies(w)) {249throw new Exception("w.txt (*) test failed");250}251} else {252if (w.implies(u) || u.implies(w)) {253throw new Exception("w.txt (*) test failed");254}255}256257258// make sure "/" does not imply "/-" nor "/*"259260System.out.println();261System.out.println("/ versus /- and /*");262File file = new File(UNIX_SLASH);263FilePermission recursive = new FilePermission264(file.getCanonicalPath() +265File.separatorChar +266"-",267"read");268FilePermission wild = new FilePermission269(file.getCanonicalPath() +270File.separatorChar +271"*",272"read");273FilePermission standard = new FilePermission274(file.getCanonicalPath(),275"read");276if (standard.implies(recursive) || standard.implies(wild)) {277throw new Exception("standard vs directory test failed");278}279}280}281282283