public void testErrors() { PibMemory pibImpl = new PibMemory(); PibKeyContainer container = new PibKeyContainer(fixture.id1, pibImpl); try { container.add(fixture.id2Key1.buf(), fixture.id2Key1Name); 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.id2Key1Name); 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.id2Key1Name); Assert.Fail("Did not throw the expected exception"); } catch (ArgumentException ex_3) { } catch (Exception ex_4) { Assert.Fail("Did not throw the expected exception"); } }
public void testBasic() { PibMemory pibImpl = new PibMemory(); // Start with an empty container. PibKeyContainer container = new PibKeyContainer(fixture.id1, pibImpl); Assert.AssertEquals(0, container.size()); Assert.AssertEquals(0, container.getKeys_().Count); // Add the first key. PibKey key11 = container .add(fixture.id1Key1.buf(), fixture.id1Key1Name); Assert.AssertTrue(fixture.id1Key1Name.equals(key11.getName())); Assert.AssertTrue(key11.getPublicKey().equals(fixture.id1Key1)); Assert.AssertEquals(1, container.size()); Assert.AssertEquals(1, container.getKeys_().Count); Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name)); // Add the same key again. PibKey key12 = container .add(fixture.id1Key1.buf(), fixture.id1Key1Name); Assert.AssertTrue(fixture.id1Key1Name.equals(key12.getName())); Assert.AssertTrue(key12.getPublicKey().equals(fixture.id1Key1)); Assert.AssertEquals(1, container.size()); Assert.AssertEquals(1, container.getKeys_().Count); Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name)); // Add the second key. PibKey key21 = container .add(fixture.id1Key2.buf(), fixture.id1Key2Name); Assert.AssertTrue(fixture.id1Key2Name.equals(key21.getName())); Assert.AssertTrue(key21.getPublicKey().equals(fixture.id1Key2)); Assert.AssertEquals(2, container.size()); Assert.AssertEquals(2, container.getKeys_().Count); Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name)); Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key2Name)); // Get keys. try { container.get(fixture.id1Key1Name); } catch (Exception ex) { Assert.Fail("Unexpected exception: " + ex.Message); } try { container.get(fixture.id1Key2Name); } catch (Exception ex_0) { Assert.Fail("Unexpected exception: " + ex_0.Message); } Name id1Key3Name = net.named_data.jndn.security.pib.PibKey.constructKeyName(fixture.id1, new Name.Component("non-existing-id")); try { container.get(id1Key3Name); 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"); } // Get and check keys. PibKey key1 = container.get(fixture.id1Key1Name); PibKey key2 = container.get(fixture.id1Key2Name); Assert.AssertTrue(fixture.id1Key1Name.equals(key1.getName())); Assert.AssertTrue(key1.getPublicKey().equals(fixture.id1Key1)); Assert.AssertEquals(fixture.id1Key2Name, key2.getName()); Assert.AssertTrue(key2.getPublicKey().equals(fixture.id1Key2)); // Create another container using the same PibImpl. The cache should be empty. PibKeyContainer container2 = new PibKeyContainer(fixture.id1, pibImpl); Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(0, container2.getKeys_().Count); // Get a key. The cache should be filled. try { container2.get(fixture.id1Key1Name); } catch (Exception ex_3) { Assert.Fail("Unexpected exception: " + ex_3.Message); } Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(1, container2.getKeys_().Count); try { container2.get(fixture.id1Key2Name); } catch (Exception ex_4) { Assert.Fail("Unexpected exception: " + ex_4.Message); } Assert.AssertEquals(2, container2.size()); Assert.AssertEquals(2, container2.getKeys_().Count); // Remove a key. container2.remove(fixture.id1Key1Name); Assert.AssertEquals(1, container2.size()); Assert.AssertEquals(1, container2.getKeys_().Count); Assert.AssertTrue(!container2.getKeys_().Contains(fixture.id1Key1Name)); Assert.AssertTrue(container2.getKeys_().Contains(fixture.id1Key2Name)); // Remove another key. container2.remove(fixture.id1Key2Name); Assert.AssertEquals(0, container2.size()); Assert.AssertEquals(0, container2.getKeys_().Count); Assert.AssertTrue(!container2.getKeys_().Contains(fixture.id1Key2Name)); }