Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | fc2e13d | 2017-07-25 02:08:48 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | 727b610 | 2022-01-11 18:40:04 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 4 | * 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 27 | |
| 28 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 29 | #include "tests/daemon/global-io-fixture.hpp" |
| 30 | #include "tests/daemon/face/dummy-face.hpp" |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 31 | |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 32 | #include <boost/test/data/test_case.hpp> |
| 33 | |
| 34 | namespace bdata = boost::unit_test::data; |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::tests { |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 37 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 38 | using namespace nfd::pit; |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 39 | |
| 40 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 41 | BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture) |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 42 | |
| 43 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1) |
| 44 | BOOST_AUTO_TEST_CASE(CanMatch) |
| 45 | { |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 46 | auto interest0 = makeInterest("/A"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 47 | Entry entry(*interest0); |
| 48 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 49 | auto interest1 = makeInterest("/B"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false); |
| 51 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 52 | auto interest2 = makeInterest("/A", false, std::nullopt, 27956); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true); |
| 54 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 55 | auto interest3 = makeInterest("/A", false, 6210_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true); |
| 57 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 58 | auto interest4 = makeInterest("/A"); |
Junxiao Shi | 727b610 | 2022-01-11 18:40:04 +0000 | [diff] [blame] | 59 | interest4->setForwardingHint({"/telia/terabits", "/ucla/cs"}); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 60 | BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162 |
| 61 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 62 | auto interest5 = makeInterest("/A", true); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false); |
| 64 | } |
| 65 | |
| 66 | BOOST_AUTO_TEST_CASE(InOutRecords) |
| 67 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 68 | auto face1 = make_shared<DummyFace>(); |
| 69 | auto face2 = make_shared<DummyFace>(); |
| 70 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 71 | Name name("/KuYfjtRq"); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 72 | auto interest = makeInterest(name); |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 73 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 77 | |
| 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 89 | auto before1 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 90 | InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 91 | auto after1 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 92 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 99 | BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(), |
| 100 | after1 - before1); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 101 | BOOST_CHECK(in1 == entry.getInRecord(*face1)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 102 | |
| 103 | // insert out-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 104 | auto before2 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 105 | OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 106 | auto after2 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 107 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 114 | BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(), |
| 115 | after2 - before2); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 116 | BOOST_CHECK(out1 == entry.getOutRecord(*face1)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 117 | |
| 118 | // update in-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 119 | auto before3 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 120 | InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 121 | auto after3 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 122 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 127 | BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(), |
| 128 | after3 - before3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 129 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 130 | // insert another in-record |
| 131 | InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 132 | 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 |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 137 | InRecordCollection::iterator in4 = entry.getInRecord(*face1); |
| 138 | BOOST_REQUIRE(in4 != entry.in_end()); |
| 139 | BOOST_CHECK_EQUAL(&in4->getFace(), face1.get()); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 140 | |
| 141 | // delete in-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 142 | entry.deleteInRecord(*face1); |
| 143 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 1); |
| 144 | BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 145 | |
| 146 | // clear in-records |
| 147 | entry.clearInRecords(); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 149 | BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end()); |
| 150 | BOOST_CHECK(entry.getInRecord(*face2) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 151 | |
| 152 | // insert another out-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 153 | OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, *interest4); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 154 | 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 |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 159 | OutRecordCollection::iterator out3 = entry.getOutRecord(*face1); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 160 | BOOST_REQUIRE(out3 != entry.out_end()); |
| 161 | BOOST_CHECK_EQUAL(&out3->getFace(), face1.get()); |
| 162 | |
| 163 | // delete out-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 164 | entry.deleteOutRecord(*face2); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 165 | const OutRecordCollection& outRecords4 = entry.getOutRecords(); |
| 166 | BOOST_REQUIRE_EQUAL(outRecords4.size(), 1); |
| 167 | BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get()); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 168 | BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 171 | const 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 | |
| 180 | BOOST_DATA_TEST_CASE(Lifetime, bdata::make(lifetimes), lifetime) |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 181 | { |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 182 | auto interest = makeInterest("/7oIEurbgy6"); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 183 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 195 | auto face = make_shared<DummyFace>(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 196 | Entry entry(*interest); |
| 197 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 198 | auto inIt = entry.insertOrUpdateInRecord(*face, *interest); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 199 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 202 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 203 | auto outIt = entry.insertOrUpdateOutRecord(*face, *interest); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 204 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | BOOST_AUTO_TEST_CASE(OutRecordNack) |
| 210 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 211 | auto face1 = make_shared<DummyFace>(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 212 | OutRecord outR(*face1); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 213 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 214 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 215 | auto interest1 = makeInterest("/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 216 | interest1->setNonce(165); |
| 217 | outR.update(*interest1); |
| 218 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 219 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 220 | auto interest2 = makeInterest("/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 221 | 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 | |
| 237 | BOOST_AUTO_TEST_SUITE_END() // TestPitEntry |
| 238 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 239 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 240 | } // namespace nfd::tests |