|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 8056179 |
|
27 * @summary Unit test for FilePermissionCollection subclass |
|
28 */ |
|
29 |
|
30 import java.io.FilePermission; |
|
31 import java.security.Permission; |
|
32 import java.security.PermissionCollection; |
|
33 import java.security.SecurityPermission; |
|
34 import java.util.Enumeration; |
|
35 |
|
36 public class FilePermissionCollection { |
|
37 |
|
38 public static void main(String[] args) throws Exception { |
|
39 |
|
40 int testFail = 0; |
|
41 |
|
42 FilePermission perm = new FilePermission("/tmp/foo", "read"); |
|
43 PermissionCollection perms = perm.newPermissionCollection(); |
|
44 |
|
45 // test 1 |
|
46 System.out.println |
|
47 ("test 1: add throws IllegalArgExc for wrong perm type"); |
|
48 try { |
|
49 perms.add(new SecurityPermission("createAccessControlContext")); |
|
50 System.err.println("Expected IllegalArgumentException"); |
|
51 testFail++; |
|
52 } catch (IllegalArgumentException iae) {} |
|
53 |
|
54 // test 2 |
|
55 System.out.println("test 2: implies returns false for wrong perm type"); |
|
56 if (perms.implies(new SecurityPermission("getPolicy"))) { |
|
57 System.err.println("Expected false, returned true"); |
|
58 testFail++; |
|
59 } |
|
60 |
|
61 // test 3 |
|
62 System.out.println("test 3: implies returns true for match on " + |
|
63 "name and action"); |
|
64 perms.add(new FilePermission("/tmp/foo", "read")); |
|
65 if (!perms.implies(new FilePermission("/tmp/foo", "read"))) { |
|
66 System.err.println("Expected true, returned false"); |
|
67 testFail++; |
|
68 } |
|
69 |
|
70 // test 4 |
|
71 System.out.println("test 4: implies returns false for match on " + |
|
72 "name but not action"); |
|
73 if (perms.implies(new FilePermission("/tmp/foo", "write"))) { |
|
74 System.err.println("Expected false, returned true"); |
|
75 testFail++; |
|
76 } |
|
77 |
|
78 // test 5 |
|
79 System.out.println("test 5: implies returns true for match on " + |
|
80 "name and subset of actions"); |
|
81 perms.add(new FilePermission("/tmp/bar", "read, write")); |
|
82 if (!perms.implies(new FilePermission("/tmp/bar", "write"))) { |
|
83 System.err.println("Expected true, returned false"); |
|
84 testFail++; |
|
85 } |
|
86 |
|
87 // test 6 |
|
88 System.out.println("test 6: implies returns true for aggregate " + |
|
89 "match on name and action"); |
|
90 perms.add(new FilePermission("/tmp/baz", "read")); |
|
91 perms.add(new FilePermission("/tmp/baz", "write")); |
|
92 if (!perms.implies(new FilePermission("/tmp/baz", "read"))) { |
|
93 System.err.println("Expected true, returned false"); |
|
94 testFail++; |
|
95 } |
|
96 if (!perms.implies(new FilePermission("/tmp/baz", "write,read"))) { |
|
97 System.err.println("Expected true, returned false"); |
|
98 testFail++; |
|
99 } |
|
100 |
|
101 // test 7 |
|
102 System.out.println("test 7: implies returns true for wildcard " + |
|
103 "and match on action"); |
|
104 perms.add(new FilePermission("/usr/tmp/*", "read")); |
|
105 if (!perms.implies(new FilePermission("/usr/tmp/foo", "read"))) { |
|
106 System.err.println("Expected true, returned false"); |
|
107 testFail++; |
|
108 } |
|
109 |
|
110 // test 8 |
|
111 System.out.println |
|
112 ("test 8: implies returns false for non-match on wildcard"); |
|
113 if (perms.implies(new FilePermission("/usr/tmp/bar/foo", "read"))) { |
|
114 System.err.println("Expected false, returned true"); |
|
115 testFail++; |
|
116 } |
|
117 |
|
118 // test 9 |
|
119 System.out.println |
|
120 ("test 9: implies returns true for deep wildcard match"); |
|
121 perms.add(new FilePermission("/usr/tmp/-", "read")); |
|
122 if (!perms.implies(new FilePermission("/usr/tmp/bar/foo", "read"))) { |
|
123 System.err.println("Expected true, returned false"); |
|
124 testFail++; |
|
125 } |
|
126 |
|
127 // test 10 |
|
128 System.out.println("test 10: implies returns true for relative match"); |
|
129 perms.add(new FilePermission(".", "read")); |
|
130 if (!perms.implies(new FilePermission(System.getProperty("user.dir"), |
|
131 "read"))) { |
|
132 System.err.println("Expected true, returned false"); |
|
133 testFail++; |
|
134 } |
|
135 |
|
136 // test 11 |
|
137 System.out.println("test 11: implies returns true for all " + |
|
138 "wildcard and match on action"); |
|
139 perms.add(new FilePermission("<<ALL FILES>>", "read")); |
|
140 if (!perms.implies(new FilePermission("/tmp/foobar", "read"))) { |
|
141 System.err.println("Expected true, returned false"); |
|
142 testFail++; |
|
143 } |
|
144 |
|
145 // test 12 |
|
146 System.out.println("test 12: implies returns false for wildcard " + |
|
147 "and non-match on action"); |
|
148 if (perms.implies(new FilePermission("/tmp/foobar", "write"))) { |
|
149 System.err.println("Expected false, returned true"); |
|
150 testFail++; |
|
151 } |
|
152 |
|
153 // test 13 |
|
154 System.out.println("test 13: elements returns correct number of perms"); |
|
155 int numPerms = 0; |
|
156 Enumeration<Permission> e = perms.elements(); |
|
157 while (e.hasMoreElements()) { |
|
158 numPerms++; |
|
159 System.out.println(e.nextElement()); |
|
160 } |
|
161 // the two "/tmp/baz" entries were combined into one |
|
162 if (numPerms != 7) { |
|
163 System.err.println("Expected 7, got " + numPerms); |
|
164 testFail++; |
|
165 } |
|
166 |
|
167 if (testFail > 0) { |
|
168 throw new Exception(testFail + " test(s) failed"); |
|
169 } |
|
170 } |
|
171 } |