mgmt: Switch nfd::Controller to use v2::Validator interface

The conversion also includes relevant changes in SegmentFetcher and
tests.

Change-Id: Ie2f55bac650e3689f4971ab814a8bd51068a2a40
Refs: #3920
diff --git a/src/mgmt/nfd/controller.cpp b/src/mgmt/nfd/controller.cpp
index bfb0843..945c16d 100644
--- a/src/mgmt/nfd/controller.cpp
+++ b/src/mgmt/nfd/controller.cpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -20,9 +20,12 @@
  */
 
 #include "controller.hpp"
-#include "../../face.hpp"
-#include "../../security/key-chain.hpp"
-#include "../../util/segment-fetcher.hpp"
+#include "face.hpp"
+#include "security/v2/key-chain.hpp"
+#include "security/validator-null.hpp"
+#include "util/segment-fetcher.hpp"
+
+#include <boost/lexical_cast.hpp>
 
 namespace ndn {
 namespace nfd {
@@ -34,13 +37,12 @@
 const uint32_t Controller::ERROR_VALIDATION = 10021; // 10000 + TLS1_ALERT_DECRYPTION_FAILED
 const uint32_t Controller::ERROR_SERVER = 500;
 const uint32_t Controller::ERROR_LBOUND = 400;
-ValidatorNull Controller::s_validatorNull;
 
-Controller::Controller(Face& face, KeyChain& keyChain, Validator& validator)
+Controller::Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator)
   : m_face(face)
   , m_keyChain(keyChain)
-  , m_signer(keyChain)
   , m_validator(validator)
+  , m_signer(keyChain)
 {
 }
 
@@ -79,11 +81,11 @@
                                    const CommandFailCallback& onFailure)
 {
   m_validator.validate(data,
-    [=] (const shared_ptr<const Data>& data) {
-      this->processValidatedCommandResponse(*data, command, onSuccess, onFailure);
+    [=] (const Data& data) {
+      this->processValidatedCommandResponse(data, command, onSuccess, onFailure);
     },
-    [=] (const shared_ptr<const Data>&, const std::string& msg) {
-      onFailure(ControlResponse(ERROR_VALIDATION, msg));
+    [=] (const Data& data, const security::v2::ValidationError& error) {
+      onFailure(ControlResponse(ERROR_VALIDATION, boost::lexical_cast<std::string>(error)));
     }
   );
 }
diff --git a/src/mgmt/nfd/controller.hpp b/src/mgmt/nfd/controller.hpp
index d9bfa1f..6582a66 100644
--- a/src/mgmt/nfd/controller.hpp
+++ b/src/mgmt/nfd/controller.hpp
@@ -1,5 +1,5 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
+/*
  * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
@@ -26,15 +26,13 @@
 #include "control-response.hpp"
 #include "status-dataset.hpp"
 #include "command-options.hpp"
-#include "../../security/validator-null.hpp"
-#include "../../security/key-chain.hpp"
 #include "../../security/command-interest-signer.hpp"
+#include "../../security/validator-null.hpp"
+#include "../../security/v2/key-chain.hpp"
+#include "../../security/v2/validator.hpp"
 
 namespace ndn {
 
-namespace security {
-class Validator;
-} // namespace security
 class Face;
 
 namespace nfd {
@@ -67,7 +65,7 @@
   /** \brief construct a Controller that uses face for transport,
    *         and uses the passed KeyChain to sign commands
    */
-  Controller(Face& face, KeyChain& keyChain, security::Validator& validator = s_validatorNull);
+  Controller(Face& face, KeyChain& keyChain, security::v2::Validator& validator = security::getAcceptAllValidator());
 
   /** \brief start command execution
    */
@@ -172,11 +170,8 @@
 protected:
   Face& m_face;
   KeyChain& m_keyChain;
+  security::v2::Validator& m_validator;
   security::CommandInterestSigner m_signer;
-  security::Validator& m_validator;
-
-private:
-  static ValidatorNull s_validatorNull;
 };
 
 template<typename Dataset>
diff --git a/src/security/validator-null.cpp b/src/security/validator-null.cpp
new file mode 100644
index 0000000..6ca3174
--- /dev/null
+++ b/src/security/validator-null.cpp
@@ -0,0 +1,44 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
+ *
+ * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
+ *
+ * ndn-cxx library is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free Software
+ * Foundation, either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-cxx library 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 Lesser General Public License for more details.
+ *
+ * You should have received copies of the GNU General Public License and GNU Lesser
+ * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
+ */
+
+#include "validator-null.hpp"
+#include "v2/validation-policy-accept-all.hpp"
+#include "v2/certificate-fetcher-offline.hpp"
+
+namespace ndn {
+namespace security {
+namespace v2 {
+
+ValidatorNull::ValidatorNull()
+  : Validator(make_unique<ValidationPolicyAcceptAll>(), make_unique<CertificateFetcherOffline>())
+{
+}
+
+security::v2::Validator&
+getAcceptAllValidator()
+{
+  static security::ValidatorNull validator;
+  return validator;
+}
+
+} // namespace v2
+} // namespace security
+} // namespace ndn
diff --git a/src/security/validator-null.hpp b/src/security/validator-null.hpp
index 36448af..f8f48ce 100644
--- a/src/security/validator-null.hpp
+++ b/src/security/validator-null.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -17,53 +17,35 @@
  * <http://www.gnu.org/licenses/>.
  *
  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
- *
- * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
- * @author Jeff Thompson <jefft0@remap.ucla.edu>
  */
 
 #ifndef NDN_SECURITY_VALIDATOR_NULL_HPP
 #define NDN_SECURITY_VALIDATOR_NULL_HPP
 
-#include "validator.hpp"
+#include "v2/validator.hpp"
 
 namespace ndn {
 namespace security {
+namespace v2 {
 
+/**
+ * @brief Validator with "accept-all" policy and offline certificate fetcher
+ */
 class ValidatorNull : public Validator
 {
 public:
-  virtual
-  ~ValidatorNull()
-  {
-  }
-
-protected:
-  virtual void
-  checkPolicy(const Data& data,
-              int nSteps,
-              const OnDataValidated& onValidated,
-              const OnDataValidationFailed& onValidationFailed,
-              std::vector<shared_ptr<ValidationRequest> >& nextSteps)
-  {
-    onValidated(data.shared_from_this());
-  }
-
-  virtual void
-  checkPolicy(const Interest& interest,
-              int nSteps,
-              const OnInterestValidated& onValidated,
-              const OnInterestValidationFailed& onValidationFailed,
-              std::vector<shared_ptr<ValidationRequest> >& nextSteps)
-  {
-    onValidated(interest.shared_from_this());
-  }
+  ValidatorNull();
 };
 
+security::v2::Validator&
+getAcceptAllValidator();
+
+} // namespace v2
+
+using v2::ValidatorNull;
+using v2::getAcceptAllValidator;
+
 } // namespace security
-
-using security::ValidatorNull;
-
 } // namespace ndn
 
-#endif //NDN_SECURITY_VALIDATOR_NULL_HPP
+#endif // NDN_SECURITY_VALIDATOR_NULL_HPP
diff --git a/src/util/segment-fetcher.cpp b/src/util/segment-fetcher.cpp
index 70ad632..c8075e7 100644
--- a/src/util/segment-fetcher.cpp
+++ b/src/util/segment-fetcher.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2016 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -25,13 +25,15 @@
 #include "../lp/nack.hpp"
 #include "../lp/nack-header.hpp"
 
+#include <boost/lexical_cast.hpp>
+
 namespace ndn {
 namespace util {
 
 const uint32_t SegmentFetcher::MAX_INTEREST_REEXPRESS = 3;
 
 SegmentFetcher::SegmentFetcher(Face& face,
-                               shared_ptr<Validator> validator,
+                               shared_ptr<security::v2::Validator> validator,
                                const CompleteCallback& completeCallback,
                                const ErrorCallback& errorCallback)
   : m_face(face)
@@ -46,19 +48,18 @@
 void
 SegmentFetcher::fetch(Face& face,
                       const Interest& baseInterest,
-                      Validator& validator,
+                      security::v2::Validator& validator,
                       const CompleteCallback& completeCallback,
                       const ErrorCallback& errorCallback)
 {
-  shared_ptr<Validator> sharedValidator = shared_ptr<Validator>(&validator, [] (Validator*) {});
-
-  fetch(face, baseInterest, sharedValidator, completeCallback, errorCallback);
+  shared_ptr<security::v2::Validator> validatorPtr(&validator, [] (security::v2::Validator*) {});
+  fetch(face, baseInterest, validatorPtr, completeCallback, errorCallback);
 }
 
 void
 SegmentFetcher::fetch(Face& face,
                       const Interest& baseInterest,
-                      shared_ptr<Validator> validator,
+                      shared_ptr<security::v2::Validator> validator,
                       const CompleteCallback& completeCallback,
                       const ErrorCallback& errorCallback)
 {
@@ -106,29 +107,29 @@
   m_validator->validate(data,
                         bind(&SegmentFetcher::afterValidationSuccess, this, _1,
                              isSegmentZeroExpected, origInterest, self),
-                        bind(&SegmentFetcher::afterValidationFailure, this, _1));
+                        bind(&SegmentFetcher::afterValidationFailure, this, _1, _2));
 
 }
 
 void
-SegmentFetcher::afterValidationSuccess(const shared_ptr<const Data> data,
+SegmentFetcher::afterValidationSuccess(const Data& data,
                                        bool isSegmentZeroExpected,
                                        const Interest& origInterest,
                                        shared_ptr<SegmentFetcher> self)
 {
-  name::Component currentSegment = data->getName().get(-1);
+  name::Component currentSegment = data.getName().get(-1);
 
   if (currentSegment.isSegment()) {
     if (isSegmentZeroExpected && currentSegment.toSegment() != 0) {
-      fetchNextSegment(origInterest, data->getName(), 0, self);
+      fetchNextSegment(origInterest, data.getName(), 0, self);
     }
     else {
-      m_buffer->write(reinterpret_cast<const char*>(data->getContent().value()),
-                      data->getContent().value_size());
+      m_buffer->write(reinterpret_cast<const char*>(data.getContent().value()),
+                      data.getContent().value_size());
 
-      const name::Component& finalBlockId = data->getMetaInfo().getFinalBlockId();
+      const name::Component& finalBlockId = data.getMetaInfo().getFinalBlockId();
       if (finalBlockId.empty() || (finalBlockId > currentSegment)) {
-        fetchNextSegment(origInterest, data->getName(), currentSegment.toSegment() + 1, self);
+        fetchNextSegment(origInterest, data.getName(), currentSegment.toSegment() + 1, self);
       }
       else {
         return m_completeCallback(m_buffer->buf());
@@ -141,9 +142,10 @@
 }
 
 void
-SegmentFetcher::afterValidationFailure(const shared_ptr<const Data> data)
+SegmentFetcher::afterValidationFailure(const Data& data, const security::v2::ValidationError& error)
 {
-  return m_errorCallback(SEGMENT_VALIDATION_FAIL, "Segment validation fail");
+  return m_errorCallback(SEGMENT_VALIDATION_FAIL, "Segment validation fail " +
+                         boost::lexical_cast<std::string>(error));
 }
 
 
diff --git a/src/util/segment-fetcher.hpp b/src/util/segment-fetcher.hpp
index 0c2e908..99510f5 100644
--- a/src/util/segment-fetcher.hpp
+++ b/src/util/segment-fetcher.hpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2013-2015 Regents of the University of California.
+/*
+ * Copyright (c) 2013-2017 Regents of the University of California.
  *
  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
  *
@@ -25,7 +25,7 @@
 #include "scheduler.hpp"
 #include "../common.hpp"
 #include "../face.hpp"
-#include "../security/validator.hpp"
+#include "../security/v2/validator.hpp"
 
 namespace ndn {
 
@@ -140,7 +140,7 @@
   void
   fetch(Face& face,
         const Interest& baseInterest,
-        Validator& validator,
+        security::v2::Validator& validator,
         const CompleteCallback& completeCallback,
         const ErrorCallback& errorCallback);
 
@@ -163,13 +163,13 @@
   void
   fetch(Face& face,
         const Interest& baseInterest,
-        shared_ptr<Validator> validator,
+        shared_ptr<security::v2::Validator> validator,
         const CompleteCallback& completeCallback,
         const ErrorCallback& errorCallback);
 
 private:
   SegmentFetcher(Face& face,
-                 shared_ptr<Validator> validator,
+                 shared_ptr<security::v2::Validator> validator,
                  const CompleteCallback& completeCallback,
                  const ErrorCallback& errorCallback);
 
@@ -185,13 +185,13 @@
                        const Data& data, bool isSegmentZeroExpected,
                        shared_ptr<SegmentFetcher> self);
   void
-  afterValidationSuccess(const shared_ptr<const Data> data,
+  afterValidationSuccess(const Data& data,
                          bool isSegmentZeroExpected,
                          const Interest& origInterest,
                          shared_ptr<SegmentFetcher> self);
 
   void
-  afterValidationFailure(const shared_ptr<const Data> data);
+  afterValidationFailure(const Data& data, const security::v2::ValidationError& error);
 
   void
   afterNackReceived(const Interest& origInterest, const lp::Nack& nack,
@@ -204,7 +204,7 @@
 private:
   Face& m_face;
   Scheduler m_scheduler;
-  shared_ptr<Validator> m_validator;
+  shared_ptr<security::v2::Validator> m_validator;
   CompleteCallback m_completeCallback;
   ErrorCallback m_errorCallback;