Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
| 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 "fw/pit-algorithm.hpp" |
| 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) |
| 38 | BOOST_FIXTURE_TEST_SUITE(TestPitAlgorithm, BaseFixture) |
| 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 | |
| 58 | BOOST_FIXTURE_TEST_SUITE(ViolatesScope, ScopeControlFixture) |
| 59 | |
| 60 | BOOST_AUTO_TEST_CASE(Unrestricted) |
| 61 | { |
| 62 | shared_ptr<Interest> interest = makeInterest("ndn:/ieWRzDsCu"); |
| 63 | pit::Entry entry(*interest); |
| 64 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 65 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 67 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 68 | } |
| 69 | |
| 70 | BOOST_AUTO_TEST_CASE(Localhost) |
| 71 | { |
| 72 | shared_ptr<Interest> interest = makeInterest("ndn:/localhost/5n1LzIt3"); |
| 73 | pit::Entry entry(*interest); |
| 74 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 75 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true); |
| 77 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 78 | } |
| 79 | |
| 80 | BOOST_AUTO_TEST_CASE(LocalhopFromLocal) |
| 81 | { |
| 82 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/YcIKWCRYJ"); |
| 83 | pit::Entry entry(*interest); |
| 84 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 85 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 87 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 88 | } |
| 89 | |
| 90 | BOOST_AUTO_TEST_CASE(LocalhopFromNonLocal) |
| 91 | { |
| 92 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/x5uFr5IpqY"); |
| 93 | pit::Entry entry(*interest); |
| 94 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 95 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true); |
| 97 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 98 | } |
| 99 | |
| 100 | BOOST_AUTO_TEST_CASE(LocalhopFromLocalAndNonLocal) |
| 101 | { |
| 102 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/gNn2MJAXt"); |
| 103 | pit::Entry entry(*interest); |
| 104 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 105 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
| 106 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 107 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 108 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 109 | } |
| 110 | |
| 111 | BOOST_AUTO_TEST_SUITE_END() // ViolatesScope |
| 112 | |
| 113 | BOOST_AUTO_TEST_CASE(CanForwardToLegacy) |
| 114 | { |
| 115 | shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG"); |
| 116 | pit::Entry entry(*interest); |
| 117 | |
| 118 | auto face1 = make_shared<DummyFace>(); |
| 119 | auto face2 = make_shared<DummyFace>(); |
| 120 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 121 | entry.insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false); |
| 123 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 124 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 125 | entry.insertOrUpdateInRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), true); |
| 127 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 128 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 129 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false); |
| 131 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(Nonce) |
| 135 | { |
| 136 | auto face1 = make_shared<DummyFace>(); |
| 137 | auto face2 = make_shared<DummyFace>(); |
| 138 | |
| 139 | shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c"); |
| 140 | interest->setNonce(25559); |
| 141 | |
| 142 | pit::Entry entry0(*interest); |
| 143 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face1), DUPLICATE_NONCE_NONE); |
| 144 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face2), DUPLICATE_NONCE_NONE); |
| 145 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 146 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 147 | |
| 148 | pit::Entry entry1(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 149 | entry1.insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face1), DUPLICATE_NONCE_IN_SAME); |
| 151 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face2), DUPLICATE_NONCE_IN_OTHER); |
| 152 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 153 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 154 | |
| 155 | pit::Entry entry2(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 156 | entry2.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 157 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face1), DUPLICATE_NONCE_OUT_SAME); |
| 158 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face2), DUPLICATE_NONCE_OUT_OTHER); |
| 159 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 160 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 161 | |
| 162 | pit::Entry entry3(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 163 | entry3.insertOrUpdateInRecord(*face1, *interest); |
| 164 | entry3.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 165 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face1), |
| 166 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_OUT_SAME); |
| 167 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face2), |
| 168 | DUPLICATE_NONCE_IN_OTHER | DUPLICATE_NONCE_OUT_OTHER); |
| 169 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 170 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 171 | |
| 172 | pit::Entry entry4(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 173 | entry4.insertOrUpdateInRecord(*face1, *interest); |
| 174 | entry4.insertOrUpdateInRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 175 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face1), |
| 176 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 177 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face2), |
| 178 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 179 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 180 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 181 | |
| 182 | pit::Entry entry5(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 183 | entry5.insertOrUpdateOutRecord(*face1, *interest); |
| 184 | entry5.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face1), |
| 186 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 187 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face2), |
| 188 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 189 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 190 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 191 | } |
| 192 | |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 193 | BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, UnitTestTimeFixture) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 194 | { |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 195 | auto face1 = make_shared<DummyFace>(); |
| 196 | auto face2 = make_shared<DummyFace>(); |
| 197 | auto face3 = make_shared<DummyFace>(); |
| 198 | |
| 199 | shared_ptr<Interest> interest = makeInterest("ndn:/totzXG0d"); |
| 200 | interest->setNonce(29321); |
| 201 | |
| 202 | pit::Entry entry(*interest); |
| 203 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 204 | |
| 205 | // Interest-Data |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 206 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 207 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 208 | entry.deleteOutRecord(*face1); |
| 209 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 210 | |
| 211 | // Interest-Nack |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 212 | entry.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 213 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 214 | pit::OutRecordCollection::iterator outR = entry.getOutRecord(*face2); |
| 215 | BOOST_REQUIRE(outR != entry.out_end()); |
| 216 | lp::Nack nack = makeNack("ndn:/totzXG0d", 29321, lp::NackReason::DUPLICATE); |
| 217 | bool isNackAccepted = outR->setIncomingNack(nack); // Nack arrival |
| 218 | BOOST_REQUIRE(isNackAccepted); |
| 219 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 220 | |
| 221 | // Interest-timeout |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 222 | entry.insertOrUpdateOutRecord(*face3, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 223 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 224 | this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2); |
| 225 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm |
| 229 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 230 | |
| 231 | } // namespace tests |
| 232 | } // namespace fw |
| 233 | } // namespace nfd |