blob: 25440dd09ef4884dc534a3bd2ef65a5341664d4e [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"
27
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace rib {
32namespace tests {
33
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080034BOOST_FIXTURE_TEST_SUITE(TestRib, nfd::tests::BaseFixture)
Alexander Afanasyev3ecec502014-04-16 13:42:44 -070035
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
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700234BOOST_AUTO_TEST_CASE(Basic)
235{
236 rib::Rib rib;
237
Vince Lehman218be0a2015-01-15 17:25:20 -0600238 Route route1;
Vince12e49462014-06-09 13:29:32 -0500239 Name name1("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600240 route1.faceId = 1;
241 route1.origin = 20;
242 route1.cost = 10;
243 route1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE;
244 route1.expires = time::steady_clock::now() + time::milliseconds(1500);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700245
Vince Lehman218be0a2015-01-15 17:25:20 -0600246 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700247 BOOST_CHECK_EQUAL(rib.size(), 1);
248
Vince Lehman218be0a2015-01-15 17:25:20 -0600249 rib.insert(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700250 BOOST_CHECK_EQUAL(rib.size(), 1);
251
Vince Lehman218be0a2015-01-15 17:25:20 -0600252 Route route2;
Vince12e49462014-06-09 13:29:32 -0500253 Name name2("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600254 route2.faceId = 1;
255 route2.origin = 20;
256 route2.cost = 100;
257 route2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
258 route2.expires = time::steady_clock::now() + time::seconds(0);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700259
Vince Lehman218be0a2015-01-15 17:25:20 -0600260 rib.insert(name2, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700261 BOOST_CHECK_EQUAL(rib.size(), 1);
262
Vince Lehman218be0a2015-01-15 17:25:20 -0600263 route2.faceId = 2;
264 rib.insert(name2, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700265 BOOST_CHECK_EQUAL(rib.size(), 2);
266
Vince Lehman218be0a2015-01-15 17:25:20 -0600267 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route1.faceId));
268 BOOST_CHECK(rib.find(name1)->second->hasFaceId(route2.faceId));
Vince12e49462014-06-09 13:29:32 -0500269
270 Name name3("/foo/bar");
Vince Lehman218be0a2015-01-15 17:25:20 -0600271 rib.insert(name3, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700272 BOOST_CHECK_EQUAL(rib.size(), 3);
273
Vince Lehman218be0a2015-01-15 17:25:20 -0600274 route2.origin = 1;
275 rib.insert(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700276 BOOST_CHECK_EQUAL(rib.size(), 4);
277
Vince Lehman218be0a2015-01-15 17:25:20 -0600278 rib.erase(name3, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700279 BOOST_CHECK_EQUAL(rib.size(), 3);
280
Vince12e49462014-06-09 13:29:32 -0500281 Name name4("/hello/world");
Vince Lehman218be0a2015-01-15 17:25:20 -0600282 rib.erase(name4, route2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700283 BOOST_CHECK_EQUAL(rib.size(), 3);
284
Vince Lehman218be0a2015-01-15 17:25:20 -0600285 route2.origin = 20;
286 rib.erase(name4, route2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700287 BOOST_CHECK_EQUAL(rib.size(), 2);
288
Vince Lehman218be0a2015-01-15 17:25:20 -0600289 BOOST_CHECK_EQUAL(rib.find(name2, route2), static_cast<Route*>(0));
290 BOOST_CHECK_NE(rib.find(name1, route1), static_cast<Route*>(0));
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700291
Vince Lehman218be0a2015-01-15 17:25:20 -0600292 rib.erase(name1, route1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700293 BOOST_CHECK_EQUAL(rib.size(), 1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700294}
295
296BOOST_AUTO_TEST_SUITE_END()
297
298} // namespace tests
299} // namespace rib
300} // namespace nfd