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