Junxiao Shi | c1e1236 | 2014-01-24 20:03:26 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "table/fib.hpp" |
| 8 | |
| 9 | #include <boost/test/unit_test.hpp> |
| 10 | |
| 11 | namespace ndn { |
| 12 | |
| 13 | class FibTestFace : public Face |
| 14 | { |
| 15 | public: |
| 16 | FibTestFace(FaceId id) |
| 17 | : Face(id) |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | virtual void |
| 22 | sendInterest(const Interest &interest) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | virtual void |
| 27 | sendData(const Data &data) |
| 28 | { |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TableFib) |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE(Entry) |
| 35 | { |
| 36 | Name prefix("ndn:/pxWhfFza"); |
| 37 | boost::shared_ptr<FibTestFace> face1 = make_shared<FibTestFace>(1); |
| 38 | boost::shared_ptr<FibTestFace> face2 = make_shared<FibTestFace>(2); |
| 39 | |
| 40 | fib::Entry entry(prefix); |
| 41 | BOOST_CHECK(entry.getPrefix().equals(prefix)); |
| 42 | |
| 43 | const fib::NextHopList& nexthops1 = entry.getNextHops(); |
| 44 | // [] |
| 45 | BOOST_CHECK_EQUAL(nexthops1.size(), 0); |
| 46 | |
| 47 | entry.addNextHop(face1, 20); |
| 48 | const fib::NextHopList& nexthops2 = entry.getNextHops(); |
| 49 | // [(face1,20)] |
| 50 | BOOST_CHECK_EQUAL(nexthops2.size(), 1); |
| 51 | BOOST_CHECK_EQUAL(nexthops2.begin()->getFace(), face1); |
| 52 | BOOST_CHECK_EQUAL(nexthops2.begin()->getCost(), 20); |
| 53 | |
| 54 | entry.addNextHop(face1, 30); |
| 55 | const fib::NextHopList& nexthops3 = entry.getNextHops(); |
| 56 | // [(face1,30)] |
| 57 | BOOST_CHECK_EQUAL(nexthops3.size(), 1); |
| 58 | BOOST_CHECK_EQUAL(nexthops3.begin()->getFace(), face1); |
| 59 | BOOST_CHECK_EQUAL(nexthops3.begin()->getCost(), 30); |
| 60 | |
| 61 | entry.addNextHop(face2, 40); |
| 62 | const fib::NextHopList& nexthops4 = entry.getNextHops(); |
| 63 | // [(face1,30), (face2,40)] |
| 64 | BOOST_CHECK_EQUAL(nexthops4.size(), 2); |
| 65 | int i = -1; |
| 66 | for (fib::NextHopList::const_iterator it = nexthops4.begin(); |
| 67 | it != nexthops4.end(); ++it) { |
| 68 | ++i; |
| 69 | switch (i) { |
| 70 | case 0 : |
| 71 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 72 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 73 | break; |
| 74 | case 1 : |
| 75 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 76 | BOOST_CHECK_EQUAL(it->getCost(), 40); |
| 77 | break; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | entry.addNextHop(face2, 10); |
| 82 | const fib::NextHopList& nexthops5 = entry.getNextHops(); |
| 83 | // [(face2,10), (face1,30)] |
| 84 | BOOST_CHECK_EQUAL(nexthops5.size(), 2); |
| 85 | i = -1; |
| 86 | for (fib::NextHopList::const_iterator it = nexthops5.begin(); |
| 87 | it != nexthops5.end(); ++it) { |
| 88 | ++i; |
| 89 | switch (i) { |
| 90 | case 0 : |
| 91 | BOOST_CHECK_EQUAL(it->getFace(), face2); |
| 92 | BOOST_CHECK_EQUAL(it->getCost(), 10); |
| 93 | break; |
| 94 | case 1 : |
| 95 | BOOST_CHECK_EQUAL(it->getFace(), face1); |
| 96 | BOOST_CHECK_EQUAL(it->getCost(), 30); |
| 97 | break; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | entry.removeNextHop(face1); |
| 102 | const fib::NextHopList& nexthops6 = entry.getNextHops(); |
| 103 | // [(face2,10)] |
| 104 | BOOST_CHECK_EQUAL(nexthops6.size(), 1); |
| 105 | BOOST_CHECK_EQUAL(nexthops6.begin()->getFace(), face2); |
| 106 | BOOST_CHECK_EQUAL(nexthops6.begin()->getCost(), 10); |
| 107 | |
| 108 | entry.removeNextHop(face1); |
| 109 | const fib::NextHopList& nexthops7 = entry.getNextHops(); |
| 110 | // [(face2,10)] |
| 111 | BOOST_CHECK_EQUAL(nexthops7.size(), 1); |
| 112 | BOOST_CHECK_EQUAL(nexthops7.begin()->getFace(), face2); |
| 113 | BOOST_CHECK_EQUAL(nexthops7.begin()->getCost(), 10); |
| 114 | |
| 115 | entry.removeNextHop(face2); |
| 116 | const fib::NextHopList& nexthops8 = entry.getNextHops(); |
| 117 | // [] |
| 118 | BOOST_CHECK_EQUAL(nexthops8.size(), 0); |
| 119 | |
| 120 | entry.removeNextHop(face2); |
| 121 | const fib::NextHopList& nexthops9 = entry.getNextHops(); |
| 122 | // [] |
| 123 | BOOST_CHECK_EQUAL(nexthops9.size(), 0); |
| 124 | } |
| 125 | |
| 126 | BOOST_AUTO_TEST_CASE(Insert_LongestPrefixMatch) |
| 127 | { |
| 128 | Name nameEmpty; |
| 129 | Name nameA ("ndn:/A"); |
| 130 | Name nameAB ("ndn:/A/B"); |
| 131 | Name nameABC ("ndn:/A/B/C"); |
| 132 | Name nameABCD("ndn:/A/B/C/D"); |
| 133 | Name nameE ("ndn:/E"); |
| 134 | |
| 135 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 136 | shared_ptr<fib::Entry> entry; |
| 137 | |
| 138 | Fib fib; |
| 139 | // ['/'] |
| 140 | |
| 141 | entry = fib.findLongestPrefixMatch(nameA); |
| 142 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 143 | BOOST_CHECK(entry->getPrefix().equals(nameEmpty)); |
| 144 | |
| 145 | insertRes = fib.insert(nameA); |
| 146 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 147 | BOOST_CHECK(insertRes.first->getPrefix().equals(nameA)); |
| 148 | // ['/', '/A'] |
| 149 | |
| 150 | insertRes = fib.insert(nameA); |
| 151 | BOOST_CHECK_EQUAL(insertRes.second, false); |
| 152 | BOOST_CHECK(insertRes.first->getPrefix().equals(nameA)); |
| 153 | // ['/', '/A'] |
| 154 | |
| 155 | entry = fib.findLongestPrefixMatch(nameA); |
| 156 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 157 | BOOST_CHECK(entry->getPrefix().equals(nameA)); |
| 158 | |
| 159 | entry = fib.findLongestPrefixMatch(nameABCD); |
| 160 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 161 | BOOST_CHECK(entry->getPrefix().equals(nameA)); |
| 162 | |
| 163 | insertRes = fib.insert(nameABC); |
| 164 | BOOST_CHECK_EQUAL(insertRes.second, true); |
| 165 | BOOST_CHECK(insertRes.first->getPrefix().equals(nameABC)); |
| 166 | // ['/', '/A', '/A/B/C'] |
| 167 | |
| 168 | entry = fib.findLongestPrefixMatch(nameA); |
| 169 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 170 | BOOST_CHECK(entry->getPrefix().equals(nameA)); |
| 171 | |
| 172 | entry = fib.findLongestPrefixMatch(nameAB); |
| 173 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 174 | BOOST_CHECK(entry->getPrefix().equals(nameA)); |
| 175 | |
| 176 | entry = fib.findLongestPrefixMatch(nameABCD); |
| 177 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 178 | BOOST_CHECK(entry->getPrefix().equals(nameABC)); |
| 179 | |
| 180 | entry = fib.findLongestPrefixMatch(nameE); |
| 181 | BOOST_CHECK_NE(entry.get(), static_cast<fib::Entry*>(0)); |
| 182 | BOOST_CHECK(entry->getPrefix().equals(nameEmpty)); |
| 183 | } |
| 184 | |
| 185 | BOOST_AUTO_TEST_CASE(RemoveNextHopFromAllEntries) |
| 186 | { |
| 187 | boost::shared_ptr<FibTestFace> face1 = make_shared<FibTestFace>(1); |
| 188 | boost::shared_ptr<FibTestFace> face2 = make_shared<FibTestFace>(2); |
| 189 | Name nameA("ndn:/A"); |
| 190 | Name nameB("ndn:/B"); |
| 191 | |
| 192 | std::pair<shared_ptr<fib::Entry>, bool> insertRes; |
| 193 | shared_ptr<fib::Entry> entry; |
| 194 | |
| 195 | Fib fib; |
| 196 | // {'/':[]} |
| 197 | |
| 198 | insertRes = fib.insert(nameA); |
| 199 | insertRes.first->addNextHop(face1, 0); |
| 200 | insertRes.first->addNextHop(face2, 0); |
| 201 | // {'/':[], '/A':[1,2]} |
| 202 | |
| 203 | insertRes = fib.insert(nameB); |
| 204 | insertRes.first->addNextHop(face1, 0); |
| 205 | // {'/':[], '/A':[1,2], '/B':[1]} |
| 206 | |
| 207 | fib.removeNextHopFromAllEntries(face1); |
| 208 | // {'/':[], '/A':[2], '/B':[]} |
| 209 | |
| 210 | entry = fib.findLongestPrefixMatch(nameA); |
| 211 | BOOST_CHECK(entry->getPrefix().equals(nameA)); |
| 212 | const fib::NextHopList& nexthopsA = entry->getNextHops(); |
| 213 | BOOST_CHECK_EQUAL(nexthopsA.size(), 1); |
| 214 | BOOST_CHECK_EQUAL(nexthopsA.begin()->getFace(), face2); |
| 215 | |
| 216 | entry = fib.findLongestPrefixMatch(nameB); |
| 217 | BOOST_CHECK(entry->getPrefix().equals(nameB)); |
| 218 | const fib::NextHopList& nexthopsB = entry->getNextHops(); |
| 219 | BOOST_CHECK_EQUAL(nexthopsB.size(), 0); |
| 220 | } |
| 221 | |
| 222 | BOOST_AUTO_TEST_SUITE_END() |
| 223 | |
| 224 | } // namespace ndn |