Updating ca storage interface to support listing function

Change-Id: Iab9d50d075a139a671d94824b97c78b741203354
Refs:#4048
diff --git a/src/ca-detail/ca-memory.cpp b/src/ca-detail/ca-memory.cpp
index 14a6f87..566e08c 100644
--- a/src/ca-detail/ca-memory.cpp
+++ b/src/ca-detail/ca-memory.cpp
@@ -80,6 +80,28 @@
   }
 }
 
+std::list<CertificateRequest>
+CaMemory::listAllRequests()
+{
+  std::list<CertificateRequest> result;
+  for (const auto& entry : m_requests) {
+    result.push_back(entry.second);
+  }
+  return result;
+}
+
+std::list<CertificateRequest>
+CaMemory::listAllRequests(const Name& caName)
+{
+  std::list<CertificateRequest> result;
+  for (const auto& entry : m_requests) {
+    if (entry.second.getCaName() == caName) {
+      result.push_back(entry.second);
+    }
+  }
+  return result;
+}
+
 // certificate related
 security::v2::Certificate
 CaMemory::getCertificate(const std::string& certId)
@@ -122,5 +144,27 @@
   }
 }
 
+std::list<security::v2::Certificate>
+CaMemory::listAllIssuedCertificates()
+{
+  std::list<security::v2::Certificate> result;
+  for (const auto& entry : m_issuedCerts) {
+    result.push_back(entry.second);
+  }
+  return result;
+}
+
+std::list<security::v2::Certificate>
+CaMemory::listAllIssuedCertificates(const Name& caName)
+{
+  std::list<security::v2::Certificate> result;
+  for (const auto& entry : m_issuedCerts) {
+    if (entry.second.getSignature().getKeyLocator().getName().getPrefix(-2) == caName) {
+      result.push_back(entry.second);
+    }
+  }
+  return result;
+}
+
 } // namespace ndncert
 } // namespace ndn
diff --git a/src/ca-detail/ca-memory.hpp b/src/ca-detail/ca-memory.hpp
index 249321d..d695359 100644
--- a/src/ca-detail/ca-memory.hpp
+++ b/src/ca-detail/ca-memory.hpp
@@ -45,6 +45,12 @@
   void
   deleteRequest(const std::string& requestId) override;
 
+  std::list<CertificateRequest>
+  listAllRequests() override;
+
+  std::list<CertificateRequest>
+  listAllRequests(const Name& caName) override;
+
   // certificate related
   security::v2::Certificate
   getCertificate(const std::string& certId) override;
@@ -58,6 +64,12 @@
   void
   deleteCertificate(const std::string& certId) override;
 
+  std::list<security::v2::Certificate>
+  listAllIssuedCertificates() override;
+
+  std::list<security::v2::Certificate>
+  listAllIssuedCertificates(const Name& caName) override;
+
 private:
   std::map<std::string, CertificateRequest> m_requests;
   std::map<std::string, security::v2::Certificate> m_issuedCerts;
diff --git a/src/ca-detail/ca-sqlite.cpp b/src/ca-detail/ca-sqlite.cpp
index 46249dc..34f5d19 100644
--- a/src/ca-detail/ca-sqlite.cpp
+++ b/src/ca-detail/ca-sqlite.cpp
@@ -72,11 +72,6 @@
   if (!location.empty()) {
     dbDir = boost::filesystem::path(location);
   }
-#ifdef HAVE_TESTS
-  else if (getenv("TEST_HOME") != nullptr) {
-    dbDir = boost::filesystem::path(getenv("TEST_HOME")) / ".ndn";
-  }
-#endif // HAVE_TESTS
   else if (getenv("HOME") != nullptr) {
     dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
   }
@@ -186,11 +181,50 @@
   }
 }
 
