blob: 0589e0d312a1edc38e17b13671c371934ff1c470 [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
26#include "fw/pit-algorithm.hpp"
27
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)
38BOOST_FIXTURE_TEST_SUITE(TestPitAlgorithm, BaseFixture)
39
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
58BOOST_FIXTURE_TEST_SUITE(ViolatesScope, ScopeControlFixture)
59
60BOOST_AUTO_TEST_CASE(Unrestricted)
61{
62 shared_ptr<Interest> interest = makeInterest("ndn:/ieWRzDsCu");
63 pit::Entry entry(*interest);
64
Junxiao Shi9cff7792016-08-01 21:45:11 +000065 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -070066 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
67 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
68}
69
70BOOST_AUTO_TEST_CASE(Localhost)
71{
72 shared_ptr<Interest> interest = makeInterest("ndn:/localhost/5n1LzIt3");
73 pit::Entry entry(*interest);
74
Junxiao Shi9cff7792016-08-01 21:45:11 +000075 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -070076 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true);
77 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
78}
79
80BOOST_AUTO_TEST_CASE(LocalhopFromLocal)
81{
82 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/YcIKWCRYJ");
83 pit::Entry entry(*interest);
84
Junxiao Shi9cff7792016-08-01 21:45:11 +000085 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -070086 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
87 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
88}
89
90BOOST_AUTO_TEST_CASE(LocalhopFromNonLocal)
91{
92 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/x5uFr5IpqY");
93 pit::Entry entry(*interest);
94
Junxiao Shi9cff7792016-08-01 21:45:11 +000095 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -070096 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), true);
97 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
98}
99
100BOOST_AUTO_TEST_CASE(LocalhopFromLocalAndNonLocal)
101{
102 shared_ptr<Interest> interest = makeInterest("ndn:/localhop/gNn2MJAXt");
103 pit::Entry entry(*interest);
104
Junxiao Shi9cff7792016-08-01 21:45:11 +0000105 entry.insertOrUpdateInRecord(*nonLocalFace1, *interest);
106 entry.insertOrUpdateInRecord(*localFace3, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700107 BOOST_CHECK_EQUAL(violatesScope(entry, *nonLocalFace2), false);
108 BOOST_CHECK_EQUAL(violatesScope(entry, *localFace4), false);
109}
110
111BOOST_AUTO_TEST_SUITE_END() // ViolatesScope
112
113BOOST_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 Shi9cff7792016-08-01 21:45:11 +0000121 entry.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700122 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
123 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
124
Junxiao Shi9cff7792016-08-01 21:45:11 +0000125 entry.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700126 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), true);
127 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
128
Junxiao Shi9cff7792016-08-01 21:45:11 +0000129 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700130 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face1), false);
131 BOOST_CHECK_EQUAL(canForwardToLegacy(entry, *face2), true);
132}
133
134BOOST_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 Shi9cff7792016-08-01 21:45:11 +0000149 entry1.insertOrUpdateInRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700150 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 Shi9cff7792016-08-01 21:45:11 +0000156 entry2.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700157 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 Shi9cff7792016-08-01 21:45:11 +0000163 entry3.insertOrUpdateInRecord(*face1, *interest);
164 entry3.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700165 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 Shi9cff7792016-08-01 21:45:11 +0000173 entry4.insertOrUpdateInRecord(*face1, *interest);
174 entry4.insertOrUpdateInRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700175 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 Shi9cff7792016-08-01 21:45:11 +0000183 entry5.insertOrUpdateOutRecord(*face1, *interest);
184 entry5.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shifef73e42016-03-29 14:15:05 -0700185 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 Shi8744c972016-04-06 10:33:46 -0700193BOOST_FIXTURE_TEST_CASE(HasPendingOutRecords, UnitTestTimeFixture)
Junxiao Shifef73e42016-03-29 14:15:05 -0700194{
Junxiao Shi8744c972016-04-06 10:33:46 -0700195 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 Shi9cff7792016-08-01 21:45:11 +0000206 entry.insertOrUpdateOutRecord(*face1, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700207 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
208 entry.deleteOutRecord(*face1);
209 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
210
211 // Interest-Nack
Junxiao Shi9cff7792016-08-01 21:45:11 +0000212 entry.insertOrUpdateOutRecord(*face2, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700213 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 Shi9cff7792016-08-01 21:45:11 +0000222 entry.insertOrUpdateOutRecord(*face3, *interest);
Junxiao Shi8744c972016-04-06 10:33:46 -0700223 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), true);
224 this->advanceClocks(ndn::DEFAULT_INTEREST_LIFETIME, 2);
225 BOOST_CHECK_EQUAL(hasPendingOutRecords(entry), false);
Junxiao Shifef73e42016-03-29 14:15:05 -0700226}
227
228BOOST_AUTO_TEST_SUITE_END() // TestPitAlgorithm
229BOOST_AUTO_TEST_SUITE_END() // Fw
230
231} // namespace tests
232} // namespace fw
233} // namespace nfd