1 // Copyright (C) 2005, 2006, 2007 Red Hat, Inc. |
|
2 // Written by Gary Benson <gbenson@redhat.com> |
|
3 |
|
4 // This file is part of Mauve. |
|
5 |
|
6 // Mauve is free software; you can redistribute it and/or modify |
|
7 // it under the terms of the GNU General Public License as published by |
|
8 // the Free Software Foundation; either version 2, or (at your option) |
|
9 // any later version. |
|
10 |
|
11 // Mauve is distributed in the hope that it will be useful, |
|
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 // GNU General Public License for more details. |
|
15 |
|
16 // You should have received a copy of the GNU General Public License |
|
17 // along with Mauve; see the file COPYING. If not, write to |
|
18 // the Free Software Foundation, 59 Temple Place - Suite 330, |
|
19 // Boston, MA 02111-1307, USA. |
|
20 |
|
21 // Tags: JDK1.2 |
|
22 |
|
23 package gnu.testlet.java.io.RandomAccessFile; |
|
24 |
|
25 import java.io.File; |
|
26 import java.io.FileNotFoundException; |
|
27 import java.io.FileOutputStream; |
|
28 import java.io.FilePermission; |
|
29 import java.io.IOException; |
|
30 import java.io.RandomAccessFile; |
|
31 import java.security.Permission; |
|
32 |
|
33 import gnu.testlet.Testlet; |
|
34 import gnu.testlet.TestHarness; |
|
35 import gnu.testlet.TestSecurityManager; |
|
36 |
|
37 public class security implements Testlet |
|
38 { |
|
39 public void test (TestHarness harness) |
|
40 { |
|
41 File dir = new File(harness.getTempDirectory(), "mauve-testdir"); |
|
42 harness.check(dir.mkdir() || dir.exists(), "temp directory"); |
|
43 |
|
44 File file = new File(dir, "file"); |
|
45 String path = file.getPath(); |
|
46 try { |
|
47 new FileOutputStream(file); |
|
48 } |
|
49 catch (FileNotFoundException e) { |
|
50 harness.debug(e); |
|
51 harness.check(false, "unexpected exception"); |
|
52 } |
|
53 |
|
54 Permission rperm = new FilePermission(path, "read"); |
|
55 Permission wperm = new FilePermission(path, "write"); |
|
56 Permission rfdperm = new RuntimePermission("readFileDescriptor"); |
|
57 Permission wfdperm = new RuntimePermission("writeFileDescriptor"); |
|
58 |
|
59 String[] modes = new String[] {"r", "rw", "rws", "rwd"}; |
|
60 |
|
61 TestSecurityManager sm = new TestSecurityManager(harness); |
|
62 try { |
|
63 sm.install(); |
|
64 |
|
65 for (int i = 0; i < modes.length; i++) { |
|
66 String mode = modes[i]; |
|
67 |
|
68 Permission[] mustCheck, mayCheck; |
|
69 if (mode.equals("r")) { |
|
70 mustCheck = new Permission[] {rperm}; |
|
71 mayCheck = new Permission[] {rfdperm}; |
|
72 } |
|
73 else { |
|
74 mustCheck = new Permission[] {rperm, wperm}; |
|
75 mayCheck = new Permission[] {rfdperm, wfdperm}; |
|
76 } |
|
77 |
|
78 RandomAccessFile raf; |
|
79 |
|
80 // throwpoint: java.io.RandomAccessFile-RandomAccessFile(File, String) |
|
81 harness.checkPoint("File constructor, mode = \"" + mode + "\""); |
|
82 try { |
|
83 sm.prepareChecks(mustCheck, mayCheck); |
|
84 raf = new RandomAccessFile(file, mode); |
|
85 sm.checkAllChecked(); |
|
86 if (mode == "r") |
|
87 ensureUnwritable(harness, raf); |
|
88 } |
|
89 catch (SecurityException ex) { |
|
90 harness.debug(ex); |
|
91 harness.check(false, "unexpected check"); |
|
92 } |
|
93 |
|
94 // throwpoint: java.io.RandomAccessFile-RandomAccessFile(String, String) |
|
95 harness.checkPoint("String constructor, mode = \"" + mode + "\""); |
|
96 try { |
|
97 sm.prepareChecks(mustCheck, mayCheck); |
|
98 raf = new RandomAccessFile(path, mode); |
|
99 sm.checkAllChecked(); |
|
100 if (mode == "r") |
|
101 ensureUnwritable(harness, raf); |
|
102 } |
|
103 catch (SecurityException ex) { |
|
104 harness.debug(ex); |
|
105 harness.check(false, "unexpected check"); |
|
106 } |
|
107 } |
|
108 } |
|
109 catch (Exception ex) { |
|
110 harness.debug(ex); |
|
111 harness.check(false, "Unexpected exception"); |
|
112 } |
|
113 finally { |
|
114 sm.uninstall(); |
|
115 |
|
116 file.delete(); |
|
117 dir.delete(); |
|
118 } |
|
119 } |
|
120 |
|
121 private void ensureUnwritable(TestHarness harness, RandomAccessFile file) |
|
122 { |
|
123 harness.checkPoint("read-only checks"); |
|
124 |
|
125 byte[] barry = new byte[] {2, 4, 2}; |
|
126 try { |
|
127 for (int i = 1; i <= 14; i++) { |
|
128 long pointer = file.getFilePointer(); |
|
129 try { |
|
130 switch (i) { |
|
131 case 1: |
|
132 file.write(barry); |
|
133 break; |
|
134 case 2: |
|
135 file.write(barry, 1, 2); |
|
136 break; |
|
137 case 3: |
|
138 file.write(1); |
|
139 break; |
|
140 case 4: |
|
141 file.writeBoolean(true); |
|
142 break; |
|
143 case 5: |
|
144 file.writeByte(1); |
|
145 break; |
|
146 case 6: |
|
147 file.writeBytes("hello mum"); |
|
148 break; |
|
149 case 7: |
|
150 file.writeChar(1); |
|
151 break; |
|
152 case 8: |
|
153 file.writeChars("hello mum"); |
|
154 break; |
|
155 case 9: |
|
156 file.writeDouble(1); |
|
157 break; |
|
158 case 10: |
|
159 file.writeFloat(1); |
|
160 break; |
|
161 case 11: |
|
162 file.writeInt(1); |
|
163 break; |
|
164 case 12: |
|
165 file.writeLong(1); |
|
166 break; |
|
167 case 13: |
|
168 file.writeShort(1); |
|
169 break; |
|
170 case 14: |
|
171 file.writeUTF("hello mum"); |
|
172 break; |
|
173 } |
|
174 harness.check(false); |
|
175 } |
|
176 catch (IOException e) { |
|
177 harness.check(file.getFilePointer() == pointer); |
|
178 } |
|
179 } |
|
180 } |
|
181 catch (IOException e) { |
|
182 harness.debug(e); |
|
183 harness.check(false, "unexpected IOException"); |
|
184 } |
|
185 } |
|
186 } |
|