CR fix 2

Change-Id: Ib5b7d7ff17f6ae31e9b6d905ef53d015a7389ccc
diff --git a/src/ca-module.cpp b/src/ca-module.cpp
index 8b8d4bb..42ea9e2 100644
--- a/src/ca-module.cpp
+++ b/src/ca-module.cpp
@@ -112,7 +112,7 @@
 void
 CaModule::setStatusUpdateCallback(const StatusUpdateCallback& onUpdateCallback)
 {
-  m_config.m_statusUpdateCallback = onUpdateCallback;
+  m_statusUpdateCallback = onUpdateCallback;
 }
 
 Data
@@ -326,8 +326,8 @@
                                                              m_config.m_caItem.m_supportedChallenges));
   m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
   m_face.put(result);
-  if (m_config.m_statusUpdateCallback) {
-    m_config.m_statusUpdateCallback(requestState);
+  if (m_statusUpdateCallback) {
+    m_statusUpdateCallback(requestState);
   }
 }
 
@@ -424,8 +424,8 @@
   result.setContent(payload);
   m_keyChain.sign(result, signingByIdentity(m_config.m_caItem.m_caPrefix));
   m_face.put(result);
-  if (m_config.m_statusUpdateCallback) {
-    m_config.m_statusUpdateCallback(*requestState);
+  if (m_statusUpdateCallback) {
+    m_statusUpdateCallback(*requestState);
   }
 }
 
diff --git a/src/ca-module.hpp b/src/ca-module.hpp
index 209352c..5a3da6a 100644
--- a/src/ca-module.hpp
+++ b/src/ca-module.hpp
@@ -29,6 +29,16 @@
 namespace ndncert {
 namespace ca {
 
+/**
+ * @brief The function would be invoked whenever the certificate request status is updated.
+ * The callback is used to notice the CA application or CA command line tool. The callback is
+ * fired whenever a request instance is created, challenge status is updated, and when certificate
+ * is issued.
+ *
+ * @param RequestState The state of the certificate request whose status is updated.
+ */
+using StatusUpdateCallback = function<void(const RequestState&)>;
+
 class CaModule : noncopyable
 {
 public:
@@ -90,6 +100,10 @@
   security::KeyChain& m_keyChain;
   uint8_t m_requestIdGenKey[32];
   std::unique_ptr<Data> m_profileData;
+  /**
+   * StatusUpdate Callback function
+   */
+  StatusUpdateCallback m_statusUpdateCallback;
 
   std::list<RegisteredPrefixHandle> m_registeredPrefixHandles;
   std::list<InterestFilterHandle> m_interestFilterHandles;
diff --git a/src/configuration.cpp b/src/configuration.cpp
index f53ebda..a56f490 100644
--- a/src/configuration.cpp
+++ b/src/configuration.cpp
@@ -251,6 +251,12 @@
   m_caItems.push_back(profile);
 }
 
+const std::list<CaProfile>&
+ProfileStorage::getCaItems() const
+{
+  return m_caItems;
+}
+
 } // namespace requester
 } // namespace ndncert
 } // namespace ndn
