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.io.ByteArrayInputStream;
024: import java.io.ByteArrayOutputStream;
025: import java.io.ObjectInputStream;
026: import java.io.ObjectOutputStream;
027: import java.lang.reflect.Field;
028: import java.util.ArrayList;
029: import java.util.Arrays;
030: import java.util.Collection;
031: import java.util.Enumeration;
032:
033: import junit.framework.TestCase;
034:
035: /**
036: * Tests for <code>BasicPermissionCollection</code>
037: *
038: */
039:
040: public class BasicPermissionCollection_ImplTest extends TestCase {
041:
042: /**
043: * Can add only BasicPermissions of the same type as the first added. Cannot
044: * add if collection is read-only.
045: */
046: public void testAdd() {
047: PermissionCollection pc = new BasicPermissionCollection();
048: Permission ap = new AllPermission();
049: Permission sp1 = new SecurityPermission("a.b.c");
050: Permission sp2 = new SecurityPermission("a.b.*");
051: try {
052: pc.add(ap);
053: fail("Should not add non-BasicPermission");
054: } catch (IllegalArgumentException ok) {
055: }
056: pc.add(sp1);
057: pc.add(sp2);
058: try {
059: pc.add(new BasicPermission("123") {
060: });
061: fail("Should not add BasicPermission of different type");
062: } catch (IllegalArgumentException ok) {
063: }
064:
065: pc.setReadOnly();
066: try {
067: pc.add(sp1);
068: fail("read-only flag is ignored");
069: } catch (SecurityException ok) {
070: }
071: }
072:
073: /**
074: * Empty collection implies nothing. Non-empty collection should imply all
075: * contained permissions, and should consider contained wildcards (if any).
076: */
077: public void testImplies() {
078: PermissionCollection pc = new BasicPermissionCollection();
079: Permission ap = new AllPermission();
080: Permission up = new UnresolvedPermission("safds", null, null,
081: null);
082: Permission sp1 = new SecurityPermission("a.b.c");
083: Permission sp11 = new SecurityPermission("a.b.");
084: Permission sp2 = new SecurityPermission("a.b.*");
085: Permission sp3 = new SecurityPermission("a.*");
086: Permission sp4 = new SecurityPermission("*");
087:
088: assertFalse(pc.implies(ap));
089: assertFalse(pc.implies(up));
090: assertFalse(pc.implies(sp1));
091:
092: pc.add(sp3);
093: assertTrue(pc.implies(sp2));
094: assertTrue(pc.implies(sp1));
095: assertTrue(pc.implies(sp11));
096: assertTrue(pc.implies(sp3));
097: assertFalse(pc.implies(sp4));
098:
099: pc.add(sp4);
100: assertTrue(pc.implies(sp4));
101: assertFalse(pc.implies(ap));
102: assertFalse(pc.implies(up));
103: assertTrue(pc.implies(new SecurityPermission(
104: "skjdnkwje wefkwjef")));
105: }
106:
107: /**
108: * Should return non-null empty enumeration for empty collection. For
109: * non-empty collection, should always return enumeration over unique
110: * elements.
111: */
112: public void testElements() {
113: PermissionCollection pc = new BasicPermissionCollection();
114: Permission sp1 = new SecurityPermission("a.b.c");
115: Permission sp2 = new SecurityPermission("a.b.*");
116: Permission sp3 = new SecurityPermission("*");
117: Enumeration en = pc.elements();
118: assertNotNull(en);
119: assertFalse(en.hasMoreElements());
120:
121: try {
122: pc.add(null);
123: fail("should throw IllegalArgumentException");
124: } catch (IllegalArgumentException e) {
125: }
126:
127: pc.add(sp1);
128: en = pc.elements();
129: assertTrue(en.hasMoreElements());
130: assertTrue(sp1.equals(en.nextElement()));
131: assertFalse(en.hasMoreElements());
132:
133: pc.add(sp1);
134: en = pc.elements();
135: assertTrue(en.hasMoreElements());
136: assertTrue(sp1.equals(en.nextElement()));
137: assertFalse(en.hasMoreElements());
138:
139: pc.add(sp3);
140: pc.add(sp2);
141: en = pc.elements();
142: Collection els = new ArrayList();
143: while (en.hasMoreElements()) {
144: els.add(en.nextElement());
145: }
146: assertEquals(3, els.size());
147: assertTrue(els.containsAll(Arrays.asList(new Permission[] {
148: sp1, sp2, sp3 })));
149: }
150:
151: /**
152: * test on deserialization of incorrect object
153: */
154: public void testNegDeserialization_01() throws Exception {
155:
156: SecurityPermission sp = new SecurityPermission("a.b.c");
157: BasicPermissionCollection pc = new BasicPermissionCollection();
158: pc.add(sp);
159: setField(pc, "permClass", BasicPermission.class);
160:
161: ByteArrayOutputStream baos = new ByteArrayOutputStream();
162: ObjectOutputStream oos = new ObjectOutputStream(baos);
163: oos.writeObject(pc);
164: oos.flush();
165:
166: ObjectInputStream in = new ObjectInputStream(
167: new ByteArrayInputStream(baos.toByteArray()));
168: try {
169: in.readObject();
170: fail("should throw InvalidObjectException");
171: } catch (java.io.InvalidObjectException e) {
172: } finally {
173: oos.close();
174: in.close();
175: }
176: }
177:
178: /**
179: * test on deserialization of incorrect object
180: */
181: public void testNegDeserialization_02() throws Exception {
182:
183: SecurityPermission sp = new SecurityPermission("a.b.c");
184: BasicPermissionCollection pc = new BasicPermissionCollection();
185: pc.add(sp);
186: setField(pc, "allEnabled", new Boolean(true));
187:
188: ByteArrayOutputStream baos = new ByteArrayOutputStream();
189: ObjectOutputStream oos = new ObjectOutputStream(baos);
190: oos.writeObject(pc);
191: oos.flush();
192:
193: ObjectInputStream in = new ObjectInputStream(
194: new ByteArrayInputStream(baos.toByteArray()));
195: try {
196: in.readObject();
197: fail("should throw InvalidObjectException");
198: } catch (java.io.InvalidObjectException e) {
199: } finally {
200: oos.close();
201: in.close();
202: }
203: }
204:
205: /**
206: * setup a private field
207: */
208: private void setField(Object obj, String name, Object newval)
209: throws Exception {
210: Field f = obj.getClass().getDeclaredField(name);
211: f.setAccessible(true);
212: f.set(obj, newval);
213: }
214: }
|