blob: 660776601ab8b066bea15854619b03aaf10acb21 [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"
8#include "../face/dummy-face.hpp"
9
10#include <boost/test/unit_test.hpp>
11
12namespace nfd {
13
Junxiao Shi8c8d2182014-01-30 22:33:00 -070014BOOST_AUTO_TEST_SUITE(FwForwarder)
15
16BOOST_AUTO_TEST_CASE(AddRemoveFace)
17{
Junxiao Shic041ca32014-02-25 20:01:15 -070018 resetGlobalIoService();
19 Forwarder forwarder;
20
Junxiao Shi8c8d2182014-01-30 22:33:00 -070021 shared_ptr<Face> face1 = make_shared<DummyFace>();
22 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shic041ca32014-02-25 20:01:15 -070023
Junxiao Shi8c8d2182014-01-30 22:33:00 -070024 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
25 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
Junxiao Shic041ca32014-02-25 20:01:15 -070026
Junxiao Shi8c8d2182014-01-30 22:33:00 -070027 forwarder.addFace(face1);
28 forwarder.addFace(face2);
29
30 BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
31 BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
32 BOOST_CHECK_NE(face1->getId(), face2->getId());
33
34 forwarder.removeFace(face1);
35 forwarder.removeFace(face2);
36
37 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
38 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
39}
40
Junxiao Shi9b27bd22014-02-26 20:29:58 -070041class ForwarderTestFace : public DummyFace {
42public:
43 virtual void
44 afterSend()
45 {
46 getGlobalIoService().stop();
47 }
48};
49
Junxiao Shi8c8d2182014-01-30 22:33:00 -070050BOOST_AUTO_TEST_CASE(SimpleExchange)
51{
Junxiao Shic041ca32014-02-25 20:01:15 -070052 resetGlobalIoService();
Junxiao Shic041ca32014-02-25 20:01:15 -070053 Forwarder forwarder;
54
Junxiao Shi8c8d2182014-01-30 22:33:00 -070055 Name nameA ("ndn:/A");
56 Name nameAB ("ndn:/A/B");
57 Name nameABC("ndn:/A/B/C");
58 Interest interestAB(nameAB);
59 interestAB.setInterestLifetime(4000);
60 Data dataABC(nameABC);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070061
Junxiao Shi9b27bd22014-02-26 20:29:58 -070062 shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>();
63 shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070064 forwarder.addFace(face1);
65 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070066
Junxiao Shi8c8d2182014-01-30 22:33:00 -070067 Fib& fib = forwarder.getFib();
68 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
69 fib.insert(Name("ndn:/A"));
70 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
71 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070072
Junxiao Shi8c8d2182014-01-30 22:33:00 -070073 face1->receiveInterest(interestAB);
Junxiao Shi9b27bd22014-02-26 20:29:58 -070074 getGlobalIoService().run();
75 getGlobalIoService().reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070076 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
77 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -070078 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070079
Junxiao Shi8c8d2182014-01-30 22:33:00 -070080 face2->receiveData(dataABC);
Junxiao Shi9b27bd22014-02-26 20:29:58 -070081 getGlobalIoService().run();
82 getGlobalIoService().reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070083 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
84 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -070085 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -070086}
87
Junxiao Shi9b27bd22014-02-26 20:29:58 -070088class ScopeLocalhostIncomingTestForwarder : public Forwarder
Junxiao Shi88884492014-02-15 15:57:43 -070089{
90public:
Junxiao Shi9b27bd22014-02-26 20:29:58 -070091 ScopeLocalhostIncomingTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -070092 {
93 }
94
95 virtual void
96 onDataUnsolicited(Face& inFace, const Data& data)
97 {
98 ++m_onDataUnsolicited_count;
99 }
100
101protected:
102 virtual void
103 dispatchToStrategy(const Face& inFace,
104 const Interest& interest,
105 shared_ptr<fib::Entry> fibEntry,
106 shared_ptr<pit::Entry> pitEntry)
107 {
108 ++m_dispatchToStrategy_count;
109 }
110
111public:
112 int m_dispatchToStrategy_count;
113 int m_onDataUnsolicited_count;
114};
115
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700116BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -0700117{
Junxiao Shic041ca32014-02-25 20:01:15 -0700118 resetGlobalIoService();
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700119 ScopeLocalhostIncomingTestForwarder forwarder;
120 shared_ptr<Face> face1 = make_shared<DummyLocalFace>();
121 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -0700122 forwarder.addFace(face1);
123 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -0700124
Junxiao Shi88884492014-02-15 15:57:43 -0700125 // local face, /localhost: OK
126 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700127 forwarder.onIncomingInterest(*face1, Interest("/localhost/A1"));
Junxiao Shi88884492014-02-15 15:57:43 -0700128 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700129
Junxiao Shi88884492014-02-15 15:57:43 -0700130 // non-local face, /localhost: violate
131 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700132 forwarder.onIncomingInterest(*face2, Interest("/localhost/A2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700133 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700134
Junxiao Shi88884492014-02-15 15:57:43 -0700135 // local face, non-/localhost: OK
136 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700137 forwarder.onIncomingInterest(*face1, Interest("/A3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700138 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700139
Junxiao Shi88884492014-02-15 15:57:43 -0700140 // non-local face, non-/localhost: OK
141 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700142 forwarder.onIncomingInterest(*face2, Interest("/A4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700143 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700144
Junxiao Shi88884492014-02-15 15:57:43 -0700145 // local face, /localhost: OK
146 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700147 forwarder.onIncomingData(*face1, Data("/localhost/B1"));
Junxiao Shi88884492014-02-15 15:57:43 -0700148 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
149
150 // non-local face, /localhost: OK
151 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700152 forwarder.onIncomingData(*face2, Data("/localhost/B2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700153 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700154
Junxiao Shi88884492014-02-15 15:57:43 -0700155 // local face, non-/localhost: OK
156 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700157 forwarder.onIncomingData(*face1, Data("/B3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700158 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
159
160 // non-local face, non-/localhost: OK
161 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700162 forwarder.onIncomingData(*face2, Data("/B4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700163 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
164}
165
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700166BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing)
167{
168 resetGlobalIoService();
169 Forwarder forwarder;
170 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
171 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
172 shared_ptr<Face> face3 = make_shared<DummyLocalFace>();
173 forwarder.addFace(face1);
174 forwarder.addFace(face2);
175 forwarder.addFace(face3);
176 Pit& pit = forwarder.getPit();
177
178 // local face, /localhost: OK
179 Interest interestA1 = Interest("/localhost/A1");
180 shared_ptr<pit::Entry> pitA1 = pit.insert(interestA1).first;
181 pitA1->insertOrUpdateInRecord(face3, interestA1);
182 face1->m_sentInterests.clear();
183 forwarder.onOutgoingInterest(pitA1, *face1);
184 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
185
186 // non-local face, /localhost: violate
187 Interest interestA2 = Interest("/localhost/A2");
188 shared_ptr<pit::Entry> pitA2 = pit.insert(interestA2).first;
189 pitA2->insertOrUpdateInRecord(face3, interestA1);
190 face2->m_sentInterests.clear();
191 forwarder.onOutgoingInterest(pitA2, *face2);
192 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0);
193
194 // local face, non-/localhost: OK
195 Interest interestA3 = Interest("/A3");
196 shared_ptr<pit::Entry> pitA3 = pit.insert(interestA3).first;
197 pitA3->insertOrUpdateInRecord(face3, interestA3);
198 face1->m_sentInterests.clear();
199 forwarder.onOutgoingInterest(pitA3, *face1);
200 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
201
202 // non-local face, non-/localhost: OK
203 Interest interestA4 = Interest("/A4");
204 shared_ptr<pit::Entry> pitA4 = pit.insert(interestA4).first;
205 pitA4->insertOrUpdateInRecord(face3, interestA4);
206 face2->m_sentInterests.clear();
207 forwarder.onOutgoingInterest(pitA4, *face2);
208 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
209
210 // local face, /localhost: OK
211 face1->m_sentDatas.clear();
212 forwarder.onOutgoingData(Data("/localhost/B1"), *face1);
213 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
214
215 // non-local face, /localhost: OK
216 face2->m_sentDatas.clear();
217 forwarder.onOutgoingData(Data("/localhost/B2"), *face2);
218 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0);
219
220 // local face, non-/localhost: OK
221 face1->m_sentDatas.clear();
222 forwarder.onOutgoingData(Data("/B3"), *face1);
223 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
224
225 // non-local face, non-/localhost: OK
226 face2->m_sentDatas.clear();
227 forwarder.onOutgoingData(Data("/B4"), *face2);
228 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1);
229}
230
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700231BOOST_AUTO_TEST_SUITE_END()
232
233} // namespace nfd