blob: 77722c76b6c366b5fbeb8f00d1838fabe66f5132 [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 Pesaventoa3a7a4e2022-05-29 16:06:22 -04003 * Copyright (c) 2014-2022, 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>
34#include <boost/mpl/vector.hpp>
35
36namespace nfd {
37namespace fw {
38namespace tests {
39
40using namespace nfd::tests;
41
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042class UnsolicitedDataPolicyFixture : public GlobalIoTimeFixture
Junxiao Shi36b22252016-08-25 13:22:21 +000043{
44protected:
Junxiao Shi36b22252016-08-25 13:22:21 +000045 /** \tparam Policy policy type, or void to keep default policy
46 */
47 template<typename Policy>
48 void
49 setPolicy()
50 {
51 forwarder.setUnsolicitedDataPolicy(make_unique<Policy>());
52 }
53
54 bool
55 isInCs(const Data& data)
56 {
57 using namespace boost::logic;
58
59 tribool isFound = indeterminate;
60 cs.find(Interest(data.getFullName()),
Davide Pesavento412c9822021-07-02 00:21:05 -040061 [&] (auto&&...) { isFound = true; },
62 [&] (auto&&...) { isFound = false; });
Junxiao Shi36b22252016-08-25 13:22:21 +000063
Davide Pesaventofa2aa502019-03-22 13:30:02 -040064 this->advanceClocks(1_ms);
Junxiao Shi36b22252016-08-25 13:22:21 +000065 BOOST_REQUIRE(!indeterminate(isFound));
Davide Pesaventofa2aa502019-03-22 13:30:02 -040066 return bool(isFound);
Junxiao Shi36b22252016-08-25 13:22:21 +000067 }
68
69protected:
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040070 FaceTable faceTable;
71 Forwarder forwarder{faceTable};
72 Cs& cs{forwarder.getCs()};
Junxiao Shi36b22252016-08-25 13:22:21 +000073};
74
75template<>
76void
77UnsolicitedDataPolicyFixture::setPolicy<void>()
78{
79 // void keeps the default policy
80}
81
82BOOST_AUTO_TEST_SUITE(Fw)
83BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture)
84
Junxiao Shi9db73592016-12-01 23:43:11 +000085BOOST_AUTO_TEST_CASE(GetPolicyNames)
86{
87 std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames();
88 BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1);
89 BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1);
90 BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1);
91 BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1);
92}
93
Junxiao Shi36b22252016-08-25 13:22:21 +000094template<typename Policy, bool shouldAdmitLocal, bool shouldAdmitNonLocal>
95struct FaceScopePolicyTest
96{
Davide Pesavento0498ce82021-06-14 02:02:21 -040097 using PolicyType = Policy;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -040098 using ShouldAdmitLocal = std::bool_constant<shouldAdmitLocal>;
99 using ShouldAdmitNonLocal = std::bool_constant<shouldAdmitNonLocal>;
Junxiao Shi36b22252016-08-25 13:22:21 +0000100};
101
Davide Pesavento0498ce82021-06-14 02:02:21 -0400102using FaceScopePolicyTests = boost::mpl::vector<
Junxiao Shi88f5f732016-09-03 07:11:37 +0000103 FaceScopePolicyTest<void, false, false>, // default policy
Junxiao Shi36b22252016-08-25 13:22:21 +0000104 FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>,
105 FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>,
106 FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>,
107 FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true>
Davide Pesavento0498ce82021-06-14 02:02:21 -0400108>;
Junxiao Shi36b22252016-08-25 13:22:21 +0000109
110BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests)
111{
112 setPolicy<typename T::PolicyType>();
113
114 auto face1 = make_shared<DummyFace>("dummy://", "dummy://",
115 ndn::nfd::FACE_SCOPE_LOCAL);
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400116 faceTable.add(face1);
Junxiao Shi36b22252016-08-25 13:22:21 +0000117
Davide Pesavento0498ce82021-06-14 02:02:21 -0400118 auto data1 = makeData("/unsolicited-from-local");
119 forwarder.onIncomingData(*data1, FaceEndpoint(*face1, 0));
Junxiao Shi36b22252016-08-25 13:22:21 +0000120 BOOST_CHECK_EQUAL(isInCs(*data1), T::ShouldAdmitLocal::value);
121
122 auto face2 = make_shared<DummyFace>("dummy://", "dummy://",
123 ndn::nfd::FACE_SCOPE_NON_LOCAL);
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400124 faceTable.add(face2);
Junxiao Shi36b22252016-08-25 13:22:21 +0000125
Davide Pesavento0498ce82021-06-14 02:02:21 -0400126 auto data2 = makeData("/unsolicited-from-non-local");
127 forwarder.onIncomingData(*data2, FaceEndpoint(*face2, 0));
Junxiao Shi36b22252016-08-25 13:22:21 +0000128 BOOST_CHECK_EQUAL(isInCs(*data2), T::ShouldAdmitNonLocal::value);
129}
130
131BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy
132BOOST_AUTO_TEST_SUITE_END() // Fw
133
134} // namespace tests
135} // namespace fw
136} // namespace nfd