blob: 0f788bbb5b6de5731a11c9b258eed8df5c6448ca [file] [log] [blame]
Junxiao Shi65d00722014-02-17 10:50:20 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi767cb332015-01-08 09:35:49 -07003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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
73 shared_ptr<fib::Entry> fibA = fib.insert("/A").first;
74 shared_ptr<fib::Entry> fibAB = fib.insert("/A/B").first;
75
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
89 shared_ptr<fib::Entry> fib0 = fib.findLongestPrefixMatch("/");
90
91 shared_ptr<measurements::Entry> entry0 = measurements.get(*fib0);
92 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;
102
103 shared_ptr<measurements::Entry> entryA = measurements.get(*pitA);
104 BOOST_REQUIRE(entryA != nullptr);
105 BOOST_CHECK_EQUAL(entryA->getName(), "/A");
106}
107
Junxiao Shib30c7b02015-01-07 15:45:54 -0700108class DummyStrategyInfo1 : public fw::StrategyInfo
109{
110public:
111 static constexpr int
112 getTypeId()
113 {
114 return 21;
115 }
116};
117
118class DummyStrategyInfo2 : public fw::StrategyInfo
119{
120public:
121 static constexpr int
122 getTypeId()
123 {
124 return 22;
125 }
126};
127
128BOOST_AUTO_TEST_CASE(FindLongestPrefixMatch)
129{
Junxiao Shib30c7b02015-01-07 15:45:54 -0700130 measurements.get("/A");
131 measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>();
132 measurements.get("/A/B/C/D");
133
134 shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch("/A/B/C/D/E");
135 BOOST_REQUIRE(found1 != nullptr);
136 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
137
138 shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
139 measurements::EntryWithStrategyInfo<DummyStrategyInfo1>());
140 BOOST_REQUIRE(found2 != nullptr);
141 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
142
143 shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch("/A/B/C/D/E",
144 measurements::EntryWithStrategyInfo<DummyStrategyInfo2>());
145 BOOST_CHECK(found3 == nullptr);
146}
147
Junxiao Shi767cb332015-01-08 09:35:49 -0700148BOOST_AUTO_TEST_CASE(FindLongestPrefixMatchWithPitEntry)
149{
Junxiao Shi767cb332015-01-08 09:35:49 -0700150 Pit pit(nameTree);
151
152 measurements.get("/A");
153 measurements.get("/A/B/C")->getOrCreateStrategyInfo<DummyStrategyInfo1>();
154 measurements.get("/A/B/C/D");
155
156 shared_ptr<Interest> interest = makeInterest("/A/B/C/D/E");
157 shared_ptr<pit::Entry> pitEntry = pit.insert(*interest).first;
158
159 shared_ptr<measurements::Entry> found1 = measurements.findLongestPrefixMatch(*pitEntry);
160 BOOST_REQUIRE(found1 != nullptr);
161 BOOST_CHECK_EQUAL(found1->getName(), "/A/B/C/D");
162
163 shared_ptr<measurements::Entry> found2 = measurements.findLongestPrefixMatch(*pitEntry,
164 measurements::EntryWithStrategyInfo<DummyStrategyInfo1>());
165 BOOST_REQUIRE(found2 != nullptr);
166 BOOST_CHECK_EQUAL(found2->getName(), "/A/B/C");
167
168 shared_ptr<measurements::Entry> found3 = measurements.findLongestPrefixMatch(*pitEntry,
169 measurements::EntryWithStrategyInfo<DummyStrategyInfo2>());
170 BOOST_CHECK(found3 == nullptr);
171}
172
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700173BOOST_AUTO_TEST_CASE(Lifetime)
Junxiao Shi19838042014-06-21 00:34:01 -0700174{
Junxiao Shi19838042014-06-21 00:34:01 -0700175 Name nameA("ndn:/A");
176 Name nameB("ndn:/B");
177 Name nameC("ndn:/C");
178
179 BOOST_CHECK_EQUAL(measurements.size(), 0);
180
181 shared_ptr<measurements::Entry> entryA = measurements.get(nameA);
182 shared_ptr<measurements::Entry> entryB = measurements.get(nameB);
183 shared_ptr<measurements::Entry> entryC = measurements.get(nameC);
184 BOOST_CHECK_EQUAL(measurements.size(), 3);
185
186 const time::nanoseconds EXTEND_A = time::seconds(2);
187 const time::nanoseconds CHECK1 = time::seconds(3);
188 const time::nanoseconds CHECK2 = time::seconds(5);
189 const time::nanoseconds EXTEND_C = time::seconds(6);
190 const time::nanoseconds CHECK3 = time::seconds(7);
Junxiao Shie368d992014-12-02 23:44:31 -0700191 BOOST_ASSERT(EXTEND_A < CHECK1);
192 BOOST_ASSERT(CHECK1 < Measurements::getInitialLifetime());
193 BOOST_ASSERT(Measurements::getInitialLifetime() < CHECK2);
194 BOOST_ASSERT(CHECK2 < EXTEND_C);
195 BOOST_ASSERT(EXTEND_C < CHECK3);
Junxiao Shi19838042014-06-21 00:34:01 -0700196
Junxiao Shie368d992014-12-02 23:44:31 -0700197 measurements.extendLifetime(*entryA, EXTEND_A);
198 measurements.extendLifetime(*entryC, EXTEND_C);
Junxiao Shi19838042014-06-21 00:34:01 -0700199 // remaining lifetime:
200 // A = initial lifetime, because it's extended by less duration
201 // B = initial lifetime
202 // C = EXTEND_C
203 entryA.reset();
204 entryB.reset();
205 entryC.reset();
206
Junxiao Shie368d992014-12-02 23:44:31 -0700207 this->advanceClocks(time::milliseconds(100), CHECK1);
208 BOOST_CHECK(measurements.findExactMatch(nameA) != nullptr);
209 BOOST_CHECK(measurements.findExactMatch(nameB) != nullptr);
210 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700211 BOOST_CHECK_EQUAL(measurements.size(), 3);
212
Junxiao Shie368d992014-12-02 23:44:31 -0700213 this->advanceClocks(time::milliseconds(100), CHECK2 - CHECK1);
214 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
215 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
216 BOOST_CHECK(measurements.findExactMatch(nameC) != nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700217 BOOST_CHECK_EQUAL(measurements.size(), 1);
218
Junxiao Shie368d992014-12-02 23:44:31 -0700219 this->advanceClocks(time::milliseconds(100), CHECK3 - CHECK2);
220 BOOST_CHECK(measurements.findExactMatch(nameA) == nullptr);
221 BOOST_CHECK(measurements.findExactMatch(nameB) == nullptr);
222 BOOST_CHECK(measurements.findExactMatch(nameC) == nullptr);
Junxiao Shi19838042014-06-21 00:34:01 -0700223 BOOST_CHECK_EQUAL(measurements.size(), 0);
224}
225
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700226BOOST_AUTO_TEST_CASE(EraseNameTreeEntry)
Junxiao Shiee5a4442014-07-27 17:13:43 -0700227{
Junxiao Shiee5a4442014-07-27 17:13:43 -0700228 size_t nNameTreeEntriesBefore = nameTree.size();
229
230 shared_ptr<measurements::Entry> entry = measurements.get("/A");
Junxiao Shie368d992014-12-02 23:44:31 -0700231 this->advanceClocks(Measurements::getInitialLifetime() + time::milliseconds(10));
Junxiao Shiee5a4442014-07-27 17:13:43 -0700232 BOOST_CHECK_EQUAL(measurements.size(), 0);
233 BOOST_CHECK_EQUAL(nameTree.size(), nNameTreeEntriesBefore);
234}
235
Junxiao Shi8c0500f2015-11-11 08:30:16 -0700236BOOST_AUTO_TEST_SUITE_END() // TestMeasurements
237BOOST_AUTO_TEST_SUITE_END() // Table
Junxiao Shi65d00722014-02-17 10:50:20 -0700238
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700239} // namespace tests
Junxiao Shi65d00722014-02-17 10:50:20 -0700240} // namespace nfd