001: package com.ecyrd.jspwiki.auth.permissions;
002:
003: import junit.framework.TestCase;
004:
005: /**
006: * @author Andrew R. Jaquith
007: */
008: public class PagePermissionTest extends TestCase {
009:
010: public static void main(String[] args) {
011: junit.textui.TestRunner.run(PagePermissionTest.class);
012: }
013:
014: /*
015: * Class under test for boolean equals(java.lang.Object)
016: */
017: public final void testEqualsObject() {
018: PagePermission p1 = new PagePermission("mywiki:Main",
019: "view,edit,delete");
020: PagePermission p2 = new PagePermission("mywiki:Main",
021: "view,edit,delete");
022: PagePermission p3 = new PagePermission("mywiki:Main",
023: "delete,view,edit");
024: PagePermission p4 = new PagePermission("mywiki:Main*",
025: "delete,view,edit");
026: assertEquals(p1, p2);
027: assertEquals(p1, p3);
028: assertFalse(p3.equals(p4));
029: }
030:
031: public final void testCreateMask() {
032: assertEquals(1, PagePermission.createMask("view"));
033: assertEquals(19, PagePermission.createMask("view,edit,delete"));
034: assertEquals(19, PagePermission.createMask("edit,delete,view"));
035: assertEquals(14, PagePermission
036: .createMask("edit,comment,upload"));
037: }
038:
039: /*
040: * Class under test for java.lang.String toString()
041: */
042: public final void testToString() {
043: PagePermission p;
044: p = new PagePermission("Main", "view,edit,delete");
045: assertEquals(
046: "(\"com.ecyrd.jspwiki.auth.permissions.PagePermission\",\":Main\",\"delete,edit,view\")",
047: p.toString());
048: p = new PagePermission("mywiki:Main", "view,edit,delete");
049: assertEquals(
050: "(\"com.ecyrd.jspwiki.auth.permissions.PagePermission\",\"mywiki:Main\",\"delete,edit,view\")",
051: p.toString());
052: }
053:
054: /**
055: * Tests wiki name support.
056: */
057: public final void testWikiNames() {
058: PagePermission p1;
059: PagePermission p2;
060:
061: // Permissions without prepended wiki name should never imply themselves
062: // or others
063: p1 = new PagePermission("Main", "edit");
064: p2 = new PagePermission("Main", "edit");
065: assertFalse(p1.implies(p1));
066: assertFalse(p1.implies(p2));
067:
068: // Permissions with a wildcard wiki should imply other wikis
069: p1 = new PagePermission("*:Main", "edit");
070: p2 = new PagePermission("mywiki:Main", "edit");
071: assertTrue(p1.implies(p2));
072: assertFalse(p2.implies(p1));
073:
074: // Permissions that start with ":" are just like null
075: p1 = new PagePermission(":Main", "edit");
076: p2 = new PagePermission("Main", "edit");
077: assertFalse(p1.implies(p1));
078: assertFalse(p1.implies(p2));
079: }
080:
081: public final void testImpliesAttachments() {
082: PagePermission p1;
083: PagePermission p2;
084:
085: // A page should imply its attachment and vice-versa
086: p1 = new PagePermission("mywiki:Main", "view");
087: p2 = new PagePermission("mywiki:Main/test.png", "view");
088: assertTrue(p1.implies(p2));
089: assertTrue(p2.implies(p1));
090: }
091:
092: /*
093: * Class under test for boolean implies(java.security.Permission)
094: */
095: public final void testImpliesPermission() {
096: PagePermission p1;
097: PagePermission p2;
098: PagePermission p3;
099:
100: // The same permission should imply itself
101: p1 = new PagePermission("mywiki:Main", "view,edit,delete");
102: p2 = new PagePermission("mywiki:Main", "view,edit,delete");
103: assertTrue(p1.implies(p2));
104: assertTrue(p2.implies(p1));
105:
106: // The same permission should imply itself for wildcard wikis
107: p1 = new PagePermission("Main", "view,edit,delete");
108: p2 = new PagePermission("*:Main", "view,edit,delete");
109: p3 = new PagePermission("mywiki:Main", "view,edit,delete");
110: assertFalse(p1.implies(p2));
111: assertFalse(p2.implies(p1));
112: assertFalse(p1.implies(p3));
113: assertTrue(p2.implies(p3));
114: assertFalse(p3.implies(p1));
115: assertFalse(p3.implies(p2));
116:
117: // Actions on collection should imply permission for page with same
118: // actions
119: p1 = new PagePermission("*:*", "view,edit,delete");
120: p2 = new PagePermission("*:Main", "view,edit,delete");
121: p3 = new PagePermission("mywiki:Main", "view,edit,delete");
122: assertTrue(p1.implies(p2));
123: assertTrue(p1.implies(p3));
124: assertTrue(p2.implies(p3));
125: assertFalse(p2.implies(p1));
126: assertFalse(p3.implies(p1));
127:
128: // Actions on single page should imply subset of those actions
129: p1 = new PagePermission("*:Main", "view,edit,delete");
130: p2 = new PagePermission("*:Main", "view");
131: p3 = new PagePermission("mywiki:Main", "view");
132: assertTrue(p1.implies(p2));
133: assertTrue(p1.implies(p3));
134: assertFalse(p2.implies(p1));
135: assertFalse(p3.implies(p1));
136: assertFalse(p3.implies(p2));
137:
138: // Actions on collection should imply subset of actions on single page
139: p1 = new PagePermission("*:*", "view,edit,delete");
140: p2 = new PagePermission("*:Main", "view");
141: p3 = new PagePermission("mywiki:Main", "view");
142: assertTrue(p1.implies(p2));
143: assertTrue(p1.implies(p3));
144: assertFalse(p2.implies(p1));
145: assertFalse(p3.implies(p1));
146:
147: p1 = new PagePermission("*:Mai*", "view,edit,delete");
148: p2 = new PagePermission("*:Main", "view");
149: p3 = new PagePermission("mywiki:Main", "view");
150: assertTrue(p1.implies(p2));
151: assertTrue(p1.implies(p3));
152: assertFalse(p2.implies(p1));
153: assertFalse(p3.implies(p1));
154:
155: p1 = new PagePermission("*:*in", "view,edit,delete");
156: p2 = new PagePermission("*:Main", "view");
157: p3 = new PagePermission("mywiki:Main", "view");
158: assertTrue(p1.implies(p2));
159: assertTrue(p1.implies(p3));
160: assertFalse(p2.implies(p1));
161: assertFalse(p3.implies(p1));
162:
163: // Delete action on collection should imply modify/edit/upload/comment/view on
164: // single page
165: p1 = new PagePermission("*:*in", "delete");
166: p2 = new PagePermission("*:Main", "edit");
167: p3 = new PagePermission("mywiki:Main", "edit");
168: assertTrue(p1.implies(p2));
169: assertTrue(p1.implies(p3));
170: assertFalse(p2.implies(p1));
171: assertFalse(p3.implies(p1));
172:
173: p2 = new PagePermission("*:Main", "modify");
174: p3 = new PagePermission("mywiki:Main", "modify");
175: assertTrue(p1.implies(p2));
176: assertTrue(p1.implies(p3));
177: assertFalse(p2.implies(p1));
178: assertFalse(p3.implies(p1));
179:
180: p2 = new PagePermission("*:Main", "upload");
181: p3 = new PagePermission("mywiki:Main", "upload");
182: assertTrue(p1.implies(p2));
183: assertTrue(p1.implies(p3));
184: assertFalse(p2.implies(p1));
185: assertFalse(p3.implies(p1));
186:
187: p2 = new PagePermission("*:Main", "comment");
188: p3 = new PagePermission("mywiki:Main", "comment");
189: assertTrue(p1.implies(p2));
190: assertTrue(p1.implies(p3));
191: assertFalse(p2.implies(p1));
192: assertFalse(p3.implies(p1));
193:
194: p2 = new PagePermission("*:Main", "view");
195: p3 = new PagePermission("mywiki:Main", "view");
196: assertTrue(p1.implies(p2));
197: assertTrue(p1.implies(p3));
198: assertFalse(p2.implies(p1));
199: assertFalse(p3.implies(p1));
200:
201: // Rename action on collection should imply edit on single page
202: p1 = new PagePermission("*:*in", "rename");
203: p2 = new PagePermission("*:Main", "edit");
204: p3 = new PagePermission("mywiki:Main", "edit");
205: assertTrue(p1.implies(p2));
206: assertTrue(p1.implies(p3));
207: assertFalse(p2.implies(p1));
208: assertFalse(p3.implies(p1));
209:
210: // Modify action on collection should imply upload/comment/view on single
211: // page
212: p1 = new PagePermission("*:*in", "modify");
213: p2 = new PagePermission("*:Main", "upload");
214: p3 = new PagePermission("mywiki:Main", "upload");
215: assertTrue(p1.implies(p2));
216: assertTrue(p1.implies(p3));
217: assertFalse(p2.implies(p1));
218: assertFalse(p3.implies(p1));
219:
220: p2 = new PagePermission("*:Main", "comment");
221: p3 = new PagePermission("mywiki:Main", "comment");
222: assertTrue(p1.implies(p2));
223: assertTrue(p1.implies(p3));
224: assertFalse(p2.implies(p1));
225: assertFalse(p3.implies(p1));
226:
227: p2 = new PagePermission("*:Main", "view");
228: p3 = new PagePermission("mywiki:Main", "view");
229: assertTrue(p1.implies(p2));
230: assertTrue(p1.implies(p3));
231: assertFalse(p2.implies(p1));
232: assertFalse(p3.implies(p1));
233:
234: // Upload action on collection should imply view on single page
235: p1 = new PagePermission("*:*in", "upload");
236: p2 = new PagePermission("*:Main", "view");
237: p3 = new PagePermission("mywiki:Main", "view");
238: assertTrue(p1.implies(p2));
239: assertTrue(p1.implies(p3));
240: assertFalse(p2.implies(p1));
241: assertFalse(p3.implies(p1));
242:
243: // Comment action on collection should imply view on single page
244: p1 = new PagePermission("*:*in", "comment");
245: p2 = new PagePermission("*:Main", "view");
246: p3 = new PagePermission("mywiki:Main", "view");
247: assertTrue(p1.implies(p2));
248: assertTrue(p1.implies(p3));
249: assertFalse(p2.implies(p1));
250: assertFalse(p3.implies(p1));
251:
252: // View action on wildcard collection SHOULD imply view on
253: // GroupConfiguration page
254: p1 = new PagePermission("*:*", "view");
255: p2 = new PagePermission("*:GroupConfiguration", "view");
256: p3 = new PagePermission("mywiki:GroupConfiguration", "view");
257: assertTrue(p1.implies(p2));
258: assertTrue(p1.implies(p3));
259: assertFalse(p2.implies(p1));
260: assertFalse(p3.implies(p1));
261:
262: // Pre- and post- wildcards should also be fine
263: p1 = new PagePermission("*:Group*", "view");
264: p2 = new PagePermission("*:GroupConfiguration", "view");
265: p3 = new PagePermission("mywiki:GroupConfiguration", "view");
266: assertTrue(p1.implies(p2));
267: assertTrue(p1.implies(p3));
268: assertFalse(p2.implies(p1));
269: assertFalse(p3.implies(p1));
270:
271: p1 = new PagePermission("*:*Configuration", "view");
272: p2 = new PagePermission("*:GroupConfiguration", "view");
273: p3 = new PagePermission("mywiki:GroupConfiguration", "view");
274: assertTrue(p1.implies(p2));
275: assertTrue(p1.implies(p3));
276: assertFalse(p2.implies(p1));
277: assertFalse(p3.implies(p1));
278: }
279:
280: public final void testImplies() {
281: assertFalse(PagePermission.RENAME
282: .implies(PagePermission.MODIFY));
283: assertTrue(PagePermission.RENAME.implies(PagePermission.EDIT));
284: assertFalse(PagePermission.RENAME
285: .implies(PagePermission.UPLOAD));
286: assertTrue(PagePermission.RENAME
287: .implies(PagePermission.COMMENT));
288: assertTrue(PagePermission.RENAME.implies(PagePermission.VIEW));
289:
290: assertTrue(PagePermission.DELETE.implies(PagePermission.MODIFY));
291: assertTrue(PagePermission.DELETE.implies(PagePermission.EDIT));
292: assertTrue(PagePermission.DELETE.implies(PagePermission.UPLOAD));
293: assertTrue(PagePermission.DELETE
294: .implies(PagePermission.COMMENT));
295: assertTrue(PagePermission.DELETE.implies(PagePermission.VIEW));
296:
297: assertTrue(PagePermission.MODIFY.implies(PagePermission.EDIT));
298: assertTrue(PagePermission.MODIFY.implies(PagePermission.UPLOAD));
299: assertTrue(PagePermission.MODIFY
300: .implies(PagePermission.COMMENT));
301: assertTrue(PagePermission.MODIFY.implies(PagePermission.VIEW));
302:
303: assertTrue(PagePermission.EDIT.implies(PagePermission.VIEW));
304: assertTrue(PagePermission.EDIT.implies(PagePermission.COMMENT));
305:
306: assertTrue(PagePermission.UPLOAD.implies(PagePermission.VIEW));
307:
308: assertTrue(PagePermission.COMMENT.implies(PagePermission.VIEW));
309: }
310:
311: public final void testImpliedMask() {
312: int result = (PagePermission.DELETE_MASK
313: | PagePermission.MODIFY_MASK | PagePermission.EDIT_MASK
314: | PagePermission.COMMENT_MASK
315: | PagePermission.UPLOAD_MASK | PagePermission.VIEW_MASK);
316: assertEquals(result, PagePermission
317: .impliedMask(PagePermission.DELETE_MASK));
318:
319: result = (PagePermission.RENAME_MASK | PagePermission.EDIT_MASK
320: | PagePermission.COMMENT_MASK | PagePermission.VIEW_MASK);
321: assertEquals(result, PagePermission
322: .impliedMask(PagePermission.RENAME_MASK));
323:
324: result = (PagePermission.MODIFY_MASK | PagePermission.EDIT_MASK
325: | PagePermission.COMMENT_MASK
326: | PagePermission.UPLOAD_MASK | PagePermission.VIEW_MASK);
327: assertEquals(result, PagePermission
328: .impliedMask(PagePermission.MODIFY_MASK));
329:
330: result = (PagePermission.EDIT_MASK
331: | PagePermission.COMMENT_MASK | PagePermission.VIEW_MASK);
332: assertEquals(result, PagePermission
333: .impliedMask(PagePermission.EDIT_MASK));
334:
335: result = (PagePermission.COMMENT_MASK | PagePermission.VIEW_MASK);
336: assertEquals(result, PagePermission
337: .impliedMask(PagePermission.COMMENT_MASK));
338:
339: result = (PagePermission.UPLOAD_MASK | PagePermission.VIEW_MASK);
340: assertEquals(result, PagePermission
341: .impliedMask(PagePermission.UPLOAD_MASK));
342: }
343:
344: public final void testGetName() {
345: PagePermission p;
346: p = new PagePermission("Main", "view,edit,delete");
347: assertEquals("Main", p.getName());
348: p = new PagePermission("mywiki:Main", "view,edit,delete");
349: assertEquals("mywiki:Main", p.getName());
350: assertNotSame("*:Main", p.getName());
351: }
352:
353: /*
354: * Class under test for java.lang.String getActions()
355: */
356: public final void testGetActions() {
357: PagePermission p = new PagePermission("Main",
358: "VIEW,edit,delete");
359: assertEquals("delete,edit,view", p.getActions());
360: }
361:
362: }
|