/* * Copyright (c) 2018, SAP SE. All rights reserved. * * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact SAP SE, Dietmar-Hopp-Allee 16, 69190 Walldorf, Germany * or visit www.sap.com if you need additional information or have any * questions. */ /** * @test * @run testng TestPosixPerms * @summary Test zip file operations handling posix permissions. */ import static java.nio.file.attribute.PosixFilePermission.*; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotEquals; import static org.testng.Assert.assertTrue; import static org.testng.Assert.fail; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.attribute.PosixFilePermission; import java.nio.file.attribute.PosixFilePermissions; import java.util.Collections; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipOutputStream; import org.testng.annotations.Test; public class TestPosixPerms { private static final int NUMBER_OF_ENTRIES_IN_POSIXTEST_ZIP = 10; private int entries; private static void checkPermissionsOfEntry(String name, ZipEntry ze, boolean directory, Set expected) { System.out.print("Checking " + name + "..."); assertEquals(ze.isDirectory(), directory, "Unexpected directory attribute."); Set permissions = ze.getPosixPermissions().orElse(null); if (expected == null) { assertEquals(permissions, null, "Nonempty posix permissions associated with entry."); System.out.println(); } else { assertNotEquals(permissions, null, "No posix permissions associated with entry."); System.out.println("[" + PosixFilePermissions.toString(permissions) + "]"); assertEquals(permissions.size(), expected.size(), "Unexpected number of permissions."); for (PosixFilePermission p : expected) { assertTrue(permissions.contains(p), "Posix permission " + p + " missing."); } } } private void putEntry(ZipOutputStream zos, String name, Set perms) throws IOException { ZipEntry e = new ZipEntry(name); if (perms != null) { e.setPosixPermissions(perms); } zos.putNextEntry(e); entries++; } @Test public void readCheckedInArchiveWithPosixPerms() throws Exception { File zipFile = new File(System.getProperty("test.src", "."), "posixtest.zip"); System.out.println("Testing " + zipFile.getAbsolutePath() + "..."); try (ZipFile zf = new ZipFile(zipFile)) { int size = zf.size(); System.out.println("Number of entries: " + size + "..."); assertEquals(size, NUMBER_OF_ENTRIES_IN_POSIXTEST_ZIP, "File contained wrong number of entries."); zf.stream().forEach((ze)->{ String name = ze.getName(); if (name.startsWith("dir")) { checkPermissionsOfEntry(name, ze, true, Set.of( OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE)); } else if (name.equals("uread")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_READ)); } else if (name.equals("uwrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_WRITE)); } else if (name.equals("uexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_EXECUTE)); } else if (name.equals("gread")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_READ)); } else if (name.equals("gwrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_WRITE)); } else if (name.equals("gexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_EXECUTE)); } else if (name.equals("oread")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_READ)); } else if (name.equals("owrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_WRITE)); } else if (name.equals("oexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_EXECUTE)); } else { fail("Found unknown entry " + name + "."); } }); } } @Test public void testWriteAndReadArchiveWithPosixPerms() throws Exception { File zfile = Paths.get(System.getProperty("test.dir", "."), "testPosixPerms.zip").toFile(); System.out.println("Create " + zfile.getAbsolutePath() + "..."); if (zfile.exists()) { zfile.delete(); } ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zfile)); entries = 0; putEntry(zos, "dir/", Set.of( OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE)); putEntry(zos, "uread", Set.of(OWNER_READ)); putEntry(zos, "uwrite", Set.of(OWNER_WRITE)); putEntry(zos, "uexec", Set.of(OWNER_EXECUTE)); putEntry(zos, "gread", Set.of(GROUP_READ)); putEntry(zos, "gwrite", Set.of(GROUP_WRITE)); putEntry(zos, "gexec", Set.of(GROUP_EXECUTE)); putEntry(zos, "oread", Set.of(OTHERS_READ)); putEntry(zos, "owrite", Set.of(OTHERS_WRITE)); putEntry(zos, "oexec", Set.of(OTHERS_EXECUTE)); putEntry(zos, "emptyperms", Collections.emptySet()); putEntry(zos, "noperms", null); zos.close(); System.out.println("Test reading " + zfile.getAbsolutePath() + "..."); try (ZipFile zf = new ZipFile(zfile)) { int size = zf.size(); System.out.println("Number of entries: " + size + "..."); assertEquals(size, entries, "File contained wrong number of entries."); zf.stream().forEach((ze)->{ String name = ze.getName(); if (name.startsWith("dir")) { checkPermissionsOfEntry(name, ze, true, Set.of( OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, GROUP_READ, GROUP_WRITE, GROUP_EXECUTE, OTHERS_READ, OTHERS_WRITE, OTHERS_EXECUTE)); } else if (name.equals("uread")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_READ)); } else if (name.equals("uwrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_WRITE)); } else if (name.equals("uexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(OWNER_EXECUTE)); } else if (name.equals("gread")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_READ)); } else if (name.equals("gwrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_WRITE)); } else if (name.equals("gexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(GROUP_EXECUTE)); } else if (name.equals("oread")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_READ)); } else if (name.equals("owrite")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_WRITE)); } else if (name.equals("oexec")) { checkPermissionsOfEntry(name, ze, false, Set.of(OTHERS_EXECUTE)); } else if (name.equals("emptyperms")) { checkPermissionsOfEntry(name, ze, false, Collections.emptySet()); } else if (name.equals("noperms")) { checkPermissionsOfEntry(name, ze, false, null); } else { fail("Found unknown entry " + name + "."); } }); } } }