OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / tests / api / java / io / FilePermissionTest.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package tests.api.java.io;
19
20 import dalvik.annotation.TestLevel;
21 import dalvik.annotation.TestTargetClass;
22 import dalvik.annotation.TestTargetNew;
23
24 import java.io.File;
25 import java.io.FilePermission;
26 import java.security.PermissionCollection;
27
28 @TestTargetClass(FilePermission.class)
29 public class FilePermissionTest extends junit.framework.TestCase {
30
31     FilePermission readAllFiles;
32     FilePermission alsoReadAllFiles;
33     FilePermission allInCurrent;
34     FilePermission readInCurrent;
35     FilePermission readInFile;
36
37     @Override protected void setUp() throws Exception {
38         super.setUp();
39
40         readAllFiles = new FilePermission("<<ALL FILES>>", "read");
41         alsoReadAllFiles = new FilePermission("<<ALL FILES>>", "read");
42         allInCurrent = new FilePermission("*", "read, write, execute,delete");
43         readInCurrent = new FilePermission("*", "read");
44         readInFile = new FilePermission("aFile.file", "read");
45     }
46
47     /**
48      * @tests java.io.FilePermission#FilePermission(java.lang.String,
49      *        java.lang.String)
50      */
51     @TestTargetNew(
52         level = TestLevel.COMPLETE,
53         notes = "Verifies FilePermission(java.lang.String, java.lang.String) constructor.",
54         method = "FilePermission",
55         args = {java.lang.String.class, java.lang.String.class}
56     )
57     public void test_ConstructorLjava_lang_StringLjava_lang_String() {
58         // Test for method java.io.FilePermission(java.lang.String,
59         // java.lang.String)
60         assertTrue("Used to test", true);
61         FilePermission constructFile = new FilePermission("test constructor",
62                 "write");
63         assertEquals("action given to the constructor did not correspond - constructor failed",
64                 "write", constructFile.getActions());
65         assertEquals(
66                 "name given to the constructor did not correspond - constructor failed",
67                 "test constructor", constructFile.getName());
68
69         // Regression test for HARMONY-1050
70         try {
71             new FilePermission(null, "drink");
72             fail("Expected IAE");
73         } catch (IllegalArgumentException e) {
74             // Expected
75         }
76
77         try {
78             new FilePermission(null, "read");
79             fail("Expected NPE");
80         } catch (NullPointerException e) {
81             // Expected
82         }
83
84         try {
85             new FilePermission(null, null);
86             fail("Expected IAE");
87         } catch (IllegalArgumentException e) {
88             // Expected
89         }
90     }
91
92     /**
93      * @tests java.io.FilePermission#getActions()
94      */
95     @TestTargetNew(
96         level = TestLevel.COMPLETE,
97         notes = "Verifies getActions() method.",
98         method = "getActions",
99         args = {}
100     )
101     public void test_getActions() {
102         // Test for method java.lang.String java.io.FilePermission.getActions()
103         assertEquals("getActions should have returned only read", "read", readAllFiles
104                 .getActions());
105         assertEquals("getActions should have returned all actions", "read,write,execute,delete", allInCurrent
106                 .getActions());
107     }
108
109     /**
110      * @tests java.io.FilePermission#equals(java.lang.Object)
111      */
112     @TestTargetNew(
113         level = TestLevel.COMPLETE,
114         notes = "Verifies equals(java.lang.Object) method.",
115         method = "equals",
116         args = {java.lang.Object.class}
117     )
118     public void test_equalsLjava_lang_Object() {
119         // test for method java.io.FilePermission.equals()
120         assertTrue(
121                 "returned false when two instance of FilePermission is equal",
122                 readAllFiles.equals(alsoReadAllFiles));
123         assertTrue(
124                 "returned true when two instance    of FilePermission is not equal",
125                 !(readInCurrent.equals(readInFile)));
126     }
127
128     /**
129      * @tests java.io.FilePermission#implies(java.security.Permission)
130      */
131     @TestTargetNew(
132         level = TestLevel.COMPLETE,
133         notes = "Verifies implies(java.security.Permission) method.",
134         method = "implies",
135         args = {java.security.Permission.class}
136     )
137     public void test_impliesLjava_security_Permission() {
138         // Test for method boolean
139         // java.io.FilePermission.implies(java.security.Permission)
140         assertTrue("Returned true for non-subset of actions", !readAllFiles
141                 .implies(allInCurrent));
142         assertTrue("Returned true for non-subset of files", !allInCurrent
143                 .implies(readAllFiles));
144         assertTrue("Returned false for subset of actions", allInCurrent
145                 .implies(readInCurrent));
146         assertTrue("Returned false for subset of files", readAllFiles
147                 .implies(readInCurrent));
148         assertTrue("Returned false for subset of files and actions",
149                 allInCurrent.implies(readInFile));
150         assertTrue("Returned false for equal FilePermissions", readAllFiles
151                 .implies(alsoReadAllFiles));
152
153         FilePermission fp3 = new FilePermission("/bob/*".replace('/',
154                 File.separatorChar), "read,write");
155         FilePermission fp4 = new FilePermission("/bob/".replace('/',
156                 File.separatorChar), "write");
157         assertTrue("returned true for same dir using * and not *", !fp3
158                 .implies(fp4));
159         FilePermission fp5 = new FilePermission("/bob/file".replace('/',
160                 File.separatorChar), "write");
161         assertTrue("returned false for same dir using * and file", fp3
162                 .implies(fp5));
163
164         FilePermission fp6 = new FilePermission("/bob/".replace('/',
165                 File.separatorChar), "read,write");
166         FilePermission fp7 = new FilePermission("/bob/*".replace('/',
167                 File.separatorChar), "write");
168         assertTrue("returned false for same dir using not * and *", !fp6
169                 .implies(fp7));
170         assertTrue("returned false for same subdir", fp6.implies(fp4));
171
172         FilePermission fp8 = new FilePermission("/".replace('/',
173                 File.separatorChar), "read,write");
174         FilePermission fp9 = new FilePermission("/".replace('/',
175                 File.separatorChar), "write");
176         assertTrue("returned false for same dir", fp8.implies(fp9));
177
178         FilePermission fp10 = new FilePermission("/".replace('/',
179                 File.separatorChar), "read,write");
180         FilePermission fp11 = new FilePermission("/".replace('/',
181                 File.separatorChar), "write");
182         assertTrue("returned false for same dir", fp10.implies(fp11));
183
184         FilePermission fp12 = new FilePermission("/*".replace('/',
185                 File.separatorChar), "read,write");
186         assertTrue("returned false for same dir using * and dir", !fp12
187                 .implies(fp10));
188     }
189
190     /**
191      * @tests java.io.FilePermission#newPermissionCollection()
192      */
193     @TestTargetNew(
194         level = TestLevel.COMPLETE,
195         notes = "Verifies newPermissionCollection() method.",
196         method = "newPermissionCollection",
197         args = {}
198     )
199     public void test_newPermissionCollection() {
200         // test for method java.io.FilePermission.newPermissionCollection
201         char s = File.separatorChar;
202         FilePermission perm[] = new FilePermission[4];
203         perm[0] = readAllFiles;
204         perm[1] = allInCurrent;
205         perm[2] = new FilePermission(s + "tmp" + s + "test" + s + "*",
206                 "read,write");
207         perm[3] = new FilePermission(s + "tmp" + s + "test" + s
208                 + "collection.file", "read");
209
210         PermissionCollection collect = perm[0].newPermissionCollection();
211         for (int i = 0; i < perm.length; i++) {
212             collect.add(perm[i]);
213         }
214         assertTrue("returned false for subset of files", collect
215                 .implies(new FilePermission("*", "write")));
216         assertTrue("returned false for subset of name and action", collect
217                 .implies(new FilePermission(s + "tmp", "read")));
218         assertTrue("returned true for non subset of file and action", collect
219                 .implies(readInFile));
220
221         FilePermission fp1 = new FilePermission("/tmp/-".replace('/',
222                 File.separatorChar), "read");
223         PermissionCollection fpc = fp1.newPermissionCollection();
224         fpc.add(fp1);
225         fpc.add(new FilePermission("/tmp/scratch/foo/*".replace('/',
226                 File.separatorChar), "write"));
227         FilePermission fp2 = new FilePermission("/tmp/scratch/foo/file"
228                 .replace('/', File.separatorChar), "read,write");
229         assertTrue("collection does not collate", fpc.implies(fp2));
230     }
231
232     /**
233      * @tests java.io.FilePermission#hashCode()
234      */
235     @TestTargetNew(
236         level = TestLevel.COMPLETE,
237         notes = "Verifies hashCode() method.",
238         method = "hashCode",
239         args = {}
240     )
241     public void test_hashCode() {
242         // test method java.io.FilePermission.hasCode()
243         assertTrue(
244                 "two equal filePermission instances returned different hashCode",
245                 readAllFiles.hashCode() == alsoReadAllFiles.hashCode());
246         assertTrue(
247                 "two filePermission instances with same permission name returned same hashCode",
248                 readInCurrent.hashCode() != allInCurrent.hashCode());
249
250     }
251 }