/** * @see java.security.IdentityScope#addIdentity(java.security.Identity) */ public synchronized void addIdentity(Identity identity) throws KeyManagementException { if (identity == null) { throw new NullPointerException("identity == null"); } String name = identity.getName(); if (names.containsKey(name)) { throw new KeyManagementException("name '" + name + "' is already used"); } PublicKey key = identity.getPublicKey(); if (key != null && keys.containsKey(key)) { throw new KeyManagementException("key '" + key + "' is already used"); } names.put(name, identity); if (key != null) { keys.put(key, identity); } }
/** * @tests java.security.IdentityScope#addIdentity(java.security.Identity) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "addIdentity", args = {java.security.Identity.class} ) public void test_addIdentityLjava_security_Identity() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass("id1"); id.setPublicKey(pubKey); sub.addIdentity(id); try { Identity id2 = new IdentitySubclass("id2"); id2.setPublicKey(pubKey); sub.addIdentity(id2); fail("KeyManagementException should have been thrown"); } catch (KeyManagementException e) { // Expected } }
/** * @tests java.security.IdentityScope#removeIdentity(java.security.Identity) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "removeIdentity", args = {java.security.Identity.class} ) public void test_removeIdentityLjava_security_Identity() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); id.setPublicKey(pubKey); sub.addIdentity(id); sub.removeIdentity(id); try { sub.removeIdentity(id); fail("KeyManagementException should have been thrown"); } catch (KeyManagementException e) { // expected } }
/** * @tests java.security.IdentityScope#identities() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "identities", args = {} ) public void test_identities() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); id.setPublicKey(pubKey); sub.addIdentity(id); Enumeration<Identity> en = sub.identities(); assertTrue("Wrong object contained in identities", en.nextElement() .equals(id)); assertTrue("Contains too many elements", !en.hasMoreElements()); }
/** * @tests java.security.IdentityScope#getIdentity(java.security.PublicKey) */ @TestTargetNew( level = TestLevel.COMPLETE, method = "getIdentity", args = {java.security.PublicKey.class} ) public void test_getIdentityLjava_security_PublicKey() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); id.setPublicKey(pubKey); sub.addIdentity(id); Identity returnedId = sub.getIdentity(pubKey); assertEquals("Test 1: Returned Identity not the same as the added one;", id, returnedId); assertNull("Test 2: Null value expected.", sub.getIdentity((PublicKey) null)); PublicKey anotherKey = KeyPairGenerator.getInstance("DSA").genKeyPair().getPublic(); assertNull("Test 3: Null value expected.", sub.getIdentity(anotherKey)); }
/** * @tests java.security.IdentityScope#getIdentity(java.lang.String) */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "getIdentity", args = {java.lang.String.class} ) public void test_getIdentityLjava_lang_String() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass("test"); id.setPublicKey(pubKey); sub.addIdentity(id); Identity returnedId = sub.getIdentity("test"); assertEquals("Returned Identity not the same as the added one", id, returnedId); }
/** * @tests java.security.IdentityScope#toString() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "toString", args = {} ) public void test_toString() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); id.setPublicKey(pubKey); sub.addIdentity(id); assertNotNull("toString returned a null", sub.toString()); assertTrue("Not a valid String ", sub.toString().length() > 0); }
/** * @tests java.security.Identity#Identity(java.lang.String) */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "Identity", args = {java.lang.String.class} ) public void test_ConstructorLjava_lang_String() { String[] str = {"test", "", null}; IdentitySubclass is; for (int i = 0; i < str.length; i++) { try { is = new IdentitySubclass(str[i]); assertNotNull(is); assertTrue(is instanceof Identity); } catch (Exception e) { fail("Unexpected exception for Identity(java.lang.String) with parameter " + str[i]); } } }
/** * @tests java.security.Identity#identityEquals(java.security.Identity) */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "Method identityEquals(java.security.Identity) is not tested", method = "identityEquals", args = {java.security.Identity.class} ) public void test_identityEqualsLjava_security_Identity() throws Exception { IdentitySubclass sub = new IdentitySubclass("test", null); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert[] = new X509Certificate[1]; cert[0] = (X509Certificate) cf.generateCertificate(certArray); sub.setPublicKey(cert[0].getPublicKey()); CertificateImpl certImpl = new CertificateImpl(cert[0]); sub.addCertificate(certImpl); IdentitySubclass sub2 = new IdentitySubclass("test", null); sub2.setPublicKey(cert[0].getPublicKey()); assertEquals("the two Identity objects are not identity-equal", sub2, sub); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "equals", args = {java.lang.Object.class} ) public void testEquals() throws Exception { IdentityStub 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)); }
/** * verify addCertificate(Certificate certificate) adds a certificate for this identity. * If the identity has a public key, the public key in the certificate must be the same * */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "addCertificate", args = {java.security.Certificate.class} ) public void testAddCertificate1() throws Exception { Identity i = new IdentityStub("iii"); PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", new byte[]{1,2,3,4,5}); i.setPublicKey(pk1); // try with the same key CertificateStub c1 = new CertificateStub("fff", null, null, pk1); i.addCertificate(c1); assertSame(c1, i.certificates()[0]); // try Certificate with different key try { i.addCertificate(new CertificateStub("ccc", null, null, new PublicKeyStub("k2", "fff", new byte[]{6,7,8,9,0}))); fail("KeyManagementException should be thrown"); } catch (KeyManagementException ok) {} }
/** * verify addCertificate(Certificate certificate) adds a certificate for this identity. * if the identity does not have a public key, the identity's public key is set to be that specified in the certificate. */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "addCertificate", args = {java.security.Certificate.class} ) public void testAddCertificate2() throws Exception { Identity i = new IdentityStub("iii"); PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); CertificateStub c1 = new CertificateStub("fff", null, null, pk1); i.addCertificate(c1); assertSame(c1, i.certificates()[0]); assertSame(pk1, i.getPublicKey()); }
/** * verify certificates() returns a copy of all certificates for this identity */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "certificates", args = {} ) public void testCertificates() throws Exception { Identity i = new IdentityStub("iii"); PublicKeyStub pk1 = new PublicKeyStub("kkk", "fff", null); CertificateStub c1 = new CertificateStub("fff", null, null, pk1); CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); i.addCertificate(c1); i.addCertificate(c2); java.security.Certificate[] s = i.certificates(); assertEquals(2, s.length); assertTrue(c1.equals(s[0]) || c2.equals(s[0])); assertTrue(c1.equals(s[1]) || c2.equals(s[1])); s[0] = null; s[1] = null; // check that the copy was modified s = i.certificates(); assertEquals(2, s.length); assertTrue(c1.equals(s[0]) || c2.equals(s[0])); assertTrue(c1.equals(s[1]) || c2.equals(s[1])); }
/** * verify Identity.getScope() returns identity's scope */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getScope", args = {} ) 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()); }
/** * * verify Identity.setPublicKey() removes old key and all identity's certificates * */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "setPublicKey", args = {java.security.PublicKey.class} ) public void testSetPublicKey4() throws Exception { Identity i = new IdentityStub("testSetPublicKey4"); PublicKeyStub pk1 = new PublicKeyStub("kkk", "Identity.testSetPublicKey4", null); CertificateStub c1 = new CertificateStub("fff", null, null, pk1); CertificateStub c2 = new CertificateStub("zzz", null, null, pk1); i.addCertificate(c1); i.addCertificate(c2); assertEquals(2, i.certificates().length); assertSame(pk1, i.getPublicKey()); PublicKeyStub pk2 = new PublicKeyStub("zzz", "Identity.testSetPublicKey4", null); i.setPublicKey(pk2); assertSame(pk2, i.getPublicKey()); assertEquals(0, i.certificates().length); }
@TestTargets({ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Both method were verified", method = "getInfo", args = {} ), @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "Both method were verified", method = "setInfo", args = {java.lang.String.class} ) }) public void testGetInfo() { Identity i = new IdentityStub("testGetInfo"); i.setInfo("some info"); assertEquals("some info", i.getInfo()); }
/** * @see java.security.IdentityScope#addIdentity(java.security.Identity) */ public synchronized void addIdentity(Identity identity) throws KeyManagementException { if (identity == null) { throw new NullPointerException(Messages.getString("security.92")); //$NON-NLS-1$ } String name = identity.getName(); if (names.containsKey(name)) { throw new KeyManagementException(Messages.getString("security.93", name)); //$NON-NLS-1$ } PublicKey key = identity.getPublicKey(); if (key != null && keys.containsKey(key)) { throw new KeyManagementException(Messages.getString("security.94", key)); //$NON-NLS-1$ } names.put(name, identity); if (key != null) { keys.put(key, identity); } }
/** * @tests java.security.IdentityScope#addIdentity(java.security.Identity) */ public void test_addIdentityLjava_security_Identity() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass("id1"); id.setPublicKey(pubKey); sub.addIdentity(id); try { Identity id2 = new IdentitySubclass("id2"); id2.setPublicKey(pubKey); sub.addIdentity(id2); fail("KeyManagementException should have been thrown"); } catch (KeyManagementException e) { // Expected } }
public void testIdentities() throws Exception { // SystemScope ss = new SystemScope("SystemScope"); java.security.Identity aaa = new IdentityScopeStub("aaa"); java.security.Identity bbb = new IdentityScopeStub("bbb"); ss.addIdentity(aaa); ss.addIdentity(bbb); boolean hasAaa = false, hasBbb = false; Enumeration e = ss.identities(); while (e.hasMoreElements()){ Object i = e.nextElement(); if (!hasAaa) hasAaa = (i==aaa); if (!hasBbb) hasBbb = (i==bbb); } assertTrue(hasAaa && hasBbb); }
/** * @see IdentityScope#addIdentity(Identity) */ public synchronized void addIdentity(Identity identity) throws KeyManagementException { if (identity == null) { throw new NullPointerException(Messages.getString("security.92")); //$NON-NLS-1$ } String name = identity.getName(); if (names.containsKey(name)) { throw new KeyManagementException(Messages.getString("security.93", name)); //$NON-NLS-1$ } PublicKey key = identity.getPublicKey(); if (key != null && keys.containsKey(key)) { throw new KeyManagementException(Messages.getString("security.94", key)); //$NON-NLS-1$ } names.put(name, identity); if (key != null) { keys.put(key, identity); } }
/** * @see java.security.IdentityScope#getIdentity(java.lang.String) */ public synchronized Identity getIdentity(String name) { if (name == null) { throw new NullPointerException(); } return (Identity) names.get(name); }
/** * @see java.security.IdentityScope#getIdentity(java.security.PublicKey) */ public synchronized Identity getIdentity(PublicKey key) { if (key == null) { return null; } return (Identity) keys.get(key); }
/** * @see java.security.IdentityScope#removeIdentity(java.security.Identity) */ public synchronized void removeIdentity(Identity identity) throws KeyManagementException { //Exception caught = null; if (identity == null) { throw new NullPointerException("identity == null"); } String name = identity.getName(); if (name == null) { throw new NullPointerException("name == null"); } boolean contains = names.containsKey(name); names.remove(name); PublicKey key = identity.getPublicKey(); if (key != null) { contains = contains || keys.containsKey(key); keys.remove(key); } if (!contains) { throw new KeyManagementException("identity not found"); } }
public IdentityScopeSubclass(String name, PublicKey pk) { super(name); try { setPublicKey(pk); } catch (KeyManagementException e) { } identities = new Hashtable<Identity, Identity>(); }
public Identity getIdentity(String name) { Enumeration<Identity> en = identities(); while (en.hasMoreElements()) { Identity current = (Identity) en.nextElement(); if (current.getName().equals(name)) return current; } return null; }
public Identity getIdentity(PublicKey pk) { Enumeration<Identity> en = identities(); while (en.hasMoreElements()) { Identity current = (Identity) en.nextElement(); if (current.getPublicKey() == pk) return current; } return null; }
public void addIdentity(Identity id) throws KeyManagementException { if (identities.containsKey(id)) throw new KeyManagementException( "This Identity is already contained in the scope"); if (getIdentity(id.getPublicKey()) != null) throw new KeyManagementException( "This Identity's public key already exists in the scope"); identities.put(id, id); }
/** * @tests java.security.IdentityScope#size() */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "size", args = {} ) public void test_size() throws Exception { IdentityScopeSubclass sub = new IdentityScopeSubclass("test", new IdentityScopeSubclass()); Identity id = new IdentitySubclass(); id.setPublicKey(pubKey); sub.addIdentity(id); assertEquals("Wrong size", 1, sub.size()); }
/** * verify Identity(String, IdentityScope) creates instance with given name and in give scope */ @TestTargetNew( level = TestLevel.PARTIAL, notes = "KeyManagementException checking missed. Null parameters are not checked.", method = "Identity", args = {java.lang.String.class, java.security.IdentityScope.class} ) 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())); }
/** * verify Identity.identityEquals(Identity) return true, only if names and public keys are equal */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "identityEquals", args = {java.security.Identity.class} ) public void testIdentityEquals() throws Exception { String name = "nnn"; PublicKey pk = new PublicKeyStub("aaa", "fff", new byte[]{1,2,3,4,5}); IdentityStub 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)); }
/** * * verify Identity.setPublicKey() throws KeyManagementException if key is invalid * */ @TestTargetNew( level = TestLevel.PARTIAL_COMPLETE, notes = "", method = "setPublicKey", args = {java.security.PublicKey.class} ) 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) {} }
/** * verify Identity.getPublicKey() returns public key */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getPublicKey", args = {} ) public void testGetPublicKey() throws Exception { Identity i = new IdentityStub("testGetPublicKey"); assertNull(i.getPublicKey()); PublicKey pk = new PublicKeyStub("kkk", "Identity.testGetPublicKey", null); i.setPublicKey(pk); assertSame(pk, i.getPublicKey()); }
@TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getName", args = {} ) public void testGetName() { Identity i = new IdentityStub("testGetName"); assertEquals ("testGetName", i.getName()); }