blob: 8bdb5feae9116c3f4df2d2459914753f75c896f9 [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();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000088 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *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);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +000099 BOOST_CHECK(in1 == entry.getInRecord(*face1));
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();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000103 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *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);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000114 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
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();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000118 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *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
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000128 // insert another in-record
129 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *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());
133
134 // get in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000135 InRecordCollection::iterator in4 = entry.getInRecord(*face1);
136 BOOST_REQUIRE(in4 != entry.in_end());
137 BOOST_CHECK_EQUAL(&in4->getFace(), face1.get());
Davide Pesaventob31206e2019-04-20 22:34:12 -0400138
139 // delete in-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000140 entry.deleteInRecord(*face1);
141 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 1);
142 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000143
144 // clear in-records
145 entry.clearInRecords();
Davide Pesaventob31206e2019-04-20 22:34:12 -0400146 BOOST_CHECK_EQUAL(entry.getInRecords().size(), 0);
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000147 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
148 BOOST_CHECK(entry.getInRecord(*face2) == entry.in_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000149
150 // insert another out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000151 OutRecordCollection::iterator out2 = entry.insertOrUpdateOutRecord(*face2, *interest4);
Junxiao Shia2742922016-11-17 22:53:23 +0000152 const OutRecordCollection& outRecords3 = entry.getOutRecords();
153 BOOST_CHECK_EQUAL(outRecords3.size(), 2);
154 BOOST_CHECK_EQUAL(&out2->getFace(), face2.get());
155
156 // get out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000157 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000158 BOOST_REQUIRE(out3 != entry.out_end());
159 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
160
161 // delete out-record
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000162 entry.deleteOutRecord(*face2);
Junxiao Shia2742922016-11-17 22:53:23 +0000163 const OutRecordCollection& outRecords4 = entry.getOutRecords();
164 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
165 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000166 BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end());
Junxiao Shia2742922016-11-17 22:53:23 +0000167}
168
169BOOST_AUTO_TEST_CASE(Lifetime)
170{
Junxiao Shi25d97282019-05-14 13:44:46 -0600171 auto interest = makeInterest("/7oIEurbgy6");
Davide Pesaventob31206e2019-04-20 22:34:12 -0400172 auto face = make_shared<DummyFace>();
Junxiao Shia2742922016-11-17 22:53:23 +0000173 Entry entry(*interest);
174
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000175 auto inIt = entry.insertOrUpdateInRecord(*face, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000176 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
177
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000178 auto outIt = entry.insertOrUpdateOutRecord(*face, *interest);
Junxiao Shia2742922016-11-17 22:53:23 +0000179 BOOST_CHECK_GT(outIt->getExpiry(), time::steady_clock::now());
180}
181
182BOOST_AUTO_TEST_CASE(OutRecordNack)
183{
Davide Pesaventob31206e2019-04-20 22:34:12 -0400184 auto face1 = make_shared<DummyFace>();
Md Ashiqur Rahmanc88d2d42019-08-28 20:19:47 +0000185 OutRecord outR(*face1);
Junxiao Shia2742922016-11-17 22:53:23 +0000186 BOOST_CHECK(outR.getIncomingNack() == nullptr);
187
Junxiao Shi25d97282019-05-14 13:44:46 -0600188 auto interest1 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000189 interest1->setNonce(165);
190 outR.update(*interest1);
191 BOOST_CHECK(outR.getIncomingNack() == nullptr);
192
Junxiao Shi25d97282019-05-14 13:44:46 -0600193 auto interest2 = makeInterest("/uWiapGjYL");
Junxiao Shia2742922016-11-17 22:53:23 +0000194 interest2->setNonce(996);
195 lp::Nack nack2(*interest2);
196 nack2.setReason(lp::NackReason::CONGESTION);
197 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack2), false);
198 BOOST_CHECK(outR.getIncomingNack() == nullptr);
199
200 lp::Nack nack1(*interest1);
201 nack1.setReason(lp::NackReason::DUPLICATE);
202 BOOST_CHECK_EQUAL(outR.setIncomingNack(nack1), true);
203 BOOST_REQUIRE(outR.getIncomingNack() != nullptr);
204 BOOST_CHECK_EQUAL(outR.getIncomingNack()->getReason(), lp::NackReason::DUPLICATE);
205
206 outR.clearIncomingNack();
207 BOOST_CHECK(outR.getIncomingNack() == nullptr);
208}
209
210BOOST_AUTO_TEST_SUITE_END() // TestPitEntry
211BOOST_AUTO_TEST_SUITE_END() // Table
212
213} // namespace tests
214} // namespace pit
215} // namespace nfd