blob: 2a84f671d581c4833af91db02900357183eb6d47 [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/*
3 * Copyright (c) 2013-2018 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[] = {
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 DiBenedetto6d792d72014-03-15 19:01:36 -060065}
66
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050067BOOST_AUTO_TEST_CASE(NextHopRecordEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060068{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050069 NextHopRecord record1, record2;
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060070
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050071 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 DiBenedetto6d792d72014-03-15 19:01:36 -060082}
83
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050084BOOST_AUTO_TEST_CASE(FibEntryNoNextHopsEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060085{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050086 FibEntry entry1;
87 entry1.setPrefix("/this/is/a/test");
88 BOOST_REQUIRE(entry1.getNextHopRecords().empty());
89 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -060090
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -050091 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 DiBenedetto6d792d72014-03-15 19:01:36 -0600101}
102
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500103BOOST_AUTO_TEST_CASE(FibEntryEncode)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600104{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500105 FibEntry entry1 = makeFibEntry();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600106 NextHopRecord oneMore;
107 oneMore.setFaceId(40);
108 oneMore.setCost(500);
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500109 entry1.addNextHopRecord(oneMore);
110 const Block& wire = entry1.wireEncode();
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600111
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500112 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 Afanasyev1c5a1a92014-03-21 13:32:36 -0700119 wire.begin(), wire.end());
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600120
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500121 FibEntry entry2(wire);
122 BOOST_CHECK_EQUAL(entry1, entry2);
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600123}
124
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500125BOOST_AUTO_TEST_CASE(FibEntryEquality)
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600126{
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500127 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
169BOOST_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 DiBenedetto6d792d72014-03-15 19:01:36 -0600175 FibEntry entry;
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500176 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(entry),
177 "FibEntry(Prefix: /,\n"
178 " NextHops: []\n"
179 " )");
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600180
Davide Pesaventoa6f32ca2017-02-11 20:08:23 -0500181 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 DiBenedetto6d792d72014-03-15 19:01:36 -0600188}
189
Junxiao Shi7357ef22016-09-07 02:39:37 +0000190BOOST_AUTO_TEST_SUITE_END() // TestFibEntry
191BOOST_AUTO_TEST_SUITE_END() // Nfd
192BOOST_AUTO_TEST_SUITE_END() // Mgmt
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600193
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800194} // namespace tests
Steve DiBenedetto6d792d72014-03-15 19:01:36 -0600195} // namespace nfd
196} // namespace ndn