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