blob: e066f52b8f7d2138ef4110765b388dc17c4f7154 [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 -0700126class ScopeLocalhostTestForwarder : public Forwarder
127{
128public:
129 explicit
130 ScopeLocalhostTestForwarder(boost::asio::io_service& ioService)
131 : Forwarder(ioService)
132 {
133 }
134
135 virtual void
136 onDataUnsolicited(Face& inFace, const Data& data)
137 {
138 ++m_onDataUnsolicited_count;
139 }
140
141protected:
142 virtual void
143 dispatchToStrategy(const Face& inFace,
144 const Interest& interest,
145 shared_ptr<fib::Entry> fibEntry,
146 shared_ptr<pit::Entry> pitEntry)
147 {
148 ++m_dispatchToStrategy_count;
149 }
150
151public:
152 int m_dispatchToStrategy_count;
153 int m_onDataUnsolicited_count;
154};
155
156BOOST_AUTO_TEST_CASE(ScopeLocalhost)
157{
158 boost::asio::io_service io;
159 ScopeLocalhostTestForwarder forwarder(io);
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800160 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
161 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -0700162 forwarder.addFace(face1);
163 forwarder.addFace(face2);
164
165 // local face, /localhost: OK
166 forwarder.m_dispatchToStrategy_count = 0;
167 forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1")));
168 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
169
170 // non-local face, /localhost: violate
171 forwarder.m_dispatchToStrategy_count = 0;
172 forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2")));
173 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
174
175 // local face, non-/localhost: OK
176 forwarder.m_dispatchToStrategy_count = 0;
177 forwarder.onIncomingInterest(*face1, Interest(Name("/A3")));
178 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
179
180 // non-local face, non-/localhost: OK
181 forwarder.m_dispatchToStrategy_count = 0;
182 forwarder.onIncomingInterest(*face2, Interest(Name("/A4")));
183 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
184
185 // local face, /localhost: OK
186 forwarder.m_onDataUnsolicited_count = 0;
187 forwarder.onIncomingData(*face1, Data(Name("/localhost/B1")));
188 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
189
190 // non-local face, /localhost: OK
191 forwarder.m_onDataUnsolicited_count = 0;
192 forwarder.onIncomingData(*face2, Data(Name("/localhost/B2")));
193 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
194
195 // local face, non-/localhost: OK
196 forwarder.m_onDataUnsolicited_count = 0;
197 forwarder.onIncomingData(*face1, Data(Name("/B3")));
198 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
199
200 // non-local face, non-/localhost: OK
201 forwarder.m_onDataUnsolicited_count = 0;
202 forwarder.onIncomingData(*face2, Data(Name("/B4")));
203 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
204}
205
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700206BOOST_AUTO_TEST_SUITE_END()
207
208} // namespace nfd