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