Improve error handling and reporting

Change-Id: I41a58409c1d488ab2038ae534851e1ab59c6d952
diff --git a/src/conf-file-processor.cpp b/src/conf-file-processor.cpp
index ec1718b..4140e0d 100644
--- a/src/conf-file-processor.cpp
+++ b/src/conf-file-processor.cpp
@@ -21,11 +21,12 @@
 
 #include "conf-file-processor.hpp"
 #include "adjacent.hpp"
-#include "utility/name-helper.hpp"
 #include "update/prefix-update-processor.hpp"
+#include "utility/name-helper.hpp"
 
 #include <ndn-cxx/name.hpp>
 #include <ndn-cxx/net/face-uri.hpp>
+#include <ndn-cxx/util/io.hpp>
 
 #include <boost/filesystem.hpp>
 #include <boost/property_tree/info_parser.hpp>
@@ -144,24 +145,19 @@
 bool
 ConfFileProcessor::processConfFile()
 {
-  bool ret = true;
-  std::ifstream inputFile;
-  inputFile.open(m_confFileName.c_str());
+  std::ifstream inputFile(m_confFileName);
   if (!inputFile.is_open()) {
-    std::string msg = "Failed to read configuration file: ";
-    msg += m_confFileName;
-    std::cerr << msg << std::endl;
+    std::cerr << "Failed to read configuration file: " << m_confFileName << std::endl;
     return false;
   }
-  ret = load(inputFile);
-  inputFile.close();
 
-  if (ret) {
-    m_confParam.buildRouterAndSyncUserPrefix();
-    m_confParam.writeLog();
+  if (!load(inputFile)) {
+    return false;
   }
 
-  return ret;
+  m_confParam.buildRouterAndSyncUserPrefix();
+  m_confParam.writeLog();
+  return true;
 }
 
 bool
@@ -171,10 +167,9 @@
   try {
     boost::property_tree::read_info(input, pt);
   }
-  catch (const boost::property_tree::info_parser_error& error) {
-    std::stringstream msg;
-    std::cerr << "Failed to parse configuration file " << std::endl;
-    std::cerr << m_confFileName << std::endl;
+  catch (const boost::property_tree::ptree_error& e) {
+    std::cerr << "Failed to parse configuration file '" << m_confFileName
+              << "': " << e.what() << std::endl;
     return false;
   }
 
@@ -190,33 +185,26 @@
 ConfFileProcessor::processSection(const std::string& sectionName, const ConfigSection& section)
 {
   bool ret = true;
-  if (sectionName == "general")
-  {
+  if (sectionName == "general") {
     ret = processConfSectionGeneral(section);
   }
-  else if (sectionName == "neighbors")
-  {
+  else if (sectionName == "neighbors") {
     ret = processConfSectionNeighbors(section);
   }
-  else if (sectionName == "hyperbolic")
-  {
+  else if (sectionName == "hyperbolic") {
     ret = processConfSectionHyperbolic(section);
   }
-  else if (sectionName == "fib")
-  {
+  else if (sectionName == "fib") {
     ret = processConfSectionFib(section);
   }
-  else if (sectionName == "advertising")
-  {
+  else if (sectionName == "advertising") {
     ret = processConfSectionAdvertising(section);
   }
-  else if (sectionName == "security")
-  {
+  else if (sectionName == "security") {
     ret = processConfSectionSecurity(section);
   }
-  else
-  {
-    std::cerr << "Wrong configuration section: " << sectionName << std::endl;
+  else {
+    std::cerr << "Unknown configuration section: " << sectionName << std::endl;
   }
   return ret;
 }
@@ -233,7 +221,7 @@
       m_confParam.setNetwork(networkName);
     }
     else {
-      std::cerr << " Network can not be null or empty or in bad URI format :(!" << std::endl;
+      std::cerr << "Network can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
     ndn::Name siteName(site);
@@ -241,7 +229,7 @@
       m_confParam.setSiteName(siteName);
     }
     else {
-      std::cerr << "Site can not be null or empty or in bad URI format:( !" << std::endl;
+      std::cerr << "Site can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
     ndn::Name routerName(router);
@@ -249,7 +237,7 @@
       m_confParam.setRouterName(routerName);
     }
     else {
-      std::cerr << " Router name can not be null or empty or in bad URI format:( !" << std::endl;
+      std::cerr << "Router name can not be null or empty or in bad URI format" << std::endl;
       return false;
     }
   }
@@ -265,15 +253,14 @@
     m_confParam.setLsaRefreshTime(lsaRefreshTime);
   }
   else {
-    std::cerr << "Wrong value for lsa-refresh-time ";
-    std::cerr << "Allowed value: " << LSA_REFRESH_TIME_MIN << "-";
-    std::cerr << LSA_REFRESH_TIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for lsa-refresh-time. "
+              << "Allowed range: " << LSA_REFRESH_TIME_MIN
+              << "-" << LSA_REFRESH_TIME_MAX << std::endl;
     return false;
   }
 
   // router-dead-interval
-  uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", (2*lsaRefreshTime));
+  uint32_t routerDeadInterval = section.get<uint32_t>("router-dead-interval", 2 * lsaRefreshTime);
 
   if (routerDeadInterval > m_confParam.getLsaRefreshTime()) {
     m_confParam.setRouterDeadInterval(routerDeadInterval);
@@ -290,10 +277,9 @@
     m_confParam.setLsaInterestLifetime(ndn::time::seconds(lifetime));
   }
   else {
-    std::cerr << "Wrong value for lsa-interest-timeout. "
-              << "Allowed value:" << LSA_INTEREST_LIFETIME_MIN << "-"
-              << LSA_INTEREST_LIFETIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for lsa-interest-timeout. "
+              << "Allowed range: " << LSA_INTEREST_LIFETIME_MIN
+              << "-" << LSA_INTEREST_LIFETIME_MAX << std::endl;
     return false;
   }
 
@@ -312,7 +298,7 @@
     m_confParam.setSyncProtocol(SYNC_PROTOCOL_PSYNC);
   }
   else {
-    std::cerr << "Sync protocol " << syncProtocol << " is not supported!"
+    std::cerr << "Sync protocol '" << syncProtocol << "' is not supported!\n"
               << "Use chronosync or psync" << std::endl;
     return false;
   }
@@ -325,10 +311,9 @@
     m_confParam.setSyncInterestLifetime(syncInterestLifetime);
   }
   else {
-    std::cerr << "Wrong value for sync-interest-lifetime. "
-              << "Allowed value:" << SYNC_INTEREST_LIFETIME_MIN << "-"
-              << SYNC_INTEREST_LIFETIME_MAX << std::endl;
-
+    std::cerr << "Invalid value for sync-interest-lifetime. "
+              << "Allowed range: " << SYNC_INTEREST_LIFETIME_MIN
+              << "-" << SYNC_INTEREST_LIFETIME_MAX << std::endl;
     return false;
   }
 
@@ -405,10 +390,8 @@
     m_confParam.setInterestRetryNumber(retrials);
   }
   else {
-    std::cerr << "Wrong value for hello-retries." << std::endl;
-    std::cerr << "Allowed value:" << HELLO_RETRIES_MIN << "-";
-    std::cerr << HELLO_RETRIES_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-retries. "
+              << "Allowed range: " << HELLO_RETRIES_MIN << "-" << HELLO_RETRIES_MAX << std::endl;
     return false;
   }
 
@@ -419,10 +402,8 @@
     m_confParam.setInterestResendTime(timeOut);
   }
   else {
-    std::cerr << "Wrong value for hello-timeout. ";
-    std::cerr << "Allowed value:" << HELLO_TIMEOUT_MIN << "-";
-    std::cerr << HELLO_TIMEOUT_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-timeout. "
+              << "Allowed range: " << HELLO_TIMEOUT_MIN << "-" << HELLO_TIMEOUT_MAX << std::endl;
     return false;
   }
 
@@ -433,10 +414,8 @@
     m_confParam.setInfoInterestInterval(interval);
   }
   else {
-    std::cerr << "Wrong value for hello-interval. ";
-    std::cerr << "Allowed value:" << HELLO_INTERVAL_MIN << "-";
-    std::cerr << HELLO_INTERVAL_MAX << std::endl;
-
+    std::cerr << "Invalid value for hello-interval. "
+              << "Allowed range: " << HELLO_INTERVAL_MIN << "-" << HELLO_INTERVAL_MAX << std::endl;
     return false;
   }
 
@@ -454,8 +433,7 @@
   // Set the retry count for fetching the FaceStatus dataset
   ConfigurationVariable<uint32_t> faceDatasetFetchTries("face-dataset-fetch-tries",
                                                         std::bind(&ConfParameter::setFaceDatasetFetchTries,
-                                                                  &m_confParam,
-                                                                  _1));
+                                                                  &m_confParam, _1));
 
   faceDatasetFetchTries.setMinAndMaxValue(FACE_DATASET_FETCH_TRIES_MIN,
                                           FACE_DATASET_FETCH_TRIES_MAX);
