blob: 0a308523e04c953191a2540e6334d264ad06158e [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 Shi8c8d2182014-01-30 22:33:00 -07009
Junxiao Shid9ee45c2014-02-27 15:38:11 -070010#include "tests/test-common.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070011
12namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070013namespace tests {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070014
Junxiao Shid9ee45c2014-02-27 15:38:11 -070015BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070016
17BOOST_AUTO_TEST_CASE(AddRemoveFace)
18{
Junxiao Shic041ca32014-02-25 20:01:15 -070019 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
41BOOST_AUTO_TEST_CASE(SimpleExchange)
42{
Junxiao Shic041ca32014-02-25 20:01:15 -070043 Forwarder forwarder;
44
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045 Name nameA ("ndn:/A");
46 Name nameAB ("ndn:/A/B");
47 Name nameABC("ndn:/A/B/C");
48 Interest interestAB(nameAB);
49 interestAB.setInterestLifetime(4000);
50 Data dataABC(nameABC);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070051
Junxiao Shid9ee45c2014-02-27 15:38:11 -070052 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
53 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
54 face1->afterSend += bind(&boost::asio::io_service::stop, &g_io);
55 face2->afterSend += bind(&boost::asio::io_service::stop, &g_io);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070056 forwarder.addFace(face1);
57 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070058
Junxiao Shi8c8d2182014-01-30 22:33:00 -070059 Fib& fib = forwarder.getFib();
60 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
61 fib.insert(Name("ndn:/A"));
62 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
63 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070064
Junxiao Shi8c8d2182014-01-30 22:33:00 -070065 face1->receiveInterest(interestAB);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070066 g_io.run();
67 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070068 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
69 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -070070 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070071
Junxiao Shi8c8d2182014-01-30 22:33:00 -070072 face2->receiveData(dataABC);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070073 g_io.run();
74 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070075 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
76 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -070077 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -070078}
79
Junxiao Shi9b27bd22014-02-26 20:29:58 -070080class ScopeLocalhostIncomingTestForwarder : public Forwarder
Junxiao Shi88884492014-02-15 15:57:43 -070081{
82public:
Junxiao Shi9b27bd22014-02-26 20:29:58 -070083 ScopeLocalhostIncomingTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -070084 {
85 }
86
87 virtual void
88 onDataUnsolicited(Face& inFace, const Data& data)
89 {
90 ++m_onDataUnsolicited_count;
91 }
92
93protected:
94 virtual void
95 dispatchToStrategy(const Face& inFace,
96 const Interest& interest,
97 shared_ptr<fib::Entry> fibEntry,
98 shared_ptr<pit::Entry> pitEntry)
99 {
100 ++m_dispatchToStrategy_count;
101 }
102
103public:
104 int m_dispatchToStrategy_count;
105 int m_onDataUnsolicited_count;
106};
107
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700108BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -0700109{
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700110 ScopeLocalhostIncomingTestForwarder forwarder;
111 shared_ptr<Face> face1 = make_shared<DummyLocalFace>();
112 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -0700113 forwarder.addFace(face1);
114 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -0700115
Junxiao Shi88884492014-02-15 15:57:43 -0700116 // local face, /localhost: OK
117 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700118 forwarder.onIncomingInterest(*face1, Interest("/localhost/A1"));
Junxiao Shi88884492014-02-15 15:57:43 -0700119 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700120
Junxiao Shi88884492014-02-15 15:57:43 -0700121 // non-local face, /localhost: violate
122 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700123 forwarder.onIncomingInterest(*face2, Interest("/localhost/A2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700124 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shi88884492014-02-15 15:57:43 -0700126 // local face, non-/localhost: OK
127 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700128 forwarder.onIncomingInterest(*face1, Interest("/A3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700129 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700130
Junxiao Shi88884492014-02-15 15:57:43 -0700131 // non-local face, non-/localhost: OK
132 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700133 forwarder.onIncomingInterest(*face2, Interest("/A4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700134 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700135
Junxiao Shi88884492014-02-15 15:57:43 -0700136 // local face, /localhost: OK
137 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700138 forwarder.onIncomingData(*face1, Data("/localhost/B1"));
Junxiao Shi88884492014-02-15 15:57:43 -0700139 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
140
141 // non-local face, /localhost: OK
142 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700143 forwarder.onIncomingData(*face2, Data("/localhost/B2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700144 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700145
Junxiao Shi88884492014-02-15 15:57:43 -0700146 // local face, non-/localhost: OK
147 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700148 forwarder.onIncomingData(*face1, Data("/B3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700149 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
150
151 // non-local face, non-/localhost: OK
152 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700153 forwarder.onIncomingData(*face2, Data("/B4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700154 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
155}
156
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700157BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing)
158{
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700159 Forwarder forwarder;
160 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
161 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
162 shared_ptr<Face> face3 = make_shared<DummyLocalFace>();
163 forwarder.addFace(face1);
164 forwarder.addFace(face2);
165 forwarder.addFace(face3);
166 Pit& pit = forwarder.getPit();
167
168 // local face, /localhost: OK
169 Interest interestA1 = Interest("/localhost/A1");
170 shared_ptr<pit::Entry> pitA1 = pit.insert(interestA1).first;
171 pitA1->insertOrUpdateInRecord(face3, interestA1);
172 face1->m_sentInterests.clear();
173 forwarder.onOutgoingInterest(pitA1, *face1);
174 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
175
176 // non-local face, /localhost: violate
177 Interest interestA2 = Interest("/localhost/A2");
178 shared_ptr<pit::Entry> pitA2 = pit.insert(interestA2).first;
179 pitA2->insertOrUpdateInRecord(face3, interestA1);
180 face2->m_sentInterests.clear();
181 forwarder.onOutgoingInterest(pitA2, *face2);
182 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0);
183
184 // local face, non-/localhost: OK
185 Interest interestA3 = Interest("/A3");
186 shared_ptr<pit::Entry> pitA3 = pit.insert(interestA3).first;
187 pitA3->insertOrUpdateInRecord(face3, interestA3);
188 face1->m_sentInterests.clear();
189 forwarder.onOutgoingInterest(pitA3, *face1);
190 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
191
192 // non-local face, non-/localhost: OK
193 Interest interestA4 = Interest("/A4");
194 shared_ptr<pit::Entry> pitA4 = pit.insert(interestA4).first;
195 pitA4->insertOrUpdateInRecord(face3, interestA4);
196 face2->m_sentInterests.clear();
197 forwarder.onOutgoingInterest(pitA4, *face2);
198 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
199
200 // local face, /localhost: OK
201 face1->m_sentDatas.clear();
202 forwarder.onOutgoingData(Data("/localhost/B1"), *face1);
203 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
204
205 // non-local face, /localhost: OK
206 face2->m_sentDatas.clear();
207 forwarder.onOutgoingData(Data("/localhost/B2"), *face2);
208 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0);
209
210 // local face, non-/localhost: OK
211 face1->m_sentDatas.clear();
212 forwarder.onOutgoingData(Data("/B3"), *face1);
213 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
214
215 // non-local face, non-/localhost: OK
216 face2->m_sentDatas.clear();
217 forwarder.onOutgoingData(Data("/B4"), *face2);
218 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1);
219}
220
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700221BOOST_AUTO_TEST_SUITE_END()
222
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700223} // namespace tests
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700224} // namespace nfd