diff --git a/src/configuration.hpp b/src/configuration.hpp
index ec8ccb1..4e466e2 100644
--- a/src/configuration.hpp
+++ b/src/configuration.hpp
@@ -28,6 +28,21 @@
 namespace ndncert {
 
 struct CaProfile {
+public:
+  /**
+   * Parse the configuration json and modify current struct to the result.
+   * @param configJson the configuration json to parse
+   */
+  void
+  parse(const JsonSection& configJson);
+
+  /**
+   * @return the JSON representation of this profile.
+   */
+  JsonSection
+  toJson() const;
+
+public:
   /**
    * CA Name prefix (without /CA suffix).
    */
@@ -65,12 +80,6 @@
    */
   std::shared_ptr<security::Certificate> m_cert;
 
-  void
-  parse(const JsonSection& configJson);
-
-  JsonSection
-  toJson() const;
-
 private:
   void
   parseProbeParameters(const JsonSection& section);
@@ -82,19 +91,8 @@
 namespace ca {
 
 /**
- * @brief The function would be invoked whenever the certificate request status is updated.
- * The callback is used to notice the CA application or CA command line tool. The callback is
- * fired whenever a request instance is created, challenge status is updated, and when certificate
- * is issued.
- *
- * @param RequestState The state of the certificate request whose status is updated.
- */
-using StatusUpdateCallback = function<void(const RequestState&)>;
-
-/**
  * @brief CA's configuration on NDNCERT.
- * For CA configuration format, please refer to:
- *   https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#213-ca-profile
+ * @sa https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3#213-ca-profile
  *
  * The format of CA configuration in JSON
  * {
@@ -124,17 +122,14 @@
   void
   load(const std::string& fileName);
 
+public:
   CaProfile m_caItem;
   /**
-   * Used for CA redirection as specified in
-   * https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3-PROBE-Extensions#probe-extension-for-redirection
+   * Used for CA redirection
+   * @sa https://github.com/named-data/ndncert/wiki/NDNCERT-Protocol-0.3-PROBE-Extensions#probe-extension-for-redirection
    */
   optional<std::vector<std::shared_ptr<security::Certificate>>> m_redirection = nullopt;
   /**
-   * StatusUpdate Callback function
-   */
-  StatusUpdateCallback m_statusUpdateCallback;
-  /**
    * Name Assignment Functions
    */
   std::vector<std::unique_ptr<NameAssignmentFunc>> m_nameAssignmentFuncs;
@@ -146,9 +141,7 @@
 
 /**
  * @brief Represents Client configuration
- *
- * For Client configuration format, please refer to:
- *   https://github.com/named-data/ndncert/wiki/Client-Configuration-Sample
+ * @sa https://github.com/named-data/ndncert/wiki/Client-Configuration-Sample
  */
 class ProfileStorage
 {
@@ -177,6 +170,10 @@
   void
   addCaProfile(const CaProfile& profile);
 
+  const std::list<CaProfile>&
+  getCaItems() const;
+
+private:
   std::list<CaProfile> m_caItems;
 };
 
diff --git a/src/detail/challenge-encoder.cpp b/src/detail/challenge-encoder.cpp
index 6d68869..0cc98a3 100644
--- a/src/detail/challenge-encoder.cpp
+++ b/src/detail/challenge-encoder.cpp
@@ -26,7 +26,7 @@
 Block
 challengeEncoder::encodeDataContent(ca::RequestState& request, const Name& issuedCertName)
 {
-  Block response = makeEmptyBlock(tlv::EncryptedPayload);
+  Block response(tlv::EncryptedPayload);
   response.push_back(makeNonNegativeIntegerBlock(tlv::Status, static_cast<uint64_t>(request.m_status)));
   if (request.m_challengeState) {
     response.push_back(makeStringBlock(tlv::ChallengeStatus, request.m_challengeState->m_challengeStatus));
diff --git a/src/detail/crypto-helpers.cpp b/src/detail/crypto-helpers.cpp
index 3142268..cc09a95 100644
--- a/src/detail/crypto-helpers.cpp
+++ b/src/detail/crypto-helpers.cpp
@@ -356,7 +356,7 @@
   uint8_t tag[16];
   size_t encryptedPayloadLen = aesGcm128Encrypt(payload, payloadSize, associatedData, associatedDataSize,
                                                 key, iv.data(), encryptedPayload.data(), tag);
-  auto content = makeEmptyBlock(tlvType);
+  Block content(tlvType);
   content.push_back(makeBinaryBlock(tlv::InitializationVector, iv.data(), iv.size()));
   content.push_back(makeBinaryBlock(tlv::AuthenticationTag, tag, 16));
   content.push_back(makeBinaryBlock(tlv::EncryptedPayload, encryptedPayload.data(), encryptedPayloadLen));
diff --git a/src/detail/error-encoder.cpp b/src/detail/error-encoder.cpp
index 8d30103..b503f86 100644
--- a/src/detail/error-encoder.cpp
+++ b/src/detail/error-encoder.cpp
@@ -26,7 +26,7 @@
 Block
 errorEncoder::encodeDataContent(ErrorCode errorCode, const std::string& description)
 {
-  Block response = makeEmptyBlock(ndn::tlv::Content);
+  Block response(ndn::tlv::Content);
   response.push_back(makeNonNegativeIntegerBlock(tlv::ErrorCode, static_cast<size_t>(errorCode)));
   response.push_back(makeStringBlock(tlv::ErrorInfo, description));
   response.encode();
diff --git a/src/detail/info-encoder.cpp b/src/detail/info-encoder.cpp
index 0a26f8a..746aafc 100644
--- a/src/detail/info-encoder.cpp
+++ b/src/detail/info-encoder.cpp
@@ -26,7 +26,7 @@
 Block
 infoEncoder::encodeDataContent(const CaProfile& caConfig, const security::Certificate& certificate)
 {
-  auto content = makeEmptyBlock(ndn::tlv::Content);
+  Block content(ndn::tlv::Content);
   content.push_back(makeNestedBlock(tlv::CaPrefix, caConfig.m_caPrefix));
   std::string caInfo = "";
   if (caConfig.m_caInfo == "") {
diff --git a/src/detail/new-renew-revoke-encoder.cpp b/src/detail/new-renew-revoke-encoder.cpp
index 0863d9b..bdde1cf 100644
--- a/src/detail/new-renew-revoke-encoder.cpp
+++ b/src/detail/new-renew-revoke-encoder.cpp
@@ -32,7 +32,7 @@
 newRenewRevokeEncoder::encodeApplicationParameters(RequestType requestType, const std::vector<uint8_t>& ecdhPub,
                                                    const security::Certificate& certRequest)
 {
-  Block request = makeEmptyBlock(ndn::tlv::ApplicationParameters);
+  Block request(ndn::tlv::ApplicationParameters);
   std::stringstream ss;
   try {
     security::transform::bufferSource(certRequest.wireEncode().wire(), certRequest.wireEncode().size())
@@ -84,7 +84,7 @@
                                          const RequestId& requestId, const Status& status,
                                          const std::list<std::string>& challenges)
 {
-  Block response = makeEmptyBlock(ndn::tlv::Content);
+  Block response(ndn::tlv::Content);
   response.push_back(makeBinaryBlock(tlv::EcdhPub, ecdhKey.data(), ecdhKey.size()));
   response.push_back(makeBinaryBlock(tlv::Salt, salt.data(), salt.size()));
   response.push_back(makeBinaryBlock(tlv::RequestId, requestId.data(), requestId.size()));
diff --git a/src/detail/probe-encoder.cpp b/src/detail/probe-encoder.cpp
index a4271f5..2a5fa08 100644
--- a/src/detail/probe-encoder.cpp
+++ b/src/detail/probe-encoder.cpp
@@ -26,7 +26,7 @@
 Block
 probeEncoder::encodeApplicationParameters(std::vector<std::tuple<std::string, std::string>>&& parameters)
 {
-  auto content = makeEmptyBlock(ndn::tlv::ApplicationParameters);
+  Block content(ndn::tlv::ApplicationParameters);
   for (size_t i = 0; i < parameters.size(); ++i) {
     content.push_back(makeStringBlock(tlv::ParameterKey, std::get<0>(parameters[i])));
     content.push_back(makeStringBlock(tlv::ParameterValue, std::get<1>(parameters[i])));
@@ -40,9 +40,10 @@
 {
   std::vector<std::tuple<std::string, std::string>> result;
   block.parse();
-  for (size_t i = 0; i < block.elements().size() - 1; ++i) {
-    if (block.elements().at(i).type() == tlv::ParameterKey && block.elements().at(i + 1).type() == tlv::ParameterValue) {
-      result.push_back(std::make_tuple(readString(block.elements().at(i)), readString(block.elements().at(i + 1))));
+  for (size_t i = 0; i < block.elements().size() - 1; i++) {
+    if (block.elements()[i].type() == tlv::ParameterKey && block.elements()[i + 1].type() == tlv::ParameterValue) {
+      result.emplace_back(readString(block.elements().at(i)), readString(block.elements().at(i + 1)));
+      i ++;
     }
   }
   return result;
@@ -52,7 +53,7 @@
 probeEncoder::encodeDataContent(const std::vector<Name>& identifiers, optional<size_t> maxSuffixLength,
                                 optional<std::vector<std::shared_ptr<security::Certificate>>> redirectionItems)
 {
-  Block content = makeEmptyBlock(ndn::tlv::Content);
+  Block content(ndn::tlv::Content);
   for (const auto& name : identifiers) {
     Block item(tlv::ProbeResponse);
     item.push_back(name.wireEncode());
@@ -81,7 +82,7 @@
       item.parse();
       Name elementName;
       int maxSuffixLength = 0;
-      for (const auto& subBlock: item.elements()) {
+      for (const auto& subBlock : item.elements()) {
           if (subBlock.type() == ndn::tlv::Name) {
               if (!elementName.empty()) {
                   NDN_THROW(std::runtime_error("Invalid probe format"));
diff --git a/src/identity-challenge/challenge-credential.cpp b/src/identity-challenge/challenge-credential.cpp
index adee8b0..7e72805 100644
--- a/src/identity-challenge/challenge-credential.cpp
+++ b/src/identity-challenge/challenge-credential.cpp
@@ -137,7 +137,7 @@
 ChallengeCredential::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
                                             std::vector<std::tuple<std::string, std::string>>&& params)
 {
-  Block request = makeEmptyBlock(tlv::EncryptedPayload);
+  Block request(tlv::EncryptedPayload);
   if (status == Status::BEFORE_CHALLENGE) {
     if (params.size() != 2) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
@@ -146,7 +146,7 @@
     for (const auto& item : params) {
       if (std::get<0>(item) == PARAMETER_KEY_CREDENTIAL_CERT) {
         request.push_back(makeStringBlock(tlv::ParameterKey, PARAMETER_KEY_CREDENTIAL_CERT));
-        Block valueBlock = makeEmptyBlock(tlv::ParameterValue);
+        Block valueBlock(tlv::ParameterValue);
         auto& certTlvStr = std::get<1>(item);
         valueBlock.push_back(Block((uint8_t*)certTlvStr.c_str(), certTlvStr.size()));
         request.push_back(valueBlock);
diff --git a/src/identity-challenge/challenge-email.cpp b/src/identity-challenge/challenge-email.cpp
index e979753..332ef96 100644
--- a/src/identity-challenge/challenge-email.cpp
+++ b/src/identity-challenge/challenge-email.cpp
@@ -20,6 +20,7 @@
 
 #include "challenge-email.hpp"
 #include <regex>
+#include <boost/process.hpp>
 
 namespace ndn {
 namespace ndncert {
@@ -125,7 +126,7 @@
 ChallengeEmail::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
                                        std::vector<std::tuple<std::string, std::string>>&& params)
 {
-  Block request = makeEmptyBlock(tlv::EncryptedPayload);
+  Block request(tlv::EncryptedPayload);
   if (status == Status::BEFORE_CHALLENGE) {
     if (params.size() != 1 || std::get<0>(params[0]) != PARAMETER_KEY_EMAIL) {
       NDN_THROW(std::runtime_error("Wrong parameter provided."));
@@ -165,13 +166,13 @@
   command += " \"" + emailAddress + "\" \"" + secret + "\" \"" +
              request.m_caPrefix.toUri() + "\" \"" +
              request.m_cert.getName().toUri() + "\"";
-  int result = system(command.c_str());
-  if (result == -1) {
+  boost::process::child child(command);
+  child.wait();
+  if (child.exit_code() != 0) {
     NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript + " fails.");
   }
   NDN_LOG_TRACE("EmailSending Script " + m_sendEmailScript +
-             " was executed successfully with return value" + std::to_string(result) + ".");
-  return;
+             " was executed successfully with return value 0.");
 }
 
 } // namespace ndncert
diff --git a/src/identity-challenge/challenge-pin.cpp b/src/identity-challenge/challenge-pin.cpp
index 66f5fec..34b987d 100644
--- a/src/identity-challenge/challenge-pin.cpp
+++ b/src/identity-challenge/challenge-pin.cpp
@@ -107,7 +107,7 @@
 ChallengePin::genChallengeRequestTLV(Status status, const std::string& challengeStatus,
                                      std::vector<std::tuple<std::string, std::string>>&& params)
 {
-  Block request = makeEmptyBlock(tlv::EncryptedPayload);
+  Block request(tlv::EncryptedPayload);
   if (status == Status::BEFORE_CHALLENGE) {
     request.push_back(makeStringBlock(tlv::SelectedChallenge, CHALLENGE_TYPE));
   }
diff --git a/src/name-assignment/assignment-func.cpp b/src/name-assignment/assignment-func.cpp
index 6a55cc2..22eb35e 100644
--- a/src/name-assignment/assignment-func.cpp
+++ b/src/name-assignment/assignment-func.cpp
@@ -43,15 +43,15 @@
 unique_ptr<NameAssignmentFunc>
 NameAssignmentFunc::createNameAssignmentFunc(const std::string& challengeType, const std::string& format)
 {
-  FuncFactoryFactory& factory = getFactory();
+  CurriedFuncFactory& factory = getFactory();
   auto i = factory.find(challengeType);
   return i == factory.end() ? nullptr : i->second(format);
 }
 
-NameAssignmentFunc::FuncFactoryFactory&
+NameAssignmentFunc::CurriedFuncFactory&
 NameAssignmentFunc::getFactory()
 {
-  static NameAssignmentFunc::FuncFactoryFactory factory;
+  static NameAssignmentFunc::CurriedFuncFactory factory;
   return factory;
 }
 
diff --git a/src/name-assignment/assignment-func.hpp b/src/name-assignment/assignment-func.hpp
index a574d05..e122ed2 100644
--- a/src/name-assignment/assignment-func.hpp
+++ b/src/name-assignment/assignment-func.hpp
@@ -28,9 +28,10 @@
 
 class NameAssignmentFunc : noncopyable
 {
-public:
+protected:
   explicit NameAssignmentFunc(const std::string& factoryType, const std::string& format = "");
 
+public:
   virtual ~NameAssignmentFunc() = default;
 
   /**
@@ -47,26 +48,28 @@
   assignName(const std::vector<std::tuple<std::string, std::string>>& params) = 0;
 
   const std::string FACTORY_TYPE;
-  std::vector<std::string> m_nameFormat;
 
 public:
-  template <class ChallengeType>
+  template <class AssignmentType>
   static void
   registerNameAssignmentFunc(const std::string& typeName)
   {
-    FuncFactoryFactory& factory = getFactory();
+    CurriedFuncFactory& factory = getFactory();
     BOOST_ASSERT(factory.count(typeName) == 0);
-    factory[typeName] = [](const std::string& format) { return std::make_unique<ChallengeType>(format); };
+    factory[typeName] = [](const std::string& format) { return std::make_unique<AssignmentType>(format); };
   }
 
   static unique_ptr<NameAssignmentFunc>
   createNameAssignmentFunc(const std::string& challengeType, const std::string& format = "");
 
+NDNCERT_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+    std::vector<std::string> m_nameFormat;
+    
 private:
   typedef function<unique_ptr<NameAssignmentFunc>(const std::string&)> FactoryCreateFunc;
-  typedef std::map<std::string, FactoryCreateFunc> FuncFactoryFactory;
+  typedef std::map<std::string, FactoryCreateFunc> CurriedFuncFactory;
 
-  static FuncFactoryFactory&
+  static CurriedFuncFactory&
   getFactory();
 };
 
diff --git a/src/name-assignment/assignment-param.hpp b/src/name-assignment/assignment-param.hpp
index bc1a084..8a31dc6 100644
--- a/src/name-assignment/assignment-param.hpp
+++ b/src/name-assignment/assignment-param.hpp
@@ -36,6 +36,7 @@
 
   std::vector<PartialName>
   assignName(const std::vector<std::tuple<std::string, std::string>>& params) override;
+
 };
 
 } // namespace ndncert