public void testOverwrite() { 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"); } new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl); PibKeyImpl key1 = new PibKeyImpl(fixture.id1Key1Name, pibImpl); // Overwriting the key should work. new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key2.buf(), pibImpl); PibKeyImpl key2 = new PibKeyImpl(fixture.id1Key1Name, pibImpl); // key1 should have cached the original public key. Assert.AssertTrue(!key1.getPublicKey().equals(key2.getPublicKey())); Assert.AssertTrue(key2.getPublicKey().equals(fixture.id1Key2)); key1.addCertificate(fixture.id1Key1Cert1); // Use the wire encoding to check equivalence. Assert.AssertTrue(key1.getCertificate(fixture.id1Key1Cert1.getName()) .wireEncode().equals(fixture.id1Key1Cert1.wireEncode())); CertificateV2 otherCert = new CertificateV2(fixture.id1Key1Cert1); ((Sha256WithRsaSignature)otherCert.getSignature()).getValidityPeriod() .setPeriod(net.named_data.jndn.util.Common.getNowMilliseconds(), net.named_data.jndn.util.Common.getNowMilliseconds() + 1000); // Don't bother resigning so we don't have to load a private key. Assert.AssertTrue(fixture.id1Key1Cert1.getName().equals(otherCert.getName())); Assert.AssertTrue(otherCert.getContent().equals( fixture.id1Key1Cert1.getContent())); Assert.AssertFalse(otherCert.wireEncode().equals( fixture.id1Key1Cert1.wireEncode())); key1.addCertificate(otherCert); Assert.AssertTrue(key1.getCertificate(fixture.id1Key1Cert1.getName()) .wireEncode().equals(otherCert.wireEncode())); }
/// <summary> /// Add the certificate. If a certificate with the same name (without implicit /// digest) already exists, then overwrite the certificate. If the key or /// identity does not exist, they will be created. If no default certificate /// for the key has been set, then set the added certificate as the default for /// the key. If no default key was set for the identity, it will be set as the /// default key for the identity. If no default identity was selected, the /// certificate's identity becomes the default. /// </summary> /// /// <param name="certificate">The certificate to add. This copies the object.</param> public override void addCertificate(CertificateV2 certificate) { Name certificateNameCopy = new Name(certificate.getName()); // getKeyName already makes a new Name. Name keyNameCopy = certificate.getKeyName(); Name identity = certificate.getIdentity(); addKey(identity, keyNameCopy, certificate.getContent().buf()); try { ILOG.J2CsMapping.Collections.Collections.Put(certificates_, certificateNameCopy, new CertificateV2( certificate)); } catch (CertificateV2.Error ex) { // We don't expect an error in the copy constructor. throw new PibImpl.Error(ex.Message); } if (!defaultCertificateNames_.Contains(keyNameCopy)) { ILOG.J2CsMapping.Collections.Collections.Put(defaultCertificateNames_, keyNameCopy, certificateNameCopy); } }
/// <summary> /// Add the certificate. If a certificate with the same name (without implicit /// digest) already exists, then overwrite the certificate. If the key or /// identity does not exist, they will be created. If no default certificate /// for the key has been set, then set the added certificate as the default for /// the key. If no default key was set for the identity, it will be set as the /// default key for the identity. If no default identity was selected, the /// certificate's identity becomes the default. /// </summary> /// /// <param name="certificate">The certificate to add. This copies the object.</param> /// <exception cref="PibImpl.Error">for a non-semantic (database access) error.</exception> public override void addCertificate(CertificateV2 certificate) { // Ensure the key exists. Blob content = certificate.getContent(); addKey(certificate.getIdentity(), certificate.getKeyName(), content.buf()); if (!hasCertificate(certificate.getName())) { try { PreparedStatement statement = database_ .prepareStatement(net.named_data.jndn.security.pib.PibSqlite3Base.INSERT_addCertificate); statement.setBytes(1, certificate.getKeyName().wireEncode() .getImmutableArray()); statement.setBytes(2, certificate.getName().wireEncode() .getImmutableArray()); statement.setBytes(3, certificate.wireEncode() .getImmutableArray()); try { statement.executeUpdate(); } finally { statement.close(); } } catch (SQLException exception) { throw new PibImpl.Error("PibSqlite3: SQLite error: " + exception); } } else { try { PreparedStatement statement_0 = database_ .prepareStatement(net.named_data.jndn.security.pib.PibSqlite3Base.UPDATE_addCertificate); statement_0.setBytes(1, certificate.wireEncode() .getImmutableArray()); statement_0.setBytes(2, certificate.getName().wireEncode() .getImmutableArray()); try { statement_0.executeUpdate(); } finally { statement_0.close(); } } catch (SQLException exception_1) { throw new PibImpl.Error("PibSqlite3: SQLite error: " + exception_1); } } if (!hasDefaultCertificateOfKey(certificate.getKeyName())) { try { setDefaultCertificateOfKey(certificate.getKeyName(), certificate.getName()); } catch (Pib.Error ex) { throw new PibImpl.Error( "PibSqlite3: Error setting the default certificate: " + ex); } } }