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 | { |
| 69 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
| 70 | shared_ptr<Face> face2 = make_shared<DummyFace>(); |
| 71 | Name name("ndn:/KuYfjtRq"); |
| 72 | shared_ptr<Interest> interest = makeInterest(name); |
| 73 | shared_ptr<Interest> interest1 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 74 | interest1->setInterestLifetime(2528_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 75 | interest1->setNonce(25559); |
| 76 | shared_ptr<Interest> interest2 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 77 | interest2->setInterestLifetime(6464_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 78 | interest2->setNonce(19004); |
| 79 | shared_ptr<Interest> interest3 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 80 | interest3->setInterestLifetime(3585_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 81 | interest3->setNonce(24216); |
| 82 | shared_ptr<Interest> interest4 = makeInterest(name); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame^] | 83 | interest4->setInterestLifetime(8795_ms); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 84 | interest4->setNonce(17365); |
| 85 | |
| 86 | Entry entry(*interest); |
| 87 | |
| 88 | BOOST_CHECK_EQUAL(entry.getInterest().getName(), name); |
| 89 | BOOST_CHECK_EQUAL(entry.getName(), name); |
| 90 | |
| 91 | const InRecordCollection& inRecords1 = entry.getInRecords(); |
| 92 | BOOST_CHECK_EQUAL(inRecords1.size(), 0); |
| 93 | const OutRecordCollection& outRecords1 = entry.getOutRecords(); |
| 94 | BOOST_CHECK_EQUAL(outRecords1.size(), 0); |
| 95 | |
| 96 | // insert in-record |
| 97 | time::steady_clock::TimePoint before1 = time::steady_clock::now(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 98 | InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, 0, *interest1); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 99 | time::steady_clock::TimePoint after1 = time::steady_clock::now(); |
| 100 | const InRecordCollection& inRecords2 = entry.getInRecords(); |
| 101 | BOOST_CHECK_EQUAL(inRecords2.size(), 1); |
| 102 | BOOST_CHECK(in1 == inRecords2.begin()); |
| 103 | BOOST_CHECK_EQUAL(&in1->getFace(), face1.get()); |
| 104 | BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce()); |
| 105 | BOOST_CHECK_GE(in1->getLastRenewed(), before1); |
| 106 | BOOST_CHECK_LE(in1->getLastRenewed(), after1); |
| 107 | BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() |
| 108 | - 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 |
| 113 | time::steady_clock::TimePoint 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); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 115 | time::steady_clock::TimePoint after2 = time::steady_clock::now(); |
| 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); |
| 123 | BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() |
| 124 | - interest1->getInterestLifetime(), |
| 125 | (after2 - before2)); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 126 | BOOST_CHECK(out1 == entry.getOutRecord(*face1, 0)); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 127 | |
| 128 | // update in-record |
| 129 | time::steady_clock::TimePoint before3 = time::steady_clock::now(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 130 | InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, 0, *interest2); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 131 | time::steady_clock::TimePoint after3 = time::steady_clock::now(); |
| 132 | const InRecordCollection& inRecords3 = entry.getInRecords(); |
| 133 | BOOST_CHECK_EQUAL(inRecords3.size(), 1); |
| 134 | BOOST_CHECK(in2 == inRecords3.begin()); |
| 135 | BOOST_CHECK_EQUAL(&in2->getFace(), face1.get()); |
| 136 | BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce()); |
| 137 | BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() |
| 138 | - interest2->getInterestLifetime(), |
| 139 | (after3 - before3)); |
| 140 | |
| 141 | // insert another in-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 142 | InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, 0, *interest3); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 143 | const InRecordCollection& inRecords4 = entry.getInRecords(); |
| 144 | BOOST_CHECK_EQUAL(inRecords4.size(), 2); |
| 145 | BOOST_CHECK_EQUAL(&in3->getFace(), face2.get()); |
| 146 | |
| 147 | // get in-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 148 | InRecordCollection::iterator in4 = entry.getInRecord(*face1, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 149 | BOOST_REQUIRE(in4 != entry.in_end()); |
| 150 | BOOST_CHECK_EQUAL(&in4->getFace(), face1.get()); |
| 151 | |
| 152 | // clear in-records |
| 153 | entry.clearInRecords(); |
| 154 | const InRecordCollection& inRecords5 = entry.getInRecords(); |
| 155 | BOOST_CHECK_EQUAL(inRecords5.size(), 0); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 156 | BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 157 | |
| 158 | // insert another out-record |
| 159 | OutRecordCollection::iterator out2 = |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 160 | entry.insertOrUpdateOutRecord(*face2, 0, *interest4); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 161 | const OutRecordCollection& outRecords3 = entry.getOutRecords(); |
| 162 | BOOST_CHECK_EQUAL(outRecords3.size(), 2); |
| 163 | BOOST_CHECK_EQUAL(&out2->getFace(), face2.get()); |
| 164 | |
| 165 | // get out-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 166 | OutRecordCollection::iterator out3 = entry.getOutRecord(*face1, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 167 | BOOST_REQUIRE(out3 != entry.out_end()); |
| 168 | BOOST_CHECK_EQUAL(&out3->getFace(), face1.get()); |
| 169 | |
| 170 | // delete out-record |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 171 | entry.deleteOutRecord(*face2, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 172 | const OutRecordCollection& outRecords4 = entry.getOutRecords(); |
| 173 | BOOST_REQUIRE_EQUAL(outRecords4.size(), 1); |
| 174 | BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get()); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 175 | BOOST_CHECK(entry.getOutRecord(*face2, 0) == entry.out_end()); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | BOOST_AUTO_TEST_CASE(Lifetime) |
| 179 | { |
| 180 | shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6"); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 181 | |
| 182 | shared_ptr<Face> face = make_shared<DummyFace>(); |
| 183 | Entry entry(*interest); |
| 184 | |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 185 | InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(*face, 0, *interest); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 186 | BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now()); |
| 187 | |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 188 | OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(*face, 0, *interest); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 189 | BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now()); |
| 190 | } |
| 191 | |
| 192 | BOOST_AUTO_TEST_CASE(OutRecordNack) |
| 193 | { |
| 194 | shared_ptr<Face> face1 = make_shared<DummyFace>(); |
ashiqopu | d3ae85d | 2019-02-17 02:29:55 +0000 | [diff] [blame] | 195 | OutRecord outR(*face1, 0); |
Junxiao Shi | a274292 | 2016-11-17 22:53:23 +0000 | [diff] [blame] | 196 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 197 | |
| 198 | shared_ptr<Interest> interest1 = makeInterest("ndn:/uWiapGjYL"); |
| 199 | interest1->setNonce(165); |
| 200 | outR.update(*interest1); |
| 201 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 202 | |
| 203 | shared_ptr<Interest> interest2 = makeInterest("ndn:/uWiapGjYL"); |
| 204 | interest2->setNonce(996); |
| 205 | lp::Nack nack2(*interest2); |
| 206 | nack2.setReason(lp::NackReason::CONGESTION); |
| 207 | BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false); |
| 208 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 209 | |
| 210 | lp::Nack nack1(*interest1); |
| 211 | nack1.setReason(lp::NackReason::DUPLICATE); |
| 212 | BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true); |
| 213 | BOOST_REQUIRE(outR.getIncomingNack() != nullptr); |
| 214 | BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE); |
| 215 | |
| 216 | outR.clearIncomingNack(); |
| 217 | BOOST_CHECK(outR.getIncomingNack() == nullptr); |
| 218 | } |
| 219 | |
| 220 | BOOST_AUTO_TEST_SUITE_END() // TestPitEntry |
| 221 | BOOST_AUTO_TEST_SUITE_END() // Table |
| 222 | |
| 223 | } // namespace tests |
| 224 | } // namespace pit |
| 225 | } // namespace nfd |