blob: c3b0eab9813c0cf6bb2243389d607818954b5d9d [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
40 rib::FaceEntry face1;
41 face1.faceId = 1;
42 face1.origin = 0;
43
44 entry.insertFace(face1);
45 BOOST_CHECK_EQUAL(entry.getFaces().size(), 1);
46
47 FaceEntry face2;
48 face2.faceId = 1;
49 face2.origin = 128;
50
51 entry.insertFace(face2);
52 BOOST_CHECK_EQUAL(entry.getFaces().size(), 2);
53
54 entry.eraseFace(face1);
55 BOOST_CHECK_EQUAL(entry.getFaces().size(), 1);
56
57 BOOST_CHECK(entry.findFace(face1) == entry.getFaces().end());
58 BOOST_CHECK(entry.findFace(face2) != entry.getFaces().end());
59
60 entry.insertFace(face2);
61 BOOST_CHECK_EQUAL(entry.getFaces().size(), 1);
62
63 BOOST_CHECK_EQUAL(entry.eraseFace(face1), false);
64}
65
66BOOST_AUTO_TEST_CASE(Rib_Parent)
67{
68 rib::Rib rib;
69
70 FaceEntry root;
71 Name name1("/");
72 root.faceId = 1;
73 root.origin = 20;
74 rib.insert(name1, root);
75
76 FaceEntry entry1;
77 Name name2("/hello");
78 entry1.faceId = 2;
79 entry1.origin = 20;
80 rib.insert(name2, entry1);
81
82 FaceEntry entry2;
83 Name name3("/hello/world");
84 entry2.faceId = 3;
85 entry2.origin = 20;
86 rib.insert(name3, entry2);
87
88 shared_ptr<rib::RibEntry> ribEntry = rib.findParent(name3);
89 BOOST_REQUIRE(static_cast<bool>(ribEntry));
90 BOOST_CHECK_EQUAL(ribEntry->getFaces().front().faceId, 2);
91
92 ribEntry = rib.findParent(name2);
93 BOOST_REQUIRE(static_cast<bool>(ribEntry));
94 BOOST_CHECK_EQUAL(ribEntry->getFaces().front().faceId, 1);
95
96 FaceEntry entry3;
97 Name name4("/hello/test/foo/bar");
98 entry2.faceId = 3;
99 entry2.origin = 20;
100 rib.insert(name4, entry3);
101
102 ribEntry = rib.findParent(name4);
103 BOOST_CHECK(ribEntry != shared_ptr<rib::RibEntry>());
104 BOOST_CHECK(ribEntry->getFaces().front().faceId == 2);
105}
106
107BOOST_AUTO_TEST_CASE(Rib_Children)
108{
109 rib::Rib rib;
110
111 FaceEntry entry1;
112 Name name1("/");
113 entry1.faceId = 1;
114 entry1.origin = 20;
115 rib.insert(name1, entry1);
116
117 FaceEntry entry2;
118 Name name2("/hello/world");
119 entry2.faceId = 2;
120 entry2.origin = 20;
121 rib.insert(name2, entry2);
122
123 FaceEntry entry3;
124 Name name3("/hello/test/foo/bar");
125 entry3.faceId = 3;
126 entry3.origin = 20;
127 rib.insert(name3, entry3);
128
129 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 2);
130 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
131 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
132
133 FaceEntry entry4;
134 Name name4("/hello");
135 entry4.faceId = 4;
136 entry4.origin = 20;
137 rib.insert(name4, entry4);
138
139 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().size(), 1);
140 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getChildren().size(), 0);
141 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getChildren().size(), 0);
142 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getChildren().size(), 2);
143
144 BOOST_CHECK_EQUAL((rib.find(name1)->second)->getChildren().front()->getName(), "/hello");
145 BOOST_CHECK_EQUAL((rib.find(name4)->second)->getParent()->getName(), "/");
146
147 BOOST_REQUIRE(static_cast<bool>((rib.find(name2)->second)->getParent()));
148 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getParent()->getName(), name4);
149 BOOST_REQUIRE(static_cast<bool>((rib.find(name3)->second)->getParent()));
150 BOOST_CHECK_EQUAL((rib.find(name3)->second)->getParent()->getName(), name4);
151}
152
153BOOST_AUTO_TEST_CASE(Rib_EraseFace)
154{
155 rib::Rib rib;
156
157 FaceEntry entry1;
158 Name name1("/");
159 entry1.faceId = 1;
160 entry1.origin = 20;
161 rib.insert(name1, entry1);
162
163 FaceEntry entry2;
164 Name name2("/hello/world");
165 entry2.faceId = 2;
166 entry2.origin = 20;
167 rib.insert(name2, entry2);
168
169 FaceEntry entry3;
170 Name name3("/hello/world");
171 entry3.faceId = 1;
172 entry3.origin = 20;
173 rib.insert(name3, entry3);
174
175 FaceEntry entry4;
176 Name name4("/not/inserted");
177 entry4.faceId = 1;
178 entry4.origin = 20;
179
180 rib.erase(name4, entry4);
181 rib.erase(name1, entry1);
182
183 BOOST_CHECK(rib.find(name1) == rib.end());
184 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 2);
185
186 rib.erase(name2, entry2);
187
188 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1);
189 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().front().faceId, 1);
190
191 rib.erase(name3, entry3);
192
193 BOOST_CHECK(rib.find(name2) == rib.end());
194
195 rib.erase(name4, entry4);
196}
197
198BOOST_AUTO_TEST_CASE(Rib_EraseRibEntry)
199{
200 rib::Rib rib;
201
202 FaceEntry entry1;
203 Name name1("/");
204 entry1.faceId = 1;
205 entry1.origin = 20;
206 rib.insert(name1, entry1);
207
208 FaceEntry entry2;
209 Name name2("/hello");
210 entry2.faceId = 2;
211 entry2.origin = 20;
212 rib.insert(name2, entry2);
213
214 FaceEntry entry3;
215 Name name3("/hello/world");
216 entry3.faceId = 1;
217 entry3.origin = 20;
218 rib.insert(name3, entry3);
219
220 shared_ptr<rib::RibEntry> ribEntry1 = rib.find(name1)->second;
221 shared_ptr<rib::RibEntry> ribEntry2 = rib.find(name2)->second;
222 shared_ptr<rib::RibEntry> ribEntry3 = rib.find(name3)->second;
223
224 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry2);
225 BOOST_CHECK(ribEntry3->getParent() == ribEntry2);
226
227 rib.erase(name2, entry2);
228 BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry3);
229 BOOST_CHECK(ribEntry3->getParent() == ribEntry1);
230}
231
232BOOST_AUTO_TEST_CASE(Rib_EraseByFaceId)
233{
234 rib::Rib rib;
235
236 FaceEntry entry1;
237 Name name1("/");
238 entry1.faceId = 1;
239 entry1.origin = 20;
240 rib.insert(name1, entry1);
241
242 FaceEntry entry2;
243 Name name2("/hello/world");
244 entry2.faceId = 2;
245 entry2.origin = 20;
246 rib.insert(name2, entry2);
247
248 FaceEntry entry3;
249 Name name3("/hello/world");
250 entry3.faceId = 1;
251 entry3.origin = 20;
252 rib.insert(name3, entry3);
253
254 rib.erase(1);
255 BOOST_CHECK(rib.find(name1) == rib.end());
256 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1);
257
258 rib.erase(3);
259 BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1);
260
261 rib.erase(2);
262 BOOST_CHECK(rib.find(name2) == rib.end());
263
264 rib.erase(3);
265}
266
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700267BOOST_AUTO_TEST_CASE(Basic)
268{
269 rib::Rib rib;
270
Vince12e49462014-06-09 13:29:32 -0500271 FaceEntry entry1;
272 Name name1("/hello/world");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700273 entry1.faceId = 1;
274 entry1.origin = 20;
275 entry1.cost = 10;
276 entry1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE;
277 entry1.expires = time::steady_clock::now() + time::milliseconds(1500);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700278
Vince12e49462014-06-09 13:29:32 -0500279 rib.insert(name1, entry1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700280 BOOST_CHECK_EQUAL(rib.size(), 1);
281
Vince12e49462014-06-09 13:29:32 -0500282 rib.insert(name1, entry1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700283 BOOST_CHECK_EQUAL(rib.size(), 1);
284
Vince12e49462014-06-09 13:29:32 -0500285 FaceEntry entry2;
286 Name name2("/hello/world");
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700287 entry2.faceId = 1;
288 entry2.origin = 20;
289 entry2.cost = 100;
290 entry2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT;
291 entry2.expires = time::steady_clock::now() + time::seconds(0);
292
Vince12e49462014-06-09 13:29:32 -0500293 rib.insert(name2, entry2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700294 BOOST_CHECK_EQUAL(rib.size(), 1);
295
296 entry2.faceId = 2;
Vince12e49462014-06-09 13:29:32 -0500297 rib.insert(name2, entry2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700298 BOOST_CHECK_EQUAL(rib.size(), 2);
299
Vince12e49462014-06-09 13:29:32 -0500300 BOOST_CHECK(rib.find(name1)->second->hasFaceId(entry1.faceId));
301 BOOST_CHECK(rib.find(name1)->second->hasFaceId(entry2.faceId));
302
303 Name name3("/foo/bar");
304 rib.insert(name3, entry2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700305 BOOST_CHECK_EQUAL(rib.size(), 3);
306
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700307 entry2.origin = 1;
Vince12e49462014-06-09 13:29:32 -0500308 rib.insert(name3, entry2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700309 BOOST_CHECK_EQUAL(rib.size(), 4);
310
Vince12e49462014-06-09 13:29:32 -0500311 rib.erase(name3, entry2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700312 BOOST_CHECK_EQUAL(rib.size(), 3);
313
Vince12e49462014-06-09 13:29:32 -0500314 Name name4("/hello/world");
315 rib.erase(name4, entry2);
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700316 BOOST_CHECK_EQUAL(rib.size(), 3);
317
318 entry2.origin = 20;
Vince12e49462014-06-09 13:29:32 -0500319 rib.erase(name4, entry2);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700320 BOOST_CHECK_EQUAL(rib.size(), 2);
321
Vince12e49462014-06-09 13:29:32 -0500322 BOOST_CHECK(rib.find(name2, entry2) == shared_ptr<rib::FaceEntry>());
323 BOOST_CHECK(rib.find(name1, entry1) != shared_ptr<rib::FaceEntry>());
Alexander Afanasyev20d31442014-04-19 17:00:53 -0700324
Vince12e49462014-06-09 13:29:32 -0500325 rib.erase(name1, entry1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700326 BOOST_CHECK_EQUAL(rib.size(), 1);
Alexander Afanasyev3ecec502014-04-16 13:42:44 -0700327}
328
329BOOST_AUTO_TEST_SUITE_END()
330
331} // namespace tests
332} // namespace rib
333} // namespace nfd