Index: src/main/java/common/javax/security/auth/Subject.java =================================================================== --- src/main/java/common/javax/security/auth/Subject.java (revision 993327) +++ src/main/java/common/javax/security/auth/Subject.java (working copy) @@ -456,7 +456,7 @@ } } catch (SecurityException e) { buf.delete(offset, buf.length()); - buf.append("\tPrivate Credentials: no accessible information\n"); //$NON-NLS-1$ + buf.append("\tPrivate Credential inaccessible\n"); //$NON-NLS-1$ } return buf.toString(); } @@ -588,7 +588,7 @@ throw new NullPointerException(); } if (permission == _PRINCIPALS && !(Principal.class.isAssignableFrom(o.getClass()))) { - throw new IllegalArgumentException(Messages.getString("auth.0B")); //$NON-NLS-1$ + throw new SecurityException(Messages.getString("auth.0B")); //$NON-NLS-1$ } } @@ -668,7 +668,7 @@ public boolean add(E o) { if (!c.isAssignableFrom(o.getClass())) { - throw new IllegalArgumentException( + throw new SecurityException( Messages.getString("auth.0C", c.getName())); //$NON-NLS-1$ } Index: src/test/java/common/org/apache/harmony/auth/internal/SecurityTest.java =================================================================== --- src/test/java/common/org/apache/harmony/auth/internal/SecurityTest.java (revision 993327) +++ src/test/java/common/org/apache/harmony/auth/internal/SecurityTest.java (working copy) @@ -874,6 +874,7 @@ set.retainAll(null); // BUG Expected: no exceptions + // Spec for Set.retainAll says null pointer should be thrown in this case. if (!testing) { fail("No expected NullPointerException"); } @@ -970,6 +971,7 @@ if (testing) { try { + // RI will allow a null element to be added. set.add(null); // priv/pub credential set: no NullPointerException } catch (NullPointerException e) { @@ -980,8 +982,11 @@ } else { try { set.add(null); - fail("No expected NullPointerException"); + fail("No expected NullPointerException (priv/pub credentials) or SecurityException (principal)"); } catch (NullPointerException e) { + // priv/pub credential set: NullPointerException + } catch (SecurityException e) { + // principal set: SecurityException attempting to add an incompatible object } assertEquals("Size", 0, set.size()); } @@ -991,6 +996,7 @@ if (testing) { try { + // RI will allow a set containing a null element to be added. set.addAll(hash); // priv/pub credentials set: no NullPointerException } catch (NullPointerException e) { @@ -1001,8 +1007,11 @@ } else { try { set.addAll(hash); - fail("No expected NullPointerException"); + fail("No expected NullPointerException (priv/pub credentials) or SecurityException (principal)"); } catch (NullPointerException e) { + // priv/pub credential set: NullPointerException + } catch (SecurityException e) { + // principal set: SecurityException attempting to add an incompatible object } assertEquals("Size", 0, set.size()); } @@ -1089,14 +1098,11 @@ try { set.add(iElement); - fail("No expected ClassCastException or IllegalArgumentException"); - } catch (ClassCastException e) { - } catch (IllegalArgumentException e) { + fail("No expected Security Exception"); } catch (SecurityException e) { - if (!testing) { - // all sets - SecurityException - throw e; - } + // all sets - SecurityException + // e.g. attempting to add an object which is not an instance of + // javax.security.auth.SubjectTest$MyClass1 } assertEquals("Size", 0, set.size()); } @@ -1112,11 +1118,12 @@ set.add(new Object()); if (!testing) { - // all Class sets - no exception - fail("No expected ClassCastException or IllegalArgumentException"); + // all Class sets - no exception. + // Spec not clear + // For consistency could be the same SecurityException found with add invalid object. + fail("No expected SecurityException"); } - } catch (ClassCastException e) { - } catch (IllegalArgumentException e) { + } catch (SecurityException e) { } } @@ -1125,14 +1132,9 @@ try { set.addAll(iHash); - fail("No expected ClassCastException or IllegalArgumentException"); - } catch (ClassCastException e) { - } catch (IllegalArgumentException e) { + fail("No expected SecurityException"); } catch (SecurityException e) { - if (!testing) { - // all sets - SecurityException - throw e; - } + // all sets - SecurityException } assertEquals("Size", 0, set.size()); } @@ -1448,6 +1450,8 @@ @Override public void testRetainAll_NullParameter_EmptySet() { setReadOnly(); + // Spec for Set.retainAll says null pointer should be thrown in this case. + // RI behaves different to spec super.testRetainAll_NullParameter_EmptySet(); } @@ -1765,6 +1769,8 @@ @Override public void testRetainAll_NullParameter_EmptySet() { setSecure(); + // Spec for Set.retainAll says null pointer should be thrown in this case. + // RI behaves different to spec super.testRetainAll_NullParameter_EmptySet(); } Index: src/test/java/common/javax/security/auth/SubjectTest.java =================================================================== --- src/test/java/common/javax/security/auth/SubjectTest.java (revision 993327) +++ src/test/java/common/javax/security/auth/SubjectTest.java (working copy) @@ -51,6 +51,8 @@ */ public class SubjectTest extends SecurityTest { + + private static final String TOSTRING_EXPECTED = "Private Credential inaccessible"; private static final Principal principal = new Principal() { public String getName() { @@ -262,10 +264,10 @@ new Subject(false, hash, new HashSet(), new HashSet()); if (!testing) { - // possible to add 'null' principal via constructor - fail("No expected IllegalArgumentException"); + // possible to add 'null' principal via constructor in RI + fail("No expected SecurityException"); } - } catch (IllegalArgumentException e) { + } catch (SecurityException e) { } } @@ -474,6 +476,8 @@ denyPermission(new SecurityPermission("createAccessControlContext")); try { + // Test fails on RI with a java.security.AccessControlException, despite it being caught + Subject.doAs(subject, emptyPAction); fail("No expected AccessControlException"); } catch (AccessControlException e) { @@ -811,6 +815,8 @@ (new Subject(false, h1, h2, h3)).hashCode(); if (!testing) { + // RI does not provide AccessControlException when calculating a hash code. + // Spec says hashCode should throw SecurityException fail("No expected AccessControlException"); } } catch (AccessControlException e) { @@ -837,9 +843,14 @@ } public final void testToString() { - //FIXME grantMode(); - //denyPermission(new PrivateCredentialPermission("* * \"*\"", "read")); - //System.out.println((new Subject(false, h1, h2, h3)).toString()); + + denyPermission(new PrivateCredentialPermission("* * \"*\"", "read")); + String subjectString = new Subject(false, h1, h2, h3).toString(); + + if (!subjectString.trim().endsWith(TOSTRING_EXPECTED)) { + fail("Subject to string does not end with " + TOSTRING_EXPECTED + ". Actual: " + subjectString.trim()); + } + } public final void testSerialization() throws Exception { @@ -1107,9 +1118,9 @@ try { sIn.readObject(); if (!testing) { - fail("No expected IllegalArgumentException"); + fail("No expected SecurityException"); } - } catch (IllegalArgumentException e) { + } catch (SecurityException e) { } } @@ -1312,9 +1323,9 @@ try { sIn.readObject(); if (!testing) { - fail("No expected IllegalArgumentException"); + fail("No expected SecurityException"); } - } catch (IllegalArgumentException e) { + } catch (SecurityException e) { } finally { sIn.close(); } @@ -1434,7 +1445,6 @@ private final Subject subject = new Subject(); /* - * FIXME??? presence of unaccessible element * forbids all operations except adding new elements */ public void testForbiddenElement() { @@ -1478,7 +1488,17 @@ assertEquals(e, PrivateCredentialPermission.class); } - assertTrue(set.equals(set)); + try { + assertTrue(set.equals(set)); + if (testing) { + // RI will throw exception if same set is compared against itself + fail("No expected AccessControlException"); + } + } catch (AccessControlException e) { + assertEquals(e, PrivateCredentialPermission.class); + } + + assertFalse(set.equals(new HashSet())); try { // set with equal size initiates iteration @@ -1545,6 +1565,7 @@ } catch (NoSuchElementException e) { } catch (IndexOutOfBoundsException e) { if (!testing) { + // RI has unexpected IndexOutOfBoundsException throw e; } } @@ -1709,9 +1730,9 @@ } // subject hash partial permissions (only for MyClass1 class) - grantPermission(getPermission(MyClass1.class.getName(), hash)); - - // FIXME why security exception is thrown? + grantPermission(getPermission(MyClass1.class.getName(), hash)); + + // RI throws security exception although // the spec. require permissions for requested class only try { subject.getPrivateCredentials(MyClass1.class);