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 | /* |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, 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 | |
| 32 | namespace nfd { |
| 33 | namespace pit { |
| 34 | namespace tests { |
| 35 | |
| 36 | using namespace nfd::tests; |
| 37 | |
| 38 | BOOST_AUTO_TEST_SUITE(Table) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture) |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 40 | |
| 41 | BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1) |
| 42 | BOOST_AUTO_TEST_CASE(CanMatch) |
| 43 | { |
| 44 | shared_ptr<Interest> interest0 = makeInterest("/A"); |
| 45 | Entry entry(*interest0); |
| 46 | |
| 47 | shared_ptr<Interest> interest1 = makeInterest("/B"); |
| 48 | BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false); |
| 49 | |
| 50 | shared_ptr<Interest> interest2 = makeInterest("/A"); |
| 51 | interest2->setNonce(27956); |
| 52 | BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true); |
| 53 | |
| 54 | shared_ptr<Interest> interest3 = makeInterest("/A"); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 55 | interest3->setInterestLifetime(6210_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true); |
| 57 | |
| 58 | shared_ptr<Interest> interest4 = makeInterest("/A"); |
Junxiao Shi | fc2e13d | 2017-07-25 02:08:48 +0000 | [diff] [blame] | 59 | interest4->setForwardingHint({{10, "/telia/terabits"}, {20, "/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 | |
| 62 | shared_ptr<Interest> interest5 = makeInterest("/A"); |
| 63 | interest5->setMaxSuffixComponents(21); |
| 64 | BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false); |
| 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(InOutRecords) |
| 68 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 69 | auto face1 = make_shared<DummyFace>(); |
| 70 | auto face2 = make_shared<DummyFace>(); |
| 71 | |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 72 | Name name("ndn:/KuYfjtRq"); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 73 | auto interest = makeInterest(name); |
| 74 | auto interest1 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 75 | interest1->setInterestLifetime(2528_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 76 | interest1->setNonce(25559); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 77 | auto interest2 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 78 | interest2->setInterestLifetime(6464_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 79 | interest2->setNonce(19004); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 80 | auto interest3 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 81 | interest3->setInterestLifetime(3585_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 82 | interest3->setNonce(24216); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 83 | auto interest4 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 84 | interest4->setInterestLifetime(8795_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 85 | interest4->setNonce(17365); |
| 86 | |
| 87 | Entry entry(*interest); |
| 88 | |
| 89 | BOOST_CHECK_EQUAL(entry.getInterest().getName(), name); |
| 90 | BOOST_CHECK_EQUAL(entry.getName(), name); |
| 91 | |
| 92 | const InRecordCollection& inRecords1 = entry.getInRecords(); |
| 93 | BOOST_CHECK_EQUAL(inRecords1.size(), 0); |
| 94 | const OutRecordCollection& outRecords1 = entry.getOutRecords(); |
| 95 | BOOST_CHECK_EQUAL(outRecords1.size(), 0); |
| 96 | |
| 97 | // insert in-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 98 | auto before1 = time::steady_clock::now(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 99 | InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, 0, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 100 | auto after1 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 101 | const InRecordCollection& inRecords2 = entry.getInRecords(); |
| 102 | BOOST_CHECK_EQUAL(inRecords2.size(), 1); |
| 103 | BOOST_CHECK(in1 == inRecords2.begin()); |
| 104 | BOOST_CHECK_EQUAL(&in1->getFace(), face1.get()); |
| 105 | BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce()); |
| 106 | BOOST_CHECK_GE(in1->getLastRenewed(), before1); |
| 107 | BOOST_CHECK_LE(in1->getLastRenewed(), after1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 108 | BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(), |
| 109 | after1 - before1); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 110 | BOOST_CHECK(in1 == entry.getInRecord(*face1, 0)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 111 | |
| 112 | // insert out-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 113 | auto before2 = time::steady_clock::now(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 114 | OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, 0, *interest1); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 115 | auto after2 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 116 | const OutRecordCollection& outRecords2 = entry.getOutRecords(); |
| 117 | BOOST_CHECK_EQUAL(outRecords2.size(), 1); |
| 118 | BOOST_CHECK(out1 == outRecords2.begin()); |
| 119 | BOOST_CHECK_EQUAL(&out1->getFace(), face1.get()); |
| 120 | BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce()); |
| 121 | BOOST_CHECK_GE(out1->getLastRenewed(), before2); |
| 122 | BOOST_CHECK_LE(out1->getLastRenewed(), after2); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 123 | BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(), |
| 124 | after2 - before2); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 125 | BOOST_CHECK(out1 == entry.getOutRecord(*face1, 0)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 126 | |
| 127 | // update in-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 128 | auto before3 = time::steady_clock::now(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 129 | InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, 0, *interest2); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 130 | auto after3 = time::steady_clock::now(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 131 | const InRecordCollection& inRecords3 = entry.getInRecords(); |
| 132 | BOOST_CHECK_EQUAL(inRecords3.size(), 1); |
| 133 | BOOST_CHECK(in2 == inRecords3.begin()); |
| 134 | BOOST_CHECK_EQUAL(&in2->getFace(), face1.get()); |
| 135 | BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce()); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 136 | BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(), |
| 137 | after3 - before3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 138 | |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 139 | // insert another in-record (different face) |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 140 | InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, 0, *interest3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 141 | const InRecordCollection& inRecords4 = entry.getInRecords(); |
| 142 | BOOST_CHECK_EQUAL(inRecords4.size(), 2); |
| 143 | BOOST_CHECK_EQUAL(&in3->getFace(), face2.get()); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(in3->getEndpointId(), 0); |
| 145 | |
| 146 | // insert another in-record (different endpoint) |
| 147 | InRecordCollection::iterator in4 = entry.insertOrUpdateInRecord(*face1, 42, *interest4); |
| 148 | const InRecordCollection& inRecords5 = entry.getInRecords(); |
| 149 | BOOST_CHECK_EQUAL(inRecords5.size(), 3); |
| 150 | BOOST_CHECK_EQUAL(&in4->getFace(), face1.get()); |
| 151 | BOOST_CHECK_EQUAL(in4->getEndpointId(), 42); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 152 | |
| 153 | // get in-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 154 | InRecordCollection::iterator in5 = entry.getInRecord(*face1, 0); |
| 155 | BOOST_REQUIRE(in5 != entry.in_end()); |
| 156 | BOOST_CHECK_EQUAL(&in5->getFace(), face1.get()); |
| 157 | BOOST_CHECK_EQUAL(in5->getEndpointId(), 0); |
| 158 | InRecordCollection::iterator in6 = entry.getInRecord(*face1, 42); |
| 159 | BOOST_REQUIRE(in6 != entry.in_end()); |
| 160 | BOOST_CHECK_EQUAL(&in6->getFace(), face1.get()); |
| 161 | BOOST_CHECK_EQUAL(in6->getEndpointId(), 42); |
| 162 | BOOST_CHECK(in5 != in6); |
| 163 | |
| 164 | // delete in-record |
| 165 | entry.deleteInRecord(*face1, 0); |
| 166 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 2); |
| 167 | BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end()); |
| 168 | BOOST_CHECK(entry.getInRecord(*face1, 42) != entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 169 | |
| 170 | // clear in-records |
| 171 | entry.clearInRecords(); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 172 | BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 173 | BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end()); |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 174 | BOOST_CHECK(entry.getInRecord(*face1, 42) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 175 | |
| 176 | // insert another out-record |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 177 | OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, 0, *interest4); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 178 | const OutRecordCollection& outRecords3 = entry.getOutRecords(); |
| 179 | BOOST_CHECK_EQUAL(outRecords3.size(), 2); |
| 180 | BOOST_CHECK_EQUAL(&out2->getFace(), face2.get()); |
| 181 | |
| 182 | // get out-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 183 | OutRecordCollection::iterator out3 = entry.getOutRecord(*face1, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 184 | BOOST_REQUIRE(out3 != entry.out_end()); |
| 185 | BOOST_CHECK_EQUAL(&out3->getFace(), face1.get()); |
| 186 | |
| 187 | // delete out-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 188 | entry.deleteOutRecord(*face2, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 189 | const OutRecordCollection& outRecords4 = entry.getOutRecords(); |
| 190 | BOOST_REQUIRE_EQUAL(outRecords4.size(), 1); |
| 191 | BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get()); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 192 | BOOST_CHECK(entry.getOutRecord(*face2, 0) == entry.out_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | BOOST_AUTO_TEST_CASE(Lifetime) |
| 196 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 197 | auto interest = makeInterest("ndn:/7oIEurbgy6"); |
| 198 | auto face = make_shared<DummyFace>(); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 199 | Entry entry(*interest); |
| 200 | |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 201 | auto inIt = entry.insertOrUpdateInRecord(*face, 0, *interest); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 202 | BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now()); |
| 203 | |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 204 | auto outIt = entry.insertOrUpdateOutRecord(*face, 0, *interest); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 205 | BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now()); |
| 206 | } |
| 207 | |
| 208 | BOOST_AUTO_TEST_CASE(OutRecordNack) |
| 209 | { |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 210 | auto face1 = make_shared<DummyFace>(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 211 | OutRecord outR(*face1, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 212 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 213 | |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 214 | auto interest1 = makeInterest("ndn:/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 215 | interest1->setNonce(165); |
| 216 | outR.update(*interest1); |
| 217 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 218 | |
Davide Pesavento | b31206e | 2019-04-20 22:34:12 -0400 | [diff] [blame] | 219 | auto interest2 = makeInterest("ndn:/uWiapGjYL"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 220 | interest2->setNonce(996); |
| 221 | lp::Nack nack2(*interest2); |
| 222 | nack2.setReason(lp::NackReason::CONGESTION); |
| 223 | BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false); |
| 224 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 225 | |
| 226 | lp::Nack nack1(*interest1); |
| 227 | nack1.setReason(lp::NackReason::DUPLICATE); |
| 228 | BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true); |
| 229 | BOOST_REQUIRE(outR.getIncomingNack() != nullptr); |
| 230 | BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE); |
| 231 | |
| 232 | outR.clearIncomingNack(); |
| 233 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 234 | } |
| 235 | |
| 236 | BOOST_AUTO_TEST_SUITE_END() // TestPitEntry |
| 237 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 238 | |
| 239 | } // namespace tests |
| 240 | } // namespace pit |
| 241 | } // namespace nfd |