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