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 java.security;
022:
023: import java.util.*;
024:
025: import junit.framework.TestCase;
026:
027: /**
028: * Tests for <code>UnresolvedPermissionCollection</code> class fields and methods
029: *
030: */
031:
032: public class UnresolvedPermissionCollection_ImplTest extends TestCase {
033:
034: /**
035: * Can add any number of UnresolvedPermission instances, but no any other permissions.
036: * Cannot add if collection is read-only.
037: */
038: public void testAdd() {
039: PermissionCollection pc = new UnresolvedPermissionCollection();
040: Permission sp = new SecurityPermission("abc");
041: Permission up1 = new UnresolvedPermission("131234", null, null,
042: null);
043: Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
044: "xcv456", "26r ytf",
045: new java.security.cert.Certificate[0]);
046:
047: try {
048: pc.add(sp);
049: fail("Should not add non-UnresolvedPermission");
050: } catch (IllegalArgumentException ok) {
051: }
052:
053: pc.add(up1);
054: pc.add(up1);
055: pc.add(up2);
056:
057: pc.setReadOnly();
058: try {
059: pc.add(up1);
060: fail("read-only flag is ignored");
061: } catch (SecurityException ok) {
062: }
063: }
064:
065: /** This collection never implies any permission. */
066: public void testImplies() {
067: Permission ap = new AllPermission();
068: Permission up = new UnresolvedPermission("131234", null, null,
069: null);
070: PermissionCollection pc = up.newPermissionCollection();
071:
072: assertFalse(pc.implies(ap));
073: assertFalse(pc.implies(up));
074:
075: //pc.add(up);
076: //assertFalse(pc.implies(up));
077: }
078:
079: /**
080: * Should return non-null empty enumeration for empty collection.
081: * For non-empty collection, should always return enumeration over unique elements.
082: */
083: public void testElements() {
084: PermissionCollection pc = new UnresolvedPermissionCollection();
085: Permission up1 = new UnresolvedPermission("131234", null, null,
086: null);
087: Permission up2 = new UnresolvedPermission("131234", "ui23rjh",
088: null, null);
089: Permission up3 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
090: "xcv456", "26r ytf",
091: new java.security.cert.Certificate[0]);
092:
093: Enumeration en = pc.elements();
094: assertNotNull(en);
095: assertFalse(en.hasMoreElements());
096:
097: pc.add(up1);
098: en = pc.elements();
099: assertTrue(en.hasMoreElements());
100: assertTrue(up1.equals(en.nextElement()));
101: assertFalse(en.hasMoreElements());
102:
103: //no check for duplicate elements - this is too implementation specific.
104: /*pc.add(up1);
105: en = pc.elements();
106: assertTrue(en.hasMoreElements());
107: assertTrue(up1.equals(en.nextElement()));
108: assertFalse(en.hasMoreElements());*/
109:
110: pc.add(up2);
111: pc.add(up3);
112: en = pc.elements();
113: Collection els = new ArrayList();
114: while (en.hasMoreElements()) {
115: els.add(en.nextElement());
116: }
117: assertEquals(3, els.size());
118: assertTrue(els.contains(up1) && els.contains(up2)
119: && els.contains(up3));
120: }
121:
122: /**
123: * For null collection passed, should behave correctly:
124: * <ul>
125: * <li>If nothing resolved, returns null and does not remove elements
126: * <li>If some permission resolved, returns proper collection and removes resolved elements
127: * </ul>
128: */
129: public void testResolveCollection() {
130: UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
131: Permission up = new UnresolvedPermission(
132: "java.security.AllPermission", "xcv456", "26r ytf",
133: new java.security.cert.Certificate[0]);
134: Permission ap = new AllPermission();
135: Permission bp = new BasicPermission("sfwertsdg") {
136: };
137:
138: PermissionCollection resolved = upc.resolveCollection(ap, null);
139: assertNull(resolved);
140:
141: upc.add(up);
142: resolved = upc.resolveCollection(bp, null);
143: assertNull(resolved);
144: assertTrue(up.equals(upc.elements().nextElement()));
145:
146: resolved = upc.resolveCollection(ap, null);
147: assertNotNull(resolved);
148: assertTrue(ap.equals(resolved.elements().nextElement()));
149: assertFalse(
150: "resolved permission should be removed from unresolved collection",
151: upc.elements().hasMoreElements());
152: }
153:
154: /**
155: * For real collection passed, should behave correctly:
156: * <ul>
157: * <li>If nothing resolved, returns the collection and does not remove elements
158: * <li>If some permission resolved, returns the collection and removes resolved elements
159: * </ul>
160: */
161: public void testResolveCollectionReturnedCollection() {
162: UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
163: Permission up3 = new UnresolvedPermission(
164: "java.security.AllPermission", "xcv456", null, null);
165: Permission ap = new AllPermission();
166: PermissionCollection apc = new AllPermissionCollection();
167:
168: PermissionCollection resolved = upc.resolveCollection(ap, apc);
169: assertSame(
170: "should return the passed collection if it is not null",
171: apc, resolved);
172: // retest the same for case of actually resolved permission
173: upc.add(up3);
174: resolved = upc.resolveCollection(ap, apc);
175: assertSame(
176: "should return the passed collection if it is not null",
177: apc, resolved);
178: }
179:
180: /**
181: * Test for case when some permissions of the expected type were not resolved for some reason,
182: * while others were resolved. Returned collection should contain resolved permissions only,
183: * and the unresolved collection should retain unresolved ones only.
184: */
185: public void testResolveCollectionPartial() {
186: UnresolvedPermissionCollection upc = new UnresolvedPermissionCollection();
187: String name = "ui23rjh";
188: Permission up1 = new UnresolvedPermission(
189: "java.security.SecurityPermission", null, null, null);
190: Permission up2 = new UnresolvedPermission(
191: "java.security.SecurityPermission", name, null, null);
192: Permission sp = new SecurityPermission(name);
193:
194: upc.add(up1);
195: upc.add(up2);
196: PermissionCollection resolved = upc.resolveCollection(
197: new SecurityPermission("34po5ijh"), null);
198: assertNotNull(resolved);
199: Enumeration els = resolved.elements();
200: assertTrue(sp.equals(els.nextElement()));
201: assertFalse(els.hasMoreElements());
202: els = upc.elements();
203: assertTrue(
204: "resolved permission should be removed from unresolved collection",
205: up1.equals(els.nextElement()));
206: assertFalse(els.hasMoreElements());
207: }
208: }
|