+std::list<CertificateRequest>
+CaSqlite::listAllRequests()
+{
+  std::list<CertificateRequest> result;
+  Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM CertRequests)_SQLTEXT_");
+
+  while(statement.step() == SQLITE_ROW) {
+    std::string requestId = statement.getString(1);
+    Name caName(statement.getBlock(2));
+    std::string status = statement.getString(3);
+    security::v2::Certificate cert(statement.getBlock(5));
+    std::string challengeType = statement.getString(6);
+    std::string challengeSecrets = statement.getString(7);
+    CertificateRequest entry(caName, requestId, status, challengeType, challengeSecrets, cert);
+    result.push_back(entry);
+  }
+  return result;
+}
+
+std::list<CertificateRequest>
+CaSqlite::listAllRequests(const Name& caName)
+{
+  std::list<CertificateRequest> result;
+  Sqlite3Statement statement(m_database,
+                             R"_SQLTEXT_(SELECT * FROM CertRequests WHERE ca_name = ?)_SQLTEXT_");
+  statement.bind(1, caName.wireEncode(), SQLITE_TRANSIENT);
+
+  while(statement.step() == SQLITE_ROW) {
+    std::string requestId = statement.getString(1);
+    std::string status = statement.getString(3);
+    security::v2::Certificate cert(statement.getBlock(5));
+    std::string challengeType = statement.getString(6);
+    std::string challengeSecrets = statement.getString(7);
+    CertificateRequest entry(caName, requestId, status, challengeType, challengeSecrets, cert);
+    result.push_back(entry);
+  }
+  return result;
+}
+
 void
 CaSqlite::deleteRequest(const std::string& requestId)
 {
   Sqlite3Statement statement(m_database,
-                             R"_SQLTEXT_(DELETE FROM CertRequest WHERE request_id = ?)_SQLTEXT_");
+                             R"_SQLTEXT_(DELETE FROM CertRequests WHERE request_id = ?)_SQLTEXT_");
   statement.bind(1, requestId, SQLITE_TRANSIENT);
   statement.step();
 }
@@ -248,6 +282,32 @@
   statement.step();
 }
 
