blob: caebecc7d6734a1721b6b4494b92f18cc6ae65d7 [file] [log] [blame]
Junxiao Shifef73e42016-03-29 14:15:05 -07001/* -*- 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 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_FIXTURE_TEST_SUITE(ViolatesScope, ScopeControlFixture)
93
94BOOST_AUTO_TEST_CASE(Unrestricted)
95{
96 shared_ptr<Interest> interest = makeInterest("ndn:/ieWRzDsCu");
97 pit::Entry entry(*interest);
98
Junxiao Shi9cff7792016-08-01 21:45:11 +000099 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700100 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
101 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
102}
103
104BOOST_AUTO_TEST_CASE(Localhost)
105{
106 shared_ptr<Interest> interest = makeInterest("ndn:/localhost/5n1LzIt3");
107 pit::Entry entry(*interest);
108
Junxiao Shi9cff7792016-08-01 21:45:11 +0000109 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700110 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true);
111 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
112}
113
114BOOST_AUTO_TEST_CASE(LocalhopFromLocal)
115{
116 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/YcIKWCRYJ");
117 pit::Entry entry(*interest);
118
Junxiao Shi9cff7792016-08-01 21:45:11 +0000119 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700120 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
121 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
122}
123
124BOOST_AUTO_TEST_CASE(LocalhopFromNonLocal)
125{
126 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/x5uFr5IpqY");
127 pit::Entry entry(*interest);
128
Junxiao Shi9cff7792016-08-01 21:45:11 +0000129 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700130 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true);
131 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
132}
133
134BOOST_AUTO_TEST_CASE(LocalhopFromLocalAndNonLocal)
135{
136 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/gNn2MJAXt");
137 pit::Entry entry(*interest);
138
Junxiao Shi9cff7792016-08-01 21:45:11 +0000139 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
140 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700141 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
142 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
143}
144
145BOOST_AUTO_TEST_SUITE_END() // ViolatesScope
146
147BOOST_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 Shi9cff7792016-08-01 21:45:11 +0000155 entry.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700156 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
157 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
158
Junxiao Shi9cff7792016-08-01 21:45:11 +0000159 entry.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700160 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), true);
161 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
162
Junxiao Shi9cff7792016-08-01 21:45:11 +0000163 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700164 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
165 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
166}
167
168BOOST_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 Shi9cff7792016-08-01 21:45:11 +0000183 entry1.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700184 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 Shi9cff7792016-08-01 21:45:11 +0000190 entry2.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700191 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 Shi9cff7792016-08-01 21:45:11 +0000197 entry3.insertOrUpdateInRecord(*face1, *interest);
198 entry3.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700199 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 Shi9cff7792016-08-01 21:45:11 +0000207 entry4.insertOrUpdateInRecord(*face1, *interest);
208 entry4.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700209 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 Shi9cff7792016-08-01 21:45:11 +0000217 entry5.insertOrUpdateOutRecord(*face1, *interest);
218 entry5.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700219 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 Shi8744c972016-04-06 10:33:46 -0700227BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, UnitTestTimeFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -0700228{
Junxiao Shi8744c972016-04-06 10:33:46 -0700229 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 Shi9cff7792016-08-01 21:45:11 +0000240 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700241 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
242 entry.deleteOutRecord(*face1);
243 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
244
245 // Interest-Nack
Junxiao Shi9cff7792016-08-01 21:45:11 +0000246 entry.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700247 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 Shi9cff7792016-08-01 21:45:11 +0000256 entry.insertOrUpdateOutRecord(*face3, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700257 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
258 this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2);
259 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
Junxiao Shifef73e42016-03-29 14:15:05 -0700260}
261
262BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm
263BOOST_AUTO_TEST_SUITE_END() // Fw
264
265} // namespace tests
266} // namespace fw
267} // namespace nfd