Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/security/tools/jarsigner/PosixPermissionsTest.java
41152 views
1
/*
2
* Copyright (c) 2020, 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
* @test
26
* @bug 8218021
27
* @summary Have jarsigner preserve posix permission attributes
28
* @modules jdk.jartool/sun.security.tools.jarsigner
29
* java.base/sun.security.tools.keytool
30
* @library /test/lib
31
* @run main/othervm PosixPermissionsTest
32
*/
33
34
import java.net.URI;
35
import java.nio.file.FileSystem;
36
import java.nio.file.FileSystems;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.nio.file.StandardCopyOption;
40
import java.nio.file.attribute.PosixFilePermission;
41
import java.nio.file.attribute.PosixFilePermissions;
42
import java.util.HashMap;
43
import java.util.List;
44
import java.util.Map;
45
import java.util.Set;
46
47
import jdk.test.lib.SecurityTools;
48
49
public class PosixPermissionsTest {
50
private static List<String> perms = List.of(
51
"---------",
52
"r--------",
53
"-w-------",
54
"--x------",
55
"rwx------",
56
"---r-----",
57
"----w----",
58
"-----x---",
59
"---rwx---",
60
"------r--",
61
"-------w-",
62
"--------x",
63
"------rwx",
64
"r--r-----",
65
"r--r--r--",
66
"rw-rw----",
67
"rwxrwx---",
68
"rw-rw-r--",
69
"r-xr-x---",
70
"r-xr-xr-x",
71
"rwxrwxrwx");
72
73
private final static String ZIPFILENAME = "8218021-test.zip";
74
private final static String JARFILENAME = "8218021-test.jar";
75
private final static URI JARURI = URI.create("jar:" + Path.of(JARFILENAME).toUri());
76
private final static URI ZIPURI = URI.create("jar:" + Path.of(ZIPFILENAME).toUri());
77
private static Path file;
78
private static int count;
79
private static Set<PosixFilePermission> permsSet;
80
private static String expectedJarPerms;
81
private static final String WARNING_MSG = "POSIX file permission and/or symlink " +
82
"attributes detected. These attributes are ignored when signing and are not " +
83
"protected by the signature.";
84
85
public static void main(String[] args) throws Exception {
86
createFiles();
87
88
// check permissions before signing
89
verifyFilePermissions(ZIPURI, true);
90
verifyFilePermissions(JARURI, false);
91
92
// generate key for signing
93
SecurityTools.keytool(
94
"-genkey",
95
"-keyalg", "RSA",
96
"-dname", "CN=Coffey, OU=JPG, O=Oracle, L=Santa Clara, ST=California, C=US",
97
"-alias", "examplekey",
98
"-storepass", "password",
99
"-keypass", "password",
100
"-keystore", "examplekeystore",
101
"-validity", "365")
102
.shouldHaveExitValue(0);
103
104
// sign zip file - expect warning
105
SecurityTools.jarsigner(
106
"-keystore", "examplekeystore",
107
"-verbose", ZIPFILENAME,
108
"-storepass", "password",
109
"-keypass", "password",
110
"examplekey")
111
.shouldHaveExitValue(0)
112
.shouldContain(WARNING_MSG);
113
114
// recheck permissions after signing
115
verifyFilePermissions(ZIPURI, true);
116
117
// sign jar file - expect no warning
118
SecurityTools.jarsigner(
119
"-keystore", "examplekeystore",
120
"-verbose", JARFILENAME,
121
"-storepass", "password",
122
"-keypass", "password",
123
"examplekey")
124
.shouldHaveExitValue(0)
125
.shouldNotContain(WARNING_MSG);
126
127
// recheck permissions after signing
128
verifyFilePermissions(JARURI, false);
129
130
// verify zip file - expect warning
131
SecurityTools.jarsigner(
132
"-keystore", "examplekeystore",
133
"-storepass", "password",
134
"-keypass", "password",
135
"-verbose",
136
"-verify", ZIPFILENAME)
137
.shouldHaveExitValue(0)
138
.shouldContain(WARNING_MSG);
139
140
// verify jar file - expect no warning
141
SecurityTools.jarsigner(
142
"-keystore", "examplekeystore",
143
"-storepass", "password",
144
"-keypass", "password",
145
"-verbose",
146
"-verify", JARFILENAME)
147
.shouldHaveExitValue(0)
148
.shouldNotContain(WARNING_MSG);
149
}
150
151
private static void createFiles() throws Exception {
152
153
String fileList = " ";
154
Map<String, String> env = new HashMap<>();
155
env.put("create", "true");
156
env.put("enablePosixFileAttributes", "true");
157
158
try (FileSystem zipfs = FileSystems.newFileSystem(ZIPURI, env)) {
159
for (String s : perms) {
160
file = Path.of("test_" + count++);
161
fileList += file + " ";
162
permsSet = PosixFilePermissions.fromString(s);
163
Files.createFile(file);
164
165
Files.copy(file,
166
zipfs.getPath(file.toString()),
167
StandardCopyOption.COPY_ATTRIBUTES);
168
Files.setPosixFilePermissions(zipfs.getPath(file.toString()), permsSet);
169
}
170
}
171
172
// create jar file for testing also
173
SecurityTools.jar("cf " + JARFILENAME + fileList);
174
try (FileSystem jarfs = FileSystems.newFileSystem(JARURI, env)) {
175
expectedJarPerms = PosixFilePermissions.toString(
176
Files.getPosixFilePermissions(jarfs.getPath("test_1")));
177
}
178
}
179
180
private static void verifyFilePermissions(URI u, boolean containAttributes) throws Exception {
181
count = 0;
182
for (String s : perms) {
183
file = Path.of("test_" + count++);
184
checkEntryAttributes(u, file, s, containAttributes);
185
}
186
}
187
188
private static void checkEntryAttributes(URI uri, Path file,
189
String expectedPerms, boolean containAttributes) throws Exception {
190
try (FileSystem zipfs = FileSystems.newFileSystem(uri, Map.of("enablePosixFileAttributes", "true"))) {
191
Path p = zipfs.getPath(file.getFileName().toString());
192
Set<PosixFilePermission> permsSet = Files.getPosixFilePermissions(p);
193
String actualPerms = PosixFilePermissions.toString(permsSet);
194
if (containAttributes) {
195
if (!expectedPerms.equals(actualPerms)) {
196
throw new RuntimeException("Unexpected permissions for: " + file + ". Received: " + actualPerms);
197
}
198
} else {
199
if (!actualPerms.equals(expectedJarPerms)) {
200
throw new RuntimeException("Expected default permissions for " + file);
201
}
202
}
203
}
204
}
205
}
206
207