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