blob: 72d43b4c88c8974d2249fa2367aa912df9baecad [file] [log] [blame]
Junxiao Shi49e11e72014-12-14 19:46:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shib84e6742016-07-19 13:16:22 +00003 * Copyright (c) 2014-2016, 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"
27#include "dummy-strategy.hpp"
28#include "tests/daemon/face/dummy-face.hpp"
29#include <boost/range/adaptor/filtered.hpp>
Junxiao Shib84e6742016-07-19 13:16:22 +000030#include <boost/range/adaptor/transformed.hpp>
31#include <boost/range/algorithm/copy.hpp>
Junxiao Shi49e11e72014-12-14 19:46:05 -070032
33#include "tests/test-common.hpp"
34
35namespace nfd {
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036namespace fw {
Junxiao Shi49e11e72014-12-14 19:46:05 -070037namespace tests {
38
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080039using namespace nfd::tests;
40
Junxiao Shicde37ad2015-12-24 01:02:05 -070041BOOST_AUTO_TEST_SUITE(Fw)
42BOOST_FIXTURE_TEST_SUITE(TestStrategy, BaseFixture)
Junxiao Shi49e11e72014-12-14 19:46:05 -070043
Junxiao Shi91f6ee02016-12-29 21:44:44 +000044// Strategy registry is tested in table/strategy-choice.t.cpp and strategy-instantiation.t.cpp
45
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{
84 Forwarder forwarder;
85 FaceTableAccessTestStrategy strategy(forwarder);
86
87 auto face1 = make_shared<DummyFace>();
Junxiao Shicde37ad2015-12-24 01:02:05 -070088 auto face2 = make_shared<DummyFace>("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
Junxiao Shi49e11e72014-12-14 19:46:05 -070089 forwarder.addFace(face1);
90 forwarder.addFace(face2);
91 FaceId id1 = face1->getId();
92 FaceId id2 = face2->getId();
93
94 BOOST_CHECK(strategy.getLocalFaces() == std::vector<FaceId>{id2});
95
96 face2->close();
97 face1->close();
98
99 BOOST_CHECK((strategy.addedFaces == std::vector<FaceId>{id1, id2}));
100 BOOST_CHECK((strategy.removedFaces == std::vector<FaceId>{id2, id1}));
101}
102
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000103class LookupFibFixture : public BaseFixture
104{
105protected:
106 class TestStrategy : public DummyStrategy
107 {
108 public:
109 explicit
110 TestStrategy(Forwarder& forwarder)
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000111 : DummyStrategy(forwarder)
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000112 {
113 }
114
115 const fib::Entry&
116 lookupFib(const pit::Entry& pitEntry) const
117 {
118 return this->Strategy::lookupFib(pitEntry);
119 }
120 };
121
122 LookupFibFixture()
123 : strategy(forwarder)
124 , fib(forwarder.getFib())
125 , pit(forwarder.getPit())
126 , nrt(forwarder.getNetworkRegionTable())
Junxiao Shi0dba0102016-09-06 01:57:53 +0000127 , inFace(make_shared<DummyFace>())
128 , outFace(make_shared<DummyFace>())
129 , link(makeLink("/net/ndnsim", {{10, "/telia/terabits"}, {20, "/ucla/cs"}}))
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000130 {
Junxiao Shi0dba0102016-09-06 01:57:53 +0000131 forwarder.addFace(inFace);
132 forwarder.addFace(outFace);
133 }
134
135 const fib::Entry&
136 lookupFib(Interest& interest)
137 {
138 shared_ptr<pit::Entry> pitEntry = pit.insert(interest).first;
139 pitEntry->insertOrUpdateInRecord(*inFace, interest);
140 return strategy.lookupFib(*pitEntry);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000141 }
142
143protected:
144 Forwarder forwarder;
145 TestStrategy strategy;
146 Fib& fib;
147 Pit& pit;
148 NetworkRegionTable& nrt;
Junxiao Shi0dba0102016-09-06 01:57:53 +0000149
150 shared_ptr<Face> inFace;
151 shared_ptr<Face> outFace;
152 shared_ptr<Link> link;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000153};
154
Junxiao Shi0dba0102016-09-06 01:57:53 +0000155BOOST_FIXTURE_TEST_SUITE(LookupFib, LookupFibFixture)
156
157BOOST_AUTO_TEST_CASE(NoLink)
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000158{
Junxiao Shi0dba0102016-09-06 01:57:53 +0000159 fib.insert("/net/ndnsim").first->addNextHop(*outFace, 10);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000160
Junxiao Shi0dba0102016-09-06 01:57:53 +0000161 auto interest = makeInterest("/net/ndnsim/www/index.html");
162 const fib::Entry& fibEntry = this->lookupFib(*interest);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000163
Junxiao Shi0dba0102016-09-06 01:57:53 +0000164 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/net/ndnsim");
165 BOOST_CHECK_EQUAL(interest->hasSelectedDelegation(), false);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000166}
167
Junxiao Shi0dba0102016-09-06 01:57:53 +0000168BOOST_AUTO_TEST_CASE(ConsumerRegion)
169{
170 nrt.insert("/arizona/cs/avenir");
171 fib.insert("/").first->addNextHop(*outFace, 10);
172
173 auto interest = makeInterest("/net/ndnsim/www/index.html");
174 interest->setLink(link->wireEncode());
175 const fib::Entry& fibEntry = this->lookupFib(*interest);
176
177 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/");
178 BOOST_CHECK_EQUAL(interest->hasSelectedDelegation(), false);
179}
180
Junxiao Shi8527f862016-09-12 21:32:35 +0000181BOOST_AUTO_TEST_CASE(DefaultFreeFirstDelegation)
Junxiao Shi0dba0102016-09-06 01:57:53 +0000182{
183 nrt.insert("/arizona/cs/hobo");
184 fib.insert("/telia").first->addNextHop(*outFace, 20);
185 fib.insert("/ucla").first->addNextHop(*outFace, 10);
186
187 auto interest = makeInterest("/net/ndnsim/www/index.html");
188 interest->setLink(link->wireEncode());
189 const fib::Entry& fibEntry = this->lookupFib(*interest);
190
191 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/telia");
192 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
193 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/telia/terabits");
194}
195
196BOOST_AUTO_TEST_CASE(DefaultFreeSecondDelegation)
197{
198 nrt.insert("/arizona/cs/hobo");
199 fib.insert("/ucla").first->addNextHop(*outFace, 10);
200
201 auto interest = makeInterest("/net/ndnsim/www/index.html");
202 interest->setLink(link->wireEncode());
203 const fib::Entry& fibEntry = this->lookupFib(*interest);
204
205 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/ucla");
206 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
207 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
208}
209
210BOOST_AUTO_TEST_CASE(DefaultFreeHasSelectedDelegation)
211{
212 nrt.insert("/ucsd/caida/click");
213 fib.insert("/telia").first->addNextHop(*outFace, 10);
214 fib.insert("/ucla").first->addNextHop(*outFace, 10);
215
216 auto interest = makeInterest("/net/ndnsim/www/index.html");
217 interest->setLink(link->wireEncode());
218 interest->setSelectedDelegation("/ucla/cs");
219 const fib::Entry& fibEntry = this->lookupFib(*interest);
220
221 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/ucla");
222 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
223 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
224}
225
226BOOST_AUTO_TEST_CASE(ProducerRegion)
227{
228 nrt.insert("/ucla/cs/spurs");
229 fib.insert("/").first->addNextHop(*outFace, 10);
230 fib.insert("/ucla").first->addNextHop(*outFace, 10);
231 fib.insert("/net/ndnsim").first->addNextHop(*outFace, 10);
232
233 auto interest = makeInterest("/net/ndnsim/www/index.html");
234 interest->setLink(link->wireEncode());
235 interest->setSelectedDelegation("/ucla/cs");
236 const fib::Entry& fibEntry = this->lookupFib(*interest);
237
238 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/net/ndnsim");
239 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
240 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
241}
242
243BOOST_AUTO_TEST_SUITE_END() // LookupFib
244
Junxiao Shicde37ad2015-12-24 01:02:05 -0700245BOOST_AUTO_TEST_SUITE_END() // TestStrategy
246BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shi49e11e72014-12-14 19:46:05 -0700247
248} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800249} // namespace fw
Junxiao Shi49e11e72014-12-14 19:46:05 -0700250} // namespace nfd