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