blob: 455c80ad1183d2138b91da75fba2c2eca6aa53f3 [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
44class FaceTableAccessTestStrategy : public DummyStrategy
45{
46public:
47 explicit
48 FaceTableAccessTestStrategy(Forwarder& forwarder)
49 : DummyStrategy(forwarder, Name("ndn:/strategy"))
50 {
Junxiao Shiae04d342016-07-19 13:20:22 +000051 this->afterAddFace.connect([this] (const Face& face) {
52 this->addedFaces.push_back(face.getId());
Junxiao Shi49e11e72014-12-14 19:46:05 -070053 });
Junxiao Shiae04d342016-07-19 13:20:22 +000054 this->beforeRemoveFace.connect([this] (const Face& face) {
55 this->removedFaces.push_back(face.getId());
Junxiao Shi49e11e72014-12-14 19:46:05 -070056 });
57 }
58
59 std::vector<FaceId>
60 getLocalFaces()
61 {
62 auto enumerable = this->getFaceTable() |
Junxiao Shib84e6742016-07-19 13:16:22 +000063 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 Shi49e11e72014-12-14 19:46:05 -070068 });
69
70 std::vector<FaceId> results;
Junxiao Shib84e6742016-07-19 13:16:22 +000071 boost::copy(enumerable, std::back_inserter(results));
Junxiao Shi49e11e72014-12-14 19:46:05 -070072 return results;
73 }
74
75public:
76 std::vector<FaceId> addedFaces;
77 std::vector<FaceId> removedFaces;
78};
79
80BOOST_AUTO_TEST_CASE(FaceTableAccess)
81{
82 Forwarder forwarder;
83 FaceTableAccessTestStrategy strategy(forwarder);
84
85 auto face1 = make_shared<DummyFace>();
Junxiao Shicde37ad2015-12-24 01:02:05 -070086 auto face2 = make_shared<DummyFace>("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
Junxiao Shi49e11e72014-12-14 19:46:05 -070087 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 Shicf0f3ce2016-09-02 13:01:59 +0000101class LookupFibFixture : public BaseFixture
102{
103protected:
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())
Junxiao Shi0dba0102016-09-06 01:57:53 +0000125 , inFace(make_shared<DummyFace>())
126 , outFace(make_shared<DummyFace>())
127 , link(makeLink("/net/ndnsim", {{10, "/telia/terabits"}, {20, "/ucla/cs"}}))
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000128 {
Junxiao Shi0dba0102016-09-06 01:57:53 +0000129 forwarder.addFace(inFace);
130 forwarder.addFace(outFace);
131 }
132
133 const fib::Entry&
134 lookupFib(Interest& interest)
135 {
136 shared_ptr<pit::Entry> pitEntry = pit.insert(interest).first;
137 pitEntry->insertOrUpdateInRecord(*inFace, interest);
138 return strategy.lookupFib(*pitEntry);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000139 }
140
141protected:
142 Forwarder forwarder;
143 TestStrategy strategy;
144 Fib& fib;
145 Pit& pit;
146 NetworkRegionTable& nrt;
Junxiao Shi0dba0102016-09-06 01:57:53 +0000147
148 shared_ptr<Face> inFace;
149 shared_ptr<Face> outFace;
150 shared_ptr<Link> link;
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000151};
152
Junxiao Shi0dba0102016-09-06 01:57:53 +0000153BOOST_FIXTURE_TEST_SUITE(LookupFib, LookupFibFixture)
154
155BOOST_AUTO_TEST_CASE(NoLink)
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000156{
Junxiao Shi0dba0102016-09-06 01:57:53 +0000157 fib.insert("/net/ndnsim").first->addNextHop(*outFace, 10);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000158
Junxiao Shi0dba0102016-09-06 01:57:53 +0000159 auto interest = makeInterest("/net/ndnsim/www/index.html");
160 const fib::Entry& fibEntry = this->lookupFib(*interest);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000161
Junxiao Shi0dba0102016-09-06 01:57:53 +0000162 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/net/ndnsim");
163 BOOST_CHECK_EQUAL(interest->hasSelectedDelegation(), false);
Junxiao Shicf0f3ce2016-09-02 13:01:59 +0000164}
165
Junxiao Shi0dba0102016-09-06 01:57:53 +0000166BOOST_AUTO_TEST_CASE(ConsumerRegion)
167{
168 nrt.insert("/arizona/cs/avenir");
169 fib.insert("/").first->addNextHop(*outFace, 10);
170
171 auto interest = makeInterest("/net/ndnsim/www/index.html");
172 interest->setLink(link->wireEncode());
173 const fib::Entry& fibEntry = this->lookupFib(*interest);
174
175 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/");
176 BOOST_CHECK_EQUAL(interest->hasSelectedDelegation(), false);
177}
178
179BOOST_AUTO_TEST_CASE(DefaultFreeFirstDegegation)
180{
181 nrt.insert("/arizona/cs/hobo");
182 fib.insert("/telia").first->addNextHop(*outFace, 20);
183 fib.insert("/ucla").first->addNextHop(*outFace, 10);
184
185 auto interest = makeInterest("/net/ndnsim/www/index.html");
186 interest->setLink(link->wireEncode());
187 const fib::Entry& fibEntry = this->lookupFib(*interest);
188
189 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/telia");
190 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
191 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/telia/terabits");
192}
193
194BOOST_AUTO_TEST_CASE(DefaultFreeSecondDelegation)
195{
196 nrt.insert("/arizona/cs/hobo");
197 fib.insert("/ucla").first->addNextHop(*outFace, 10);
198
199 auto interest = makeInterest("/net/ndnsim/www/index.html");
200 interest->setLink(link->wireEncode());
201 const fib::Entry& fibEntry = this->lookupFib(*interest);
202
203 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/ucla");
204 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
205 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
206}
207
208BOOST_AUTO_TEST_CASE(DefaultFreeHasSelectedDelegation)
209{
210 nrt.insert("/ucsd/caida/click");
211 fib.insert("/telia").first->addNextHop(*outFace, 10);
212 fib.insert("/ucla").first->addNextHop(*outFace, 10);
213
214 auto interest = makeInterest("/net/ndnsim/www/index.html");
215 interest->setLink(link->wireEncode());
216 interest->setSelectedDelegation("/ucla/cs");
217 const fib::Entry& fibEntry = this->lookupFib(*interest);
218
219 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/ucla");
220 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
221 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
222}
223
224BOOST_AUTO_TEST_CASE(ProducerRegion)
225{
226 nrt.insert("/ucla/cs/spurs");
227 fib.insert("/").first->addNextHop(*outFace, 10);
228 fib.insert("/ucla").first->addNextHop(*outFace, 10);
229 fib.insert("/net/ndnsim").first->addNextHop(*outFace, 10);
230
231 auto interest = makeInterest("/net/ndnsim/www/index.html");
232 interest->setLink(link->wireEncode());
233 interest->setSelectedDelegation("/ucla/cs");
234 const fib::Entry& fibEntry = this->lookupFib(*interest);
235
236 BOOST_CHECK_EQUAL(fibEntry.getPrefix(), "/net/ndnsim");
237 BOOST_REQUIRE_EQUAL(interest->hasSelectedDelegation(), true);
238 BOOST_CHECK_EQUAL(interest->getSelectedDelegation(), "/ucla/cs");
239}
240
241BOOST_AUTO_TEST_SUITE_END() // LookupFib
242
Junxiao Shicde37ad2015-12-24 01:02:05 -0700243BOOST_AUTO_TEST_SUITE_END() // TestStrategy
244BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shi49e11e72014-12-14 19:46:05 -0700245
246} // namespace tests
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800247} // namespace fw
Junxiao Shi49e11e72014-12-14 19:46:05 -0700248} // namespace nfd