public void testOverwrite() { /* foreach */ foreach (PibDataFixture2 fixture in pibImpls) { PibImpl pib = fixture.pib; // Check for id1Key1, which should not exist. pib.removeIdentity(fixture.id1); Assert.AssertEquals(false, pib.hasKey(fixture.id1Key1Name)); // Add id1Key1. pib.addKey(fixture.id1, fixture.id1Key1Name, fixture.id1Key1.buf()); Assert.AssertEquals(true, pib.hasKey(fixture.id1Key1Name)); Blob keyBits = pib.getKeyBits(fixture.id1Key1Name); Assert.AssertTrue(keyBits.equals(fixture.id1Key1)); // To check overwrite, add a key with the same name. pib.addKey(fixture.id1, fixture.id1Key1Name, fixture.id1Key2.buf()); Blob keyBits2 = pib.getKeyBits(fixture.id1Key1Name); Assert.AssertTrue(keyBits2.equals(fixture.id1Key2)); // Check for id1Key1Cert1, which should not exist. pib.removeIdentity(fixture.id1); Assert.AssertEquals(false, pib.hasCertificate(fixture.id1Key1Cert1.getName())); // Add id1Key1Cert1. pib.addKey(fixture.id1, fixture.id1Key1Name, fixture.id1Key1.buf()); pib.addCertificate(fixture.id1Key1Cert1); Assert.AssertEquals(true, pib.hasCertificate(fixture.id1Key1Cert1.getName())); CertificateV2 cert = pib.getCertificate(fixture.id1Key1Cert1 .getName()); Assert.AssertTrue(cert.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())); // Create a fake certificate with the same name. CertificateV2 cert2 = fixture.id1Key2Cert1; cert2.setName(fixture.id1Key1Cert1.getName()); cert2.setSignature(fixture.id1Key2Cert1.getSignature()); pib.addCertificate(cert2); CertificateV2 cert3 = pib.getCertificate(fixture.id1Key1Cert1 .getName()); Assert.AssertTrue(cert3.wireEncode().equals(cert2.wireEncode())); // Check that both the key and certificate are overwritten. Blob keyBits3 = pib.getKeyBits(fixture.id1Key1Name); Assert.AssertTrue(keyBits3.equals(fixture.id1Key2)); } }
public void testCertificateManagement() { /* foreach */ foreach (PibDataFixture2 fixture in pibImpls) { PibImpl pib = fixture.pib; // There is no default setting. This should throw an Error. try { pib.getDefaultCertificateOfKey(fixture.id1Key1Name); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex) { } catch (Exception ex_0) { Assert.Fail("Did not throw the expected exception"); } // Check for id1Key1Cert1, which should not exist. Neither should id1 or id1Key1. Assert.AssertEquals(false, pib.hasCertificate(fixture.id1Key1Cert1.getName())); Assert.AssertEquals(false, pib.hasIdentity(fixture.id1)); Assert.AssertEquals(false, pib.hasKey(fixture.id1Key1Name)); // Add id1Key1Cert1, which should be the default. // id1 and id1Key1 should be added implicitly. pib.addCertificate(fixture.id1Key1Cert1); Assert.AssertEquals(true, pib.hasCertificate(fixture.id1Key1Cert1.getName())); Assert.AssertEquals(true, pib.hasIdentity(fixture.id1)); Assert.AssertEquals(true, pib.hasKey(fixture.id1Key1Name)); Assert.AssertTrue(pib.getCertificate(fixture.id1Key1Cert1.getName()) .wireEncode().equals(fixture.id1Key1Cert1.wireEncode())); try { pib.getDefaultCertificateOfKey(fixture.id1Key1Name); } catch (Exception ex_1) { Assert.Fail("Unexpected exception: " + ex_1.Message); } // Use the wire encoding to check equivalence. Assert.AssertTrue(fixture.id1Key1Cert1.wireEncode().equals( pib.getDefaultCertificateOfKey(fixture.id1Key1Name) .wireEncode())); // Add id1Key1Cert2, which should not be the default. pib.addCertificate(fixture.id1Key1Cert2); Assert.AssertEquals(true, pib.hasCertificate(fixture.id1Key1Cert2.getName())); Assert.AssertTrue(fixture.id1Key1Cert1.wireEncode().equals( pib.getDefaultCertificateOfKey(fixture.id1Key1Name) .wireEncode())); // Explicitly set id1Key1Cert2 as the default. pib.setDefaultCertificateOfKey(fixture.id1Key1Name, fixture.id1Key1Cert2.getName()); Assert.AssertTrue(fixture.id1Key1Cert2.wireEncode().equals( pib.getDefaultCertificateOfKey(fixture.id1Key1Name) .wireEncode())); // Set a non-existing certificate as the default. This should throw an Error. try { pib.setDefaultCertificateOfKey(fixture.id1Key1Name, new Name( "/non-existing")); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_2) { } catch (Exception ex_3) { Assert.Fail("Did not throw the expected exception"); } // Remove id1Key1Cert2, which should not have a default certificate. pib.removeCertificate(fixture.id1Key1Cert2.getName()); Assert.AssertEquals(false, pib.hasCertificate(fixture.id1Key1Cert2.getName())); try { pib.getCertificate(fixture.id1Key1Cert2.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_4) { } catch (Exception ex_5) { Assert.Fail("Did not throw the expected exception"); } try { pib.getDefaultCertificateOfKey(fixture.id1Key1Name); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_6) { } catch (Exception ex_7) { Assert.Fail("Did not throw the expected exception"); } // Add id1Key1Cert2, which should be the default. pib.addCertificate(fixture.id1Key1Cert2); try { pib.getCertificate(fixture.id1Key1Cert1.getName()); } catch (Exception ex_8) { Assert.Fail("Unexpected exception: " + ex_8.Message); } Assert.AssertTrue(fixture.id1Key1Cert2.wireEncode().equals( pib.getDefaultCertificateOfKey(fixture.id1Key1Name) .wireEncode())); // Get all certificates, which should have id1Key1Cert1 and id1Key1Cert2. HashedSet <Name> certNames = pib .getCertificatesOfKey(fixture.id1Key1Name); Assert.AssertEquals(2, certNames.Count); Assert.AssertTrue(ILOG.J2CsMapping.Collections.Collections.Contains(fixture.id1Key1Cert1.getName(), certNames)); Assert.AssertTrue(ILOG.J2CsMapping.Collections.Collections.Contains(fixture.id1Key1Cert2.getName(), certNames)); // Remove id1Key1, which should remove all the certificates. pib.removeKey(fixture.id1Key1Name); certNames = pib.getCertificatesOfKey(fixture.id1Key1Name); Assert.AssertEquals(0, certNames.Count); } }