ndns-dig: adapt to the fact that NDN testbed does not have root zone

- Enable dig to set the start label during iterative process.
- Enable validation of intermediate results with an option to disable
it

Change-Id: I221e3c328c875ad06a1f8094f1004e68d9d46a57
diff --git a/src/clients/iterative-query-controller.cpp b/src/clients/iterative-query-controller.cpp
index c5e5104..be3f9f8 100644
--- a/src/clients/iterative-query-controller.cpp
+++ b/src/clients/iterative-query-controller.cpp
@@ -17,6 +17,7 @@
  * NDNS, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include "validator.hpp"
 #include "iterative-query-controller.hpp"
 #include "logger.hpp"
 #include <iostream>
@@ -30,14 +31,14 @@
                                                    const time::milliseconds& interestLifetime,
                                                    const QuerySucceedCallback& onSucceed,
                                                    const QueryFailCallback& onFail,
-                                                   Face& face)
+                                                   Face& face,
+                                                   Validator* validator)
   : QueryController(dstLabel, rrType, interestLifetime, onSucceed, onFail, face)
+  , m_validator(validator)
   , m_step(QUERY_STEP_QUERY_NS)
   , m_nFinishedComps(0)
   , m_nTryComps(1)
 {
-  if (m_dstLabel.size() == 1) // the first one is to Query RR directly
-    m_step = QUERY_STEP_QUERY_RR;
 }
 
 void
@@ -70,7 +71,22 @@
 
   NDNS_LOG_TRACE("[* -> *] get a " << ndnsType
                  << " Response: " << data.getName());
-
+  if (m_validator == nullptr) {
+    this->onDataValidated(make_shared<Data>(data), ndnsType);
+  }
+  else {
+    m_validator->validate(data,
+                          bind(&IterativeQueryController::onDataValidated, this, _1, ndnsType),
+                          [this] (const shared_ptr<const Data>& data, const std::string& str) {
+                            NDNS_LOG_WARN("data: " << data->getName() << " fails verification");
+                            this->abort();
+                          }
+                          );
+  }
+}
+void
+IterativeQueryController::onDataValidated(const shared_ptr<const Data>& data, NdnsType ndnsType)
+{
   switch (m_step) {
   case QUERY_STEP_QUERY_NS:
     if (ndnsType == NDNS_NACK) {
@@ -118,9 +134,9 @@
     this->express(this->makeLatestInterest()); // express new Expres
   else if (m_step == QUERY_STEP_ANSWER_STUB) {
     NDNS_LOG_TRACE("query ends: " << *this);
-    Response re = this->parseFinalResponse(data);
+    Response re = this->parseFinalResponse(*data);
     if (m_onSucceed != nullptr)
-      m_onSucceed(data, re);
+      m_onSucceed(*data, re);
     else
       NDNS_LOG_TRACE("succeed callback is nullptr");
   }
@@ -137,6 +153,9 @@
 void
 IterativeQueryController::start()
 {
+  if (m_dstLabel.size() == m_nFinishedComps)
+    m_step = QUERY_STEP_QUERY_RR;
+
   Interest interest = this->makeLatestInterest();
   express(interest);
 }
diff --git a/src/clients/iterative-query-controller.hpp b/src/clients/iterative-query-controller.hpp
index c97cc66..714a504 100644
--- a/src/clients/iterative-query-controller.hpp
+++ b/src/clients/iterative-query-controller.hpp
@@ -26,6 +26,7 @@
 #include "query-controller.hpp"
 #include "config.hpp"
 #include "common.hpp"
+#include "validator.hpp"
 
 #include <ndn-cxx/common.hpp>
 #include <ndn-cxx/data.hpp>
@@ -59,7 +60,7 @@
   IterativeQueryController(const Name& dstLabel, const name::Component& rrType,
                            const time::milliseconds& interestLifetime,
                            const QuerySucceedCallback& onSucceed, const QueryFailCallback& onFail,
-                           Face& face);
+                           Face& face, Validator* validator = nullptr);
 
   virtual void
   start();
@@ -75,6 +76,9 @@
   void
   onData(const ndn::Interest& interest, const Data& data);
 
+  void
+  onDataValidated(const shared_ptr<const Data>& data, NdnsType ndnsType);
+
   /**
    * @brief change the Controller state according to timeout. For current,
    * abort the query when timeout
@@ -98,13 +102,13 @@
   void
   express(const Interest& interest);
 
+public:
   void
-  setNFinishedComps(size_t finished)
+  setStartComponentIndex(size_t finished)
   {
     m_nFinishedComps = finished;
   }
 
-public:
   QueryStep
   getStep() const
   {
@@ -124,6 +128,7 @@
   }
 
 protected:
+  Validator* m_validator;
   /**
    * @brief current query step
    */
diff --git a/src/clients/query-controller.hpp b/src/clients/query-controller.hpp
index 778d563..7729ea2 100644
--- a/src/clients/query-controller.hpp
+++ b/src/clients/query-controller.hpp
@@ -68,6 +68,9 @@
   virtual bool
   hasEnded() = 0;
 
+  virtual void
+  setStartComponentIndex(size_t startIndex) = 0;
+
 public:
   ////////////////
   // getter