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.java.security;
022:
023: import java.security.*;
024: import java.util.ArrayList;
025: import java.util.Arrays;
026: import java.util.Collection;
027: import java.util.Enumeration;
028: import java.util.NoSuchElementException;
029:
030: import junit.framework.TestCase;
031:
032: /**
033: * Tests for <code>Permissions</code>
034: *
035: */
036:
037: public class PermissionsTest extends TestCase {
038:
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(PermissionsTest.class);
041: }
042:
043: /**
044: * Can add any type of permissions. Cannot add if collection is read-only.
045: */
046: public void testAdd() {
047: Permissions ps = new Permissions();
048: Permission ap = new AllPermission();
049: Permission bp = new BasicPermission("jhb23jhg5") {
050: };
051: Permission sp0 = new SecurityPermission("abc");
052: Permission sp1 = new SecurityPermission("a.b.c");
053: Permission sp2 = new SecurityPermission("a.b.*");
054: Permission sp3 = new SecurityPermission("a.*");
055: Permission up1 = new UnresolvedPermission("131234", null, null,
056: null);
057: Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
058: "xcv456", "26r ytf",
059: new java.security.cert.Certificate[0]);
060: Permission[] arr = new Permission[] { up1, up2, ap, bp, sp0,
061: sp1, sp2, sp3, };
062: for (int i = 0; i < arr.length; i++) {
063: ps.add(arr[i]);
064: }
065:
066: //test add duplicate
067: ps.add(up1);
068: ps.add(sp0);
069:
070: ps.setReadOnly();
071: try {
072: ps.add(up1);
073: fail("read-only flag is ignored");
074: } catch (SecurityException ok) {
075: }
076: }
077:
078: /**
079: * Should return non-null empty enumeration for empty collection. For
080: * non-empty collection, should always return enumeration over unique
081: * elements.
082: */
083: public void testElements() {
084: Permissions ps = new Permissions();
085: Permission ap = new AllPermission();
086: Permission bp = new BasicPermission("jhb23jhg5") {
087:
088: public PermissionCollection newPermissionCollection() {
089: return null;
090: }
091: };
092: Permission sp = new SecurityPermission("abc");
093: Permission up1 = new UnresolvedPermission("131234", null, null,
094: null);
095: Permission up2 = new UnresolvedPermission("KUJKHVKJgyuygjhb",
096: "xcv456", "26r ytf",
097: new java.security.cert.Certificate[0]);
098:
099: Enumeration en = ps.elements();
100: assertNotNull(en);
101: assertFalse(en.hasMoreElements());
102:
103: ps.add(up1);
104: en = ps.elements();
105: assertTrue(en.hasMoreElements());
106: assertTrue(up1.equals(en.nextElement()));
107: assertFalse(en.hasMoreElements());
108:
109: ps.add(up1);
110: en = ps.elements();
111: assertTrue(en.hasMoreElements());
112: assertTrue(up1.equals(en.nextElement()));
113: //assertFalse(en.hasMoreElements());
114:
115: Permission[] arr = new Permission[] { ap, bp, sp, up1, up2 };
116: for (int i = 0; i < arr.length; i++) {
117: ps.add(arr[i]);
118: }
119: en = ps.elements();
120: Collection els = new ArrayList();
121: while (en.hasMoreElements()) {
122: els.add(en.nextElement());
123: }
124: //assertEquals(5, els.size());
125: assertTrue(els.containsAll(Arrays.asList(arr)));
126: }
127:
128: /**
129: * input parameter is null
130: */
131: public void testNull() {
132: Permissions ps = new Permissions();
133: try {
134: ps.elements().nextElement();
135: fail("should throw NoSuchElementException");
136: } catch (NoSuchElementException e) {
137: }
138: try {
139: ps.implies(null);
140: fail("should throw NPE");
141: } catch (NullPointerException e) {
142: }
143:
144: try {
145: ps.add(null);
146: fail("should throw NullPointerException");
147: } catch (NullPointerException e) {
148: }
149:
150: }
151:
152: }
|