Path: blob/master/test/jdk/java/io/FilePermission/FilePermissionTest.java
41149 views
/*1* Copyright (c) 2016, 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*/2223import java.io.File;24import java.io.FilePermission;25import java.util.Arrays;26import java.util.List;2728/*29* @test30* @bug 815605431* @summary Test some of FilePermission methods when canonicalization property32* set and un-set.33* @run main/othervm -Djdk.io.permissionsUseCanonicalPath=true34* FilePermissionTest truetruetruetruetruetrue35* @run main/othervm -Djdk.io.permissionsUseCanonicalPath=false36* FilePermissionTest falsefalsefalsefalsefalsefalse37* @run main FilePermissionTest falsefalsefalsefalsefalsefalse38*/39public class FilePermissionTest {4041public static void main(String[] args) throws Exception {4243final File realFile = new File("exist.file");44try {45if (!realFile.createNewFile()) {46throw new RuntimeException("Unable to create a file.");47}48check(Arrays.asList(realFile.getName(), "notexist.file"), args[0]);49} finally {50if (realFile.exists()) {51realFile.delete();52}53}54}5556private static void check(List<String> files, String expected) {5758StringBuilder actual = new StringBuilder();59files.forEach(f -> {60StringBuilder result = new StringBuilder();61FilePermission fp1 = new FilePermission(f, "read");62FilePermission fp2 = new FilePermission(63new File(f).getAbsolutePath(), "read");64result.append(fp1.equals(fp2));65result.append(fp1.implies(fp2));66result.append(fp1.hashCode() == fp2.hashCode());67System.out.println(fp1 + " Vs. " + fp2 + " : Result: " + result);68actual.append(result);69});70if (!expected.equals(actual.toString())) {71throw new RuntimeException("Failed: " + expected + "/" + actual);72}73}74}757677