+std::list<security::v2::Certificate>
+CaSqlite::listAllIssuedCertificates()
+{
+  std::list<security::v2::Certificate> result;
+  Sqlite3Statement statement(m_database, R"_SQLTEXT_(SELECT * FROM IssuedCerts)_SQLTEXT_");
+
+  while (statement.step() == SQLITE_ROW) {
+    security::v2::Certificate cert(statement.getBlock(3));
+    result.push_back(cert);
+  }
+  return result;
+}
+
+std::list<security::v2::Certificate>
+CaSqlite::listAllIssuedCertificates(const Name& caName)
+{
+  auto allCerts = listAllIssuedCertificates();
+  std::list<security::v2::Certificate> result;
+  for (const auto& entry : allCerts) {
+    if (entry.getSignature().getKeyLocator().getName().getPrefix(-2) == caName) {
+      result.push_back(entry);
+    }
+  }
+  return result;
+}
+
 std::string
 CaSqlite::convertJson2String(const JsonSection& json)
 {
diff --git a/src/ca-detail/ca-sqlite.hpp b/src/ca-detail/ca-sqlite.hpp
index d31f895..6249db5 100644
--- a/src/ca-detail/ca-sqlite.hpp
+++ b/src/ca-detail/ca-sqlite.hpp
@@ -53,6 +53,12 @@
   void
   deleteRequest(const std::string& requestId) override;
 
+  std::list<CertificateRequest>
+  listAllRequests() override;
+
+  std::list<CertificateRequest>
+  listAllRequests(const Name& caName) override;
+
   // certificate related
   security::v2::Certificate
   getCertificate(const std::string& certId) override;
@@ -66,7 +72,13 @@
   void
   deleteCertificate(const std::string& certId) override;
 
-private:
+  std::list<security::v2::Certificate>
+  listAllIssuedCertificates() override;
+
+  std::list<security::v2::Certificate>
+  listAllIssuedCertificates(const Name& caName) override;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
   static std::string
   convertJson2String(const JsonSection& json);
 
diff --git a/src/ca-storage.hpp b/src/ca-storage.hpp
index f06e70c..0a85f6c 100644
--- a/src/ca-storage.hpp
+++ b/src/ca-storage.hpp
@@ -52,6 +52,12 @@
   virtual void
   deleteRequest(const std::string& requestId) = 0;
 
+  virtual std::list<CertificateRequest>
+  listAllRequests() = 0;
+
+  virtual std::list<CertificateRequest>
+  listAllRequests(const Name& caName) = 0;
+
   // certificate related
   virtual security::v2::Certificate
   getCertificate(const std::string& certId) = 0;
@@ -65,6 +71,12 @@
   virtual void
   deleteCertificate(const std::string& certId) = 0;
 
+  virtual std::list<security::v2::Certificate>
+  listAllIssuedCertificates() = 0;
+
+  virtual std::list<security::v2::Certificate>
+  listAllIssuedCertificates(const Name& caName) = 0;
+
 public:
   template<class CaStorageType>
   static void
diff --git a/tests/database-fixture.hpp b/tests/database-fixture.hpp
new file mode 100644
index 0000000..fc9da55
--- /dev/null
+++ b/tests/database-fixture.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2017, Regents of the University of California,
+ *                          Arizona Board of Regents,
+ *                          Colorado State University,
+ *                          University Pierre & Marie Curie, Sorbonne University,
+ *                          Washington University in St. Louis,
+ *                          Beijing Institute of Technology,
+ *                          The University of Memphis.
+ *
+ * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
+ * is a part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert 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 General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#ifndef NDNCERT_TESTS_DATABASE_FIXTURE_HPP
+#define NDNCERT_TESTS_DATABASE_FIXTURE_HPP
+
+#include "test-common.hpp"
+#include "identity-management-fixture.hpp"
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+class DatabaseFixture : public IdentityManagementV2TimeFixture
+{
+public:
+  DatabaseFixture()
+  {
+    dbDir = boost::filesystem::path(getenv("HOME")) / ".ndn";
+  }
+
+  ~DatabaseFixture()
+  {
+    boost::filesystem::remove_all(dbDir);
+  }
+
+public:
+  boost::filesystem::path dbDir;
+};
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
+
+#endif // NDNCERT_TESTS_DATABASE_FIXTURE_HPP
diff --git a/tests/unit-tests/ca-memory.t.cpp b/tests/unit-tests/ca-memory.t.cpp
new file mode 100644
index 0000000..a717e31
--- /dev/null
+++ b/tests/unit-tests/ca-memory.t.cpp
@@ -0,0 +1,125 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2017, Regents of the University of California.
+ *
+ * This file is part of ndncert, a certificate management system based on NDN.
+ *
+ * ndncert is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation, either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * ndncert 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 General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License along with
+ * ndncert, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndncert authors and contributors.
+ */
+
+#include "identity-management-fixture.hpp"
+#include "ca-detail/ca-memory.hpp"
+#include "ca-detail/ca-sqlite.hpp"
+
+namespace ndn {
+namespace ndncert {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(TestCaMemory, IdentityManagementV2TimeFixture)
+
+BOOST_AUTO_TEST_CASE(Initialization)
+{
+  BOOST_CHECK_NO_THROW(CaMemory storage);
+}
+
+BOOST_AUTO_TEST_CASE(CertificateOperations)
+{
+  CaMemory storage;
+
+  auto identity1 = addIdentity(Name("/ndn/site1"));
+  auto key1 = identity1.getDefaultKey();
+  auto cert1 = key1.getDefaultCertificate();
+
+  // add operation
+  BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert1));
+
+  // get operation
+  BOOST_CHECK_EQUAL(storage.getCertificate("123"), cert1);
+
+  // list operation
+  auto allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 1);
+
+  auto identity2 = addIdentity(Name("/ndn/site2"));
+  auto key2 = identity2.getDefaultKey();
+  auto cert2 = key2.getDefaultCertificate();
+  storage.addCertificate("456", cert2);
+
+  allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 2);
+
+  BOOST_CHECK_NO_THROW(storage.deleteCertificate("123"));
+
+  allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 1);
+
+  auto identity3 = addIdentity(Name("/ndn/site3"));
+  auto key3 = identity3.getDefaultKey();
+  auto cert3 = key3.getDefaultCertificate();
+
+  // update operation
+  BOOST_CHECK_NO_THROW(storage.updateCertificate("456", cert3));
+  BOOST_CHECK_EQUAL(storage.getCertificate("456"), cert3);
+}
+
+BOOST_AUTO_TEST_CASE(RequestOperations)
+{
+  CaMemory storage;
+
+  auto identity1 = addIdentity(Name("/ndn/site1"));
+  auto key1 = identity1.getDefaultKey();
+  auto cert1 = key1.getDefaultCertificate();
+
+  // add operation
+  CertificateRequest request1(Name("/ndn/site1"), "123", cert1);
+  BOOST_CHECK_NO_THROW(storage.addRequest(request1));
+
+  // get operation
+  auto result = storage.getRequest("123");
+  BOOST_CHECK_EQUAL(request1.getCert(), result.getCert());
+  BOOST_CHECK_EQUAL(request1.getStatus(), result.getStatus());
+  BOOST_CHECK_EQUAL(request1.getCaName(), result.getCaName());
+
+  JsonSection json;
+  json.put("code", "1234");
+
+  // update operation
+  CertificateRequest request2(Name("/ndn/site1"), "123", "need-verify", "EMAIL",
+                              CaSqlite::convertJson2String(json), cert1);
+  storage.updateRequest(request2);
+  result = storage.getRequest("123");
+  BOOST_CHECK_EQUAL(request2.getCert(), result.getCert());
+  BOOST_CHECK_EQUAL(request2.getStatus(), result.getStatus());
+  BOOST_CHECK_EQUAL(request2.getCaName(), result.getCaName());
+
+  auto identity2 = addIdentity(Name("/ndn/site2"));
+  auto key2 = identity2.getDefaultKey();
+  auto cert2 = key2.getDefaultCertificate();
+  CertificateRequest request3(Name("/ndn/site2"), "456", cert2);
+  storage.addRequest(request3);
+
+  // list operation
+  auto allRequests = storage.listAllRequests();
+  BOOST_CHECK_EQUAL(allRequests.size(), 2);
+
+  storage.deleteRequest("456");
+  allRequests = storage.listAllRequests();
+  BOOST_CHECK_EQUAL(allRequests.size(), 1);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestCaModule
+
+} // namespace tests
+} // namespace ndncert
+} // namespace ndn
diff --git a/tests/unit-tests/ca-module.t.cpp b/tests/unit-tests/ca-module.t.cpp
index 33c81ee..0142755 100644
--- a/tests/unit-tests/ca-module.t.cpp
+++ b/tests/unit-tests/ca-module.t.cpp
@@ -18,7 +18,7 @@
  * See AUTHORS.md for complete list of ndncert authors and contributors.
  */
 
