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 Maxim V. Makarov
020: * @version $Revision$
021: */package org.apache.harmony.auth.tests.javax.security.auth.kerberos;
022:
023: import java.security.AllPermission;
024: import java.security.BasicPermission;
025: import java.security.Permission;
026: import java.security.PermissionCollection;
027: import java.security.SecurityPermission;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Enumeration;
031: import java.util.NoSuchElementException;
032:
033: import javax.security.auth.kerberos.DelegationPermission;
034: import javax.security.auth.kerberos.ServicePermission;
035:
036: import junit.framework.TestCase;
037:
038: /**
039: * Tests DelegationPermission class implementation.
040: */
041: public class DelegationPermissionTest extends TestCase {
042:
043: /**
044: * testing of a correct ctor
045: */
046: public void testCtor() {
047: String name1 = "\"aaa.bbb.com@CCC.COM\" \"ccc.ddd.com@DDD.COM\"";
048: DelegationPermission dp;
049: dp = new DelegationPermission(name1);
050: assertEquals(name1, dp.getName());
051: assertEquals("", dp.getActions());
052: dp = new DelegationPermission(name1, "action");
053: assertEquals("", dp.getActions());
054: dp = new DelegationPermission(name1, null);
055: assertEquals("", dp.getActions());
056: }
057:
058: /**
059: * testing of a incorrect ctor
060: */
061: public void testFailCtor() {
062: try {
063: new DelegationPermission(null);
064: fail("no expected NPE");
065: } catch (NullPointerException e) {
066: }
067: try {
068: new DelegationPermission("");
069: fail("no expected IAE");
070: } catch (IllegalArgumentException e) {
071: }
072: try {
073: new DelegationPermission("\"aaa.bbb.com\" ccc.ddd.com");
074: fail("Target name must be enveloped by quotes");
075: } catch (IllegalArgumentException e) {
076: }
077: try {
078: new DelegationPermission("\"aaa.bbb.com\" ccc.ddd.com\"");
079: fail("Target name must be enveloped by quotes");
080: } catch (IllegalArgumentException e) {
081: }
082: try {
083: new DelegationPermission("\"aaa.bbb.com\" \"ccc.ddd.com");
084: fail("Target name must be enveloped by quotes");
085: } catch (IllegalArgumentException e) {
086: }
087: try {
088: new DelegationPermission("\" \" \" \"");
089: //TODO: fail("Target name is empty");
090: } catch (IllegalArgumentException e) {
091: }
092: try {
093: new DelegationPermission("\"\"");
094: fail("Target name is incorrect");
095: } catch (IllegalArgumentException e) {
096: }
097: try {
098: new DelegationPermission("\"aaa.bbb.com\" \"\"");
099: fail("service principal is empty");
100: } catch (IllegalArgumentException e) {
101: }
102: try {
103: new DelegationPermission("\"\" \"aaa.bbb.com\"");
104: fail("subordinate service principal is empty");
105: } catch (IllegalArgumentException e) {
106: }
107:
108: }
109:
110: public void testFailCtor_2() {
111: try {
112: new DelegationPermission("\"AAA\"");
113: fail("target name should be specifies a pair of kerberos service principals");
114: } catch (IllegalArgumentException e) {
115: }
116: }
117:
118: // testing of the equals method
119: @SuppressWarnings("serial")
120: public void testEquals() {
121: DelegationPermission dp1 = new DelegationPermission(
122: "\"AAA\" \"BBB\"");
123: DelegationPermission dp2 = new DelegationPermission(
124: "\"AAA\" \"BBB\"");
125:
126: assertTrue(dp1.equals(dp1));
127: assertFalse(dp1.equals(new DelegationPermission(
128: "\"aaa\" \"bbb\"")));
129: assertTrue(dp2.equals(dp1));
130: assertTrue(dp1.equals(dp2));
131: assertTrue(dp1.hashCode() == dp2.hashCode());
132: assertFalse(dp1.equals(new BasicPermission("\"AAA\"") {
133: }));
134: }
135:
136: // testing of the implies method
137: public void testImplies() {
138: DelegationPermission dp1 = new DelegationPermission(
139: "\"AAA\" \"BBB\"");
140: DelegationPermission dp2 = new DelegationPermission(
141: "\"BBB\" \"AAA\"");
142: assertFalse(dp1.implies(dp2));
143: assertFalse(dp2.implies(dp1));
144: assertTrue(dp1.implies(dp1));
145: assertFalse(dp1.implies(new ServicePermission("aaa", "accept")));
146: assertFalse(dp1.implies(null));
147: }
148:
149: // testing of the KrbDelegationPermissionCollection
150:
151: // testing of the add collection method
152: public void testAddCollection() {
153: DelegationPermission dp = new DelegationPermission(
154: "\"AAA\" \"BBB\"");
155: PermissionCollection pc1 = dp.newPermissionCollection();
156: PermissionCollection pc2 = dp.newPermissionCollection();
157: assertNotSame(pc1, pc2);
158: pc1.add(new DelegationPermission("\"BBB\" \"AAA\""));
159: try {
160: pc1.add(new SecurityPermission("aaa"));
161: fail("should not add non DelegationPermission");
162: } catch (IllegalArgumentException e) {
163: }
164: try {
165: pc1.add(null);
166: fail("permission is null");
167: } catch (IllegalArgumentException e) {
168: }
169: pc1.setReadOnly();
170: try {
171: pc1.add(new DelegationPermission("\"CCC\" \"AAA\""));
172: fail("read-only flag is ignored");
173: } catch (SecurityException e) {
174: }
175: }
176:
177: public void testImpliesCollection() {
178:
179: Permission ap = new AllPermission();
180: Permission p = new DelegationPermission("\"AAA\" \"BBB\"");
181: PermissionCollection pc = p.newPermissionCollection();
182: assertFalse(pc.implies(ap));
183: assertFalse(pc.implies(p));
184: pc.add(p);
185: assertTrue(pc.implies(p));
186: assertFalse(pc.implies(null));
187: DelegationPermission dp1 = new DelegationPermission(
188: "\"AAA\" \"BBB\"");
189: assertTrue(dp1.implies(dp1));
190: DelegationPermission dp2 = new DelegationPermission(
191: "\"BBB\" \"AAA\"");
192: assertFalse(dp1.implies(dp2));
193: assertFalse(dp1.implies(null));
194: assertFalse(dp1.implies(new ServicePermission("aaa", "accept")));
195: }
196:
197: public void testElements() throws Exception {
198: Permission p = new DelegationPermission("\"AAA\" \"BBB\"");
199: PermissionCollection pc = p.newPermissionCollection();
200: try {
201: pc.elements().nextElement();
202: fail("expected NoSuchElementException");
203: } catch (NoSuchElementException e) {
204: }
205: Enumeration<Permission> en = pc.elements();
206: assertNotNull(en);
207: assertFalse(en.hasMoreElements());
208: Permission sp1 = new DelegationPermission("\"DDD\" \"BBB\"");
209: Permission sp2 = new DelegationPermission("\"CCC\" \"BBB\"");
210: pc.add(sp1);
211: en = pc.elements();
212: assertTrue(en.hasMoreElements());
213: assertTrue(sp1.equals(en.nextElement()));
214: assertFalse(en.hasMoreElements());
215: pc.add(sp2);
216: en = pc.elements();
217: Collection<Permission> c = new ArrayList<Permission>();
218: while (en.hasMoreElements()) {
219: c.add(en.nextElement());
220: }
221: assertFalse(en.hasMoreElements());
222: assertEquals(2, c.size());
223: assertTrue(c.contains(sp1) && c.contains(sp2));
224: }
225:
226: }
|