Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4183136

java.security.Identity violates equals/hashcode contract

XMLWordPrintable

    • beta
    • sparc
    • solaris_2.5



      Name: mgC56079 Date: 10/21/98



      ======================================================================

      Name: mgC56079 Date: 11/23/98


      java.security.Identity violates equals/hashcode contract
      (The JLS, 20.1.4):
           The general contract of <code>hashCode</code> is:
             [skip]
           If two objects are equal according to the <tt>equals(Object)</tt>
           method, then calling the <code>hashCode</code> method on each of
           the two objects must produce the same integer result.

      ==== Here is the test demonstrating the bug (IdentityTest.java) ====
      import java.security.*;

      public class IdentityTest {

          public static void main(String[] args) throws KeyManagementException {
              PublicKey pk=new MyPublicKey();
              Identity i1=new MyIdentity("identity",new MyIdentityScope(""));
              i1.setPublicKey(pk);
              Identity i2=new MyIdentity("identity",new MyIdentityScope("IdentityScope"));
              i2.setPublicKey(pk);
              System.out.println(i1.equals(i2));
              System.out.println(i1.hashCode()==i2.hashCode());
              if ( (i1.equals(i2)) == (i1.hashCode()==i2.hashCode()) )
                  System.out.println("PASSED");
              else
                  System.out.println("FAILED");

          }

      }

      class MyIdentity extends Identity {
          public MyIdentity(String name, IdentityScope is) throws KeyManagementException {
              super(name, is);
          }
      }

      class MyPublicKey implements PublicKey {
          public String getAlgorithm() {
              return null;
          }
          public String getFormat() {
              return null;
          }

          public byte[] getEncoded() {
              return null;
          }

      }

      class MyIdentityScope extends IdentityScope {
          public MyIdentityScope(String name) {
      super(name);
          }

          public int size() {
              return 0;
          }

          public Identity getIdentity(String name) {
              return null;
          }

          public Identity getIdentity(PublicKey key) {
              return null;
          }

          public void addIdentity(Identity identity) {
          }

          public void removeIdentity(Identity identity) {
          }

          public java.util.Enumeration identities() {
              return null;
          }


      }
      ==== Sample run (jdk1.2FCS-O) ====
      % java IdentityTest
      true
      false
      FAILED

      ======================================================================

            gellisonsunw Gary Ellison (Inactive)
            mgorshen Mikhail Gorshenev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: