Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 2 | /* |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [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. |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 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/strategy.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | #include "tests/daemon/global-io-fixture.hpp" |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 30 | #include "tests/daemon/face/dummy-face.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 31 | #include "dummy-strategy.hpp" |
| 32 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 33 | #include <boost/range/adaptor/filtered.hpp> |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 34 | #include <boost/range/adaptor/transformed.hpp> |
| 35 | #include <boost/range/algorithm/copy.hpp> |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 36 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 37 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 38 | namespace fw { |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 39 | namespace tests { |
| 40 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 41 | using namespace nfd::tests; |
| 42 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 43 | BOOST_AUTO_TEST_SUITE(Fw) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 44 | BOOST_FIXTURE_TEST_SUITE(TestStrategy, GlobalIoFixture) |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 45 | |
Eric Newberry | 2377ada | 2020-09-28 22:40:14 -0700 | [diff] [blame^] | 46 | // Strategy registry is tested in strategy-choice.t.cpp and strategy-instantiation.t.cpp |
Junxiao Shi | 91f6ee0 | 2016-12-29 21:44:44 +0000 | [diff] [blame] | 47 | |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 48 | class FaceTableAccessTestStrategy : public DummyStrategy |
| 49 | { |
| 50 | public: |
| 51 | explicit |
| 52 | FaceTableAccessTestStrategy(Forwarder& forwarder) |
Junxiao Shi | 0e4a1f1 | 2016-12-24 02:39:01 +0000 | [diff] [blame] | 53 | : DummyStrategy(forwarder) |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 54 | { |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 55 | this->afterAddFace.connect([this] (const Face& face) { |
| 56 | this->addedFaces.push_back(face.getId()); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 57 | }); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 58 | this->beforeRemoveFace.connect([this] (const Face& face) { |
| 59 | this->removedFaces.push_back(face.getId()); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 60 | }); |
| 61 | } |
| 62 | |
| 63 | std::vector<FaceId> |
| 64 | getLocalFaces() |
| 65 | { |
| 66 | auto enumerable = this->getFaceTable() | |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 67 | boost::adaptors::filtered([] (Face& face) { |
| 68 | return face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL; |
| 69 | }) | |
| 70 | boost::adaptors::transformed([] (Face& face) { |
| 71 | return face.getId(); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 72 | }); |
| 73 | |
| 74 | std::vector<FaceId> results; |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 75 | boost::copy(enumerable, std::back_inserter(results)); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 76 | return results; |
| 77 | } |
| 78 | |
| 79 | public: |
| 80 | std::vector<FaceId> addedFaces; |
| 81 | std::vector<FaceId> removedFaces; |
| 82 | }; |
| 83 | |
| 84 | BOOST_AUTO_TEST_CASE(FaceTableAccess) |
| 85 | { |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 86 | FaceTable faceTable; |
| 87 | Forwarder forwarder(faceTable); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 88 | FaceTableAccessTestStrategy strategy(forwarder); |
| 89 | |
| 90 | auto face1 = make_shared<DummyFace>(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 91 | auto face2 = make_shared<DummyFace>("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL); |
Davide Pesavento | a4abfb0 | 2019-10-06 16:02:56 -0400 | [diff] [blame] | 92 | faceTable.add(face1); |
| 93 | faceTable.add(face2); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 94 | FaceId id1 = face1->getId(); |
| 95 | FaceId id2 = face2->getId(); |
| 96 | |
| 97 | BOOST_CHECK(strategy.getLocalFaces() == std::vector<FaceId>{id2}); |
| 98 | |
| 99 | face2->close(); |
| 100 | face1->close(); |
| 101 | |
| 102 | BOOST_CHECK((strategy.addedFaces == std::vector<FaceId>{id1, id2})); |
| 103 | BOOST_CHECK((strategy.removedFaces == std::vector<FaceId>{id2, id1})); |
| 104 | } |
| 105 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 106 | BOOST_AUTO_TEST_SUITE_END() // TestStrategy |
| 107 | BOOST_AUTO_TEST_SUITE_END() // Fw |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 108 | |
| 109 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 110 | } // namespace fw |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 111 | } // namespace nfd |