blob: ed8f6a732b93035f99b21a3de5eb89f23f8622bd [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
14class ForwarderTestFace : public Face {
15public:
Junxiao Shi88884492014-02-15 15:57:43 -070016 explicit
Junxiao Shi8c8d2182014-01-30 22:33:00 -070017 ForwarderTestFace(boost::asio::io_service& ioService)
18 : m_ioService(ioService)
19 {
20 }
21
22 virtual void
23 sendInterest(const Interest& interest)
24 {
25 m_sentInterests.push_back(interest);
26 m_ioService.stop();
27 }
28
29 virtual void
30 sendData(const Data& data)
31 {
32 m_sentDatas.push_back(data);
33 m_ioService.stop();
34 }
35
Alexander Afanasyeva0a10fb2014-02-13 19:56:15 -080036 virtual void
37 close()
38 {
39 }
40
Alexander Afanasyev93ce75e2014-02-18 19:45:34 -080041 virtual bool
42 isLocal() const
43 {
44 return false;
45 }
46
Junxiao Shi8c8d2182014-01-30 22:33:00 -070047 void
48 receiveInterest(const Interest& interest)
49 {
50 onReceiveInterest(interest);
51 }
52
53 void
54 receiveData(const Data& data)
55 {
56 onReceiveData(data);
57 }
58
59public:
60 std::vector<Interest> m_sentInterests;
61 std::vector<Data> m_sentDatas;
62
63private:
64 boost::asio::io_service& m_ioService;
65};
66
67BOOST_AUTO_TEST_SUITE(FwForwarder)
68
69BOOST_AUTO_TEST_CASE(AddRemoveFace)
70{
71 boost::asio::io_service io;
72 Forwarder forwarder(io);
73
74 shared_ptr<Face> face1 = make_shared<DummyFace>();
75 shared_ptr<Face> face2 = make_shared<DummyFace>();
76
77 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
78 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
79
80 forwarder.addFace(face1);
81 forwarder.addFace(face2);
82
83 BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
84 BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
85 BOOST_CHECK_NE(face1->getId(), face2->getId());
86
87 forwarder.removeFace(face1);
88 forwarder.removeFace(face2);
89
90 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
91 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
92}
93
94BOOST_AUTO_TEST_CASE(SimpleExchange)
95{
96 Name nameA ("ndn:/A");
97 Name nameAB ("ndn:/A/B");
98 Name nameABC("ndn:/A/B/C");
99 Interest interestAB(nameAB);
100 interestAB.setInterestLifetime(4000);
101 Data dataABC(nameABC);
102
103 boost::asio::io_service io;
104 Forwarder forwarder(io);
105
106 shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io));
107 shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io));
108 forwarder.addFace(face1);
109 forwarder.addFace(face2);
110
111 Fib& fib = forwarder.getFib();
112 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
113 fib.insert(Name("ndn:/A"));
114 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
115 fibEntry->addNextHop(face2, 0);
116
117 face1->receiveInterest(interestAB);
118 io.run();
119 io.reset();
120 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
121 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700122 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700123
124 face2->receiveData(dataABC);
125 io.run();
126 io.reset();
127 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
128 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700129 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700130}
131
Junxiao Shi88884492014-02-15 15:57:43 -0700132
133class ForwarderTestLocalFace : public DummyFace {
134public:
135 explicit
136 ForwarderTestLocalFace(bool isLocal)
137 : m_isLocal(isLocal)
138 {
139 }
140
141 virtual bool
142 isLocal() const
143 {
144 return m_isLocal;
145 }
146
147private:
148 bool m_isLocal;
149};
150
151class ScopeLocalhostTestForwarder : public Forwarder
152{
153public:
154 explicit
155 ScopeLocalhostTestForwarder(boost::asio::io_service& ioService)
156 : Forwarder(ioService)
157 {
158 }
159
160 virtual void
161 onDataUnsolicited(Face& inFace, const Data& data)
162 {
163 ++m_onDataUnsolicited_count;
164 }
165
166protected:
167 virtual void
168 dispatchToStrategy(const Face& inFace,
169 const Interest& interest,
170 shared_ptr<fib::Entry> fibEntry,
171 shared_ptr<pit::Entry> pitEntry)
172 {
173 ++m_dispatchToStrategy_count;
174 }
175
176public:
177 int m_dispatchToStrategy_count;
178 int m_onDataUnsolicited_count;
179};
180
181BOOST_AUTO_TEST_CASE(ScopeLocalhost)
182{
183 boost::asio::io_service io;
184 ScopeLocalhostTestForwarder forwarder(io);
185 shared_ptr<ForwarderTestLocalFace> face1 = make_shared<ForwarderTestLocalFace>(true);
186 shared_ptr<ForwarderTestLocalFace> face2 = make_shared<ForwarderTestLocalFace>(false);
187 forwarder.addFace(face1);
188 forwarder.addFace(face2);
189
190 // local face, /localhost: OK
191 forwarder.m_dispatchToStrategy_count = 0;
192 forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1")));
193 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
194
195 // non-local face, /localhost: violate
196 forwarder.m_dispatchToStrategy_count = 0;
197 forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2")));
198 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
199
200 // local face, non-/localhost: OK
201 forwarder.m_dispatchToStrategy_count = 0;
202 forwarder.onIncomingInterest(*face1, Interest(Name("/A3")));
203 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
204
205 // non-local face, non-/localhost: OK
206 forwarder.m_dispatchToStrategy_count = 0;
207 forwarder.onIncomingInterest(*face2, Interest(Name("/A4")));
208 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
209
210 // local face, /localhost: OK
211 forwarder.m_onDataUnsolicited_count = 0;
212 forwarder.onIncomingData(*face1, Data(Name("/localhost/B1")));
213 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
214
215 // non-local face, /localhost: OK
216 forwarder.m_onDataUnsolicited_count = 0;
217 forwarder.onIncomingData(*face2, Data(Name("/localhost/B2")));
218 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
219
220 // local face, non-/localhost: OK
221 forwarder.m_onDataUnsolicited_count = 0;
222 forwarder.onIncomingData(*face1, Data(Name("/B3")));
223 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
224
225 // non-local face, non-/localhost: OK
226 forwarder.m_onDataUnsolicited_count = 0;
227 forwarder.onIncomingData(*face2, Data(Name("/B4")));
228 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
229}
230
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700231BOOST_AUTO_TEST_SUITE_END()
232
233} // namespace nfd