Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, 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" |
| 27 | #include "dummy-strategy.hpp" |
| 28 | #include "tests/daemon/face/dummy-face.hpp" |
| 29 | #include <boost/range/adaptor/filtered.hpp> |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 30 | #include <boost/range/adaptor/transformed.hpp> |
| 31 | #include <boost/range/algorithm/copy.hpp> |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 32 | |
| 33 | #include "tests/test-common.hpp" |
| 34 | |
| 35 | namespace nfd { |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 36 | namespace fw { |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 37 | namespace tests { |
| 38 | |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 39 | using namespace nfd::tests; |
| 40 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Fw) |
| 42 | BOOST_FIXTURE_TEST_SUITE(TestStrategy, BaseFixture) |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 43 | |
| 44 | class FaceTableAccessTestStrategy : public DummyStrategy |
| 45 | { |
| 46 | public: |
| 47 | explicit |
| 48 | FaceTableAccessTestStrategy(Forwarder& forwarder) |
| 49 | : DummyStrategy(forwarder, Name("ndn:/strategy")) |
| 50 | { |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 51 | this->afterAddFace.connect([this] (const Face& face) { |
| 52 | this->addedFaces.push_back(face.getId()); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 53 | }); |
Junxiao Shi | ae04d34 | 2016-07-19 13:20:22 +0000 | [diff] [blame] | 54 | this->beforeRemoveFace.connect([this] (const Face& face) { |
| 55 | this->removedFaces.push_back(face.getId()); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 56 | }); |
| 57 | } |
| 58 | |
| 59 | std::vector<FaceId> |
| 60 | getLocalFaces() |
| 61 | { |
| 62 | auto enumerable = this->getFaceTable() | |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 63 | boost::adaptors::filtered([] (Face& face) { |
| 64 | return face.getScope() == ndn::nfd::FACE_SCOPE_LOCAL; |
| 65 | }) | |
| 66 | boost::adaptors::transformed([] (Face& face) { |
| 67 | return face.getId(); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 68 | }); |
| 69 | |
| 70 | std::vector<FaceId> results; |
Junxiao Shi | b84e674 | 2016-07-19 13:16:22 +0000 | [diff] [blame] | 71 | boost::copy(enumerable, std::back_inserter(results)); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 72 | return results; |
| 73 | } |
| 74 | |
| 75 | public: |
| 76 | std::vector<FaceId> addedFaces; |
| 77 | std::vector<FaceId> removedFaces; |
| 78 | }; |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(FaceTableAccess) |
| 81 | { |
| 82 | Forwarder forwarder; |
| 83 | FaceTableAccessTestStrategy strategy(forwarder); |
| 84 | |
| 85 | auto face1 = make_shared<DummyFace>(); |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 86 | auto face2 = make_shared<DummyFace>("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL); |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 87 | forwarder.addFace(face1); |
| 88 | forwarder.addFace(face2); |
| 89 | FaceId id1 = face1->getId(); |
| 90 | FaceId id2 = face2->getId(); |
| 91 | |
| 92 | BOOST_CHECK(strategy.getLocalFaces() == std::vector<FaceId>{id2}); |
| 93 | |
| 94 | face2->close(); |
| 95 | face1->close(); |
| 96 | |
| 97 | BOOST_CHECK((strategy.addedFaces == std::vector<FaceId>{id1, id2})); |
| 98 | BOOST_CHECK((strategy.removedFaces == std::vector<FaceId>{id2, id1})); |
| 99 | } |
| 100 | |
Junxiao Shi | cf0f3ce | 2016-09-02 13:01:59 +0000 | [diff] [blame] | 101 | class LookupFibFixture : public BaseFixture |
| 102 | { |
| 103 | protected: |
| 104 | class TestStrategy : public DummyStrategy |
| 105 | { |
| 106 | public: |
| 107 | explicit |
| 108 | TestStrategy(Forwarder& forwarder) |
| 109 | : DummyStrategy(forwarder, Name("ndn:/strategy")) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | const fib::Entry& |
| 114 | lookupFib(const pit::Entry& pitEntry) const |
| 115 | { |
| 116 | return this->Strategy::lookupFib(pitEntry); |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | LookupFibFixture() |
| 121 | : strategy(forwarder) |
| 122 | , fib(forwarder.getFib()) |
| 123 | , pit(forwarder.getPit()) |
| 124 | , nrt(forwarder.getNetworkRegionTable()) |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | protected: |
| 129 | Forwarder forwarder; |
| 130 | TestStrategy strategy; |
| 131 | Fib& fib; |
| 132 | Pit& pit; |
| 133 | NetworkRegionTable& nrt; |
| 134 | }; |
| 135 | |
| 136 | BOOST_FIXTURE_TEST_CASE(LookupFib, LookupFibFixture) |
| 137 | { |
| 138 | /// \todo test lookupFib without Link |
| 139 | /// \todo split each step to a separate test case |
| 140 | |
| 141 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 142 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 143 | forwarder.addFace(face1); |
| 144 | forwarder.addFace(face2); |
| 145 | |
| 146 | shared_ptr<Link> link = makeLink("/net/ndnsim", {{10, "/telia/terabits"}, {20, "/ucla/cs"}}); |
| 147 | |
| 148 | // consumer region |
| 149 | nrt.clear(); |
| 150 | nrt.insert("/arizona/cs/avenir"); |
| 151 | fib.insert("/").first->addNextHop(*face2, 10); |
| 152 | |
| 153 | auto interest1 = makeInterest("/net/ndnsim/www/1.html"); |
| 154 | interest1->setLink(link->wireEncode()); |
| 155 | shared_ptr<pit::Entry> pit1 = pit.insert(*interest1).first; |
| 156 | pit1->insertOrUpdateInRecord(*face1, *interest1); |
| 157 | |
| 158 | BOOST_CHECK_EQUAL(strategy.lookupFib(*pit1).getPrefix(), "/"); |
| 159 | BOOST_CHECK_EQUAL(interest1->hasSelectedDelegation(), false); |
| 160 | |
| 161 | fib.insert("/").first->removeNextHop(*face2); |
| 162 | |
| 163 | // first default-free router, both delegations are available |
| 164 | nrt.clear(); |
| 165 | nrt.insert("/arizona/cs/hobo"); |
| 166 | fib.insert("/telia").first->addNextHop(*face2, 10); |
| 167 | fib.insert("/ucla").first->addNextHop(*face2, 10); |
| 168 | |
| 169 | auto interest2 = makeInterest("/net/ndnsim/www/2.html"); |
| 170 | interest2->setLink(link->wireEncode()); |
| 171 | shared_ptr<pit::Entry> pit2 = pit.insert(*interest2).first; |
| 172 | pit2->insertOrUpdateInRecord(*face1, *interest2); |
| 173 | |
| 174 | BOOST_CHECK_EQUAL(strategy.lookupFib(*pit2).getPrefix(), "/telia"); |
| 175 | BOOST_REQUIRE_EQUAL(interest2->hasSelectedDelegation(), true); |
| 176 | BOOST_CHECK_EQUAL(interest2->getSelectedDelegation(), "/telia/terabits"); |
| 177 | |
| 178 | fib.erase("/telia"); |
| 179 | fib.erase("/ucla"); |
| 180 | |
| 181 | // first default-free router, only second delegation is available |
| 182 | nrt.clear(); |
| 183 | nrt.insert("/arizona/cs/hobo"); |
| 184 | fib.insert("/ucla").first->addNextHop(*face2, 10); |
| 185 | |
| 186 | auto interest3 = makeInterest("/net/ndnsim/www/3.html"); |
| 187 | interest3->setLink(link->wireEncode()); |
| 188 | shared_ptr<pit::Entry> pit3 = pit.insert(*interest3).first; |
| 189 | pit3->insertOrUpdateInRecord(*face1, *interest3); |
| 190 | |
| 191 | BOOST_CHECK_EQUAL(strategy.lookupFib(*pit3).getPrefix(), "/ucla"); |
| 192 | BOOST_REQUIRE_EQUAL(interest3->hasSelectedDelegation(), true); |
| 193 | BOOST_CHECK_EQUAL(interest3->getSelectedDelegation(), "/ucla/cs"); |
| 194 | |
| 195 | fib.erase("/ucla"); |
| 196 | |
| 197 | // default-free router, chosen SelectedDelegation |
| 198 | nrt.clear(); |
| 199 | nrt.insert("/ucsd/caida/click"); |
| 200 | fib.insert("/telia").first->addNextHop(*face2, 10); |
| 201 | fib.insert("/ucla").first->addNextHop(*face2, 10); |
| 202 | |
| 203 | auto interest4 = makeInterest("/net/ndnsim/www/4.html"); |
| 204 | interest4->setLink(link->wireEncode()); |
| 205 | interest4->setSelectedDelegation("/ucla/cs"); |
| 206 | shared_ptr<pit::Entry> pit4 = pit.insert(*interest4).first; |
| 207 | pit4->insertOrUpdateInRecord(*face1, *interest4); |
| 208 | |
| 209 | BOOST_CHECK_EQUAL(strategy.lookupFib(*pit4).getPrefix(), "/ucla"); |
| 210 | BOOST_REQUIRE_EQUAL(interest4->hasSelectedDelegation(), true); |
| 211 | BOOST_CHECK_EQUAL(interest4->getSelectedDelegation(), "/ucla/cs"); |
| 212 | |
| 213 | fib.erase("/telia"); |
| 214 | fib.erase("/ucla"); |
| 215 | |
| 216 | // producer region |
| 217 | nrt.clear(); |
| 218 | nrt.insert("/ucla/cs/spurs"); |
| 219 | fib.insert("/").first->addNextHop(*face2, 10); |
| 220 | fib.insert("/ucla").first->addNextHop(*face2, 10); |
| 221 | fib.insert("/net/ndnsim").first->addNextHop(*face2, 10); |
| 222 | |
| 223 | auto interest5 = makeInterest("/net/ndnsim/www/5.html"); |
| 224 | interest5->setLink(link->wireEncode()); |
| 225 | interest5->setSelectedDelegation("/ucla/cs"); |
| 226 | shared_ptr<pit::Entry> pit5 = pit.insert(*interest5).first; |
| 227 | pit5->insertOrUpdateInRecord(*face1, *interest5); |
| 228 | |
| 229 | BOOST_CHECK_EQUAL(strategy.lookupFib(*pit1).getPrefix(), "/net/ndnsim"); |
| 230 | BOOST_REQUIRE_EQUAL(interest5->hasSelectedDelegation(), true); |
| 231 | BOOST_CHECK_EQUAL(interest5->getSelectedDelegation(), "/ucla/cs"); |
| 232 | |
| 233 | fib.insert("/").first->removeNextHop(*face2); |
| 234 | fib.erase("/ucla"); |
| 235 | fib.erase("/ndnsim"); |
| 236 | } |
| 237 | |
Junxiao Shi | cde37ad | 2015-12-24 01:02:05 -0700 | [diff] [blame] | 238 | BOOST_AUTO_TEST_SUITE_END() // TestStrategy |
| 239 | BOOST_AUTO_TEST_SUITE_END() // Fw |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 240 | |
| 241 | } // namespace tests |
Spyridon Mastorakis | d0381c0 | 2015-02-19 10:29:41 -0800 | [diff] [blame] | 242 | } // namespace fw |
Junxiao Shi | 49e11e7 | 2014-12-14 19:46:05 -0700 | [diff] [blame] | 243 | } // namespace nfd |