blob: ecd06acf11caa69cd3d4fa25b4950d9e6fd37838 [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"
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -08009#include <ndn-cpp-dev/security/key-chain.hpp>
Junxiao Shi8c8d2182014-01-30 22:33:00 -070010
Junxiao Shid9ee45c2014-02-27 15:38:11 -070011#include "tests/test-common.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070012
13namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070014namespace tests {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070015
Junxiao Shid9ee45c2014-02-27 15:38:11 -070016BOOST_FIXTURE_TEST_SUITE(FwForwarder, BaseFixture)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070017
Junxiao Shi8c8d2182014-01-30 22:33:00 -070018BOOST_AUTO_TEST_CASE(SimpleExchange)
19{
Junxiao Shic041ca32014-02-25 20:01:15 -070020 Forwarder forwarder;
21
Junxiao Shi8c8d2182014-01-30 22:33:00 -070022 Name nameA ("ndn:/A");
23 Name nameAB ("ndn:/A/B");
24 Name nameABC("ndn:/A/B/C");
25 Interest interestAB(nameAB);
26 interestAB.setInterestLifetime(4000);
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080027 shared_ptr<Data> dataABC = make_shared<Data>(nameABC);
28 ndn::SignatureSha256WithRsa fakeSignature;
29 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
30 dataABC->setSignature(fakeSignature);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070031
Junxiao Shid9ee45c2014-02-27 15:38:11 -070032 shared_ptr<DummyFace> face1 = make_shared<DummyFace>();
33 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
34 face1->afterSend += bind(&boost::asio::io_service::stop, &g_io);
35 face2->afterSend += bind(&boost::asio::io_service::stop, &g_io);
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036 forwarder.addFace(face1);
37 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070038
Junxiao Shi8c8d2182014-01-30 22:33:00 -070039 Fib& fib = forwarder.getFib();
40 std::pair<shared_ptr<fib::Entry>, bool> fibInsertResult =
41 fib.insert(Name("ndn:/A"));
42 shared_ptr<fib::Entry> fibEntry = fibInsertResult.first;
43 fibEntry->addNextHop(face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070044
Junxiao Shi8c8d2182014-01-30 22:33:00 -070045 face1->receiveInterest(interestAB);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070046 g_io.run();
47 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070048 BOOST_REQUIRE_EQUAL(face2->m_sentInterests.size(), 1);
49 BOOST_CHECK(face2->m_sentInterests[0].getName().equals(nameAB));
Junxiao Shi06887ac2014-02-13 20:15:42 -070050 BOOST_CHECK_EQUAL(face2->m_sentInterests[0].getIncomingFaceId(), face1->getId());
Junxiao Shic041ca32014-02-25 20:01:15 -070051
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080052 face2->receiveData(*dataABC);
Junxiao Shid9ee45c2014-02-27 15:38:11 -070053 g_io.run();
54 g_io.reset();
Junxiao Shi8c8d2182014-01-30 22:33:00 -070055 BOOST_REQUIRE_EQUAL(face1->m_sentDatas.size(), 1);
56 BOOST_CHECK(face1->m_sentDatas[0].getName().equals(nameABC));
Junxiao Shi06887ac2014-02-13 20:15:42 -070057 BOOST_CHECK_EQUAL(face1->m_sentDatas[0].getIncomingFaceId(), face2->getId());
Junxiao Shi8c8d2182014-01-30 22:33:00 -070058}
59
Junxiao Shi9b27bd22014-02-26 20:29:58 -070060class ScopeLocalhostIncomingTestForwarder : public Forwarder
Junxiao Shi88884492014-02-15 15:57:43 -070061{
62public:
Junxiao Shi9b27bd22014-02-26 20:29:58 -070063 ScopeLocalhostIncomingTestForwarder()
Junxiao Shi88884492014-02-15 15:57:43 -070064 {
65 }
66
67 virtual void
68 onDataUnsolicited(Face& inFace, const Data& data)
69 {
70 ++m_onDataUnsolicited_count;
71 }
72
73protected:
74 virtual void
75 dispatchToStrategy(const Face& inFace,
76 const Interest& interest,
77 shared_ptr<fib::Entry> fibEntry,
78 shared_ptr<pit::Entry> pitEntry)
79 {
80 ++m_dispatchToStrategy_count;
81 }
82
83public:
84 int m_dispatchToStrategy_count;
85 int m_onDataUnsolicited_count;
86};
87
Junxiao Shi9b27bd22014-02-26 20:29:58 -070088BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -070089{
Junxiao Shi9b27bd22014-02-26 20:29:58 -070090 ScopeLocalhostIncomingTestForwarder forwarder;
91 shared_ptr<Face> face1 = make_shared<DummyLocalFace>();
92 shared_ptr<Face> face2 = make_shared<DummyFace>();
Junxiao Shi88884492014-02-15 15:57:43 -070093 forwarder.addFace(face1);
94 forwarder.addFace(face2);
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -080096 shared_ptr<Data> d1, d2, d3, d4;
97 shared_ptr<Interest> i1, i2, i3, i4;
98
99 ndn::SignatureSha256WithRsa fakeSignature;
100 fakeSignature.setValue(ndn::dataBlock(tlv::SignatureValue, reinterpret_cast<const uint8_t*>(0), 0));
101
Junxiao Shi88884492014-02-15 15:57:43 -0700102 // local face, /localhost: OK
103 forwarder.m_dispatchToStrategy_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800104 i1 = make_shared<Interest>(Name("/localhost/A1"));
105 forwarder.onIncomingInterest(*face1, *i1);
Junxiao Shi88884492014-02-15 15:57:43 -0700106 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700107
Junxiao Shi88884492014-02-15 15:57:43 -0700108 // non-local face, /localhost: violate
109 forwarder.m_dispatchToStrategy_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800110 i2 = make_shared<Interest>(Name("/localhost/A2"));
111 forwarder.onIncomingInterest(*face2, *i2);
Junxiao Shi88884492014-02-15 15:57:43 -0700112 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700113
Junxiao Shi88884492014-02-15 15:57:43 -0700114 // local face, non-/localhost: OK
115 forwarder.m_dispatchToStrategy_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800116 i3 = make_shared<Interest>(Name("/A3"));
117 forwarder.onIncomingInterest(*face1, *i3);
Junxiao Shi88884492014-02-15 15:57:43 -0700118 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700119
Junxiao Shi88884492014-02-15 15:57:43 -0700120 // non-local face, non-/localhost: OK
121 forwarder.m_dispatchToStrategy_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800122 i4 = make_shared<Interest>(Name("/A4"));
123 forwarder.onIncomingInterest(*face2, *i4);
Junxiao Shi88884492014-02-15 15:57:43 -0700124 BOOST_CHECK_EQUAL(forwarder.m_dispatchToStrategy_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700125
Junxiao Shi88884492014-02-15 15:57:43 -0700126 // local face, /localhost: OK
127 forwarder.m_onDataUnsolicited_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800128 d1 = make_shared<Data>(Name("/localhost/B1"));
129 d1->setSignature(fakeSignature);
130 forwarder.onIncomingData(*face1, *d1);
Junxiao Shi88884492014-02-15 15:57:43 -0700131 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
132
133 // non-local face, /localhost: OK
134 forwarder.m_onDataUnsolicited_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800135 d2 = make_shared<Data>(Name("/localhost/B2"));
136 d2->setSignature(fakeSignature);
137 forwarder.onIncomingData(*face2, *d2);
Junxiao Shi88884492014-02-15 15:57:43 -0700138 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -0700139
Junxiao Shi88884492014-02-15 15:57:43 -0700140 // local face, non-/localhost: OK
141 forwarder.m_onDataUnsolicited_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800142 d3 = make_shared<Data>(Name("/B3"));
143 d3->setSignature(fakeSignature);
144 forwarder.onIncomingData(*face1, *d3);
Junxiao Shi88884492014-02-15 15:57:43 -0700145 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
146
147 // non-local face, non-/localhost: OK
148 forwarder.m_onDataUnsolicited_count = 0;
Ilya Moiseenko76cf77a2014-03-05 14:35:51 -0800149 d4 = make_shared<Data>(Name("/B4"));
150 d4->setSignature(fakeSignature);
151 forwarder.onIncomingData(*face2, *d4);
Junxiao Shi88884492014-02-15 15:57:43 -0700152 BOOST_CHECK_EQUAL(forwarder.m_onDataUnsolicited_count, 1);
153}
154
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700155BOOST_AUTO_TEST_CASE(ScopeLocalhostOutgoing)
156{
Junxiao Shi9b27bd22014-02-26 20:29:58 -0700157 Forwarder forwarder;
158 shared_ptr<DummyLocalFace> face1 = make_shared<DummyLocalFace>();
159 shared_ptr<DummyFace> face2 = make_shared<DummyFace>();
160 shared_ptr<Face> face3 = make_shared<DummyLocalFace>();
161 forwarder.addFace(face1);
162 forwarder.addFace(face2);
163 forwarder.addFace(face3);
164 Pit& pit = forwarder.getPit();
165
166 // local face, /localhost: OK
167 Interest interestA1 = Interest("/localhost/A1");
168 shared_ptr<pit::Entry> pitA1 = pit.insert(interestA1).first;
169 pitA1->insertOrUpdateInRecord(face3, interestA1);
170 face1->m_sentInterests.clear();
171 forwarder.onOutgoingInterest(pitA1, *face1);
172 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
173
174 // non-local face, /localhost: violate
175 Interest interestA2 = Interest("/localhost/A2");
176 shared_ptr<pit::Entry> pitA2 = pit.insert(interestA2).first;
177 pitA2->insertOrUpdateInRecord(face3, interestA1);
178 face2->m_sentInterests.clear();
179 forwarder.onOutgoingInterest(pitA2, *face2);
180 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 0);
181
182 // local face, non-/localhost: OK
183 Interest interestA3 = Interest("/A3");
184 shared_ptr<pit::Entry> pitA3 = pit.insert(interestA3).first;
185 pitA3->insertOrUpdateInRecord(face3, interestA3);
186 face1->m_sentInterests.clear();
187 forwarder.onOutgoingInterest(pitA3, *face1);
188 BOOST_CHECK_EQUAL(face1->m_sentInterests.size(), 1);
189
190 // non-local face, non-/localhost: OK
191 Interest interestA4 = Interest("/A4");
192 shared_ptr<pit::Entry> pitA4 = pit.insert(interestA4).first;
193 pitA4->insertOrUpdateInRecord(face3, interestA4);
194 face2->m_sentInterests.clear();
195 forwarder.onOutgoingInterest(pitA4, *face2);
196 BOOST_CHECK_EQUAL(face2->m_sentInterests.size(), 1);
197
198 // local face, /localhost: OK
199 face1->m_sentDatas.clear();
200 forwarder.onOutgoingData(Data("/localhost/B1"), *face1);
201 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
202
203 // non-local face, /localhost: OK
204 face2->m_sentDatas.clear();
205 forwarder.onOutgoingData(Data("/localhost/B2"), *face2);
206 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 0);
207
208 // local face, non-/localhost: OK
209 face1->m_sentDatas.clear();
210 forwarder.onOutgoingData(Data("/B3"), *face1);
211 BOOST_CHECK_EQUAL(face1->m_sentDatas.size(), 1);
212
213 // non-local face, non-/localhost: OK
214 face2->m_sentDatas.clear();
215 forwarder.onOutgoingData(Data("/B4"), *face2);
216 BOOST_CHECK_EQUAL(face2->m_sentDatas.size(), 1);
217}
218
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700219BOOST_AUTO_TEST_SUITE_END()
220
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700221} // namespace tests
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700222} // namespace nfd