blob: f46aa38f66027796cd388cf3ff3aace76b1ed364 [file] [log] [blame]
Junxiao Shia2742922016-11-17 22:53:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shifc2e13d2017-07-25 02:08:48 +00002/*
ashiqopud3ae85d2019-02-17 02:29:55 +00003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shia2742922016-11-17 22:53:23 +00004 * 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 "table/pit-entry.hpp"
Junxiao Shia2742922016-11-17 22:53:23 +000027
28#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040029#include "tests/daemon/global-io-fixture.hpp"
30#include "tests/daemon/face/dummy-face.hpp"
Junxiao Shia2742922016-11-17 22:53:23 +000031
32namespace nfd {
33namespace pit {
34namespace tests {
35
36using namespace nfd::tests;
37
38BOOST_AUTO_TEST_SUITE(Table)
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040039BOOST_FIXTURE_TEST_SUITE(TestPitEntry, GlobalIoFixture)
Junxiao Shia2742922016-11-17 22:53:23 +000040
41BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1)
42BOOST_AUTO_TEST_CASE(CanMatch)
43{
44 shared_ptr<Interest> interest0 = makeInterest("/A");
45 Entry entry(*interest0);
46
47 shared_ptr<Interest> interest1 = makeInterest("/B");
48 BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false);
49
50 shared_ptr<Interest> interest2 = makeInterest("/A");
51 interest2->setNonce(27956);
52 BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true);
53
54 shared_ptr<Interest> interest3 = makeInterest("/A");
Davide Pesavento14e71f02019-03-28 17:35:25 -040055 interest3->setInterestLifetime(6210_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000056 BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true);
57
58 shared_ptr<Interest> interest4 = makeInterest("/A");
Junxiao Shifc2e13d2017-07-25 02:08:48 +000059 interest4->setForwardingHint({{10, "/telia/terabits"}, {20, "/ucla/cs"}});
Junxiao Shia2742922016-11-17 22:53:23 +000060 BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162
61
62 shared_ptr<Interest> interest5 = makeInterest("/A");
63 interest5->setMaxSuffixComponents(21);
64 BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false);
65}
66
67BOOST_AUTO_TEST_CASE(InOutRecords)
68{
69 shared_ptr<Face> face1 = make_shared<DummyFace>();
70 shared_ptr<Face> face2 = make_shared<DummyFace>();
71 Name name("ndn:/KuYfjtRq");
72 shared_ptr<Interest> interest = makeInterest(name);
73 shared_ptr<Interest> interest1 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040074 interest1->setInterestLifetime(2528_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000075 interest1->setNonce(25559);
76 shared_ptr<Interest> interest2 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040077 interest2->setInterestLifetime(6464_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000078 interest2->setNonce(19004);
79 shared_ptr<Interest> interest3 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040080 interest3->setInterestLifetime(3585_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000081 interest3->setNonce(24216);
82 shared_ptr<Interest> interest4 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040083 interest4->setInterestLifetime(8795_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000084 interest4->setNonce(17365);
85
86 Entry entry(*interest);
87
88 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
89 BOOST_CHECK_EQUAL(entry.getName(), name);
90
91 const InRecordCollection& inRecords1 = entry.getInRecords();
92 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
93 const OutRecordCollection& outRecords1 = entry.getOutRecords();
94 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
95
96 // insert in-record
97 time::steady_clock::TimePoint before1 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +000098 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, 0, *interest1);
Junxiao Shia2742922016-11-17 22:53:23 +000099 time::steady_clock::TimePoint after1 = time::steady_clock::now();
100 const InRecordCollection& inRecords2 = entry.getInRecords();
101 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
102 BOOST_CHECK(in1 == inRecords2.begin());
103 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
104 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
105 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
106 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
107 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed()
108 - interest1->getInterestLifetime(),
109 (after1 - before1));
ashiqopud3ae85d2019-02-17 02:29:55 +0000110 BOOST_CHECK(in1 == entry.getInRecord(*face1, 0));
Junxiao Shia2742922016-11-17 22:53:23 +0000111
112 // insert out-record
113 time::steady_clock::TimePoint before2 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000114 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, 0, *interest1);
Junxiao Shia2742922016-11-17 22:53:23 +0000115 time::steady_clock::TimePoint after2 = time::steady_clock::now();
116 const OutRecordCollection& outRecords2 = entry.getOutRecords();
117 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
118 BOOST_CHECK(out1 == outRecords2.begin());
119 BOOST_CHECK_EQUAL(&out1->getFace(), face1.get());
120 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
121 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
122 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
123 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed()
124 - interest1->getInterestLifetime(),
125 (after2 - before2));
ashiqopud3ae85d2019-02-17 02:29:55 +0000126 BOOST_CHECK(out1 == entry.getOutRecord(*face1, 0));
Junxiao Shia2742922016-11-17 22:53:23 +0000127
128 // update in-record
129 time::steady_clock::TimePoint before3 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000130 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, 0, *interest2);
Junxiao Shia2742922016-11-17 22:53:23 +0000131 time::steady_clock::TimePoint after3 = time::steady_clock::now();
132 const InRecordCollection& inRecords3 = entry.getInRecords();
133 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
134 BOOST_CHECK(in2 == inRecords3.begin());
135 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
136 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
137 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed()
138 - interest2->getInterestLifetime(),
139 (after3 - before3));
140
141 // insert another in-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000142 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, 0, *interest3);
Junxiao Shia2742922016-11-17 22:53:23 +0000143 const InRecordCollection& inRecords4 = entry.getInRecords();
144 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
145 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
146
147 // get in-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000148 InRecordCollection::iterator in4 = entry.getInRecord(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000149 BOOST_REQUIRE(in4 != entry.in_end());
150 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
151
152 // clear in-records
153 entry.clearInRecords();
154 const InRecordCollection& inRecords5 = entry.getInRecords();
155 BOOST_CHECK_EQUAL(inRecords5.size(), 0);
ashiqopud3ae85d2019-02-17 02:29:55 +0000156 BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000157
158 // insert another out-record
159 OutRecordCollection::iterator out2 =
ashiqopud3ae85d2019-02-17 02:29:55 +0000160 entry.insertOrUpdateOutRecord(*face2, 0, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000161 const OutRecordCollection& outRecords3 = entry.getOutRecords();
162 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
163 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
164
165 // get out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000166 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000167 BOOST_REQUIRE(out3 != entry.out_end());
168 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
169
170 // delete out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000171 entry.deleteOutRecord(*face2, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000172 const OutRecordCollection& outRecords4 = entry.getOutRecords();
173 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
174 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
ashiqopud3ae85d2019-02-17 02:29:55 +0000175 BOOST_CHECK(entry.getOutRecord(*face2, 0) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000176}
177
178BOOST_AUTO_TEST_CASE(Lifetime)
179{
180 shared_ptr<Interest> interest = makeInterest("ndn:/7oIEurbgy6");
Junxiao Shia2742922016-11-17 22:53:23 +0000181
182 shared_ptr<Face> face = make_shared<DummyFace>();
183 Entry entry(*interest);
184
ashiqopud3ae85d2019-02-17 02:29:55 +0000185 InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000186 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
187
ashiqopud3ae85d2019-02-17 02:29:55 +0000188 OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000189 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
190}
191
192BOOST_AUTO_TEST_CASE(OutRecordNack)
193{
194 shared_ptr<Face> face1 = make_shared<DummyFace>();
ashiqopud3ae85d2019-02-17 02:29:55 +0000195 OutRecord outR(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000196 BOOST_CHECK(outR.getIncomingNack() == nullptr);
197
198 shared_ptr<Interest> interest1 = makeInterest("ndn:/uWiapGjYL");
199 interest1->setNonce(165);
200 outR.update(*interest1);
201 BOOST_CHECK(outR.getIncomingNack() == nullptr);
202
203 shared_ptr<Interest> interest2 = makeInterest("ndn:/uWiapGjYL");
204 interest2->setNonce(996);
205 lp::Nack nack2(*interest2);
206 nack2.setReason(lp::NackReason::CONGESTION);
207 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
208 BOOST_CHECK(outR.getIncomingNack() == nullptr);
209
210 lp::Nack nack1(*interest1);
211 nack1.setReason(lp::NackReason::DUPLICATE);
212 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
213 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
214 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
215
216 outR.clearIncomingNack();
217 BOOST_CHECK(outR.getIncomingNack() == nullptr);
218}
219
220BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
221BOOST_AUTO_TEST_SUITE_END() // Table
222
223} // namespace tests
224} // namespace pit
225} // namespace nfd