blob: ac6709bf21187d9d818a1bfea63f57a056c3b8e9 [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{
Davide Pesaventob31206e2019-04-20 22:34:12 -040069 auto face1 = make_shared<DummyFace>();
70 auto face2 = make_shared<DummyFace>();
71
Junxiao Shia2742922016-11-17 22:53:23 +000072 Name name("ndn:/KuYfjtRq");
Davide Pesaventob31206e2019-04-20 22:34:12 -040073 auto interest = makeInterest(name);
74 auto interest1 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040075 interest1->setInterestLifetime(2528_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000076 interest1->setNonce(25559);
Davide Pesaventob31206e2019-04-20 22:34:12 -040077 auto interest2 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040078 interest2->setInterestLifetime(6464_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000079 interest2->setNonce(19004);
Davide Pesaventob31206e2019-04-20 22:34:12 -040080 auto interest3 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040081 interest3->setInterestLifetime(3585_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000082 interest3->setNonce(24216);
Davide Pesaventob31206e2019-04-20 22:34:12 -040083 auto interest4 = makeInterest(name);
Davide Pesavento14e71f02019-03-28 17:35:25 -040084 interest4->setInterestLifetime(8795_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000085 interest4->setNonce(17365);
86
87 Entry entry(*interest);
88
89 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
90 BOOST_CHECK_EQUAL(entry.getName(), name);
91
92 const InRecordCollection& inRecords1 = entry.getInRecords();
93 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
94 const OutRecordCollection& outRecords1 = entry.getOutRecords();
95 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
96
97 // insert in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -040098 auto before1 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +000099 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, 0, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400100 auto after1 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000101 const InRecordCollection& inRecords2 = entry.getInRecords();
102 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
103 BOOST_CHECK(in1 == inRecords2.begin());
104 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
105 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
106 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
107 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400108 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - 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
Davide Pesaventob31206e2019-04-20 22:34:12 -0400113 auto before2 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000114 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, 0, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400115 auto after2 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000116 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);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400123 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(),
124 after2 - before2);
ashiqopud3ae85d2019-02-17 02:29:55 +0000125 BOOST_CHECK(out1 == entry.getOutRecord(*face1, 0));
Junxiao Shia2742922016-11-17 22:53:23 +0000126
127 // update in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400128 auto before3 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000129 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, 0, *interest2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400130 auto after3 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000131 const InRecordCollection& inRecords3 = entry.getInRecords();
132 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
133 BOOST_CHECK(in2 == inRecords3.begin());
134 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
135 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400136 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(),
137 after3 - before3);
Junxiao Shia2742922016-11-17 22:53:23 +0000138
Davide Pesaventob31206e2019-04-20 22:34:12 -0400139 // insert another in-record (different face)
ashiqopud3ae85d2019-02-17 02:29:55 +0000140 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, 0, *interest3);
Junxiao Shia2742922016-11-17 22:53:23 +0000141 const InRecordCollection& inRecords4 = entry.getInRecords();
142 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
143 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400144 BOOST_CHECK_EQUAL(in3->getEndpointId(), 0);
145
146 // insert another in-record (different endpoint)
147 InRecordCollection::iterator in4 = entry.insertOrUpdateInRecord(*face1, 42, *interest4);
148 const InRecordCollection& inRecords5 = entry.getInRecords();
149 BOOST_CHECK_EQUAL(inRecords5.size(), 3);
150 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
151 BOOST_CHECK_EQUAL(in4->getEndpointId(), 42);
Junxiao Shia2742922016-11-17 22:53:23 +0000152
153 // get in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400154 InRecordCollection::iterator in5 = entry.getInRecord(*face1, 0);
155 BOOST_REQUIRE(in5 != entry.in_end());
156 BOOST_CHECK_EQUAL(&in5->getFace(), face1.get());
157 BOOST_CHECK_EQUAL(in5->getEndpointId(), 0);
158 InRecordCollection::iterator in6 = entry.getInRecord(*face1, 42);
159 BOOST_REQUIRE(in6 != entry.in_end());
160 BOOST_CHECK_EQUAL(&in6->getFace(), face1.get());
161 BOOST_CHECK_EQUAL(in6->getEndpointId(), 42);
162 BOOST_CHECK(in5 != in6);
163
164 // delete in-record
165 entry.deleteInRecord(*face1, 0);
166 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 2);
167 BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end());
168 BOOST_CHECK(entry.getInRecord(*face1, 42) != entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000169
170 // clear in-records
171 entry.clearInRecords();
Davide Pesaventob31206e2019-04-20 22:34:12 -0400172 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0);
ashiqopud3ae85d2019-02-17 02:29:55 +0000173 BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400174 BOOST_CHECK(entry.getInRecord(*face1, 42) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000175
176 // insert another out-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400177 OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, 0, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000178 const OutRecordCollection& outRecords3 = entry.getOutRecords();
179 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
180 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
181
182 // get out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000183 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000184 BOOST_REQUIRE(out3 != entry.out_end());
185 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
186
187 // delete out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000188 entry.deleteOutRecord(*face2, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000189 const OutRecordCollection& outRecords4 = entry.getOutRecords();
190 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
191 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
ashiqopud3ae85d2019-02-17 02:29:55 +0000192 BOOST_CHECK(entry.getOutRecord(*face2, 0) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000193}
194
195BOOST_AUTO_TEST_CASE(Lifetime)
196{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400197 auto interest = makeInterest("ndn:/7oIEurbgy6");
198 auto face = make_shared<DummyFace>();
Junxiao Shia2742922016-11-17 22:53:23 +0000199 Entry entry(*interest);
200
Davide Pesaventob31206e2019-04-20 22:34:12 -0400201 auto inIt = entry.insertOrUpdateInRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000202 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
203
Davide Pesaventob31206e2019-04-20 22:34:12 -0400204 auto outIt = entry.insertOrUpdateOutRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000205 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
206}
207
208BOOST_AUTO_TEST_CASE(OutRecordNack)
209{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400210 auto face1 = make_shared<DummyFace>();
ashiqopud3ae85d2019-02-17 02:29:55 +0000211 OutRecord outR(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000212 BOOST_CHECK(outR.getIncomingNack() == nullptr);
213
Davide Pesaventob31206e2019-04-20 22:34:12 -0400214 auto interest1 = makeInterest("ndn:/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000215 interest1->setNonce(165);
216 outR.update(*interest1);
217 BOOST_CHECK(outR.getIncomingNack() == nullptr);
218
Davide Pesaventob31206e2019-04-20 22:34:12 -0400219 auto interest2 = makeInterest("ndn:/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000220 interest2->setNonce(996);
221 lp::Nack nack2(*interest2);
222 nack2.setReason(lp::NackReason::CONGESTION);
223 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
224 BOOST_CHECK(outR.getIncomingNack() == nullptr);
225
226 lp::Nack nack1(*interest1);
227 nack1.setReason(lp::NackReason::DUPLICATE);
228 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
229 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
230 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
231
232 outR.clearIncomingNack();
233 BOOST_CHECK(outR.getIncomingNack() == nullptr);
234}
235
236BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
237BOOST_AUTO_TEST_SUITE_END() // Table
238
239} // namespace tests
240} // namespace pit
241} // namespace nfd