示例#1
0
 /**
  * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal
  */
 public void testIdentityEquals() throws Exception {
   String name = "nnn";
   PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[] {1, 2, 3, 4, 5});
   Identity i = new IdentityStub(name);
   i.setPublicKey(pk);
   Object[] value = {
     // null, Boolean.FALSE,
     // new Object(), Boolean.FALSE,
     new IdentityStub("111"),
     Boolean.FALSE,
     new IdentityStub(name),
     Boolean.FALSE,
     new IdentityStub(name, IdentityScope.getSystemScope()),
     Boolean.FALSE,
     i,
     Boolean.TRUE,
     new IdentityStub(name, pk),
     Boolean.TRUE
   };
   for (int k = 0; k < value.length; k += 2) {
     assertEquals(value[k + 1], new Boolean(i.identityEquals((Identity) value[k])));
     if (Boolean.TRUE.equals(value[k + 1])) assertEquals(i.hashCode(), value[k].hashCode());
   }
   Identity i2 = IdentityScope.getSystemScope().getIdentity(name);
   i2.setPublicKey(pk);
   assertTrue(i.identityEquals(i2));
 }
示例#2
0
 /** verify Identity(String, IdentityScope) creates instance with given name and in give scope */
 public void testIdentityStringIdentityScope() throws Exception {
   IdentityScope s = IdentityScope.getSystemScope();
   Identity i = new IdentityStub("iii2", s);
   assertNotNull(i);
   assertEquals("iii2", i.getName());
   assertSame(s, i.getScope());
   assertSame(i, s.getIdentity(i.getName()));
 }
 /**
  * Creates a new instance of {@code Identity} with the specified name and the scope of this {@code
  * Identity}.
  *
  * @param name the name of this {@code Identity}.
  * @param scope the {@code IdentityScope} of this {@code Identity}.
  * @throws KeyManagementException if an {@code Identity} with the same name is already present in
  *     the specified scope.
  */
 public Identity(String name, IdentityScope scope) throws KeyManagementException {
   this(name);
   if (scope != null) {
     scope.addIdentity(this);
     this.scope = scope;
   }
 }
 /**
  * Returns a string containing a concise, human-readable description of the this {@code Identity}
  * including its name and its scope.
  *
  * @return a printable representation for this {@code Identity}.
  */
 @Override
 public String toString() {
   String s = (this.name == null ? "" : this.name);
   if (scope != null) {
     s += " [" + scope.getName() + "]";
   }
   return s;
 }
示例#5
0
  /** verify Identity.getScope() returns identity's scope */
  public void testGetScope() throws Exception {
    Identity i = new IdentityStub("testGetScope");
    assertNull(i.getScope());
    IdentityScope s = IdentityScope.getSystemScope();

    Identity i2 = new IdentityStub("testGetScope2", s);
    assertSame(s, i2.getScope());
  }
示例#6
0
 /** verify Identity.setPublicKey() throws KeyManagementException if key is invalid */
 public void testSetPublicKey2() throws Exception {
   Identity i2 = new IdentityStub("testSetPublicKey2_2", IdentityScope.getSystemScope());
   new PublicKeyStub("kkk", "testSetPublicKey2", new byte[] {1, 2, 3, 4, 5});
   try {
     i2.setPublicKey(null);
     // fail("KeyManagementException should be thrown - key is null");
   } catch (KeyManagementException ok) {
   }
 }
 /**
  * Returns the hash code value for this {@code Identity}. Returns the same hash code for {@code
  * Identity}s that are equal to each other as required by the general contract of {@link
  * Object#hashCode}.
  *
  * @return the hash code value for this {@code Identity}.
  * @see Object#equals(Object)
  * @see Identity#equals(Object)
  */
 @Override
 public int hashCode() {
   int hash = 0;
   if (name != null) {
     hash += name.hashCode();
   }
   if (scope != null) {
     hash += scope.hashCode();
   }
   return hash;
 }
 /**
  * Sets the specified {@code PublicKey} to this {@code Identity}.
  *
  * @param key the {@code PublicKey} to be set.
  * @throws KeyManagementException if another {@code Identity} in the same scope as this {@code
  *     Identity} already has the same {@code PublicKey}.
  */
 public void setPublicKey(PublicKey key) throws KeyManagementException {
   // this check does not always work
   if ((scope != null) && (key != null)) {
     Identity i = scope.getIdentity(key);
     // System.out.println("###DEBUG## Identity: "+i);
     if ((i != null) && (i != this)) {
       throw new KeyManagementException("key already used in scope");
     }
   }
   this.publicKey = key;
   certificates = null;
 }
示例#9
0
  public void testEquals() throws Exception {
    Identity i1 = new IdentityStub("testEquals");
    Object value[] = {
      null,
      Boolean.FALSE,
      new Object(),
      Boolean.FALSE,
      i1,
      Boolean.TRUE,
      new IdentityStub(i1.getName()),
      Boolean.TRUE
    };

    for (int k = 0; k < value.length; k += 2) {
      assertEquals(value[k + 1], new Boolean(i1.equals(value[k])));
      if (Boolean.TRUE.equals(value[k + 1])) assertEquals(i1.hashCode(), value[k].hashCode());
    }
    // check other cases
    Identity i2 = new IdentityStub("testEquals", IdentityScope.getSystemScope());
    assertEquals(i1.identityEquals(i2), i1.equals(i2));
    Identity i3 = new IdentityStub("testEquals3");
    assertEquals(i1.identityEquals(i3), i1.equals(i3));
  }
示例#10
0
 /** verify Identity.toString(boolean) return string representation of identity */
 public void testToStringboolean() throws Exception {
   new IdentityStub("aaa").toString(false);
   new IdentityStub("aaa2", IdentityScope.getSystemScope()).toString(false);
   new IdentityStub("bbb").toString(true);
   new IdentityStub("bbb2", IdentityScope.getSystemScope()).toString(true);
 }