blob: 4a86d78b87e92288e82d36172aba3f79d65d8760 [file] [log] [blame]
Junxiao Shifef73e42016-03-29 14:15:05 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Ashlesh Gawande90015992017-07-11 17:25:48 -05002/*
Davide Pesavento58091582021-03-05 19:02:48 -05003 * Copyright (c) 2014-2021, Regents of the University of California,
Junxiao Shifef73e42016-03-29 14:15:05 -07004 * 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 Shi00dc9142016-11-21 14:23:12 +000026#include "fw/algorithm.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070027
28#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shifef73e42016-03-29 14:15:05 -070030#include "tests/daemon/face/dummy-face.hpp"
31
32namespace nfd {
33namespace fw {
34namespace tests {
35
36using namespace nfd::tests;
37
38BOOST_AUTO_TEST_SUITE(Fw)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039BOOST_FIXTURE_TEST_SUITE(TestAlgorithm, GlobalIoFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -070040
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041class ScopeControlFixture : public GlobalIoFixture
Junxiao Shifef73e42016-03-29 14:15:05 -070042{
43protected:
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
52protected:
53 shared_ptr<Face> nonLocalFace1;
54 shared_ptr<Face> nonLocalFace2;
55 shared_ptr<Face> localFace3;
56 shared_ptr<Face> localFace4;
57};
58
Junxiao Shi00dc9142016-11-21 14:23:12 +000059BOOST_FIXTURE_TEST_SUITE(WouldViolateScope, ScopeControlFixture)
60
61BOOST_AUTO_TEST_CASE(Unrestricted)
62{
Junxiao Shi9d727852019-05-14 13:44:22 -060063 auto interest = makeInterest("ndn:/ieWRzDsCu");
Junxiao Shi00dc9142016-11-21 14:23:12 +000064
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
71BOOST_AUTO_TEST_CASE(Localhost)
72{
Junxiao Shi9d727852019-05-14 13:44:22 -060073 auto interest = makeInterest("ndn:/localhost/5n1LzIt3");
Junxiao Shi00dc9142016-11-21 14:23:12 +000074
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
81BOOST_AUTO_TEST_CASE(Localhop)
82{
Junxiao Shi9d727852019-05-14 13:44:22 -060083 auto interest = makeInterest("ndn:/localhop/YcIKWCRYJ");
Junxiao Shi00dc9142016-11-21 14:23:12 +000084
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
91BOOST_AUTO_TEST_SUITE_END() // WouldViolateScope
92
Junxiao Shifef73e42016-03-29 14:15:05 -070093BOOST_AUTO_TEST_CASE(Nonce)
94{
95 auto face1 = make_shared<DummyFace>();
96 auto face2 = make_shared<DummyFace>();
97
Junxiao Shi9d727852019-05-14 13:44:22 -060098 auto interest = makeInterest("ndn:/qtCQ7I1c");
Junxiao Shifef73e42016-03-29 14:15:05 -070099 interest->setNonce(25559);
100
101 pit::Entry entry0(*interest);
102 BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face1), DUPLICATE_NONCE_NONE);
103 BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 25559, *face2), DUPLICATE_NONCE_NONE);
104 BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face1), DUPLICATE_NONCE_NONE);
105 BOOST_CHECK_EQUAL(findDuplicateNonce(entry0, 19004, *face2), DUPLICATE_NONCE_NONE);
106
107 pit::Entry entry1(*interest);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000108 entry1.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700109 BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face1), DUPLICATE_NONCE_IN_SAME);
110 BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 25559, *face2), DUPLICATE_NONCE_IN_OTHER);
111 BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face1), DUPLICATE_NONCE_NONE);
112 BOOST_CHECK_EQUAL(findDuplicateNonce(entry1, 19004, *face2), DUPLICATE_NONCE_NONE);
113
114 pit::Entry entry2(*interest);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000115 entry2.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700116 BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face1), DUPLICATE_NONCE_OUT_SAME);
117 BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 25559, *face2), DUPLICATE_NONCE_OUT_OTHER);
118 BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face1), DUPLICATE_NONCE_NONE);
119 BOOST_CHECK_EQUAL(findDuplicateNonce(entry2, 19004, *face2), DUPLICATE_NONCE_NONE);
120
121 pit::Entry entry3(*interest);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000122 entry3.insertOrUpdateInRecord(*face1, *interest);
123 entry3.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700124 BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face1),
125 DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_OUT_SAME);
126 BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 25559, *face2),
127 DUPLICATE_NONCE_IN_OTHER | DUPLICATE_NONCE_OUT_OTHER);
128 BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face1), DUPLICATE_NONCE_NONE);
129 BOOST_CHECK_EQUAL(findDuplicateNonce(entry3, 19004, *face2), DUPLICATE_NONCE_NONE);
130
131 pit::Entry entry4(*interest);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000132 entry4.insertOrUpdateInRecord(*face1, *interest);
133 entry4.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700134 BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face1),
135 DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER);
136 BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 25559, *face2),
137 DUPLICATE_NONCE_IN_SAME | DUPLICATE_NONCE_IN_OTHER);
138 BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face1), DUPLICATE_NONCE_NONE);
139 BOOST_CHECK_EQUAL(findDuplicateNonce(entry4, 19004, *face2), DUPLICATE_NONCE_NONE);
140
141 pit::Entry entry5(*interest);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000142 entry5.insertOrUpdateOutRecord(*face1, *interest);
143 entry5.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700144 BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face1),
145 DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER);
146 BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 25559, *face2),
147 DUPLICATE_NONCE_OUT_SAME | DUPLICATE_NONCE_OUT_OTHER);
148 BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face1), DUPLICATE_NONCE_NONE);
149 BOOST_CHECK_EQUAL(findDuplicateNonce(entry5, 19004, *face2), DUPLICATE_NONCE_NONE);
150}
151
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400152BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, GlobalIoTimeFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -0700153{
Junxiao Shi8744c972016-04-06 10:33:46 -0700154 auto face1 = make_shared<DummyFace>();
155 auto face2 = make_shared<DummyFace>();
156 auto face3 = make_shared<DummyFace>();
157
Junxiao Shi9d727852019-05-14 13:44:22 -0600158 auto interest = makeInterest("/totzXG0d", false, nullopt, 29321);
Junxiao Shi8744c972016-04-06 10:33:46 -0700159 pit::Entry entry(*interest);
160 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
161
162 // Interest-Data
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000163 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700164 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000165 entry.deleteOutRecord(*face1);
Junxiao Shi8744c972016-04-06 10:33:46 -0700166 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
167
168 // Interest-Nack
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000169 entry.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700170 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000171 pit::OutRecordCollection::iterator outR = entry.getOutRecord(*face2);
Junxiao Shi8744c972016-04-06 10:33:46 -0700172 BOOST_REQUIRE(outR != entry.out_end());
Junxiao Shi9d727852019-05-14 13:44:22 -0600173 lp::Nack nack = makeNack(*interest, lp::NackReason::DUPLICATE);
Junxiao Shi8744c972016-04-06 10:33:46 -0700174 bool isNackAccepted = outR->setIncomingNack(nack); // Nack arrival
175 BOOST_REQUIRE(isNackAccepted);
176 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
177
178 // Interest-timeout
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000179 entry.insertOrUpdateOutRecord(*face3, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700180 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
181 this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2);
182 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
Junxiao Shifef73e42016-03-29 14:15:05 -0700183}
184
Davide Pesaventocf7db2f2019-03-24 23:17:28 -0400185BOOST_FIXTURE_TEST_CASE(GetLastOutgoing, GlobalIoTimeFixture)
Ashlesh Gawande90015992017-07-11 17:25:48 -0500186{
187 auto face1 = make_shared<DummyFace>();
188 auto face2 = make_shared<DummyFace>();
189
Junxiao Shi9d727852019-05-14 13:44:22 -0600190 auto interest = makeInterest("ndn:/c1I7QCtc");
Ashlesh Gawande90015992017-07-11 17:25:48 -0500191 pit::Entry entry(*interest);
192
Davide Pesavento412c9822021-07-02 00:21:05 -0400193 auto before = time::steady_clock::now();
Ashlesh Gawande90015992017-07-11 17:25:48 -0500194
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000195 entry.insertOrUpdateOutRecord(*face1, *interest);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400196 this->advanceClocks(1_s);
Ashlesh Gawande90015992017-07-11 17:25:48 -0500197
198 BOOST_CHECK_EQUAL(getLastOutgoing(entry), before);
199
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000200 entry.insertOrUpdateOutRecord(*face2, *interest);
Ashlesh Gawande90015992017-07-11 17:25:48 -0500201
202 BOOST_CHECK_EQUAL(getLastOutgoing(entry), time::steady_clock::now());
203}
204
Junxiao Shifef73e42016-03-29 14:15:05 -0700205BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm
206BOOST_AUTO_TEST_SUITE_END() // Fw
207
208} // namespace tests
209} // namespace fw
210} // namespace nfd