Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/zipfs/ZipFSPermissionsTest.java
38833 views
1
/*
2
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*
23
*/
24
25
import org.testng.SkipException;
26
import org.testng.annotations.*;
27
28
import java.io.IOException;
29
import java.net.URI;
30
import java.nio.file.FileSystem;
31
import java.nio.file.FileSystems;
32
import java.nio.file.spi.FileSystemProvider;
33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.nio.file.Paths;
36
import java.nio.file.attribute.PosixFileAttributeView;
37
import java.nio.file.attribute.PosixFileAttributes;
38
import java.nio.file.attribute.PosixFilePermission;
39
import java.nio.file.attribute.PosixFilePermissions;
40
import java.util.Arrays;
41
import java.util.Collections;
42
import java.util.LinkedHashSet;
43
import java.util.Map;
44
import java.util.Set;
45
46
import static java.nio.charset.StandardCharsets.UTF_8;
47
import static java.nio.file.attribute.PosixFilePermission.*;
48
import static org.testng.Assert.assertEquals;
49
50
/**
51
* @test
52
* @bug 8229888
53
* @summary Updating an existing zip file does not preserve original permissions
54
* @modules jdk.zipfs
55
* @run testng/othervm ZipFSPermissionsTest
56
* @run testng/othervm/java.security.policy=ZipFSPermissionsTest.policy ZipFSPermissionsTest
57
*/
58
public class ZipFSPermissionsTest {
59
60
// Files used for testing
61
private static final Path zipFile = Paths.get("zipPermsTest.zip");
62
private static final Path entry0 = Paths.get("Entry-0.txt");
63
// Path of 2nd file to add to the Zip file
64
private static final Path entry1 = Paths.get("Entry-1.txt");
65
66
// Enable for permissions output
67
private static final boolean DEBUG = false;
68
69
/**
70
* Create the files used by the test
71
*/
72
@BeforeSuite
73
public void setUp() throws Exception {
74
boolean supportsPosix = FileSystems.getDefault()
75
.supportedFileAttributeViews().contains("posix");
76
77
// Check to see if File System supports POSIX permissions
78
if (supportsPosix) {
79
System.out.println("File Store Supports Posix");
80
} else {
81
// As there is no POSIX permission support, skip running the test
82
throw new SkipException("Cannot set permissions on this File Store");
83
}
84
Files.write(entry0, "Tennis Pro".getBytes(UTF_8));
85
Files.write(entry1, "Tennis is a lifetime sport!".getBytes(UTF_8));
86
}
87
88
/**
89
* Re-create the initial Zip file prior to each run.
90
*/
91
@BeforeMethod
92
public void before() throws Exception {
93
Files.deleteIfExists(zipFile);
94
zip(zipFile, Collections.singletonMap("create", "true"), entry0);
95
}
96
97
/**
98
* Remove Zip file used by test after each run.
99
*/
100
@AfterMethod
101
public void tearDown() throws Exception {
102
Files.deleteIfExists(zipFile);
103
}
104
105
/**
106
* Remove files used by test as part of final test run clean-up
107
*/
108
@AfterSuite
109
public void suiteCleanUp() throws Exception {
110
Files.deleteIfExists(zipFile);
111
Files.deleteIfExists(entry0);
112
Files.deleteIfExists(entry1);
113
}
114
115
/**
116
* Validate that the Zip file permissions are as expected after updating the
117
* Zip file
118
* @param newPerms The permissions to set on the Zip File before updating the
119
* file
120
* @throws Exception If an error occurs
121
*/
122
@Test(dataProvider = "posixPermissions")
123
public void testZipPerms(Set<PosixFilePermission> newPerms) throws Exception {
124
if (DEBUG) {
125
System.out.printf("Test Run with perms= %s%n", newPerms);
126
}
127
128
PosixFileAttributes attrs = getPosixAttributes(zipFile);
129
130
// Permissions used to verify the results of updating the Zip file
131
if (newPerms == null) {
132
// Use current Zip File permissions;
133
newPerms = attrs.permissions();
134
}
135
displayPermissions("Original permissions", zipFile);
136
137
// Now set the new permissions
138
Files.setPosixFilePermissions(zipFile, newPerms);
139
displayPermissions("Revised permissions", zipFile);
140
141
// Update the Zip file
142
zip(zipFile, Collections.emptyMap(), entry1);
143
144
// Validate that the permissions are as expected after updating the
145
// Zip file
146
PosixFileAttributes afterAttrs = getPosixAttributes(zipFile);
147
displayPermissions("Permissions after updating the Zip File", zipFile);
148
assertEquals(afterAttrs.permissions(), newPerms,
149
"Permissions were not updated as expected!");
150
}
151
152
/**
153
* Display the permissions for the specified Zip file when {@code DEBUG}
154
* is set to {@code true}
155
*
156
* @param msg String to include in the message
157
* @param zipFile Path to the Zip File
158
* @throws IOException If an error occurs obtaining the permissions
159
*/
160
public void displayPermissions(String msg, Path zipFile) throws IOException {
161
if (DEBUG) {
162
PosixFileAttributeView view = Files.getFileAttributeView(zipFile,
163
PosixFileAttributeView.class);
164
if (view == null) {
165
System.out.println("Could not obtain a PosixFileAttributeView!");
166
return;
167
}
168
PosixFileAttributes attrs = view.readAttributes();
169
System.out.printf("%s: %s, Owner: %s, Group:%s, permissions: %s%n", msg,
170
zipFile.getFileName(), attrs.owner().getName(),
171
attrs.group().getName(), PosixFilePermissions.toString(attrs.permissions()));
172
}
173
}
174
175
/**
176
* Create a Zip File System using the specified properties and a Zip file
177
* with the specified number of entries
178
*
179
* @param zipFile Path to the Zip File to create/update
180
* @param env Properties used for creating the Zip Filesystem
181
* @param source The path of the file to add to the Zip File
182
* @throws Exception If an error occurs while creating/updating the Zip file
183
*/
184
public void zip(Path zipFile, Map<String, String> env, Path source) throws Exception {
185
if (DEBUG) {
186
System.out.printf("File:%s, adding:%s%n", zipFile.toAbsolutePath(), source);
187
}
188
try (FileSystem zipfs = FileSystems.newFileSystem(new URI("jar", zipFile.toUri().toString(), null), env)) {
189
Files.copy(source, zipfs.getPath(source.getFileName().toString()));
190
}
191
}
192
193
/**
194
* Returns a file's POSIX file attributes.
195
*
196
* @param path The path to the Zip file
197
* @return The POSIX file attributes for the specified file or
198
* null if the POSIX attribute view is not available
199
* @throws IOException If an error occurs obtaining the POSIX attributes for
200
* the specified file
201
*/
202
public PosixFileAttributes getPosixAttributes(Path path) throws IOException {
203
PosixFileAttributes attrs = null;
204
PosixFileAttributeView view =
205
Files.getFileAttributeView(path, PosixFileAttributeView.class);
206
// Return if the attribute view is not supported
207
if (view == null) {
208
return null;
209
}
210
attrs = view.readAttributes();
211
return attrs;
212
}
213
214
/*
215
* DataProvider used to verify the permissions on a Zip file
216
* are as expected after updating the Zip file
217
*/
218
@DataProvider(name = "posixPermissions")
219
private Object[][] posixPermissions() {
220
return new Object[][]{
221
{null},
222
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OTHERS_READ))},
223
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE))},
224
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OTHERS_READ, OTHERS_WRITE))},
225
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE, OTHERS_READ,
226
OTHERS_WRITE, OTHERS_EXECUTE))},
227
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, OWNER_EXECUTE,
228
GROUP_READ, GROUP_WRITE,GROUP_EXECUTE, OTHERS_READ,
229
OTHERS_WRITE, OTHERS_EXECUTE))},
230
{new LinkedHashSet<>(Arrays.asList(OWNER_READ, OWNER_WRITE, GROUP_READ, GROUP_WRITE,
231
OTHERS_READ, OTHERS_WRITE))},
232
};
233
}
234
}
235
236