blob: ff5d21bf4c488a9084d40843c1f2c37daa624348 [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)
42 .setCost(i * 100 + 100));
43 }
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060044
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050045 return FibEntry()
46 .setPrefix("/this/is/a/test")
47 .setNextHopRecords(nexthops.begin(), nexthops.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060048}
49
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050050BOOST_AUTO_TEST_CASE(NextHopRecordEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060051{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050052 NextHopRecord record1;
53 record1.setFaceId(10)
54 .setCost(200);
55 const Block& wire = record1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060056
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050057 static const uint8_t expected[] = {
Ju Pan92dbb002019-08-05 22:59:13 +000058 0x81, 0x06, // NextHopRecord
Ju Panccce0bc2019-06-03 23:33:03 +000059 0x69, 0x01, 0x0a, // FaceId
60 0x6a, 0x01, 0xc8, // Cost
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050061 };
62 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
63 wire.begin(), wire.end());
64
65 NextHopRecord record2(wire);
66 BOOST_CHECK_EQUAL(record1, record2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060067}
68
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050069BOOST_AUTO_TEST_CASE(NextHopRecordEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060070{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050071 NextHopRecord record1, record2;
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060072
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050073 record1.setFaceId(10)
74 .setCost(200);
75 record2 = record1;
76 BOOST_CHECK_EQUAL(record1, record2);
77
78 record2.setFaceId(42);
79 BOOST_CHECK_NE(record1, record2);
80
81 record2 = record1;
82 record2.setCost(42);
83 BOOST_CHECK_NE(record1, record2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060084}
85
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050086BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060087{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050088 FibEntry entry1;
89 entry1.setPrefix("/this/is/a/test");
90 BOOST_REQUIRE(entry1.getNextHopRecords().empty());
91 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060092
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050093 static const uint8_t expected[] = {
94 0x80, 0x15, 0x07, 0x13, 0x08, 0x04, 0x74, 0x68, 0x69, 0x73,
95 0x08, 0x02, 0x69, 0x73, 0x08, 0x01, 0x61, 0x08, 0x04, 0x74,
96 0x65, 0x73, 0x74
97 };
98 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
99 wire.begin(), wire.end());
100
101 FibEntry entry2(wire);
102 BOOST_CHECK_EQUAL(entry1, entry2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600103}
104
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500105BOOST_AUTO_TEST_CASE(FibEntryEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600106{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500107 FibEntry entry1 = makeFibEntry();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600108 NextHopRecord oneMore;
109 oneMore.setFaceId(40);
110 oneMore.setCost(500);
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500111 entry1.addNextHopRecord(oneMore);
112 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600113
Ju Pan92dbb002019-08-05 22:59:13 +0000114 static const uint8_t expected[] = {
115 0x80, 0x38, // FibEntry
116 0x07, 0x13, // Name
117 0x08, 0x04, 0x74, 0x68, 0x69, 0x73, // GenericNameComponent
118 0x08, 0x02, 0x69, 0x73, // GenericNameComponent
119 0x08, 0x01, 0x61, // GenericNameComponent
120 0x08, 0x04, 0x74, 0x65, 0x73, 0x74, // GenericNameComponent
121 0x81, 0x06, // NextHopRecord
122 0x69, 0x01, 0x0a, // FaceId
123 0x6a, 0x01, 0xc8, // Cost
124 0x81, 0x07, // NextHopRecord
125 0x69, 0x01, 0x14, // FaceId
126 0x6a, 0x02, 0x01, 0x2c, // Cost
127 0x81, 0x07, // NextHopRecord
128 0x69, 0x01, 0x1e, // FaceId
129 0x6a, 0x02, 0x01, 0x90, // Cost
130 0x81, 0x07, // NextHopRecord
131 0x69, 0x01, 0x28, // FaceId
132 0x6a, 0x02, 0x01, 0xf4, // Cost
133 };
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500134 BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
Alexander Afanasyev1c5a1a92014-03-21 13:32:36 -0700135 wire.begin(), wire.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600136
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500137 FibEntry entry2(wire);
138 BOOST_CHECK_EQUAL(entry1, entry2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600139}
140
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500141BOOST_AUTO_TEST_CASE(FibEntryEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600142{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500143 FibEntry entry1, entry2;
144 BOOST_CHECK_EQUAL(entry1, entry2);
145
146 entry1 = entry2 = makeFibEntry();
147 BOOST_CHECK_EQUAL(entry1, entry2);
148 BOOST_CHECK_EQUAL(entry2, entry1);
149
150 entry2.setPrefix("/another/prefix");
151 BOOST_CHECK_NE(entry1, entry2);
152
153 entry2 = entry1;
154 std::vector<NextHopRecord> empty;
155 entry2.setNextHopRecords(empty.begin(), empty.end());
156 BOOST_CHECK_NE(entry1, entry2);
157 BOOST_CHECK_NE(entry2, entry1);
158
159 entry2 = entry1;
160 auto nh1 = NextHopRecord()
161 .setFaceId(1)
162 .setCost(1000);
163 entry1.addNextHopRecord(nh1);
164 BOOST_CHECK_NE(entry1, entry2);
165 BOOST_CHECK_NE(entry2, entry1);
166
167 auto nh42 = NextHopRecord()
168 .setFaceId(42)
169 .setCost(42);
170 entry1.addNextHopRecord(nh42);
171 entry2.addNextHopRecord(nh42)
172 .addNextHopRecord(nh1);
173 BOOST_CHECK_EQUAL(entry1, entry2); // order of NextHopRecords is irrelevant
174 BOOST_CHECK_EQUAL(entry2, entry1);
175
176 entry1 = entry2 = makeFibEntry();
177 entry1.addNextHopRecord(nh1)
178 .addNextHopRecord(nh42);
179 entry2.addNextHopRecord(nh42)
180 .addNextHopRecord(nh42);
181 BOOST_CHECK_NE(entry1, entry2); // match each NextHopRecord at most once
182 BOOST_CHECK_NE(entry2, entry1);
183}
184
185BOOST_AUTO_TEST_CASE(Print)
186{
187 NextHopRecord record;
188 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(record),
189 "NextHopRecord(FaceId: 0, Cost: 0)");
190
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600191 FibEntry entry;
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500192 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
193 "FibEntry(Prefix: /,\n"
194 " NextHops: []\n"
195 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600196
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500197 entry = makeFibEntry();
198 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
199 "FibEntry(Prefix: /this/is/a/test,\n"
200 " NextHops: [NextHopRecord(FaceId: 10, Cost: 200),\n"
Ju Pan92dbb002019-08-05 22:59:13 +0000201 " NextHopRecord(FaceId: 20, Cost: 300),\n"
202 " NextHopRecord(FaceId: 30, Cost: 400)]\n"
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500203 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600204}
205
Junxiao Shi7357ef22016-09-07 02:39:37 +0000206BOOST_AUTO_TEST_SUITE_END() // TestFibEntry
207BOOST_AUTO_TEST_SUITE_END() // Nfd
208BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600209
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800210} // namespace tests
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600211} // namespace nfd
212} // namespace ndn