public void testCertificateOperation() { PibMemory pibImpl = new PibMemory(); PibKeyImpl key11 = new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl); try { new PibKeyImpl(fixture.id1Key1Name, pibImpl); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex.Message); } // The key should not have any certificates. Assert.AssertEquals(0, key11.getCertificates_().size()); // Getting a non-existing certificate should throw Pib.Error. try { key11.getCertificate(fixture.id1Key1Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_0) { } catch (Exception ex_1) { Assert.Fail("Did not throw the expected exception"); } // Getting the non-existing default certificate should throw Pib.Error. try { key11.getDefaultCertificate(); 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"); } // Setting a non-existing certificate as the default should throw Pib.Error. try { key11.setDefaultCertificate(fixture.id1Key1Cert1.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"); } // Add a certificate. key11.addCertificate(fixture.id1Key1Cert1); try { key11.getCertificate(fixture.id1Key1Cert1.getName()); } catch (Exception ex_6) { Assert.Fail("Unexpected exception: " + ex_6.Message); } // The new certificate becomes the default when there was no default. try { key11.getDefaultCertificate(); } catch (Exception ex_7) { Assert.Fail("Unexpected exception: " + ex_7.Message); } CertificateV2 defaultCert0 = key11.getDefaultCertificate(); Assert.AssertTrue(fixture.id1Key1Cert1.getName() .equals(defaultCert0.getName())); // Use the wire encoding to check equivalence. Assert.AssertTrue(fixture.id1Key1Cert1.wireEncode().equals( defaultCert0.wireEncode())); // Remove the certificate. key11.removeCertificate(fixture.id1Key1Cert1.getName()); try { key11.getCertificate(fixture.id1Key1Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_8) { } catch (Exception ex_9) { Assert.Fail("Did not throw the expected exception"); } try { key11.getDefaultCertificate(); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_10) { } catch (Exception ex_11) { Assert.Fail("Did not throw the expected exception"); } // Set the default certificate directly. try { key11.setDefaultCertificate(fixture.id1Key1Cert1); } catch (Exception ex_12) { Assert.Fail("Unexpected exception: " + ex_12.Message); } try { key11.getDefaultCertificate(); } catch (Exception ex_13) { Assert.Fail("Unexpected exception: " + ex_13.Message); } try { key11.getCertificate(fixture.id1Key1Cert1.getName()); } catch (Exception ex_14) { Assert.Fail("Unexpected exception: " + ex_14.Message); } // Check the default cert. CertificateV2 defaultCert1 = key11.getDefaultCertificate(); Assert.AssertTrue(fixture.id1Key1Cert1.getName() .equals(defaultCert1.getName())); Assert.AssertTrue(defaultCert1.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())); // Add another certificate. key11.addCertificate(fixture.id1Key1Cert2); Assert.AssertEquals(2, key11.getCertificates_().size()); // Set the default certificate using a name. try { key11.setDefaultCertificate(fixture.id1Key1Cert2.getName()); } catch (Exception ex_15) { Assert.Fail("Unexpected exception: " + ex_15.Message); } try { key11.getDefaultCertificate(); } catch (Exception ex_16) { Assert.Fail("Unexpected exception: " + ex_16.Message); } CertificateV2 defaultCert2 = key11.getDefaultCertificate(); Assert.AssertTrue(fixture.id1Key1Cert2.getName() .equals(defaultCert2.getName())); Assert.AssertTrue(defaultCert2.wireEncode().equals( fixture.id1Key1Cert2.wireEncode())); // Remove a certificate. key11.removeCertificate(fixture.id1Key1Cert1.getName()); try { key11.getCertificate(fixture.id1Key1Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_17) { } catch (Exception ex_18) { Assert.Fail("Did not throw the expected exception"); } Assert.AssertEquals(1, key11.getCertificates_().size()); // Set the default certificate directly again, which should change the default. try { key11.setDefaultCertificate(fixture.id1Key1Cert1); } catch (Exception ex_19) { Assert.Fail("Unexpected exception: " + ex_19.Message); } CertificateV2 defaultCert3 = key11.getDefaultCertificate(); Assert.AssertTrue(fixture.id1Key1Cert1.getName() .equals(defaultCert3.getName())); Assert.AssertTrue(defaultCert3.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())); Assert.AssertEquals(2, key11.getCertificates_().size()); // Remove all certificates. key11.removeCertificate(fixture.id1Key1Cert1.getName()); try { key11.getCertificate(fixture.id1Key1Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_20) { } catch (Exception ex_21) { Assert.Fail("Did not throw the expected exception"); } Assert.AssertEquals(1, key11.getCertificates_().size()); key11.removeCertificate(fixture.id1Key1Cert2.getName()); try { key11.getCertificate(fixture.id1Key1Cert2.getName()); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_22) { } catch (Exception ex_23) { Assert.Fail("Did not throw the expected exception"); } try { key11.getDefaultCertificate(); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_24) { } catch (Exception ex_25) { Assert.Fail("Did not throw the expected exception"); } Assert.AssertEquals(0, key11.getCertificates_().size()); }
public void testErrors() { PibMemory pibImpl = new PibMemory(); try { new PibKeyImpl(fixture.id1Key1Name, pibImpl); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex) { } catch (Exception ex_0) { Assert.Fail("Did not throw the expected exception"); } PibKeyImpl key11 = new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl); try { new PibKeyImpl(new Name("/wrong"), pibImpl); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_1) { } catch (Exception ex_2) { Assert.Fail("Did not throw the expected exception"); } try { new PibKeyImpl(new Name("/wrong"), fixture.id1Key1.buf(), pibImpl); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_3) { } catch (Exception ex_4) { Assert.Fail("Did not throw the expected exception"); } Blob wrongKey = new Blob(""); try { new PibKeyImpl(fixture.id1Key2Name, wrongKey.buf(), pibImpl); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_5) { } catch (Exception ex_6) { Assert.Fail("Did not throw the expected exception"); } key11.addCertificate(fixture.id1Key1Cert1); try { key11.addCertificate(fixture.id1Key2Cert1); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_7) { } catch (Exception ex_8) { Assert.Fail("Did not throw the expected exception"); } try { key11.removeCertificate(fixture.id1Key2Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_9) { } catch (Exception ex_10) { Assert.Fail("Did not throw the expected exception"); } try { key11.getCertificate(fixture.id1Key2Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_11) { } catch (Exception ex_12) { Assert.Fail("Did not throw the expected exception"); } try { key11.setDefaultCertificate(fixture.id1Key2Cert1); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_13) { } catch (Exception ex_14) { Assert.Fail("Did not throw the expected exception"); } try { key11.setDefaultCertificate(fixture.id1Key2Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_15) { } catch (Exception ex_16) { Assert.Fail("Did not throw the expected exception"); } }