@@ -468,8 +446,7 @@
   // Set the interval between FaceStatus dataset fetch attempts.
   ConfigurationVariable<uint32_t> faceDatasetFetchInterval("face-dataset-fetch-interval",
                                                            std::bind(&ConfParameter::setFaceDatasetFetchInterval,
-                                                                     &m_confParam,
-                                                                     _1));
+                                                                     &m_confParam, _1));
 
   faceDatasetFetchInterval.setMinAndMaxValue(FACE_DATASET_FETCH_INTERVAL_MIN,
                                              FACE_DATASET_FETCH_INTERVAL_MAX);
@@ -542,13 +519,12 @@
   else if (boost::iequals(state, "on")) {
     m_confParam.setHyperbolicState(HYPERBOLIC_STATE_ON);
   }
-  else if (state == "dry-run") {
+  else if (boost::iequals(state, "dry-run")) {
     m_confParam.setHyperbolicState(HYPERBOLIC_STATE_DRY_RUN);
   }
   else {
-    std::cerr << "Wrong format for hyperbolic state." << std::endl;
-    std::cerr << "Allowed value: off, on, dry-run" << std::endl;
-
+    std::cerr << "Invalid setting for hyperbolic state. "
+              << "Allowed values: off, on, dry-run" << std::endl;
     return false;
   }
 
@@ -594,14 +570,13 @@
   int maxFacesPerPrefix = section.get<int>("max-faces-per-prefix", MAX_FACES_PER_PREFIX_DEFAULT);
 
   if (maxFacesPerPrefix >= MAX_FACES_PER_PREFIX_MIN &&
-      maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX)
-  {
+      maxFacesPerPrefix <= MAX_FACES_PER_PREFIX_MAX) {
     m_confParam.setMaxFacesPerPrefix(maxFacesPerPrefix);
   }
   else {
-    std::cerr << "Wrong value for max-faces-per-prefix. ";
-    std::cerr << MAX_FACES_PER_PREFIX_MIN << std::endl;
-
+    std::cerr << "Invalid value for max-faces-per-prefix. "
+              << "Allowed range: " << MAX_FACES_PER_PREFIX_MIN
+              << "-" << MAX_FACES_PER_PREFIX_MAX << std::endl;
     return false;
   }
 
@@ -646,10 +621,10 @@
 bool
 ConfFileProcessor::processConfSectionSecurity(const ConfigSection& section)
 {
-  ConfigSection::const_iterator it = section.begin();
+  auto it = section.begin();
 
   if (it == section.end() || it->first != "validator") {
-    std::cerr << "Error: Expect validator section!" << std::endl;
+    std::cerr << "Error: Expected validator section!" << std::endl;
     return false;
   }
 
@@ -661,22 +636,26 @@
 
     it++;
     for (; it != section.end(); it++) {
-
       if (it->first != "cert-to-publish") {
-        std::cerr << "Error: Expect cert-to-publish!" << std::endl;
+        std::cerr << "Error: Expected cert-to-publish!" << std::endl;
         return false;
       }
 
       std::string file = it->second.data();
       bf::path certfilePath = absolute(file, bf::path(m_confFileName).parent_path());
-      auto idCert = ndn::io::load<ndn::security::Certificate>(certfilePath.string());
+      std::ifstream ifs(certfilePath.string());
 
-      if (idCert == nullptr) {
-        std::cerr << "Error: Cannot load cert-to-publish: " << file << "!" << std::endl;
+      ndn::security::Certificate idCert;
+      try {
+        idCert = ndn::io::loadTlv<ndn::security::Certificate>(ifs);
+      }
+      catch (const std::exception& e) {
+        std::cerr << "Error: Cannot load cert-to-publish '" << file << "': " << e.what() << std::endl;
         return false;
       }
+
       m_confParam.addCertPath(certfilePath.string());
-      m_confParam.loadCertToValidator(*idCert);
+      m_confParam.loadCertToValidator(idCert);
     }
   }
 
diff --git a/src/security/certificate-store.cpp b/src/security/certificate-store.cpp
index 1a3025e..9fcaf84 100644
--- a/src/security/certificate-store.cpp
+++ b/src/security/certificate-store.cpp
@@ -24,6 +24,7 @@
 #include "logger.hpp"
 
 #include <ndn-cxx/util/io.hpp>
+#include <fstream>
 
 namespace nlsr {
 namespace security {
@@ -35,9 +36,9 @@
   , m_confParam(confParam)
   , m_validator(m_confParam.getValidator())
 {
-  for (const auto& x: confParam.getIdCerts()) {
-    auto idCert = ndn::io::load<ndn::security::Certificate>(x);
-    insert(*idCert);
+  for (const auto& certfile : confParam.getIdCerts()) {
+    std::ifstream ifs(certfile);
+    insert(ndn::io::loadTlv<ndn::security::Certificate>(ifs));
   }
 
   registerKeyPrefixes();
diff --git a/src/stats-collector.cpp b/src/stats-collector.cpp
index cb179ec..989e7a8 100644
--- a/src/stats-collector.cpp
+++ b/src/stats-collector.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  The University of Memphis,
+/*
+ * Copyright (c) 2014-2021,  The University of Memphis,
  *                           Regents of the University of California
  *
  * This file is part of NLSR (Named-data Link State Routing).
@@ -16,11 +16,10 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "stats-collector.hpp"
 #include "logger.hpp"
-#include <fstream>
 
 namespace nlsr {
 
@@ -28,13 +27,10 @@
   : m_lsdb(lsdb)
   , m_hp(hp)
 {
-  m_lsaIncrementConn =
-  this->m_lsdb.lsaIncrementSignal.connect(std::bind(&StatsCollector::statsIncrement,
-                                                    this, _1));
-
-  m_helloIncrementConn =
-  this->m_hp.hpIncrementSignal.connect(std::bind(&StatsCollector::statsIncrement,
-                                                 this, _1));
+  m_lsaIncrementConn = m_lsdb.lsaIncrementSignal.connect(std::bind(&StatsCollector::statsIncrement,
+                                                                   this, _1));
+  m_helloIncrementConn = m_hp.hpIncrementSignal.connect(std::bind(&StatsCollector::statsIncrement,
+                                                                  this, _1));
 }
 
 StatsCollector::~StatsCollector()
diff --git a/src/update/prefix-update-processor.cpp b/src/update/prefix-update-processor.cpp
index 99cd76b..d8302d4 100644
--- a/src/update/prefix-update-processor.cpp
+++ b/src/update/prefix-update-processor.cpp
@@ -28,6 +28,7 @@
 
 #include <boost/algorithm/string.hpp>
 #include <algorithm>
+#include <fstream>
 
 namespace nlsr {
 namespace update {
diff --git a/src/update/prefix-update-processor.hpp b/src/update/prefix-update-processor.hpp
index d705fa8..80f6564 100644
--- a/src/update/prefix-update-processor.hpp
+++ b/src/update/prefix-update-processor.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2020,  The University of Memphis,
+/*
+ * Copyright (c) 2014-2021,  The University of Memphis,
  *                           Regents of the University of California,
  *                           Arizona Board of Regents.
  *
@@ -17,18 +17,17 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NLSR, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NLSR_UPDATE_PREFIX_UPDATE_PROCESSOR_HPP
 #define NLSR_UPDATE_PREFIX_UPDATE_PROCESSOR_HPP
 
 #include "manager-base.hpp"
 #include "prefix-update-commands.hpp"
-#include <ndn-cxx/util/io.hpp>
+
 #include <ndn-cxx/security/key-chain.hpp>
-#include <ndn-cxx/util/io.hpp>
+
 #include <boost/property_tree/ptree.hpp>
-#include <boost/property_tree/info_parser.hpp>
 
 namespace nlsr {
 namespace update {
diff --git a/tests/security/test-certificate-store.cpp b/tests/security/test-certificate-store.cpp
index df74c28..ffb94d1 100644
--- a/tests/security/test-certificate-store.cpp
+++ b/tests/security/test-certificate-store.cpp
@@ -27,12 +27,12 @@
 
 #include <ndn-cxx/security/key-chain.hpp>
 #include <boost/lexical_cast.hpp>
+#include <boost/property_tree/info_parser.hpp>
 
 namespace nlsr {
 namespace test {
 
 using std::shared_ptr;
-using namespace nlsr::test;
 
 class CertificateStoreFixture : public UnitTestTimeFixture
 {
diff --git a/tests/update/test-save-delete-prefix.cpp b/tests/update/test-save-delete-prefix.cpp
index 7f93e9d..9843489 100644
--- a/tests/update/test-save-delete-prefix.cpp
+++ b/tests/update/test-save-delete-prefix.cpp
@@ -32,14 +32,14 @@
 #include <ndn-cxx/security/signing-helpers.hpp>
 
 #include <boost/filesystem.hpp>
+#include <boost/property_tree/info_parser.hpp>
 
 namespace nlsr {
 namespace test {
 
-namespace pt = boost::property_tree;
-using namespace pt;
+namespace bpt = boost::property_tree;
 
-class PrefixSaveDeleteFixture : public nlsr::test::UnitTestTimeFixture
+class PrefixSaveDeleteFixture : public UnitTestTimeFixture
 {
 public:
   PrefixSaveDeleteFixture()
@@ -70,8 +70,8 @@
     std::ifstream inputFile;
     inputFile.open(testConfFile);
     BOOST_REQUIRE(inputFile.is_open());
-    pt::ptree pt;
-    boost::property_tree::read_info(inputFile, pt);
+    bpt::ptree pt;
+    bpt::read_info(inputFile, pt);
     // Loads section and file name
     for (const auto& section : pt) {
       if (section.first == "security") {
@@ -117,20 +117,18 @@
   bool
   checkPrefix(const std::string prefixName)
   {
-    counter = 0;
-    pt::ptree m_savePrefix;
-    pt::info_parser::read_info(testConfFile, m_savePrefix);
+    bpt::ptree m_savePrefix;
+    bpt::read_info(testConfFile, m_savePrefix);
+
     // counter helps to check if multiple prefix of same name exists on conf file
+    counter = 0;
     for (const auto& section : m_savePrefix.get_child("advertising")) {
-      std:: string b = section.second.get_value<std::string>();
+      auto b = section.second.get_value<std::string>();
       if (b == prefixName) {
         counter++;
       }
     }
-    if (counter > 0) {
-      return true;
-    }
-    return false;
+    return counter > 0;
   }
 
   ndn::Interest
@@ -176,7 +174,6 @@
 
 BOOST_AUTO_TEST_CASE(Basic)
 {
-
   face.receive(advertiseWithdraw("/prefix/to/save", "advertise", false));
   this->advanceClocks(ndn::time::milliseconds(10));
   BOOST_CHECK_EQUAL(checkPrefix("/prefix/to/save"), false);