Add certificate field to client config and use bool for probe field in ca config

Change-Id: Ib1dcaf07cdb214ea777a26adaed12e488663b3b3
diff --git a/src/ca-config.cpp b/src/ca-config.cpp
index 8d3f309..1032709 100644
--- a/src/ca-config.cpp
+++ b/src/ca-config.cpp
@@ -52,8 +52,7 @@
   for (; it != caList.end(); it++) {
     CaItem item;
     item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
-    item.m_caInfo = it->second.get<std::string>("ca-info");
-    item.m_probe = it->second.get("probe", "");
+    item.m_probe = it->second.get("probe", false);
     item.m_freshnessPeriod = time::seconds(it->second.get<uint64_t>("issuing-freshness"));
     item.m_validityPeriod = time::days(it->second.get<uint64_t>("validity-period"));
 
@@ -65,7 +64,7 @@
 }
 
 std::list<std::string>
-CaConfig::parseChallengeList(const ConfigSection& section)
+CaConfig::parseChallengeList(const JsonSection& section)
 {
   std::list<std::string> result;
   auto it = section.begin();
diff --git a/src/ca-config.hpp b/src/ca-config.hpp
index ed95cb2..12cc396 100644
--- a/src/ca-config.hpp
+++ b/src/ca-config.hpp
@@ -21,20 +21,17 @@
 #ifndef NDNCERT_CA_CONFIG_HPP
 #define NDNCERT_CA_CONFIG_HPP
 
-#include "ndncert-common.hpp"
+#include "certificate-request.hpp"
 #include <ndn-cxx/security/v2/certificate.hpp>
 
 namespace ndn {
 namespace ndncert {
 
-typedef boost::property_tree::ptree ConfigSection;
-
 class CaItem
 {
 public:
   Name m_caName;
-  std::string m_caInfo;
-  std::string m_probe;
+  bool m_probe;
   time::seconds m_freshnessPeriod;
   time::days m_validityPeriod;
   std::list<std::string> m_supportedChallenges;
@@ -65,13 +62,13 @@
   parse();
 
   std::list<std::string>
-  parseChallengeList(const ConfigSection& configSection);
+  parseChallengeList(const JsonSection& configSection);
 
 public:
   std::list<CaItem> m_caItems;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  ConfigSection m_config;
+  JsonSection m_config;
 };
 
 } // namespace ndncert
diff --git a/src/client-config.cpp b/src/client-config.cpp
index c75615d..7fd5280 100644
--- a/src/client-config.cpp
+++ b/src/client-config.cpp
@@ -19,6 +19,7 @@
  */
 
 #include "client-config.hpp"
+#include <ndn-cxx/util/io.hpp>
 
 namespace ndn {
 namespace ndncert {
@@ -48,11 +49,14 @@
   auto caList = m_config.get_child("ca-list");
   auto it = caList.begin();
   for (; it != caList.end(); it++) {
-    CaItem item;
+    ClientCaItem item;
     item.m_caName = Name(it->second.get<std::string>("ca-prefix"));
     item.m_caInfo = it->second.get<std::string>("ca-info");
     item.m_probe = it->second.get("probe", "");
 
+    std::istringstream ss(it->second.get<std::string>("certificate"));
+    item.m_anchor = *(io::load<security::v2::Certificate>(ss));
+
     auto challengeList = it->second.get_child("supported-challenges");
     item.m_supportedChallenges = parseChallengeList(challengeList);
 
@@ -61,7 +65,7 @@
 }
 
 std::list<std::string>
-ClientConfig::parseChallengeList(const ConfigSection& section)
+ClientConfig::parseChallengeList(const JsonSection& section)
 {
   std::list<std::string> result;
   auto it = section.begin();
@@ -72,43 +76,15 @@
 }
 
 void
-ClientConfig::addNewCaItem(const CaItem& item)
+ClientConfig::addNewCaItem(const ClientCaItem& item)
 {
-  auto& caList = m_config.get_child("ca-list");
-
-  ConfigSection newCaItem;
-  ConfigSection newCaChallengeList;
-  newCaItem.put("ca-prefix", item.m_caName.toUri());
-  newCaItem.put("ca-info", item.m_caInfo);
-  if (item.m_probe != "") {
-    newCaItem.put("probe", item.m_probe);
-  }
-  for (const auto& challengeType : item.m_supportedChallenges) {
-    ConfigSection challengeSection;
-    challengeSection.put("type", challengeType);
-    newCaChallengeList.push_back(std::make_pair("", challengeSection));
-  }
-  newCaItem.add_child("supported-challenges", newCaChallengeList);
-  caList.push_back(std::make_pair("", newCaItem));
-
-  parse();
+  m_caItems.push_back(item);
 }
 
 void
 ClientConfig::removeCaItem(const Name& caName)
 {
-  auto& caList = m_config.get_child("ca-list");
-  auto it = caList.begin();
-  while (it != caList.end()) {
-    if (it->second.get<std::string>("ca-prefix") == caName.toUri()) {
-      it = caList.erase(it);
-      break;
-    }
-    else {
-      it++;
-    }
-  }
-  parse();
+  m_caItems.remove_if([&] (const ClientCaItem& item) {return item.m_caName == caName;});
 }
 
 } // namespace ndncert
diff --git a/src/client-config.hpp b/src/client-config.hpp
index fbb703b..8234c01 100644
--- a/src/client-config.hpp
+++ b/src/client-config.hpp
@@ -21,12 +21,22 @@
 #ifndef NDNCERT_CLIENT_CONFIG_HPP
 #define NDNCERT_CLIENT_CONFIG_HPP
 
-#include "ca-config.hpp"
-#include <boost/filesystem.hpp>
+#include "certificate-request.hpp"
+#include <ndn-cxx/security/v2/certificate.hpp>
 
 namespace ndn {
 namespace ndncert {
 
+class ClientCaItem
+{
+public:
+  Name m_caName;
+  std::string m_caInfo;
+  std::string m_probe;
+  std::list<std::string> m_supportedChallenges;
+  security::v2::Certificate m_anchor;
+};
+
 class ClientConfig
 {
 public:
@@ -41,7 +51,7 @@
   load(const std::string& fileName);
 
   void
-  addNewCaItem(const CaItem& item);
+  addNewCaItem(const ClientCaItem& item);
 
   void
   removeCaItem(const Name& caName);
@@ -51,13 +61,13 @@
   parse();
 
   std::list<std::string>
-  parseChallengeList(const ConfigSection& section);
+  parseChallengeList(const JsonSection& section);
 
 public:
-  std::list<CaItem> m_caItems;
+  std::list<ClientCaItem> m_caItems;
 
 PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  ConfigSection m_config;
+  JsonSection m_config;
 };
 
 } // namespace ndncert
diff --git a/tests/unit-tests/ca-config.t.cpp b/tests/unit-tests/ca-config.t.cpp
index e803254..532aeeb 100644
--- a/tests/unit-tests/ca-config.t.cpp
+++ b/tests/unit-tests/ca-config.t.cpp
@@ -36,7 +36,7 @@
   config.load("tests/unit-tests/ca.conf.test");
   auto itemA = config.m_caItems.front();
   BOOST_CHECK_EQUAL(itemA.m_caName.toUri(), "/ndn/edu/ucla/cs/zhiyi");
-  BOOST_CHECK_EQUAL(itemA.m_probe, "true");
+  BOOST_CHECK(!itemA.m_probe);
   BOOST_CHECK_EQUAL(itemA.m_freshnessPeriod, time::seconds(720));
   BOOST_CHECK_EQUAL(itemA.m_validityPeriod, time::days(360));
   BOOST_CHECK_EQUAL(itemA.m_anchor.toUri(),
@@ -45,7 +45,7 @@
 
   auto itemB = config.m_caItems.back();
   BOOST_CHECK_EQUAL(itemB.m_caName.toUri(), "/ndn/site1");
-  BOOST_CHECK_EQUAL(itemB.m_probe, "true");
+  BOOST_CHECK(itemB.m_probe);
   BOOST_CHECK_EQUAL(itemB.m_freshnessPeriod, time::seconds(720));
   BOOST_CHECK_EQUAL(itemB.m_validityPeriod, time::days(360));
   BOOST_CHECK_EQUAL(itemB.m_anchor.toUri(),
diff --git a/tests/unit-tests/ca.conf.test b/tests/unit-tests/ca.conf.test
index 838d9c4..87db072 100644
--- a/tests/unit-tests/ca.conf.test
+++ b/tests/unit-tests/ca.conf.test
@@ -3,8 +3,6 @@
   [
     {
         "ca-prefix": "/ndn/edu/ucla/cs/zhiyi",
-        "ca-info": "UCLA's ceritificate authority, located in BH4805.",
-        "probe": "true",
         "issuing-freshness": "720",
         "validity-period": "360",
         "ca-anchor": "/ndn/edu/ucla/cs/zhiyi/KEY/%9A%E0%C6%C6%09%7C%92i/self/%FD%00%00%01Z%B0%2AJ%B4",
@@ -15,7 +13,6 @@
     },
     {
         "ca-prefix": "/ndn/site1",
-        "ca-info": "UCLA's ceritificate authority, located in BH4805.",
         "probe": "true",
         "issuing-freshness": "720",
         "validity-period": "360",
diff --git a/tests/unit-tests/client-config.t.cpp b/tests/unit-tests/client-config.t.cpp
index f9c7530..e1ad082 100644
--- a/tests/unit-tests/client-config.t.cpp
+++ b/tests/unit-tests/client-config.t.cpp
@@ -39,6 +39,8 @@
   BOOST_CHECK_EQUAL(item.m_probe, "Please use your email address to apply a namespace first. UCLA email is preferred.");
   BOOST_CHECK_EQUAL(item.m_supportedChallenges.size(), 2);
   BOOST_CHECK_EQUAL(item.m_supportedChallenges.front(), "PIN");
+  BOOST_CHECK_EQUAL(item.m_anchor.getName().toUri(),
+                    "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
 }
 
 BOOST_AUTO_TEST_CASE(AddAndRemoveCaItem)
@@ -46,7 +48,7 @@
   ClientConfig config;
   config.load("tests/unit-tests/client.conf.test");
 
-  CaItem item;
+  ClientCaItem item;
   item.m_caName = Name("/test");
   item.m_caInfo = "test";
   item.m_probe = "test";
diff --git a/tests/unit-tests/client.conf.test b/tests/unit-tests/client.conf.test
index 745a43b..7c2dbf1 100644
--- a/tests/unit-tests/client.conf.test
+++ b/tests/unit-tests/client.conf.test
@@ -5,6 +5,7 @@
         "ca-prefix": "/ndn/edu/ucla/CA",
         "ca-info": "UCLA's ceritificate authority, located in BH4805.",
         "probe": "Please use your email address to apply a namespace first. UCLA email is preferred.",
+        "certificate": "Bv0CJAcsCANuZG4IBXNpdGUxCANLRVkICBG8IvRjFf8XCARzZWxmCAn9AAABWcgU2aUUCRgBAhkEADbugBX9AU8wggFLMIIBAwYHKoZIzj0CATCB9wIBATAsBgcqhkjOPQEBAiEA/////wAAAAEAAAAAAAAAAAAAAAD///////////////8wWwQg/////wAAAAEAAAAAAAAAAAAAAAD///////////////wEIFrGNdiqOpPns+u9VXaYhrxlHQawzFOw9jvOPD4n0mBLAxUAxJ02CIbnBJNqZnjhE50mt4GffpAEQQRrF9Hy4SxCR/i85uVjpEDydwN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQEDQgAES9Cb9iANUNYmwt5bjwNW1mZgjzIkDJb6FTCdiYWnkMMIVxh2YDllphoWDEAPS6kqJczzCuhnGYpZCp9tTaYKGxZMGwEDHB0HGwgDbmRuCAVzaXRlMQgDS0VZCAgRvCL0YxX/F/0A/Sb9AP4PMTk3MDAxMDFUMDAwMDAw/QD/DzIwMzcwMTE3VDIxMjg0NhdIMEYCIQDXkR1hF3GiP7yLXq+0JBJfi9QC+hhAu/1Bykx+MWz6RAIhANwelBTxxZr2C5bD15mjfhWudK4I1tOb4b/9xWCHyM7F",
         "supported-challenges":
         [
             { "type": "PIN" },
@@ -14,6 +15,7 @@
     {
         "ca-prefix": "/ndn/edu/ucla/zhiyi/CA",
         "ca-info": "Zhiyi's own ceritificate authority",
+        "certificate": "Bv0CJAcsCANuZG4IBXNpdGUxCANLRVkICBG8IvRjFf8XCARzZWxmCAn9AAABWcgU2aUUCRgBAhkEADbugBX9AU8wggFLMIIBAwYHKoZIzj0CATCB9wIBATAsBgcqhkjOPQEBAiEA/////wAAAAEAAAAAAAAAAAAAAAD///////////////8wWwQg/////wAAAAEAAAAAAAAAAAAAAAD///////////////wEIFrGNdiqOpPns+u9VXaYhrxlHQawzFOw9jvOPD4n0mBLAxUAxJ02CIbnBJNqZnjhE50mt4GffpAEQQRrF9Hy4SxCR/i85uVjpEDydwN9gS3rM6D0oTlF2JjClk/jQuL+Gn+bjufrSnwPnhYrzjNXazFezsu2QGg3v1H1AiEA/////wAAAAD//////////7zm+q2nF56E87nKwvxjJVECAQEDQgAES9Cb9iANUNYmwt5bjwNW1mZgjzIkDJb6FTCdiYWnkMMIVxh2YDllphoWDEAPS6kqJczzCuhnGYpZCp9tTaYKGxZMGwEDHB0HGwgDbmRuCAVzaXRlMQgDS0VZCAgRvCL0YxX/F/0A/Sb9AP4PMTk3MDAxMDFUMDAwMDAw/QD/DzIwMzcwMTE3VDIxMjg0NhdIMEYCIQDXkR1hF3GiP7yLXq+0JBJfi9QC+hhAu/1Bykx+MWz6RAIhANwelBTxxZr2C5bD15mjfhWudK4I1tOb4b/9xWCHyM7F",
         "supported-challenges":
         [
             { "type": "PIN" }