|
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 PropertyPermissionCollection subclass |
|
28 */ |
|
29 |
|
30 import java.security.Permission; |
|
31 import java.security.PermissionCollection; |
|
32 import java.security.SecurityPermission; |
|
33 import java.util.Enumeration; |
|
34 import java.util.PropertyPermission; |
|
35 |
|
36 public class PropertyPermissionCollection { |
|
37 |
|
38 public static void main(String[] args) throws Exception { |
|
39 |
|
40 int testFail = 0; |
|
41 |
|
42 PropertyPermission perm = new PropertyPermission("user.home", "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 |
|
63 ("test 3: implies returns true for match on name and action"); |
|
64 perms.add(new PropertyPermission("user.home", "read")); |
|
65 if (!perms.implies(new PropertyPermission("user.home", "read"))) { |
|
66 System.err.println("Expected true, returned false"); |
|
67 testFail++; |
|
68 } |
|
69 |
|
70 // test 4 |
|
71 System.out.println |
|
72 ("test 4: implies returns false for match on name but not action"); |
|
73 if (perms.implies(new PropertyPermission("user.home", "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 " + |
|
80 "on name and subset of actions"); |
|
81 perms.add(new PropertyPermission("java.home", "read, write")); |
|
82 if (!perms.implies(new PropertyPermission("java.home", "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 PropertyPermission("user.name", "read")); |
|
91 perms.add(new PropertyPermission("user.name", "write")); |
|
92 if (!perms.implies(new PropertyPermission("user.name", "read"))) { |
|
93 System.err.println("Expected true, returned false"); |
|
94 testFail++; |
|
95 } |
|
96 if (!perms.implies(new PropertyPermission("user.name", "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 PropertyPermission("foo.*", "read")); |
|
105 if (!perms.implies(new PropertyPermission("foo.bar", "read"))) { |
|
106 System.err.println("Expected true, returned false"); |
|
107 testFail++; |
|
108 } |
|
109 |
|
110 // test 8 |
|
111 System.out.println("test 8: implies returns true for deep " + |
|
112 "wildcard and match on action"); |
|
113 if (!perms.implies(new PropertyPermission("foo.bar.baz", "read"))) { |
|
114 System.err.println("Expected true, returned false"); |
|
115 testFail++; |
|
116 } |
|
117 |
|
118 // test 8 |
|
119 System.out.println |
|
120 ("test 8: implies returns false for invalid wildcard"); |
|
121 perms.add(new PropertyPermission("baz*", "read")); |
|
122 if (perms.implies(new PropertyPermission("baz.foo", "read"))) { |
|
123 System.err.println("Expected false, returned true"); |
|
124 testFail++; |
|
125 } |
|
126 |
|
127 // test 9 |
|
128 System.out.println("test 9: implies returns true for all " + |
|
129 "wildcard and match on action"); |
|
130 perms.add(new PropertyPermission("*", "read")); |
|
131 if (!perms.implies(new PropertyPermission("java.version", "read"))) { |
|
132 System.err.println("Expected true, returned false"); |
|
133 testFail++; |
|
134 } |
|
135 |
|
136 // test 10 |
|
137 System.out.println("test 10: implies returns false for wildcard " + |
|
138 "and non-match on action"); |
|
139 if (perms.implies(new PropertyPermission("java.version", "write"))) { |
|
140 System.err.println("Expected false, returned true"); |
|
141 testFail++; |
|
142 } |
|
143 |
|
144 // test 11 |
|
145 System.out.println("test 11: elements returns correct number of perms"); |
|
146 int numPerms = 0; |
|
147 Enumeration<Permission> e = perms.elements(); |
|
148 while (e.hasMoreElements()) { |
|
149 numPerms++; |
|
150 System.out.println(e.nextElement()); |
|
151 } |
|
152 // the 2 user.name permissions added were combined into one |
|
153 if (numPerms != 6) { |
|
154 System.err.println("Expected 6, got " + numPerms); |
|
155 testFail++; |
|
156 } |
|
157 |
|
158 if (testFail > 0) { |
|
159 throw new Exception(testFail + " test(s) failed"); |
|
160 } |
|
161 } |
|
162 } |