FilePermissionTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
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
18package tests.api.java.io;
19
20import java.io.File;
21import java.io.FilePermission;
22import java.security.PermissionCollection;
23
24import dalvik.annotation.TestLevel;
25import dalvik.annotation.TestTargetClass;
26import dalvik.annotation.TestTargetNew;
27
28@TestTargetClass(FilePermission.class)
29public class FilePermissionTest extends junit.framework.TestCase {
30
31    FilePermission readAllFiles = new FilePermission("<<ALL FILES>>", "read");
32
33    FilePermission alsoReadAllFiles = new FilePermission("<<ALL FILES>>",
34            "read");
35
36    FilePermission allInCurrent = new FilePermission("*",
37            "read, write, execute,delete");
38
39    FilePermission readInCurrent = new FilePermission("*", "read");
40
41    FilePermission readInFile = new FilePermission("aFile.file", "read");
42
43    /**
44     * @tests java.io.FilePermission#FilePermission(java.lang.String,
45     *        java.lang.String)
46     */
47    @TestTargetNew(
48        level = TestLevel.COMPLETE,
49        notes = "Verifies FilePermission(java.lang.String, java.lang.String) constructor.",
50        method = "FilePermission",
51        args = {java.lang.String.class, java.lang.String.class}
52    )
53    public void test_ConstructorLjava_lang_StringLjava_lang_String() {
54        // Test for method java.io.FilePermission(java.lang.String,
55        // java.lang.String)
56        assertTrue("Used to test", true);
57        FilePermission constructFile = new FilePermission("test constructor",
58                "write");
59        assertEquals("action given to the constructor did not correspond - constructor failed",
60                "write", constructFile.getActions());
61        assertTrue(
62                "name given to the construcotr did not correspond - construcotr failed",
63                constructFile.getName() == "test constructor");
64
65        // Regression test for HARMONY-1050
66        try {
67            new FilePermission(null, "drink");
68            fail("Expected IAE");
69        } catch (IllegalArgumentException e) {
70            // Expected
71        }
72
73        try {
74            new FilePermission(null, "read");
75            fail("Expected NPE");
76        } catch (NullPointerException e) {
77            // Expected
78        }
79
80        try {
81            new FilePermission(null, null);
82            fail("Expected IAE");
83        } catch (IllegalArgumentException e) {
84            // Expected
85        }
86    }
87
88    /**
89     * @tests java.io.FilePermission#getActions()
90     */
91    @TestTargetNew(
92        level = TestLevel.COMPLETE,
93        notes = "Verifies getActions() method.",
94        method = "getActions",
95        args = {}
96    )
97    public void test_getActions() {
98        // Test for method java.lang.String java.io.FilePermission.getActions()
99        assertEquals("getActions should have returned only read", "read", readAllFiles
100                .getActions());
101        assertEquals("getActions should have returned all actions", "read,write,execute,delete", allInCurrent
102                .getActions());
103    }
104
105    /**
106     * @tests java.io.FilePermission#equals(java.lang.Object)
107     */
108    @TestTargetNew(
109        level = TestLevel.COMPLETE,
110        notes = "Verifies equals(java.lang.Object) method.",
111        method = "equals",
112        args = {java.lang.Object.class}
113    )
114    public void test_equalsLjava_lang_Object() {
115        // test for method java.io.FilePermission.equals()
116        assertTrue(
117                "returned false when two instance of FilePermission is equal",
118                readAllFiles.equals(alsoReadAllFiles));
119        assertTrue(
120                "returned true when two instance    of FilePermission is not equal",
121                !(readInCurrent.equals(readInFile)));
122    }
123
124    /**
125     * @tests java.io.FilePermission#implies(java.security.Permission)
126     */
127    @TestTargetNew(
128        level = TestLevel.COMPLETE,
129        notes = "Verifies implies(java.security.Permission) method.",
130        method = "implies",
131        args = {java.security.Permission.class}
132    )
133    public void test_impliesLjava_security_Permission() {
134        // Test for method boolean
135        // java.io.FilePermission.implies(java.security.Permission)
136        assertTrue("Returned true for non-subset of actions", !readAllFiles
137                .implies(allInCurrent));
138        assertTrue("Returned true for non-subset of files", !allInCurrent
139                .implies(readAllFiles));
140        assertTrue("Returned false for subset of actions", allInCurrent
141                .implies(readInCurrent));
142        assertTrue("Returned false for subset of files", readAllFiles
143                .implies(readInCurrent));
144        assertTrue("Returned false for subset of files and actions",
145                allInCurrent.implies(readInFile));
146        assertTrue("Returned false for equal FilePermissions", readAllFiles
147                .implies(alsoReadAllFiles));
148
149        FilePermission fp3 = new FilePermission("/bob/*".replace('/',
150                File.separatorChar), "read,write");
151        FilePermission fp4 = new FilePermission("/bob/".replace('/',
152                File.separatorChar), "write");
153        assertTrue("returned true for same dir using * and not *", !fp3
154                .implies(fp4));
155        FilePermission fp5 = new FilePermission("/bob/file".replace('/',
156                File.separatorChar), "write");
157        assertTrue("returned false for same dir using * and file", fp3
158                .implies(fp5));
159
160        FilePermission fp6 = new FilePermission("/bob/".replace('/',
161                File.separatorChar), "read,write");
162        FilePermission fp7 = new FilePermission("/bob/*".replace('/',
163                File.separatorChar), "write");
164        assertTrue("returned false for same dir using not * and *", !fp6
165                .implies(fp7));
166        assertTrue("returned false for same subdir", fp6.implies(fp4));
167
168        FilePermission fp8 = new FilePermission("/".replace('/',
169                File.separatorChar), "read,write");
170        FilePermission fp9 = new FilePermission("/".replace('/',
171                File.separatorChar), "write");
172        assertTrue("returned false for same dir", fp8.implies(fp9));
173
174        FilePermission fp10 = new FilePermission("/".replace('/',
175                File.separatorChar), "read,write");
176        FilePermission fp11 = new FilePermission("/".replace('/',
177                File.separatorChar), "write");
178        assertTrue("returned false for same dir", fp10.implies(fp11));
179
180        FilePermission fp12 = new FilePermission("/*".replace('/',
181                File.separatorChar), "read,write");
182        assertTrue("returned false for same dir using * and dir", !fp12
183                .implies(fp10));
184    }
185
186    /**
187     * @tests java.io.FilePermission#newPermissionCollection()
188     */
189    @TestTargetNew(
190        level = TestLevel.COMPLETE,
191        notes = "Verifies newPermissionCollection() method.",
192        method = "newPermissionCollection",
193        args = {}
194    )
195    public void test_newPermissionCollection() {
196        // test for method java.io.FilePermission.newPermissionCollection
197        char s = File.separatorChar;
198        FilePermission perm[] = new FilePermission[4];
199        perm[0] = readAllFiles;
200        perm[1] = allInCurrent;
201        perm[2] = new FilePermission(s + "tmp" + s + "test" + s + "*",
202                "read,write");
203        perm[3] = new FilePermission(s + "tmp" + s + "test" + s
204                + "collection.file", "read");
205
206        PermissionCollection collect = perm[0].newPermissionCollection();
207        for (int i = 0; i < perm.length; i++) {
208            collect.add(perm[i]);
209        }
210        assertTrue("returned false for subset of files", collect
211                .implies(new FilePermission("*", "write")));
212        assertTrue("returned false for subset of name and action", collect
213                .implies(new FilePermission(s + "tmp", "read")));
214        assertTrue("returned true for non subset of file and action", collect
215                .implies(readInFile));
216
217        FilePermission fp1 = new FilePermission("/tmp/-".replace('/',
218                File.separatorChar), "read");
219        PermissionCollection fpc = fp1.newPermissionCollection();
220        fpc.add(fp1);
221        fpc.add(new FilePermission("/tmp/scratch/foo/*".replace('/',
222                File.separatorChar), "write"));
223        FilePermission fp2 = new FilePermission("/tmp/scratch/foo/file"
224                .replace('/', File.separatorChar), "read,write");
225        assertTrue("collection does not collate", fpc.implies(fp2));
226    }
227
228    /**
229     * @tests java.io.FilePermission#hashCode()
230     */
231    @TestTargetNew(
232        level = TestLevel.COMPLETE,
233        notes = "Verifies hashCode() method.",
234        method = "hashCode",
235        args = {}
236    )
237    public void test_hashCode() {
238        // test method java.io.FilePermission.hasCode()
239        assertTrue(
240                "two equal filePermission instances returned different hashCode",
241                readAllFiles.hashCode() == alsoReadAllFiles.hashCode());
242        assertTrue(
243                "two filePermission instances with same permission name returned same hashCode",
244                readInCurrent.hashCode() != allInCurrent.hashCode());
245
246    }
247
248    /**
249     * Sets up the fixture, for example, open a network connection. This method
250     * is called before a test is executed.
251     */
252    protected void setUp() {
253    }
254
255    /**
256     * Tears down the fixture, for example, close a network connection. This
257     * method is called after a test is executed.
258     */
259    protected void tearDown() {
260    }
261}
262