blob: 81f94e51432bfdb583e5bfc0d7e4ed81d7315ff2 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento152ef442023-04-22 02:02:29 -04003 * Copyright (c) 2013-2023 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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 DiBenedetto6d792d72014-03-15 19:01:36 -060020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/nfd/fib-entry.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040023#include "ndn-cxx/util/concepts.hpp"
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040026
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050027#include <boost/lexical_cast.hpp>
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060028
29namespace ndn {
30namespace nfd {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080031namespace tests {
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060032
Davide Pesavento152ef442023-04-22 02:02:29 -040033BOOST_CONCEPT_ASSERT((StatusDatasetItem<NextHopRecord>));
34BOOST_CONCEPT_ASSERT((StatusDatasetItem<FibEntry>));
35
Junxiao Shi7357ef22016-09-07 02:39:37 +000036BOOST_AUTO_TEST_SUITE(Mgmt)
37BOOST_AUTO_TEST_SUITE(Nfd)
38BOOST_AUTO_TEST_SUITE(TestFibEntry)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060039
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050040static FibEntry
41makeFibEntry()
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060042{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050043 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 DiBenedetto6d792d72014-03-15 19:01:36 -060049
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050050 return FibEntry()
51 .setPrefix("/this/is/a/test")
52 .setNextHopRecords(nexthops.begin(), nexthops.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060053}
54
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050055BOOST_AUTO_TEST_CASE(NextHopRecordEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060056{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050057 NextHopRecord record1;
58 record1.setFaceId(10)
59 .setCost(200);
60 const Block& wire = record1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060061
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050062 static const uint8_t expected[] = {
Ju Pan92dbb002019-08-05 22:59:13 +000063 0x81, 0x06, // NextHopRecord
Ju Panccce0bc2019-06-03 23:33:03 +000064 0x69, 0x01, 0x0a, // FaceId
65 0x6a, 0x01, 0xc8, // Cost
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050066 };
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 DiBenedetto6d792d72014-03-15 19:01:36 -060072}
73
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050074BOOST_AUTO_TEST_CASE(NextHopRecordEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060075{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050076 NextHopRecord record1, record2;
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060077
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050078 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 DiBenedetto6d792d72014-03-15 19:01:36 -060089}
90
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050091BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060092{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050093 FibEntry entry1;
94 entry1.setPrefix("/this/is/a/test");
95 BOOST_REQUIRE(entry1.getNextHopRecords().empty());
96 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060097
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050098 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 DiBenedetto6d792d72014-03-15 19:01:36 -0600108}
109
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500110BOOST_AUTO_TEST_CASE(FibEntryEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600111{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500112 FibEntry entry1 = makeFibEntry();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600113 NextHopRecord oneMore;
114 oneMore.setFaceId(40);
115 oneMore.setCost(500);
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500116 entry1.addNextHopRecord(oneMore);
117 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600118
Ju Pan92dbb002019-08-05 22:59:13 +0000119 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 Pesaventoa6f32ca2017-02-11 20:08:23 -0500139 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Alexander Afanasyev1c5a1a92014-03-21 13:32:36 -0700140 wire.begin(), wire.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600141
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500142 FibEntry entry2(wire);
143 BOOST_CHECK_EQUAL(entry1, entry2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600144}
145
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500146BOOST_AUTO_TEST_CASE(FibEntryEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600147{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500148 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
190BOOST_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 DiBenedetto6d792d72014-03-15 19:01:36 -0600196 FibEntry entry;
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500197 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
198 "FibEntry(Prefix: /,\n"
199 " NextHops: []\n"
200 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600201
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500202 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 Pan92dbb002019-08-05 22:59:13 +0000206 " NextHopRecord(FaceId: 20, Cost: 300),\n"
207 " NextHopRecord(FaceId: 30, Cost: 400)]\n"
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500208 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600209}
210
Junxiao Shi7357ef22016-09-07 02:39:37 +0000211BOOST_AUTO_TEST_SUITE_END() // TestFibEntry
212BOOST_AUTO_TEST_SUITE_END() // Nfd
213BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600214
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800215} // namespace tests
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600216} // namespace nfd
217} // namespace ndn