blob: eed09f9b17acc376b4b0f4a99881e1f36f1cbf9d [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento97210d52016-10-14 15:45:48 +02003 * Copyright (c) 2014-2016, Regents of the University of California,
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08004 * 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.
Alexander Afanasyev3ecec502014-04-16 13:42:44 -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/>.
Vince12e49462014-06-09 13:29:32 -050024 */
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070025
26#include "rib/rib.hpp"
27
28#include "tests/test-common.hpp"
29
Davide Pesavento97210d52016-10-14 15:45:48 +020030#include <ndn-cxx/encoding/tlv-nfd.hpp>
31
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070032namespace nfd {
33namespace rib {
34namespace tests {
35
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080036BOOST_FIXTURE_TEST_SUITE(TestRib, nfd::tests::BaseFixture)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070037
Vince12e49462014-06-09 13:29:32 -050038BOOST_AUTO_TEST_CASE(RibEntry)
39{
40 rib::RibEntry entry;
41
Vince Lehman218be0a2015-01-15 17:25:20 -060042 rib::Route route1;
43 route1.faceId = 1;
44 route1.origin = 0;
Vince12e49462014-06-09 13:29:32 -050045
Vince Lehman218be0a2015-01-15 17:25:20 -060046 entry.insertRoute(route1);
47 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050048
Vince Lehman218be0a2015-01-15 17:25:20 -060049 Route route2;
50 route2.faceId = 1;
51 route2.origin = 128;
Vince12e49462014-06-09 13:29:32 -050052
Vince Lehman218be0a2015-01-15 17:25:20 -060053 entry.insertRoute(route2);
54 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -050055
Vince Lehman218be0a2015-01-15 17:25:20 -060056 entry.eraseRoute(route1);
57 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050058
Vince Lehman218be0a2015-01-15 17:25:20 -060059 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
60 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050061
Vince Lehman218be0a2015-01-15 17:25:20 -060062 entry.insertRoute(route2);
63 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050064
Vince Lehman218be0a2015-01-15 17:25:20 -060065 entry.eraseRoute(route1);
66 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
67 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050068}
69
Vince Lehman4387e782014-06-19 16:57:45 -050070BOOST_AUTO_TEST_CASE(Parent)
Vince12e49462014-06-09 13:29:32 -050071{
72 rib::Rib rib;
73
Vince Lehman218be0a2015-01-15 17:25:20 -060074 Route root;
Vince12e49462014-06-09 13:29:32 -050075 Name name1("/");
76 root.faceId = 1;
77 root.origin = 20;
78 rib.insert(name1, root);
79
Vince Lehman218be0a2015-01-15 17:25:20 -060080 Route route1;
Vince12e49462014-06-09 13:29:32 -050081 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -060082 route1.faceId = 2;
83 route1.origin = 20;
84 rib.insert(name2, route1);
Vince12e49462014-06-09 13:29:32 -050085
Vince Lehman218be0a2015-01-15 17:25:20 -060086 Route route2;
Vince12e49462014-06-09 13:29:32 -050087 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -060088 route2.faceId = 3;
89 route2.origin = 20;
90 rib.insert(name3, route2);
Vince12e49462014-06-09 13:29:32 -050091
92 shared_ptr<rib::RibEntry> ribEntry = rib.findParent(name3);
93 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060094 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 2);
Vince12e49462014-06-09 13:29:32 -050095
96 ribEntry = rib.findParent(name2);
97 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060098 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -050099
Vince Lehman218be0a2015-01-15 17:25:20 -0600100 Route route3;
Vince12e49462014-06-09 13:29:32 -0500101 Name name4("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600102 route2.faceId = 3;
103 route2.origin = 20;
104 rib.insert(name4, route3);
Vince12e49462014-06-09 13:29:32 -0500105
106 ribEntry = rib.findParent(name4);
107 BOOST_CHECK(ribEntry != shared_ptr<rib::RibEntry>());
Vince Lehman218be0a2015-01-15 17:25:20 -0600108 BOOST_CHECK(ribEntry->getRoutes().front().faceId == 2);
Vince12e49462014-06-09 13:29:32 -0500109}
110
Vince Lehman4387e782014-06-19 16:57:45 -0500111BOOST_AUTO_TEST_CASE(Children)
Vince12e49462014-06-09 13:29:32 -0500112{
113 rib::Rib rib;
114
Vince Lehman218be0a2015-01-15 17:25:20 -0600115 Route route1;
Vince12e49462014-06-09 13:29:32 -0500116 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600117 route1.faceId = 1;
118 route1.origin = 20;
119 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500120
Vince Lehman218be0a2015-01-15 17:25:20 -0600121 Route route2;
Vince12e49462014-06-09 13:29:32 -0500122 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600123 route2.faceId = 2;
124 route2.origin = 20;
125 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500126
Vince Lehman218be0a2015-01-15 17:25:20 -0600127 Route route3;
Vince12e49462014-06-09 13:29:32 -0500128 Name name3("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600129 route3.faceId = 3;
130 route3.origin = 20;
131 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500132
133 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 2);
134 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
135 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
136
Vince Lehman218be0a2015-01-15 17:25:20 -0600137 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500138 Name name4("/hello");
139 entry4.faceId = 4;
140 entry4.origin = 20;
141 rib.insert(name4, entry4);
142
143 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 1);
144 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
145 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
146 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getChildren().size(), 2);
147
148 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().front()->getName(), "/hello");
149 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getParent()->getName(), "/");
150
151 BOOST_REQUIRE(static_cast<bool>((rib.find(name2)->second)->getParent()));
152 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getParent()->getName(), name4);
153 BOOST_REQUIRE(static_cast<bool>((rib.find(name3)->second)->getParent()));
154 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getParent()->getName(), name4);
155}
156
Vince Lehman4387e782014-06-19 16:57:45 -0500157BOOST_AUTO_TEST_CASE(EraseFace)
Vince12e49462014-06-09 13:29:32 -0500158{
159 rib::Rib rib;
160
Vince Lehman218be0a2015-01-15 17:25:20 -0600161 Route route1;
Vince12e49462014-06-09 13:29:32 -0500162 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600163 route1.faceId = 1;
164 route1.origin = 20;
165 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500166
Vince Lehman218be0a2015-01-15 17:25:20 -0600167 Route route2;
Vince12e49462014-06-09 13:29:32 -0500168 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600169 route2.faceId = 2;
170 route2.origin = 20;
171 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500172
Vince Lehman218be0a2015-01-15 17:25:20 -0600173 Route route3;
Vince12e49462014-06-09 13:29:32 -0500174 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600175 route3.faceId = 1;
176 route3.origin = 20;
177 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500178
Vince Lehman218be0a2015-01-15 17:25:20 -0600179 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500180 Name name4("/not/inserted");
181 entry4.faceId = 1;
182 entry4.origin = 20;
183
184 rib.erase(name4, entry4);
Vince Lehman218be0a2015-01-15 17:25:20 -0600185 rib.erase(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500186
187 BOOST_CHECK(rib.find(name1) == rib.end());
Vince Lehman218be0a2015-01-15 17:25:20 -0600188 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -0500189
Vince Lehman218be0a2015-01-15 17:25:20 -0600190 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500191
Vince Lehman218be0a2015-01-15 17:25:20 -0600192 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 1);
193 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -0500194
Vince Lehman218be0a2015-01-15 17:25:20 -0600195 rib.erase(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500196
197 BOOST_CHECK(rib.find(name2) == rib.end());
198
199 rib.erase(name4, entry4);
200}
201
Vince Lehman4387e782014-06-19 16:57:45 -0500202BOOST_AUTO_TEST_CASE(EraseRibEntry)
Vince12e49462014-06-09 13:29:32 -0500203{
204 rib::Rib rib;
205
Vince Lehman218be0a2015-01-15 17:25:20 -0600206 Route route1;
Vince12e49462014-06-09 13:29:32 -0500207 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600208 route1.faceId = 1;
209 route1.origin = 20;
210 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500211
Vince Lehman218be0a2015-01-15 17:25:20 -0600212 Route route2;
Vince12e49462014-06-09 13:29:32 -0500213 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -0600214 route2.faceId = 2;
215 route2.origin = 20;
216 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500217
Vince Lehman218be0a2015-01-15 17:25:20 -0600218 Route route3;
Vince12e49462014-06-09 13:29:32 -0500219 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600220 route3.faceId = 1;
221 route3.origin = 20;
222 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500223
224 shared_ptr<rib::RibEntry> ribEntry1 = rib.find(name1)->second;
225 shared_ptr<rib::RibEntry> ribEntry2 = rib.find(name2)->second;
226 shared_ptr<rib::RibEntry> ribEntry3 = rib.find(name3)->second;
227
228 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry2);
229 BOOST_CHECK(ribEntry3->getParent() == ribEntry2);
230
Vince Lehman218be0a2015-01-15 17:25:20 -0600231 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500232 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry3);
233 BOOST_CHECK(ribEntry3->getParent() == ribEntry1);
234}
235
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700236BOOST_AUTO_TEST_CASE(Basic)
237{
238 rib::Rib rib;
239
Vince Lehman218be0a2015-01-15 17:25:20 -0600240 Route route1;
Vince12e49462014-06-09 13:29:32 -0500241 Name name1("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600242 route1.faceId = 1;
243 route1.origin = 20;
244 route1.cost = 10;
245 route1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE;
246 route1.expires = time::steady_clock::now() + time::milliseconds(1500);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700247
Vince Lehman218be0a2015-01-15 17:25:20 -0600248 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700249 BOOST_CHECK_EQUAL(rib.size(), 1);
250
Vince Lehman218be0a2015-01-15 17:25:20 -0600251 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700252 BOOST_CHECK_EQUAL(rib.size(), 1);
253
Vince Lehman218be0a2015-01-15 17:25:20 -0600254 Route route2;
Vince12e49462014-06-09 13:29:32 -0500255 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600256 route2.faceId = 1;
257 route2.origin = 20;
258 route2.cost = 100;
259 route2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
260 route2.expires = time::steady_clock::now() + time::seconds(0);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261
Vince Lehman218be0a2015-01-15 17:25:20 -0600262 rib.insert(name2, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700263 BOOST_CHECK_EQUAL(rib.size(), 1);
264
Vince Lehman218be0a2015-01-15 17:25:20 -0600265 route2.faceId = 2;
266 rib.insert(name2, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700267 BOOST_CHECK_EQUAL(rib.size(), 2);
268
Vince Lehman218be0a2015-01-15 17:25:20 -0600269 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route1.faceId));
270 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route2.faceId));
Vince12e49462014-06-09 13:29:32 -0500271
272 Name name3("/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600273 rib.insert(name3, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700274 BOOST_CHECK_EQUAL(rib.size(), 3);
275
Vince Lehman218be0a2015-01-15 17:25:20 -0600276 route2.origin = 1;
277 rib.insert(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700278 BOOST_CHECK_EQUAL(rib.size(), 4);
279
Vince Lehman218be0a2015-01-15 17:25:20 -0600280 rib.erase(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700281 BOOST_CHECK_EQUAL(rib.size(), 3);
282
Vince12e49462014-06-09 13:29:32 -0500283 Name name4("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600284 rib.erase(name4, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700285 BOOST_CHECK_EQUAL(rib.size(), 3);
286
Vince Lehman218be0a2015-01-15 17:25:20 -0600287 route2.origin = 20;
288 rib.erase(name4, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700289 BOOST_CHECK_EQUAL(rib.size(), 2);
290
Vince Lehman218be0a2015-01-15 17:25:20 -0600291 BOOST_CHECK_EQUAL(rib.find(name2, route2), static_cast<Route*>(0));
292 BOOST_CHECK_NE(rib.find(name1, route1), static_cast<Route*>(0));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700293
Vince Lehman218be0a2015-01-15 17:25:20 -0600294 rib.erase(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700295 BOOST_CHECK_EQUAL(rib.size(), 1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700296}
297
Davide Pesavento97210d52016-10-14 15:45:48 +0200298BOOST_AUTO_TEST_SUITE_END() // TestRib
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700299
300} // namespace tests
301} // namespace rib
302} // namespace nfd