author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8558 | e51c07113d09 |
permissions | -rw-r--r-- |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8558
diff
changeset
|
2 |
* Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved. |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
4 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
8 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
13 |
* accompanied this code). |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
14 |
* |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
22 |
*/ |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
23 |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
24 |
/* @test |
8558
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
25 |
* @bug 4313887 6838333 7006126 7023034 |
8158 | 26 |
* @summary Unit test for Files.createTempXXX |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
27 |
* @library .. |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
28 |
*/ |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
29 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
30 |
import java.nio.file.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
31 |
import static java.nio.file.StandardOpenOption.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
32 |
import java.nio.file.attribute.*; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
33 |
import java.io.IOException; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
34 |
import java.util.Set; |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
35 |
|
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
36 |
public class TemporaryFiles { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
37 |
|
8158 | 38 |
static void checkInDirectory(Path file, Path dir) { |
39 |
if (dir == null) |
|
40 |
dir = Paths.get(System.getProperty("java.io.tmpdir")); |
|
41 |
if (!file.getParent().equals(dir)) |
|
42 |
throw new RuntimeException("Not in expected directory"); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
43 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
44 |
|
8158 | 45 |
static void testTempFile(String prefix, String suffix, Path dir) |
46 |
throws IOException |
|
47 |
{ |
|
48 |
Path file = (dir == null) ? |
|
49 |
Files.createTempFile(prefix, suffix) : |
|
50 |
Files.createTempFile(dir, prefix, suffix); |
|
51 |
try { |
|
52 |
// check file name |
|
53 |
String name = file.getFileName().toString(); |
|
54 |
if (prefix != null && !name.startsWith(prefix)) |
|
55 |
throw new RuntimeException("Should start with " + prefix); |
|
56 |
if (suffix == null && !name.endsWith(".tmp")) |
|
57 |
throw new RuntimeException("Should end with .tmp"); |
|
58 |
if (suffix != null && !name.endsWith(suffix)) |
|
59 |
throw new RuntimeException("Should end with " + suffix); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
60 |
|
8158 | 61 |
// check file is in expected directory |
62 |
checkInDirectory(file, dir); |
|
63 |
||
64 |
// check that file can be opened for reading and writing |
|
65 |
Files.newByteChannel(file, READ).close(); |
|
66 |
Files.newByteChannel(file, WRITE).close(); |
|
67 |
Files.newByteChannel(file, READ,WRITE).close(); |
|
68 |
||
69 |
// check file permissions are 0600 or more secure |
|
70 |
if (Files.getFileStore(file).supportsFileAttributeView("posix")) { |
|
71 |
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(file); |
|
72 |
perms.remove(PosixFilePermission.OWNER_READ); |
|
73 |
perms.remove(PosixFilePermission.OWNER_WRITE); |
|
74 |
if (!perms.isEmpty()) |
|
75 |
throw new RuntimeException("Temporary file is not secure"); |
|
76 |
} |
|
77 |
} finally { |
|
78 |
Files.delete(file); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
79 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
80 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
81 |
|
8158 | 82 |
static void testTempFile(String prefix, String suffix) |
83 |
throws IOException |
|
84 |
{ |
|
85 |
testTempFile(prefix, suffix, null); |
|
86 |
} |
|
87 |
||
88 |
static void testTempDirectory(String prefix, Path dir) throws IOException { |
|
89 |
Path subdir = (dir == null) ? |
|
90 |
Files.createTempDirectory(prefix) : |
|
91 |
Files.createTempDirectory(dir, prefix); |
|
92 |
try { |
|
93 |
// check file name |
|
94 |
String name = subdir.getFileName().toString(); |
|
95 |
if (prefix != null && !name.startsWith(prefix)) |
|
96 |
throw new RuntimeException("Should start with " + prefix); |
|
97 |
||
98 |
// check directory is in expected directory |
|
99 |
checkInDirectory(subdir, dir); |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
100 |
|
8158 | 101 |
// check directory is empty |
102 |
DirectoryStream<Path> stream = Files.newDirectoryStream(subdir); |
|
103 |
try { |
|
104 |
if (stream.iterator().hasNext()) |
|
105 |
throw new RuntimeException("Tempory directory not empty"); |
|
106 |
} finally { |
|
107 |
stream.close(); |
|
108 |
} |
|
109 |
||
110 |
// check that we can create file in directory |
|
111 |
Path file = Files.createFile(subdir.resolve("foo")); |
|
112 |
try { |
|
113 |
Files.newByteChannel(file, READ,WRITE).close(); |
|
114 |
} finally { |
|
115 |
Files.delete(file); |
|
116 |
} |
|
117 |
||
118 |
// check file permissions are 0700 or more secure |
|
119 |
if (Files.getFileStore(subdir).supportsFileAttributeView("posix")) { |
|
120 |
Set<PosixFilePermission> perms = Files.getPosixFilePermissions(subdir); |
|
121 |
perms.remove(PosixFilePermission.OWNER_READ); |
|
122 |
perms.remove(PosixFilePermission.OWNER_WRITE); |
|
123 |
perms.remove(PosixFilePermission.OWNER_EXECUTE); |
|
124 |
if (!perms.isEmpty()) |
|
125 |
throw new RuntimeException("Temporary directory is not secure"); |
|
126 |
} |
|
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
127 |
} finally { |
8158 | 128 |
Files.delete(subdir); |
3065
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
129 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
130 |
} |
452aaa2899fc
6838333: New I/O: Update file system API to jsr203/nio2-b101
alanb
parents:
2057
diff
changeset
|
131 |
|
8158 | 132 |
static void testTempDirectory(String prefix) throws IOException { |
133 |
testTempDirectory(prefix, null); |
|
134 |
} |
|
135 |
||
136 |
static void testInvalidFileTemp(String prefix, String suffix) throws IOException { |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
137 |
try { |
8158 | 138 |
Path file = Files.createTempFile(prefix, suffix); |
139 |
Files.delete(file); |
|
140 |
throw new RuntimeException("IllegalArgumentException expected"); |
|
141 |
} catch (IllegalArgumentException expected) { } |
|
142 |
} |
|
143 |
||
144 |
public static void main(String[] args) throws IOException { |
|
145 |
// temporary-file directory |
|
146 |
testTempFile("blah", ".dat"); |
|
147 |
testTempFile("blah", null); |
|
148 |
testTempFile(null, ".dat"); |
|
149 |
testTempFile(null, null); |
|
150 |
testTempDirectory("blah"); |
|
151 |
testTempDirectory(null); |
|
152 |
||
153 |
// a given directory |
|
154 |
Path dir = Files.createTempDirectory("tmpdir"); |
|
155 |
try { |
|
156 |
testTempFile("blah", ".dat", dir); |
|
157 |
testTempFile("blah", null, dir); |
|
158 |
testTempFile(null, ".dat", dir); |
|
159 |
testTempFile(null, null, dir); |
|
160 |
testTempDirectory("blah", dir); |
|
161 |
testTempDirectory(null, dir); |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
162 |
} finally { |
8158 | 163 |
Files.delete(dir); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
164 |
} |
8158 | 165 |
|
166 |
// invalid prefix and suffix |
|
167 |
testInvalidFileTemp("../blah", null); |
|
168 |
testInvalidFileTemp("dir/blah", null); |
|
169 |
testInvalidFileTemp("blah", ".dat/foo"); |
|
8558
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
170 |
|
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
171 |
// nulls |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
172 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
173 |
Files.createTempFile("blah", ".tmp", (FileAttribute<?>[])null); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
174 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
175 |
} catch (NullPointerException ignore) { } |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
176 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
177 |
Files.createTempFile("blah", ".tmp", new FileAttribute<?>[] { null }); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
178 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
179 |
} catch (NullPointerException ignore) { } |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
180 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
181 |
Files.createTempDirectory("blah", (FileAttribute<?>[])null); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
182 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
183 |
} catch (NullPointerException ignore) { } |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
184 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
185 |
Files.createTempDirectory("blah", new FileAttribute<?>[] { null }); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
186 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
187 |
} catch (NullPointerException ignore) { } |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
188 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
189 |
Files.createTempFile((Path)null, "blah", ".tmp"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
190 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
191 |
} catch (NullPointerException ignore) { } |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
192 |
try { |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
193 |
Files.createTempDirectory((Path)null, "blah"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
194 |
throw new RuntimeException("NullPointerException expected"); |
e51c07113d09
7023034: Files.createTempDirectory((Path)null, "temp") does not throw NPE
alanb
parents:
8158
diff
changeset
|
195 |
} catch (NullPointerException ignore) { } |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
196 |
} |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
diff
changeset
|
197 |
} |