blob: 8a188e62512d9b97b3b4c3212dc225cc63befcf5 [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/*
3 * Copyright (c) 2014-2017, 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"
29#include "tests/daemon/face/dummy-face.hpp"
30
31namespace nfd {
32namespace fw {
33namespace tests {
34
35using namespace nfd::tests;
36
37BOOST_AUTO_TEST_SUITE(Fw)
Junxiao Shi00dc9142016-11-21 14:23:12 +000038BOOST_FIXTURE_TEST_SUITE(TestAlgorithm, BaseFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -070039
40class ScopeControlFixture : public BaseFixture
41{
42protected:
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
51protected:
52 shared_ptr<Face> nonLocalFace1;
53 shared_ptr<Face> nonLocalFace2;
54 shared_ptr<Face> localFace3;
55 shared_ptr<Face> localFace4;
56};
57
Junxiao Shi00dc9142016-11-21 14:23:12 +000058BOOST_FIXTURE_TEST_SUITE(WouldViolateScope, ScopeControlFixture)
59
60BOOST_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
70BOOST_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
80BOOST_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
90BOOST_AUTO_TEST_SUITE_END() // WouldViolateScope
91
Junxiao Shifef73e42016-03-29 14:15:05 -070092BOOST_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
Junxiao Shi9cff7792016-08-01 21:45:11 +0000100 entry.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700101 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
102 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
103
Junxiao Shi9cff7792016-08-01 21:45:11 +0000104 entry.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700105 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), true);
106 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
107
Junxiao Shi9cff7792016-08-01 21:45:11 +0000108 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700109 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
110 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
111}
112
113BOOST_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);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000128 entry1.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700129 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);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000135 entry2.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700136 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);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000142 entry3.insertOrUpdateInRecord(*face1, *interest);
143 entry3.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700144 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);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000152 entry4.insertOrUpdateInRecord(*face1, *interest);
153 entry4.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700154 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);
Junxiao Shi9cff7792016-08-01 21:45:11 +0000162 entry5.insertOrUpdateOutRecord(*face1, *interest);
163 entry5.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700164 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 Shi8744c972016-04-06 10:33:46 -0700172BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, UnitTestTimeFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -0700173{
Junxiao Shi8744c972016-04-06 10:33:46 -0700174 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
Junxiao Shi9cff7792016-08-01 21:45:11 +0000185 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700186 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
187 entry.deleteOutRecord(*face1);
188 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
189
190 // Interest-Nack
Junxiao Shi9cff7792016-08-01 21:45:11 +0000191 entry.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700192 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
193 pit::OutRecordCollection::iterator outR = entry.getOutRecord(*face2);
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
Junxiao Shi9cff7792016-08-01 21:45:11 +0000201 entry.insertOrUpdateOutRecord(*face3, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700202 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
203 this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2);
204 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
Junxiao Shifef73e42016-03-29 14:15:05 -0700205}
206
Ashlesh Gawande90015992017-07-11 17:25:48 -0500207BOOST_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
217 entry.insertOrUpdateOutRecord(*face1, *interest);
218 this->advanceClocks(time::milliseconds(1000));
219
220 BOOST_CHECK_EQUAL(getLastOutgoing(entry), before);
221
222 entry.insertOrUpdateOutRecord(*face2, *interest);
223
224 BOOST_CHECK_EQUAL(getLastOutgoing(entry), time::steady_clock::now());
225}
226
Junxiao Shifef73e42016-03-29 14:15:05 -0700227BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm
228BOOST_AUTO_TEST_SUITE_END() // Fw
229
230} // namespace tests
231} // namespace fw
232} // namespace nfd