blob: 915a049080a59da914b3649a4c69367cfad79af8 [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi3f9234b2017-12-07 17:41:50 +00002/*
Davide Pesaventocf7db2f2019-03-24 23:17:28 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi767cb332015-01-08 09:35:49 -07004 * Arizona Board of Regents,
5 * Colorado State University,
Junxiao Shi35353962015-01-08 09:13:47 -07006 * University Pierre & Marie Curie, Sorbonne University,
Junxiao Shi767cb332015-01-08 09:35:49 -07007 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
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/>.
Junxiao Shi19838042014-06-21 00:34:01 -070024 */
Junxiao Shi65d00722014-02-17 10:50:20 -070025
26#include "table/measurements.hpp"
Junxiao Shi8c0500f2015-11-11 08:30:16 -070027#include "table/fib.hpp"
Junxiao Shi767cb332015-01-08 09:35:49 -070028#include "table/pit.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070029
Junxiao Shid9ee45c2014-02-27 15:38:11 -070030#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040031#include "tests/daemon/global-io-fixture.hpp"
Junxiao Shi65d00722014-02-17 10:50:20 -070032
33namespace nfd {
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000034namespace measurements {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070035namespace tests {
Junxiao Shi65d00722014-02-17 10:50:20 -070036
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000037using namespace nfd::tests;
38
Junxiao Shi8c0500f2015-11-11 08:30:16 -070039BOOST_AUTO_TEST_SUITE(Table)
40
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040041class MeasurementsFixture : public GlobalIoTimeFixture
Junxiao Shi8c0500f2015-11-11 08:30:16 -070042{
43public:
44 MeasurementsFixture()
45 : measurements(nameTree)
46 {
47 }
48
49public:
50 NameTree nameTree;
51 Measurements measurements;
52};
53
54BOOST_FIXTURE_TEST_SUITE(TestMeasurements, MeasurementsFixture)
Junxiao Shi65d00722014-02-17 10:50:20 -070055
56BOOST_AUTO_TEST_CASE(Get_Parent)
57{
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000058 Entry& entryAB = measurements.get("/A/B");
59 BOOST_CHECK_EQUAL(entryAB.getName(), "/A/B");
Junxiao Shi65d00722014-02-17 10:50:20 -070060
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000061 Entry& entry0 = measurements.get("/");
62 BOOST_CHECK_EQUAL(entry0.getName(), "/");
Junxiao Shi65d00722014-02-17 10:50:20 -070063
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000064 Entry* entryA = measurements.getParent(entryAB);
Junxiao Shie368d992014-12-02 23:44:31 -070065 BOOST_REQUIRE(entryA != nullptr);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070066 BOOST_CHECK_EQUAL(entryA->getName(), "/A");
Junxiao Shi65d00722014-02-17 10:50:20 -070067
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000068 Entry* entry0c = measurements.getParent(*entryA);
69 BOOST_REQUIRE(entry0c != nullptr);
70 BOOST_CHECK_EQUAL(&entry0, entry0c);
Junxiao Shi65d00722014-02-17 10:50:20 -070071}
72
Junxiao Shi3f9234b2017-12-07 17:41:50 +000073BOOST_AUTO_TEST_CASE(GetLongName)
74{
75 Name n;
76 while (n.size() < NameTree::getMaxDepth() - 1) {
77 n.append("A");
78 }
79 Entry& entry1 = measurements.get(n);
80 BOOST_CHECK_EQUAL(entry1.getName().size(), NameTree::getMaxDepth() - 1);
81
82 n.append("B");
83 Entry& entry2 = measurements.get(n);
84 BOOST_CHECK_EQUAL(entry2.getName().size(), NameTree::getMaxDepth());
85
86 n.append("C");
87 Entry& entry3 = measurements.get(n);
88 BOOST_CHECK_EQUAL(entry3.getName().size(), NameTree::getMaxDepth());
89}
90
Junxiao Shi8c0500f2015-11-11 08:30:16 -070091BOOST_AUTO_TEST_CASE(GetWithFibEntry)
92{
93 Fib fib(nameTree);
94
Junxiao Shia6de4292016-07-12 02:08:10 +000095 const fib::Entry* fibA = fib.insert("/A").first;
96 const fib::Entry* fibAB = fib.insert("/A/B").first;
Junxiao Shi8c0500f2015-11-11 08:30:16 -070097
Junxiao Shi80f9fcd2016-07-23 02:48:36 +000098 Entry& entryA = measurements.get(*fibA);
99 BOOST_CHECK_EQUAL(entryA.getName(), "/A");
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700100
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000101 Entry& entryAB = measurements.get(*fibAB);
102 BOOST_CHECK_EQUAL(entryAB.getName(), "/A/B");
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700103}
104
105BOOST_AUTO_TEST_CASE(GetWithEmptyFibEntry) // Bug 3275
106{
107 Fib fib(nameTree);
108
Junxiao Shia6de4292016-07-12 02:08:10 +0000109 const fib::Entry& fib0 = fib.findLongestPrefixMatch("/");
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700110
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000111 Entry& entry0 = measurements.get(fib0);
112 BOOST_CHECK_EQUAL(entry0.getName(), "/");
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700113}
114
115BOOST_AUTO_TEST_CASE(GetWithPitEntry)
116{
117 Pit pit(nameTree);
118
119 shared_ptr<Interest> interestA = makeInterest("/A");
120 shared_ptr<pit::Entry> pitA = pit.insert(*interestA).first;
Junxiao Shi4370fde2016-02-24 12:20:46 -0700121 shared_ptr<Data> dataABC = makeData("/A/B/C");
122 Name fullName = dataABC->getFullName();
123 shared_ptr<Interest> interestFull = makeInterest(fullName);
124 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700125
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000126 Entry& entryA = measurements.get(*pitA);
127 BOOST_CHECK_EQUAL(entryA.getName(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700128
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000129 Entry& entryFull = measurements.get(*pitFull);
130 BOOST_CHECK_EQUAL(entryFull.getName(), fullName);
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700131}
132
Junxiao Shib30c7b02015-01-07 15:45:54 -0700133class DummyStrategyInfo1 : public fw::StrategyInfo
134{
135public:
136 static constexpr int
137 getTypeId()
138 {
139 return 21;
140 }
141};
142
143class DummyStrategyInfo2 : public fw::StrategyInfo
144{
145public:
146 static constexpr int
147 getTypeId()
148 {
149 return 22;
150 }
151};
152
153BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
154{
Junxiao Shib30c7b02015-01-07 15:45:54 -0700155 measurements.get("/A");
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000156 measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>();
Junxiao Shib30c7b02015-01-07 15:45:54 -0700157 measurements.get("/A/B/C/D");
158
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000159 Entry* found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E");
Junxiao Shib30c7b02015-01-07 15:45:54 -0700160 BOOST_REQUIRE(found1 != nullptr);
161 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
162
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000163 Entry* found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
164 EntryWithStrategyInfo<DummyStrategyInfo1>());
Junxiao Shib30c7b02015-01-07 15:45:54 -0700165 BOOST_REQUIRE(found2 != nullptr);
166 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
167
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000168 Entry* found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
169 EntryWithStrategyInfo<DummyStrategyInfo2>());
Junxiao Shib30c7b02015-01-07 15:45:54 -0700170 BOOST_CHECK(found3 == nullptr);
171}
172
Junxiao Shi767cb332015-01-08 09:35:49 -0700173BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry)
174{
Junxiao Shi767cb332015-01-08 09:35:49 -0700175 Pit pit(nameTree);
176
177 measurements.get("/A");
Junxiao Shi5b3feb62016-08-19 01:51:41 +0000178 measurements.get("/A/B/C").insertStrategyInfo<DummyStrategyInfo1>();
Junxiao Shi767cb332015-01-08 09:35:49 -0700179 measurements.get("/A/B/C/D");
180
181 shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E");
182 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
183
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000184 Entry* found1 = measurements.findLongestPrefixMatch(*pitEntry);
Junxiao Shi767cb332015-01-08 09:35:49 -0700185 BOOST_REQUIRE(found1 != nullptr);
186 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
187
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000188 Entry* found2 = measurements.findLongestPrefixMatch(*pitEntry,
189 EntryWithStrategyInfo<DummyStrategyInfo1>());
Junxiao Shi767cb332015-01-08 09:35:49 -0700190 BOOST_REQUIRE(found2 != nullptr);
191 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
192
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000193 Entry* found3 = measurements.findLongestPrefixMatch(*pitEntry,
194 EntryWithStrategyInfo<DummyStrategyInfo2>());
Junxiao Shi767cb332015-01-08 09:35:49 -0700195 BOOST_CHECK(found3 == nullptr);
196}
197
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700198BOOST_AUTO_TEST_CASE(Lifetime)
Junxiao Shi19838042014-06-21 00:34:01 -0700199{
Junxiao Shi19838042014-06-21 00:34:01 -0700200 Name nameA("ndn:/A");
201 Name nameB("ndn:/B");
202 Name nameC("ndn:/C");
203
204 BOOST_CHECK_EQUAL(measurements.size(), 0);
205
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000206 Entry& entryA = measurements.get(nameA);
207 measurements.get(nameB);
208 Entry& entryC = measurements.get(nameC);
Junxiao Shi19838042014-06-21 00:34:01 -0700209 BOOST_CHECK_EQUAL(measurements.size(), 3);
210
211 const time::nanoseconds EXTEND_A = time::seconds(2);
212 const time::nanoseconds CHECK1 = time::seconds(3);
213 const time::nanoseconds CHECK2 = time::seconds(5);
214 const time::nanoseconds EXTEND_C = time::seconds(6);
215 const time::nanoseconds CHECK3 = time::seconds(7);
Junxiao Shie368d992014-12-02 23:44:31 -0700216 BOOST_ASSERT(EXTEND_A < CHECK1);
217 BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime());
218 BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2);
219 BOOST_ASSERT(CHECK2 < EXTEND_C);
220 BOOST_ASSERT(EXTEND_C < CHECK3);
Junxiao Shi19838042014-06-21 00:34:01 -0700221
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000222 measurements.extendLifetime(entryA, EXTEND_A);
223 measurements.extendLifetime(entryC, EXTEND_C);
Junxiao Shi19838042014-06-21 00:34:01 -0700224 // remaining lifetime:
225 // A = initial lifetime, because it's extended by less duration
226 // B = initial lifetime
227 // C = EXTEND_C
Junxiao Shi19838042014-06-21 00:34:01 -0700228
Junxiao Shie368d992014-12-02 23:44:31 -0700229 this->advanceClocks(time::milliseconds(100), CHECK1);
230 BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr);
231 BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr);
232 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700233 BOOST_CHECK_EQUAL(measurements.size(), 3);
234
Junxiao Shie368d992014-12-02 23:44:31 -0700235 this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1);
236 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
237 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
238 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700239 BOOST_CHECK_EQUAL(measurements.size(), 1);
240
Junxiao Shie368d992014-12-02 23:44:31 -0700241 this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2);
242 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
243 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
244 BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700245 BOOST_CHECK_EQUAL(measurements.size(), 0);
246}
247
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700248BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700249{
Junxiao Shiee5a4442014-07-27 17:13:43 -0700250 size_t nNameTreeEntriesBefore = nameTree.size();
251
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000252 measurements.get("/A");
253 BOOST_CHECK_EQUAL(measurements.size(), 1);
254
Junxiao Shie368d992014-12-02 23:44:31 -0700255 this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10));
Junxiao Shiee5a4442014-07-27 17:13:43 -0700256 BOOST_CHECK_EQUAL(measurements.size(), 0);
257 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
258}
259
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700260BOOST_AUTO_TEST_SUITE_END() // TestMeasurements
261BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi65d00722014-02-17 10:50:20 -0700262
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700263} // namespace tests
Junxiao Shi80f9fcd2016-07-23 02:48:36 +0000264} // namespace measurements
Junxiao Shi65d00722014-02-17 10:50:20 -0700265} // namespace nfd