Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/ZipFSPermissionsTest.java
38833 views
/*1* Copyright (c) 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*22*/2324import org.testng.SkipException;25import org.testng.annotations.*;2627import java.io.IOException;28import java.net.URI;29import java.nio.file.FileSystem;30import java.nio.file.FileSystems;31import java.nio.file.spi.FileSystemProvider;32import java.nio.file.Files;33import java.nio.file.Path;34import java.nio.file.Paths;35import java.nio.file.attribute.PosixFileAttributeView;36import java.nio.file.attribute.PosixFileAttributes;37import java.nio.file.attribute.PosixFilePermission;38import java.nio.file.attribute.PosixFilePermissions;39import java.util.Arrays;40import java.util.Collections;41import java.util.LinkedHashSet;42import java.util.Map;43import java.util.Set;4445import static java.nio.charset.StandardCharsets.UTF_8;46import static java.nio.file.attribute.PosixFilePermission.*;47import static org.testng.Assert.assertEquals;4849/**50* @test51* @bug 822988852* @summary Updating an existing zip file does not preserve original permissions53* @modules jdk.zipfs54* @run testng/othervm ZipFSPermissionsTest55* @run testng/othervm/java.security.policy=ZipFSPermissionsTest.policy ZipFSPermissionsTest56*/57public class ZipFSPermissionsTest {5859// Files used for testing60private static final Path zipFile = Paths.get("zipPermsTest.zip");61private static final Path entry0 = Paths.get("Entry-0.txt");62// Path of 2nd file to add to the Zip file63private static final Path entry1 = Paths.get("Entry-1.txt");6465// Enable for permissions output66private static final boolean DEBUG = false;6768/**69* Create the files used by the test70*/71@BeforeSuite72public void setUp() throws Exception {73boolean supportsPosix = FileSystems.getDefault()74.supportedFileAttributeViews().contains("posix");7576// Check to see if File System supports POSIX permissions77if (supportsPosix) {78System.out.println("File Store Supports Posix");79} else {80// As there is no POSIX permission support, skip running the test81throw new SkipException("Cannot set permissions on this File Store");82}83Files.write(entry0, "Tennis Pro".getBytes(UTF_8));84Files.write(entry1, "Tennis is a lifetime sport!".getBytes(UTF_8));85}8687/**88* Re-create the initial Zip file prior to each run.89*/90@BeforeMethod91public void before() throws Exception {92Files.deleteIfExists(zipFile);93zip(zipFile, Collections.singletonMap("create", "true"), entry0);94}9596/**97* Remove Zip file used by test after each run.98*/99@AfterMethod100public void tearDown() throws Exception {101Files.deleteIfExists(zipFile);102}103104/**105* Remove files used by test as part of final test run clean-up106*/107@AfterSuite108public void suiteCleanUp() throws Exception {109Files.deleteIfExists(zipFile);110Files.deleteIfExists(entry0);111Files.deleteIfExists(entry1);112}113114/**115* Validate that the Zip file permissions are as expected after updating the116* Zip file117* @param newPerms The permissions to set on the Zip File before updating the118* file119* @throws Exception If an error occurs120*/121@Test(dataProvider = "posixPermissions")122public void testZipPerms(Set<PosixFilePermission> newPerms) throws Exception {123if (DEBUG) {124System.out.printf("Test Run with perms= %s%n", newPerms);125}126127PosixFileAttributes attrs = getPosixAttributes(zipFile);128129// Permissions used to verify the results of updating the Zip file130if (newPerms == null) {131// Use current Zip File permissions;132newPerms = attrs.permissions();133}134displayPermissions("Original permissions", zipFile);135136// Now set the new permissions137Files.setPosixFilePermissions(zipFile, newPerms);138displayPermissions("Revised permissions", zipFile);139140// Update the Zip file141zip(zipFile, Collections.emptyMap(), entry1);142143// Validate that the permissions are as expected after updating the144// Zip file145PosixFileAttributes afterAttrs = getPosixAttributes(zipFile);146displayPermissions("Permissions after updating the Zip File", zipFile);147assertEquals(afterAttrs.permissions(), newPerms,148"Permissions were not updated as expected!");149}150151/**152* Display the permissions for the specified Zip file when {@code DEBUG}153* is set to {@code true}154*155* @param msg String to include in the message156* @param zipFile Path to the Zip File157* @throws IOException If an error occurs obtaining the permissions158*/159public void displayPermissions(String msg, Path zipFile) throws IOException {160if (DEBUG) {161PosixFileAttributeView view = Files.getFileAttributeView(zipFile,162PosixFileAttributeView.class);163if (view == null) {164System.out.println("Could not obtain a PosixFileAttributeView!");165return;166}167PosixFileAttributes attrs = view.readAttributes();168System.out.printf("%s: %s, Owner: %s, Group:%s, permissions: %s%n", msg,169zipFile.getFileName(), attrs.owner().getName(),170attrs.group().getName(), PosixFilePermissions.toString(attrs.permissions()));171}172}173174/**175* Create a Zip File System using the specified properties and a Zip file176* with the specified number of entries177*178* @param zipFile Path to the Zip File to create/update179* @param env Properties used for creating the Zip Filesystem180* @param source The path of the file to add to the Zip File181* @throws Exception If an error occurs while creating/updating the Zip file182*/183public void zip(Path zipFile, Map<String, String> env, Path source) throws Exception {184if (DEBUG) {185System.out.printf("File:%s, adding:%s%n", zipFile.toAbsolutePath(), source);186}187try (FileSystem zipfs = FileSystems.newFileSystem(new URI("jar", zipFile.toUri().toString(), null), env)) {188Files.copy(source, zipfs.getPath(source.getFileName().toString()));189}190}191192/**193* Returns a file's POSIX file attributes.194*195* @param path The path to the Zip file196* @return The POSIX file attributes for the specified file or197* null if the POSIX attribute view is not available198* @throws IOException If an error occurs obtaining the POSIX attributes for199* the specified file200*/201public PosixFileAttributes getPosixAttributes(Path path) throws IOException {202PosixFileAttributes attrs = null;203PosixFileAttributeView view =204Files.getFileAttributeView(path, PosixFileAttributeView.class);205// Return if the attribute view is not supported206if (view == null) {207return null;208}209attrs = view.readAttributes();210return attrs;211}212213/*214* DataProvider used to verify the permissions on a Zip file215* are as expected after updating the Zip file216*/217@DataProvider(name = "posixPermissions")218private Object[][] posixPermissions() {219return new Object[][]{220{null},221{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OTHERS_READ))},222{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE))},223{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OTHERS_READ, OTHERS_WRITE))},224{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, OTHERS_READ,225OTHERS_WRITE, OTHERS_EXECUTE))},226{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE,227GROUP_READ, GROUP_WRITE,GROUP_EXECUTE, OTHERS_READ,228OTHERS_WRITE, OTHERS_EXECUTE))},229{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, GROUP_READ, GROUP_WRITE,230OTHERS_READ, OTHERS_WRITE))},231};232}233}234235236