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