security: Add in-memory pib

Refs: #2451

Change-Id: I943b0b51d8fe9a8911826b4f907c5f9fd94aeacc
diff --git a/tests/unit-tests/security/in-memory-pib-impl.t.cpp b/tests/unit-tests/security/in-memory-pib-impl.t.cpp
new file mode 100644
index 0000000..f55533f
--- /dev/null
+++ b/tests/unit-tests/security/in-memory-pib-impl.t.cpp
@@ -0,0 +1,141 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2013-2015 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "security/in-memory-pib-impl.hpp"
+#include "security/pib.hpp"
+#include "pib-data-fixture.hpp"
+
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+BOOST_AUTO_TEST_SUITE(SecurityInMemoryPibImpl)
+
+BOOST_AUTO_TEST_CASE(TpmLocatorManagement)
+{
+  InMemoryPibImpl pibImpl;
+
+  BOOST_CHECK_EQUAL(pibImpl.getTpmLocator(), "tpm-memory:");
+  BOOST_CHECK_THROW(pibImpl.setTpmLocator(""), PibImpl::Error);
+}
+
+BOOST_FIXTURE_TEST_CASE(IdentityManagement, PibDataFixture)
+{
+  InMemoryPibImpl pibImpl;
+
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+  pibImpl.addIdentity(id1);
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
+  pibImpl.removeIdentity(id1);
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), false);
+
+  BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
+  pibImpl.setDefaultIdentity(id1);
+  BOOST_CHECK_EQUAL(pibImpl.hasIdentity(id1), true);
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultIdentity(), id1);
+
+  pibImpl.addIdentity(id2);
+  std::set<Name> idNames = pibImpl.getIdentities();
+  BOOST_CHECK_EQUAL(idNames.size(), 2);
+  BOOST_CHECK_EQUAL(idNames.count(id1), 1);
+  BOOST_CHECK_EQUAL(idNames.count(id2), 1);
+}
+
+BOOST_FIXTURE_TEST_CASE(KeyManagement, PibDataFixture)
+{
+  InMemoryPibImpl pibImpl;
+
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
+  pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), true);
+  pibImpl.removeKey(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL(pibImpl.hasKey(id1, id1Key1Name.get(-1)), false);
+
+  BOOST_CHECK_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)), Pib::Error);
+  pibImpl.addKey(id1, id1Key1Name.get(-1), id1Key1);
+  BOOST_CHECK_NO_THROW(pibImpl.getKeyBits(id1, id1Key1Name.get(-1)));
+  const PublicKey& keyBits = pibImpl.getKeyBits(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL_COLLECTIONS(keyBits.get().buf(), keyBits.get().buf() + keyBits.get().size(),
+                                id1Key1.get().buf(), id1Key1.get().buf() + id1Key1.get().size());
+
+  pibImpl.addKey(id1, id1Key2Name.get(-1), id1Key2);
+  pibImpl.addKey(id2, id2Key2Name.get(-1), id2Key2);
+
+  std::set<name::Component> keyNames = pibImpl.getKeysOfIdentity(id1);
+  BOOST_CHECK_EQUAL(keyNames.size(), 2);
+  BOOST_CHECK_EQUAL(keyNames.count(id1Key1Name.get(-1)), 1);
+  BOOST_CHECK_EQUAL(keyNames.count(id1Key2Name.get(-1)), 1);
+
+  BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
+  pibImpl.setDefaultKeyOfIdentity(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL(pibImpl.getDefaultKeyOfIdentity(id1), id1Key1Name.get(-1));
+  BOOST_CHECK_THROW(pibImpl.setDefaultKeyOfIdentity(id1, name::Component("non-existing")),
+                    Pib::Error);
+}
+
+BOOST_FIXTURE_TEST_CASE(CertificateManagement, PibDataFixture)
+{
+  InMemoryPibImpl pibImpl;
+
+  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
+  pibImpl.addCertificate(id1Key1Cert1);
+  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), true);
+  pibImpl.removeCertificate(id1Key1Cert1.getName());
+  BOOST_CHECK_EQUAL(pibImpl.hasCertificate(id1Key1Cert1.getName()), false);
+
+  BOOST_CHECK_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()), Pib::Error);
+  pibImpl.addCertificate(id1Key1Cert1);
+  BOOST_CHECK_NO_THROW(pibImpl.getCertificate(id1Key1Cert1.getName()));
+  const IdentityCertificate& cert = pibImpl.getCertificate(id1Key1Cert1.getName());
+  BOOST_CHECK_EQUAL_COLLECTIONS(cert.wireEncode().wire(),
+                                cert.wireEncode().wire() + cert.wireEncode().size(),
+                                id1Key1Cert1.wireEncode().wire(),
+                                id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
+
+  pibImpl.addCertificate(id1Key1Cert2);
+  pibImpl.addCertificate(id1Key2Cert2);
+
+  std::set<Name> certNames = pibImpl.getCertificatesOfKey(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL(certNames.size(), 2);
+  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert1.getName()), 1);
+  BOOST_CHECK_EQUAL(certNames.count(id1Key1Cert2.getName()), 1);
+
+  BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
+  pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key1Cert1.getName());
+  BOOST_CHECK_NO_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)));
+  const IdentityCertificate& defaultCert =
+    pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1));
+  BOOST_CHECK_EQUAL_COLLECTIONS(defaultCert.wireEncode().wire(),
+                                defaultCert.wireEncode().wire() + defaultCert.wireEncode().size(),
+                                id1Key1Cert1.wireEncode().wire(),
+                                id1Key1Cert1.wireEncode().wire() + id1Key1Cert1.wireEncode().size());
+
+  BOOST_CHECK_THROW(pibImpl.setDefaultCertificateOfKey(id1, id1Key1Name.get(-1), id1Key2Cert1.getName()),
+                    Pib::Error);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace security
+} // namespace ndn