security: implement PibSqlite3 based on PibImpl

Change-Id: I47a61cf8ebe5f76d5ca492db52c2f4f2f5ac3c9b
Refs: #2807
diff --git a/tests/unit-tests/security/pib-impl.t.cpp b/tests/unit-tests/security/pib-impl.t.cpp
index 1f6158b..4c01033 100644
--- a/tests/unit-tests/security/pib-impl.t.cpp
+++ b/tests/unit-tests/security/pib-impl.t.cpp
@@ -20,10 +20,11 @@
  */
 
 #include "security/pib-memory.hpp"
+#include "security/pib-sqlite3.hpp"
 #include "security/pib.hpp"
 #include "pib-data-fixture.hpp"
 
-#include <boost/test/test_case_template.hpp>
+#include <boost/filesystem.hpp>
 #include <boost/mpl/list.hpp>
 #include "boost-test.hpp"
 
@@ -33,11 +34,37 @@
 
 BOOST_AUTO_TEST_SUITE(SecurityPibImpl)
 
-typedef boost::mpl::list<PibMemory> PibImpls;
+class PibMemoryWrapper
+{
+public:
+  PibMemory impl;
+};
+
+class PibSqlite3Wrapper
+{
+public:
+  PibSqlite3Wrapper()
+    : tmpPath(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
+    , impl(tmpPath.c_str())
+  {
+  }
+
+  ~PibSqlite3Wrapper()
+  {
+    boost::filesystem::remove_all(tmpPath);
+  }
+
+  boost::filesystem::path tmpPath;
+  PibSqlite3 impl;
+};
+
+typedef boost::mpl::list<PibMemoryWrapper,
+                         PibSqlite3Wrapper> PibImpls;
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(IdentityManagement, T, PibImpls, PibDataFixture)
 {
-  T pibImpl;
+  T wrapper;
+  PibImpl& pibImpl = wrapper.impl;
 
   // no default setting, throw Error
   BOOST_CHECK_THROW(pibImpl.getDefaultIdentity(), Pib::Error);
@@ -78,7 +105,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(KeyManagement, T, PibImpls, PibDataFixture)
 {
-  T pibImpl;
+  T wrapper;
+  PibImpl& pibImpl = wrapper.impl;
 
   // no default setting, throw Error
   BOOST_CHECK_THROW(pibImpl.getDefaultKeyOfIdentity(id1), Pib::Error);
@@ -135,7 +163,8 @@
 
 BOOST_FIXTURE_TEST_CASE_TEMPLATE(CertificateManagement, T, PibImpls, PibDataFixture)
 {
-  T pibImpl;
+  T wrapper;
+  PibImpl& pibImpl = wrapper.impl;
 
   // no default setting, throw Error
   BOOST_CHECK_THROW(pibImpl.getDefaultCertificateOfKey(id1, id1Key1Name.get(-1)), Pib::Error);
diff --git a/tests/unit-tests/security/pib-sqlite3.t.cpp b/tests/unit-tests/security/pib-sqlite3.t.cpp
new file mode 100644
index 0000000..52d5ff1
--- /dev/null
+++ b/tests/unit-tests/security/pib-sqlite3.t.cpp
@@ -0,0 +1,124 @@
+/* -*- 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/pib-sqlite3.hpp"
+#include "security/pib.hpp"
+
+#include <boost/filesystem.hpp>
+#include "boost-test.hpp"
+
+namespace ndn {
+namespace security {
+namespace tests {
+
+class PibSqlite3TestFixture
+{
+public:
+  PibSqlite3TestFixture()
+    : m_path(boost::filesystem::path(UNIT_TEST_CONFIG_PATH) / "DbTest")
+    , impl(m_path.c_str())
+  {
+  }
+
+  ~PibSqlite3TestFixture()
+  {
+    boost::filesystem::remove_all(m_path);
+  }
+
+private:
+  boost::filesystem::path m_path;
+
+public:
+  PibSqlite3 impl;
+};
+
+
+BOOST_FIXTURE_TEST_SUITE(SecurityPibSqlite3, PibSqlite3TestFixture)
+
+// most functionalities are tested in pib-impl.t.cpp
+
+const uint8_t SELF_SIGNED_ECDSA_CERT[] = {
+0x06, 0xfd, 0x01, 0x5b, 0x07, 0x33, 0x08, 0x05, 0x65, 0x63, 0x64, 0x73, 0x61, 0x08, 0x03,
+0x4b, 0x45, 0x59, 0x08, 0x11, 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39,
+0x34, 0x35, 0x35, 0x32, 0x38, 0x32, 0x37, 0x08, 0x07, 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52,
+0x54, 0x08, 0x09, 0xfd, 0x00, 0x00, 0x01, 0x49, 0xd3, 0x9d, 0x78, 0x00, 0x14, 0x03, 0x18,
+0x01, 0x02, 0x15, 0xa5, 0x30, 0x81, 0xa2, 0x30, 0x22, 0x18, 0x0f, 0x32, 0x30, 0x31, 0x34,
+0x31, 0x31, 0x32, 0x31, 0x31, 0x38, 0x32, 0x39, 0x31, 0x32, 0x5a, 0x18, 0x0f, 0x32, 0x30,
+0x33, 0x34, 0x31, 0x31, 0x31, 0x36, 0x31, 0x38, 0x32, 0x39, 0x31, 0x32, 0x5a, 0x30, 0x21,
+0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x29, 0x13, 0x18, 0x2f, 0x65, 0x63, 0x64, 0x73, 0x61,
+0x2f, 0x6b, 0x73, 0x6b, 0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39, 0x34, 0x35, 0x35, 0x32,
+0x38, 0x32, 0x37, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
+0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04,
+0x83, 0xe5, 0x81, 0x19, 0xd9, 0xfa, 0x64, 0x40, 0xad, 0x7c, 0x93, 0xfc, 0x15, 0x90, 0x6b,
+0x38, 0x1e, 0xc5, 0xca, 0xb1, 0x6b, 0x0b, 0x1f, 0x64, 0xbf, 0x48, 0xaa, 0xd0, 0x91, 0x5c,
+0x24, 0xd6, 0x78, 0x40, 0xfd, 0x95, 0x5d, 0x54, 0x64, 0xe1, 0x2d, 0x0e, 0x98, 0x66, 0x1d,
+0x7a, 0xb0, 0x61, 0x17, 0x05, 0x26, 0x13, 0x63, 0x25, 0x7c, 0xda, 0x87, 0x11, 0xc9, 0x67,
+0xcd, 0x12, 0x05, 0xf0, 0x16, 0x2f, 0x1b, 0x01, 0x03, 0x1c, 0x2a, 0x07, 0x28, 0x08, 0x05,
+0x65, 0x63, 0x64, 0x73, 0x61, 0x08, 0x03, 0x4b, 0x45, 0x59, 0x08, 0x11, 0x6b, 0x73, 0x6b,
+0x2d, 0x31, 0x34, 0x31, 0x36, 0x35, 0x39, 0x34, 0x35, 0x35, 0x32, 0x38, 0x32, 0x37, 0x08,
+0x07, 0x49, 0x44, 0x2d, 0x43, 0x45, 0x52, 0x54, 0x17, 0x47, 0x30, 0x45, 0x02, 0x21, 0x00,
+0x9b, 0xae, 0xf4, 0x87, 0x55, 0xaa, 0x78, 0xbf, 0x00, 0xff, 0x1a, 0xbe, 0x90, 0x46, 0x6e,
+0xdd, 0xe6, 0x3b, 0x44, 0xfd, 0x41, 0x04, 0x86, 0xcc, 0x6a, 0x8b, 0x5a, 0x25, 0xbb, 0xf1,
+0x55, 0xcd, 0x02, 0x20, 0x0e, 0x67, 0xd8, 0x86, 0xe8, 0x7c, 0x90, 0x3c, 0x13, 0xfd, 0x36,
+0x9c, 0xbc, 0xa1, 0xc3, 0x7c, 0xe0, 0x0c, 0x6d, 0x64, 0xac, 0xdb, 0x69, 0x99, 0xde, 0x80,
+0x35, 0x3f, 0xf4, 0x6a, 0xcd, 0x6f
+};
+
+BOOST_AUTO_TEST_CASE(TpmTest)
+{
+  ndn::Block selfSignedCertBlock(SELF_SIGNED_ECDSA_CERT, sizeof(SELF_SIGNED_ECDSA_CERT));
+  ndn::IdentityCertificate cert;
+  cert.wireDecode(selfSignedCertBlock);
+  Name identity = cert.getPublicKeyName().getPrefix(-1);
+  name::Component keyId = cert.getPublicKeyName().get(-1);
+  Name certName = cert.getName();
+
+  // Basic getting and setting
+  BOOST_REQUIRE_THROW(impl.getTpmLocator(), Pib::Error);
+  impl.setTpmLocator("tpmLocator");
+  BOOST_CHECK_EQUAL(impl.getTpmLocator(), "tpmLocator");
+
+  // Add cert, and do not change tpmLocator
+  impl.addCertificate(cert);
+  BOOST_CHECK(impl.hasIdentity(identity));
+  BOOST_CHECK(impl.hasKey(identity, keyId));
+  BOOST_CHECK(impl.hasCertificate(certName));
+
+  // Set tpmLocator with the existing value, nothing should change.
+  impl.setTpmLocator("tpmLocator");
+  BOOST_CHECK(impl.hasIdentity(identity));
+  BOOST_CHECK(impl.hasKey(identity, keyId));
+  BOOST_CHECK(impl.hasCertificate(certName));
+
+  // Change tpmLocator and ensure the pib is reset
+  impl.setTpmLocator("newTpmLocator");
+  BOOST_CHECK_EQUAL(impl.getTpmLocator(), "newTpmLocator");
+
+  BOOST_CHECK_EQUAL(impl.getIdentities().size(), 0);
+  BOOST_CHECK_EQUAL(impl.getKeysOfIdentity(identity).size(), 0);
+  BOOST_CHECK_EQUAL(impl.getCertificatesOfKey(identity, keyId).size(), 0);
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace security
+} // namespace ndn