blob: c4a7097463ffa3eebcd037711626cca647c77153 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2014-2024, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis.
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fw/unsolicited-data-policy.hpp"
#include "fw/forwarder.hpp"
#include "tests/test-common.hpp"
#include "tests/daemon/global-io-fixture.hpp"
#include "tests/daemon/face/dummy-face.hpp"
#include <boost/logic/tribool.hpp>
#include <boost/mp11/list.hpp>
namespace nfd::tests {
using namespace nfd::fw;
class UnsolicitedDataPolicyFixture : public GlobalIoTimeFixture
{
protected:
/**
* \tparam Policy The policy type, or `void` to keep the default policy.
*/
template<typename Policy>
void
setPolicy()
{
forwarder.setUnsolicitedDataPolicy(make_unique<Policy>());
}
bool
isInCs(const Data& data)
{
using namespace boost::logic;
tribool isFound = indeterminate;
cs.find(Interest(data.getFullName()),
[&] (auto&&...) { isFound = true; },
[&] (auto&&...) { isFound = false; });
this->advanceClocks(1_ms);
BOOST_REQUIRE(!indeterminate(isFound));
return bool(isFound);
}
protected:
FaceTable faceTable;
Forwarder forwarder{faceTable};
Cs& cs{forwarder.getCs()};
};
template<>
void
UnsolicitedDataPolicyFixture::setPolicy<void>()
{
// void keeps the default policy
}
BOOST_AUTO_TEST_SUITE(Fw)
BOOST_FIXTURE_TEST_SUITE(TestUnsolicitedDataPolicy, UnsolicitedDataPolicyFixture)
BOOST_AUTO_TEST_CASE(GetPolicyNames)
{
std::set<std::string> policyNames = UnsolicitedDataPolicy::getPolicyNames();
BOOST_CHECK_EQUAL(policyNames.count("drop-all"), 1);
BOOST_CHECK_EQUAL(policyNames.count("admit-local"), 1);
BOOST_CHECK_EQUAL(policyNames.count("admit-network"), 1);
BOOST_CHECK_EQUAL(policyNames.count("admit-all"), 1);
}
template<typename P, bool Local, bool NonLocal>
struct FaceScopePolicyTest
{
using PolicyType = P;
static constexpr bool shouldAdmitLocal = Local;
static constexpr bool shouldAdmitNonLocal = NonLocal;
};
using FaceScopePolicyTests = boost::mp11::mp_list<
FaceScopePolicyTest<void, false, false>, // default policy
FaceScopePolicyTest<DropAllUnsolicitedDataPolicy, false, false>,
FaceScopePolicyTest<AdmitLocalUnsolicitedDataPolicy, true, false>,
FaceScopePolicyTest<AdmitNetworkUnsolicitedDataPolicy, false, true>,
FaceScopePolicyTest<AdmitAllUnsolicitedDataPolicy, true, true>
>;
BOOST_AUTO_TEST_CASE_TEMPLATE(FaceScopePolicy, T, FaceScopePolicyTests)
{
setPolicy<typename T::PolicyType>();
auto face1 = make_shared<DummyFace>("dummy://", "dummy://",
ndn::nfd::FACE_SCOPE_LOCAL);
faceTable.add(face1);
auto data1 = makeData("/unsolicited-from-local");
forwarder.onIncomingData(*data1, FaceEndpoint(*face1));
BOOST_CHECK_EQUAL(isInCs(*data1), T::shouldAdmitLocal);
auto face2 = make_shared<DummyFace>("dummy://", "dummy://",
ndn::nfd::FACE_SCOPE_NON_LOCAL);
faceTable.add(face2);
auto data2 = makeData("/unsolicited-from-non-local");
forwarder.onIncomingData(*data2, FaceEndpoint(*face2));
BOOST_CHECK_EQUAL(isInCs(*data2), T::shouldAdmitNonLocal);
}
BOOST_AUTO_TEST_SUITE_END() // TestUnsolicitedDataPolicy
BOOST_AUTO_TEST_SUITE_END() // Fw
} // namespace nfd::tests