blob: 59b6f0d65ebe86083d8b7fd14f5776d1db085cc2 [file] [log] [blame]
Junxiao Shi8c8d2182014-01-30 22:33:00 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Davide Pesaventob124b8e2024-02-16 16:54:26 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi82e7f582014-09-07 15:15:40 -070024 */
Junxiao Shi8c8d2182014-01-30 22:33:00 -070025
26#include "fw/forwarder.hpp"
Davide Pesavento2cae8ca2019-04-18 20:48:05 -040027#include "common/global.hpp"
Junxiao Shi8c8d2182014-01-30 22:33:00 -070028
Junxiao Shid9ee45c2014-02-27 15:38:11 -070029#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi06a1eab2017-09-04 13:13:02 +000031#include "tests/daemon/face/dummy-face.hpp"
32#include "choose-strategy.hpp"
33#include "dummy-strategy.hpp"
34
35#include <ndn-cxx/lp/tags.hpp>
Junxiao Shi8c8d2182014-01-30 22:33:00 -070036
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040037namespace nfd::tests {
Junxiao Shi8c8d2182014-01-30 22:33:00 -070038
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040039class ForwarderFixture : public GlobalIoTimeFixture
40{
41protected:
42 template<typename ...Args>
43 shared_ptr<DummyFace>
44 addFace(Args&&... args)
45 {
46 auto face = make_shared<DummyFace>(std::forward<Args>(args)...);
47 faceTable.add(face);
48 return face;
49 }
50
51protected:
52 FaceTable faceTable;
53 Forwarder forwarder{faceTable};
Davide Pesavento95afc6d2023-08-02 20:50:25 -040054 const ForwarderCounters& counters{forwarder.getCounters()};
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040055};
56
Junxiao Shi0355e9f2015-09-02 07:24:53 -070057BOOST_AUTO_TEST_SUITE(Fw)
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040058BOOST_FIXTURE_TEST_SUITE(TestForwarder, ForwarderFixture)
Junxiao Shi8c8d2182014-01-30 22:33:00 -070059
Junxiao Shi8c8d2182014-01-30 22:33:00 -070060BOOST_AUTO_TEST_CASE(SimpleExchange)
61{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -040062 auto face1 = addFace();
63 auto face2 = addFace();
Junxiao Shic041ca32014-02-25 20:01:15 -070064
Junxiao Shi8c8d2182014-01-30 22:33:00 -070065 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +000066 fib::Entry* entry = fib.insert("/A").first;
67 fib.addOrUpdateNextHop(*entry, *face2, 0);
Junxiao Shic041ca32014-02-25 20:01:15 -070068
Davide Pesavento95afc6d2023-08-02 20:50:25 -040069 BOOST_CHECK_EQUAL(counters.nInInterests, 0);
70 BOOST_CHECK_EQUAL(counters.nOutInterests, 0);
71 BOOST_CHECK_EQUAL(counters.nInData, 0);
72 BOOST_CHECK_EQUAL(counters.nOutData, 0);
73 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
74 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
75 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 0);
76 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
77 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
78 BOOST_CHECK_EQUAL(counters.nCsHits, 0);
79 BOOST_CHECK_EQUAL(counters.nCsMisses, 0);
80
Teng Liangd94b7b32022-07-10 21:29:37 +080081 face1->receiveInterest(*makeInterest("/A/B"));
Davide Pesavento14e71f02019-03-28 17:35:25 -040082 this->advanceClocks(100_ms, 1_s);
Junxiao Shicde37ad2015-12-24 01:02:05 -070083 BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1);
Junxiao Shia6de4292016-07-12 02:08:10 +000084 BOOST_CHECK_EQUAL(face2->sentInterests[0].getName(), "/A/B");
Junxiao Shicde37ad2015-12-24 01:02:05 -070085 BOOST_REQUIRE(face2->sentInterests[0].getTag<lp::IncomingFaceIdTag>() != nullptr);
86 BOOST_CHECK_EQUAL(*face2->sentInterests[0].getTag<lp::IncomingFaceIdTag>(), face1->getId());
Davide Pesavento95afc6d2023-08-02 20:50:25 -040087 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
88 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
89 BOOST_CHECK_EQUAL(counters.nInData, 0);
90 BOOST_CHECK_EQUAL(counters.nOutData, 0);
91 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 0);
92 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
93 BOOST_CHECK_EQUAL(counters.nCsHits, 0);
94 BOOST_CHECK_EQUAL(counters.nCsMisses, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -070095
Teng Liangd94b7b32022-07-10 21:29:37 +080096 face2->receiveData(*makeData("/A/B"));
Davide Pesavento14e71f02019-03-28 17:35:25 -040097 this->advanceClocks(100_ms, 1_s);
Junxiao Shicde37ad2015-12-24 01:02:05 -070098 BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1);
Junxiao Shi9d727852019-05-14 13:44:22 -060099 BOOST_CHECK_EQUAL(face1->sentData[0].getName(), "/A/B");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700100 BOOST_REQUIRE(face1->sentData[0].getTag<lp::IncomingFaceIdTag>() != nullptr);
101 BOOST_CHECK_EQUAL(*face1->sentData[0].getTag<lp::IncomingFaceIdTag>(), face2->getId());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400102 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
103 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
104 BOOST_CHECK_EQUAL(counters.nInData, 1);
105 BOOST_CHECK_EQUAL(counters.nOutData, 1);
106 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
107 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
108 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 1);
109 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
110 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
111 BOOST_CHECK_EQUAL(counters.nCsHits, 0);
112 BOOST_CHECK_EQUAL(counters.nCsMisses, 1);
Junxiao Shi8c8d2182014-01-30 22:33:00 -0700113}
114
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400115BOOST_AUTO_TEST_CASE(CsHit)
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700116{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400117 auto face1 = addFace();
118 auto face2 = addFace();
119 auto face3 = addFace();
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700120
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700121 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000122 fib::Entry* entry = fib.insert("/A").first;
123 fib.addOrUpdateNextHop(*entry, *face2, 0);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700124
125 Pit& pit = forwarder.getPit();
126 BOOST_CHECK_EQUAL(pit.size(), 0);
127
Junxiao Shi9d727852019-05-14 13:44:22 -0600128 auto data = makeData("/A/B");
129 data->setTag(make_shared<lp::IncomingFaceIdTag>(face3->getId()));
130 forwarder.getCs().insert(*data);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700131
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400132 BOOST_CHECK_EQUAL(counters.nCsHits, 0);
133 BOOST_CHECK_EQUAL(counters.nCsMisses, 0);
134
Teng Liangd94b7b32022-07-10 21:29:37 +0800135 face1->receiveInterest(*makeInterest("/A", true));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400136 this->advanceClocks(1_ms, 5_ms);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700137 // Interest matching ContentStore should not be forwarded
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400138 BOOST_CHECK_EQUAL(face2->sentInterests.size(), 0);
139 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
140 BOOST_CHECK_EQUAL(counters.nOutInterests, 0);
141 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
142 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
143 BOOST_CHECK_EQUAL(counters.nCsHits, 1);
144 BOOST_CHECK_EQUAL(counters.nCsMisses, 0);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700145
Junxiao Shicde37ad2015-12-24 01:02:05 -0700146 BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700147 // IncomingFaceId field should be reset to represent CS
Junxiao Shi9d727852019-05-14 13:44:22 -0600148 BOOST_CHECK_EQUAL(face1->sentData[0].getName(), "/A/B");
Junxiao Shicde37ad2015-12-24 01:02:05 -0700149 BOOST_REQUIRE(face1->sentData[0].getTag<lp::IncomingFaceIdTag>() != nullptr);
150 BOOST_CHECK_EQUAL(*face1->sentData[0].getTag<lp::IncomingFaceIdTag>(), face::FACEID_CONTENT_STORE);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400151 BOOST_CHECK_EQUAL(counters.nInData, 0);
152 BOOST_CHECK_EQUAL(counters.nOutData, 1);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700153
Davide Pesavento14e71f02019-03-28 17:35:25 -0400154 this->advanceClocks(100_ms, 500_ms);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700155 // PIT entry should not be left behind
156 BOOST_CHECK_EQUAL(pit.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400157
158 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 1);
159 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
160 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
Junxiao Shiad3f1cb2014-08-18 11:12:30 -0700161}
162
Eric Newberryfdc06452020-06-23 22:40:16 -0700163BOOST_AUTO_TEST_CASE(InterestWithoutNonce)
164{
165 auto face1 = addFace();
166 auto face2 = addFace();
167
168 Fib& fib = forwarder.getFib();
169 fib::Entry* entry = fib.insert("/A").first;
170 fib.addOrUpdateNextHop(*entry, *face2, 0);
171
172 auto interest = makeInterest("/A");
173 BOOST_CHECK_EQUAL(interest->hasNonce(), false);
Teng Liangd94b7b32022-07-10 21:29:37 +0800174 face1->receiveInterest(*interest);
Eric Newberryfdc06452020-06-23 22:40:16 -0700175
176 // Ensure Nonce added if incoming packet did not have Nonce
Eric Newberryfdc06452020-06-23 22:40:16 -0700177 BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400178 BOOST_CHECK_EQUAL(face2->sentInterests[0].hasNonce(), true);
179 BOOST_CHECK_EQUAL(face2->getCounters().nOutInterests, 1);
180 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
181 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
182 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Eric Newberryfdc06452020-06-23 22:40:16 -0700183}
184
Junxiao Shi891f47b2016-06-20 00:02:11 +0000185BOOST_AUTO_TEST_CASE(OutgoingInterest)
186{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400187 auto face1 = addFace();
188 auto face2 = addFace();
Junxiao Shi891f47b2016-06-20 00:02:11 +0000189
190 Pit& pit = forwarder.getPit();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400191 auto interestA1 = makeInterest("/A", false, std::nullopt, 8378);
Eric Newberry2377ada2020-09-28 22:40:14 -0700192 auto pitA = pit.insert(*interestA1).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000193 pitA->insertOrUpdateInRecord(*face1, *interestA1);
Junxiao Shi891f47b2016-06-20 00:02:11 +0000194
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400195 auto interestA2 = makeInterest("/A", false, std::nullopt, 1698);
Davide Pesavento0498ce82021-06-14 02:02:21 -0400196 auto outA2 = forwarder.onOutgoingInterest(*interestA2, *face2, pitA);
Eric Newberry2377ada2020-09-28 22:40:14 -0700197 BOOST_REQUIRE(outA2 != nullptr);
Junxiao Shic5f651f2016-11-17 22:58:12 +0000198 BOOST_CHECK_EQUAL(outA2->getLastNonce(), 1698);
Junxiao Shi891f47b2016-06-20 00:02:11 +0000199
Eric Newberry2377ada2020-09-28 22:40:14 -0700200 // This packet will be dropped because HopLimit=0
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400201 auto interestA3 = makeInterest("/A", false, std::nullopt, 9876);
Eric Newberry2377ada2020-09-28 22:40:14 -0700202 interestA3->setHopLimit(0);
Davide Pesavento0498ce82021-06-14 02:02:21 -0400203 auto outA3 = forwarder.onOutgoingInterest(*interestA3, *face2, pitA);
Eric Newberry2377ada2020-09-28 22:40:14 -0700204 BOOST_CHECK(outA3 == nullptr);
205
Junxiao Shi891f47b2016-06-20 00:02:11 +0000206 BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400207 BOOST_CHECK_EQUAL(face2->sentInterests[0].getNonce(), 1698);
208 BOOST_CHECK_EQUAL(counters.nInInterests, 0);
209 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
Junxiao Shi891f47b2016-06-20 00:02:11 +0000210}
211
Junxiao Shie342e8d2016-09-18 16:48:00 +0000212BOOST_AUTO_TEST_CASE(NextHopFaceId)
213{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400214 auto face1 = addFace();
215 auto face2 = addFace();
216 auto face3 = addFace();
Junxiao Shie342e8d2016-09-18 16:48:00 +0000217
218 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000219 fib::Entry* entry = fib.insert("/A").first;
220 fib.addOrUpdateNextHop(*entry, *face3, 0);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000221
Junxiao Shi9d727852019-05-14 13:44:22 -0600222 auto interest = makeInterest("/A/B");
Junxiao Shie342e8d2016-09-18 16:48:00 +0000223 interest->setTag(make_shared<lp::NextHopFaceIdTag>(face2->getId()));
224
Teng Liangd94b7b32022-07-10 21:29:37 +0800225 face1->receiveInterest(*interest);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400226 this->advanceClocks(100_ms, 1_s);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000227 BOOST_CHECK_EQUAL(face3->sentInterests.size(), 0);
228 BOOST_REQUIRE_EQUAL(face2->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400229 BOOST_CHECK_EQUAL(face2->sentInterests[0].getName(), "/A/B");
230 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
231 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
232 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Junxiao Shie342e8d2016-09-18 16:48:00 +0000233}
234
Eric Newberry9d283ad2020-04-12 23:37:17 -0700235BOOST_AUTO_TEST_CASE(HopLimit)
236{
237 auto faceIn = addFace();
238 auto faceRemote = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_NON_LOCAL);
239 auto faceLocal = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
240 Fib& fib = forwarder.getFib();
241 fib::Entry* entryRemote = fib.insert("/remote").first;
242 fib.addOrUpdateNextHop(*entryRemote, *faceRemote, 0);
243 fib::Entry* entryLocal = fib.insert("/local").first;
244 fib.addOrUpdateNextHop(*entryLocal, *faceLocal, 0);
245
246 // Incoming interest w/o HopLimit will not be dropped on send or receive paths
247 auto interestNoHopLimit = makeInterest("/remote/abcdefgh");
Teng Liangd94b7b32022-07-10 21:29:37 +0800248 faceIn->receiveInterest(*interestNoHopLimit);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700249 this->advanceClocks(100_ms, 1_s);
250 BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 1);
251 BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0);
252 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 0);
253 BOOST_REQUIRE_EQUAL(faceRemote->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400254 BOOST_CHECK(!faceRemote->sentInterests[0].getHopLimit());
255 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
256 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700257
258 // Incoming interest w/ HopLimit > 1 will not be dropped on send/receive
259 auto interestHopLimit2 = makeInterest("/remote/ijklmnop");
260 interestHopLimit2->setHopLimit(2);
Teng Liangd94b7b32022-07-10 21:29:37 +0800261 faceIn->receiveInterest(*interestHopLimit2);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700262 this->advanceClocks(100_ms, 1_s);
263 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2);
264 BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0);
265 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 0);
266 BOOST_REQUIRE_EQUAL(faceRemote->sentInterests.size(), 2);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400267 BOOST_REQUIRE(faceRemote->sentInterests[1].getHopLimit());
268 BOOST_CHECK_EQUAL(*faceRemote->sentInterests[1].getHopLimit(), 1);
269 BOOST_CHECK_EQUAL(counters.nInInterests, 2);
270 BOOST_CHECK_EQUAL(counters.nOutInterests, 2);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700271
272 // Incoming interest w/ HopLimit == 1 will be dropped on send path if going out on remote face
273 auto interestHopLimit1Remote = makeInterest("/remote/qrstuvwx");
274 interestHopLimit1Remote->setHopLimit(1);
Teng Liangd94b7b32022-07-10 21:29:37 +0800275 faceIn->receiveInterest(*interestHopLimit1Remote);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700276 this->advanceClocks(100_ms, 1_s);
277 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2);
278 BOOST_CHECK_EQUAL(faceRemote->getCounters().nInHopLimitZero, 0);
279 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 1);
280 BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 2);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400281 BOOST_CHECK_EQUAL(counters.nInInterests, 3);
282 BOOST_CHECK_EQUAL(counters.nOutInterests, 2);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700283
284 // Incoming interest w/ HopLimit == 1 will not be dropped on send path if going out on local face
285 auto interestHopLimit1Local = makeInterest("/local/abcdefgh");
286 interestHopLimit1Local->setHopLimit(1);
Teng Liangd94b7b32022-07-10 21:29:37 +0800287 faceIn->receiveInterest(*interestHopLimit1Local);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700288 this->advanceClocks(100_ms, 1_s);
289 BOOST_CHECK_EQUAL(faceLocal->getCounters().nOutInterests, 1);
290 BOOST_CHECK_EQUAL(faceLocal->getCounters().nInHopLimitZero, 0);
291 BOOST_CHECK_EQUAL(faceLocal->getCounters().nOutHopLimitZero, 0);
292 BOOST_REQUIRE_EQUAL(faceLocal->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400293 BOOST_REQUIRE(faceLocal->sentInterests[0].getHopLimit());
294 BOOST_CHECK_EQUAL(*faceLocal->sentInterests[0].getHopLimit(), 0);
295 BOOST_CHECK_EQUAL(counters.nInInterests, 4);
296 BOOST_CHECK_EQUAL(counters.nOutInterests, 3);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700297
298 // Interest w/ HopLimit == 0 will be dropped on receive path
299 auto interestHopLimit0 = makeInterest("/remote/yzabcdef");
300 interestHopLimit0->setHopLimit(0);
Teng Liangd94b7b32022-07-10 21:29:37 +0800301 faceIn->receiveInterest(*interestHopLimit0);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700302 this->advanceClocks(100_ms, 1_s);
303 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutInterests, 2);
304 BOOST_CHECK_EQUAL(faceIn->getCounters().nInHopLimitZero, 1);
305 BOOST_CHECK_EQUAL(faceRemote->getCounters().nOutHopLimitZero, 1);
306 BOOST_CHECK_EQUAL(faceRemote->sentInterests.size(), 2);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400307 BOOST_CHECK_EQUAL(counters.nInInterests, 5);
308 BOOST_CHECK_EQUAL(counters.nOutInterests, 3);
309
310 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Eric Newberry9d283ad2020-04-12 23:37:17 -0700311}
312
Philipp Molla1033342021-06-14 09:34:21 +0200313BOOST_AUTO_TEST_CASE(AddDefaultHopLimit)
314{
315 auto face = addFace();
Teng Liangd94b7b32022-07-10 21:29:37 +0800316 auto faceEndpoint = FaceEndpoint(*face);
Philipp Molla1033342021-06-14 09:34:21 +0200317 Pit& pit = forwarder.getPit();
318 auto i1 = makeInterest("/A");
319 auto pitA = pit.insert(*i1).first;
320
321 // By default, no HopLimit should be added
322 auto i2 = makeInterest("/A");
323 BOOST_TEST(!i2->getHopLimit().has_value());
324 forwarder.onContentStoreMiss(*i2, faceEndpoint, pitA);
325 BOOST_TEST(!i2->getHopLimit().has_value());
326
327 // Change config value to 10
328 forwarder.m_config.defaultHopLimit = 10;
329
330 // HopLimit should be set to 10 now
331 auto i3 = makeInterest("/A");
332 BOOST_TEST(!i3->getHopLimit().has_value());
333 forwarder.onContentStoreMiss(*i3, faceEndpoint, pitA);
334 BOOST_REQUIRE(i3->getHopLimit().has_value());
335 BOOST_TEST(*i3->getHopLimit() == 10);
336
337 // An existing HopLimit should be preserved
338 auto i4 = makeInterest("/A");
339 i4->setHopLimit(50);
340 forwarder.onContentStoreMiss(*i4, faceEndpoint, pitA);
341 BOOST_REQUIRE(i4->getHopLimit().has_value());
342 BOOST_TEST(*i4->getHopLimit() == 50);
343}
344
Davide Pesavento05590472021-02-17 15:53:27 -0500345BOOST_AUTO_TEST_CASE(ScopeLocalhostIncoming)
Junxiao Shi88884492014-02-15 15:57:43 -0700346{
Davide Pesavento05590472021-02-17 15:53:27 -0500347 auto face1 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
348 auto face2 = addFace(); // default is non-local
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400349
Davide Pesavento05590472021-02-17 15:53:27 -0500350 auto& strategy = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400351 strategy.afterReceiveInterest_count = 0;
Junxiao Shic041ca32014-02-25 20:01:15 -0700352
Junxiao Shi88884492014-02-15 15:57:43 -0700353 // local face, /localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600354 auto i1 = makeInterest("/localhost/A1");
Teng Liangd94b7b32022-07-10 21:29:37 +0800355 forwarder.onIncomingInterest(*i1, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400356 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
Davide Pesavento05590472021-02-17 15:53:27 -0500357 BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700358
Junxiao Shi88884492014-02-15 15:57:43 -0700359 // non-local face, /localhost: violate
Junxiao Shi9d727852019-05-14 13:44:22 -0600360 auto i2 = makeInterest("/localhost/A2");
Teng Liangd94b7b32022-07-10 21:29:37 +0800361 forwarder.onIncomingInterest(*i2, FaceEndpoint(*face2));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400362 BOOST_CHECK_EQUAL(counters.nInInterests, 2);
363 BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700364
Junxiao Shi88884492014-02-15 15:57:43 -0700365 // local face, non-/localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600366 auto i3 = makeInterest("/A3");
Teng Liangd94b7b32022-07-10 21:29:37 +0800367 forwarder.onIncomingInterest(*i3, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400368 BOOST_CHECK_EQUAL(counters.nInInterests, 3);
369 BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 2);
Junxiao Shic041ca32014-02-25 20:01:15 -0700370
Junxiao Shi88884492014-02-15 15:57:43 -0700371 // non-local face, non-/localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600372 auto i4 = makeInterest("/A4");
Teng Liangd94b7b32022-07-10 21:29:37 +0800373 forwarder.onIncomingInterest(*i4, FaceEndpoint(*face2));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400374 BOOST_CHECK_EQUAL(counters.nInInterests, 4);
375 BOOST_CHECK_EQUAL(strategy.afterReceiveInterest_count, 3);
Junxiao Shic041ca32014-02-25 20:01:15 -0700376
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400377 BOOST_CHECK_EQUAL(counters.nOutInterests, 0);
378 BOOST_CHECK_EQUAL(counters.nInData, 0);
379 BOOST_CHECK_EQUAL(counters.nOutData, 0);
380 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
Alex Lane6bead9b2020-05-12 19:08:20 -0500381
Junxiao Shi88884492014-02-15 15:57:43 -0700382 // local face, /localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600383 auto d1 = makeData("/localhost/B1");
Teng Liangd94b7b32022-07-10 21:29:37 +0800384 forwarder.onIncomingData(*d1, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400385 BOOST_CHECK_EQUAL(counters.nInData, 1);
386 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 1);
Junxiao Shi88884492014-02-15 15:57:43 -0700387
Davide Pesavento05590472021-02-17 15:53:27 -0500388 // non-local face, /localhost: violate
Junxiao Shi9d727852019-05-14 13:44:22 -0600389 auto d2 = makeData("/localhost/B2");
Teng Liangd94b7b32022-07-10 21:29:37 +0800390 forwarder.onIncomingData(*d2, FaceEndpoint(*face2));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400391 BOOST_CHECK_EQUAL(counters.nInData, 2);
392 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 1);
Junxiao Shic041ca32014-02-25 20:01:15 -0700393
Junxiao Shi88884492014-02-15 15:57:43 -0700394 // local face, non-/localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600395 auto d3 = makeData("/B3");
Teng Liangd94b7b32022-07-10 21:29:37 +0800396 forwarder.onIncomingData(*d3, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400397 BOOST_CHECK_EQUAL(counters.nInData, 3);
398 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 2);
Junxiao Shi88884492014-02-15 15:57:43 -0700399
400 // non-local face, non-/localhost: OK
Junxiao Shi9d727852019-05-14 13:44:22 -0600401 auto d4 = makeData("/B4");
Teng Liangd94b7b32022-07-10 21:29:37 +0800402 forwarder.onIncomingData(*d4, FaceEndpoint(*face2));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400403 BOOST_CHECK_EQUAL(counters.nInData, 4);
404 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 3);
405
406 BOOST_CHECK_EQUAL(counters.nOutData, 0);
407 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Junxiao Shi88884492014-02-15 15:57:43 -0700408}
409
Junxiao Shi0355e9f2015-09-02 07:24:53 -0700410BOOST_AUTO_TEST_CASE(IncomingInterestStrategyDispatch)
Junxiao Shif3c07812014-03-11 21:48:49 -0700411{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400412 auto face1 = addFace();
413 auto face2 = addFace();
Junxiao Shif3c07812014-03-11 21:48:49 -0700414
Eric Newberry2377ada2020-09-28 22:40:14 -0700415 auto& strategyA = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName());
416 auto& strategyB = choose<DummyStrategy>(forwarder, "/B", DummyStrategy::getStrategyName());
Junxiao Shif3c07812014-03-11 21:48:49 -0700417
Junxiao Shi9d727852019-05-14 13:44:22 -0600418 auto interest1 = makeInterest("/A/1");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000419 strategyA.afterReceiveInterest_count = 0;
420 strategyA.interestOutFace = face2;
Teng Liangd94b7b32022-07-10 21:29:37 +0800421 forwarder.onIncomingInterest(*interest1, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000422 BOOST_CHECK_EQUAL(strategyA.afterReceiveInterest_count, 1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700423
Junxiao Shi9d727852019-05-14 13:44:22 -0600424 auto interest2 = makeInterest("/B/2", true);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000425 strategyB.afterReceiveInterest_count = 0;
426 strategyB.interestOutFace = face2;
Teng Liangd94b7b32022-07-10 21:29:37 +0800427 forwarder.onIncomingInterest(*interest2, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000428 BOOST_CHECK_EQUAL(strategyB.afterReceiveInterest_count, 1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700429
Davide Pesavento14e71f02019-03-28 17:35:25 -0400430 this->advanceClocks(1_ms, 5_ms);
Junxiao Shif3c07812014-03-11 21:48:49 -0700431
Junxiao Shi9d727852019-05-14 13:44:22 -0600432 auto data1 = makeData("/A/1");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000433 strategyA.beforeSatisfyInterest_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800434 forwarder.onIncomingData(*data1, FaceEndpoint(*face2));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000435 BOOST_CHECK_EQUAL(strategyA.beforeSatisfyInterest_count, 1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700436
Junxiao Shi9d727852019-05-14 13:44:22 -0600437 auto data2 = makeData("/B/2/b");
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000438 strategyB.beforeSatisfyInterest_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800439 forwarder.onIncomingData(*data2, FaceEndpoint(*face2));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000440 BOOST_CHECK_EQUAL(strategyB.beforeSatisfyInterest_count, 1);
Junxiao Shif3c07812014-03-11 21:48:49 -0700441
Junxiao Shi9d727852019-05-14 13:44:22 -0600442 auto interest3 = makeInterest("/A/3", false, 30_ms);
Teng Liangd94b7b32022-07-10 21:29:37 +0800443 forwarder.onIncomingInterest(*interest3, FaceEndpoint(*face1));
Junxiao Shi9d727852019-05-14 13:44:22 -0600444 auto interest4 = makeInterest("/B/4", false, 5_s);
Teng Liangd94b7b32022-07-10 21:29:37 +0800445 forwarder.onIncomingInterest(*interest4, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400446
447 this->advanceClocks(1_ms, 5_ms);
448
449 BOOST_CHECK_EQUAL(counters.nInInterests, 4);
450 BOOST_CHECK_EQUAL(counters.nInData, 2);
451 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
452 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 2);
453 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
454 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
455 BOOST_CHECK_EQUAL(counters.nCsHits, 0);
456 BOOST_CHECK_EQUAL(counters.nCsMisses, 4);
Junxiao Shif3c07812014-03-11 21:48:49 -0700457}
458
Junxiao Shida006f52014-05-16 11:18:00 -0700459BOOST_AUTO_TEST_CASE(IncomingData)
460{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400461 auto face1 = addFace();
462 auto face2 = addFace();
463 auto face3 = addFace();
464 auto face4 = addFace();
Junxiao Shida006f52014-05-16 11:18:00 -0700465
466 Pit& pit = forwarder.getPit();
Junxiao Shi9d727852019-05-14 13:44:22 -0600467 auto interestD = makeInterest("/A/B/C/D");
Eric Newberry2377ada2020-09-28 22:40:14 -0700468 auto pitD = pit.insert(*interestD).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000469 pitD->insertOrUpdateInRecord(*face1, *interestD);
Junxiao Shi9d727852019-05-14 13:44:22 -0600470 auto interestA = makeInterest("/A", true);
Eric Newberry2377ada2020-09-28 22:40:14 -0700471 auto pitA = pit.insert(*interestA).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000472 pitA->insertOrUpdateInRecord(*face2, *interestA);
473 pitA->insertOrUpdateInRecord(*face3, *interestA);
Junxiao Shi9d727852019-05-14 13:44:22 -0600474 auto interestC = makeInterest("/A/B/C", true);
Eric Newberry2377ada2020-09-28 22:40:14 -0700475 auto pitC = pit.insert(*interestC).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000476 pitC->insertOrUpdateInRecord(*face3, *interestC);
477 pitC->insertOrUpdateInRecord(*face4, *interestC);
Junxiao Shida006f52014-05-16 11:18:00 -0700478
Junxiao Shi9d727852019-05-14 13:44:22 -0600479 auto dataD = makeData("/A/B/C/D");
Teng Liangd94b7b32022-07-10 21:29:37 +0800480 forwarder.onIncomingData(*dataD, FaceEndpoint(*face3));
Davide Pesavento14e71f02019-03-28 17:35:25 -0400481 this->advanceClocks(1_ms, 5_ms);
Junxiao Shida006f52014-05-16 11:18:00 -0700482
Junxiao Shicde37ad2015-12-24 01:02:05 -0700483 BOOST_CHECK_EQUAL(face1->sentData.size(), 1);
484 BOOST_CHECK_EQUAL(face2->sentData.size(), 1);
485 BOOST_CHECK_EQUAL(face3->sentData.size(), 0);
486 BOOST_CHECK_EQUAL(face4->sentData.size(), 1);
Alex Lane6bead9b2020-05-12 19:08:20 -0500487
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400488 BOOST_CHECK_EQUAL(counters.nInData, 1);
489 BOOST_CHECK_EQUAL(counters.nOutData, 3);
490 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 3);
491 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 0);
492 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
Junxiao Shida006f52014-05-16 11:18:00 -0700493}
494
Eric Newberry2377ada2020-09-28 22:40:14 -0700495BOOST_AUTO_TEST_CASE(OutgoingData)
496{
497 auto face1 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_LOCAL);
498 auto face2 = addFace("dummy://", "dummy://", ndn::nfd::FACE_SCOPE_NON_LOCAL);
499 auto face3 = addFace();
500 face3->setId(face::INVALID_FACEID);
501
502 auto data = makeData("/QkzAWU6K");
503 auto localData = makeData("/localhost/YH8bqnbv");
504
505 face1->sentData.clear();
506 BOOST_CHECK(forwarder.onOutgoingData(*data, *face1));
507 BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400508 BOOST_CHECK_EQUAL(face1->sentData[0].getName(), data->getName());
509 BOOST_CHECK_EQUAL(counters.nInData, 0);
510 BOOST_CHECK_EQUAL(counters.nOutData, 1);
Eric Newberry2377ada2020-09-28 22:40:14 -0700511
512 // scope control
513 face1->sentData.clear();
514 face2->sentData.clear();
515 BOOST_CHECK(!forwarder.onOutgoingData(*localData, *face2));
516 BOOST_CHECK_EQUAL(face2->sentData.size(), 0);
517 BOOST_CHECK(forwarder.onOutgoingData(*localData, *face1));
518 BOOST_REQUIRE_EQUAL(face1->sentData.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400519 BOOST_CHECK_EQUAL(face1->sentData[0].getName(), localData->getName());
520 BOOST_CHECK_EQUAL(counters.nInData, 0);
521 BOOST_CHECK_EQUAL(counters.nOutData, 2);
Eric Newberry2377ada2020-09-28 22:40:14 -0700522
523 // face with invalid ID
524 face3->sentData.clear();
525 BOOST_CHECK(!forwarder.onOutgoingData(*data, *face3));
526 BOOST_CHECK_EQUAL(face3->sentData.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400527 BOOST_CHECK_EQUAL(counters.nInData, 0);
528 BOOST_CHECK_EQUAL(counters.nOutData, 2);
Eric Newberry2377ada2020-09-28 22:40:14 -0700529}
530
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700531BOOST_AUTO_TEST_CASE(IncomingNack)
532{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400533 auto face1 = addFace();
534 auto face2 = addFace();
535 auto face3 = addFace("dummy://", "dummy://",
536 ndn::nfd::FACE_SCOPE_NON_LOCAL,
537 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
538 ndn::nfd::LINK_TYPE_MULTI_ACCESS);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700539
Eric Newberry2377ada2020-09-28 22:40:14 -0700540 auto& strategyA = choose<DummyStrategy>(forwarder, "/", DummyStrategy::getStrategyName());
541 auto& strategyB = choose<DummyStrategy>(forwarder, "/B", DummyStrategy::getStrategyName());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700542
543 Pit& pit = forwarder.getPit();
544
545 // dispatch to the correct strategy
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400546 auto interest1 = makeInterest("/A/AYJqayrzF", false, std::nullopt, 562);
Eric Newberry2377ada2020-09-28 22:40:14 -0700547 auto pit1 = pit.insert(*interest1).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000548 pit1->insertOrUpdateOutRecord(*face1, *interest1);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400549 auto interest2 = makeInterest("/B/EVyP73ru", false, std::nullopt, 221);
Eric Newberry2377ada2020-09-28 22:40:14 -0700550 auto pit2 = pit.insert(*interest2).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000551 pit2->insertOrUpdateOutRecord(*face1, *interest2);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700552
Eric Newberry2377ada2020-09-28 22:40:14 -0700553 auto nack1 = makeNack(*interest1, lp::NackReason::CONGESTION);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000554 strategyA.afterReceiveNack_count = 0;
555 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800556 forwarder.onIncomingNack(nack1, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000557 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 1);
558 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700559
Eric Newberry2377ada2020-09-28 22:40:14 -0700560 auto nack2 = makeNack(*interest2, lp::NackReason::CONGESTION);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000561 strategyA.afterReceiveNack_count = 0;
562 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800563 forwarder.onIncomingNack(nack2, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000564 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0);
565 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700566
567 // record Nack on PIT out-record
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500568 auto outRecord1 = pit1->findOutRecord(*face1);
Junxiao Shi4846f372016-04-05 13:39:30 -0700569 BOOST_REQUIRE(outRecord1 != pit1->out_end());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700570 BOOST_REQUIRE(outRecord1->getIncomingNack() != nullptr);
571 BOOST_CHECK_EQUAL(outRecord1->getIncomingNack()->getReason(), lp::NackReason::CONGESTION);
572
573 // drop if no PIT entry
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400574 auto nack3 = makeNack(*makeInterest("/yEcw5HhdM", false, std::nullopt, 243), lp::NackReason::CONGESTION);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000575 strategyA.afterReceiveNack_count = 0;
576 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800577 forwarder.onIncomingNack(nack3, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000578 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0);
579 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700580
581 // drop if no out-record
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400582 auto interest4 = makeInterest("/Etab4KpY", false, std::nullopt, 157);
Eric Newberry2377ada2020-09-28 22:40:14 -0700583 auto pit4 = pit.insert(*interest4).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000584 pit4->insertOrUpdateOutRecord(*face1, *interest4);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700585
Eric Newberry2377ada2020-09-28 22:40:14 -0700586 auto nack4a = makeNack(*interest4, lp::NackReason::CONGESTION);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000587 strategyA.afterReceiveNack_count = 0;
588 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800589 forwarder.onIncomingNack(nack4a, FaceEndpoint(*face2));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000590 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0);
591 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700592
593 // drop if Nonce does not match out-record
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400594 auto nack4b = makeNack(*makeInterest("/Etab4KpY", false, std::nullopt, 294), lp::NackReason::CONGESTION);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000595 strategyA.afterReceiveNack_count = 0;
596 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800597 forwarder.onIncomingNack(nack4b, FaceEndpoint(*face1));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000598 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0);
599 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700600
601 // drop if inFace is multi-access
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000602 pit4->insertOrUpdateOutRecord(*face3, *interest4);
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000603 strategyA.afterReceiveNack_count = 0;
604 strategyB.afterReceiveNack_count = 0;
Teng Liangd94b7b32022-07-10 21:29:37 +0800605 forwarder.onIncomingNack(nack4a, FaceEndpoint(*face3));
Junxiao Shi0e4a1f12016-12-24 02:39:01 +0000606 BOOST_CHECK_EQUAL(strategyA.afterReceiveNack_count, 0);
607 BOOST_CHECK_EQUAL(strategyB.afterReceiveNack_count, 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400608
609 this->advanceClocks(1_ms, 10_ms);
610
611 BOOST_CHECK_EQUAL(counters.nInNacks, 6);
612 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
613 BOOST_CHECK_EQUAL(counters.nSatisfiedInterests, 0);
614 BOOST_CHECK_EQUAL(counters.nUnsatisfiedInterests, 2);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700615}
616
617BOOST_AUTO_TEST_CASE(OutgoingNack)
618{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400619 auto face1 = addFace();
620 auto face2 = addFace();
621 auto face3 = addFace("dummy://", "dummy://",
622 ndn::nfd::FACE_SCOPE_NON_LOCAL,
623 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
624 ndn::nfd::LINK_TYPE_MULTI_ACCESS);
Eric Newberry2377ada2020-09-28 22:40:14 -0700625 auto face4 = addFace();
626 face4->setId(face::INVALID_FACEID);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700627
628 Pit& pit = forwarder.getPit();
629
630 lp::NackHeader nackHeader;
631 nackHeader.setReason(lp::NackReason::CONGESTION);
632
633 // don't send Nack if there's no in-record
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400634 auto interest1 = makeInterest("/fM5IVEtC", false, std::nullopt, 719);
Eric Newberry2377ada2020-09-28 22:40:14 -0700635 auto pit1 = pit.insert(*interest1).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000636 pit1->insertOrUpdateInRecord(*face1, *interest1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700637
638 face2->sentNacks.clear();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400639 BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face2, pit1));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700640 BOOST_CHECK_EQUAL(face2->sentNacks.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400641 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700642
643 // send Nack with correct Nonce
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400644 auto interest2a = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 152);
Eric Newberry2377ada2020-09-28 22:40:14 -0700645 auto pit2 = pit.insert(*interest2a).first;
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000646 pit2->insertOrUpdateInRecord(*face1, *interest2a);
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400647 auto interest2b = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 808);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000648 pit2->insertOrUpdateInRecord(*face2, *interest2b);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700649 face1->sentNacks.clear();
Eric Newberry2377ada2020-09-28 22:40:14 -0700650 face2->sentNacks.clear();
651
Davide Pesavento0498ce82021-06-14 02:02:21 -0400652 BOOST_CHECK(forwarder.onOutgoingNack(nackHeader, *face1, pit2));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700653 BOOST_REQUIRE_EQUAL(face1->sentNacks.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400654 BOOST_CHECK_EQUAL(face1->sentNacks[0].getReason(), lp::NackReason::CONGESTION);
655 BOOST_CHECK_EQUAL(face1->sentNacks[0].getInterest().getNonce(), 152);
Eric Newberry2377ada2020-09-28 22:40:14 -0700656 BOOST_CHECK_EQUAL(face2->sentNacks.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400657 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700658
Eric Newberry2377ada2020-09-28 22:40:14 -0700659 // in-record is erased
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500660 auto inRecord2a = pit2->findInRecord(*face1);
Junxiao Shi4846f372016-04-05 13:39:30 -0700661 BOOST_CHECK(inRecord2a == pit2->in_end());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700662
663 // send Nack with correct Nonce
Davide Pesavento0498ce82021-06-14 02:02:21 -0400664 BOOST_CHECK(forwarder.onOutgoingNack(nackHeader, *face2, pit2));
Eric Newberry2377ada2020-09-28 22:40:14 -0700665 BOOST_CHECK_EQUAL(face1->sentNacks.size(), 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700666 BOOST_REQUIRE_EQUAL(face2->sentNacks.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400667 BOOST_CHECK_EQUAL(face2->sentNacks[0].getReason(), lp::NackReason::CONGESTION);
668 BOOST_CHECK_EQUAL(face2->sentNacks[0].getInterest().getNonce(), 808);
669 BOOST_CHECK_EQUAL(counters.nOutNacks, 2);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700670
Eric Newberry2377ada2020-09-28 22:40:14 -0700671 // in-record is erased
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500672 auto inRecord2b = pit2->findInRecord(*face2);
Junxiao Shi4846f372016-04-05 13:39:30 -0700673 BOOST_CHECK(inRecord2b == pit2->in_end());
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700674
675 // don't send Nack to multi-access face
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400676 auto interest2c = makeInterest("/Vi8tRm9MG3", false, std::nullopt, 228);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000677 pit2->insertOrUpdateInRecord(*face3, *interest2c);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700678
679 face3->sentNacks.clear();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400680 BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face3, pit2));
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700681 BOOST_CHECK_EQUAL(face3->sentNacks.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400682 BOOST_CHECK_EQUAL(counters.nOutNacks, 2);
Eric Newberry2377ada2020-09-28 22:40:14 -0700683
684 // don't send Nack to face with invalid ID
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400685 auto interest1b = makeInterest("/fM5IVEtC", false, std::nullopt, 553);
Eric Newberry2377ada2020-09-28 22:40:14 -0700686 pit1->insertOrUpdateInRecord(*face4, *interest1b);
687
688 face4->sentNacks.clear();
Davide Pesavento0498ce82021-06-14 02:02:21 -0400689 BOOST_CHECK(!forwarder.onOutgoingNack(nackHeader, *face4, pit1));
Eric Newberry2377ada2020-09-28 22:40:14 -0700690 BOOST_CHECK_EQUAL(face4->sentNacks.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400691 BOOST_CHECK_EQUAL(counters.nOutNacks, 2);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700692}
693
694BOOST_AUTO_TEST_CASE(InterestLoopNack)
695{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400696 auto face1 = addFace();
697 auto face2 = addFace();
698 auto face3 = addFace("dummy://", "dummy://",
699 ndn::nfd::FACE_SCOPE_NON_LOCAL,
700 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
701 ndn::nfd::LINK_TYPE_MULTI_ACCESS);
702 auto face4 = addFace();
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700703
704 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000705 fib::Entry* entry = fib.insert("/zT4XwK0Hnx").first;
706 fib.addOrUpdateNextHop(*entry, *face4, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700707
708 // receive Interest on face1
709 face1->sentNacks.clear();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400710 auto interest1a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732);
Teng Liangd94b7b32022-07-10 21:29:37 +0800711 face1->receiveInterest(*interest1a);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700712 BOOST_CHECK(face1->sentNacks.empty());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400713 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
714 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
715 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
716 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700717
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000718 // receive Interest with duplicate Nonce on face1: legit retransmission
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700719 face1->sentNacks.clear();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400720 auto interest1b = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732);
Teng Liangd94b7b32022-07-10 21:29:37 +0800721 face1->receiveInterest(*interest1b);
Junxiao Shi2fe3af02017-03-04 17:24:19 +0000722 BOOST_CHECK(face1->sentNacks.empty());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400723 BOOST_CHECK_EQUAL(counters.nInInterests, 2);
724 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
725 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
726 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700727
728 // receive Interest with duplicate Nonce on face2
729 face2->sentNacks.clear();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400730 auto interest2a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732);
Teng Liangd94b7b32022-07-10 21:29:37 +0800731 face2->receiveInterest(*interest2a);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700732 BOOST_REQUIRE_EQUAL(face2->sentNacks.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400733 BOOST_CHECK_EQUAL(face2->sentNacks[0].getInterest().wireEncode(), interest2a->wireEncode());
734 BOOST_CHECK_EQUAL(face2->sentNacks[0].getReason(), lp::NackReason::DUPLICATE);
735 BOOST_CHECK_EQUAL(counters.nInInterests, 3);
736 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
737 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
738 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700739
740 // receive Interest with new Nonce on face2
741 face2->sentNacks.clear();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400742 auto interest2b = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 944);
Teng Liangd94b7b32022-07-10 21:29:37 +0800743 face2->receiveInterest(*interest2b);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700744 BOOST_CHECK(face2->sentNacks.empty());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400745 BOOST_CHECK_EQUAL(counters.nInInterests, 4);
746 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
747 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
748 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700749
750 // receive Interest with duplicate Nonce on face3, don't send Nack to multi-access face
751 face3->sentNacks.clear();
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400752 auto interest3a = makeInterest("/zT4XwK0Hnx/28JBUvbEzc", false, std::nullopt, 732);
Teng Liangd94b7b32022-07-10 21:29:37 +0800753 face3->receiveInterest(*interest3a);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700754 BOOST_CHECK(face3->sentNacks.empty());
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400755 BOOST_CHECK_EQUAL(counters.nInInterests, 5);
756 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
757 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
758 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi5e5e4452015-09-24 16:56:52 -0700759}
760
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400761BOOST_AUTO_TEST_CASE(InterestLoopWithShortLifetime) // Bug 1953
Junxiao Shia110f262014-10-12 12:35:20 -0700762{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400763 auto face1 = addFace();
764 auto face2 = addFace();
Junxiao Shia110f262014-10-12 12:35:20 -0700765
766 // cause an Interest sent out of face2 to loop back into face1 after a delay
Junxiao Shicde37ad2015-12-24 01:02:05 -0700767 face2->afterSend.connect([face1, face2] (uint32_t pktType) {
768 if (pktType == tlv::Interest) {
769 auto interest = make_shared<Interest>(face2->sentInterests.back());
Teng Liangd94b7b32022-07-10 21:29:37 +0800770 getScheduler().schedule(170_ms, [face1, interest] { face1->receiveInterest(*interest); });
Junxiao Shicde37ad2015-12-24 01:02:05 -0700771 }
Junxiao Shic099ddb2014-12-25 20:53:20 -0700772 });
Junxiao Shia110f262014-10-12 12:35:20 -0700773
774 Fib& fib = forwarder.getFib();
Ju Pand8315bf2019-07-31 06:59:07 +0000775 fib::Entry* entry = fib.insert("/A").first;
776 fib.addOrUpdateNextHop(*entry, *face2, 0);
Junxiao Shia110f262014-10-12 12:35:20 -0700777
Junxiao Shi455581d2014-11-17 18:38:40 -0700778 // receive an Interest
Junxiao Shi9d727852019-05-14 13:44:22 -0600779 auto interest = makeInterest("/A/1", false, 50_ms, 82101183);
Teng Liangd94b7b32022-07-10 21:29:37 +0800780 face1->receiveInterest(*interest);
Junxiao Shia110f262014-10-12 12:35:20 -0700781
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400782 // Interest should be forwarded only once, as long as Nonce is in Dead Nonce List
Davide Pesavento14e71f02019-03-28 17:35:25 -0400783 BOOST_ASSERT(25_ms * 40 < forwarder.getDeadNonceList().getLifetime());
784 this->advanceClocks(25_ms, 40);
Junxiao Shicde37ad2015-12-24 01:02:05 -0700785 BOOST_CHECK_EQUAL(face2->sentInterests.size(), 1);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400786 BOOST_CHECK_EQUAL(counters.nInInterests, 2);
787 BOOST_CHECK_EQUAL(counters.nOutInterests, 1);
788 BOOST_CHECK_EQUAL(counters.nInNacks, 0);
789 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi455581d2014-11-17 18:38:40 -0700790
791 // It's unnecessary to check that Interest with duplicate Nonce can be forwarded again
792 // after it's gone from Dead Nonce List, because the entry lifetime of Dead Nonce List
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400793 // is an implementation decision. The NDN protocol requires Name+Nonce to be unique,
794 // without specifying when Name+Nonce could repeat. Theoretically, a forwarder is allowed
795 // to suppress an Interest if its Name+Nonce has appeared at any point in the past.
Junxiao Shia110f262014-10-12 12:35:20 -0700796}
797
Junxiao Shid41d6072016-06-19 23:35:27 +0000798BOOST_AUTO_TEST_CASE(PitLeak) // Bug 3484
Junxiao Shi330136a2016-03-10 04:53:08 -0700799{
Davide Pesaventoa4abfb02019-10-06 16:02:56 -0400800 auto face1 = addFace();
Junxiao Shi9d727852019-05-14 13:44:22 -0600801 auto interest = makeInterest("/hcLSAsQ9A", false, 2_s, 61883075);
Junxiao Shi330136a2016-03-10 04:53:08 -0700802
Davide Pesavento0498ce82021-06-14 02:02:21 -0400803 auto& dnl = forwarder.getDeadNonceList();
Junxiao Shi330136a2016-03-10 04:53:08 -0700804 dnl.add(interest->getName(), interest->getNonce());
Davide Pesavento0498ce82021-06-14 02:02:21 -0400805 auto& pit = forwarder.getPit();
806 BOOST_CHECK_EQUAL(pit.size(), 0);
Junxiao Shi330136a2016-03-10 04:53:08 -0700807
Teng Liangd94b7b32022-07-10 21:29:37 +0800808 forwarder.onIncomingInterest(*interest, FaceEndpoint(*face1));
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400809 // PIT entry is never created if a loop is detected by the Dead Nonce List
Junxiao Shi330136a2016-03-10 04:53:08 -0700810 BOOST_CHECK_EQUAL(pit.size(), 0);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400811 BOOST_CHECK_EQUAL(counters.nInInterests, 1);
812 BOOST_CHECK_EQUAL(counters.nOutInterests, 0);
813 BOOST_CHECK_EQUAL(counters.nOutNacks, 1);
Junxiao Shi330136a2016-03-10 04:53:08 -0700814}
815
Alex Lane6bead9b2020-05-12 19:08:20 -0500816BOOST_AUTO_TEST_CASE(UnsolicitedData)
817{
818 auto face1 = addFace();
819 auto data = makeData("/A");
820
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400821 BOOST_CHECK_EQUAL(counters.nInData, 0);
822 BOOST_CHECK_EQUAL(counters.nOutData, 0);
823 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 0);
824
Teng Liangd94b7b32022-07-10 21:29:37 +0800825 forwarder.onIncomingData(*data, FaceEndpoint(*face1));
Alex Lane6bead9b2020-05-12 19:08:20 -0500826 this->advanceClocks(1_ms, 10_ms);
Davide Pesavento95afc6d2023-08-02 20:50:25 -0400827 BOOST_CHECK_EQUAL(counters.nInData, 1);
828 BOOST_CHECK_EQUAL(counters.nOutData, 0);
829 BOOST_CHECK_EQUAL(counters.nOutNacks, 0);
830 BOOST_CHECK_EQUAL(counters.nUnsolicitedData, 1);
Alex Lane6bead9b2020-05-12 19:08:20 -0500831}
832
Davide Pesaventob21bed82021-02-13 00:20:06 -0500833BOOST_AUTO_TEST_CASE(NewNextHop)
834{
835 auto face1 = addFace();
836 auto face2 = addFace();
837 auto face3 = addFace();
838 auto face4 = addFace();
839
840 auto& strategy = choose<DummyStrategy>(forwarder, "/A", DummyStrategy::getStrategyName());
841
842 Fib& fib = forwarder.getFib();
843 Pit& pit = forwarder.getPit();
844
845 // fib: "/", "/A/B", "/A/B/C/D/E"
846 fib::Entry* entry = fib.insert("/").first;
847 fib.addOrUpdateNextHop(*entry, *face1, 100);
848 entry = fib.insert("/A/B").first;
849 fib.addOrUpdateNextHop(*entry, *face2, 0);
850 entry = fib.insert("/A/B/C/D/E").first;
851 fib.addOrUpdateNextHop(*entry, *face3, 0);
852
853 // pit: "/A", "/A/B/C", "/A/B/Z"
854 auto interest1 = makeInterest("/A");
855 auto pit1 = pit.insert(*interest1).first;
856 pit1->insertOrUpdateInRecord(*face3, *interest1);
857 auto interest2 = makeInterest("/A/B/C");
858 auto pit2 = pit.insert(*interest2).first;
859 pit2->insertOrUpdateInRecord(*face3, *interest2);
860 auto interest3 = makeInterest("/A/B/Z");
861 auto pit3 = pit.insert(*interest3).first;
862 pit3->insertOrUpdateInRecord(*face3, *interest3);
863
864 // new nexthop for "/"
865 entry = fib.insert("/").first;
866 fib.addOrUpdateNextHop(*entry, *face2, 50);
867
868 // /A --> triggered
869 // /A/B/C --> not triggered
870 // /A/B/Z --> not triggered
871 BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 1);
872 BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A");
873 strategy.afterNewNextHopCalls.clear();
874
875 // new nexthop for "/A"
876 entry = fib.insert("/A").first;
877 fib.addOrUpdateNextHop(*entry, *face4, 50);
878
879 // /A --> triggered
880 // /A/B/C --> not triggered
881 // /A/B/Z --> not triggered
882 BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 1);
883 BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A");
884 strategy.afterNewNextHopCalls.clear();
885
886 // new nexthop for "/A/B"
887 entry = fib.insert("/A/B").first;
888 fib.addOrUpdateNextHop(*entry, *face4, 0);
889
890 // /A --> not triggered
891 // /A/B/C --> triggered
892 // /A/B/Z --> triggered
893 BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 2);
894 BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A/B/C");
895 BOOST_TEST(strategy.afterNewNextHopCalls[1] == "/A/B/Z");
896 strategy.afterNewNextHopCalls.clear();
897
898 // new nexthop for "/A/B/C/D"
899 entry = fib.insert("/A/B/C/D").first;
900 fib.addOrUpdateNextHop(*entry, *face1, 0);
901
902 // nothing triggered
903 BOOST_TEST(strategy.afterNewNextHopCalls.size() == 0);
904
905 // create a second pit entry for /A
906 auto interest4 = makeInterest("/A");
907 interest4->setMustBeFresh(true);
908 auto pit4 = pit.insert(*interest4).first;
909 pit4->insertOrUpdateInRecord(*face3, *interest4);
910
911 // new nexthop for "/A"
912 entry = fib.insert("/A").first;
913 fib.addOrUpdateNextHop(*entry, *face1, 0);
914
915 // /A --> triggered twice
916 // /A/B/C --> not triggered
917 // /A/B/Z --> not triggered
918 BOOST_TEST_REQUIRE(strategy.afterNewNextHopCalls.size() == 2);
919 BOOST_TEST(strategy.afterNewNextHopCalls[0] == "/A");
920 BOOST_TEST(strategy.afterNewNextHopCalls[1] == "/A");
921}
922
Philipp Molla1033342021-06-14 09:34:21 +0200923BOOST_AUTO_TEST_SUITE(ProcessConfig)
924
925BOOST_AUTO_TEST_CASE(DefaultHopLimit)
926{
927 ConfigFile cf;
928 forwarder.setConfigFile(cf);
929
930 std::string config = R"CONFIG(
931 forwarder
932 {
933 default_hop_limit 10
934 }
935 )CONFIG";
936
937 // The default value is 0
938 BOOST_TEST(forwarder.m_config.defaultHopLimit == 0);
939
940 // Dry run parsing should not change the default config
941 cf.parse(config, true, "dummy-config");
942 BOOST_TEST(forwarder.m_config.defaultHopLimit == 0);
943
944 // Check if the actual parsing works
945 cf.parse(config, false, "dummy-config");
946 BOOST_TEST(forwarder.m_config.defaultHopLimit == 10);
947
948 // After removing default_hop_limit from the config file,
949 // the default value of zero should be restored
950 config = R"CONFIG(
951 forwarder
952 {
953 }
954 )CONFIG";
955
956 cf.parse(config, false, "dummy-config");
957 BOOST_TEST(forwarder.m_config.defaultHopLimit == 0);
958}
959
960BOOST_AUTO_TEST_CASE(BadDefaultHopLimit)
961{
962 ConfigFile cf;
963 forwarder.setConfigFile(cf);
964
965 // not a number
966 std::string config = R"CONFIG(
967 forwarder
968 {
969 default_hop_limit hello
970 }
971 )CONFIG";
972
973 BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error);
974 BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error);
975
976 // negative number
977 config = R"CONFIG(
978 forwarder
979 {
980 default_hop_limit -1
981 }
982 )CONFIG";
983
984 BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error);
985 BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error);
986
987 // out of range
988 config = R"CONFIG(
989 forwarder
990 {
991 default_hop_limit 256
992 }
993 )CONFIG";
994
995 BOOST_CHECK_THROW(cf.parse(config, true, "dummy-config"), ConfigFile::Error);
996 BOOST_CHECK_THROW(cf.parse(config, false, "dummy-config"), ConfigFile::Error);
997}
998
999BOOST_AUTO_TEST_SUITE_END() // ProcessConfig
1000
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04001001BOOST_AUTO_TEST_SUITE_END() // TestForwarder
1002BOOST_AUTO_TEST_SUITE_END() // Fw
Junxiao Shi8c8d2182014-01-30 22:33:00 -07001003
Davide Pesaventoe422f9e2022-06-03 01:30:23 -04001004} // namespace nfd::tests