001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexey V. Varlamov
020: * @version $Revision$
021: */package org.apache.harmony.security.tests;
022:
023: import java.net.URL;
024: import java.security.cert.Certificate;
025: import java.security.AllPermission;
026: import java.security.CodeSource;
027: import java.security.Permission;
028: import java.security.Principal;
029: import java.security.SecurityPermission;
030: import java.util.ArrayList;
031: import java.util.Arrays;
032: import java.util.Collection;
033: import java.util.HashSet;
034:
035: import org.apache.harmony.security.PolicyEntry;
036: import org.apache.harmony.security.UnresolvedPrincipal;
037: import junit.framework.TestCase;
038:
039: /**
040: * TODO Put your class description here
041: *
042: */
043:
044: public class PolicyEntryTest extends TestCase {
045:
046: /**
047: * Tests constructor and accessors of PolicyEntry
048: */
049: public void testCtor() {
050: PolicyEntry pe = new PolicyEntry(null, null, null);
051: assertTrue(pe.isVoid());
052: assertNull(pe.getPermissions());
053:
054: pe = new PolicyEntry(
055: new CodeSource(null, (Certificate[]) null),
056: new ArrayList(), new ArrayList());
057: assertTrue(pe.isVoid());
058: assertNull(pe.getPermissions());
059:
060: Collection perms = Arrays.asList(new Permission[] {
061: new SecurityPermission("dsfg"), new AllPermission() });
062: pe = new PolicyEntry(null, null, perms);
063: assertFalse(pe.isVoid());
064: assertEquals(perms, new ArrayList(pe.getPermissions()));
065: }
066:
067: /**
068: * Null CodeSource of PolicyEntry implies any CodeSource; non-null
069: * CodeSource should delegate to its own imply() functionality
070: */
071: public void testImpliesCodeSource() throws Exception {
072: CodeSource cs0 = new CodeSource(null, (Certificate[]) null);
073:
074: CodeSource cs10 = new CodeSource(new URL("file:"),
075: (Certificate[]) null);
076: CodeSource cs11 = new CodeSource(new URL("file:/"),
077: (Certificate[]) null);
078: CodeSource cs12 = new CodeSource(new URL("file://"),
079: (Certificate[]) null);
080: CodeSource cs13 = new CodeSource(new URL("file:///"),
081: (Certificate[]) null);
082:
083: CodeSource cs20 = new CodeSource(new URL("file:*"),
084: (Certificate[]) null);
085: CodeSource cs21 = new CodeSource(new URL("file:/*"),
086: (Certificate[]) null);
087: CodeSource cs22 = new CodeSource(new URL("file://*"),
088: (Certificate[]) null);
089: CodeSource cs23 = new CodeSource(new URL("file:///*"),
090: (Certificate[]) null);
091:
092: CodeSource cs30 = new CodeSource(new URL("file:-"),
093: (Certificate[]) null);
094: CodeSource cs31 = new CodeSource(new URL("file:/-"),
095: (Certificate[]) null);
096: CodeSource cs32 = new CodeSource(new URL("file://-"),
097: (Certificate[]) null);
098: CodeSource cs33 = new CodeSource(new URL("file:///-"),
099: (Certificate[]) null);
100:
101: PolicyEntry pe0 = new PolicyEntry(null, null, null);
102:
103: PolicyEntry pe10 = new PolicyEntry(cs10, null, null);
104: PolicyEntry pe11 = new PolicyEntry(cs11, null, null);
105: PolicyEntry pe12 = new PolicyEntry(cs12, null, null);
106: PolicyEntry pe13 = new PolicyEntry(cs13, null, null);
107:
108: PolicyEntry pe20 = new PolicyEntry(cs20, null, null);
109: PolicyEntry pe21 = new PolicyEntry(cs21, null, null);
110: PolicyEntry pe22 = new PolicyEntry(cs22, null, null);
111: PolicyEntry pe23 = new PolicyEntry(cs23, null, null);
112:
113: PolicyEntry pe30 = new PolicyEntry(cs30, null, null);
114: PolicyEntry pe31 = new PolicyEntry(cs31, null, null);
115: PolicyEntry pe32 = new PolicyEntry(cs32, null, null);
116: PolicyEntry pe33 = new PolicyEntry(cs33, null, null);
117:
118: assertTrue(pe0.impliesCodeSource(null));
119: assertTrue(pe0.impliesCodeSource(cs10));
120: assertTrue(pe0.impliesCodeSource(cs11));
121: assertTrue(pe0.impliesCodeSource(cs12));
122: assertTrue(pe0.impliesCodeSource(cs13));
123: assertTrue(pe0.impliesCodeSource(cs20));
124: assertTrue(pe0.impliesCodeSource(cs21));
125: assertTrue(pe0.impliesCodeSource(cs22));
126: assertTrue(pe0.impliesCodeSource(cs23));
127: assertTrue(pe0.impliesCodeSource(cs30));
128: assertTrue(pe0.impliesCodeSource(cs31));
129: assertTrue(pe0.impliesCodeSource(cs32));
130: assertTrue(pe0.impliesCodeSource(cs33));
131:
132: assertFalse(pe10.impliesCodeSource(null));
133: assertTrue(pe10.impliesCodeSource(cs10));
134: assertFalse(pe10.impliesCodeSource(cs11));
135: assertTrue(pe10.impliesCodeSource(cs12));
136: assertFalse(pe10.impliesCodeSource(cs13));
137: assertTrue(pe10.impliesCodeSource(cs20));
138: assertFalse(pe10.impliesCodeSource(cs21));
139: assertFalse(pe10.impliesCodeSource(cs22));
140: assertFalse(pe10.impliesCodeSource(cs23));
141: assertTrue(pe10.impliesCodeSource(cs30));
142: assertFalse(pe10.impliesCodeSource(cs31));
143: assertFalse(pe10.impliesCodeSource(cs32));
144: assertFalse(pe10.impliesCodeSource(cs33));
145:
146: assertFalse(pe11.impliesCodeSource(null));
147: assertFalse(pe11.impliesCodeSource(cs10));
148: assertTrue(pe11.impliesCodeSource(cs11));
149: assertFalse(pe11.impliesCodeSource(cs12));
150: assertTrue(pe11.impliesCodeSource(cs13));
151: assertFalse(pe11.impliesCodeSource(cs20));
152: assertFalse(pe11.impliesCodeSource(cs21));
153: assertFalse(pe11.impliesCodeSource(cs22));
154: assertFalse(pe11.impliesCodeSource(cs23));
155: assertFalse(pe11.impliesCodeSource(cs30));
156: assertFalse(pe11.impliesCodeSource(cs31));
157: assertFalse(pe11.impliesCodeSource(cs32));
158: assertFalse(pe11.impliesCodeSource(cs33));
159:
160: assertFalse(pe12.impliesCodeSource(null));
161: assertTrue(pe12.impliesCodeSource(cs10));
162: assertFalse(pe12.impliesCodeSource(cs11));
163: assertTrue(pe12.impliesCodeSource(cs12));
164: assertFalse(pe12.impliesCodeSource(cs13));
165: assertTrue(pe12.impliesCodeSource(cs20));
166: assertFalse(pe12.impliesCodeSource(cs21));
167: assertFalse(pe12.impliesCodeSource(cs22));
168: assertFalse(pe12.impliesCodeSource(cs23));
169: assertTrue(pe12.impliesCodeSource(cs30));
170: assertFalse(pe12.impliesCodeSource(cs31));
171: assertFalse(pe12.impliesCodeSource(cs32));
172: assertFalse(pe12.impliesCodeSource(cs33));
173:
174: assertFalse(pe13.impliesCodeSource(null));
175: assertFalse(pe13.impliesCodeSource(cs10));
176: assertTrue(pe13.impliesCodeSource(cs11));
177: assertFalse(pe13.impliesCodeSource(cs12));
178: assertTrue(pe13.impliesCodeSource(cs13));
179: assertFalse(pe13.impliesCodeSource(cs20));
180: assertFalse(pe13.impliesCodeSource(cs21));
181: assertFalse(pe13.impliesCodeSource(cs22));
182: assertFalse(pe13.impliesCodeSource(cs23));
183: assertFalse(pe13.impliesCodeSource(cs30));
184: assertFalse(pe13.impliesCodeSource(cs31));
185: assertFalse(pe13.impliesCodeSource(cs32));
186: assertFalse(pe13.impliesCodeSource(cs33));
187:
188: assertFalse(pe20.impliesCodeSource(null));
189: assertTrue(pe20.impliesCodeSource(cs10));
190: assertFalse(pe20.impliesCodeSource(cs11));
191: assertTrue(pe20.impliesCodeSource(cs12));
192: assertFalse(pe20.impliesCodeSource(cs13));
193: assertTrue(pe20.impliesCodeSource(cs20));
194: assertFalse(pe20.impliesCodeSource(cs21));
195: assertFalse(pe20.impliesCodeSource(cs22));
196: assertFalse(pe20.impliesCodeSource(cs23));
197: assertTrue(pe20.impliesCodeSource(cs30));
198: assertFalse(pe20.impliesCodeSource(cs31));
199: assertFalse(pe20.impliesCodeSource(cs32));
200: assertFalse(pe20.impliesCodeSource(cs33));
201:
202: assertFalse(pe21.impliesCodeSource(null));
203: assertFalse(pe21.impliesCodeSource(cs10));
204: assertTrue(pe21.impliesCodeSource(cs11));
205: assertFalse(pe21.impliesCodeSource(cs12));
206: assertTrue(pe21.impliesCodeSource(cs13));
207: assertFalse(pe21.impliesCodeSource(cs20));
208: assertTrue(pe21.impliesCodeSource(cs21));
209: assertFalse(pe21.impliesCodeSource(cs22));
210: assertTrue(pe21.impliesCodeSource(cs23));
211: assertFalse(pe21.impliesCodeSource(cs30));
212: assertTrue(pe21.impliesCodeSource(cs31));
213: assertFalse(pe21.impliesCodeSource(cs32));
214: assertTrue(pe21.impliesCodeSource(cs33));
215:
216: assertFalse(pe22.impliesCodeSource(null));
217: assertFalse(pe22.impliesCodeSource(cs10));
218: // assertFalse(pe22.impliesCodeSource(cs11));
219: assertFalse(pe22.impliesCodeSource(cs12));
220: // assertFalse(pe22.impliesCodeSource(cs13));
221: assertFalse(pe22.impliesCodeSource(cs20));
222: assertFalse(pe22.impliesCodeSource(cs21));
223: assertTrue(pe22.impliesCodeSource(cs22));
224: assertFalse(pe22.impliesCodeSource(cs23));
225: assertFalse(pe22.impliesCodeSource(cs30));
226: assertFalse(pe22.impliesCodeSource(cs31));
227: assertTrue(pe22.impliesCodeSource(cs32));
228: assertFalse(pe22.impliesCodeSource(cs33));
229:
230: assertFalse(pe23.impliesCodeSource(null));
231: assertFalse(pe23.impliesCodeSource(cs10));
232: assertTrue(pe23.impliesCodeSource(cs11));
233: assertFalse(pe23.impliesCodeSource(cs12));
234: assertTrue(pe23.impliesCodeSource(cs13));
235: assertFalse(pe23.impliesCodeSource(cs20));
236: assertTrue(pe23.impliesCodeSource(cs21));
237: assertFalse(pe23.impliesCodeSource(cs22));
238: assertTrue(pe23.impliesCodeSource(cs23));
239: assertFalse(pe23.impliesCodeSource(cs30));
240: assertTrue(pe23.impliesCodeSource(cs31));
241: assertFalse(pe23.impliesCodeSource(cs32));
242: assertTrue(pe23.impliesCodeSource(cs33));
243:
244: assertFalse(pe30.impliesCodeSource(null));
245: assertTrue(pe30.impliesCodeSource(cs10));
246: assertFalse(pe30.impliesCodeSource(cs11));
247: assertTrue(pe30.impliesCodeSource(cs12));
248: assertFalse(pe30.impliesCodeSource(cs13));
249: assertTrue(pe30.impliesCodeSource(cs20));
250: assertFalse(pe30.impliesCodeSource(cs21));
251: assertFalse(pe30.impliesCodeSource(cs22));
252: assertFalse(pe30.impliesCodeSource(cs23));
253: assertTrue(pe30.impliesCodeSource(cs30));
254: assertFalse(pe30.impliesCodeSource(cs31));
255: assertFalse(pe30.impliesCodeSource(cs32));
256: assertFalse(pe30.impliesCodeSource(cs33));
257:
258: assertFalse(pe31.impliesCodeSource(null));
259: assertTrue(pe31.impliesCodeSource(cs10));
260: assertTrue(pe31.impliesCodeSource(cs11));
261: assertTrue(pe31.impliesCodeSource(cs12));
262: assertTrue(pe31.impliesCodeSource(cs13));
263: assertTrue(pe31.impliesCodeSource(cs20));
264: assertTrue(pe31.impliesCodeSource(cs21));
265: assertFalse(pe31.impliesCodeSource(cs22));
266: assertTrue(pe31.impliesCodeSource(cs23));
267: assertTrue(pe31.impliesCodeSource(cs30));
268: assertTrue(pe31.impliesCodeSource(cs31));
269: assertFalse(pe31.impliesCodeSource(cs32));
270: assertTrue(pe31.impliesCodeSource(cs33));
271:
272: assertFalse(pe32.impliesCodeSource(null));
273: assertFalse(pe32.impliesCodeSource(cs10));
274: assertFalse(pe32.impliesCodeSource(cs11));
275: assertFalse(pe32.impliesCodeSource(cs12));
276: assertFalse(pe32.impliesCodeSource(cs13));
277: assertFalse(pe32.impliesCodeSource(cs20));
278: assertFalse(pe32.impliesCodeSource(cs21));
279: assertFalse(pe32.impliesCodeSource(cs22));
280: assertFalse(pe32.impliesCodeSource(cs23));
281: assertFalse(pe32.impliesCodeSource(cs30));
282: assertFalse(pe32.impliesCodeSource(cs31));
283: assertTrue(pe32.impliesCodeSource(cs32));
284: assertFalse(pe32.impliesCodeSource(cs33));
285:
286: assertFalse(pe33.impliesCodeSource(null));
287: assertTrue(pe33.impliesCodeSource(cs10));
288: assertTrue(pe33.impliesCodeSource(cs11));
289: assertTrue(pe33.impliesCodeSource(cs12));
290: assertTrue(pe33.impliesCodeSource(cs13));
291: assertTrue(pe33.impliesCodeSource(cs20));
292: assertTrue(pe33.impliesCodeSource(cs21));
293: assertFalse(pe33.impliesCodeSource(cs22));
294: assertTrue(pe33.impliesCodeSource(cs23));
295: assertTrue(pe33.impliesCodeSource(cs30));
296: assertTrue(pe33.impliesCodeSource(cs31));
297: assertFalse(pe33.impliesCodeSource(cs32));
298: assertTrue(pe33.impliesCodeSource(cs33));
299: }
300:
301: /**
302: * Null or empty set of Principals of PolicyEntry implies any Principals;
303: * otherwise tested set must contain all Principals of PolicyEntry.
304: */
305: public void testImpliesPrincipals() {
306: PolicyEntry pe = new PolicyEntry(null, null, null);
307: Principal[] pp1 = new Principal[] {};
308: Principal[] pp2 = new Principal[] { new UnresolvedPrincipal(
309: "a.b.c", "XXX") };
310: Principal[] pp3 = new Principal[] {
311: new UnresolvedPrincipal("a.b.c", "YYY"),
312: new UnresolvedPrincipal("a.b.c", "XXX"),
313: new UnresolvedPrincipal("e.f.g", "ZZZ") };
314:
315: assertTrue(pe.impliesPrincipals(null));
316: assertTrue(pe.impliesPrincipals(pp1));
317:
318: pe = new PolicyEntry(null, new HashSet(), null);
319: assertTrue(pe.impliesPrincipals(pp3));
320:
321: pe = new PolicyEntry(null, Arrays.asList(pp2), null);
322: assertFalse(pe.impliesPrincipals(null));
323: assertFalse(pe.impliesPrincipals(pp1));
324: assertTrue(pe.impliesPrincipals(pp3));
325: }
326: }
|