blob: b199ba956b1a0883d08c3cd875d3be2874a80fe7 [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/*
Ju Panccce0bc2019-06-03 23:33:03 +00003 * Copyright (c) 2013-2019 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"
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050025#include <boost/lexical_cast.hpp>
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060026
27namespace ndn {
28namespace nfd {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060030
Junxiao Shi7357ef22016-09-07 02:39:37 +000031BOOST_AUTO_TEST_SUITE(Mgmt)
32BOOST_AUTO_TEST_SUITE(Nfd)
33BOOST_AUTO_TEST_SUITE(TestFibEntry)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060034
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050035static FibEntry
36makeFibEntry()
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060037{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050038 std::vector<NextHopRecord> nexthops;
39 for (size_t i = 1; i < 4; i++) {
40 nexthops.push_back(NextHopRecord()
41 .setFaceId(i * 10)
Ju Panccce0bc2019-06-03 23:33:03 +000042 .setEndpointId(i * 10 + 1)
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050043 .setCost(i * 100 + 100));
44 }
Ju Panccce0bc2019-06-03 23:33:03 +000045 nexthops.front().unsetEndpointId();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060046
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050047 return FibEntry()
48 .setPrefix("/this/is/a/test")
49 .setNextHopRecords(nexthops.begin(), nexthops.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060050}
51
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050052BOOST_AUTO_TEST_CASE(NextHopRecordEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060053{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050054 NextHopRecord record1;
55 record1.setFaceId(10)
Ju Panccce0bc2019-06-03 23:33:03 +000056 .setEndpointId(7)
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050057 .setCost(200);
58 const Block& wire = record1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060059
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050060 static const uint8_t expected[] = {
Ju Panccce0bc2019-06-03 23:33:03 +000061 0x81, 0x09, // NextHopRecord
62 0x69, 0x01, 0x0a, // FaceId
63 0x6a, 0x01, 0xc8, // Cost
64 0x71, 0x01, 0x07 // EndpointId
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050065 };
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 DiBenedetto6d792d72014-03-15 19:01:36 -060071}
72
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050073BOOST_AUTO_TEST_CASE(NextHopRecordEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060074{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050075 NextHopRecord record1, record2;
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060076
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050077 record1.setFaceId(10)
78 .setCost(200);
79 record2 = record1;
80 BOOST_CHECK_EQUAL(record1, record2);
81
Ju Panccce0bc2019-06-03 23:33:03 +000082 record1.setEndpointId(7);
83 record2.setEndpointId(7);
84 BOOST_CHECK_EQUAL(record1, record2);
85
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050086 record2.setFaceId(42);
87 BOOST_CHECK_NE(record1, record2);
88
89 record2 = record1;
90 record2.setCost(42);
91 BOOST_CHECK_NE(record1, record2);
Ju Panccce0bc2019-06-03 23:33:03 +000092
93 record2 = record1;
94 record2.setEndpointId(42);
95 BOOST_CHECK_NE(record1, record2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060096}
97
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050098BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060099{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500100 FibEntry entry1;
101 entry1.setPrefix("/this/is/a/test");
102 BOOST_REQUIRE(entry1.getNextHopRecords().empty());
103 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600104
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500105 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 DiBenedetto6d792d72014-03-15 19:01:36 -0600115}
116
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500117BOOST_AUTO_TEST_CASE(FibEntryEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600118{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500119 FibEntry entry1 = makeFibEntry();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600120 NextHopRecord oneMore;
121 oneMore.setFaceId(40);
Ju Panccce0bc2019-06-03 23:33:03 +0000122 oneMore.setEndpointId(8);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600123 oneMore.setCost(500);
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500124 entry1.addNextHopRecord(oneMore);
125 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600126
Ju Panccce0bc2019-06-03 23:33:03 +0000127static 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 Pesaventoa6f32ca2017-02-11 20:08:23 -0500150 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Alexander Afanasyev1c5a1a92014-03-21 13:32:36 -0700151 wire.begin(), wire.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600152
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500153 FibEntry entry2(wire);
154 BOOST_CHECK_EQUAL(entry1, entry2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600155}
156
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500157BOOST_AUTO_TEST_CASE(FibEntryEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600158{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500159 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 Panccce0bc2019-06-03 23:33:03 +0000178 .setEndpointId(7)
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500179 .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 Panccce0bc2019-06-03 23:33:03 +0000186 .setEndpointId(22)
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500187 .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
203BOOST_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 DiBenedetto6d792d72014-03-15 19:01:36 -0600209 FibEntry entry;
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500210 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
211 "FibEntry(Prefix: /,\n"
212 " NextHops: []\n"
213 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600214
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500215 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 Panccce0bc2019-06-03 23:33:03 +0000219 " NextHopRecord(FaceId: 20, EndpointId: 21, Cost: 300),\n"
220 " NextHopRecord(FaceId: 30, EndpointId: 31, Cost: 400)]\n"
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500221 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600222}
223
Junxiao Shi7357ef22016-09-07 02:39:37 +0000224BOOST_AUTO_TEST_SUITE_END() // TestFibEntry
225BOOST_AUTO_TEST_SUITE_END() // Nfd
226BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600227
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800228} // namespace tests
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600229} // namespace nfd
230} // namespace ndn