security: IMPORTANT CHANGE!! Change Validator constructor to accept Face& instead of shared_ptr<Face>

The old API is kept as deprecated, but will be removed as soon as all
known applications are checked and updated to use new API.

Refs: #1481

Change-Id: Iba0f92b3a3cd48235688f52ab89b2d1f0be9124b
diff --git a/src/security/conf/checker.hpp b/src/security/conf/checker.hpp
index b1aaf7b..72ae726 100644
--- a/src/security/conf/checker.hpp
+++ b/src/security/conf/checker.hpp
@@ -108,7 +108,7 @@
       }
     catch (const Signature::Error& e)
       {
-        onValidationFailed(interest.shared_from_this(), "No valid signature");
+        onValidationFailed(interest.shared_from_this(), "Invalid signature");
         return -1;
       }
   }
@@ -228,7 +228,7 @@
       }
     catch (const Signature::Error& e)
       {
-        onValidationFailed(interest.shared_from_this(), "No valid signature");
+        onValidationFailed(interest.shared_from_this(), "Invalid signature");
         return -1;
       }
   }
diff --git a/src/security/validator-config.cpp b/src/security/validator-config.cpp
index f622ede..77d3354 100644
--- a/src/security/validator-config.cpp
+++ b/src/security/validator-config.cpp
@@ -16,20 +16,29 @@
 
 const shared_ptr<CertificateCache> ValidatorConfig::DEFAULT_CERTIFICATE_CACHE;
 
