|
1 /* |
|
2 * Copyright 2008-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 import java.nio.file.*; |
|
25 import static java.nio.file.StandardOpenOption.*; |
|
26 import java.nio.file.attribute.*; |
|
27 import java.io.File; |
|
28 import java.io.IOException; |
|
29 import java.io.OutputStream; |
|
30 import java.util.Set; |
|
31 |
|
32 public class TemporaryFiles { |
|
33 |
|
34 static void checkFile(Path file) throws IOException { |
|
35 // check file is in temporary directory |
|
36 Path tmpdir = Paths.get(System.getProperty("java.io.tmpdir")); |
|
37 if (!file.getParent().equals(tmpdir)) |
|
38 throw new RuntimeException("Not in temporary directory"); |
|
39 |
|
40 // check that file can be opened for reading and writing |
|
41 file.newByteChannel(READ).close(); |
|
42 file.newByteChannel(WRITE).close(); |
|
43 file.newByteChannel(READ,WRITE).close(); |
|
44 |
|
45 // check file permissions are 0600 or more secure |
|
46 if (file.getFileStore().supportsFileAttributeView("posix")) { |
|
47 Set<PosixFilePermission> perms = Attributes |
|
48 .readPosixFileAttributes(file).permissions(); |
|
49 perms.remove(PosixFilePermission.OWNER_READ); |
|
50 perms.remove(PosixFilePermission.OWNER_WRITE); |
|
51 if (!perms.isEmpty()) |
|
52 throw new RuntimeException("Temporary file is not secure"); |
|
53 } |
|
54 } |
|
55 |
|
56 public static void main(String[] args) throws IOException { |
|
57 Path file = File.createTempFile("blah", null, false).toPath(); |
|
58 try { |
|
59 checkFile(file); |
|
60 } finally { |
|
61 TestUtil.deleteUnchecked(file); |
|
62 } |
|
63 |
|
64 // temporary file with deleteOnExit |
|
65 file = File.createTempFile("blah", "tmp", true).toPath(); |
|
66 checkFile(file); |
|
67 // write path to temporary file to file so that calling script can |
|
68 // check that it is deleted |
|
69 OutputStream out = Paths.get(args[0]).newOutputStream(); |
|
70 try { |
|
71 out.write(file.toString().getBytes()); |
|
72 } finally { |
|
73 out.close(); |
|
74 } |
|
75 } |
|
76 } |