Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [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 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 26 | #include "fw/algorithm.hpp" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [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" |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 30 | #include "tests/daemon/face/dummy-face.hpp" |
| 31 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 32 | namespace nfd::tests { |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 33 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 34 | using namespace nfd::fw; |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 35 | |
| 36 | BOOST_AUTO_TEST_SUITE(Fw) |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestAlgorithm, GlobalIoFixture) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 39 | class ScopeControlFixture : public GlobalIoFixture |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 40 | { |
| 41 | protected: |
| 42 | ScopeControlFixture() |
| 43 | : nonLocalFace1(make_shared<DummyFace>("dummy://1", "dummy://1", ndn::nfd::FACE_SCOPE_NON_LOCAL)) |
| 44 | , nonLocalFace2(make_shared<DummyFace>("dummy://2", "dummy://2", ndn::nfd::FACE_SCOPE_NON_LOCAL)) |
| 45 | , localFace3(make_shared<DummyFace>("dummy://3", "dummy://3", ndn::nfd::FACE_SCOPE_LOCAL)) |
| 46 | , localFace4(make_shared<DummyFace>("dummy://4", "dummy://4", ndn::nfd::FACE_SCOPE_LOCAL)) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | protected: |
| 51 | shared_ptr<Face> nonLocalFace1; |
| 52 | shared_ptr<Face> nonLocalFace2; |
| 53 | shared_ptr<Face> localFace3; |
| 54 | shared_ptr<Face> localFace4; |
| 55 | }; |
| 56 | |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 57 | BOOST_FIXTURE_TEST_SUITE(WouldViolateScope, ScopeControlFixture) |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(Unrestricted) |
| 60 | { |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 61 | auto interest = makeInterest("ndn:/ieWRzDsCu"); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 62 | |
| 63 | BOOST_CHECK_EQUAL(wouldViolateScope(*nonLocalFace1, *interest, *nonLocalFace2), false); |
| 64 | BOOST_CHECK_EQUAL(wouldViolateScope(*nonLocalFace1, *interest, *localFace4), false); |
| 65 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *nonLocalFace2), false); |
| 66 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *localFace4), false); |
| 67 | } |
| 68 | |
| 69 | BOOST_AUTO_TEST_CASE(Localhost) |
| 70 | { |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 71 | auto interest = makeInterest("ndn:/localhost/5n1LzIt3"); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 72 | |
| 73 | // /localhost Interests from non-local faces should be rejected by incoming Interest pipeline, |
| 74 | // and are not tested here. |
| 75 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *nonLocalFace2), true); |
| 76 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *localFace4), false); |
| 77 | } |
| 78 | |
| 79 | BOOST_AUTO_TEST_CASE(Localhop) |
| 80 | { |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 81 | auto interest = makeInterest("ndn:/localhop/YcIKWCRYJ"); |
Junxiao Shi | 00dc914 | 2016-11-21 14:23:12 +0000 | [diff] [blame] | 82 | |
| 83 | BOOST_CHECK_EQUAL(wouldViolateScope(*nonLocalFace1, *interest, *nonLocalFace2), true); |
| 84 | BOOST_CHECK_EQUAL(wouldViolateScope(*nonLocalFace1, *interest, *localFace4), false); |
| 85 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *nonLocalFace2), false); |
| 86 | BOOST_CHECK_EQUAL(wouldViolateScope(*localFace3, *interest, *localFace4), false); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_SUITE_END() // WouldViolateScope |
| 90 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(Nonce) |
| 92 | { |
| 93 | auto face1 = make_shared<DummyFace>(); |
| 94 | auto face2 = make_shared<DummyFace>(); |
| 95 | |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 96 | auto interest = makeInterest("ndn:/qtCQ7I1c"); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 97 | interest->setNonce(25559); |
| 98 | |
| 99 | pit::Entry entry0(*interest); |
| 100 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face1), DUPLICATE_NONCE_NONE); |
| 101 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face2), DUPLICATE_NONCE_NONE); |
| 102 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 103 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 104 | |
| 105 | pit::Entry entry1(*interest); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 106 | entry1.insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face1), DUPLICATE_NONCE_IN_SAME); |
| 108 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face2), DUPLICATE_NONCE_IN_OTHER); |
| 109 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 110 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 111 | |
| 112 | pit::Entry entry2(*interest); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 113 | entry2.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face1), DUPLICATE_NONCE_OUT_SAME); |
| 115 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face2), DUPLICATE_NONCE_OUT_OTHER); |
| 116 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 117 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 118 | |
| 119 | pit::Entry entry3(*interest); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 120 | entry3.insertOrUpdateInRecord(*face1, *interest); |
| 121 | entry3.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face1), |
| 123 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_OUT_SAME); |
| 124 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face2), |
| 125 | DUPLICATE_NONCE_IN_OTHER | DUPLICATE_NONCE_OUT_OTHER); |
| 126 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 127 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 128 | |
| 129 | pit::Entry entry4(*interest); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 130 | entry4.insertOrUpdateInRecord(*face1, *interest); |
| 131 | entry4.insertOrUpdateInRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 132 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face1), |
| 133 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 134 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face2), |
| 135 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 136 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 137 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 138 | |
| 139 | pit::Entry entry5(*interest); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 140 | entry5.insertOrUpdateOutRecord(*face1, *interest); |
| 141 | entry5.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 142 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face1), |
| 143 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 144 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face2), |
| 145 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 146 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 147 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 148 | } |
| 149 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 150 | BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, GlobalIoTimeFixture) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 151 | { |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 152 | auto face1 = make_shared<DummyFace>(); |
| 153 | auto face2 = make_shared<DummyFace>(); |
| 154 | auto face3 = make_shared<DummyFace>(); |
| 155 | |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 156 | auto interest = makeInterest("/totzXG0d", false, std::nullopt, 29321); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 157 | pit::Entry entry(*interest); |
| 158 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 159 | |
| 160 | // Interest-Data |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 161 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 162 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 163 | entry.deleteOutRecord(*face1); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 165 | |
| 166 | // Interest-Nack |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 167 | entry.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 169 | pit::OutRecordCollection::iterator outR = entry.getOutRecord(*face2); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 170 | BOOST_REQUIRE(outR != entry.out_end()); |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 171 | lp::Nack nack = makeNack(*interest, lp::NackReason::DUPLICATE); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 172 | bool isNackAccepted = outR->setIncomingNack(nack); // Nack arrival |
| 173 | BOOST_REQUIRE(isNackAccepted); |
| 174 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 175 | |
| 176 | // Interest-timeout |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 177 | entry.insertOrUpdateOutRecord(*face3, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 178 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 179 | this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2); |
| 180 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 183 | BOOST_FIXTURE_TEST_CASE(GetLastOutgoing, GlobalIoTimeFixture) |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 184 | { |
| 185 | auto face1 = make_shared<DummyFace>(); |
| 186 | auto face2 = make_shared<DummyFace>(); |
| 187 | |
Junxiao Shi | 9d72785 | 2019-05-14 13:44:22 -0600 | [diff] [blame] | 188 | auto interest = makeInterest("ndn:/c1I7QCtc"); |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 189 | pit::Entry entry(*interest); |
| 190 | |
Davide Pesavento | 412c982 | 2021-07-02 00:21:05 -0400 | [diff] [blame] | 191 | auto before = time::steady_clock::now(); |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 192 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 193 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Davide Pesavento | 14e71f0 | 2019-03-28 17:35:25 -0400 | [diff] [blame] | 194 | this->advanceClocks(1_s); |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 195 | |
| 196 | BOOST_CHECK_EQUAL(getLastOutgoing(entry), before); |
| 197 | |
Md Ashiqur Rahman | c88d2d4 | 2019-08-28 20:19:47 +0000 | [diff] [blame] | 198 | entry.insertOrUpdateOutRecord(*face2, *interest); |
Ashlesh Gawande | 9001599 | 2017-07-11 17:25:48 -0500 | [diff] [blame] | 199 | |
| 200 | BOOST_CHECK_EQUAL(getLastOutgoing(entry), time::steady_clock::now()); |
| 201 | } |
| 202 | |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 203 | BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm |
| 204 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 205 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 206 | } // namespace nfd::tests |