blob: b9e460cb46c336f665384056bef960696123180e [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
Junxiao Shi8c8d2182014-01-30 22:33:00 -070017BOOST_AUTO_TEST_CASE(SimpleExchange)
18{
Junxiao Shic041ca32014-02-25 20:01:15 -070019 Forwarder forwarder;
20
Junxiao Shi8c8d2182014-01-30 22:33:00 -070021 Name nameA ("ndn:/A");
22 Name nameAB ("ndn:/A/B");
23 Name nameABC("ndn:/A/B/C");
24 Interest interestAB(nameAB);
25 interestAB.setInterestLifetime(4000);
26 Data dataABC(nameABC);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070027
Junxiao Shid9ee45c2014-02-27 15:38:11 -070028 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
29 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
30 face1->afterSend += bind(&boost::asio::io_service::stop, &g_io);
31 face2->afterSend += bind(&boost::asio::io_service::stop, &g_io);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070032 forwarder.addFace(face1);
33 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070034
Junxiao Shi8c8d2182014-01-30 22:33:00 -070035 Fib& fib = forwarder.getFib();
36 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
37 fib.insert(Name("ndn:/A"));
38 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
39 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070040
Junxiao Shi8c8d2182014-01-30 22:33:00 -070041 face1->receiveInterest(interestAB);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070042 g_io.run();
43 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070044 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
45 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -070046 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070047
Junxiao Shi8c8d2182014-01-30 22:33:00 -070048 face2->receiveData(dataABC);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070049 g_io.run();
50 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070051 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
52 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -070053 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -070054}
55
Junxiao Shi9b27bd22014-02-26 20:29:58 -070056class ScopeLocalhostIncomingTestForwarder : public Forwarder
Junxiao Shi88884492014-02-15 15:57:43 -070057{
58public:
Junxiao Shi9b27bd22014-02-26 20:29:58 -070059 ScopeLocalhostIncomingTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -070060 {
61 }
62
63 virtual void
64 onDataUnsolicited(Face& inFace, const Data& data)
65 {
66 ++m_onDataUnsolicited_count;
67 }
68
69protected:
70 virtual void
71 dispatchToStrategy(const Face& inFace,
72 const Interest& interest,
73 shared_ptr<fib::Entry> fibEntry,
74 shared_ptr<pit::Entry> pitEntry)
75 {
76 ++m_dispatchToStrategy_count;
77 }
78
79public:
80 int m_dispatchToStrategy_count;
81 int m_onDataUnsolicited_count;
82};
83
Junxiao Shi9b27bd22014-02-26 20:29:58 -070084BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -070085{
Junxiao Shi9b27bd22014-02-26 20:29:58 -070086 ScopeLocalhostIncomingTestForwarder forwarder;
87 shared_ptr<Face> face1 = make_shared<DummyLocalFace>();
88 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -070089 forwarder.addFace(face1);
90 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070091
Junxiao Shi88884492014-02-15 15:57:43 -070092 // local face, /localhost: OK
93 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -070094 forwarder.onIncomingInterest(*face1, Interest("/localhost/A1"));
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 Shi9b27bd22014-02-26 20:29:58 -070099 forwarder.onIncomingInterest(*face2, Interest("/localhost/A2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700100 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700101
Junxiao Shi88884492014-02-15 15:57:43 -0700102 // local face, non-/localhost: OK
103 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700104 forwarder.onIncomingInterest(*face1, Interest("/A3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700105 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700106
Junxiao Shi88884492014-02-15 15:57:43 -0700107 // non-local face, non-/localhost: OK
108 forwarder.m_dispatchToStrategy_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700109 forwarder.onIncomingInterest(*face2, Interest("/A4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700110 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700111
Junxiao Shi88884492014-02-15 15:57:43 -0700112 // local face, /localhost: OK
113 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700114 forwarder.onIncomingData(*face1, Data("/localhost/B1"));
Junxiao Shi88884492014-02-15 15:57:43 -0700115 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
116
117 // non-local face, /localhost: OK
118 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700119 forwarder.onIncomingData(*face2, Data("/localhost/B2"));
Junxiao Shi88884492014-02-15 15:57:43 -0700120 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700121
Junxiao Shi88884492014-02-15 15:57:43 -0700122 // local face, non-/localhost: OK
123 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700124 forwarder.onIncomingData(*face1, Data("/B3"));
Junxiao Shi88884492014-02-15 15:57:43 -0700125 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
126
127 // non-local face, non-/localhost: OK
128 forwarder.m_onDataUnsolicited_count = 0;
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700129 forwarder.onIncomingData(*face2, Data("/B4"));
Junxiao Shi88884492014-02-15 15:57:43 -0700130 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
131}
132
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700133BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing)
134{
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700135 Forwarder forwarder;
136 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
137 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
138 shared_ptr<Face> face3 = make_shared<DummyLocalFace>();
139 forwarder.addFace(face1);
140 forwarder.addFace(face2);
141 forwarder.addFace(face3);
142 Pit& pit = forwarder.getPit();
143
144 // local face, /localhost: OK
145 Interest interestA1 = Interest("/localhost/A1");
146 shared_ptr<pit::Entry> pitA1 = pit.insert(interestA1).first;
147 pitA1->insertOrUpdateInRecord(face3, interestA1);
148 face1->m_sentInterests.clear();
149 forwarder.onOutgoingInterest(pitA1, *face1);
150 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
151
152 // non-local face, /localhost: violate
153 Interest interestA2 = Interest("/localhost/A2");
154 shared_ptr<pit::Entry> pitA2 = pit.insert(interestA2).first;
155 pitA2->insertOrUpdateInRecord(face3, interestA1);
156 face2->m_sentInterests.clear();
157 forwarder.onOutgoingInterest(pitA2, *face2);
158 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0);
159
160 // local face, non-/localhost: OK
161 Interest interestA3 = Interest("/A3");
162 shared_ptr<pit::Entry> pitA3 = pit.insert(interestA3).first;
163 pitA3->insertOrUpdateInRecord(face3, interestA3);
164 face1->m_sentInterests.clear();
165 forwarder.onOutgoingInterest(pitA3, *face1);
166 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
167
168 // non-local face, non-/localhost: OK
169 Interest interestA4 = Interest("/A4");
170 shared_ptr<pit::Entry> pitA4 = pit.insert(interestA4).first;
171 pitA4->insertOrUpdateInRecord(face3, interestA4);
172 face2->m_sentInterests.clear();
173 forwarder.onOutgoingInterest(pitA4, *face2);
174 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
175
176 // local face, /localhost: OK
177 face1->m_sentDatas.clear();
178 forwarder.onOutgoingData(Data("/localhost/B1"), *face1);
179 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
180
181 // non-local face, /localhost: OK
182 face2->m_sentDatas.clear();
183 forwarder.onOutgoingData(Data("/localhost/B2"), *face2);
184 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0);
185
186 // local face, non-/localhost: OK
187 face1->m_sentDatas.clear();
188 forwarder.onOutgoingData(Data("/B3"), *face1);
189 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
190
191 // non-local face, non-/localhost: OK
192 face2->m_sentDatas.clear();
193 forwarder.onOutgoingData(Data("/B4"), *face2);
194 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1);
195}
196
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700197BOOST_AUTO_TEST_SUITE_END()
198
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700199} // namespace tests
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700200} // namespace nfd