001: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
002: *
003: * Licensed under the Apache License, Version 2.0 (the "License");
004: * you may not use this file except in compliance with the License.
005: * You may obtain a copy of the License at
006: *
007: * http://www.apache.org/licenses/LICENSE-2.0
008: *
009: * Unless required by applicable law or agreed to in writing, software
010: * distributed under the License is distributed on an "AS IS" BASIS,
011: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: * See the License for the specific language governing permissions and
013: * limitations under the License.
014: */
015:
016: package org.acegisecurity.acl.basic;
017:
018: import junit.framework.TestCase;
019:
020: /**
021: * Tests {@link SimpleAclEntry}.
022: *
023: * @author Ben Alex
024: * @version $Id: SimpleAclEntryTests.java 1597 2006-08-22 17:57:18Z carlossg $
025: */
026: public class SimpleAclEntryTests extends TestCase {
027: //~ Constructors ===================================================================================================
028:
029: public SimpleAclEntryTests() {
030: super ();
031: }
032:
033: public SimpleAclEntryTests(String arg0) {
034: super (arg0);
035: }
036:
037: //~ Methods ========================================================================================================
038:
039: public static void main(String[] args) {
040: junit.textui.TestRunner.run(SimpleAclEntryTests.class);
041: }
042:
043: public final void setUp() throws Exception {
044: super .setUp();
045: }
046:
047: public void testCorrectOperation() {
048: String recipient = "marissa";
049: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
050: "domain", "12");
051: SimpleAclEntry acl = new SimpleAclEntry(recipient,
052: objectIdentity, null, 0);
053:
054: assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
055: acl.addPermission(SimpleAclEntry.ADMINISTRATION);
056: assertTrue(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
057: assertFalse(acl.isPermitted(SimpleAclEntry.CREATE));
058: assertFalse(acl.isPermitted(SimpleAclEntry.DELETE));
059: assertFalse(acl.isPermitted(SimpleAclEntry.READ));
060: assertFalse(acl.isPermitted(SimpleAclEntry.WRITE));
061: assertEquals("A----", acl.printPermissionsBlock());
062: acl.deletePermission(SimpleAclEntry.ADMINISTRATION);
063: assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
064: assertEquals("-----", acl.printPermissionsBlock());
065:
066: acl.addPermissions(new int[] { SimpleAclEntry.READ,
067: SimpleAclEntry.WRITE });
068: acl.addPermission(SimpleAclEntry.CREATE);
069: assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
070: assertTrue(acl.isPermitted(SimpleAclEntry.CREATE));
071: assertFalse(acl.isPermitted(SimpleAclEntry.DELETE));
072: assertTrue(acl.isPermitted(SimpleAclEntry.READ));
073: assertTrue(acl.isPermitted(SimpleAclEntry.WRITE));
074: assertEquals("-RWC-", acl.printPermissionsBlock());
075:
076: acl.deletePermission(SimpleAclEntry.CREATE);
077: acl.deletePermissions(new int[] { SimpleAclEntry.READ,
078: SimpleAclEntry.WRITE });
079: assertEquals("-----", acl.printPermissionsBlock());
080:
081: acl.togglePermission(SimpleAclEntry.CREATE);
082: assertTrue(acl.isPermitted(SimpleAclEntry.CREATE));
083: assertFalse(acl.isPermitted(SimpleAclEntry.ADMINISTRATION));
084: acl.togglePermission(SimpleAclEntry.CREATE);
085: assertFalse(acl.isPermitted(SimpleAclEntry.CREATE));
086:
087: acl.togglePermission(SimpleAclEntry.DELETE);
088: assertTrue(acl.isPermitted(SimpleAclEntry.DELETE));
089: assertEquals("----D", acl.printPermissionsBlock());
090: }
091:
092: public void testDetectsNullOnMainConstructor() {
093: String recipient = "marissa";
094: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
095: "domain", "12");
096:
097: try {
098: new SimpleAclEntry(recipient, null, null, 2);
099: fail("Should have thrown IllegalArgumentException");
100: } catch (IllegalArgumentException expected) {
101: assertTrue(true);
102: }
103:
104: try {
105: new SimpleAclEntry(null, objectIdentity, null, 2);
106: fail("Should have thrown IllegalArgumentException");
107: } catch (IllegalArgumentException expected) {
108: assertTrue(true);
109: }
110: }
111:
112: public void testGettersSetters() {
113: SimpleAclEntry acl = new SimpleAclEntry();
114:
115: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
116: "domain", "693");
117: acl.setAclObjectIdentity(objectIdentity);
118: assertEquals(objectIdentity, acl.getAclObjectIdentity());
119:
120: AclObjectIdentity parentObjectIdentity = new NamedEntityObjectIdentity(
121: "domain", "13");
122: acl.setAclObjectParentIdentity(parentObjectIdentity);
123: assertEquals(parentObjectIdentity, acl
124: .getAclObjectParentIdentity());
125:
126: acl.setMask(2);
127: assertEquals(2, acl.getMask());
128:
129: acl.setRecipient("scott");
130: assertEquals("scott", acl.getRecipient());
131: }
132:
133: public void testRejectsInvalidMasksInAddMethod() {
134: String recipient = "marissa";
135: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
136: "domain", "12");
137: SimpleAclEntry acl = new SimpleAclEntry(recipient,
138: objectIdentity, null, 4);
139:
140: try {
141: acl.addPermission(Integer.MAX_VALUE);
142: fail("Should have thrown IllegalArgumentException");
143: } catch (IllegalArgumentException expected) {
144: assertTrue(true);
145: }
146: }
147:
148: public void testRejectsInvalidMasksInDeleteMethod() {
149: String recipient = "marissa";
150: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
151: "domain", "12");
152: SimpleAclEntry acl = new SimpleAclEntry(recipient,
153: objectIdentity, null, 0);
154: acl.addPermissions(new int[] { SimpleAclEntry.READ,
155: SimpleAclEntry.WRITE, SimpleAclEntry.CREATE });
156:
157: try {
158: acl.deletePermission(SimpleAclEntry.READ); // can't write if we can't read
159: fail("Should have thrown IllegalArgumentException");
160: } catch (IllegalArgumentException expected) {
161: assertTrue(true);
162: }
163: }
164:
165: public void testRejectsInvalidMasksInTogglePermissionMethod() {
166: String recipient = "marissa";
167: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
168: "domain", "12");
169: SimpleAclEntry acl = new SimpleAclEntry(recipient,
170: objectIdentity, null, 0);
171: acl.addPermissions(new int[] { SimpleAclEntry.READ,
172: SimpleAclEntry.WRITE, SimpleAclEntry.CREATE });
173:
174: try {
175: acl.togglePermission(SimpleAclEntry.READ); // can't write if we can't read
176: fail("Should have thrown IllegalArgumentException");
177: } catch (IllegalArgumentException expected) {
178: assertTrue(true);
179: }
180: }
181:
182: public void testToString() {
183: String recipient = "marissa";
184: AclObjectIdentity objectIdentity = new NamedEntityObjectIdentity(
185: "domain", "12");
186: SimpleAclEntry acl = new SimpleAclEntry(recipient,
187: objectIdentity, null, 0);
188: acl.addPermissions(new int[] { SimpleAclEntry.READ,
189: SimpleAclEntry.WRITE, SimpleAclEntry.CREATE });
190: assertTrue(acl.toString().endsWith(
191: "marissa=-RWC- ............................111. (14)]"));
192: }
193:
194: public void testParsePermission() {
195: assertPermission("NOTHING", SimpleAclEntry.NOTHING);
196: assertPermission("ADMINISTRATION",
197: SimpleAclEntry.ADMINISTRATION);
198: assertPermission("READ", SimpleAclEntry.READ);
199: assertPermission("WRITE", SimpleAclEntry.WRITE);
200: assertPermission("CREATE", SimpleAclEntry.CREATE);
201: assertPermission("DELETE", SimpleAclEntry.DELETE);
202: assertPermission("READ_WRITE_DELETE",
203: SimpleAclEntry.READ_WRITE_DELETE);
204: }
205:
206: public void testParsePermissionWrongValues() {
207: try {
208: SimpleAclEntry.parsePermission("X");
209: fail(IllegalArgumentException.class.getName()
210: + " must have been thrown.");
211: } catch (IllegalArgumentException e) {
212: // expected
213: }
214: }
215:
216: private void assertPermission(String permission, int value) {
217: assertEquals(value, SimpleAclEntry.parsePermission(permission));
218: }
219:
220: /**
221: * Check that the value returned by {@link SimpleAclEntry#getValidPermissions()} is not modifiable.
222: */
223: public void testGetPermissions() {
224: SimpleAclEntry acl = new SimpleAclEntry("",
225: new NamedEntityObjectIdentity("x", "x"), null, 0);
226: int[] permissions = acl.getValidPermissions();
227: int i = permissions[0];
228: permissions[0] -= 100;
229: assertEquals(
230: "Value returned by getValidPermissions can be modified",
231: i, acl.getValidPermissions()[0]);
232: }
233: }
|