blob: 3427c95bc43607ee5f27b92113d94b6814416074 [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Vince12e49462014-06-09 13:29:32 -05003 * Copyright (c) 2014, Regents of the University of California,
4 * 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
30namespace nfd {
31namespace rib {
32namespace tests {
33
34BOOST_FIXTURE_TEST_SUITE(Rib, nfd::tests::BaseFixture)
35
Vince12e49462014-06-09 13:29:32 -050036BOOST_AUTO_TEST_CASE(RibEntry)
37{
38 rib::RibEntry entry;
39
Vince Lehman218be0a2015-01-15 17:25:20 -060040 rib::Route route1;
41 route1.faceId = 1;
42 route1.origin = 0;
Vince12e49462014-06-09 13:29:32 -050043
Vince Lehman218be0a2015-01-15 17:25:20 -060044 entry.insertRoute(route1);
45 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050046
Vince Lehman218be0a2015-01-15 17:25:20 -060047 Route route2;
48 route2.faceId = 1;
49 route2.origin = 128;
Vince12e49462014-06-09 13:29:32 -050050
Vince Lehman218be0a2015-01-15 17:25:20 -060051 entry.insertRoute(route2);
52 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -050053
Vince Lehman218be0a2015-01-15 17:25:20 -060054 entry.eraseRoute(route1);
55 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050056
Vince Lehman218be0a2015-01-15 17:25:20 -060057 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
58 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050059
Vince Lehman218be0a2015-01-15 17:25:20 -060060 entry.insertRoute(route2);
61 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050062
Vince Lehman218be0a2015-01-15 17:25:20 -060063 entry.eraseRoute(route1);
64 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
65 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050066}
67
Vince Lehman4387e782014-06-19 16:57:45 -050068BOOST_AUTO_TEST_CASE(Parent)
Vince12e49462014-06-09 13:29:32 -050069{
70 rib::Rib rib;
71
Vince Lehman218be0a2015-01-15 17:25:20 -060072 Route root;
Vince12e49462014-06-09 13:29:32 -050073 Name name1("/");
74 root.faceId = 1;
75 root.origin = 20;
76 rib.insert(name1, root);
77
Vince Lehman218be0a2015-01-15 17:25:20 -060078 Route route1;
Vince12e49462014-06-09 13:29:32 -050079 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -060080 route1.faceId = 2;
81 route1.origin = 20;
82 rib.insert(name2, route1);
Vince12e49462014-06-09 13:29:32 -050083
Vince Lehman218be0a2015-01-15 17:25:20 -060084 Route route2;
Vince12e49462014-06-09 13:29:32 -050085 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -060086 route2.faceId = 3;
87 route2.origin = 20;
88 rib.insert(name3, route2);
Vince12e49462014-06-09 13:29:32 -050089
90 shared_ptr<rib::RibEntry> ribEntry = rib.findParent(name3);
91 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060092 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 2);
Vince12e49462014-06-09 13:29:32 -050093
94 ribEntry = rib.findParent(name2);
95 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060096 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -050097
Vince Lehman218be0a2015-01-15 17:25:20 -060098 Route route3;
Vince12e49462014-06-09 13:29:32 -050099 Name name4("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600100 route2.faceId = 3;
101 route2.origin = 20;
102 rib.insert(name4, route3);
Vince12e49462014-06-09 13:29:32 -0500103
104 ribEntry = rib.findParent(name4);
105 BOOST_CHECK(ribEntry != shared_ptr<rib::RibEntry>());
Vince Lehman218be0a2015-01-15 17:25:20 -0600106 BOOST_CHECK(ribEntry->getRoutes().front().faceId == 2);
Vince12e49462014-06-09 13:29:32 -0500107}
108
Vince Lehman4387e782014-06-19 16:57:45 -0500109BOOST_AUTO_TEST_CASE(Children)
Vince12e49462014-06-09 13:29:32 -0500110{
111 rib::Rib rib;
112
Vince Lehman218be0a2015-01-15 17:25:20 -0600113 Route route1;
Vince12e49462014-06-09 13:29:32 -0500114 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600115 route1.faceId = 1;
116 route1.origin = 20;
117 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500118
Vince Lehman218be0a2015-01-15 17:25:20 -0600119 Route route2;
Vince12e49462014-06-09 13:29:32 -0500120 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600121 route2.faceId = 2;
122 route2.origin = 20;
123 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500124
Vince Lehman218be0a2015-01-15 17:25:20 -0600125 Route route3;
Vince12e49462014-06-09 13:29:32 -0500126 Name name3("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600127 route3.faceId = 3;
128 route3.origin = 20;
129 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500130
131 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 2);
132 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
133 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
134
Vince Lehman218be0a2015-01-15 17:25:20 -0600135 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500136 Name name4("/hello");
137 entry4.faceId = 4;
138 entry4.origin = 20;
139 rib.insert(name4, entry4);
140
141 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 1);
142 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
143 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
144 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getChildren().size(), 2);
145
146 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().front()->getName(), "/hello");
147 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getParent()->getName(), "/");
148
149 BOOST_REQUIRE(static_cast<bool>((rib.find(name2)->second)->getParent()));
150 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getParent()->getName(), name4);
151 BOOST_REQUIRE(static_cast<bool>((rib.find(name3)->second)->getParent()));
152 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getParent()->getName(), name4);
153}
154
Vince Lehman4387e782014-06-19 16:57:45 -0500155BOOST_AUTO_TEST_CASE(EraseFace)
Vince12e49462014-06-09 13:29:32 -0500156{
157 rib::Rib rib;
158
Vince Lehman218be0a2015-01-15 17:25:20 -0600159 Route route1;
Vince12e49462014-06-09 13:29:32 -0500160 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600161 route1.faceId = 1;
162 route1.origin = 20;
163 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500164
Vince Lehman218be0a2015-01-15 17:25:20 -0600165 Route route2;
Vince12e49462014-06-09 13:29:32 -0500166 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600167 route2.faceId = 2;
168 route2.origin = 20;
169 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500170
Vince Lehman218be0a2015-01-15 17:25:20 -0600171 Route route3;
Vince12e49462014-06-09 13:29:32 -0500172 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600173 route3.faceId = 1;
174 route3.origin = 20;
175 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500176
Vince Lehman218be0a2015-01-15 17:25:20 -0600177 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500178 Name name4("/not/inserted");
179 entry4.faceId = 1;
180 entry4.origin = 20;
181
182 rib.erase(name4, entry4);
Vince Lehman218be0a2015-01-15 17:25:20 -0600183 rib.erase(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500184
185 BOOST_CHECK(rib.find(name1) == rib.end());
Vince Lehman218be0a2015-01-15 17:25:20 -0600186 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -0500187
Vince Lehman218be0a2015-01-15 17:25:20 -0600188 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500189
Vince Lehman218be0a2015-01-15 17:25:20 -0600190 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 1);
191 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -0500192
Vince Lehman218be0a2015-01-15 17:25:20 -0600193 rib.erase(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500194
195 BOOST_CHECK(rib.find(name2) == rib.end());
196
197 rib.erase(name4, entry4);
198}
199
Vince Lehman4387e782014-06-19 16:57:45 -0500200BOOST_AUTO_TEST_CASE(EraseRibEntry)
Vince12e49462014-06-09 13:29:32 -0500201{
202 rib::Rib rib;
203
Vince Lehman218be0a2015-01-15 17:25:20 -0600204 Route route1;
Vince12e49462014-06-09 13:29:32 -0500205 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600206 route1.faceId = 1;
207 route1.origin = 20;
208 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500209
Vince Lehman218be0a2015-01-15 17:25:20 -0600210 Route route2;
Vince12e49462014-06-09 13:29:32 -0500211 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -0600212 route2.faceId = 2;
213 route2.origin = 20;
214 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500215
Vince Lehman218be0a2015-01-15 17:25:20 -0600216 Route route3;
Vince12e49462014-06-09 13:29:32 -0500217 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600218 route3.faceId = 1;
219 route3.origin = 20;
220 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500221
222 shared_ptr<rib::RibEntry> ribEntry1 = rib.find(name1)->second;
223 shared_ptr<rib::RibEntry> ribEntry2 = rib.find(name2)->second;
224 shared_ptr<rib::RibEntry> ribEntry3 = rib.find(name3)->second;
225
226 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry2);
227 BOOST_CHECK(ribEntry3->getParent() == ribEntry2);
228
Vince Lehman218be0a2015-01-15 17:25:20 -0600229 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500230 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry3);
231 BOOST_CHECK(ribEntry3->getParent() == ribEntry1);
232}
233
Vince Lehman4387e782014-06-19 16:57:45 -0500234BOOST_AUTO_TEST_CASE(EraseByFaceId)
Vince12e49462014-06-09 13:29:32 -0500235{
236 rib::Rib rib;
237
Vince Lehman218be0a2015-01-15 17:25:20 -0600238 Route route1;
Vince12e49462014-06-09 13:29:32 -0500239 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600240 route1.faceId = 1;
241 route1.origin = 20;
242 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500243
Vince Lehman218be0a2015-01-15 17:25:20 -0600244 Route route2;
Vince12e49462014-06-09 13:29:32 -0500245 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600246 route2.faceId = 2;
247 route2.origin = 20;
248 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500249
Vince Lehman218be0a2015-01-15 17:25:20 -0600250 Route route3;
Vince12e49462014-06-09 13:29:32 -0500251 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600252 route3.faceId = 1;
253 route3.origin = 20;
254 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500255
256 rib.erase(1);
257 BOOST_CHECK(rib.find(name1) == rib.end());
Vince Lehman218be0a2015-01-15 17:25:20 -0600258 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -0500259
260 rib.erase(3);
Vince Lehman218be0a2015-01-15 17:25:20 -0600261 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -0500262
263 rib.erase(2);
264 BOOST_CHECK(rib.find(name2) == rib.end());
265
266 rib.erase(3);
267}
268
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700269BOOST_AUTO_TEST_CASE(Basic)
270{
271 rib::Rib rib;
272
Vince Lehman218be0a2015-01-15 17:25:20 -0600273 Route route1;
Vince12e49462014-06-09 13:29:32 -0500274 Name name1("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600275 route1.faceId = 1;
276 route1.origin = 20;
277 route1.cost = 10;
278 route1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE;
279 route1.expires = time::steady_clock::now() + time::milliseconds(1500);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700280
Vince Lehman218be0a2015-01-15 17:25:20 -0600281 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700282 BOOST_CHECK_EQUAL(rib.size(), 1);
283
Vince Lehman218be0a2015-01-15 17:25:20 -0600284 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700285 BOOST_CHECK_EQUAL(rib.size(), 1);
286
Vince Lehman218be0a2015-01-15 17:25:20 -0600287 Route route2;
Vince12e49462014-06-09 13:29:32 -0500288 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600289 route2.faceId = 1;
290 route2.origin = 20;
291 route2.cost = 100;
292 route2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
293 route2.expires = time::steady_clock::now() + time::seconds(0);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700294
Vince Lehman218be0a2015-01-15 17:25:20 -0600295 rib.insert(name2, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700296 BOOST_CHECK_EQUAL(rib.size(), 1);
297
Vince Lehman218be0a2015-01-15 17:25:20 -0600298 route2.faceId = 2;
299 rib.insert(name2, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700300 BOOST_CHECK_EQUAL(rib.size(), 2);
301
Vince Lehman218be0a2015-01-15 17:25:20 -0600302 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route1.faceId));
303 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route2.faceId));
Vince12e49462014-06-09 13:29:32 -0500304
305 Name name3("/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600306 rib.insert(name3, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700307 BOOST_CHECK_EQUAL(rib.size(), 3);
308
Vince Lehman218be0a2015-01-15 17:25:20 -0600309 route2.origin = 1;
310 rib.insert(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700311 BOOST_CHECK_EQUAL(rib.size(), 4);
312
Vince Lehman218be0a2015-01-15 17:25:20 -0600313 rib.erase(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700314 BOOST_CHECK_EQUAL(rib.size(), 3);
315
Vince12e49462014-06-09 13:29:32 -0500316 Name name4("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600317 rib.erase(name4, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700318 BOOST_CHECK_EQUAL(rib.size(), 3);
319
Vince Lehman218be0a2015-01-15 17:25:20 -0600320 route2.origin = 20;
321 rib.erase(name4, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700322 BOOST_CHECK_EQUAL(rib.size(), 2);
323
Vince Lehman218be0a2015-01-15 17:25:20 -0600324 BOOST_CHECK_EQUAL(rib.find(name2, route2), static_cast<Route*>(0));
325 BOOST_CHECK_NE(rib.find(name1, route1), static_cast<Route*>(0));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700326
Vince Lehman218be0a2015-01-15 17:25:20 -0600327 rib.erase(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700328 BOOST_CHECK_EQUAL(rib.size(), 1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700329}
330
331BOOST_AUTO_TEST_SUITE_END()
332
333} // namespace tests
334} // namespace rib
335} // namespace nfd