blob: c8b2e27bfb1ce62abe03e58c19f727503716e5fd [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi4370fde2016-02-24 12:20:46 -07003 * Copyright (c) 2014-2016, 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"
Junxiao Shi65d00722014-02-17 10:50:20 -070031
32namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070033namespace tests {
Junxiao Shi65d00722014-02-17 10:50:20 -070034
Junxiao Shi8c0500f2015-11-11 08:30:16 -070035BOOST_AUTO_TEST_SUITE(Table)
36
37class MeasurementsFixture : public UnitTestTimeFixture
38{
39public:
40 MeasurementsFixture()
41 : measurements(nameTree)
42 {
43 }
44
45public:
46 NameTree nameTree;
47 Measurements measurements;
48};
49
50BOOST_FIXTURE_TEST_SUITE(TestMeasurements, MeasurementsFixture)
Junxiao Shi65d00722014-02-17 10:50:20 -070051
52BOOST_AUTO_TEST_CASE(Get_Parent)
53{
Junxiao Shi8c0500f2015-11-11 08:30:16 -070054 shared_ptr<measurements::Entry> entryAB = measurements.get("/A/B");
Junxiao Shie368d992014-12-02 23:44:31 -070055 BOOST_REQUIRE(entryAB != nullptr);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070056 BOOST_CHECK_EQUAL(entryAB->getName(), "/A/B");
Junxiao Shi65d00722014-02-17 10:50:20 -070057
Junxiao Shi8c0500f2015-11-11 08:30:16 -070058 shared_ptr<measurements::Entry> entry0 = measurements.get("/");
Junxiao Shie368d992014-12-02 23:44:31 -070059 BOOST_REQUIRE(entry0 != nullptr);
Junxiao Shi65d00722014-02-17 10:50:20 -070060
Junxiao Shie368d992014-12-02 23:44:31 -070061 shared_ptr<measurements::Entry> entryA = measurements.getParent(*entryAB);
62 BOOST_REQUIRE(entryA != nullptr);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070063 BOOST_CHECK_EQUAL(entryA->getName(), "/A");
Junxiao Shi65d00722014-02-17 10:50:20 -070064
Junxiao Shie368d992014-12-02 23:44:31 -070065 shared_ptr<measurements::Entry> entry0c = measurements.getParent(*entryA);
Junxiao Shi65d00722014-02-17 10:50:20 -070066 BOOST_CHECK_EQUAL(entry0, entry0c);
67}
68
Junxiao Shi8c0500f2015-11-11 08:30:16 -070069BOOST_AUTO_TEST_CASE(GetWithFibEntry)
70{
71 Fib fib(nameTree);
72
Junxiao Shia6de4292016-07-12 02:08:10 +000073 const fib::Entry* fibA = fib.insert("/A").first;
74 const fib::Entry* fibAB = fib.insert("/A/B").first;
Junxiao Shi8c0500f2015-11-11 08:30:16 -070075
76 shared_ptr<measurements::Entry> entryA = measurements.get(*fibA);
77 BOOST_REQUIRE(entryA != nullptr);
78 BOOST_CHECK_EQUAL(entryA->getName(), "/A");
79
80 shared_ptr<measurements::Entry> entryAB = measurements.get(*fibAB);
81 BOOST_REQUIRE(entryAB != nullptr);
82 BOOST_CHECK_EQUAL(entryAB->getName(), "/A/B");
83}
84
85BOOST_AUTO_TEST_CASE(GetWithEmptyFibEntry) // Bug 3275
86{
87 Fib fib(nameTree);
88
Junxiao Shia6de4292016-07-12 02:08:10 +000089 const fib::Entry& fib0 = fib.findLongestPrefixMatch("/");
Junxiao Shi8c0500f2015-11-11 08:30:16 -070090
Junxiao Shia6de4292016-07-12 02:08:10 +000091 shared_ptr<measurements::Entry> entry0 = measurements.get(fib0);
Junxiao Shi8c0500f2015-11-11 08:30:16 -070092 BOOST_REQUIRE(entry0 != nullptr);
93 BOOST_CHECK_EQUAL(entry0->getName(), "/");
94}
95
96BOOST_AUTO_TEST_CASE(GetWithPitEntry)
97{
98 Pit pit(nameTree);
99
100 shared_ptr<Interest> interestA = makeInterest("/A");
101 shared_ptr<pit::Entry> pitA = pit.insert(*interestA).first;
Junxiao Shi4370fde2016-02-24 12:20:46 -0700102 shared_ptr<Data> dataABC = makeData("/A/B/C");
103 Name fullName = dataABC->getFullName();
104 shared_ptr<Interest> interestFull = makeInterest(fullName);
105 shared_ptr<pit::Entry> pitFull = pit.insert(*interestFull).first;
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700106
107 shared_ptr<measurements::Entry> entryA = measurements.get(*pitA);
108 BOOST_REQUIRE(entryA != nullptr);
109 BOOST_CHECK_EQUAL(entryA->getName(), "/A");
Junxiao Shi4370fde2016-02-24 12:20:46 -0700110
111 shared_ptr<measurements::Entry> entryFull = measurements.get(*pitFull);
112 BOOST_REQUIRE(entryFull != nullptr);
113 BOOST_CHECK_EQUAL(entryFull->getName(), fullName);
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700114}
115
Junxiao Shib30c7b02015-01-07 15:45:54 -0700116class DummyStrategyInfo1 : public fw::StrategyInfo
117{
118public:
119 static constexpr int
120 getTypeId()
121 {
122 return 21;
123 }
124};
125
126class DummyStrategyInfo2 : public fw::StrategyInfo
127{
128public:
129 static constexpr int
130 getTypeId()
131 {
132 return 22;
133 }
134};
135
136BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
137{
Junxiao Shib30c7b02015-01-07 15:45:54 -0700138 measurements.get("/A");
139 measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>();
140 measurements.get("/A/B/C/D");
141
142 shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E");
143 BOOST_REQUIRE(found1 != nullptr);
144 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
145
146 shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
147 measurements::EntryWithStrategyInfo<DummyStrategyInfo1>());
148 BOOST_REQUIRE(found2 != nullptr);
149 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
150
151 shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
152 measurements::EntryWithStrategyInfo<DummyStrategyInfo2>());
153 BOOST_CHECK(found3 == nullptr);
154}
155
Junxiao Shi767cb332015-01-08 09:35:49 -0700156BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry)
157{
Junxiao Shi767cb332015-01-08 09:35:49 -0700158 Pit pit(nameTree);
159
160 measurements.get("/A");
161 measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>();
162 measurements.get("/A/B/C/D");
163
164 shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E");
165 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
166
167 shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch(*pitEntry);
168 BOOST_REQUIRE(found1 != nullptr);
169 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
170
171 shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch(*pitEntry,
172 measurements::EntryWithStrategyInfo<DummyStrategyInfo1>());
173 BOOST_REQUIRE(found2 != nullptr);
174 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
175
176 shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch(*pitEntry,
177 measurements::EntryWithStrategyInfo<DummyStrategyInfo2>());
178 BOOST_CHECK(found3 == nullptr);
179}
180
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700181BOOST_AUTO_TEST_CASE(Lifetime)
Junxiao Shi19838042014-06-21 00:34:01 -0700182{
Junxiao Shi19838042014-06-21 00:34:01 -0700183 Name nameA("ndn:/A");
184 Name nameB("ndn:/B");
185 Name nameC("ndn:/C");
186
187 BOOST_CHECK_EQUAL(measurements.size(), 0);
188
189 shared_ptr<measurements::Entry> entryA = measurements.get(nameA);
190 shared_ptr<measurements::Entry> entryB = measurements.get(nameB);
191 shared_ptr<measurements::Entry> entryC = measurements.get(nameC);
192 BOOST_CHECK_EQUAL(measurements.size(), 3);
193
194 const time::nanoseconds EXTEND_A = time::seconds(2);
195 const time::nanoseconds CHECK1 = time::seconds(3);
196 const time::nanoseconds CHECK2 = time::seconds(5);
197 const time::nanoseconds EXTEND_C = time::seconds(6);
198 const time::nanoseconds CHECK3 = time::seconds(7);
Junxiao Shie368d992014-12-02 23:44:31 -0700199 BOOST_ASSERT(EXTEND_A < CHECK1);
200 BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime());
201 BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2);
202 BOOST_ASSERT(CHECK2 < EXTEND_C);
203 BOOST_ASSERT(EXTEND_C < CHECK3);
Junxiao Shi19838042014-06-21 00:34:01 -0700204
Junxiao Shie368d992014-12-02 23:44:31 -0700205 measurements.extendLifetime(*entryA, EXTEND_A);
206 measurements.extendLifetime(*entryC, EXTEND_C);
Junxiao Shi19838042014-06-21 00:34:01 -0700207 // remaining lifetime:
208 // A = initial lifetime, because it's extended by less duration
209 // B = initial lifetime
210 // C = EXTEND_C
211 entryA.reset();
212 entryB.reset();
213 entryC.reset();
214
Junxiao Shie368d992014-12-02 23:44:31 -0700215 this->advanceClocks(time::milliseconds(100), CHECK1);
216 BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr);
217 BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr);
218 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700219 BOOST_CHECK_EQUAL(measurements.size(), 3);
220
Junxiao Shie368d992014-12-02 23:44:31 -0700221 this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1);
222 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
223 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
224 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700225 BOOST_CHECK_EQUAL(measurements.size(), 1);
226
Junxiao Shie368d992014-12-02 23:44:31 -0700227 this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2);
228 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
229 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
230 BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700231 BOOST_CHECK_EQUAL(measurements.size(), 0);
232}
233
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700234BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700235{
Junxiao Shiee5a4442014-07-27 17:13:43 -0700236 size_t nNameTreeEntriesBefore = nameTree.size();
237
238 shared_ptr<measurements::Entry> entry = measurements.get("/A");
Junxiao Shie368d992014-12-02 23:44:31 -0700239 this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10));
Junxiao Shiee5a4442014-07-27 17:13:43 -0700240 BOOST_CHECK_EQUAL(measurements.size(), 0);
241 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
242}
243
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700244BOOST_AUTO_TEST_SUITE_END() // TestMeasurements
245BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi65d00722014-02-17 10:50:20 -0700246
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700247} // namespace tests
Junxiao Shi65d00722014-02-17 10:50:20 -0700248} // namespace nfd