Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/Misc.java
41153 views
1
/*
2
* Copyright (c) 2008, 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
/* @test
25
* @bug 4313887 6838333 8005566 8032220 8215467 8255576
26
* @summary Unit test for miscellenous methods in java.nio.file.Files
27
* @library ..
28
*/
29
30
import java.nio.file.*;
31
import static java.nio.file.Files.*;
32
import static java.nio.file.LinkOption.*;
33
import java.nio.file.attribute.*;
34
import java.io.IOException;
35
import java.util.*;
36
37
public class Misc {
38
39
public static void main(String[] args) throws IOException {
40
Path dir = TestUtil.createTemporaryDirectory();
41
try {
42
testCreateDirectories(dir);
43
testIsHidden(dir);
44
testIsSameFile(dir);
45
testFileTypeMethods(dir);
46
testAccessMethods(dir);
47
} finally {
48
TestUtil.removeAll(dir);
49
}
50
}
51
52
/**
53
* Tests createDirectories
54
*/
55
static void testCreateDirectories(Path tmpdir) throws IOException {
56
// a no-op
57
createDirectories(tmpdir);
58
59
// create one directory
60
Path subdir = tmpdir.resolve("a");
61
createDirectories(subdir);
62
assertTrue(exists(subdir));
63
64
// create parents
65
subdir = subdir.resolve("b/c/d");
66
createDirectories(subdir);
67
assertTrue(exists(subdir));
68
69
// existing file is not a directory
70
Path file = createFile(tmpdir.resolve("x"));
71
try {
72
createDirectories(file);
73
throw new RuntimeException("failure expected");
74
} catch (FileAlreadyExistsException x) { }
75
try {
76
createDirectories(file.resolve("y"));
77
throw new RuntimeException("failure expected");
78
} catch (IOException x) { }
79
80
// the root directory always exists
81
Path root = Paths.get("/");
82
Files.createDirectories(root);
83
Files.createDirectories(root.toAbsolutePath());
84
}
85
86
/**
87
* Tests isHidden
88
*/
89
static void testIsHidden(Path tmpdir) throws IOException {
90
// passing an empty path must not throw any runtime exception
91
assertTrue(!isHidden(Path.of("")));
92
93
assertTrue(!isHidden(tmpdir));
94
95
Path file = tmpdir.resolve(".foo");
96
if (System.getProperty("os.name").startsWith("Windows")) {
97
createFile(file);
98
try {
99
setAttribute(file, "dos:hidden", true);
100
try {
101
assertTrue(isHidden(file));
102
} finally {
103
setAttribute(file, "dos:hidden", false);
104
}
105
} finally {
106
delete(file);
107
}
108
Path dir = tmpdir.resolve("hidden");
109
createDirectory(dir);
110
try {
111
setAttribute(dir, "dos:hidden", true);
112
try {
113
assertTrue(isHidden(dir));
114
} finally {
115
setAttribute(dir, "dos:hidden", false);
116
}
117
} finally {
118
delete(dir);
119
}
120
} else {
121
assertTrue(isHidden(file));
122
}
123
}
124
125
/**
126
* Tests isSameFile
127
*/
128
static void testIsSameFile(Path tmpdir) throws IOException {
129
Path thisFile = tmpdir.resolve("thisFile");
130
Path thatFile = tmpdir.resolve("thatFile");
131
132
/**
133
* Test: isSameFile for self
134
*/
135
assertTrue(isSameFile(thisFile, thisFile));
136
137
/**
138
* Test: Neither files exist
139
*/
140
try {
141
isSameFile(thisFile, thatFile);
142
throw new RuntimeException("IOException not thrown");
143
} catch (IOException x) {
144
}
145
try {
146
isSameFile(thatFile, thisFile);
147
throw new RuntimeException("IOException not thrown");
148
} catch (IOException x) {
149
}
150
151
createFile(thisFile);
152
try {
153
/**
154
* Test: One file exists
155
*/
156
try {
157
isSameFile(thisFile, thatFile);
158
throw new RuntimeException("IOException not thrown");
159
} catch (IOException x) {
160
}
161
try {
162
isSameFile(thatFile, thisFile);
163
throw new RuntimeException("IOException not thrown");
164
} catch (IOException x) {
165
}
166
167
/**
168
* Test: Both file exists
169
*/
170
createFile(thatFile);
171
try {
172
assertTrue(!isSameFile(thisFile, thatFile));
173
assertTrue(!isSameFile(thatFile, thisFile));
174
} finally {
175
delete(thatFile);
176
}
177
178
/**
179
* Test: Symbolic links
180
*/
181
if (TestUtil.supportsLinks(tmpdir)) {
182
createSymbolicLink(thatFile, thisFile);
183
try {
184
assertTrue(isSameFile(thisFile, thatFile));
185
assertTrue(isSameFile(thatFile, thisFile));
186
} finally {
187
TestUtil.deleteUnchecked(thatFile);
188
}
189
}
190
} finally {
191
delete(thisFile);
192
}
193
194
// nulls
195
try {
196
isSameFile(thisFile, null);
197
throw new RuntimeException("NullPointerException expected");
198
} catch (NullPointerException ignore) { }
199
try {
200
isSameFile(null, thatFile);
201
throw new RuntimeException("NullPointerException expected");
202
} catch (NullPointerException ignore) { }
203
}
204
205
/**
206
* Exercise isRegularFile, isDirectory, isSymbolicLink
207
*/
208
static void testFileTypeMethods(Path tmpdir) throws IOException {
209
assertTrue(!isRegularFile(tmpdir));
210
assertTrue(!isRegularFile(tmpdir, NOFOLLOW_LINKS));
211
assertTrue(isDirectory(tmpdir));
212
assertTrue(isDirectory(tmpdir, NOFOLLOW_LINKS));
213
assertTrue(!isSymbolicLink(tmpdir));
214
215
Path file = createFile(tmpdir.resolve("foo"));
216
try {
217
assertTrue(isRegularFile(file));
218
assertTrue(isRegularFile(file, NOFOLLOW_LINKS));
219
assertTrue(!isDirectory(file));
220
assertTrue(!isDirectory(file, NOFOLLOW_LINKS));
221
assertTrue(!isSymbolicLink(file));
222
223
if (TestUtil.supportsLinks(tmpdir)) {
224
Path link = tmpdir.resolve("link");
225
226
createSymbolicLink(link, tmpdir);
227
try {
228
assertTrue(!isRegularFile(link));
229
assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));
230
assertTrue(isDirectory(link));
231
assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
232
assertTrue(isSymbolicLink(link));
233
} finally {
234
delete(link);
235
}
236
237
createSymbolicLink(link, file);
238
try {
239
assertTrue(isRegularFile(link));
240
assertTrue(!isRegularFile(link, NOFOLLOW_LINKS));
241
assertTrue(!isDirectory(link));
242
assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
243
assertTrue(isSymbolicLink(link));
244
} finally {
245
delete(link);
246
}
247
248
createLink(link, file);
249
try {
250
assertTrue(isRegularFile(link));
251
assertTrue(isRegularFile(link, NOFOLLOW_LINKS));
252
assertTrue(!isDirectory(link));
253
assertTrue(!isDirectory(link, NOFOLLOW_LINKS));
254
assertTrue(!isSymbolicLink(link));
255
} finally {
256
delete(link);
257
}
258
}
259
260
} finally {
261
delete(file);
262
}
263
}
264
265
/**
266
* Exercise isReadbale, isWritable, isExecutable, exists, notExists
267
*/
268
static void testAccessMethods(Path tmpdir) throws IOException {
269
// should return false when file does not exist
270
Path doesNotExist = tmpdir.resolve("doesNotExist");
271
assertTrue(!isReadable(doesNotExist));
272
assertTrue(!isWritable(doesNotExist));
273
assertTrue(!isExecutable(doesNotExist));
274
assertTrue(!exists(doesNotExist));
275
assertTrue(notExists(doesNotExist));
276
277
Path file = createFile(tmpdir.resolve("foo"));
278
try {
279
// files exist
280
assertTrue(isReadable(file));
281
assertTrue(isWritable(file));
282
assertTrue(exists(file));
283
assertTrue(!notExists(file));
284
assertTrue(isReadable(tmpdir));
285
assertTrue(isWritable(tmpdir));
286
assertTrue(exists(tmpdir));
287
assertTrue(!notExists(tmpdir));
288
289
290
// sym link exists
291
if (TestUtil.supportsLinks(tmpdir)) {
292
Path link = tmpdir.resolve("link");
293
294
createSymbolicLink(link, file);
295
try {
296
assertTrue(isReadable(link));
297
assertTrue(isWritable(link));
298
assertTrue(exists(link));
299
assertTrue(!notExists(link));
300
} finally {
301
delete(link);
302
}
303
304
createSymbolicLink(link, doesNotExist);
305
try {
306
assertTrue(!isReadable(link));
307
assertTrue(!isWritable(link));
308
assertTrue(!exists(link));
309
assertTrue(exists(link, NOFOLLOW_LINKS));
310
assertTrue(notExists(link));
311
assertTrue(!notExists(link, NOFOLLOW_LINKS));
312
} finally {
313
delete(link);
314
}
315
}
316
317
/**
318
* Test: Edit ACL to deny WRITE and EXECUTE
319
*/
320
if (getFileStore(file).supportsFileAttributeView("acl")) {
321
AclFileAttributeView view =
322
getFileAttributeView(file, AclFileAttributeView.class);
323
UserPrincipal owner = view.getOwner();
324
List<AclEntry> acl = view.getAcl();
325
326
// Insert entry to deny WRITE and EXECUTE
327
AclEntry entry = AclEntry.newBuilder()
328
.setType(AclEntryType.DENY)
329
.setPrincipal(owner)
330
.setPermissions(AclEntryPermission.WRITE_DATA,
331
AclEntryPermission.EXECUTE)
332
.build();
333
acl.add(0, entry);
334
view.setAcl(acl);
335
try {
336
if (isRoot()) {
337
// root has all permissions
338
assertTrue(isWritable(file));
339
assertTrue(isExecutable(file));
340
} else {
341
assertTrue(!isWritable(file));
342
assertTrue(!isExecutable(file));
343
}
344
} finally {
345
// Restore ACL
346
acl.remove(0);
347
view.setAcl(acl);
348
}
349
}
350
351
/**
352
* Test: Windows DOS read-only attribute
353
*/
354
if (System.getProperty("os.name").startsWith("Windows")) {
355
setAttribute(file, "dos:readonly", true);
356
try {
357
assertTrue(!isWritable(file));
358
} finally {
359
setAttribute(file, "dos:readonly", false);
360
}
361
362
// Read-only attribute does not make direcory read-only
363
DosFileAttributeView view =
364
getFileAttributeView(tmpdir, DosFileAttributeView.class);
365
boolean save = view.readAttributes().isReadOnly();
366
view.setReadOnly(true);
367
try {
368
assertTrue(isWritable(file));
369
} finally {
370
view.setReadOnly(save);
371
}
372
}
373
} finally {
374
delete(file);
375
}
376
}
377
378
static void assertTrue(boolean okay) {
379
if (!okay)
380
throw new RuntimeException("Assertion Failed");
381
}
382
383
private static boolean isRoot() {
384
if (System.getProperty("os.name").startsWith("Windows"))
385
return false;
386
387
Path passwd = Paths.get("/etc/passwd");
388
return Files.isWritable(passwd);
389
}
390
}
391
392