Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/nfd/fib-entry.hpp" |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 25 | #include <boost/lexical_cast.hpp> |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 30 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 31 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 32 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 33 | BOOST_AUTO_TEST_SUITE(TestFibEntry) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 34 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 35 | static FibEntry |
| 36 | makeFibEntry() |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 37 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 38 | std::vector<NextHopRecord> nexthops; |
| 39 | for (size_t i = 1; i < 4; i++) { |
| 40 | nexthops.push_back(NextHopRecord() |
| 41 | .setFaceId(i * 10) |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 42 | .setEndpointId(i * 10 + 1) |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 43 | .setCost(i * 100 + 100)); |
| 44 | } |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 45 | nexthops.front().unsetEndpointId(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 46 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 47 | return FibEntry() |
| 48 | .setPrefix("/this/is/a/test") |
| 49 | .setNextHopRecords(nexthops.begin(), nexthops.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 50 | } |
| 51 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 52 | BOOST_AUTO_TEST_CASE(NextHopRecordEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 53 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 54 | NextHopRecord record1; |
| 55 | record1.setFaceId(10) |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 56 | .setEndpointId(7) |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 57 | .setCost(200); |
| 58 | const Block& wire = record1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 59 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 60 | static const uint8_t expected[] = { |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 61 | 0x81, 0x09, // NextHopRecord |
| 62 | 0x69, 0x01, 0x0a, // FaceId |
| 63 | 0x6a, 0x01, 0xc8, // Cost |
| 64 | 0x71, 0x01, 0x07 // EndpointId |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 65 | }; |
| 66 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 67 | wire.begin(), wire.end()); |
| 68 | |
| 69 | NextHopRecord record2(wire); |
| 70 | BOOST_CHECK_EQUAL(record1, record2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 71 | } |
| 72 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 73 | BOOST_AUTO_TEST_CASE(NextHopRecordEquality) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 74 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 75 | NextHopRecord record1, record2; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 76 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 77 | record1.setFaceId(10) |
| 78 | .setCost(200); |
| 79 | record2 = record1; |
| 80 | BOOST_CHECK_EQUAL(record1, record2); |
| 81 | |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 82 | record1.setEndpointId(7); |
| 83 | record2.setEndpointId(7); |
| 84 | BOOST_CHECK_EQUAL(record1, record2); |
| 85 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 86 | record2.setFaceId(42); |
| 87 | BOOST_CHECK_NE(record1, record2); |
| 88 | |
| 89 | record2 = record1; |
| 90 | record2.setCost(42); |
| 91 | BOOST_CHECK_NE(record1, record2); |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 92 | |
| 93 | record2 = record1; |
| 94 | record2.setEndpointId(42); |
| 95 | BOOST_CHECK_NE(record1, record2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 98 | BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 99 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 100 | FibEntry entry1; |
| 101 | entry1.setPrefix("/this/is/a/test"); |
| 102 | BOOST_REQUIRE(entry1.getNextHopRecords().empty()); |
| 103 | const Block& wire = entry1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 104 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 105 | static const uint8_t expected[] = { |
| 106 | 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, |
| 107 | 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74, |
| 108 | 0x65, 0x73, 0x74 |
| 109 | }; |
| 110 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 111 | wire.begin(), wire.end()); |
| 112 | |
| 113 | FibEntry entry2(wire); |
| 114 | BOOST_CHECK_EQUAL(entry1, entry2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 115 | } |
| 116 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 117 | BOOST_AUTO_TEST_CASE(FibEntryEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 118 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 119 | FibEntry entry1 = makeFibEntry(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 120 | NextHopRecord oneMore; |
| 121 | oneMore.setFaceId(40); |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 122 | oneMore.setEndpointId(8); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 123 | oneMore.setCost(500); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 124 | entry1.addNextHopRecord(oneMore); |
| 125 | const Block& wire = entry1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 126 | |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 127 | static const uint8_t expected[] = { |
| 128 | 0x80, 0x41, // FibEntry |
| 129 | 0x07, 0x13, // Name |
| 130 | 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, // GenericNameComponent |
| 131 | 0x08, 0x02, 0x69, 0x73, // GenericNameComponent |
| 132 | 0x08, 0x01, 0x61, // GenericNameComponent |
| 133 | 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, // GenericNameComponent |
| 134 | 0x81, 0x06, // NextHopRecord |
| 135 | 0x69, 0x01, 0x0a, // FaceId |
| 136 | 0x6a, 0x01, 0xc8, // Cost |
| 137 | 0x81, 0x0a, // NextHopRecord |
| 138 | 0x69, 0x01, 0x14, // FaceId |
| 139 | 0x6a, 0x02, 0x01, 0x2c, // Cost |
| 140 | 0x71, 0x01, 0x15, // EndpointId |
| 141 | 0x81, 0x0a, // NextHopRecord |
| 142 | 0x69, 0x01, 0x1e, // FaceId |
| 143 | 0x6a, 0x02, 0x01, 0x90, // Cost |
| 144 | 0x71, 0x01, 0x1f, // EndpointId |
| 145 | 0x81, 0x0a, // NextHopRecord |
| 146 | 0x69, 0x01, 0x28, // FaceId |
| 147 | 0x6a, 0x02, 0x01, 0xf4, // Cost |
| 148 | 0x71, 0x01, 0x08 // EndpointId |
| 149 | }; |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 151 | wire.begin(), wire.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 152 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 153 | FibEntry entry2(wire); |
| 154 | BOOST_CHECK_EQUAL(entry1, entry2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 155 | } |
| 156 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 157 | BOOST_AUTO_TEST_CASE(FibEntryEquality) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 158 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 159 | FibEntry entry1, entry2; |
| 160 | BOOST_CHECK_EQUAL(entry1, entry2); |
| 161 | |
| 162 | entry1 = entry2 = makeFibEntry(); |
| 163 | BOOST_CHECK_EQUAL(entry1, entry2); |
| 164 | BOOST_CHECK_EQUAL(entry2, entry1); |
| 165 | |
| 166 | entry2.setPrefix("/another/prefix"); |
| 167 | BOOST_CHECK_NE(entry1, entry2); |
| 168 | |
| 169 | entry2 = entry1; |
| 170 | std::vector<NextHopRecord> empty; |
| 171 | entry2.setNextHopRecords(empty.begin(), empty.end()); |
| 172 | BOOST_CHECK_NE(entry1, entry2); |
| 173 | BOOST_CHECK_NE(entry2, entry1); |
| 174 | |
| 175 | entry2 = entry1; |
| 176 | auto nh1 = NextHopRecord() |
| 177 | .setFaceId(1) |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 178 | .setEndpointId(7) |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 179 | .setCost(1000); |
| 180 | entry1.addNextHopRecord(nh1); |
| 181 | BOOST_CHECK_NE(entry1, entry2); |
| 182 | BOOST_CHECK_NE(entry2, entry1); |
| 183 | |
| 184 | auto nh42 = NextHopRecord() |
| 185 | .setFaceId(42) |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 186 | .setEndpointId(22) |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 187 | .setCost(42); |
| 188 | entry1.addNextHopRecord(nh42); |
| 189 | entry2.addNextHopRecord(nh42) |
| 190 | .addNextHopRecord(nh1); |
| 191 | BOOST_CHECK_EQUAL(entry1, entry2); // order of NextHopRecords is irrelevant |
| 192 | BOOST_CHECK_EQUAL(entry2, entry1); |
| 193 | |
| 194 | entry1 = entry2 = makeFibEntry(); |
| 195 | entry1.addNextHopRecord(nh1) |
| 196 | .addNextHopRecord(nh42); |
| 197 | entry2.addNextHopRecord(nh42) |
| 198 | .addNextHopRecord(nh42); |
| 199 | BOOST_CHECK_NE(entry1, entry2); // match each NextHopRecord at most once |
| 200 | BOOST_CHECK_NE(entry2, entry1); |
| 201 | } |
| 202 | |
| 203 | BOOST_AUTO_TEST_CASE(Print) |
| 204 | { |
| 205 | NextHopRecord record; |
| 206 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(record), |
| 207 | "NextHopRecord(FaceId: 0, Cost: 0)"); |
| 208 | |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 209 | FibEntry entry; |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 211 | "FibEntry(Prefix: /,\n" |
| 212 | " NextHops: []\n" |
| 213 | " )"); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 214 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 215 | entry = makeFibEntry(); |
| 216 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 217 | "FibEntry(Prefix: /this/is/a/test,\n" |
| 218 | " NextHops: [NextHopRecord(FaceId: 10, Cost: 200),\n" |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 219 | " NextHopRecord(FaceId: 20, EndpointId: 21, Cost: 300),\n" |
| 220 | " NextHopRecord(FaceId: 30, EndpointId: 31, Cost: 400)]\n" |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 221 | " )"); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 222 | } |
| 223 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 224 | BOOST_AUTO_TEST_SUITE_END() // TestFibEntry |
| 225 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 226 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 227 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 228 | } // namespace tests |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 229 | } // namespace nfd |
| 230 | } // namespace ndn |