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 | |
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_FIXTURE_TEST_SUITE(ViolatesScope, ScopeControlFixture) |
| 93 | |
| 94 | BOOST_AUTO_TEST_CASE(Unrestricted) |
| 95 | { |
| 96 | shared_ptr<Interest> interest = makeInterest("ndn:/ieWRzDsCu"); |
| 97 | pit::Entry entry(*interest); |
| 98 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 99 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 101 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(Localhost) |
| 105 | { |
| 106 | shared_ptr<Interest> interest = makeInterest("ndn:/localhost/5n1LzIt3"); |
| 107 | pit::Entry entry(*interest); |
| 108 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 109 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true); |
| 111 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(LocalhopFromLocal) |
| 115 | { |
| 116 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/YcIKWCRYJ"); |
| 117 | pit::Entry entry(*interest); |
| 118 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 119 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 120 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 121 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 122 | } |
| 123 | |
| 124 | BOOST_AUTO_TEST_CASE(LocalhopFromNonLocal) |
| 125 | { |
| 126 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/x5uFr5IpqY"); |
| 127 | pit::Entry entry(*interest); |
| 128 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 129 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true); |
| 131 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(LocalhopFromLocalAndNonLocal) |
| 135 | { |
| 136 | shared_ptr<Interest> interest = makeInterest("ndn:/localhop/gNn2MJAXt"); |
| 137 | pit::Entry entry(*interest); |
| 138 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 139 | entry.insertOrUpdateInRecord(*nonLocalFace1, *interest); |
| 140 | entry.insertOrUpdateInRecord(*localFace3, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 141 | BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false); |
| 142 | BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false); |
| 143 | } |
| 144 | |
| 145 | BOOST_AUTO_TEST_SUITE_END() // ViolatesScope |
| 146 | |
| 147 | BOOST_AUTO_TEST_CASE(CanForwardToLegacy) |
| 148 | { |
| 149 | shared_ptr<Interest> interest = makeInterest("ndn:/WDsuBLIMG"); |
| 150 | pit::Entry entry(*interest); |
| 151 | |
| 152 | auto face1 = make_shared<DummyFace>(); |
| 153 | auto face2 = make_shared<DummyFace>(); |
| 154 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 155 | entry.insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false); |
| 157 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 158 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 159 | entry.insertOrUpdateInRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), true); |
| 161 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 162 | |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 163 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false); |
| 165 | BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_CASE(Nonce) |
| 169 | { |
| 170 | auto face1 = make_shared<DummyFace>(); |
| 171 | auto face2 = make_shared<DummyFace>(); |
| 172 | |
| 173 | shared_ptr<Interest> interest = makeInterest("ndn:/qtCQ7I1c"); |
| 174 | interest->setNonce(25559); |
| 175 | |
| 176 | pit::Entry entry0(*interest); |
| 177 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face1), DUPLICATE_NONCE_NONE); |
| 178 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face2), DUPLICATE_NONCE_NONE); |
| 179 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 180 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 181 | |
| 182 | pit::Entry entry1(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 183 | entry1.insertOrUpdateInRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face1), DUPLICATE_NONCE_IN_SAME); |
| 185 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face2), DUPLICATE_NONCE_IN_OTHER); |
| 186 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 187 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 188 | |
| 189 | pit::Entry entry2(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 190 | entry2.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 191 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face1), DUPLICATE_NONCE_OUT_SAME); |
| 192 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face2), DUPLICATE_NONCE_OUT_OTHER); |
| 193 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 194 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 195 | |
| 196 | pit::Entry entry3(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 197 | entry3.insertOrUpdateInRecord(*face1, *interest); |
| 198 | entry3.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 199 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face1), |
| 200 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_OUT_SAME); |
| 201 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face2), |
| 202 | DUPLICATE_NONCE_IN_OTHER | DUPLICATE_NONCE_OUT_OTHER); |
| 203 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 204 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 205 | |
| 206 | pit::Entry entry4(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 207 | entry4.insertOrUpdateInRecord(*face1, *interest); |
| 208 | entry4.insertOrUpdateInRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 209 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face1), |
| 210 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 211 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face2), |
| 212 | DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER); |
| 213 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 214 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 215 | |
| 216 | pit::Entry entry5(*interest); |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 217 | entry5.insertOrUpdateOutRecord(*face1, *interest); |
| 218 | entry5.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face1), |
| 220 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 221 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face2), |
| 222 | DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER); |
| 223 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face1), DUPLICATE_NONCE_NONE); |
| 224 | BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face2), DUPLICATE_NONCE_NONE); |
| 225 | } |
| 226 | |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 227 | BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, UnitTestTimeFixture) |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 228 | { |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 229 | auto face1 = make_shared<DummyFace>(); |
| 230 | auto face2 = make_shared<DummyFace>(); |
| 231 | auto face3 = make_shared<DummyFace>(); |
| 232 | |
| 233 | shared_ptr<Interest> interest = makeInterest("ndn:/totzXG0d"); |
| 234 | interest->setNonce(29321); |
| 235 | |
| 236 | pit::Entry entry(*interest); |
| 237 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 238 | |
| 239 | // Interest-Data |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 240 | entry.insertOrUpdateOutRecord(*face1, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 242 | entry.deleteOutRecord(*face1); |
| 243 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 244 | |
| 245 | // Interest-Nack |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 246 | entry.insertOrUpdateOutRecord(*face2, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 247 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 248 | pit::OutRecordCollection::iterator outR = entry.getOutRecord(*face2); |
| 249 | BOOST_REQUIRE(outR != entry.out_end()); |
| 250 | lp::Nack nack = makeNack("ndn:/totzXG0d", 29321, lp::NackReason::DUPLICATE); |
| 251 | bool isNackAccepted = outR->setIncomingNack(nack); // Nack arrival |
| 252 | BOOST_REQUIRE(isNackAccepted); |
| 253 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
| 254 | |
| 255 | // Interest-timeout |
Junxiao Shi | 9cff779 | 2016-08-01 21:45:11 +0000 | [diff] [blame] | 256 | entry.insertOrUpdateOutRecord(*face3, *interest); |
Junxiao Shi | 8744c97 | 2016-04-06 10:33:46 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true); |
| 258 | this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2); |
| 259 | BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false); |
Junxiao Shi | fef73e4 | 2016-03-29 14:15:05 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm |
| 263 | BOOST_AUTO_TEST_SUITE_END() // Fw |
| 264 | |
| 265 | } // namespace tests |
| 266 | } // namespace fw |
| 267 | } // namespace nfd |