blob: 419a7ba600f277f64f8ea7d0b12d4d915f610654 [file] [log] [blame]
Junxiao Shi8c8d2182014-01-30 22:33:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (C) 2014 Named Data Networking Project
4 * See COPYING for copyright and distribution information.
5 */
6
7#include "fw/forwarder.hpp"
Junxiao Shid9ee45c2014-02-27 15:38:11 -07008#include "tests/face/dummy-face.hpp"
Junxiao Shif3c07812014-03-11 21:48:49 -07009#include "dummy-strategy.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070010
Junxiao Shid9ee45c2014-02-27 15:38:11 -070011#include "tests/test-common.hpp"
Junxiao Shif3c07812014-03-11 21:48:49 -070012#include "tests/core/limited-io.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070013
14namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015namespace tests {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070016
Junxiao Shid9ee45c2014-02-27 15:38:11 -070017BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070018
Junxiao Shi8c8d2182014-01-30 22:33:00 -070019BOOST_AUTO_TEST_CASE(SimpleExchange)
20{
Junxiao Shic041ca32014-02-25 20:01:15 -070021 Forwarder forwarder;
22
Junxiao Shi8c8d2182014-01-30 22:33:00 -070023 Name nameA ("ndn:/A");
24 Name nameAB ("ndn:/A/B");
25 Name nameABC("ndn:/A/B/C");
Junxiao Shif3c07812014-03-11 21:48:49 -070026 shared_ptr<Interest> interestAB = makeInterest(nameAB);
27 interestAB->setInterestLifetime(4000);
28 shared_ptr<Data> dataABC = makeData(nameABC);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
31 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
32 face1->afterSend += bind(&boost::asio::io_service::stop, &g_io);
33 face2->afterSend += bind(&boost::asio::io_service::stop, &g_io);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070034 forwarder.addFace(face1);
35 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070036
Junxiao Shi8c8d2182014-01-30 22:33:00 -070037 Fib& fib = forwarder.getFib();
38 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
39 fib.insert(Name("ndn:/A"));
40 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
41 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070042
Junxiao Shif3c07812014-03-11 21:48:49 -070043 face1->receiveInterest(*interestAB);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070044 g_io.run();
45 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070046 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
47 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -070048 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070049
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080050 face2->receiveData(*dataABC);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070051 g_io.run();
52 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070053 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
54 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -070055 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -070056}
57
Junxiao Shi9b27bd22014-02-26 20:29:58 -070058class ScopeLocalhostIncomingTestForwarder : public Forwarder
Junxiao Shi88884492014-02-15 15:57:43 -070059{
60public:
Junxiao Shi9b27bd22014-02-26 20:29:58 -070061 ScopeLocalhostIncomingTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -070062 {
63 }
64
65 virtual void
66 onDataUnsolicited(Face& inFace, const Data& data)
67 {
68 ++m_onDataUnsolicited_count;
69 }
70
71protected:
72 virtual void
Junxiao Shif3c07812014-03-11 21:48:49 -070073 dispatchToStrategy(shared_ptr<pit::Entry> pitEntry, function<void(fw::Strategy*)> f)
Junxiao Shi88884492014-02-15 15:57:43 -070074 {
75 ++m_dispatchToStrategy_count;
76 }
77
78public:
79 int m_dispatchToStrategy_count;
80 int m_onDataUnsolicited_count;
81};
82
Junxiao Shi9b27bd22014-02-26 20:29:58 -070083BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -070084{
Junxiao Shi9b27bd22014-02-26 20:29:58 -070085 ScopeLocalhostIncomingTestForwarder forwarder;
86 shared_ptr<Face> face1 = make_shared<DummyLocalFace>();
87 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -070088 forwarder.addFace(face1);
89 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070090
Junxiao Shi88884492014-02-15 15:57:43 -070091 // local face, /localhost: OK
92 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -070093 shared_ptr<Interest> i1 = makeInterest("/localhost/A1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080094 forwarder.onIncomingInterest(*face1, *i1);
Junxiao Shi88884492014-02-15 15:57:43 -070095 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -070096
Junxiao Shi88884492014-02-15 15:57:43 -070097 // non-local face, /localhost: violate
98 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -070099 shared_ptr<Interest> i2 = makeInterest("/localhost/A2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800100 forwarder.onIncomingInterest(*face2, *i2);
Junxiao Shi88884492014-02-15 15:57:43 -0700101 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700102
Junxiao Shi88884492014-02-15 15:57:43 -0700103 // local face, non-/localhost: OK
104 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700105 shared_ptr<Interest> i3 = makeInterest("/A3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800106 forwarder.onIncomingInterest(*face1, *i3);
Junxiao Shi88884492014-02-15 15:57:43 -0700107 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700108
Junxiao Shi88884492014-02-15 15:57:43 -0700109 // non-local face, non-/localhost: OK
110 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700111 shared_ptr<Interest> i4 = makeInterest("/A4");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800112 forwarder.onIncomingInterest(*face2, *i4);
Junxiao Shi88884492014-02-15 15:57:43 -0700113 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700114
Junxiao Shi88884492014-02-15 15:57:43 -0700115 // local face, /localhost: OK
116 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700117 shared_ptr<Data> d1 = makeData("/localhost/B1");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800118 forwarder.onIncomingData(*face1, *d1);
Junxiao Shi88884492014-02-15 15:57:43 -0700119 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
120
121 // non-local face, /localhost: OK
122 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700123 shared_ptr<Data> d2 = makeData("/localhost/B2");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800124 forwarder.onIncomingData(*face2, *d2);
Junxiao Shi88884492014-02-15 15:57:43 -0700125 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700126
Junxiao Shi88884492014-02-15 15:57:43 -0700127 // local face, non-/localhost: OK
128 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700129 shared_ptr<Data> d3 = makeData("/B3");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800130 forwarder.onIncomingData(*face1, *d3);
Junxiao Shi88884492014-02-15 15:57:43 -0700131 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
132
133 // non-local face, non-/localhost: OK
134 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shif3c07812014-03-11 21:48:49 -0700135 shared_ptr<Data> d4 = makeData("/B4");
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800136 forwarder.onIncomingData(*face2, *d4);
Junxiao Shi88884492014-02-15 15:57:43 -0700137 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
138}
139
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700140BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing)
141{
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700142 Forwarder forwarder;
143 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
144 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
145 shared_ptr<Face> face3 = make_shared<DummyLocalFace>();
146 forwarder.addFace(face1);
147 forwarder.addFace(face2);
148 forwarder.addFace(face3);
149 Pit& pit = forwarder.getPit();
150
151 // local face, /localhost: OK
Junxiao Shif3c07812014-03-11 21:48:49 -0700152 shared_ptr<Interest> interestA1 = makeInterest("/localhost/A1");
153 shared_ptr<pit::Entry> pitA1 = pit.insert(*interestA1).first;
154 pitA1->insertOrUpdateInRecord(face3, *interestA1);
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700155 face1->m_sentInterests.clear();
156 forwarder.onOutgoingInterest(pitA1, *face1);
157 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
158
159 // non-local face, /localhost: violate
Junxiao Shif3c07812014-03-11 21:48:49 -0700160 shared_ptr<Interest> interestA2 = makeInterest("/localhost/A2");
161 shared_ptr<pit::Entry> pitA2 = pit.insert(*interestA2).first;
162 pitA2->insertOrUpdateInRecord(face3, *interestA2);
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700163 face2->m_sentInterests.clear();
164 forwarder.onOutgoingInterest(pitA2, *face2);
165 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0);
166
167 // local face, non-/localhost: OK
Junxiao Shif3c07812014-03-11 21:48:49 -0700168 shared_ptr<Interest> interestA3 = makeInterest("/A3");
169 shared_ptr<pit::Entry> pitA3 = pit.insert(*interestA3).first;
170 pitA3->insertOrUpdateInRecord(face3, *interestA3);
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700171 face1->m_sentInterests.clear();
172 forwarder.onOutgoingInterest(pitA3, *face1);
173 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
174
175 // non-local face, non-/localhost: OK
Junxiao Shif3c07812014-03-11 21:48:49 -0700176 shared_ptr<Interest> interestA4 = makeInterest("/A4");
177 shared_ptr<pit::Entry> pitA4 = pit.insert(*interestA4).first;
178 pitA4->insertOrUpdateInRecord(face3, *interestA4);
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700179 face2->m_sentInterests.clear();
180 forwarder.onOutgoingInterest(pitA4, *face2);
181 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
182
183 // local face, /localhost: OK
184 face1->m_sentDatas.clear();
185 forwarder.onOutgoingData(Data("/localhost/B1"), *face1);
186 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
187
188 // non-local face, /localhost: OK
189 face2->m_sentDatas.clear();
190 forwarder.onOutgoingData(Data("/localhost/B2"), *face2);
191 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0);
192
193 // local face, non-/localhost: OK
194 face1->m_sentDatas.clear();
195 forwarder.onOutgoingData(Data("/B3"), *face1);
196 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
197
198 // non-local face, non-/localhost: OK
199 face2->m_sentDatas.clear();
200 forwarder.onOutgoingData(Data("/B4"), *face2);
201 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1);
202}
203
Junxiao Shif3c07812014-03-11 21:48:49 -0700204BOOST_AUTO_TEST_CASE(StrategyDispatch)
205{
206 LimitedIo limitedIo;
207 Forwarder forwarder;
208 shared_ptr<Face> face1 = make_shared<DummyFace>();
209 shared_ptr<Face> face2 = make_shared<DummyFace>();
210 forwarder.addFace(face1);
211 forwarder.addFace(face2);
212
213 StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
214 shared_ptr<DummyStrategy> strategyP = make_shared<DummyStrategy>(
215 boost::ref(forwarder), "ndn:/strategyP");
216 shared_ptr<DummyStrategy> strategyQ = make_shared<DummyStrategy>(
217 boost::ref(forwarder), "ndn:/strategyQ");
218 strategyChoice.install(strategyP);
219 strategyChoice.install(strategyQ);
220 strategyChoice.insert("ndn:/" , strategyP->getName());
221 strategyChoice.insert("ndn:/B", strategyQ->getName());
222
223 shared_ptr<Interest> interest1 = makeInterest("ndn:/A/1");
224 strategyP->m_afterReceiveInterest_count = 0;
225 strategyP->m_interestOutFace = face2;
226 forwarder.onInterest(*face1, *interest1);
227 BOOST_CHECK_EQUAL(strategyP->m_afterReceiveInterest_count, 1);
228
229 shared_ptr<Interest> interest2 = makeInterest("ndn:/B/2");
230 strategyQ->m_afterReceiveInterest_count = 0;
231 strategyQ->m_interestOutFace = face2;
232 forwarder.onInterest(*face1, *interest2);
233 BOOST_CHECK_EQUAL(strategyQ->m_afterReceiveInterest_count, 1);
234
235 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(5));
236
237 shared_ptr<Data> data1 = makeData("ndn:/A/1/a");
238 strategyP->m_beforeSatisfyPendingInterest_count = 0;
239 forwarder.onData(*face2, *data1);
240 BOOST_CHECK_EQUAL(strategyP->m_beforeSatisfyPendingInterest_count, 1);
241
242 shared_ptr<Data> data2 = makeData("ndn:/B/2/b");
243 strategyQ->m_beforeSatisfyPendingInterest_count = 0;
244 forwarder.onData(*face2, *data2);
245 BOOST_CHECK_EQUAL(strategyQ->m_beforeSatisfyPendingInterest_count, 1);
246
247 shared_ptr<Interest> interest3 = makeInterest("ndn:/A/3");
248 interest3->setInterestLifetime(30);
249 forwarder.onInterest(*face1, *interest3);
250 shared_ptr<Interest> interest4 = makeInterest("ndn:/B/4");
251 interest4->setInterestLifetime(5000);
252 forwarder.onInterest(*face1, *interest4);
253
254 strategyP->m_beforeExpirePendingInterest_count = 0;
255 strategyQ->m_beforeExpirePendingInterest_count = 0;
256 limitedIo.run(LimitedIo::UNLIMITED_OPS, time::milliseconds(100));
257 BOOST_CHECK_EQUAL(strategyP->m_beforeExpirePendingInterest_count, 1);
258 BOOST_CHECK_EQUAL(strategyQ->m_beforeExpirePendingInterest_count, 0);
259}
260
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700261BOOST_AUTO_TEST_SUITE_END()
262
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700263} // namespace tests
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700264} // namespace nfd