-#include "identity-management-fixture.hpp"
+#include "database-fixture.hpp"
 #include "ca-module.hpp"
 #include "client-module.hpp"
 #include "challenge-module.hpp"
@@ -31,7 +31,7 @@
 namespace ndncert {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TestCaModule, IdentityManagementV2TimeFixture)
+BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture)
 
 BOOST_AUTO_TEST_CASE(Initialization)
 {
diff --git a/tests/unit-tests/ca-sqlite.t.cpp b/tests/unit-tests/ca-sqlite.t.cpp
index 496d7ae..be4b54d 100644
--- a/tests/unit-tests/ca-sqlite.t.cpp
+++ b/tests/unit-tests/ca-sqlite.t.cpp
@@ -18,43 +18,73 @@
  * See AUTHORS.md for complete list of ndncert authors and contributors.
  */
 
-#include "identity-management-fixture.hpp"
+#include "database-fixture.hpp"
 #include "ca-detail/ca-sqlite.hpp"
 
 namespace ndn {
 namespace ndncert {
 namespace tests {
 
-BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, IdentityManagementV2TimeFixture)
+BOOST_FIXTURE_TEST_SUITE(TestCaSqlite, DatabaseFixture)
 
 BOOST_AUTO_TEST_CASE(Initialization)
 {
-  BOOST_CHECK_NO_THROW(CaSqlite storage);
+  BOOST_CHECK_NO_THROW(CaSqlite storage(dbDir.string()));
 }
 
 BOOST_AUTO_TEST_CASE(CertificateOperations)
 {
-  CaSqlite storage;
+  CaSqlite storage(dbDir.string());
 
-  auto identity = addIdentity(Name("/ndn/site1"));
-  auto key = identity.getDefaultKey();
-  auto cert = key.getDefaultCertificate();
+  auto identity1 = addIdentity(Name("/ndn/site1"));
+  auto key1 = identity1.getDefaultKey();
+  auto cert1 = key1.getDefaultCertificate();
 
-  BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert));
-  auto result = storage.getCertificate("123");
-  BOOST_CHECK_EQUAL(cert, result);
+  // add operation
+  BOOST_CHECK_NO_THROW(storage.addCertificate("123", cert1));
+
+  // get operation
+  BOOST_CHECK_EQUAL(storage.getCertificate("123"), cert1);
+
+  // list operation
+  auto allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 1);
+
+  auto identity2 = addIdentity(Name("/ndn/site2"));
+  auto key2 = identity2.getDefaultKey();
+  auto cert2 = key2.getDefaultCertificate();
+  storage.addCertificate("456", cert2);
+
+  allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 2);
+
+  BOOST_CHECK_NO_THROW(storage.deleteCertificate("123"));
+
+  allCerts = storage.listAllIssuedCertificates();
+  BOOST_CHECK_EQUAL(allCerts.size(), 1);
+
+  auto identity3 = addIdentity(Name("/ndn/site3"));
+  auto key3 = identity3.getDefaultKey();
+  auto cert3 = key3.getDefaultCertificate();
+
+  // update operation
+  BOOST_CHECK_NO_THROW(storage.updateCertificate("456", cert3));
+  BOOST_CHECK_EQUAL(storage.getCertificate("456"), cert3);
 }
 
 BOOST_AUTO_TEST_CASE(RequestOperations)
 {
-  CaSqlite storage;
+  CaSqlite storage(dbDir.string());
 
-  auto identity = addIdentity(Name("/ndn/site2"));
-  auto key = identity.getDefaultKey();
-  auto cert = key.getDefaultCertificate();
+  auto identity1 = addIdentity(Name("/ndn/site1"));
+  auto key1 = identity1.getDefaultKey();
+  auto cert1 = key1.getDefaultCertificate();
 
-  CertificateRequest request1(Name("/ndn/site2"), "123", cert);
+  // add operation
+  CertificateRequest request1(Name("/ndn/site1"), "123", cert1);
   BOOST_CHECK_NO_THROW(storage.addRequest(request1));
+
+  // get operation
   auto result = storage.getRequest("123");
   BOOST_CHECK_EQUAL(request1.getCert(), result.getCert());
   BOOST_CHECK_EQUAL(request1.getStatus(), result.getStatus());
@@ -62,16 +92,33 @@
 
   JsonSection json;
   json.put("code", "1234");
-  std::stringstream ss;
-  boost::property_tree::write_json(ss, json);
-  std::string jsonValue = ss.str();
 
-  CertificateRequest request2(Name("/ndn/site2"), "123", "need-verify", "EMAIL", jsonValue, cert);
+  // update operation
+  CertificateRequest request2(Name("/ndn/site1"), "123", "need-verify", "EMAIL",
+                              CaSqlite::convertJson2String(json), cert1);
   storage.updateRequest(request2);
   result = storage.getRequest("123");
   BOOST_CHECK_EQUAL(request2.getCert(), result.getCert());
   BOOST_CHECK_EQUAL(request2.getStatus(), result.getStatus());
   BOOST_CHECK_EQUAL(request2.getCaName(), result.getCaName());
+
+  auto identity2 = addIdentity(Name("/ndn/site2"));
+  auto key2 = identity2.getDefaultKey();
+  auto cert2 = key2.getDefaultCertificate();
+  CertificateRequest request3(Name("/ndn/site2"), "456", cert2);
+  storage.addRequest(request3);
+
+  // list operation
+  auto allRequests = storage.listAllRequests();
+  BOOST_CHECK_EQUAL(allRequests.size(), 2);
+
+  storage.deleteRequest("456");
+  allRequests = storage.listAllRequests();
+  BOOST_CHECK_EQUAL(allRequests.size(), 1);
+
+  storage.deleteRequest("123");
+  allRequests = storage.listAllRequests();
+  BOOST_CHECK_EQUAL(allRequests.size(), 0);
 }
 
 BOOST_AUTO_TEST_SUITE_END() // TestCaModule