blob: 7768faa95c648d2e8c917f75e045d800f25248d2 [file] [log] [blame]
Junxiao Shia2742922016-11-17 22:53:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
Davide Pesavento4d2e7882024-02-15 23:00:21 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Junxiao Shia2742922016-11-17 22:53:23 +00004 * 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.
10 *
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/>.
24 */
25
26#include "table/pit-entry.hpp"
Junxiao Shia2742922016-11-17 22:53:23 +000027
28#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
30#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shia2742922016-11-17 22:53:23 +000031
Junxiao Shie1cf4ba2020-06-19 16:40:53 -060032#include <boost/test/data/test_case.hpp>
33
34namespace bdata = boost::unit_test::data;
35
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040036namespace nfd::tests {
Junxiao Shia2742922016-11-17 22:53:23 +000037
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040038using namespace nfd::pit;
Junxiao Shia2742922016-11-17 22:53:23 +000039
40BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture)
Junxiao Shia2742922016-11-17 22:53:23 +000042
43BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1)
44BOOST_AUTO_TEST_CASE(CanMatch)
45{
Junxiao Shi25d97282019-05-14 13:44:46 -060046 auto interest0 = makeInterest("/A");
Junxiao Shia2742922016-11-17 22:53:23 +000047 Entry entry(*interest0);
48
Junxiao Shi25d97282019-05-14 13:44:46 -060049 auto interest1 = makeInterest("/B");
Junxiao Shia2742922016-11-17 22:53:23 +000050 BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false);
51
Davide Pesaventob7bfcb92022-05-22 23:55:23 -040052 auto interest2 = makeInterest("/A", false, std::nullopt, 27956);
Junxiao Shia2742922016-11-17 22:53:23 +000053 BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true);
54
Junxiao Shi25d97282019-05-14 13:44:46 -060055 auto interest3 = makeInterest("/A", false, 6210_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000056 BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true);
57
Junxiao Shi25d97282019-05-14 13:44:46 -060058 auto interest4 = makeInterest("/A");
Junxiao Shi727b6102022-01-11 18:40:04 +000059 interest4->setForwardingHint({"/telia/terabits", "/ucla/cs"});
Junxiao Shia2742922016-11-17 22:53:23 +000060 BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162
61
Junxiao Shi25d97282019-05-14 13:44:46 -060062 auto interest5 = makeInterest("/A", true);
Junxiao Shia2742922016-11-17 22:53:23 +000063 BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false);
64}
65
66BOOST_AUTO_TEST_CASE(InOutRecords)
67{
Davide Pesaventob31206e2019-04-20 22:34:12 -040068 auto face1 = make_shared<DummyFace>();
69 auto face2 = make_shared<DummyFace>();
70
Junxiao Shi25d97282019-05-14 13:44:46 -060071 Name name("/KuYfjtRq");
Davide Pesaventob31206e2019-04-20 22:34:12 -040072 auto interest = makeInterest(name);
Junxiao Shi25d97282019-05-14 13:44:46 -060073 auto interest1 = makeInterest(name, false, 2528_ms, 25559);
74 auto interest2 = makeInterest(name, false, 6464_ms, 19004);
75 auto interest3 = makeInterest(name, false, 3585_ms, 24216);
76 auto interest4 = makeInterest(name, false, 8795_ms, 17365);
Junxiao Shia2742922016-11-17 22:53:23 +000077
78 Entry entry(*interest);
79
80 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
81 BOOST_CHECK_EQUAL(entry.getName(), name);
82
83 const InRecordCollection& inRecords1 = entry.getInRecords();
84 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
85 const OutRecordCollection& outRecords1 = entry.getOutRecords();
86 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
87
88 // insert in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -040089 auto before1 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000090 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -040091 auto after1 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +000092 const InRecordCollection& inRecords2 = entry.getInRecords();
93 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
94 BOOST_CHECK(in1 == inRecords2.begin());
95 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
96 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
97 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
98 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Davide Pesaventob31206e2019-04-20 22:34:12 -040099 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(),
100 after1 - before1);
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500101 BOOST_CHECK(in1 == entry.findInRecord(*face1));
Junxiao Shia2742922016-11-17 22:53:23 +0000102
103 // insert out-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400104 auto before2 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000105 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400106 auto after2 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000107 const OutRecordCollection& outRecords2 = entry.getOutRecords();
108 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
109 BOOST_CHECK(out1 == outRecords2.begin());
110 BOOST_CHECK_EQUAL(&out1->getFace(), face1.get());
111 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
112 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
113 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400114 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(),
115 after2 - before2);
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500116 BOOST_CHECK(out1 == entry.findOutRecord(*face1));
Junxiao Shia2742922016-11-17 22:53:23 +0000117
118 // update in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400119 auto before3 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000120 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400121 auto after3 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000122 const InRecordCollection& inRecords3 = entry.getInRecords();
123 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
124 BOOST_CHECK(in2 == inRecords3.begin());
125 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
126 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400127 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(),
128 after3 - before3);
Junxiao Shia2742922016-11-17 22:53:23 +0000129
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000130 // insert another in-record
131 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3);
Junxiao Shia2742922016-11-17 22:53:23 +0000132 const InRecordCollection& inRecords4 = entry.getInRecords();
133 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
134 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
135
136 // get in-record
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500137 InRecordCollection::iterator in4 = entry.findInRecord(*face1);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000138 BOOST_REQUIRE(in4 != entry.in_end());
139 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400140
141 // delete in-record
Davide Pesavento4d2e7882024-02-15 23:00:21 -0500142 entry.deleteInRecord(in4);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000143 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 1);
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500144 BOOST_CHECK(entry.findInRecord(*face1) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000145
146 // clear in-records
147 entry.clearInRecords();
Davide Pesaventob31206e2019-04-20 22:34:12 -0400148 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0);
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500149 BOOST_CHECK(entry.findInRecord(*face1) == entry.in_end());
150 BOOST_CHECK(entry.findInRecord(*face2) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000151
152 // insert another out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000153 OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000154 const OutRecordCollection& outRecords3 = entry.getOutRecords();
155 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
156 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
157
158 // get out-record
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500159 OutRecordCollection::iterator out3 = entry.findOutRecord(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000160 BOOST_REQUIRE(out3 != entry.out_end());
161 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
162
163 // delete out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000164 entry.deleteOutRecord(*face2);
Junxiao Shia2742922016-11-17 22:53:23 +0000165 const OutRecordCollection& outRecords4 = entry.getOutRecords();
166 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
167 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
Davide Pesaventob124b8e2024-02-16 16:54:26 -0500168 BOOST_CHECK(entry.findOutRecord(*face2) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000169}
170
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600171const time::milliseconds lifetimes[] = {
172 -1_ms, // unset
173 1_ms,
174 ndn::DEFAULT_INTEREST_LIFETIME,
175 8624_ms,
176 86400_s,
177 time::milliseconds(std::numeric_limits<time::milliseconds::rep>::max()),
178};
179
180BOOST_DATA_TEST_CASE(Lifetime, bdata::make(lifetimes), lifetime)
Junxiao Shia2742922016-11-17 22:53:23 +0000181{
Junxiao Shi25d97282019-05-14 13:44:46 -0600182 auto interest = makeInterest("/7oIEurbgy6");
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600183 if (lifetime >= 0_ms) {
184 interest->setInterestLifetime(lifetime);
185 }
186
187 auto expectedLifetime = lifetime;
188 if (lifetime < 0_ms) {
189 expectedLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
190 }
191 else if (lifetime > 10_days) {
192 expectedLifetime = 10_days;
193 }
194
Davide Pesaventob31206e2019-04-20 22:34:12 -0400195 auto face = make_shared<DummyFace>();
Junxiao Shia2742922016-11-17 22:53:23 +0000196 Entry entry(*interest);
197
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000198 auto inIt = entry.insertOrUpdateInRecord(*face, *interest);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600199 auto expiryFromNow = inIt->getExpiry() - time::steady_clock::now();
200 BOOST_CHECK_GT(expiryFromNow, 0_ms);
201 BOOST_CHECK_LT(time::abs(expiryFromNow - expectedLifetime), 100_ms);
Junxiao Shia2742922016-11-17 22:53:23 +0000202
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000203 auto outIt = entry.insertOrUpdateOutRecord(*face, *interest);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600204 expiryFromNow = outIt->getExpiry() - time::steady_clock::now();
205 BOOST_CHECK_GT(expiryFromNow, 0_ms);
206 BOOST_CHECK_LT(time::abs(expiryFromNow - expectedLifetime), 100_ms);
Junxiao Shia2742922016-11-17 22:53:23 +0000207}
208
209BOOST_AUTO_TEST_CASE(OutRecordNack)
210{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400211 auto face1 = make_shared<DummyFace>();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000212 OutRecord outR(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000213 BOOST_CHECK(outR.getIncomingNack() == nullptr);
214
Junxiao Shi25d97282019-05-14 13:44:46 -0600215 auto interest1 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000216 interest1->setNonce(165);
217 outR.update(*interest1);
218 BOOST_CHECK(outR.getIncomingNack() == nullptr);
219
Junxiao Shi25d97282019-05-14 13:44:46 -0600220 auto interest2 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000221 interest2->setNonce(996);
222 lp::Nack nack2(*interest2);
223 nack2.setReason(lp::NackReason::CONGESTION);
224 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
225 BOOST_CHECK(outR.getIncomingNack() == nullptr);
226
227 lp::Nack nack1(*interest1);
228 nack1.setReason(lp::NackReason::DUPLICATE);
229 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
230 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
231 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
232
233 outR.clearIncomingNack();
234 BOOST_CHECK(outR.getIncomingNack() == nullptr);
235}
236
237BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
238BOOST_AUTO_TEST_SUITE_END() // Table
239
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400240} // namespace nfd::tests