Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2019, Regents of the University of California, |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 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" |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 28 | |
| 29 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 30 | #include "tests/daemon/global-io-fixture.hpp" |
| 31 | #include "tests/daemon/face/dummy-face.hpp" |
| 32 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 33 | #include <boost/logic/tribool.hpp> |
| 34 | #include <boost/mpl/vector.hpp> |
| 35 | |
| 36 | namespace nfd { |
| 37 | namespace fw { |
| 38 | namespace tests { |
| 39 | |
| 40 | using namespace nfd::tests; |
| 41 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 42 | class UnsolicitedDataPolicyFixture : public GlobalIoTimeFixture |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 43 | { |
| 44 | protected: |
| 45 | UnsolicitedDataPolicyFixture() |
| 46 | : cs(forwarder.getCs()) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | /** \tparam Policy policy type, or void to keep default policy |
| 51 | */ |
| 52 | template<typename Policy> |
| 53 | void |
| 54 | setPolicy() |
| 55 | { |
| 56 | forwarder.setUnsolicitedDataPolicy(make_unique<Policy>()); |
| 57 | } |
| 58 | |
| 59 | bool |
| 60 | isInCs(const Data& data) |
| 61 | { |
| 62 | using namespace boost::logic; |
| 63 | |
| 64 | tribool isFound = indeterminate; |
| 65 | cs.find(Interest(data.getFullName()), |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 66 | bind([&] { isFound = true; }), |
| 67 | bind([&] { isFound = false; })); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 68 | |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 69 | this->advanceClocks(1_ms); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 70 | BOOST_REQUIRE(!indeterminate(isFound)); |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 71 | return bool(isFound); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | protected: |
| 75 | Forwarder forwarder; |
| 76 | Cs& cs; |
| 77 | }; |
| 78 | |
| 79 | template<> |
| 80 | void |
| 81 | UnsolicitedDataPolicyFixture::setPolicy<void>() |
| 82 | { |
| 83 | // void keeps the default policy |
| 84 | } |
| 85 | |
| 86 | BOOST_AUTO_TEST_SUITE(Fw) |
| 87 | BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture) |
| 88 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 89 | BOOST_AUTO_TEST_CASE(GetPolicyNames) |
| 90 | { |
| 91 | std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames(); |
| 92 | BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1); |
| 93 | BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1); |
| 94 | BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1); |
| 95 | BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1); |
| 96 | } |
| 97 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 98 | template<typename Policy, bool shouldAdmitLocal, bool shouldAdmitNonLocal> |
| 99 | struct FaceScopePolicyTest |
| 100 | { |
| 101 | typedef Policy PolicyType; |
| 102 | typedef std::integral_constant<bool, shouldAdmitLocal> ShouldAdmitLocal; |
| 103 | typedef std::integral_constant<bool, shouldAdmitNonLocal> ShouldAdmitNonLocal; |
| 104 | }; |
| 105 | |
| 106 | typedef boost::mpl::vector< |
Junxiao Shi | 88f5f73 | 2016-09-03 07:11:37 +0000 | [diff] [blame] | 107 | FaceScopePolicyTest<void, false, false>, // default policy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 108 | FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>, |
| 109 | FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>, |
| 110 | FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>, |
| 111 | FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true> |
| 112 | > FaceScopePolicyTests; |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests) |
| 115 | { |
| 116 | setPolicy<typename T::PolicyType>(); |
| 117 | |
| 118 | auto face1 = make_shared<DummyFace>("dummy://", "dummy://", |
| 119 | ndn::nfd::FACE_SCOPE_LOCAL); |
| 120 | forwarder.addFace(face1); |
| 121 | |
| 122 | shared_ptr<Data> data1 = makeData("/unsolicited-from-local"); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 123 | forwarder.onIncomingData(FaceEndpoint(*face1, 0), *data1); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(isInCs(*data1), T::ShouldAdmitLocal::value); |
| 125 | |
| 126 | auto face2 = make_shared<DummyFace>("dummy://", "dummy://", |
| 127 | ndn::nfd::FACE_SCOPE_NON_LOCAL); |
| 128 | forwarder.addFace(face2); |
| 129 | |
| 130 | shared_ptr<Data> data2 = makeData("/unsolicited-from-non-local"); |
ashiqopu | c707948 | 2019-02-20 05:34:37 +0000 | [diff] [blame] | 131 | forwarder.onIncomingData(FaceEndpoint(*face2, 0), *data2); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(isInCs(*data2), T::ShouldAdmitNonLocal::value); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy |
| 136 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 137 | |
| 138 | } // namespace tests |
| 139 | } // namespace fw |
| 140 | } // namespace nfd |