|
1 /* |
|
2 * Copyright 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 /** |
|
25 * @test |
|
26 * @bug 6684104 |
|
27 * @summary Test verifies that ImageIO checks all permissions required for |
|
28 * the file cache usage: |
|
29 * |
|
30 * no policy file: No security restrictions. |
|
31 * Expected result: ImageIO creates file-cached stream. |
|
32 * |
|
33 * w.policy: the case when we have read and write permissions |
|
34 * for java.io.temp directory but have only write permission |
|
35 * for a temp file. |
|
36 * Expected result: ImageIO create a memory-cached stream |
|
37 * image output stream. |
|
38 * |
|
39 * rw.policy: the case when we have read and write permissions |
|
40 * for java.io.temp directory but have only read and write |
|
41 * permission for a temp cache file. |
|
42 * Expected result: ImageIO creates a memory-cached stream |
|
43 * because temporary cache file can not be deleted. |
|
44 * |
|
45 * rwd.policy: the case when we have read and write permissions |
|
46 * for java.io.temp directory and have all required permissions |
|
47 * (read, write, and delete) for a temporary cache file. |
|
48 * Expected result: ImageIO creates file-cached stream. |
|
49 * |
|
50 * -Djava.security.debug=access can be used to verify file permissions. |
|
51 * |
|
52 * @run main CachePermissionsTest true |
|
53 * @run main/othervm/policy=w.policy CachePermissionsTest false |
|
54 * @run main/othervm/policy=rw.policy CachePermissionsTest false |
|
55 * @run main/othervm/policy=rwd.policy CachePermissionsTest true |
|
56 */ |
|
57 |
|
58 import java.io.File; |
|
59 import java.io.IOException; |
|
60 import java.io.ByteArrayOutputStream; |
|
61 import javax.imageio.stream.ImageOutputStream; |
|
62 |
|
63 import javax.imageio.ImageIO; |
|
64 |
|
65 |
|
66 public class CachePermissionsTest { |
|
67 public static void main(String[] args) { |
|
68 boolean isFileCacheExpected = |
|
69 Boolean.valueOf(args[0]).booleanValue(); |
|
70 System.out.println("Is file cache expected: " + isFileCacheExpected); |
|
71 |
|
72 ImageIO.setUseCache(true); |
|
73 |
|
74 System.out.println("java.io.tmpdir is " + System.getProperty("java.io.tmpdir")); |
|
75 |
|
76 ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
|
77 |
|
78 try { |
|
79 ImageOutputStream ios = ImageIO.createImageOutputStream(baos); |
|
80 |
|
81 boolean isFileCache = ios.isCachedFile(); |
|
82 System.out.println("Is file cache used: " + isFileCache); |
|
83 |
|
84 if (isFileCache !=isFileCacheExpected) { |
|
85 System.out.println("WARNING: file chace usage is not as expected!"); |
|
86 } |
|
87 |
|
88 System.out.println("Verify data writing..."); |
|
89 for (int i = 0; i < 8192; i++) { |
|
90 ios.writeInt(i); |
|
91 } |
|
92 |
|
93 System.out.println("Verify data reading..."); |
|
94 ios.seek(0L); |
|
95 |
|
96 for (int i = 0; i < 8192; i++) { |
|
97 int j = ios.readInt(); |
|
98 if (i != j) { |
|
99 throw new RuntimeException("Wrong data in the stream " + j + " instead of " + i); |
|
100 } |
|
101 } |
|
102 |
|
103 System.out.println("Verify stream closing..."); |
|
104 ios.close(); |
|
105 } catch (IOException e) { |
|
106 /* |
|
107 * Something went wrong? |
|
108 */ |
|
109 throw new RuntimeException("Test FAILED.", e); |
|
110 } catch (SecurityException e) { |
|
111 /* |
|
112 * We do not expect security execptions here: |
|
113 * we there are any security restrition, ImageIO |
|
114 * should swith to memory-cached streams, instead |
|
115 * of using file cache. |
|
116 */ |
|
117 throw new RuntimeException("Test FAILED.", e); |
|
118 } |
|
119 } |
|
120 } |