blob: c4a7097463ffa3eebcd037711626cca647c77153 [file] [log] [blame]
Junxiao Shi36b22252016-08-25 13:22:21 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
ashiqopuc7079482019-02-20 05:34:37 +00002/*
Davide Pesaventof56cf632024-01-27 22:22:06 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shi36b22252016-08-25 13:22:21 +00004 * 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 Shi36b22252016-08-25 13:22:21 +000028
29#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
31#include "tests/daemon/face/dummy-face.hpp"
32
Junxiao Shi36b22252016-08-25 13:22:21 +000033#include <boost/logic/tribool.hpp>
Davide Pesaventof56cf632024-01-27 22:22:06 -050034#include <boost/mp11/list.hpp>
Junxiao Shi36b22252016-08-25 13:22:21 +000035
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Junxiao Shi36b22252016-08-25 13:22:21 +000037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038using namespace nfd::fw;
Junxiao Shi36b22252016-08-25 13:22:21 +000039
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040040class UnsolicitedDataPolicyFixture : public GlobalIoTimeFixture
Junxiao Shi36b22252016-08-25 13:22:21 +000041{
42protected:
Davide Pesaventof56cf632024-01-27 22:22:06 -050043 /**
44 * \tparam Policy The policy type, or `void` to keep the default policy.
Junxiao Shi36b22252016-08-25 13:22:21 +000045 */
46 template<typename Policy>
47 void
48 setPolicy()
49 {
50 forwarder.setUnsolicitedDataPolicy(make_unique<Policy>());
51 }
52
53 bool
54 isInCs(const Data& data)
55 {
56 using namespace boost::logic;
57
58 tribool isFound = indeterminate;
59 cs.find(Interest(data.getFullName()),
Davide Pesavento412c9822021-07-02 00:21:05 -040060 [&] (auto&&...) { isFound = true; },
61 [&] (auto&&...) { isFound = false; });
Junxiao Shi36b22252016-08-25 13:22:21 +000062
Davide Pesaventofa2aa502019-03-22 13:30:02 -040063 this->advanceClocks(1_ms);
Junxiao Shi36b22252016-08-25 13:22:21 +000064 BOOST_REQUIRE(!indeterminate(isFound));
Davide Pesaventofa2aa502019-03-22 13:30:02 -040065 return bool(isFound);
Junxiao Shi36b22252016-08-25 13:22:21 +000066 }
67
68protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040069 FaceTable faceTable;
70 Forwarder forwarder{faceTable};
71 Cs& cs{forwarder.getCs()};
Junxiao Shi36b22252016-08-25 13:22:21 +000072};
73
74template<>
75void
76UnsolicitedDataPolicyFixture::setPolicy<void>()
77{
78 // void keeps the default policy
79}
80
81BOOST_AUTO_TEST_SUITE(Fw)
82BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture)
83
Junxiao Shi9db73592016-12-01 23:43:11 +000084BOOST_AUTO_TEST_CASE(GetPolicyNames)
85{
86 std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames();
87 BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1);
88 BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1);
89 BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1);
90 BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1);
91}
92
Davide Pesaventof56cf632024-01-27 22:22:06 -050093template<typename P, bool Local, bool NonLocal>
Junxiao Shi36b22252016-08-25 13:22:21 +000094struct FaceScopePolicyTest
95{
Davide Pesaventof56cf632024-01-27 22:22:06 -050096 using PolicyType = P;
97 static constexpr bool shouldAdmitLocal = Local;
98 static constexpr bool shouldAdmitNonLocal = NonLocal;
Junxiao Shi36b22252016-08-25 13:22:21 +000099};
100
Davide Pesaventof56cf632024-01-27 22:22:06 -0500101using FaceScopePolicyTests = boost::mp11::mp_list<
Junxiao Shi88f5f732016-09-03 07:11:37 +0000102 FaceScopePolicyTest<void, false, false>, // default policy
Junxiao Shi36b22252016-08-25 13:22:21 +0000103 FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>,
104 FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>,
105 FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>,
106 FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true>
Davide Pesavento0498ce82021-06-14 02:02:21 -0400107>;
Junxiao Shi36b22252016-08-25 13:22:21 +0000108
109BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests)
110{
111 setPolicy<typename T::PolicyType>();
112
113 auto face1 = make_shared<DummyFace>("dummy://", "dummy://",
114 ndn::nfd::FACE_SCOPE_LOCAL);
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400115 faceTable.add(face1);
Junxiao Shi36b22252016-08-25 13:22:21 +0000116
Davide Pesavento0498ce82021-06-14 02:02:21 -0400117 auto data1 = makeData("/unsolicited-from-local");
Teng Liangd94b7b32022-07-10 21:29:37 +0800118 forwarder.onIncomingData(*data1, FaceEndpoint(*face1));
Davide Pesaventof56cf632024-01-27 22:22:06 -0500119 BOOST_CHECK_EQUAL(isInCs(*data1), T::shouldAdmitLocal);
Junxiao Shi36b22252016-08-25 13:22:21 +0000120
121 auto face2 = make_shared<DummyFace>("dummy://", "dummy://",
122 ndn::nfd::FACE_SCOPE_NON_LOCAL);
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400123 faceTable.add(face2);
Junxiao Shi36b22252016-08-25 13:22:21 +0000124
Davide Pesavento0498ce82021-06-14 02:02:21 -0400125 auto data2 = makeData("/unsolicited-from-non-local");
Teng Liangd94b7b32022-07-10 21:29:37 +0800126 forwarder.onIncomingData(*data2, FaceEndpoint(*face2));
Davide Pesaventof56cf632024-01-27 22:22:06 -0500127 BOOST_CHECK_EQUAL(isInCs(*data2), T::shouldAdmitNonLocal);
Junxiao Shi36b22252016-08-25 13:22:21 +0000128}
129
130BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy
131BOOST_AUTO_TEST_SUITE_END() // Fw
132
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400133} // namespace nfd::tests