Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 3 | * 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 Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 10 | * |
| 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/>. |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 24 | */ |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 25 | |
| 26 | #include "rib/rib.hpp" |
| 27 | |
| 28 | #include "tests/test-common.hpp" |
| 29 | |
| 30 | namespace nfd { |
| 31 | namespace rib { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(Rib, nfd::tests::BaseFixture) |
| 35 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 36 | BOOST_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 | |
Vince Lehman | 281ded7 | 2014-08-21 12:17:08 -0500 | [diff] [blame^] | 63 | entry.eraseFace(face1); |
| 64 | BOOST_CHECK_EQUAL(entry.getFaces().size(), 1); |
| 65 | BOOST_CHECK(entry.findFace(face2) != entry.getFaces().end()); |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 68 | BOOST_AUTO_TEST_CASE(Parent) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 69 | { |
| 70 | rib::Rib rib; |
| 71 | |
| 72 | FaceEntry root; |
| 73 | Name name1("/"); |
| 74 | root.faceId = 1; |
| 75 | root.origin = 20; |
| 76 | rib.insert(name1, root); |
| 77 | |
| 78 | FaceEntry entry1; |
| 79 | Name name2("/hello"); |
| 80 | entry1.faceId = 2; |
| 81 | entry1.origin = 20; |
| 82 | rib.insert(name2, entry1); |
| 83 | |
| 84 | FaceEntry entry2; |
| 85 | Name name3("/hello/world"); |
| 86 | entry2.faceId = 3; |
| 87 | entry2.origin = 20; |
| 88 | rib.insert(name3, entry2); |
| 89 | |
| 90 | shared_ptr<rib::RibEntry> ribEntry = rib.findParent(name3); |
| 91 | BOOST_REQUIRE(static_cast<bool>(ribEntry)); |
| 92 | BOOST_CHECK_EQUAL(ribEntry->getFaces().front().faceId, 2); |
| 93 | |
| 94 | ribEntry = rib.findParent(name2); |
| 95 | BOOST_REQUIRE(static_cast<bool>(ribEntry)); |
| 96 | BOOST_CHECK_EQUAL(ribEntry->getFaces().front().faceId, 1); |
| 97 | |
| 98 | FaceEntry entry3; |
| 99 | Name name4("/hello/test/foo/bar"); |
| 100 | entry2.faceId = 3; |
| 101 | entry2.origin = 20; |
| 102 | rib.insert(name4, entry3); |
| 103 | |
| 104 | ribEntry = rib.findParent(name4); |
| 105 | BOOST_CHECK(ribEntry != shared_ptr<rib::RibEntry>()); |
| 106 | BOOST_CHECK(ribEntry->getFaces().front().faceId == 2); |
| 107 | } |
| 108 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 109 | BOOST_AUTO_TEST_CASE(Children) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 110 | { |
| 111 | rib::Rib rib; |
| 112 | |
| 113 | FaceEntry entry1; |
| 114 | Name name1("/"); |
| 115 | entry1.faceId = 1; |
| 116 | entry1.origin = 20; |
| 117 | rib.insert(name1, entry1); |
| 118 | |
| 119 | FaceEntry entry2; |
| 120 | Name name2("/hello/world"); |
| 121 | entry2.faceId = 2; |
| 122 | entry2.origin = 20; |
| 123 | rib.insert(name2, entry2); |
| 124 | |
| 125 | FaceEntry entry3; |
| 126 | Name name3("/hello/test/foo/bar"); |
| 127 | entry3.faceId = 3; |
| 128 | entry3.origin = 20; |
| 129 | rib.insert(name3, entry3); |
| 130 | |
| 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 | |
| 135 | FaceEntry entry4; |
| 136 | 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 Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 155 | BOOST_AUTO_TEST_CASE(EraseFace) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 156 | { |
| 157 | rib::Rib rib; |
| 158 | |
| 159 | FaceEntry entry1; |
| 160 | Name name1("/"); |
| 161 | entry1.faceId = 1; |
| 162 | entry1.origin = 20; |
| 163 | rib.insert(name1, entry1); |
| 164 | |
| 165 | FaceEntry entry2; |
| 166 | Name name2("/hello/world"); |
| 167 | entry2.faceId = 2; |
| 168 | entry2.origin = 20; |
| 169 | rib.insert(name2, entry2); |
| 170 | |
| 171 | FaceEntry entry3; |
| 172 | Name name3("/hello/world"); |
| 173 | entry3.faceId = 1; |
| 174 | entry3.origin = 20; |
| 175 | rib.insert(name3, entry3); |
| 176 | |
| 177 | FaceEntry entry4; |
| 178 | Name name4("/not/inserted"); |
| 179 | entry4.faceId = 1; |
| 180 | entry4.origin = 20; |
| 181 | |
| 182 | rib.erase(name4, entry4); |
| 183 | rib.erase(name1, entry1); |
| 184 | |
| 185 | BOOST_CHECK(rib.find(name1) == rib.end()); |
| 186 | BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 2); |
| 187 | |
| 188 | rib.erase(name2, entry2); |
| 189 | |
| 190 | BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1); |
| 191 | BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().front().faceId, 1); |
| 192 | |
| 193 | rib.erase(name3, entry3); |
| 194 | |
| 195 | BOOST_CHECK(rib.find(name2) == rib.end()); |
| 196 | |
| 197 | rib.erase(name4, entry4); |
| 198 | } |
| 199 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 200 | BOOST_AUTO_TEST_CASE(EraseRibEntry) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 201 | { |
| 202 | rib::Rib rib; |
| 203 | |
| 204 | FaceEntry entry1; |
| 205 | Name name1("/"); |
| 206 | entry1.faceId = 1; |
| 207 | entry1.origin = 20; |
| 208 | rib.insert(name1, entry1); |
| 209 | |
| 210 | FaceEntry entry2; |
| 211 | Name name2("/hello"); |
| 212 | entry2.faceId = 2; |
| 213 | entry2.origin = 20; |
| 214 | rib.insert(name2, entry2); |
| 215 | |
| 216 | FaceEntry entry3; |
| 217 | Name name3("/hello/world"); |
| 218 | entry3.faceId = 1; |
| 219 | entry3.origin = 20; |
| 220 | rib.insert(name3, entry3); |
| 221 | |
| 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 | |
| 229 | rib.erase(name2, entry2); |
| 230 | BOOST_CHECK(ribEntry1->getChildren().front() == ribEntry3); |
| 231 | BOOST_CHECK(ribEntry3->getParent() == ribEntry1); |
| 232 | } |
| 233 | |
Vince Lehman | 4387e78 | 2014-06-19 16:57:45 -0500 | [diff] [blame] | 234 | BOOST_AUTO_TEST_CASE(EraseByFaceId) |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 235 | { |
| 236 | rib::Rib rib; |
| 237 | |
| 238 | FaceEntry entry1; |
| 239 | Name name1("/"); |
| 240 | entry1.faceId = 1; |
| 241 | entry1.origin = 20; |
| 242 | rib.insert(name1, entry1); |
| 243 | |
| 244 | FaceEntry entry2; |
| 245 | Name name2("/hello/world"); |
| 246 | entry2.faceId = 2; |
| 247 | entry2.origin = 20; |
| 248 | rib.insert(name2, entry2); |
| 249 | |
| 250 | FaceEntry entry3; |
| 251 | Name name3("/hello/world"); |
| 252 | entry3.faceId = 1; |
| 253 | entry3.origin = 20; |
| 254 | rib.insert(name3, entry3); |
| 255 | |
| 256 | rib.erase(1); |
| 257 | BOOST_CHECK(rib.find(name1) == rib.end()); |
| 258 | BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1); |
| 259 | |
| 260 | rib.erase(3); |
| 261 | BOOST_CHECK_EQUAL((rib.find(name2)->second)->getFaces().size(), 1); |
| 262 | |
| 263 | rib.erase(2); |
| 264 | BOOST_CHECK(rib.find(name2) == rib.end()); |
| 265 | |
| 266 | rib.erase(3); |
| 267 | } |
| 268 | |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 269 | BOOST_AUTO_TEST_CASE(Basic) |
| 270 | { |
| 271 | rib::Rib rib; |
| 272 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 273 | FaceEntry entry1; |
| 274 | Name name1("/hello/world"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 275 | entry1.faceId = 1; |
| 276 | entry1.origin = 20; |
| 277 | entry1.cost = 10; |
| 278 | entry1.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT | ndn::nfd::ROUTE_FLAG_CAPTURE; |
| 279 | entry1.expires = time::steady_clock::now() + time::milliseconds(1500); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 280 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 281 | rib.insert(name1, entry1); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(rib.size(), 1); |
| 283 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 284 | rib.insert(name1, entry1); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 285 | BOOST_CHECK_EQUAL(rib.size(), 1); |
| 286 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 287 | FaceEntry entry2; |
| 288 | Name name2("/hello/world"); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 289 | entry2.faceId = 1; |
| 290 | entry2.origin = 20; |
| 291 | entry2.cost = 100; |
| 292 | entry2.flags = ndn::nfd::ROUTE_FLAG_CHILD_INHERIT; |
| 293 | entry2.expires = time::steady_clock::now() + time::seconds(0); |
| 294 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 295 | rib.insert(name2, entry2); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 296 | BOOST_CHECK_EQUAL(rib.size(), 1); |
| 297 | |
| 298 | entry2.faceId = 2; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 299 | rib.insert(name2, entry2); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 300 | BOOST_CHECK_EQUAL(rib.size(), 2); |
| 301 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 302 | BOOST_CHECK(rib.find(name1)->second->hasFaceId(entry1.faceId)); |
| 303 | BOOST_CHECK(rib.find(name1)->second->hasFaceId(entry2.faceId)); |
| 304 | |
| 305 | Name name3("/foo/bar"); |
| 306 | rib.insert(name3, entry2); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 307 | BOOST_CHECK_EQUAL(rib.size(), 3); |
| 308 | |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 309 | entry2.origin = 1; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 310 | rib.insert(name3, entry2); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 311 | BOOST_CHECK_EQUAL(rib.size(), 4); |
| 312 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 313 | rib.erase(name3, entry2); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 314 | BOOST_CHECK_EQUAL(rib.size(), 3); |
| 315 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 316 | Name name4("/hello/world"); |
| 317 | rib.erase(name4, entry2); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 318 | BOOST_CHECK_EQUAL(rib.size(), 3); |
| 319 | |
| 320 | entry2.origin = 20; |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 321 | rib.erase(name4, entry2); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 322 | BOOST_CHECK_EQUAL(rib.size(), 2); |
| 323 | |
Syed Obaid | 3313a37 | 2014-07-01 01:31:33 -0500 | [diff] [blame] | 324 | BOOST_CHECK_EQUAL(rib.find(name2, entry2), static_cast<FaceEntry*>(0)); |
| 325 | BOOST_CHECK_NE(rib.find(name1, entry1), static_cast<FaceEntry*>(0)); |
Alexander Afanasyev | 20d3144 | 2014-04-19 17:00:53 -0700 | [diff] [blame] | 326 | |
Vince | 12e4946 | 2014-06-09 13:29:32 -0500 | [diff] [blame] | 327 | rib.erase(name1, entry1); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 328 | BOOST_CHECK_EQUAL(rib.size(), 1); |
Alexander Afanasyev | 3ecec50 | 2014-04-16 13:42:44 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | BOOST_AUTO_TEST_SUITE_END() |
| 332 | |
| 333 | } // namespace tests |
| 334 | } // namespace rib |
| 335 | } // namespace nfd |