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: * @author Maxim V. Makarov
019: * @version $Revision$
020: */package javax.security.auth;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.ObjectInputStream;
025: import java.io.ObjectOutputStream;
026: import java.security.Principal;
027: import java.util.HashSet;
028:
029: import junit.framework.TestCase;
030:
031: /**
032: * Tests PrivateCredentialPermission class implementation.
033: */
034: public class PrivateCredentialPermissionTest extends TestCase {
035:
036: private PrivateCredentialPermission p_that;
037:
038: private PrivateCredentialPermission p_this ;
039:
040: String s_that;
041:
042: String s_this ;
043:
044: /**
045: * Constructor for PrivateCredentialPermissionTest.
046: *
047: * @param name
048: */
049: public PrivateCredentialPermissionTest(String name) {
050: super (name);
051: }
052:
053: /**
054: * [C1 P1 "duke"] implies [C1 P1 "duke" P2 "nuke"].
055: */
056: public final void testImplies_01() {
057: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
058: s_this = "a.b.Credential a.b.Principal \"duke\"";
059: p_that = new PrivateCredentialPermission(s_that, "read");
060: p_this = new PrivateCredentialPermission(s_this , "read");
061: assertTrue(p_this .implies(p_that));
062: assertTrue(p_this .implies(p_this ));
063: }
064:
065: /**
066: * [C1 P1 "nuke"] implies [C1 P1 "duke" P2 "nuke"].
067: */
068: public final void testImplies_02() {
069: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
070: s_this = "a.b.Credential a.c.Principal \"nuke\"";
071: p_that = new PrivateCredentialPermission(s_that, "read");
072: p_this = new PrivateCredentialPermission(s_this , "read");
073: assertTrue(p_this .implies(p_that));
074:
075: }
076:
077: /**
078: * [* P1 "duke"] implies [C1 P1 "duke"]
079: */
080: public final void testImplies_03() {
081: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
082: s_this = "* a.b.Principal \"duke\"";
083: p_that = new PrivateCredentialPermission(s_that, "read");
084: p_this = new PrivateCredentialPermission(s_this , "read");
085: assertTrue(p_this .implies(p_that));
086:
087: }
088:
089: /**
090: * [C1P1 "duke"] implies [C1 P1 "*"]
091: */
092: public final void testImplies_04() {
093: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
094: s_this = "a.b.Credential a.c.Principal \"*\"";
095: p_that = new PrivateCredentialPermission(s_that, "read");
096: p_this = new PrivateCredentialPermission(s_this , "read");
097: assertTrue(p_this .implies(p_that));
098:
099: }
100:
101: /**
102: * [C1 P1 "duke" P2 "nuke"] implies [C1 * "*"]
103: */
104: public final void testImplies_05() {
105: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
106: s_this = "a.b.Credential * \"*\"";
107: p_that = new PrivateCredentialPermission(s_that, "read");
108: p_this = new PrivateCredentialPermission(s_this , "read");
109: assertTrue(p_this .implies(p_that));
110:
111: }
112:
113: /**
114: * [C1 P1 "duke" P2 "nuke"] implies [C1 P1 "duke" P2 "nuke" P3 "duke"]
115: */
116: public final void testImplies_06() {
117: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\" a.d.Principal \"duke\"";
118: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
119: p_that = new PrivateCredentialPermission(s_that, "read");
120: p_this = new PrivateCredentialPermission(s_this , "read");
121: assertTrue(p_this .implies(p_that));
122:
123: }
124:
125: /**
126: * [C2 P1 "duke" ] does not imply [C1 P1 "duke" P2 "nuke" ]
127: */
128: public final void testImplies_07() {
129: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
130: s_this = "a.c.Credential a.b.Principal \"duke\"";
131: p_that = new PrivateCredentialPermission(s_that, "read");
132: p_this = new PrivateCredentialPermission(s_this , "read");
133: assertFalse(p_this .implies(p_that));
134: }
135:
136: /**
137: * [C1 P3 "duke" ] does not imply [C1 P1 "duke" P2 "nuke" ]
138: */
139: public final void testImplies_08() {
140: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
141: s_this = "a.b.Credential a.d.Principal \"duke\"";
142: p_that = new PrivateCredentialPermission(s_that, "read");
143: p_this = new PrivateCredentialPermission(s_this , "read");
144: assertFalse(p_this .implies(p_that));
145: }
146:
147: /**
148: * [C1 P1 "nuke" ] does not imply [C1 P1 "duke" P3 "nuke" ]
149: */
150: public final void testImplies_09() {
151: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
152: s_this = "a.b.Credential a.b.Principal \"nuke\"";
153: p_that = new PrivateCredentialPermission(s_that, "read");
154: p_this = new PrivateCredentialPermission(s_this , "read");
155: assertFalse(p_this .implies(p_that));
156:
157: }
158:
159: /**
160: * [C1 P1 "nuke" P2 "buke"] does not imply [C1 P1 "duke" P2 "nuke" ]
161: */
162: public final void testImplies_10() {
163: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
164: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"buke\"";
165: p_that = new PrivateCredentialPermission(s_that, "read");
166: p_this = new PrivateCredentialPermission(s_this , "read");
167: p_this .implies(p_that);
168: assertFalse(p_this .implies(p_that));
169:
170: }
171:
172: /**
173: * [C1 P1 "nuke" P2 "nuke" P3 "buke"] does not imply [C1 P1 "duke" P2 "nuke" P3 "kuke"]
174: */
175: public final void testImplies_110() {
176: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\" a.d.Principal \"kuke\"";
177: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\" a.d.Principal \"buke\"";
178: p_that = new PrivateCredentialPermission(s_that, "read");
179: p_this = new PrivateCredentialPermission(s_this , "read");
180: assertFalse(p_this .implies(p_that));
181: }
182:
183: /**
184: * [C1 P1 "duke" P2 "buke"] does not imply [C1 P1 "*" ]
185: */
186: public final void testImplies_11() {
187: s_that = "a.b.Credential a.b.Principal \"*\"";
188: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"buke\"";
189: p_that = new PrivateCredentialPermission(s_that, "read");
190: p_this = new PrivateCredentialPermission(s_this , "read");
191: assertFalse(p_this .implies(p_that));
192:
193: }
194:
195: /**
196: * [C1 P2 "*"] does not imply [C1 P1 "*" ]
197: */
198: public final void testImplies_12() {
199: s_that = "a.b.Credential a.b.Principal \"*\"";
200: s_this = "a.b.Credential a.c.Principal \"*\"";
201: p_that = new PrivateCredentialPermission(s_that, "read");
202: p_this = new PrivateCredentialPermission(s_this , "read");
203: assertFalse(p_this .implies(p_that));
204:
205: }
206:
207: /**
208: * [* P3 "duke"] does not imply [C1 P1 "duke" P2 "nuke"]
209: */
210: public final void testImplies_13() {
211: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
212: s_this = "* a.d.Principal \"duke\"";
213: p_that = new PrivateCredentialPermission(s_that, "read");
214: p_this = new PrivateCredentialPermission(s_this , "read");
215: assertFalse(p_this .implies(p_that));
216: }
217:
218: /**
219: * [* P2 "buke"] does not imply [C1 P1 "duke" P2 "nuke"]
220: */
221: public final void testImplies_14() {
222: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
223: s_this = "* a.c.Principal \"buke\"";
224: p_that = new PrivateCredentialPermission(s_that, "read");
225: p_this = new PrivateCredentialPermission(s_this , "read");
226: assertFalse(p_this .implies(p_that));
227: }
228:
229: /**
230: * [C1 P1 "buke"] does not imply [C1 P1 "*"]
231: */
232: public final void testImplies_15() {
233: s_that = "a.b.Credential a.b.Principal \"*\"";
234: s_this = "a.b.Credential a.b.Principal \"nuke\"";
235: p_that = new PrivateCredentialPermission(s_that, "read");
236: p_this = new PrivateCredentialPermission(s_this , "read");
237: assertFalse(p_this .implies(p_that));
238: }
239:
240: /**
241: * [C1 * "*"] does not imply [C1 P1 "duke" P2 "nuke"]
242: */
243: public final void testImplies_16() {
244: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
245: s_this = "a.b.Credential * \"*\" a.b.Principal \"nuke\"";
246: p_that = new PrivateCredentialPermission(s_that, "read");
247: p_this = new PrivateCredentialPermission(s_this , "read");
248: assertFalse(p_this .implies(p_that));
249: }
250:
251: /**
252: * [C1 a.c* "nuke"] not implies [C1 P1 "duke" a.c.P2 "nuke"]
253: */
254: public final void testImplies_17() {
255: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
256: s_this = "a.b.Credential a.c.* \"nuke\"";
257: p_that = new PrivateCredentialPermission(s_that, "read");
258: p_this = new PrivateCredentialPermission(s_this , "read");
259: //try {
260: assertFalse(p_this .implies(p_that));
261: //} catch (AssertionFailedError e){
262: // fail("It seems to me this test should be pass, so \"principalClass\"" +
263: // " should be has a name like 'a.b.*'");
264: //}
265: }
266:
267: /**
268: * A permission is not an instance of PrivateCredentialPermission
269: */
270: public final void testImplies_18() {
271: AuthPermission p_this = new AuthPermission("name", "read");
272: s_that = "a.b.Credential a.c.* \"nuke\"";
273: p_that = new PrivateCredentialPermission(s_that, "read");
274: assertFalse(p_that.implies(p_this ));
275: assertFalse(p_that.implies(null));
276: }
277:
278: /**
279: * Create a correct ctor
280: *
281: * @throws IllegalArgumentException
282: */
283: public final void testPCP_01() throws IllegalArgumentException {
284: p_this = new PrivateCredentialPermission(
285: "a.b.Credential a.b.Principal \"duke\" a.b.Principal \"nuke\"",
286: "read");
287: }
288:
289: /**
290: * Create a incorrect ctor without string of Principal class and Principal name,
291: * expected IAE
292: */
293: public final void testPCP_02() {
294: try {
295: p_this = new PrivateCredentialPermission("a.b.Credential",
296: "read");
297: fail("new PrivateCredentialPermission('a.b.Credential', 'read') should throw IllegalArgumentException");
298: } catch (IllegalArgumentException e) {
299: }
300: }
301:
302: /**
303: * Create a incorrect ctor without string of Principal name,
304: * expected IAE
305: */
306: public final void testPCP_03() {
307: try {
308: p_this = new PrivateCredentialPermission(
309: "a.b.Credential a.b.Principal", "read");
310: fail("new PrivateCredentialPermission('a.b.Credential a.b.Principal', 'read') should throw IllegalArgumentException");
311: } catch (IllegalArgumentException e) {
312: }
313: }
314:
315: /**
316: * Create a incorrect ctor without string of Principal class,
317: * expected IAE
318: */
319: public final void testPCP_04() {
320: try {
321: p_this = new PrivateCredentialPermission(
322: "a.b.Credential \"duke\" a.b.Principal \"nuke\"",
323: "read");
324: fail("should throw IllegalArgumentException");
325: } catch (IllegalArgumentException e) {
326: }
327: }
328:
329: /**
330: * Create a incorrect ctor. Principal Class can not be a wildcard (*)
331: * value if Principal Name is not a wildcard (*) value,
332: * expected IAE
333: */
334: public final void testPCP_05() {
335:
336: try {
337: p_this = new PrivateCredentialPermission(
338: "a.b.Credential * \"nuke\"", "read");
339: fail("should throw IllegalArgumentException");
340: } catch (IllegalArgumentException e) {
341: }
342: }
343:
344: /**
345: * "Action must be "read" only,
346: * expected IAE
347: */
348: public final void testPCP_06() throws IllegalArgumentException {
349:
350: try {
351: p_this = new PrivateCredentialPermission(
352: "a.b.Credential a.b.Principal \"nuke\"", "write");
353: fail("Action can be \"read\" only, IllegalArgumentException should be thrown");
354: } catch (IllegalArgumentException e) {
355: }
356: }
357:
358: /**
359: * Principal name must be enveloped by quotes,
360: * expected IAE
361: */
362: public final void testPCP_07() throws IllegalArgumentException {
363:
364: try {
365: p_this = new PrivateCredentialPermission(
366: "a.b.Credential a.b.Principal a.c.Principal",
367: "read");
368: fail("IllegalArgumentException should be thrown");
369: } catch (IllegalArgumentException e) {
370: }
371: }
372:
373: /**
374: * Principal name must be enveloped by quotes,
375: * expected IAE
376: */
377: public final void testPCP_08() throws IllegalArgumentException {
378:
379: try {
380: p_this = new PrivateCredentialPermission(
381: "a.b.Credential a.b.Principal \"duke\" a.c.Principal",
382: "read");
383: } catch (IllegalArgumentException e) {
384: }
385: }
386:
387: /**
388: * Principal name must be enveloped by quotes,
389: * expected IAE
390: */
391: public final void testPCP_09() throws IllegalArgumentException {
392:
393: try {
394: p_this = new PrivateCredentialPermission(
395: "a.b.Credential a.b.Principal duke\"", "read");
396: fail("should be throw IllegalArgumentException");
397: } catch (IllegalArgumentException e) {
398: }
399: }
400:
401: /**
402: * Principal name must be enveloped by quotes,
403: * expected IAE
404: */
405: public final void testPCP_10() throws IllegalArgumentException {
406:
407: try {
408: p_this = new PrivateCredentialPermission(
409: "a.b.Credential a.b.Principal \"duke", "read");
410: fail("should be throw IllegalArgumentException");
411: } catch (IllegalArgumentException e) {
412: }
413: }
414:
415: /**
416: * Create a ctor. Code to support a principal class as a.c.*
417: *
418: * @throws IllegalArgumentException
419: */
420: public final void testPCP_11() throws IllegalArgumentException {
421: p_this = new PrivateCredentialPermission(
422: "a.b.Credential a.b.* \"duke\"", "read");
423: }
424:
425: /**
426: * Create a ctor. [* * "*"]
427: *
428: * @throws IllegalArgumentException
429: */
430: public final void testPCP_12() throws IllegalArgumentException {
431: p_this = new PrivateCredentialPermission("* * \"*\"", "read");
432: }
433:
434: /**
435: * a target name should not be empty
436: * @throws IllegalArgumentException
437: */
438: public final void testPCP_13() throws IllegalArgumentException {
439: try {
440: p_this = new PrivateCredentialPermission(new String(),
441: "read");
442: fail("should be throw IllegalArgumentException");
443: } catch (IllegalArgumentException e) {
444: }
445: }
446:
447: /**
448: * a target name should not be null
449: */
450: public final void testPCP_14() {
451: try {
452: new PrivateCredentialPermission(null, "read");
453: fail("should be throw NullPointerException");
454: } catch (NullPointerException e) {
455: }
456: }
457:
458: /**
459: * test empty principal/name pairs
460: */
461: public final void testPCP_15() {
462: try {
463: p_this = new PrivateCredentialPermission(
464: "java.lang.Object", "read");
465: fail("new PrivateCredentialPermission('java.lang.Object', 'read') should throw IllegalArgumentException");
466: } catch (IllegalArgumentException e) {
467: }
468: }
469:
470: /**
471: * [C1 P1 "nuke" P2 "nuke"] equals [C1 P1 "duke" P2 "nuke"]
472: */
473: public final void testEquals_01() {
474: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
475: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
476: p_that = new PrivateCredentialPermission(s_that, "read");
477: p_this = new PrivateCredentialPermission(s_this , "read");
478: assertTrue(p_this .equals(p_that));
479: assertTrue(p_this .equals(p_this ));
480: assertEquals(p_this .hashCode(), p_that.hashCode());
481: }
482:
483: /**
484: * two permissions are equal if the order of the Principals in
485: * the respective target names is not relevant.
486: */
487: public final void testEquals_02() {
488: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
489: s_this = "a.b.Credential a.c.Principal \"nuke\" a.b.Principal \"duke\"";
490: p_that = new PrivateCredentialPermission(s_that, "read");
491: p_this = new PrivateCredentialPermission(s_this , "read");
492: assertTrue(p_this .equals(p_that));
493: assertEquals(p_this .hashCode(), p_that.hashCode());
494: }
495:
496: /**
497: * two permissions are not equal even if its have the same credential class
498: * and a different number of principals.
499: */
500: public final void testEquals_03() {
501:
502: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
503: s_this = "a.b.Credential a.b.Principal \"duke\"";
504: p_that = new PrivateCredentialPermission(s_that, "read");
505: p_this = new PrivateCredentialPermission(s_this , "read");
506: assertFalse(p_this .equals(p_that));
507: }
508:
509: /**
510: * two permissions are not equal if either of them has not the same
511: * credential class, principal class and principal name
512: */
513: public final void testEquals_04() {
514: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
515: s_this = "a.c.Credential a.d.Principal \"buke\"";
516: p_that = new PrivateCredentialPermission(s_that, "read");
517: p_this = new PrivateCredentialPermission(s_this , "read");
518: assertFalse(p_this .equals(p_that));
519: }
520:
521: /**
522: * two permissions are not equal if either of them has not the same
523: * principal class and principal name
524: */
525: public final void testEquals_05() {
526: s_that = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
527: s_this = "a.b.Credential a.d.Principal \"buke\"";
528: p_that = new PrivateCredentialPermission(s_that, "read");
529: p_this = new PrivateCredentialPermission(s_this , "read");
530: assertFalse(p_this .equals(p_that));
531: }
532:
533: /**
534: * [C1 P1 "duke"] equals [C1 P1 "duke"] and
535: * hashCode of them equals too.
536: */
537: public final void testEquals_06() {
538: s_that = "a.b.Credential a.b.Principal \"duke\"";
539: s_this = "a.b.Credential a.b.Principal \"duke\"";
540: p_that = new PrivateCredentialPermission(s_that, "read");
541: p_this = new PrivateCredentialPermission(s_this , "read");
542: assertTrue(p_this .equals(p_that));
543: assertTrue(p_that.equals(p_this ));
544: assertEquals(p_this .hashCode(), p_that.hashCode());
545: }
546:
547: /**
548: * two permissions are not equal if either of them has not the same principal name
549: */
550: public final void testEquals_07() {
551: s_that = "a.b.Credential a.b.Principal \"duke\"";
552: s_this = "a.b.Credential a.b.Principal \"buke\"";
553: p_that = new PrivateCredentialPermission(s_that, "read");
554: p_this = new PrivateCredentialPermission(s_this , "read");
555: assertFalse(p_this .equals(p_that));
556: }
557:
558: /**
559: * Verifies that a permission object is equal to itself
560: */
561: public final void testEquals_08() {
562: s_this = "a.b.Credential a.b.Principal \"buke\"";
563: p_this = new PrivateCredentialPermission(s_this , "read");
564: assertTrue(p_this .equals(p_this ));
565: }
566:
567: /**
568: * [C1 P1 "duke"] does not equal NULL
569: */
570: public final void testEquals_09() {
571: s_this = "a.b.Credential a.b.Principal \"buke\"";
572: p_this = new PrivateCredentialPermission(s_this , "read");
573: assertFalse(p_this .equals(null));
574: AuthPermission other = new AuthPermission("some");
575: assertFalse(p_this .implies(other));
576: }
577:
578: /**
579: * two permissions are not equals if either of them has not the same credential class
580: */
581: public final void testEquals_10() {
582: s_that = "a.b.Credential a.b.Principal \"duke\"";
583: s_this = "a.c.Credential a.b.Principal \"duke\"";
584: p_that = new PrivateCredentialPermission(s_that, "read");
585: p_this = new PrivateCredentialPermission(s_this , "read");
586: assertFalse(p_this .equals(p_that));
587: assertFalse(p_that.equals(p_this ));
588: }
589:
590: /**
591: * the method newPermissionCollection() always returns null,
592: * the method getActions() always returns "read" and
593: * the method getCredentialClass() returns the name of the CredentialClass.
594: */
595: public final void testGetCredentialClassAndGetAction() {
596: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
597: p_this = new PrivateCredentialPermission(s_this , "read");
598: assertEquals("read", p_this .getActions());
599: assertEquals("a.b.Credential", p_this .getCredentialClass());
600: assertNull(p_this .newPermissionCollection());
601: }
602:
603: /**
604: * Returns the set of principals which to represent like array[x][y]
605: * Implementation specific.
606: */
607: public final void testGetPrincipals_01() {
608: s_this = "a.b.Credential a.b.Principal \"duke\" a.c.Principal \"nuke\"";
609: p_this = new PrivateCredentialPermission(s_this , "read");
610: String p[][] = p_this .getPrincipals();
611: assertEquals("a.b.Principal", p[0][0]);
612: assertEquals("duke", p[0][1]);
613: assertEquals("a.c.Principal", p[1][0]);
614: assertEquals("nuke", p[1][1]);
615: }
616:
617: /**
618: * the same as the method testGetPrincipals_01()
619: */
620: public final void testGetPrincipals_02() {
621: s_this = "a.b.Credential a.d.Principal \"buke\" a.b.Principal \"duke\" a.c.Principal \"nuke\"";
622: p_this = new PrivateCredentialPermission(s_this , "read");
623: String p[][] = p_this .getPrincipals();
624: assertEquals("a.d.Principal", p[0][0]);
625: assertEquals("buke", p[0][1]);
626: assertEquals("a.b.Principal", p[1][0]);
627: assertEquals("duke", p[1][1]);
628: assertEquals("a.c.Principal", p[2][0]);
629: assertEquals("nuke", p[2][1]);
630: }
631:
632: public final void testCtor() {
633:
634: class MyPrincipal implements Principal {
635: String name;
636:
637: MyPrincipal(String name) {
638: this .name = name;
639: }
640:
641: public String getName() {
642: return name;
643: }
644: }
645:
646: MyPrincipal mp = new MyPrincipal("duke");
647: MyPrincipal mp1 = new MyPrincipal("nuke");
648: HashSet<Principal> hash = new HashSet<Principal>();
649: hash.add(mp);
650: hash.add(mp1);
651:
652: PrivateCredentialPermission p1 = new PrivateCredentialPermission(
653: "java.lang.Object", hash);
654:
655: PrivateCredentialPermission p2 = new PrivateCredentialPermission(
656: "java.lang.Object " + MyPrincipal.class.getName()
657: + " \"duke\"", "read");
658: assertFalse(p1.implies(p2));
659: assertTrue(p2.implies(p1));
660:
661: PrivateCredentialPermission p3 = new PrivateCredentialPermission(
662: "java.lang.Object", new HashSet<Principal>());
663:
664: PrivateCredentialPermission p4 = new PrivateCredentialPermission(
665: "java.lang.Object * \"*\"", "read");
666:
667: assertTrue(p4.implies(p3));
668: }
669:
670: public final void testDuplicates() {
671:
672: // string contains duplicate entries: b "c"
673: PrivateCredentialPermission p = new PrivateCredentialPermission(
674: "a b \"c\" b \"c\"", "read");
675:
676: assertEquals("Size", p.getPrincipals().length, 1);
677: }
678:
679: public final void testImmutability() {
680: PrivateCredentialPermission p = new PrivateCredentialPermission(
681: "a b \"c\"", "read");
682:
683: assertTrue("Array reference", p.getPrincipals() != p
684: .getPrincipals());
685: }
686:
687: public final void testWhiteSpaces() {
688:
689: String[] illegalTargetNames = new String[] { "a\nb \"c\"", // \n - delimiter
690: "a\tb \"c\"", // \t - delimiter
691: "a\rb \"c\"", // \r - delimiter
692: "a b\n\"c\"", // \n - delimiter
693: "a b\t\"c\"", // \t - delimiter
694: "a b\r\"c\"", // \r - delimiter
695: "a b \"c\"", // two spaces between credential and principal
696: "a b \"c\"" // two spaces between principal class and name
697: };
698:
699: for (String element : illegalTargetNames) {
700: try {
701: new PrivateCredentialPermission(element, "read");
702: fail("No expected IllegalArgumentException");
703: } catch (IllegalArgumentException e) {
704: }
705: }
706:
707: // principal name has a space
708: PrivateCredentialPermission p = new PrivateCredentialPermission(
709: "a b \"c c\"", "read");
710:
711: assertEquals("Principal name:", "c c", p.getPrincipals()[0][1]);
712: }
713:
714: public final void testSerialization_Wildcard() throws Exception {
715:
716: PrivateCredentialPermission all = new PrivateCredentialPermission(
717: "* * \"*\"", "read");
718: PrivateCredentialPermission p = new PrivateCredentialPermission(
719: "a b \"c\"", "read");
720:
721: assertTrue(all.implies(p));
722:
723: ByteArrayOutputStream out = new ByteArrayOutputStream();
724: ObjectOutputStream oOut = new ObjectOutputStream(out);
725:
726: oOut.writeObject(all);
727: oOut.flush();
728: oOut.close();
729:
730: ByteArrayInputStream in = new ByteArrayInputStream(out
731: .toByteArray());
732: ObjectInputStream oIn = new ObjectInputStream(in);
733:
734: PrivateCredentialPermission newAll = (PrivateCredentialPermission) oIn
735: .readObject();
736:
737: assertTrue("New implies", newAll.implies(p));
738: assertEquals("Equals", all, newAll);
739: }
740:
741: public final void testSerialization_Golden() throws Exception {
742:
743: new PrivateCredentialPermission("a b \"c\" d \"e\"", "read");
744:
745: ByteArrayInputStream in = new ByteArrayInputStream(gForm);
746: ObjectInputStream sIn = new ObjectInputStream(in);
747:
748: PrivateCredentialPermission p = (PrivateCredentialPermission) sIn
749: .readObject();
750:
751: assertEquals("CredentialClass ", "a", p.getCredentialClass());
752:
753: String[][] principals = p.getPrincipals();
754: assertEquals("Size:", 1, principals.length);
755: assertEquals("PrincipalClass:", "b", principals[0][0]);
756: assertEquals("PrincipalName:", "c", principals[0][1]);
757: }
758:
759: public final void testSerialization_Self() throws Exception {
760:
761: ByteArrayInputStream in = new ByteArrayInputStream(selfForm);
762: ObjectInputStream sIn = new ObjectInputStream(in);
763:
764: PrivateCredentialPermission p = (PrivateCredentialPermission) sIn
765: .readObject();
766:
767: assertEquals("CredentialClass ", "a", p.getCredentialClass());
768:
769: String[][] principals = p.getPrincipals();
770: assertEquals("Size:", 1, principals.length);
771: assertEquals("PrincipalClass:", "b", principals[0][0]);
772: assertEquals("PrincipalName:", "c", principals[0][1]);
773: }
774:
775: // Golden: PrivateCredentialPermission("a b \"c\"","read");
776: // generated using public API
777: public static final byte[] gForm = new byte[] { (byte) 0xac,
778: (byte) 0xed, (byte) 0x00, (byte) 0x05, (byte) 0x73,
779: (byte) 0x72, (byte) 0x00, (byte) 0x2f, (byte) 0x6a,
780: (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x78,
781: (byte) 0x2e, (byte) 0x73, (byte) 0x65, (byte) 0x63,
782: (byte) 0x75, (byte) 0x72, (byte) 0x69, (byte) 0x74,
783: (byte) 0x79, (byte) 0x2e, (byte) 0x61, (byte) 0x75,
784: (byte) 0x74, (byte) 0x68, (byte) 0x2e, (byte) 0x50,
785: (byte) 0x72, (byte) 0x69, (byte) 0x76, (byte) 0x61,
786: (byte) 0x74, (byte) 0x65, (byte) 0x43, (byte) 0x72,
787: (byte) 0x65, (byte) 0x64, (byte) 0x65, (byte) 0x6e,
788: (byte) 0x74, (byte) 0x69, (byte) 0x61, (byte) 0x6c,
789: (byte) 0x50, (byte) 0x65, (byte) 0x72, (byte) 0x6d,
790: (byte) 0x69, (byte) 0x73, (byte) 0x73, (byte) 0x69,
791: (byte) 0x6f, (byte) 0x6e, (byte) 0x49, (byte) 0x55,
792: (byte) 0xdc, (byte) 0x77, (byte) 0x7b, (byte) 0x50,
793: (byte) 0x7f, (byte) 0x4c, (byte) 0x02, (byte) 0x00,
794: (byte) 0x03, (byte) 0x5a, (byte) 0x00, (byte) 0x07,
795: (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x74,
796: (byte) 0x69, (byte) 0x6e, (byte) 0x67, (byte) 0x4c,
797: (byte) 0x00, (byte) 0x0f, (byte) 0x63, (byte) 0x72,
798: (byte) 0x65, (byte) 0x64, (byte) 0x65, (byte) 0x6e,
799: (byte) 0x74, (byte) 0x69, (byte) 0x61, (byte) 0x6c,
800: (byte) 0x43, (byte) 0x6c, (byte) 0x61, (byte) 0x73,
801: (byte) 0x73, (byte) 0x74, (byte) 0x00, (byte) 0x12,
802: (byte) 0x4c, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
803: (byte) 0x61, (byte) 0x2f, (byte) 0x6c, (byte) 0x61,
804: (byte) 0x6e, (byte) 0x67, (byte) 0x2f, (byte) 0x53,
805: (byte) 0x74, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
806: (byte) 0x67, (byte) 0x3b, (byte) 0x4c, (byte) 0x00,
807: (byte) 0x0a, (byte) 0x70, (byte) 0x72, (byte) 0x69,
808: (byte) 0x6e, (byte) 0x63, (byte) 0x69, (byte) 0x70,
809: (byte) 0x61, (byte) 0x6c, (byte) 0x73, (byte) 0x74,
810: (byte) 0x00, (byte) 0x0f, (byte) 0x4c, (byte) 0x6a,
811: (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2f,
812: (byte) 0x75, (byte) 0x74, (byte) 0x69, (byte) 0x6c,
813: (byte) 0x2f, (byte) 0x53, (byte) 0x65, (byte) 0x74,
814: (byte) 0x3b, (byte) 0x78, (byte) 0x72, (byte) 0x00,
815: (byte) 0x18, (byte) 0x6a, (byte) 0x61, (byte) 0x76,
816: (byte) 0x61, (byte) 0x2e, (byte) 0x73, (byte) 0x65,
817: (byte) 0x63, (byte) 0x75, (byte) 0x72, (byte) 0x69,
818: (byte) 0x74, (byte) 0x79, (byte) 0x2e, (byte) 0x50,
819: (byte) 0x65, (byte) 0x72, (byte) 0x6d, (byte) 0x69,
820: (byte) 0x73, (byte) 0x73, (byte) 0x69, (byte) 0x6f,
821: (byte) 0x6e, (byte) 0xb1, (byte) 0xc6, (byte) 0xe1,
822: (byte) 0x3f, (byte) 0x28, (byte) 0x57, (byte) 0x51,
823: (byte) 0x7e, (byte) 0x02, (byte) 0x00, (byte) 0x01,
824: (byte) 0x4c, (byte) 0x00, (byte) 0x04, (byte) 0x6e,
825: (byte) 0x61, (byte) 0x6d, (byte) 0x65, (byte) 0x74,
826: (byte) 0x00, (byte) 0x12, (byte) 0x4c, (byte) 0x6a,
827: (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2f,
828: (byte) 0x6c, (byte) 0x61, (byte) 0x6e, (byte) 0x67,
829: (byte) 0x2f, (byte) 0x53, (byte) 0x74, (byte) 0x72,
830: (byte) 0x69, (byte) 0x6e, (byte) 0x67, (byte) 0x3b,
831: (byte) 0x78, (byte) 0x70, (byte) 0x74, (byte) 0x00,
832: (byte) 0x07, (byte) 0x61, (byte) 0x20, (byte) 0x62,
833: (byte) 0x20, (byte) 0x22, (byte) 0x63, (byte) 0x22,
834: (byte) 0x00, (byte) 0x74, (byte) 0x00, (byte) 0x01,
835: (byte) 0x61, (byte) 0x70 };
836:
837: // Self generated: PrivateCredentialPermission("a b \"c\"","read");
838: // Note: Set principals = new LinkedHashSet()
839: // generated using public API
840: public static final byte[] selfForm = { (byte) 0xac, (byte) 0xed,
841: (byte) 0x00, (byte) 0x05, (byte) 0x73, (byte) 0x72,
842: (byte) 0x00, (byte) 0x2f, (byte) 0x6a, (byte) 0x61,
843: (byte) 0x76, (byte) 0x61, (byte) 0x78, (byte) 0x2e,
844: (byte) 0x73, (byte) 0x65, (byte) 0x63, (byte) 0x75,
845: (byte) 0x72, (byte) 0x69, (byte) 0x74, (byte) 0x79,
846: (byte) 0x2e, (byte) 0x61, (byte) 0x75, (byte) 0x74,
847: (byte) 0x68, (byte) 0x2e, (byte) 0x50, (byte) 0x72,
848: (byte) 0x69, (byte) 0x76, (byte) 0x61, (byte) 0x74,
849: (byte) 0x65, (byte) 0x43, (byte) 0x72, (byte) 0x65,
850: (byte) 0x64, (byte) 0x65, (byte) 0x6e, (byte) 0x74,
851: (byte) 0x69, (byte) 0x61, (byte) 0x6c, (byte) 0x50,
852: (byte) 0x65, (byte) 0x72, (byte) 0x6d, (byte) 0x69,
853: (byte) 0x73, (byte) 0x73, (byte) 0x69, (byte) 0x6f,
854: (byte) 0x6e, (byte) 0x49, (byte) 0x55, (byte) 0xdc,
855: (byte) 0x77, (byte) 0x7b, (byte) 0x50, (byte) 0x7f,
856: (byte) 0x4c, (byte) 0x02, (byte) 0x00, (byte) 0x03,
857: (byte) 0x5a, (byte) 0x00, (byte) 0x07, (byte) 0x74,
858: (byte) 0x65, (byte) 0x73, (byte) 0x74, (byte) 0x69,
859: (byte) 0x6e, (byte) 0x67, (byte) 0x4c, (byte) 0x00,
860: (byte) 0x0f, (byte) 0x63, (byte) 0x72, (byte) 0x65,
861: (byte) 0x64, (byte) 0x65, (byte) 0x6e, (byte) 0x74,
862: (byte) 0x69, (byte) 0x61, (byte) 0x6c, (byte) 0x43,
863: (byte) 0x6c, (byte) 0x61, (byte) 0x73, (byte) 0x73,
864: (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4c,
865: (byte) 0x6a, (byte) 0x61, (byte) 0x76, (byte) 0x61,
866: (byte) 0x2f, (byte) 0x6c, (byte) 0x61, (byte) 0x6e,
867: (byte) 0x67, (byte) 0x2f, (byte) 0x53, (byte) 0x74,
868: (byte) 0x72, (byte) 0x69, (byte) 0x6e, (byte) 0x67,
869: (byte) 0x3b, (byte) 0x4c, (byte) 0x00, (byte) 0x0a,
870: (byte) 0x70, (byte) 0x72, (byte) 0x69, (byte) 0x6e,
871: (byte) 0x63, (byte) 0x69, (byte) 0x70, (byte) 0x61,
872: (byte) 0x6c, (byte) 0x73, (byte) 0x74, (byte) 0x00,
873: (byte) 0x0f, (byte) 0x4c, (byte) 0x6a, (byte) 0x61,
874: (byte) 0x76, (byte) 0x61, (byte) 0x2f, (byte) 0x75,
875: (byte) 0x74, (byte) 0x69, (byte) 0x6c, (byte) 0x2f,
876: (byte) 0x53, (byte) 0x65, (byte) 0x74, (byte) 0x3b,
877: (byte) 0x78, (byte) 0x72, (byte) 0x00, (byte) 0x18,
878: (byte) 0x6a, (byte) 0x61, (byte) 0x76, (byte) 0x61,
879: (byte) 0x2e, (byte) 0x73, (byte) 0x65, (byte) 0x63,
880: (byte) 0x75, (byte) 0x72, (byte) 0x69, (byte) 0x74,
881: (byte) 0x79, (byte) 0x2e, (byte) 0x50, (byte) 0x65,
882: (byte) 0x72, (byte) 0x6d, (byte) 0x69, (byte) 0x73,
883: (byte) 0x73, (byte) 0x69, (byte) 0x6f, (byte) 0x6e,
884: (byte) 0xb1, (byte) 0xc6, (byte) 0xe1, (byte) 0x3f,
885: (byte) 0x28, (byte) 0x57, (byte) 0x51, (byte) 0x7e,
886: (byte) 0x02, (byte) 0x00, (byte) 0x01, (byte) 0x4c,
887: (byte) 0x00, (byte) 0x04, (byte) 0x6e, (byte) 0x61,
888: (byte) 0x6d, (byte) 0x65, (byte) 0x74, (byte) 0x00,
889: (byte) 0x12, (byte) 0x4c, (byte) 0x6a, (byte) 0x61,
890: (byte) 0x76, (byte) 0x61, (byte) 0x2f, (byte) 0x6c,
891: (byte) 0x61, (byte) 0x6e, (byte) 0x67, (byte) 0x2f,
892: (byte) 0x53, (byte) 0x74, (byte) 0x72, (byte) 0x69,
893: (byte) 0x6e, (byte) 0x67, (byte) 0x3b, (byte) 0x78,
894: (byte) 0x70, (byte) 0x74, (byte) 0x00, (byte) 0x07,
895: (byte) 0x61, (byte) 0x20, (byte) 0x62, (byte) 0x20,
896: (byte) 0x22, (byte) 0x63, (byte) 0x22, (byte) 0x00,
897: (byte) 0x74, (byte) 0x00, (byte) 0x01, (byte) 0x61,
898: (byte) 0x73, (byte) 0x72, (byte) 0x00, (byte) 0x17,
899: (byte) 0x6a, (byte) 0x61, (byte) 0x76, (byte) 0x61,
900: (byte) 0x2e, (byte) 0x75, (byte) 0x74, (byte) 0x69,
901: (byte) 0x6c, (byte) 0x2e, (byte) 0x4c, (byte) 0x69,
902: (byte) 0x6e, (byte) 0x6b, (byte) 0x65, (byte) 0x64,
903: (byte) 0x48, (byte) 0x61, (byte) 0x73, (byte) 0x68,
904: (byte) 0x53, (byte) 0x65, (byte) 0x74, (byte) 0xd8,
905: (byte) 0x6c, (byte) 0xd7, (byte) 0x5a, (byte) 0x95,
906: (byte) 0xdd, (byte) 0x2a, (byte) 0x1e, (byte) 0x02,
907: (byte) 0x00, (byte) 0x00, (byte) 0x78, (byte) 0x72,
908: (byte) 0x00, (byte) 0x11, (byte) 0x6a, (byte) 0x61,
909: (byte) 0x76, (byte) 0x61, (byte) 0x2e, (byte) 0x75,
910: (byte) 0x74, (byte) 0x69, (byte) 0x6c, (byte) 0x2e,
911: (byte) 0x48, (byte) 0x61, (byte) 0x73, (byte) 0x68,
912: (byte) 0x53, (byte) 0x65, (byte) 0x74, (byte) 0xba,
913: (byte) 0x44, (byte) 0x85, (byte) 0x95, (byte) 0x96,
914: (byte) 0xb8, (byte) 0xb7, (byte) 0x34, (byte) 0x03,
915: (byte) 0x00, (byte) 0x00, (byte) 0x78, (byte) 0x70,
916: (byte) 0x77, (byte) 0x0c, (byte) 0x00, (byte) 0x00,
917: (byte) 0x00, (byte) 0x10, (byte) 0x3f, (byte) 0x40,
918: (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
919: (byte) 0x00, (byte) 0x01, (byte) 0x73, (byte) 0x72,
920: (byte) 0x00, (byte) 0x39, (byte) 0x6a, (byte) 0x61,
921: (byte) 0x76, (byte) 0x61, (byte) 0x78, (byte) 0x2e,
922: (byte) 0x73, (byte) 0x65, (byte) 0x63, (byte) 0x75,
923: (byte) 0x72, (byte) 0x69, (byte) 0x74, (byte) 0x79,
924: (byte) 0x2e, (byte) 0x61, (byte) 0x75, (byte) 0x74,
925: (byte) 0x68, (byte) 0x2e, (byte) 0x50, (byte) 0x72,
926: (byte) 0x69, (byte) 0x76, (byte) 0x61, (byte) 0x74,
927: (byte) 0x65, (byte) 0x43, (byte) 0x72, (byte) 0x65,
928: (byte) 0x64, (byte) 0x65, (byte) 0x6e, (byte) 0x74,
929: (byte) 0x69, (byte) 0x61, (byte) 0x6c, (byte) 0x50,
930: (byte) 0x65, (byte) 0x72, (byte) 0x6d, (byte) 0x69,
931: (byte) 0x73, (byte) 0x73, (byte) 0x69, (byte) 0x6f,
932: (byte) 0x6e, (byte) 0x24, (byte) 0x43, (byte) 0x72,
933: (byte) 0x65, (byte) 0x64, (byte) 0x4f, (byte) 0x77,
934: (byte) 0x6e, (byte) 0x65, (byte) 0x72, (byte) 0xb2,
935: (byte) 0x2e, (byte) 0x56, (byte) 0x16, (byte) 0xb9,
936: (byte) 0x03, (byte) 0x74, (byte) 0x36, (byte) 0x02,
937: (byte) 0x00, (byte) 0x02, (byte) 0x4c, (byte) 0x00,
938: (byte) 0x0e, (byte) 0x70, (byte) 0x72, (byte) 0x69,
939: (byte) 0x6e, (byte) 0x63, (byte) 0x69, (byte) 0x70,
940: (byte) 0x61, (byte) 0x6c, (byte) 0x43, (byte) 0x6c,
941: (byte) 0x61, (byte) 0x73, (byte) 0x73, (byte) 0x74,
942: (byte) 0x00, (byte) 0x12, (byte) 0x4c, (byte) 0x6a,
943: (byte) 0x61, (byte) 0x76, (byte) 0x61, (byte) 0x2f,
944: (byte) 0x6c, (byte) 0x61, (byte) 0x6e, (byte) 0x67,
945: (byte) 0x2f, (byte) 0x53, (byte) 0x74, (byte) 0x72,
946: (byte) 0x69, (byte) 0x6e, (byte) 0x67, (byte) 0x3b,
947: (byte) 0x4c, (byte) 0x00, (byte) 0x0d, (byte) 0x70,
948: (byte) 0x72, (byte) 0x69, (byte) 0x6e, (byte) 0x63,
949: (byte) 0x69, (byte) 0x70, (byte) 0x61, (byte) 0x6c,
950: (byte) 0x4e, (byte) 0x61, (byte) 0x6d, (byte) 0x65,
951: (byte) 0x74, (byte) 0x00, (byte) 0x12, (byte) 0x4c,
952: (byte) 0x6a, (byte) 0x61, (byte) 0x76, (byte) 0x61,
953: (byte) 0x2f, (byte) 0x6c, (byte) 0x61, (byte) 0x6e,
954: (byte) 0x67, (byte) 0x2f, (byte) 0x53, (byte) 0x74,
955: (byte) 0x72, (byte) 0x69, (byte) 0x6e, (byte) 0x67,
956: (byte) 0x3b, (byte) 0x78, (byte) 0x70, (byte) 0x74,
957: (byte) 0x00, (byte) 0x01, (byte) 0x62, (byte) 0x74,
958: (byte) 0x00, (byte) 0x01, (byte) 0x63, (byte) 0x78 };
959: }
|