Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 13 | * |
| 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "fw/unsolicited-data-policy.hpp" |
| 27 | #include "fw/forwarder.hpp" |
| 28 | #include "tests/daemon/face/dummy-face.hpp" |
| 29 | |
| 30 | #include "tests/test-common.hpp" |
| 31 | #include <boost/logic/tribool.hpp> |
| 32 | #include <boost/mpl/vector.hpp> |
| 33 | |
| 34 | namespace nfd { |
| 35 | namespace fw { |
| 36 | namespace tests { |
| 37 | |
| 38 | using namespace nfd::tests; |
| 39 | |
| 40 | class UnsolicitedDataPolicyFixture : public UnitTestTimeFixture |
| 41 | { |
| 42 | protected: |
| 43 | UnsolicitedDataPolicyFixture() |
| 44 | : cs(forwarder.getCs()) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | /** \tparam Policy policy type, or void to keep default policy |
| 49 | */ |
| 50 | template<typename Policy> |
| 51 | void |
| 52 | setPolicy() |
| 53 | { |
| 54 | forwarder.setUnsolicitedDataPolicy(make_unique<Policy>()); |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | isInCs(const Data& data) |
| 59 | { |
| 60 | using namespace boost::logic; |
| 61 | |
| 62 | tribool isFound = indeterminate; |
| 63 | cs.find(Interest(data.getFullName()), |
| 64 | bind([&] { isFound = true; }), |
| 65 | bind([&] { isFound = false; })); |
| 66 | |
| 67 | this->advanceClocks(time::milliseconds(1)); |
| 68 | BOOST_REQUIRE(!indeterminate(isFound)); |
| 69 | return isFound; |
| 70 | } |
| 71 | |
| 72 | protected: |
| 73 | Forwarder forwarder; |
| 74 | Cs& cs; |
| 75 | }; |
| 76 | |
| 77 | template<> |
| 78 | void |
| 79 | UnsolicitedDataPolicyFixture::setPolicy<void>() |
| 80 | { |
| 81 | // void keeps the default policy |
| 82 | } |
| 83 | |
| 84 | BOOST_AUTO_TEST_SUITE(Fw) |
| 85 | BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture) |
| 86 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 87 | BOOST_AUTO_TEST_CASE(GetPolicyNames) |
| 88 | { |
| 89 | std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames(); |
| 90 | BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1); |
| 91 | BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1); |
| 92 | BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1); |
| 93 | BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1); |
| 94 | } |
| 95 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 96 | template<typename Policy, bool shouldAdmitLocal, bool shouldAdmitNonLocal> |
| 97 | struct FaceScopePolicyTest |
| 98 | { |
| 99 | typedef Policy PolicyType; |
| 100 | typedef std::integral_constant<bool, shouldAdmitLocal> ShouldAdmitLocal; |
| 101 | typedef std::integral_constant<bool, shouldAdmitNonLocal> ShouldAdmitNonLocal; |
| 102 | }; |
| 103 | |
| 104 | typedef boost::mpl::vector< |
Junxiao Shi | 88f5f73 | 2016-09-03 07:11:37 +0000 | [diff] [blame] | 105 | FaceScopePolicyTest<void, false, false>, // default policy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 106 | FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>, |
| 107 | FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>, |
| 108 | FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>, |
| 109 | FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true> |
| 110 | > FaceScopePolicyTests; |
| 111 | |
| 112 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests) |
| 113 | { |
| 114 | setPolicy<typename T::PolicyType>(); |
| 115 | |
| 116 | auto face1 = make_shared<DummyFace>("dummy://", "dummy://", |
| 117 | ndn::nfd::FACE_SCOPE_LOCAL); |
| 118 | forwarder.addFace(face1); |
| 119 | |
| 120 | shared_ptr<Data> data1 = makeData("/unsolicited-from-local"); |
| 121 | forwarder.onIncomingData(*face1, *data1); |
| 122 | BOOST_CHECK_EQUAL(isInCs(*data1), T::ShouldAdmitLocal::value); |
| 123 | |
| 124 | auto face2 = make_shared<DummyFace>("dummy://", "dummy://", |
| 125 | ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 126 | forwarder.addFace(face2); |
| 127 | |
| 128 | shared_ptr<Data> data2 = makeData("/unsolicited-from-non-local"); |
| 129 | forwarder.onIncomingData(*face2, *data2); |
| 130 | BOOST_CHECK_EQUAL(isInCs(*data2), T::ShouldAdmitNonLocal::value); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy |
| 134 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 135 | |
| 136 | } // namespace tests |
| 137 | } // namespace fw |
| 138 | } // namespace nfd |