blob: 73cf7de6b8c099110604020d5f5fe832c3f4b156 [file] [log] [blame]
Junxiao Shia2742922016-11-17 22:53:23 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberryf4056d02017-05-26 17:31:53 +00003 * Copyright (c) 2014-2017, 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"
27#include "tests/daemon/face/dummy-face.hpp"
28
29#include "tests/test-common.hpp"
30
31namespace nfd {
32namespace pit {
33namespace tests {
34
35using namespace nfd::tests;
36
37BOOST_AUTO_TEST_SUITE(Table)
38BOOST_FIXTURE_TEST_SUITE(TestPitEntry, BaseFixture)
39
40BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(CanMatch, 1)
41BOOST_AUTO_TEST_CASE(CanMatch)
42{
43 shared_ptr<Interest> interest0 = makeInterest("/A");
44 Entry entry(*interest0);
45
46 shared_ptr<Interest> interest1 = makeInterest("/B");
47 BOOST_CHECK_EQUAL(entry.canMatch(*interest1), false);
48
49 shared_ptr<Interest> interest2 = makeInterest("/A");
50 interest2->setNonce(27956);
51 BOOST_CHECK_EQUAL(entry.canMatch(*interest2), true);
52
53 shared_ptr<Interest> interest3 = makeInterest("/A");
54 interest3->setInterestLifetime(time::milliseconds(6210));
55 BOOST_CHECK_EQUAL(entry.canMatch(*interest3), true);
56
57 shared_ptr<Interest> interest4 = makeInterest("/A");
58 shared_ptr<Link> link4 = makeLink("/net/ndnsim", {{10, "/telia/terabits"}, {20, "/ucla/cs"}});
59 interest4->setLink(link4->wireEncode());
60 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);
74 interest1->setInterestLifetime(time::milliseconds(2528));
75 interest1->setNonce(25559);
76 shared_ptr<Interest> interest2 = makeInterest(name);
77 interest2->setInterestLifetime(time::milliseconds(6464));
78 interest2->setNonce(19004);
79 shared_ptr<Interest> interest3 = makeInterest(name);
80 interest3->setInterestLifetime(time::milliseconds(3585));
81 interest3->setNonce(24216);
82 shared_ptr<Interest> interest4 = makeInterest(name);
83 interest4->setInterestLifetime(time::milliseconds(8795));
84 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();
98 InRecordCollection::iterator in1 = entry.insertOrUpdateInRecord(*face1, *interest1);
99 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));
110 BOOST_CHECK(in1 == entry.getInRecord(*face1));
111
112 // insert out-record
113 time::steady_clock::TimePoint before2 = time::steady_clock::now();
114 OutRecordCollection::iterator out1 = entry.insertOrUpdateOutRecord(*face1, *interest1);
115 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));
126 BOOST_CHECK(out1 == entry.getOutRecord(*face1));
127
128 // update in-record
129 time::steady_clock::TimePoint before3 = time::steady_clock::now();
130 InRecordCollection::iterator in2 = entry.insertOrUpdateInRecord(*face1, *interest2);
131 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
142 InRecordCollection::iterator in3 = entry.insertOrUpdateInRecord(*face2, *interest3);
143 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
148 InRecordCollection::iterator in4 = entry.getInRecord(*face1);
149 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);
156 BOOST_CHECK(entry.getInRecord(*face1) == entry.in_end());
157
158 // insert another out-record
159 OutRecordCollection::iterator out2 =
160 entry.insertOrUpdateOutRecord(*face2, *interest4);
161 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
166 OutRecordCollection::iterator out3 = entry.getOutRecord(*face1);
167 BOOST_REQUIRE(out3 != entry.out_end());
168 BOOST_CHECK_EQUAL(&out3->getFace(), face1.get());
169
170 // delete out-record
171 entry.deleteOutRecord(*face2);
172 const OutRecordCollection& outRecords4 = entry.getOutRecords();
173 BOOST_REQUIRE_EQUAL(outRecords4.size(), 1);
174 BOOST_CHECK_EQUAL(&outRecords4.begin()->getFace(), face1.get());
175 BOOST_CHECK(entry.getOutRecord(*face2) == entry.out_end());
176}
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
185 InRecordCollection::iterator inIt = entry.insertOrUpdateInRecord(*face, *interest);
186 BOOST_CHECK_GT(inIt->getExpiry(), time::steady_clock::now());
187
188 OutRecordCollection::iterator outIt = entry.insertOrUpdateOutRecord(*face, *interest);
189 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>();
195 OutRecord outR(*face1);
196 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