blob: daa80c7ba0cf07c7ea82f45259ce3b3cfd6cbe32 [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 }
Junxiao Shic041ca32014-02-25 20:01:15 -070021
Junxiao Shi8c8d2182014-01-30 22:33:00 -070022 virtual void
23 sendInterest(const Interest& interest)
24 {
25 m_sentInterests.push_back(interest);
26 m_ioService.stop();
27 }
Junxiao Shic041ca32014-02-25 20:01:15 -070028
Junxiao Shi8c8d2182014-01-30 22:33:00 -070029 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 }
Junxiao Shic041ca32014-02-25 20:01:15 -070046
Junxiao Shi8c8d2182014-01-30 22:33:00 -070047 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{
Junxiao Shic041ca32014-02-25 20:01:15 -070065 resetGlobalIoService();
66 Forwarder forwarder;
67
Junxiao Shi8c8d2182014-01-30 22:33:00 -070068 shared_ptr<Face> face1 = make_shared<DummyFace>();
69 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shic041ca32014-02-25 20:01:15 -070070
Junxiao Shi8c8d2182014-01-30 22:33:00 -070071 BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
72 BOOST_CHECK_EQUAL(face2->getId(), INVALID_FACEID);
Junxiao Shic041ca32014-02-25 20:01:15 -070073
Junxiao Shi8c8d2182014-01-30 22:33:00 -070074 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{
Junxiao Shic041ca32014-02-25 20:01:15 -070090 resetGlobalIoService();
91 boost::asio::io_service& io = getGlobalIoService();
92 Forwarder forwarder;
93
Junxiao Shi8c8d2182014-01-30 22:33:00 -070094 Name nameA ("ndn:/A");
95 Name nameAB ("ndn:/A/B");
96 Name nameABC("ndn:/A/B/C");
97 Interest interestAB(nameAB);
98 interestAB.setInterestLifetime(4000);
99 Data dataABC(nameABC);
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700100
101 shared_ptr<ForwarderTestFace> face1 = make_shared<ForwarderTestFace>(boost::ref(io));
102 shared_ptr<ForwarderTestFace> face2 = make_shared<ForwarderTestFace>(boost::ref(io));
103 forwarder.addFace(face1);
104 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -0700105
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700106 Fib& fib = forwarder.getFib();
107 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
108 fib.insert(Name("ndn:/A"));
109 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
110 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700111
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700112 face1->receiveInterest(interestAB);
113 io.run();
114 io.reset();
115 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
116 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700117 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -0700118
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700119 face2->receiveData(dataABC);
120 io.run();
121 io.reset();
122 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
123 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -0700124 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700125}
126
Junxiao Shi88884492014-02-15 15:57:43 -0700127class ScopeLocalhostTestForwarder : public Forwarder
128{
129public:
Junxiao Shic041ca32014-02-25 20:01:15 -0700130 ScopeLocalhostTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -0700131 {
132 }
133
134 virtual void
135 onDataUnsolicited(Face& inFace, const Data& data)
136 {
137 ++m_onDataUnsolicited_count;
138 }
139
140protected:
141 virtual void
142 dispatchToStrategy(const Face& inFace,
143 const Interest& interest,
144 shared_ptr<fib::Entry> fibEntry,
145 shared_ptr<pit::Entry> pitEntry)
146 {
147 ++m_dispatchToStrategy_count;
148 }
149
150public:
151 int m_dispatchToStrategy_count;
152 int m_onDataUnsolicited_count;
153};
154
155BOOST_AUTO_TEST_CASE(ScopeLocalhost)
156{
Junxiao Shic041ca32014-02-25 20:01:15 -0700157 resetGlobalIoService();
158 ScopeLocalhostTestForwarder forwarder;
Alexander Afanasyevbd220a02014-02-20 00:29:56 -0800159 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
160 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -0700161 forwarder.addFace(face1);
162 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -0700163
Junxiao Shi88884492014-02-15 15:57:43 -0700164 // local face, /localhost: OK
165 forwarder.m_dispatchToStrategy_count = 0;
166 forwarder.onIncomingInterest(*face1, Interest(Name("/localhost/A1")));
167 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700168
Junxiao Shi88884492014-02-15 15:57:43 -0700169 // non-local face, /localhost: violate
170 forwarder.m_dispatchToStrategy_count = 0;
171 forwarder.onIncomingInterest(*face2, Interest(Name("/localhost/A2")));
172 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700173
Junxiao Shi88884492014-02-15 15:57:43 -0700174 // local face, non-/localhost: OK
175 forwarder.m_dispatchToStrategy_count = 0;
176 forwarder.onIncomingInterest(*face1, Interest(Name("/A3")));
177 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700178
Junxiao Shi88884492014-02-15 15:57:43 -0700179 // non-local face, non-/localhost: OK
180 forwarder.m_dispatchToStrategy_count = 0;
181 forwarder.onIncomingInterest(*face2, Interest(Name("/A4")));
182 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700183
Junxiao Shi88884492014-02-15 15:57:43 -0700184 // local face, /localhost: OK
185 forwarder.m_onDataUnsolicited_count = 0;
186 forwarder.onIncomingData(*face1, Data(Name("/localhost/B1")));
187 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
188
189 // non-local face, /localhost: OK
190 forwarder.m_onDataUnsolicited_count = 0;
191 forwarder.onIncomingData(*face2, Data(Name("/localhost/B2")));
192 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700193
Junxiao Shi88884492014-02-15 15:57:43 -0700194 // local face, non-/localhost: OK
195 forwarder.m_onDataUnsolicited_count = 0;
196 forwarder.onIncomingData(*face1, Data(Name("/B3")));
197 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
198
199 // non-local face, non-/localhost: OK
200 forwarder.m_onDataUnsolicited_count = 0;
201 forwarder.onIncomingData(*face2, Data(Name("/B4")));
202 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
203}
204
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700205BOOST_AUTO_TEST_SUITE_END()
206
207} // namespace nfd