blob: c1fdabd20af60a78c23f7762fdd6f493b1d127ab [file] [log] [blame]
Junxiao Shi49e11e72014-12-14 19:46:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04002/*
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -04003 * Copyright (c) 2014-2022, Regents of the University of California,
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -08004 * 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.
Junxiao Shi49e11e72014-12-14 19:46:05 -070010 *
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/strategy.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040027
28#include "tests/test-common.hpp"
29#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi49e11e72014-12-14 19:46:05 -070030#include "tests/daemon/face/dummy-face.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040031#include "dummy-strategy.hpp"
32
Junxiao Shi49e11e72014-12-14 19:46:05 -070033#include <boost/range/adaptor/filtered.hpp>
Junxiao Shib84e6742016-07-19 13:16:22 +000034#include <boost/range/adaptor/transformed.hpp>
35#include <boost/range/algorithm/copy.hpp>
Junxiao Shi49e11e72014-12-14 19:46:05 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Junxiao Shi49e11e72014-12-14 19:46:05 -070038
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040039using namespace nfd::fw;
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080040
Junxiao Shicde37ad2015-12-24 01:02:05 -070041BOOST_AUTO_TEST_SUITE(Fw)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042BOOST_FIXTURE_TEST_SUITE(TestStrategy, GlobalIoFixture)
Junxiao Shi49e11e72014-12-14 19:46:05 -070043
Eric Newberry2377ada2020-09-28 22:40:14 -070044// Strategy registry is tested in strategy-choice.t.cpp and strategy-instantiation.t.cpp
Junxiao Shi91f6ee02016-12-29 21:44:44 +000045
Junxiao Shi49e11e72014-12-14 19:46:05 -070046class FaceTableAccessTestStrategy : public DummyStrategy
47{
48public:
49 explicit
50 FaceTableAccessTestStrategy(Forwarder& forwarder)
Junxiao Shi0e4a1f12016-12-24 02:39:01 +000051 : DummyStrategy(forwarder)
Junxiao Shi49e11e72014-12-14 19:46:05 -070052 {
Junxiao Shiae04d342016-07-19 13:20:22 +000053 this->afterAddFace.connect([this] (const Face& face) {
54 this->addedFaces.push_back(face.getId());
Junxiao Shi49e11e72014-12-14 19:46:05 -070055 });
Junxiao Shiae04d342016-07-19 13:20:22 +000056 this->beforeRemoveFace.connect([this] (const Face& face) {
57 this->removedFaces.push_back(face.getId());
Junxiao Shi49e11e72014-12-14 19:46:05 -070058 });
59 }
60
61 std::vector<FaceId>
62 getLocalFaces()
63 {
64 auto enumerable = this->getFaceTable() |
Junxiao Shib84e6742016-07-19 13:16:22 +000065 boost::adaptors::filtered([] (Face& face) {
66 return face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL;
67 }) |
68 boost::adaptors::transformed([] (Face& face) {
69 return face.getId();
Junxiao Shi49e11e72014-12-14 19:46:05 -070070 });
71
72 std::vector<FaceId> results;
Junxiao Shib84e6742016-07-19 13:16:22 +000073 boost::copy(enumerable, std::back_inserter(results));
Junxiao Shi49e11e72014-12-14 19:46:05 -070074 return results;
75 }
76
77public:
78 std::vector<FaceId> addedFaces;
79 std::vector<FaceId> removedFaces;
80};
81
82BOOST_AUTO_TEST_CASE(FaceTableAccess)
83{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040084 FaceTable faceTable;
85 Forwarder forwarder(faceTable);
Junxiao Shi49e11e72014-12-14 19:46:05 -070086 FaceTableAccessTestStrategy strategy(forwarder);
87
88 auto face1 = make_shared<DummyFace>();
Junxiao Shicde37ad2015-12-24 01:02:05 -070089 auto face2 = make_shared<DummyFace>("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040090 faceTable.add(face1);
91 faceTable.add(face2);
Junxiao Shi49e11e72014-12-14 19:46:05 -070092 FaceId id1 = face1->getId();
93 FaceId id2 = face2->getId();
94
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -040095 BOOST_TEST(strategy.getLocalFaces() == std::vector<FaceId>{id2}, boost::test_tools::per_element());
Junxiao Shi49e11e72014-12-14 19:46:05 -070096
97 face2->close();
98 face1->close();
99
Ashlesh Gawande1ef93d02022-04-08 00:25:06 -0400100 BOOST_TEST((strategy.addedFaces == std::vector<FaceId>{id1, id2}), boost::test_tools::per_element());
101 BOOST_TEST((strategy.removedFaces == std::vector<FaceId>{id2, id1}), boost::test_tools::per_element());
102}
103
104BOOST_AUTO_TEST_CASE(ParseParameters)
105{
106 BOOST_TEST(Strategy::parseParameters("").empty());
107 BOOST_TEST(Strategy::parseParameters("/").empty());
108 BOOST_CHECK_THROW(Strategy::parseParameters("/foo"), std::invalid_argument);
109 BOOST_CHECK_THROW(Strategy::parseParameters("/foo~"), std::invalid_argument);
110 BOOST_CHECK_THROW(Strategy::parseParameters("/~bar"), std::invalid_argument);
111 BOOST_CHECK_THROW(Strategy::parseParameters("/~"), std::invalid_argument);
112 BOOST_CHECK_THROW(Strategy::parseParameters("/~~"), std::invalid_argument);
113
114 StrategyParameters expected;
115 expected["foo"] = "bar";
116 BOOST_TEST(Strategy::parseParameters("/foo~bar") == expected);
117 BOOST_CHECK_THROW(Strategy::parseParameters("/foo~bar/42"), std::invalid_argument);
118 expected["the-answer"] = "42";
119 BOOST_TEST(Strategy::parseParameters("/foo~bar/the-answer~42") == expected);
120 expected["foo"] = "foo2";
121 BOOST_TEST(Strategy::parseParameters("/foo~bar/the-answer~42/foo~foo2") == expected);
Junxiao Shi49e11e72014-12-14 19:46:05 -0700122}
123
Junxiao Shicde37ad2015-12-24 01:02:05 -0700124BOOST_AUTO_TEST_SUITE_END() // TestStrategy
125BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shi49e11e72014-12-14 19:46:05 -0700126
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400127} // namespace nfd::tests