fw: additional unsolicited Data policies

refs #2181

Change-Id: I439a77f8f48ad194459dc2391cd0ad8d0860b83e
diff --git a/daemon/fw/unsolicited-data-policy.cpp b/daemon/fw/unsolicited-data-policy.cpp
index 894cc86..79c6d77 100644
--- a/daemon/fw/unsolicited-data-policy.cpp
+++ b/daemon/fw/unsolicited-data-policy.cpp
@@ -41,6 +41,12 @@
 }
 
 UnsolicitedDataDecision
+DropAllUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const
+{
+  return UnsolicitedDataDecision::DROP;
+}
+
+UnsolicitedDataDecision
 AdmitLocalUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const
 {
   if (inFace.getScope() == ndn::nfd::FACE_SCOPE_LOCAL) {
@@ -49,5 +55,20 @@
   return UnsolicitedDataDecision::DROP;
 }
 
+UnsolicitedDataDecision
+AdmitNetworkUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const
+{
+  if (inFace.getScope() == ndn::nfd::FACE_SCOPE_NON_LOCAL) {
+    return UnsolicitedDataDecision::CACHE;
+  }
+  return UnsolicitedDataDecision::DROP;
+}
+
+UnsolicitedDataDecision
+AdmitAllUnsolicitedDataPolicy::decide(const Face& inFace, const Data& data) const
+{
+  return UnsolicitedDataDecision::CACHE;
+}
+
 } // namespace fw
 } // namespace nfd
diff --git a/daemon/fw/unsolicited-data-policy.hpp b/daemon/fw/unsolicited-data-policy.hpp
index 54112ec..2cbad2a 100644
--- a/daemon/fw/unsolicited-data-policy.hpp
+++ b/daemon/fw/unsolicited-data-policy.hpp
@@ -56,6 +56,15 @@
   decide(const Face& inFace, const Data& data) const = 0;
 };
 
+/** \brief drops all unsolicited Data
+ */
+class DropAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
+{
+public:
+  virtual UnsolicitedDataDecision
+  decide(const Face& inFace, const Data& data) const final;
+};
+
 /** \brief admits unsolicited Data from local faces
  */
 class AdmitLocalUnsolicitedDataPolicy : public UnsolicitedDataPolicy
@@ -65,6 +74,24 @@
   decide(const Face& inFace, const Data& data) const final;
 };
 
+/** \brief admits unsolicited Data from non-local faces
+ */
+class AdmitNetworkUnsolicitedDataPolicy : public UnsolicitedDataPolicy
+{
+public:
+  virtual UnsolicitedDataDecision
+  decide(const Face& inFace, const Data& data) const final;
+};
+
+/** \brief admits all unsolicited Data
+ */
+class AdmitAllUnsolicitedDataPolicy : public UnsolicitedDataPolicy
+{
+public:
+  virtual UnsolicitedDataDecision
+  decide(const Face& inFace, const Data& data) const final;
+};
+
 } // namespace fw
 } // namespace nfd
 
diff --git a/tests/daemon/fw/unsolicited-data-policy.t.cpp b/tests/daemon/fw/unsolicited-data-policy.t.cpp
new file mode 100644
index 0000000..a858758
--- /dev/null
+++ b/tests/daemon/fw/unsolicited-data-policy.t.cpp
@@ -0,0 +1,129 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2016,  Regents of the University of California,
+ *                           Arizona Board of Regents,
+ *                           Colorado State University,
+ *                           University Pierre & Marie Curie, Sorbonne University,
+ *                           Washington University in St. Louis,
+ *                           Beijing Institute of Technology,
+ *                           The University of Memphis.
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "fw/unsolicited-data-policy.hpp"
+#include "fw/forwarder.hpp"
+#include "tests/daemon/face/dummy-face.hpp"
+
+#include "tests/test-common.hpp"
+#include <boost/logic/tribool.hpp>
+#include <boost/mpl/vector.hpp>
+
+namespace nfd {
+namespace fw {
+namespace tests {
+
+using namespace nfd::tests;
+
+class UnsolicitedDataPolicyFixture : public UnitTestTimeFixture
+{
+protected:
+  UnsolicitedDataPolicyFixture()
+    : cs(forwarder.getCs())
+  {
+  }
+
+  /** \tparam Policy policy type, or void to keep default policy
+   */
+  template<typename Policy>
+  void
+  setPolicy()
+  {
+    forwarder.setUnsolicitedDataPolicy(make_unique<Policy>());
+  }
+
+  bool
+  isInCs(const Data& data)
+  {
+    using namespace boost::logic;
+
+    tribool isFound = indeterminate;
+    cs.find(Interest(data.getFullName()),
+      bind([&] { isFound = true; }),
+      bind([&] { isFound = false; }));
+
+    this->advanceClocks(time::milliseconds(1));
+    BOOST_REQUIRE(!indeterminate(isFound));
+    return isFound;
+  }
+
+protected:
+  Forwarder forwarder;
+  Cs& cs;
+};
+
+template<>
+void
+UnsolicitedDataPolicyFixture::setPolicy<void>()
+{
+  // void keeps the default policy
+}
+
+BOOST_AUTO_TEST_SUITE(Fw)
+BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture)
+
+template<typename Policy, bool shouldAdmitLocal, bool shouldAdmitNonLocal>
+struct FaceScopePolicyTest
+{
+  typedef Policy PolicyType;
+  typedef std::integral_constant<bool, shouldAdmitLocal> ShouldAdmitLocal;
+  typedef std::integral_constant<bool, shouldAdmitNonLocal> ShouldAdmitNonLocal;
+};
+
+typedef boost::mpl::vector<
+  FaceScopePolicyTest<void, true, false>, // default policy
+  FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>,
+  FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>,
+  FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>,
+  FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true>
+> FaceScopePolicyTests;
+
+BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests)
+{
+  setPolicy<typename T::PolicyType>();
+
+  auto face1 = make_shared<DummyFace>("dummy://", "dummy://",
+                                      ndn::nfd::FACE_SCOPE_LOCAL);
+  forwarder.addFace(face1);
+
+  shared_ptr<Data> data1 = makeData("/unsolicited-from-local");
+  forwarder.onIncomingData(*face1, *data1);
+  BOOST_CHECK_EQUAL(isInCs(*data1), T::ShouldAdmitLocal::value);
+
+  auto face2 = make_shared<DummyFace>("dummy://", "dummy://",
+                                      ndn::nfd::FACE_SCOPE_NON_LOCAL);
+  forwarder.addFace(face2);
+
+  shared_ptr<Data> data2 = makeData("/unsolicited-from-non-local");
+  forwarder.onIncomingData(*face2, *data2);
+  BOOST_CHECK_EQUAL(isInCs(*data2), T::ShouldAdmitNonLocal::value);
+}
+
+BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy
+BOOST_AUTO_TEST_SUITE_END() // Fw
+
+} // namespace tests
+} // namespace fw
+} // namespace nfd