/// <summary> /// Create a PibKeyImpl with keyName. Initialize the cached key encoding with /// pibImpl.getKeyBits(). /// </summary> /// /// <param name="keyName">The name of the key, which is copied.</param> /// <param name="pibImpl">The Pib backend implementation.</param> /// <exception cref="Pib.Error">if the key with keyName does not exist.</exception> public PibKeyImpl(Name keyName, PibImpl pibImpl) { this.defaultCertificate_ = null; identityName_ = net.named_data.jndn.security.pib.PibKey.extractIdentityFromKeyName(keyName); keyName_ = new Name(keyName); certificates_ = new PibCertificateContainer(keyName, pibImpl); pibImpl_ = pibImpl; if (pibImpl == null) { throw new AssertionError("The pibImpl is null"); } keyEncoding_ = pibImpl_.getKeyBits(keyName_); PublicKey publicKey; try { publicKey = new PublicKey(keyEncoding_); } catch (UnrecognizedKeyFormatException ex) { // We don't expect this since we just fetched the encoding. throw new Pib.Error("Error decoding public key: " + ex); } keyType_ = publicKey.getKeyType(); }
public void testErrors() { PibMemory pibImpl = new PibMemory(); PibCertificateContainer container = new PibCertificateContainer( fixture.id1Key1Name, pibImpl); try { container.add(fixture.id1Key2Cert1); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex) { } catch (Exception ex_0) { Assert.Fail("Did not throw the expected exception"); } try { container.remove(fixture.id1Key2Cert1.getName()); 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 { container.get(fixture.id1Key2Cert1.getName()); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_3) { } catch (Exception ex_4) { Assert.Fail("Did not throw the expected exception"); } }
/// <summary> /// Create a PibKeyImpl with keyName. If the key does not exist in the backend /// implementation, add it by creating it from the keyEncoding. If a key with /// keyName already exists, overwrite it. /// </summary> /// /// <param name="keyName">The name of the key, which is copied.</param> /// <param name="keyEncoding">The buffer of encoded key bytes, which is copied.</param> /// <param name="pibImpl">The Pib backend implementation.</param> public PibKeyImpl(Name keyName, ByteBuffer keyEncoding, PibImpl pibImpl) { this.defaultCertificate_ = null; identityName_ = net.named_data.jndn.security.pib.PibKey.extractIdentityFromKeyName(keyName); keyName_ = new Name(keyName); keyEncoding_ = new Blob(keyEncoding, true); certificates_ = new PibCertificateContainer(keyName, pibImpl); pibImpl_ = pibImpl; if (pibImpl == null) { throw new AssertionError("The pibImpl is null"); } try { PublicKey publicKey = new PublicKey(keyEncoding_); keyType_ = publicKey.getKeyType(); } catch (UnrecognizedKeyFormatException ex) { throw new ArgumentException("Invalid key encoding"); } pibImpl_.addKey(identityName_, keyName_, keyEncoding); }
public void testBasic() { PibMemory pibImpl = new PibMemory(); // Start with an empty container. PibCertificateContainer container = new PibCertificateContainer( fixture.id1Key1Name, pibImpl); Assert.AssertEquals(0, container.size()); Assert.AssertEquals(0, container.getCertificates_().Count); // Add a certificate. container.add(fixture.id1Key1Cert1); Assert.AssertEquals(1, container.size()); Assert.AssertEquals(1, container.getCertificates_().Count); Assert.AssertTrue(container.getCertificates_().Contains( fixture.id1Key1Cert1.getName())); // Add the same certificate again. container.add(fixture.id1Key1Cert1); Assert.AssertEquals(1, container.size()); Assert.AssertEquals(1, container.getCertificates_().Count); Assert.AssertTrue(container.getCertificates_().Contains( fixture.id1Key1Cert1.getName())); // Add another certificate. container.add(fixture.id1Key1Cert2); Assert.AssertEquals(2, container.size()); Assert.AssertEquals(2, container.getCertificates_().Count); Assert.AssertTrue(container.getCertificates_().Contains( fixture.id1Key1Cert1.getName())); Assert.AssertTrue(container.getCertificates_().Contains( fixture.id1Key1Cert2.getName())); // Get the certificates. try { container.get(fixture.id1Key1Cert1.getName()); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex.Message); } try { container.get(fixture.id1Key1Cert2.getName()); } catch (Exception ex_0) { Assert.Fail("Unexpected exception: " + ex_0.Message); } Name id1Key1Cert3Name = new Name(fixture.id1Key1Name); id1Key1Cert3Name.append("issuer").appendVersion(3); try { container.get(id1Key1Cert3Name); Assert.Fail("Did not throw the expected exception"); } catch (Pib.Error ex_1) { } catch (Exception ex_2) { Assert.Fail("Did not throw the expected exception"); } // Check the certificates. CertificateV2 cert1 = container.get(fixture.id1Key1Cert1.getName()); CertificateV2 cert2 = container.get(fixture.id1Key1Cert2.getName()); // Use the wire encoding to check equivalence. Assert.AssertTrue(cert1.wireEncode().equals(fixture.id1Key1Cert1.wireEncode())); Assert.AssertTrue(cert2.wireEncode().equals(fixture.id1Key1Cert2.wireEncode())); // Create another container with the same PibImpl. The cache should be empty. PibCertificateContainer container2 = new PibCertificateContainer( fixture.id1Key1Name, pibImpl); Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(0, container2.getCertificates_().Count); // Get a certificate. The cache should be filled. try { container2.get(fixture.id1Key1Cert1.getName()); } catch (Exception ex_3) { Assert.Fail("Unexpected exception: " + ex_3.Message); } Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(1, container2.getCertificates_().Count); try { container2.get(fixture.id1Key1Cert2.getName()); } catch (Exception ex_4) { Assert.Fail("Unexpected exception: " + ex_4.Message); } Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(2, container2.getCertificates_().Count); // Remove a certificate. container2.remove(fixture.id1Key1Cert1.getName()); Assert.AssertEquals(1, container2.size()); Assert.AssertEquals(1, container2.getCertificates_().Count); Assert.AssertTrue(!container2.getCertificates_().Contains( fixture.id1Key1Cert1.getName())); Assert.AssertTrue(container2.getCertificates_().Contains( fixture.id1Key1Cert2.getName())); // Remove another certificate. container2.remove(fixture.id1Key1Cert2.getName()); Assert.AssertEquals(0, container2.size()); Assert.AssertEquals(0, container2.getCertificates_().Count); Assert.AssertTrue(!container2.getCertificates_().Contains( fixture.id1Key1Cert2.getName())); }