blob: 221753b54d801df49020a79041b390a12c746bcc [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{
Junxiao Shi25d97282019-05-14 13:44:46 -060044 auto interest0 = makeInterest("/A");
Junxiao Shia2742922016-11-17 22:53:23 +000045 Entry entry(*interest0);
46
Junxiao Shi25d97282019-05-14 13:44:46 -060047 auto interest1 = makeInterest("/B");
Junxiao Shia2742922016-11-17 22:53:23 +000048 BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false);
49
Junxiao Shi25d97282019-05-14 13:44:46 -060050 auto interest2 = makeInterest("/A", false, nullopt, 27956);
Junxiao Shia2742922016-11-17 22:53:23 +000051 BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true);
52
Junxiao Shi25d97282019-05-14 13:44:46 -060053 auto interest3 = makeInterest("/A", false, 6210_ms);
Junxiao Shia2742922016-11-17 22:53:23 +000054 BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true);
55
Junxiao Shi25d97282019-05-14 13:44:46 -060056 auto interest4 = makeInterest("/A");
Junxiao Shifc2e13d2017-07-25 02:08:48 +000057 interest4->setForwardingHint({{10, "/telia/terabits"}, {20, "/ucla/cs"}});
Junxiao Shia2742922016-11-17 22:53:23 +000058 BOOST_CHECK_EQUAL(entry.canMatch(*interest4), false); // expected failure until #3162
59
Junxiao Shi25d97282019-05-14 13:44:46 -060060 auto interest5 = makeInterest("/A", true);
Junxiao Shia2742922016-11-17 22:53:23 +000061 BOOST_CHECK_EQUAL(entry.canMatch(*interest5), false);
62}
63
64BOOST_AUTO_TEST_CASE(InOutRecords)
65{
Davide Pesaventob31206e2019-04-20 22:34:12 -040066 auto face1 = make_shared<DummyFace>();
67 auto face2 = make_shared<DummyFace>();
68
Junxiao Shi25d97282019-05-14 13:44:46 -060069 Name name("/KuYfjtRq");
Davide Pesaventob31206e2019-04-20 22:34:12 -040070 auto interest = makeInterest(name);
Junxiao Shi25d97282019-05-14 13:44:46 -060071 auto interest1 = makeInterest(name, false, 2528_ms, 25559);
72 auto interest2 = makeInterest(name, false, 6464_ms, 19004);
73 auto interest3 = makeInterest(name, false, 3585_ms, 24216);
74 auto interest4 = makeInterest(name, false, 8795_ms, 17365);
Junxiao Shia2742922016-11-17 22:53:23 +000075
76 Entry entry(*interest);
77
78 BOOST_CHECK_EQUAL(entry.getInterest().getName(), name);
79 BOOST_CHECK_EQUAL(entry.getName(), name);
80
81 const InRecordCollection& inRecords1 = entry.getInRecords();
82 BOOST_CHECK_EQUAL(inRecords1.size(), 0);
83 const OutRecordCollection& outRecords1 = entry.getOutRecords();
84 BOOST_CHECK_EQUAL(outRecords1.size(), 0);
85
86 // insert in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -040087 auto before1 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +000088 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, 0, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -040089 auto after1 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +000090 const InRecordCollection& inRecords2 = entry.getInRecords();
91 BOOST_CHECK_EQUAL(inRecords2.size(), 1);
92 BOOST_CHECK(in1 == inRecords2.begin());
93 BOOST_CHECK_EQUAL(&in1->getFace(), face1.get());
94 BOOST_CHECK_EQUAL(in1->getLastNonce(), interest1->getNonce());
95 BOOST_CHECK_GE(in1->getLastRenewed(), before1);
96 BOOST_CHECK_LE(in1->getLastRenewed(), after1);
Davide Pesaventob31206e2019-04-20 22:34:12 -040097 BOOST_CHECK_LE(in1->getExpiry() - in1->getLastRenewed() - interest1->getInterestLifetime(),
98 after1 - before1);
ashiqopud3ae85d2019-02-17 02:29:55 +000099 BOOST_CHECK(in1 == entry.getInRecord(*face1, 0));
Junxiao Shia2742922016-11-17 22:53:23 +0000100
101 // insert out-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400102 auto before2 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000103 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, 0, *interest1);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400104 auto after2 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000105 const OutRecordCollection& outRecords2 = entry.getOutRecords();
106 BOOST_CHECK_EQUAL(outRecords2.size(), 1);
107 BOOST_CHECK(out1 == outRecords2.begin());
108 BOOST_CHECK_EQUAL(&out1->getFace(), face1.get());
109 BOOST_CHECK_EQUAL(out1->getLastNonce(), interest1->getNonce());
110 BOOST_CHECK_GE(out1->getLastRenewed(), before2);
111 BOOST_CHECK_LE(out1->getLastRenewed(), after2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400112 BOOST_CHECK_LE(out1->getExpiry() - out1->getLastRenewed() - interest1->getInterestLifetime(),
113 after2 - before2);
ashiqopud3ae85d2019-02-17 02:29:55 +0000114 BOOST_CHECK(out1 == entry.getOutRecord(*face1, 0));
Junxiao Shia2742922016-11-17 22:53:23 +0000115
116 // update in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400117 auto before3 = time::steady_clock::now();
ashiqopud3ae85d2019-02-17 02:29:55 +0000118 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, 0, *interest2);
Davide Pesaventob31206e2019-04-20 22:34:12 -0400119 auto after3 = time::steady_clock::now();
Junxiao Shia2742922016-11-17 22:53:23 +0000120 const InRecordCollection& inRecords3 = entry.getInRecords();
121 BOOST_CHECK_EQUAL(inRecords3.size(), 1);
122 BOOST_CHECK(in2 == inRecords3.begin());
123 BOOST_CHECK_EQUAL(&in2->getFace(), face1.get());
124 BOOST_CHECK_EQUAL(in2->getLastNonce(), interest2->getNonce());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400125 BOOST_CHECK_LE(in2->getExpiry() - in2->getLastRenewed() - interest2->getInterestLifetime(),
126 after3 - before3);
Junxiao Shia2742922016-11-17 22:53:23 +0000127
Davide Pesaventob31206e2019-04-20 22:34:12 -0400128 // insert another in-record (different face)
ashiqopud3ae85d2019-02-17 02:29:55 +0000129 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, 0, *interest3);
Junxiao Shia2742922016-11-17 22:53:23 +0000130 const InRecordCollection& inRecords4 = entry.getInRecords();
131 BOOST_CHECK_EQUAL(inRecords4.size(), 2);
132 BOOST_CHECK_EQUAL(&in3->getFace(), face2.get());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400133 BOOST_CHECK_EQUAL(in3->getEndpointId(), 0);
134
135 // insert another in-record (different endpoint)
136 InRecordCollection::iterator in4 = entry.insertOrUpdateInRecord(*face1, 42, *interest4);
137 const InRecordCollection& inRecords5 = entry.getInRecords();
138 BOOST_CHECK_EQUAL(inRecords5.size(), 3);
139 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
140 BOOST_CHECK_EQUAL(in4->getEndpointId(), 42);
Junxiao Shia2742922016-11-17 22:53:23 +0000141
142 // get in-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400143 InRecordCollection::iterator in5 = entry.getInRecord(*face1, 0);
144 BOOST_REQUIRE(in5 != entry.in_end());
145 BOOST_CHECK_EQUAL(&in5->getFace(), face1.get());
146 BOOST_CHECK_EQUAL(in5->getEndpointId(), 0);
147 InRecordCollection::iterator in6 = entry.getInRecord(*face1, 42);
148 BOOST_REQUIRE(in6 != entry.in_end());
149 BOOST_CHECK_EQUAL(&in6->getFace(), face1.get());
150 BOOST_CHECK_EQUAL(in6->getEndpointId(), 42);
151 BOOST_CHECK(in5 != in6);
152
153 // delete in-record
154 entry.deleteInRecord(*face1, 0);
155 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 2);
156 BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end());
157 BOOST_CHECK(entry.getInRecord(*face1, 42) != entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000158
159 // clear in-records
160 entry.clearInRecords();
Davide Pesaventob31206e2019-04-20 22:34:12 -0400161 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0);
ashiqopud3ae85d2019-02-17 02:29:55 +0000162 BOOST_CHECK(entry.getInRecord(*face1, 0) == entry.in_end());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400163 BOOST_CHECK(entry.getInRecord(*face1, 42) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000164
165 // insert another out-record
Davide Pesaventob31206e2019-04-20 22:34:12 -0400166 OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, 0, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000167 const OutRecordCollection& outRecords3 = entry.getOutRecords();
168 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
169 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
170
171 // get out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000172 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000173 BOOST_REQUIRE(out3 != entry.out_end());
174 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
175
176 // delete out-record
ashiqopud3ae85d2019-02-17 02:29:55 +0000177 entry.deleteOutRecord(*face2, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000178 const OutRecordCollection& outRecords4 = entry.getOutRecords();
179 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
180 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
ashiqopud3ae85d2019-02-17 02:29:55 +0000181 BOOST_CHECK(entry.getOutRecord(*face2, 0) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000182}
183
184BOOST_AUTO_TEST_CASE(Lifetime)
185{
Junxiao Shi25d97282019-05-14 13:44:46 -0600186 auto interest = makeInterest("/7oIEurbgy6");
Davide Pesaventob31206e2019-04-20 22:34:12 -0400187 auto face = make_shared<DummyFace>();
Junxiao Shia2742922016-11-17 22:53:23 +0000188 Entry entry(*interest);
189
Davide Pesaventob31206e2019-04-20 22:34:12 -0400190 auto inIt = entry.insertOrUpdateInRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000191 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
192
Davide Pesaventob31206e2019-04-20 22:34:12 -0400193 auto outIt = entry.insertOrUpdateOutRecord(*face, 0, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000194 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
195}
196
197BOOST_AUTO_TEST_CASE(OutRecordNack)
198{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400199 auto face1 = make_shared<DummyFace>();
ashiqopud3ae85d2019-02-17 02:29:55 +0000200 OutRecord outR(*face1, 0);
Junxiao Shia2742922016-11-17 22:53:23 +0000201 BOOST_CHECK(outR.getIncomingNack() == nullptr);
202
Junxiao Shi25d97282019-05-14 13:44:46 -0600203 auto interest1 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000204 interest1->setNonce(165);
205 outR.update(*interest1);
206 BOOST_CHECK(outR.getIncomingNack() == nullptr);
207
Junxiao Shi25d97282019-05-14 13:44:46 -0600208 auto interest2 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000209 interest2->setNonce(996);
210 lp::Nack nack2(*interest2);
211 nack2.setReason(lp::NackReason::CONGESTION);
212 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
213 BOOST_CHECK(outR.getIncomingNack() == nullptr);
214
215 lp::Nack nack1(*interest1);
216 nack1.setReason(lp::NackReason::DUPLICATE);
217 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
218 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
219 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
220
221 outR.clearIncomingNack();
222 BOOST_CHECK(outR.getIncomingNack() == nullptr);
223}
224
225BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
226BOOST_AUTO_TEST_SUITE_END() // Table
227
228} // namespace tests
229} // namespace pit
230} // namespace nfd