blob: 91445c4d0af5bae4d9d0a414fe2485894fca398c [file] [log] [blame]
Alexander Afanasyev3ecec502014-04-16 13:42:44 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev7c10b3b2015-01-20 12:24:27 -08003 * Copyright (c) 2014-2015, 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"
Junxiao Shi0de23a22015-12-03 20:07:02 +000027#include <ndn-cxx/encoding/tlv-nfd.hpp>
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070028
29#include "tests/test-common.hpp"
30
31namespace nfd {
32namespace rib {
33namespace tests {
34
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080035BOOST_FIXTURE_TEST_SUITE(TestRib, nfd::tests::BaseFixture)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070036
Vince12e49462014-06-09 13:29:32 -050037BOOST_AUTO_TEST_CASE(RibEntry)
38{
39 rib::RibEntry entry;
40
Vince Lehman218be0a2015-01-15 17:25:20 -060041 rib::Route route1;
42 route1.faceId = 1;
43 route1.origin = 0;
Vince12e49462014-06-09 13:29:32 -050044
Vince Lehman218be0a2015-01-15 17:25:20 -060045 entry.insertRoute(route1);
46 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050047
Vince Lehman218be0a2015-01-15 17:25:20 -060048 Route route2;
49 route2.faceId = 1;
50 route2.origin = 128;
Vince12e49462014-06-09 13:29:32 -050051
Vince Lehman218be0a2015-01-15 17:25:20 -060052 entry.insertRoute(route2);
53 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -050054
Vince Lehman218be0a2015-01-15 17:25:20 -060055 entry.eraseRoute(route1);
56 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050057
Vince Lehman218be0a2015-01-15 17:25:20 -060058 BOOST_CHECK(entry.findRoute(route1) == entry.getRoutes().end());
59 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050060
Vince Lehman218be0a2015-01-15 17:25:20 -060061 entry.insertRoute(route2);
62 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
Vince12e49462014-06-09 13:29:32 -050063
Vince Lehman218be0a2015-01-15 17:25:20 -060064 entry.eraseRoute(route1);
65 BOOST_CHECK_EQUAL(entry.getRoutes().size(), 1);
66 BOOST_CHECK(entry.findRoute(route2) != entry.getRoutes().end());
Vince12e49462014-06-09 13:29:32 -050067}
68
Vince Lehman4387e782014-06-19 16:57:45 -050069BOOST_AUTO_TEST_CASE(Parent)
Vince12e49462014-06-09 13:29:32 -050070{
71 rib::Rib rib;
72
Vince Lehman218be0a2015-01-15 17:25:20 -060073 Route root;
Vince12e49462014-06-09 13:29:32 -050074 Name name1("/");
75 root.faceId = 1;
76 root.origin = 20;
77 rib.insert(name1, root);
78
Vince Lehman218be0a2015-01-15 17:25:20 -060079 Route route1;
Vince12e49462014-06-09 13:29:32 -050080 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -060081 route1.faceId = 2;
82 route1.origin = 20;
83 rib.insert(name2, route1);
Vince12e49462014-06-09 13:29:32 -050084
Vince Lehman218be0a2015-01-15 17:25:20 -060085 Route route2;
Vince12e49462014-06-09 13:29:32 -050086 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -060087 route2.faceId = 3;
88 route2.origin = 20;
89 rib.insert(name3, route2);
Vince12e49462014-06-09 13:29:32 -050090
91 shared_ptr<rib::RibEntry> ribEntry = rib.findParent(name3);
92 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060093 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 2);
Vince12e49462014-06-09 13:29:32 -050094
95 ribEntry = rib.findParent(name2);
96 BOOST_REQUIRE(static_cast<bool>(ribEntry));
Vince Lehman218be0a2015-01-15 17:25:20 -060097 BOOST_CHECK_EQUAL(ribEntry->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -050098
Vince Lehman218be0a2015-01-15 17:25:20 -060099 Route route3;
Vince12e49462014-06-09 13:29:32 -0500100 Name name4("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600101 route2.faceId = 3;
102 route2.origin = 20;
103 rib.insert(name4, route3);
Vince12e49462014-06-09 13:29:32 -0500104
105 ribEntry = rib.findParent(name4);
106 BOOST_CHECK(ribEntry != shared_ptr<rib::RibEntry>());
Vince Lehman218be0a2015-01-15 17:25:20 -0600107 BOOST_CHECK(ribEntry->getRoutes().front().faceId == 2);
Vince12e49462014-06-09 13:29:32 -0500108}
109
Vince Lehman4387e782014-06-19 16:57:45 -0500110BOOST_AUTO_TEST_CASE(Children)
Vince12e49462014-06-09 13:29:32 -0500111{
112 rib::Rib rib;
113
Vince Lehman218be0a2015-01-15 17:25:20 -0600114 Route route1;
Vince12e49462014-06-09 13:29:32 -0500115 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600116 route1.faceId = 1;
117 route1.origin = 20;
118 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500119
Vince Lehman218be0a2015-01-15 17:25:20 -0600120 Route route2;
Vince12e49462014-06-09 13:29:32 -0500121 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600122 route2.faceId = 2;
123 route2.origin = 20;
124 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500125
Vince Lehman218be0a2015-01-15 17:25:20 -0600126 Route route3;
Vince12e49462014-06-09 13:29:32 -0500127 Name name3("/hello/test/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600128 route3.faceId = 3;
129 route3.origin = 20;
130 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500131
132 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 2);
133 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
134 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
135
Vince Lehman218be0a2015-01-15 17:25:20 -0600136 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500137 Name name4("/hello");
138 entry4.faceId = 4;
139 entry4.origin = 20;
140 rib.insert(name4, entry4);
141
142 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 1);
143 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
144 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
145 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getChildren().size(), 2);
146
147 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().front()->getName(), "/hello");
148 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getParent()->getName(), "/");
149
150 BOOST_REQUIRE(static_cast<bool>((rib.find(name2)->second)->getParent()));
151 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getParent()->getName(), name4);
152 BOOST_REQUIRE(static_cast<bool>((rib.find(name3)->second)->getParent()));
153 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getParent()->getName(), name4);
154}
155
Vince Lehman4387e782014-06-19 16:57:45 -0500156BOOST_AUTO_TEST_CASE(EraseFace)
Vince12e49462014-06-09 13:29:32 -0500157{
158 rib::Rib rib;
159
Vince Lehman218be0a2015-01-15 17:25:20 -0600160 Route route1;
Vince12e49462014-06-09 13:29:32 -0500161 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600162 route1.faceId = 1;
163 route1.origin = 20;
164 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500165
Vince Lehman218be0a2015-01-15 17:25:20 -0600166 Route route2;
Vince12e49462014-06-09 13:29:32 -0500167 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600168 route2.faceId = 2;
169 route2.origin = 20;
170 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500171
Vince Lehman218be0a2015-01-15 17:25:20 -0600172 Route route3;
Vince12e49462014-06-09 13:29:32 -0500173 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600174 route3.faceId = 1;
175 route3.origin = 20;
176 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500177
Vince Lehman218be0a2015-01-15 17:25:20 -0600178 Route entry4;
Vince12e49462014-06-09 13:29:32 -0500179 Name name4("/not/inserted");
180 entry4.faceId = 1;
181 entry4.origin = 20;
182
183 rib.erase(name4, entry4);
Vince Lehman218be0a2015-01-15 17:25:20 -0600184 rib.erase(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500185
186 BOOST_CHECK(rib.find(name1) == rib.end());
Vince Lehman218be0a2015-01-15 17:25:20 -0600187 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 2);
Vince12e49462014-06-09 13:29:32 -0500188
Vince Lehman218be0a2015-01-15 17:25:20 -0600189 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500190
Vince Lehman218be0a2015-01-15 17:25:20 -0600191 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().size(), 1);
192 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getRoutes().front().faceId, 1);
Vince12e49462014-06-09 13:29:32 -0500193
Vince Lehman218be0a2015-01-15 17:25:20 -0600194 rib.erase(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500195
196 BOOST_CHECK(rib.find(name2) == rib.end());
197
198 rib.erase(name4, entry4);
199}
200
Vince Lehman4387e782014-06-19 16:57:45 -0500201BOOST_AUTO_TEST_CASE(EraseRibEntry)
Vince12e49462014-06-09 13:29:32 -0500202{
203 rib::Rib rib;
204
Vince Lehman218be0a2015-01-15 17:25:20 -0600205 Route route1;
Vince12e49462014-06-09 13:29:32 -0500206 Name name1("/");
Vince Lehman218be0a2015-01-15 17:25:20 -0600207 route1.faceId = 1;
208 route1.origin = 20;
209 rib.insert(name1, route1);
Vince12e49462014-06-09 13:29:32 -0500210
Vince Lehman218be0a2015-01-15 17:25:20 -0600211 Route route2;
Vince12e49462014-06-09 13:29:32 -0500212 Name name2("/hello");
Vince Lehman218be0a2015-01-15 17:25:20 -0600213 route2.faceId = 2;
214 route2.origin = 20;
215 rib.insert(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500216
Vince Lehman218be0a2015-01-15 17:25:20 -0600217 Route route3;
Vince12e49462014-06-09 13:29:32 -0500218 Name name3("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600219 route3.faceId = 1;
220 route3.origin = 20;
221 rib.insert(name3, route3);
Vince12e49462014-06-09 13:29:32 -0500222
223 shared_ptr<rib::RibEntry> ribEntry1 = rib.find(name1)->second;
224 shared_ptr<rib::RibEntry> ribEntry2 = rib.find(name2)->second;
225 shared_ptr<rib::RibEntry> ribEntry3 = rib.find(name3)->second;
226
227 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry2);
228 BOOST_CHECK(ribEntry3->getParent() == ribEntry2);
229
Vince Lehman218be0a2015-01-15 17:25:20 -0600230 rib.erase(name2, route2);
Vince12e49462014-06-09 13:29:32 -0500231 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry3);
232 BOOST_CHECK(ribEntry3->getParent() == ribEntry1);
233}
234
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700235BOOST_AUTO_TEST_CASE(Basic)
236{
237 rib::Rib rib;
238
Vince Lehman218be0a2015-01-15 17:25:20 -0600239 Route route1;
Vince12e49462014-06-09 13:29:32 -0500240 Name name1("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600241 route1.faceId = 1;
242 route1.origin = 20;
243 route1.cost = 10;
244 route1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE;
245 route1.expires = time::steady_clock::now() + time::milliseconds(1500);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700246
Vince Lehman218be0a2015-01-15 17:25:20 -0600247 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700248 BOOST_CHECK_EQUAL(rib.size(), 1);
249
Vince Lehman218be0a2015-01-15 17:25:20 -0600250 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700251 BOOST_CHECK_EQUAL(rib.size(), 1);
252
Vince Lehman218be0a2015-01-15 17:25:20 -0600253 Route route2;
Vince12e49462014-06-09 13:29:32 -0500254 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600255 route2.faceId = 1;
256 route2.origin = 20;
257 route2.cost = 100;
258 route2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
259 route2.expires = time::steady_clock::now() + time::seconds(0);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700260
Vince Lehman218be0a2015-01-15 17:25:20 -0600261 rib.insert(name2, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700262 BOOST_CHECK_EQUAL(rib.size(), 1);
263
Vince Lehman218be0a2015-01-15 17:25:20 -0600264 route2.faceId = 2;
265 rib.insert(name2, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700266 BOOST_CHECK_EQUAL(rib.size(), 2);
267
Vince Lehman218be0a2015-01-15 17:25:20 -0600268 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route1.faceId));
269 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route2.faceId));
Vince12e49462014-06-09 13:29:32 -0500270
271 Name name3("/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600272 rib.insert(name3, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700273 BOOST_CHECK_EQUAL(rib.size(), 3);
274
Vince Lehman218be0a2015-01-15 17:25:20 -0600275 route2.origin = 1;
276 rib.insert(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700277 BOOST_CHECK_EQUAL(rib.size(), 4);
278
Vince Lehman218be0a2015-01-15 17:25:20 -0600279 rib.erase(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700280 BOOST_CHECK_EQUAL(rib.size(), 3);
281
Vince12e49462014-06-09 13:29:32 -0500282 Name name4("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600283 rib.erase(name4, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700284 BOOST_CHECK_EQUAL(rib.size(), 3);
285
Vince Lehman218be0a2015-01-15 17:25:20 -0600286 route2.origin = 20;
287 rib.erase(name4, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700288 BOOST_CHECK_EQUAL(rib.size(), 2);
289
Vince Lehman218be0a2015-01-15 17:25:20 -0600290 BOOST_CHECK_EQUAL(rib.find(name2, route2), static_cast<Route*>(0));
291 BOOST_CHECK_NE(rib.find(name1, route1), static_cast<Route*>(0));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700292
Vince Lehman218be0a2015-01-15 17:25:20 -0600293 rib.erase(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700294 BOOST_CHECK_EQUAL(rib.size(), 1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700295}
296
297BOOST_AUTO_TEST_SUITE_END()
298
299} // namespace tests
300} // namespace rib
301} // namespace nfd