Updating ca storage interface to support listing function

Change-Id: Iab9d50d075a139a671d94824b97c78b741203354
Refs:#4048
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)
 {