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 | /* |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, 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 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | using namespace nfd::fw; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 40 | class UnsolicitedDataPolicyFixture : public GlobalIoTimeFixture |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 41 | { |
| 42 | protected: |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 43 | /** \tparam Policy policy type, or void to keep default policy |
| 44 | */ |
| 45 | template<typename Policy> |
| 46 | void |
| 47 | setPolicy() |
| 48 | { |
| 49 | forwarder.setUnsolicitedDataPolicy(make_unique<Policy>()); |
| 50 | } |
| 51 | |
| 52 | bool |
| 53 | isInCs(const Data& data) |
| 54 | { |
| 55 | using namespace boost::logic; |
| 56 | |
| 57 | tribool isFound = indeterminate; |
| 58 | cs.find(Interest(data.getFullName()), |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 59 | [&] (auto&&...) { isFound = true; }, |
| 60 | [&] (auto&&...) { isFound = false; }); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 61 | |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 62 | this->advanceClocks(1_ms); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 63 | BOOST_REQUIRE(!indeterminate(isFound)); |
Davide Pesavento | fa2aa50 | 2019-03-22 13:30:02 -0400 | [diff] [blame] | 64 | return bool(isFound); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | protected: |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 68 | FaceTable faceTable; |
| 69 | Forwarder forwarder{faceTable}; |
| 70 | Cs& cs{forwarder.getCs()}; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | template<> |
| 74 | void |
| 75 | UnsolicitedDataPolicyFixture::setPolicy<void>() |
| 76 | { |
| 77 | // void keeps the default policy |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_SUITE(Fw) |
| 81 | BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture) |
| 82 | |
Junxiao Shi | 9db7359 | 2016-12-01 23:43:11 +0000 | [diff] [blame] | 83 | BOOST_AUTO_TEST_CASE(GetPolicyNames) |
| 84 | { |
| 85 | std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames(); |
| 86 | BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1); |
| 87 | BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1); |
| 88 | BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1); |
| 89 | BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1); |
| 90 | } |
| 91 | |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 92 | template<typename Policy, bool shouldAdmitLocal, bool shouldAdmitNonLocal> |
| 93 | struct FaceScopePolicyTest |
| 94 | { |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 95 | using PolicyType = Policy; |
Davide Pesavento | a3a7a4e | 2022-05-29 16:06:22 -0400 | [diff] [blame] | 96 | using ShouldAdmitLocal = std::bool_constant<shouldAdmitLocal>; |
| 97 | using ShouldAdmitNonLocal = std::bool_constant<shouldAdmitNonLocal>; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 100 | using FaceScopePolicyTests = boost::mpl::vector< |
Junxiao Shi | 88f5f73 | 2016-09-03 07:11:37 +0000 | [diff] [blame] | 101 | FaceScopePolicyTest<void, false, false>, // default policy |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 102 | FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>, |
| 103 | FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>, |
| 104 | FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>, |
| 105 | FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true> |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 106 | >; |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 107 | |
| 108 | BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests) |
| 109 | { |
| 110 | setPolicy<typename T::PolicyType>(); |
| 111 | |
| 112 | auto face1 = make_shared<DummyFace>("dummy://", "dummy://", |
| 113 | ndn::nfd::FACE_SCOPE_LOCAL); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 114 | faceTable.add(face1); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 115 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 116 | auto data1 = makeData("/unsolicited-from-local"); |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame^] | 117 | forwarder.onIncomingData(*data1, FaceEndpoint(*face1)); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 118 | BOOST_CHECK_EQUAL(isInCs(*data1), T::ShouldAdmitLocal::value); |
| 119 | |
| 120 | auto face2 = make_shared<DummyFace>("dummy://", "dummy://", |
| 121 | ndn::nfd::FACE_SCOPE_NON_LOCAL); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 122 | faceTable.add(face2); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 123 | |
Davide Pesavento | 0498ce8 | 2021-06-14 02:02:21 -0400 | [diff] [blame] | 124 | auto data2 = makeData("/unsolicited-from-non-local"); |
Teng Liang | d94b7b3 | 2022-07-10 21:29:37 +0800 | [diff] [blame^] | 125 | forwarder.onIncomingData(*data2, FaceEndpoint(*face2)); |
Junxiao Shi | 36b2225 | 2016-08-25 13:22:21 +0000 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(isInCs(*data2), T::ShouldAdmitNonLocal::value); |
| 127 | } |
| 128 | |
| 129 | BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy |
| 130 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 131 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 132 | } // namespace nfd::tests |