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 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 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" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/util/concepts.hpp" |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 26 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 27 | #include <boost/lexical_cast.hpp> |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 28 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 29 | namespace ndn::tests { |
| 30 | |
| 31 | using namespace ndn::nfd; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 32 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<NextHopRecord>)); |
| 34 | BOOST_CONCEPT_ASSERT((StatusDatasetItem<FibEntry>)); |
| 35 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 37 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 38 | BOOST_AUTO_TEST_SUITE(TestFibEntry) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 39 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 40 | static FibEntry |
| 41 | makeFibEntry() |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 42 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 43 | std::vector<NextHopRecord> nexthops; |
| 44 | for (size_t i = 1; i < 4; i++) { |
| 45 | nexthops.push_back(NextHopRecord() |
| 46 | .setFaceId(i * 10) |
| 47 | .setCost(i * 100 + 100)); |
| 48 | } |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 49 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 50 | return FibEntry() |
| 51 | .setPrefix("/this/is/a/test") |
| 52 | .setNextHopRecords(nexthops.begin(), nexthops.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 53 | } |
| 54 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 55 | BOOST_AUTO_TEST_CASE(NextHopRecordEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 56 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 57 | NextHopRecord record1; |
| 58 | record1.setFaceId(10) |
| 59 | .setCost(200); |
| 60 | const Block& wire = record1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 61 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 62 | static const uint8_t expected[] = { |
Ju Pan | 92dbb00 | 2019-08-05 22:59:13 +0000 | [diff] [blame] | 63 | 0x81, 0x06, // NextHopRecord |
Ju Pan | ccce0bc | 2019-06-03 23:33:03 +0000 | [diff] [blame] | 64 | 0x69, 0x01, 0x0a, // FaceId |
| 65 | 0x6a, 0x01, 0xc8, // Cost |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 66 | }; |
| 67 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 68 | wire.begin(), wire.end()); |
| 69 | |
| 70 | NextHopRecord record2(wire); |
| 71 | BOOST_CHECK_EQUAL(record1, record2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 72 | } |
| 73 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 74 | BOOST_AUTO_TEST_CASE(NextHopRecordEquality) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 75 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 76 | NextHopRecord record1, record2; |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 77 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 78 | record1.setFaceId(10) |
| 79 | .setCost(200); |
| 80 | record2 = record1; |
| 81 | BOOST_CHECK_EQUAL(record1, record2); |
| 82 | |
| 83 | record2.setFaceId(42); |
| 84 | BOOST_CHECK_NE(record1, record2); |
| 85 | |
| 86 | record2 = record1; |
| 87 | record2.setCost(42); |
| 88 | BOOST_CHECK_NE(record1, record2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 89 | } |
| 90 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 91 | BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 92 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 93 | FibEntry entry1; |
| 94 | entry1.setPrefix("/this/is/a/test"); |
| 95 | BOOST_REQUIRE(entry1.getNextHopRecords().empty()); |
| 96 | const Block& wire = entry1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 97 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 98 | static const uint8_t expected[] = { |
| 99 | 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, |
| 100 | 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74, |
| 101 | 0x65, 0x73, 0x74 |
| 102 | }; |
| 103 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
| 104 | wire.begin(), wire.end()); |
| 105 | |
| 106 | FibEntry entry2(wire); |
| 107 | BOOST_CHECK_EQUAL(entry1, entry2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 108 | } |
| 109 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 110 | BOOST_AUTO_TEST_CASE(FibEntryEncode) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 111 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 112 | FibEntry entry1 = makeFibEntry(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 113 | NextHopRecord oneMore; |
| 114 | oneMore.setFaceId(40); |
| 115 | oneMore.setCost(500); |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 116 | entry1.addNextHopRecord(oneMore); |
| 117 | const Block& wire = entry1.wireEncode(); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 118 | |
Ju Pan | 92dbb00 | 2019-08-05 22:59:13 +0000 | [diff] [blame] | 119 | static const uint8_t expected[] = { |
| 120 | 0x80, 0x38, // FibEntry |
| 121 | 0x07, 0x13, // Name |
| 122 | 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, // GenericNameComponent |
| 123 | 0x08, 0x02, 0x69, 0x73, // GenericNameComponent |
| 124 | 0x08, 0x01, 0x61, // GenericNameComponent |
| 125 | 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, // GenericNameComponent |
| 126 | 0x81, 0x06, // NextHopRecord |
| 127 | 0x69, 0x01, 0x0a, // FaceId |
| 128 | 0x6a, 0x01, 0xc8, // Cost |
| 129 | 0x81, 0x07, // NextHopRecord |
| 130 | 0x69, 0x01, 0x14, // FaceId |
| 131 | 0x6a, 0x02, 0x01, 0x2c, // Cost |
| 132 | 0x81, 0x07, // NextHopRecord |
| 133 | 0x69, 0x01, 0x1e, // FaceId |
| 134 | 0x6a, 0x02, 0x01, 0x90, // Cost |
| 135 | 0x81, 0x07, // NextHopRecord |
| 136 | 0x69, 0x01, 0x28, // FaceId |
| 137 | 0x6a, 0x02, 0x01, 0xf4, // Cost |
| 138 | }; |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected), |
Alexander Afanasyev | 1c5a1a9 | 2014-03-21 13:32:36 -0700 | [diff] [blame] | 140 | wire.begin(), wire.end()); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 141 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 142 | FibEntry entry2(wire); |
| 143 | BOOST_CHECK_EQUAL(entry1, entry2); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 146 | BOOST_AUTO_TEST_CASE(FibEntryEquality) |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 147 | { |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 148 | FibEntry entry1, entry2; |
| 149 | BOOST_CHECK_EQUAL(entry1, entry2); |
| 150 | |
| 151 | entry1 = entry2 = makeFibEntry(); |
| 152 | BOOST_CHECK_EQUAL(entry1, entry2); |
| 153 | BOOST_CHECK_EQUAL(entry2, entry1); |
| 154 | |
| 155 | entry2.setPrefix("/another/prefix"); |
| 156 | BOOST_CHECK_NE(entry1, entry2); |
| 157 | |
| 158 | entry2 = entry1; |
| 159 | std::vector<NextHopRecord> empty; |
| 160 | entry2.setNextHopRecords(empty.begin(), empty.end()); |
| 161 | BOOST_CHECK_NE(entry1, entry2); |
| 162 | BOOST_CHECK_NE(entry2, entry1); |
| 163 | |
| 164 | entry2 = entry1; |
| 165 | auto nh1 = NextHopRecord() |
| 166 | .setFaceId(1) |
| 167 | .setCost(1000); |
| 168 | entry1.addNextHopRecord(nh1); |
| 169 | BOOST_CHECK_NE(entry1, entry2); |
| 170 | BOOST_CHECK_NE(entry2, entry1); |
| 171 | |
| 172 | auto nh42 = NextHopRecord() |
| 173 | .setFaceId(42) |
| 174 | .setCost(42); |
| 175 | entry1.addNextHopRecord(nh42); |
| 176 | entry2.addNextHopRecord(nh42) |
| 177 | .addNextHopRecord(nh1); |
| 178 | BOOST_CHECK_EQUAL(entry1, entry2); // order of NextHopRecords is irrelevant |
| 179 | BOOST_CHECK_EQUAL(entry2, entry1); |
| 180 | |
| 181 | entry1 = entry2 = makeFibEntry(); |
| 182 | entry1.addNextHopRecord(nh1) |
| 183 | .addNextHopRecord(nh42); |
| 184 | entry2.addNextHopRecord(nh42) |
| 185 | .addNextHopRecord(nh42); |
| 186 | BOOST_CHECK_NE(entry1, entry2); // match each NextHopRecord at most once |
| 187 | BOOST_CHECK_NE(entry2, entry1); |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(Print) |
| 191 | { |
| 192 | NextHopRecord record; |
| 193 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(record), |
| 194 | "NextHopRecord(FaceId: 0, Cost: 0)"); |
| 195 | |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 196 | FibEntry entry; |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 197 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 198 | "FibEntry(Prefix: /,\n" |
| 199 | " NextHops: []\n" |
| 200 | " )"); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 201 | |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 202 | entry = makeFibEntry(); |
| 203 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry), |
| 204 | "FibEntry(Prefix: /this/is/a/test,\n" |
| 205 | " NextHops: [NextHopRecord(FaceId: 10, Cost: 200),\n" |
Ju Pan | 92dbb00 | 2019-08-05 22:59:13 +0000 | [diff] [blame] | 206 | " NextHopRecord(FaceId: 20, Cost: 300),\n" |
| 207 | " NextHopRecord(FaceId: 30, Cost: 400)]\n" |
Davide Pesavento | a6f32ca | 2017-02-11 20:08:23 -0500 | [diff] [blame] | 208 | " )"); |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 209 | } |
| 210 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 211 | BOOST_AUTO_TEST_SUITE_END() // TestFibEntry |
| 212 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 213 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Steve DiBenedetto | 6d792d7 | 2014-03-15 19:01:36 -0600 | [diff] [blame] | 214 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 215 | } // namespace ndn::tests |