blob: d5b71fc8575af6655ee0670eb3214403235c7158 [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/*
Junxiao Shi727b6102022-01-11 18:40:04 +00003 * Copyright (c) 2014-2022, 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
Junxiao Shia2742922016-11-17 22:53:23 +000036namespace nfd {
37namespace pit {
38namespace tests {
39
40using namespace nfd::tests;
41
42BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040043BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture)
Junxiao Shia2742922016-11-17 22:53:23 +000044
45BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1)
46BOOST_AUTO_TEST_CASE(CanMatch)
47{
Junxiao Shi25d97282019-05-14 13:44:46 -060048 auto interest0 = makeInterest("/A");
Junxiao Shia2742922016-11-17 22:53:23 +000049 Entry entry(*interest0);
50
Junxiao Shi25d97282019-05-14 13:44:46 -060051 auto interest1 = makeInterest("/B");
Junxiao Shia2742922016-11-17 22:53:23 +000052 BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false);
53
Junxiao Shi25d97282019-05-14 13:44:46 -060054 auto interest2 = makeInterest("/A", false, nullopt, 27956);
Junxiao Shia2742922016-11-17 22:53:23 +000055 BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true);
56
Junxiao Shi25d97282019-05-14 13:44:46 -060057 auto interest3 = makeInterest("/A", false, 6210_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000058 BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true);
59
Junxiao Shi25d97282019-05-14 13:44:46 -060060 auto interest4 = makeInterest("/A");
Junxiao Shi727b6102022-01-11 18:40:04 +000061 interest4->setForwardingHint({"/telia/terabits", "/ucla/cs"});
Junxiao Shia2742922016-11-17 22:53:23 +000062 BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162
63
Junxiao Shi25d97282019-05-14 13:44:46 -060064 auto interest5 = makeInterest("/A", true);
Junxiao Shia2742922016-11-17 22:53:23 +000065 BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false);
66}
67
68BOOST_AUTO_TEST_CASE(InOutRecords)
69{
Davide Pesaventob31206e2019-04-20 22:34:12 -040070 auto face1 = make_shared<DummyFace>();
71 auto face2 = make_shared<DummyFace>();
72
Junxiao Shi25d97282019-05-14 13:44:46 -060073 Name name("/KuYfjtRq");
Davide Pesaventob31206e2019-04-20 22:34:12 -040074 auto interest = makeInterest(name);
Junxiao Shi25d97282019-05-14 13:44:46 -060075 auto interest1 = makeInterest(name, false, 2528_ms, 25559);
76 auto interest2 = makeInterest(name, false, 6464_ms, 19004);
77 auto interest3 = makeInterest(name, false, 3585_ms, 24216);
78 auto interest4 = makeInterest(name, false, 8795_ms, 17365);
Junxiao Shia2742922016-11-17 22:53:23 +000079
80 Entry entry(*interest);
81
82 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
83 BOOST_CHECK_EQUAL(entry.getName(), name);
84
85 const InRecordCollection& inRecords1 = entry.getInRecords();
86 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
87 const OutRecordCollection& outRecords1 = entry.getOutRecords();
88 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
89
90 // insert in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -040091 auto before1 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000092 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -040093 auto after1 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +000094 const InRecordCollection& inRecords2 = entry.getInRecords();
95 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
96 BOOST_CHECK(in1 == inRecords2.begin());
97 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
98 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
99 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
100 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400101 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(),
102 after1 - before1);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000103 BOOST_CHECK(in1 == entry.getInRecord(*face1));
Junxiao Shia2742922016-11-17 22:53:23 +0000104
105 // insert out-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400106 auto before2 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000107 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400108 auto after2 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000109 const OutRecordCollection& outRecords2 = entry.getOutRecords();
110 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
111 BOOST_CHECK(out1 == outRecords2.begin());
112 BOOST_CHECK_EQUAL(&out1->getFace(), face1.get());
113 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
114 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
115 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400116 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(),
117 after2 - before2);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000118 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
Junxiao Shia2742922016-11-17 22:53:23 +0000119
120 // update in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400121 auto before3 = time::steady_clock::now();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000122 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400123 auto after3 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000124 const InRecordCollection& inRecords3 = entry.getInRecords();
125 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
126 BOOST_CHECK(in2 == inRecords3.begin());
127 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
128 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400129 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(),
130 after3 - before3);
Junxiao Shia2742922016-11-17 22:53:23 +0000131
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000132 // insert another in-record
133 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3);
Junxiao Shia2742922016-11-17 22:53:23 +0000134 const InRecordCollection& inRecords4 = entry.getInRecords();
135 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
136 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
137
138 // get in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000139 InRecordCollection::iterator in4 = entry.getInRecord(*face1);
140 BOOST_REQUIRE(in4 != entry.in_end());
141 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400142
143 // delete in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000144 entry.deleteInRecord(*face1);
145 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 1);
146 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000147
148 // clear in-records
149 entry.clearInRecords();
Davide Pesaventob31206e2019-04-20 22:34:12 -0400150 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000151 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
152 BOOST_CHECK(entry.getInRecord(*face2) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000153
154 // insert another out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000155 OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000156 const OutRecordCollection& outRecords3 = entry.getOutRecords();
157 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
158 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
159
160 // get out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000161 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000162 BOOST_REQUIRE(out3 != entry.out_end());
163 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
164
165 // delete out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000166 entry.deleteOutRecord(*face2);
Junxiao Shia2742922016-11-17 22:53:23 +0000167 const OutRecordCollection& outRecords4 = entry.getOutRecords();
168 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
169 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000170 BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000171}
172
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600173const time::milliseconds lifetimes[] = {
174 -1_ms, // unset
175 1_ms,
176 ndn::DEFAULT_INTEREST_LIFETIME,
177 8624_ms,
178 86400_s,
179 time::milliseconds(std::numeric_limits<time::milliseconds::rep>::max()),
180};
181
182BOOST_DATA_TEST_CASE(Lifetime, bdata::make(lifetimes), lifetime)
Junxiao Shia2742922016-11-17 22:53:23 +0000183{
Junxiao Shi25d97282019-05-14 13:44:46 -0600184 auto interest = makeInterest("/7oIEurbgy6");
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600185 if (lifetime >= 0_ms) {
186 interest->setInterestLifetime(lifetime);
187 }
188
189 auto expectedLifetime = lifetime;
190 if (lifetime < 0_ms) {
191 expectedLifetime = ndn::DEFAULT_INTEREST_LIFETIME;
192 }
193 else if (lifetime > 10_days) {
194 expectedLifetime = 10_days;
195 }
196
Davide Pesaventob31206e2019-04-20 22:34:12 -0400197 auto face = make_shared<DummyFace>();
Junxiao Shia2742922016-11-17 22:53:23 +0000198 Entry entry(*interest);
199
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000200 auto inIt = entry.insertOrUpdateInRecord(*face, *interest);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600201 auto expiryFromNow = inIt->getExpiry() - time::steady_clock::now();
202 BOOST_CHECK_GT(expiryFromNow, 0_ms);
203 BOOST_CHECK_LT(time::abs(expiryFromNow - expectedLifetime), 100_ms);
Junxiao Shia2742922016-11-17 22:53:23 +0000204
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000205 auto outIt = entry.insertOrUpdateOutRecord(*face, *interest);
Junxiao Shie1cf4ba2020-06-19 16:40:53 -0600206 expiryFromNow = outIt->getExpiry() - time::steady_clock::now();
207 BOOST_CHECK_GT(expiryFromNow, 0_ms);
208 BOOST_CHECK_LT(time::abs(expiryFromNow - expectedLifetime), 100_ms);
Junxiao Shia2742922016-11-17 22:53:23 +0000209}
210
211BOOST_AUTO_TEST_CASE(OutRecordNack)
212{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400213 auto face1 = make_shared<DummyFace>();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000214 OutRecord outR(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000215 BOOST_CHECK(outR.getIncomingNack() == nullptr);
216
Junxiao Shi25d97282019-05-14 13:44:46 -0600217 auto interest1 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000218 interest1->setNonce(165);
219 outR.update(*interest1);
220 BOOST_CHECK(outR.getIncomingNack() == nullptr);
221
Junxiao Shi25d97282019-05-14 13:44:46 -0600222 auto interest2 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000223 interest2->setNonce(996);
224 lp::Nack nack2(*interest2);
225 nack2.setReason(lp::NackReason::CONGESTION);
226 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
227 BOOST_CHECK(outR.getIncomingNack() == nullptr);
228
229 lp::Nack nack1(*interest1);
230 nack1.setReason(lp::NackReason::DUPLICATE);
231 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
232 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
233 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
234
235 outR.clearIncomingNack();
236 BOOST_CHECK(outR.getIncomingNack() == nullptr);
237}
238
239BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
240BOOST_AUTO_TEST_SUITE_END() // Table
241
242} // namespace tests
243} // namespace pit
244} // namespace nfd