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 | |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 36 | namespace nfd { |
| 37 | namespace pit { |
| 38 | namespace tests { |
| 39 | |
| 40 | using namespace nfd::tests; |
| 41 | |
| 42 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 43 | BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture) |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 44 | |
| 45 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1) |
| 46 | BOOST_AUTO_TEST_CASE(CanMatch) |
| 47 | { |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 48 | auto interest0 = makeInterest("/A"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 49 | Entry entry(*interest0); |
| 50 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 51 | auto interest1 = makeInterest("/B"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false); |
| 53 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 54 | auto interest2 = makeInterest("/A", false, nullopt, 27956); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true); |
| 56 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 57 | auto interest3 = makeInterest("/A", false, 6210_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true); |
| 59 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 60 | auto interest4 = makeInterest("/A"); |
Junxiao Shi | 727b610 | 2022-01-11 18:40:04 +0000 | [diff] [blame^] | 61 | interest4->setForwardingHint({"/telia/terabits", "/ucla/cs"}); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162 |
| 63 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 64 | auto interest5 = makeInterest("/A", true); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(InOutRecords) |
| 69 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 70 | auto face1 = make_shared<DummyFace>(); |
| 71 | auto face2 = make_shared<DummyFace>(); |
| 72 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 73 | Name name("/KuYfjtRq"); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 74 | auto interest = makeInterest(name); |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 75 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 79 | |
| 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 91 | auto before1 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 92 | InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 93 | auto after1 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 94 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 101 | BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(), |
| 102 | after1 - before1); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 103 | BOOST_CHECK(in1 == entry.getInRecord(*face1)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 104 | |
| 105 | // insert out-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 106 | auto before2 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 107 | OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 108 | auto after2 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 109 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 116 | BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(), |
| 117 | after2 - before2); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 118 | BOOST_CHECK(out1 == entry.getOutRecord(*face1)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 119 | |
| 120 | // update in-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 121 | auto before3 = time::steady_clock::now(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 122 | InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 123 | auto after3 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 124 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 129 | BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(), |
| 130 | after3 - before3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 131 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 132 | // insert another in-record |
| 133 | InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 134 | 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 Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 139 | InRecordCollection::iterator in4 = entry.getInRecord(*face1); |
| 140 | BOOST_REQUIRE(in4 != entry.in_end()); |
| 141 | BOOST_CHECK_EQUAL(&in4->getFace(), face1.get()); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 142 | |
| 143 | // delete in-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 144 | entry.deleteInRecord(*face1); |
| 145 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 1); |
| 146 | BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 147 | |
| 148 | // clear in-records |
| 149 | entry.clearInRecords(); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 151 | BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end()); |
| 152 | BOOST_CHECK(entry.getInRecord(*face2) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 153 | |
| 154 | // insert another out-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 155 | OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, *interest4); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 156 | 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 Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 161 | OutRecordCollection::iterator out3 = entry.getOutRecord(*face1); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 162 | BOOST_REQUIRE(out3 != entry.out_end()); |
| 163 | BOOST_CHECK_EQUAL(&out3->getFace(), face1.get()); |
| 164 | |
| 165 | // delete out-record |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 166 | entry.deleteOutRecord(*face2); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 167 | const OutRecordCollection& outRecords4 = entry.getOutRecords(); |
| 168 | BOOST_REQUIRE_EQUAL(outRecords4.size(), 1); |
| 169 | BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get()); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 170 | BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 173 | const 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 | |
| 182 | BOOST_DATA_TEST_CASE(Lifetime, bdata::make(lifetimes), lifetime) |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 183 | { |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 184 | auto interest = makeInterest("/7oIEurbgy6"); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 185 | 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 Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 197 | auto face = make_shared<DummyFace>(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 198 | Entry entry(*interest); |
| 199 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 200 | auto inIt = entry.insertOrUpdateInRecord(*face, *interest); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 201 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 204 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 205 | auto outIt = entry.insertOrUpdateOutRecord(*face, *interest); |
Junxiao Shi | e1cf4ba | 2020-06-19 16:40:53 -0600 | [diff] [blame] | 206 | 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 Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | BOOST_AUTO_TEST_CASE(OutRecordNack) |
| 212 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 213 | auto face1 = make_shared<DummyFace>(); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 214 | OutRecord outR(*face1); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 215 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 216 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 217 | auto interest1 = makeInterest("/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 218 | interest1->setNonce(165); |
| 219 | outR.update(*interest1); |
| 220 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 221 | |
Junxiao Shi | 25d9728 | 2019-05-14 13:44:46 -0600 | [diff] [blame] | 222 | auto interest2 = makeInterest("/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 223 | 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 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() // TestPitEntry |
| 240 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 241 | |
| 242 | } // namespace tests |
| 243 | } // namespace pit |
| 244 | } // namespace nfd |