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