-ValidatorConfig::ValidatorConfig(shared_ptr<Face> face,
-                                 shared_ptr<CertificateCache> certificateCache,
+ValidatorConfig::ValidatorConfig(Face& face,
+                                 const shared_ptr<CertificateCache>& certificateCache,
                                  const int stepLimit)
   : Validator(face)
   , m_stepLimit(stepLimit)
   , m_certificateCache(certificateCache)
 {
-  if (!static_cast<bool>(face))
-    throw Error("Face is not set!");
-
   if (!static_cast<bool>(m_certificateCache))
-    m_certificateCache = make_shared<CertificateCacheTtl>(m_face->ioService());
+    m_certificateCache = make_shared<CertificateCacheTtl>(m_face.ioService());
 }
 
+ValidatorConfig::ValidatorConfig(const shared_ptr<Face>& face,
+                                 const shared_ptr<CertificateCache>& certificateCache,
+                                 const int stepLimit)
+  : Validator(*face)
+  , m_stepLimit(stepLimit)
+  , m_certificateCache(certificateCache)
+{
+  if (!static_cast<bool>(m_certificateCache))
+    m_certificateCache = make_shared<CertificateCacheTtl>(m_face.ioService());
+}
+
+
 void
 ValidatorConfig::load(const std::string& filename)
 {
diff --git a/src/security/validator-config.hpp b/src/security/validator-config.hpp
index 9551fa5..bedca53 100644
--- a/src/security/validator-config.hpp
+++ b/src/security/validator-config.hpp
@@ -29,8 +29,16 @@
 
   static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
 
-  ValidatorConfig(shared_ptr<Face> face,
-                  shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE,
+  explicit
+  ValidatorConfig(Face& face,
+                  const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
+                  const int stepLimit = 10);
+
+  /**
+   * \deprecated Use the other version of the constructor
+   */
+  ValidatorConfig(const shared_ptr<Face>& face,
+                  const shared_ptr<CertificateCache>& certificateCache = DEFAULT_CERTIFICATE_CACHE,
                   const int stepLimit = 10);
 
   virtual
diff --git a/src/security/validator-regex.cpp b/src/security/validator-regex.cpp
index 48b9145..1eea45e 100644
--- a/src/security/validator-regex.cpp
+++ b/src/security/validator-regex.cpp
@@ -21,18 +21,26 @@
 
 const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE;
 
-ValidatorRegex::ValidatorRegex(shared_ptr<Face> face,
+ValidatorRegex::ValidatorRegex(Face& face,
                                shared_ptr<CertificateCache> certificateCache,
                                const int stepLimit)
   : Validator(face)
   , m_stepLimit(stepLimit)
   , m_certificateCache(certificateCache)
 {
-  if (!static_cast<bool>(face))
-    throw Error("Face is not set!");
-
   if (!static_cast<bool>(m_certificateCache))
-    m_certificateCache = make_shared<CertificateCacheTtl>(m_face->ioService());
+    m_certificateCache = make_shared<CertificateCacheTtl>(m_face.ioService());
+}
+
+ValidatorRegex::ValidatorRegex(const shared_ptr<Face>& face,
+                               shared_ptr<CertificateCache> certificateCache,
+                               const int stepLimit)
+  : Validator(*face)
+  , m_stepLimit(stepLimit)
+  , m_certificateCache(certificateCache)
+{
+  if (!static_cast<bool>(m_certificateCache))
+    m_certificateCache = make_shared<CertificateCacheTtl>(m_face.ioService());
 }
 
 void
diff --git a/src/security/validator-regex.hpp b/src/security/validator-regex.hpp
index fddbb0a..fb2572b 100644
--- a/src/security/validator-regex.hpp
+++ b/src/security/validator-regex.hpp
@@ -31,7 +31,14 @@
 
   static const shared_ptr<CertificateCache> DEFAULT_CERTIFICATE_CACHE;
 
-  ValidatorRegex(shared_ptr<Face> face,
+  ValidatorRegex(Face& face,
+                 shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE,
+                 const int stepLimit = 3);
+
+  /**
+   * \deprecated Use the other version of the constructor
+   */
+  ValidatorRegex(const shared_ptr<Face>& face,
                  shared_ptr<CertificateCache> certificateCache = DEFAULT_CERTIFICATE_CACHE,
                  const int stepLimit = 3);
 
diff --git a/src/security/validator.cpp b/src/security/validator.cpp
index 029f766..338ef9f 100644
--- a/src/security/validator.cpp
+++ b/src/security/validator.cpp
@@ -17,10 +17,15 @@
 
 namespace ndn {
 
-const shared_ptr<Face> Validator::DEFAULT_FACE;
+Validator::Validator()
+  : m_hasFace(false)
+  , m_face(*static_cast<Face*>(0))
+{
+}
 
-Validator::Validator(shared_ptr<Face> face /* = DefaultFace */)
-  : m_face(face)
+Validator::Validator(Face& face)
+  : m_hasFace(true)
+  , m_face(face)
 {
 }
 
@@ -35,18 +40,22 @@
 
   if (!nextSteps.empty())
     {
-      if (!static_cast<bool>(m_face))
-        throw Error("Face should be set before calling validate method");
+      if (!m_hasFace)
+        {
+          onValidationFailed(interest.shared_from_this(),
+                             "Require more information to validate the interest!");
+          return;
+        }
 
       vector<shared_ptr<ValidationRequest> >::const_iterator it = nextSteps.begin();
       OnFailure onFailure = bind(onValidationFailed, interest.shared_from_this(), _1);
       for (; it != nextSteps.end(); it++)
-        m_face->expressInterest((*it)->m_interest,
-                                bind(&Validator::onData, this, _1, _2, *it),
-                                bind(&Validator::onTimeout,
-                                     this, _1, (*it)->m_nRetrials,
-                                     onFailure,
-                                     *it));
+        m_face.expressInterest((*it)->m_interest,
+                               bind(&Validator::onData, this, _1, _2, *it),
+                               bind(&Validator::onTimeout,
+                                    this, _1, (*it)->m_nRetrials,
+                                    onFailure,
+                                    *it));
     }
   else
     {
@@ -67,18 +76,21 @@
 
   if (!nextSteps.empty())
     {
-      if (!static_cast<bool>(m_face))
-        throw Error("Face should be set prior to verify method to call");
+      if (!m_hasFace)
+        {
+          onValidationFailed(data.shared_from_this(),
+                             "Require more information to validate the data!");
+        }
 
       vector<shared_ptr<ValidationRequest> >::const_iterator it = nextSteps.begin();
       OnFailure onFailure = bind(onValidationFailed, data.shared_from_this(), _1);
       for (; it != nextSteps.end(); it++)
-        m_face->expressInterest((*it)->m_interest,
-                                bind(&Validator::onData, this, _1, _2, *it),
-                                bind(&Validator::onTimeout,
-                                     this, _1, (*it)->m_nRetrials,
-                                     onFailure,
-                                     *it));
+        m_face.expressInterest((*it)->m_interest,
+                               bind(&Validator::onData, this, _1, _2, *it),
+                               bind(&Validator::onTimeout,
+                                    this, _1, (*it)->m_nRetrials,
+                                    onFailure,
+                                    *it));
     }
   else
     {
@@ -104,7 +116,7 @@
 {
   if (nRetrials > 0)
     // Issue the same expressInterest except decrement nRetrials.
-    m_face->expressInterest(interest,
+    m_face.expressInterest(interest,
                             bind(&Validator::onData, this, _1, _2, nextStep),
                             bind(&Validator::onTimeout, this, _1,
                                  nRetrials - 1, onFailure, nextStep));
diff --git a/src/security/validator.hpp b/src/security/validator.hpp
index e65d7a8..43ab01a 100644
--- a/src/security/validator.hpp
+++ b/src/security/validator.hpp
@@ -36,10 +36,10 @@
     }
   };
 
-  static const shared_ptr<Face> DEFAULT_FACE;
+  Validator();
 
   explicit
-  Validator(shared_ptr<Face> face = DEFAULT_FACE);
+  Validator(Face& face);
 
   /**
    * @brief Validate Data and call either onValidated or onValidationFailed.
@@ -246,7 +246,8 @@
            int nSteps);
 
 protected:
-  shared_ptr<Face> m_face;
+  bool m_hasFace;
+  Face& m_face;
 };
 
 } // namespace ndn
diff --git a/tests-integrated/security/test-validator-config.cpp b/tests-integrated/security/test-validator-config.cpp
index ecde534..aee389a 100644
--- a/tests-integrated/security/test-validator-config.cpp
+++ b/tests-integrated/security/test-validator-config.cpp
@@ -125,7 +125,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -204,7 +204,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -287,7 +287,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -369,7 +369,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -448,7 +448,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -573,7 +573,7 @@
   keyChain.addCertificateAsIdentityDefault(*nldCert);
 
   shared_ptr<Face> face = make_shared<Face>();
-  shared_ptr<Face> face2 = shared_ptr<Face>(new Face(face->ioService()));
+  Face face2(face->ioService());
   Scheduler scheduler(*face->ioService());
 
   scheduler.scheduleEvent(time::seconds(1),
@@ -717,7 +717,7 @@
     (boost::filesystem::current_path() / std::string("unit-test-nfd.conf"));
 
 
-  shared_ptr<Face> face = make_shared<Face>();
+  Face face;
   ValidatorConfig validator(face);
   validator.load(CONFIG, CONFIG_PATH.native());
 
@@ -778,7 +778,7 @@
   keyChain.addCertificateAsIdentityDefault(*nldCert);
 
   shared_ptr<Face> face = make_shared<Face>();
-  shared_ptr<Face> face2 = shared_ptr<Face>(new Face(face->ioService()));
+  Face face2(face->ioService());
   Scheduler scheduler(*face->ioService());
 
   scheduler.scheduleEvent(time::seconds(1),
diff --git a/tests/security/test-validator.cpp b/tests/security/test-validator.cpp
index c3e5809..2d51f32 100644
--- a/tests/security/test-validator.cpp
+++ b/tests/security/test-validator.cpp
@@ -45,8 +45,8 @@
 
   // data must be a shared pointer
   validator.validate(*data,
-		     bind(&onValidated, _1),
-		     bind(&onValidationFailed, _1, _2));
+                     bind(&onValidated, _1),
+                     bind(&onValidationFailed, _1, _2));
 
   keyChain.deleteIdentity(identity);
 }