blob: 486662034b6ed0c00fc8c465b8a41b000cd3f6ce [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0f830802018-01-16 23:58:58 -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.
Junxiao Shibc19b372014-03-23 16:59:25 -070020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/mgmt/nfd/control-parameters.hpp"
Davide Pesavento152ef442023-04-22 02:02:29 -040023#include "ndn-cxx/util/concepts.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070026
27namespace ndn {
28namespace nfd {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080029namespace tests {
Junxiao Shibc19b372014-03-23 16:59:25 -070030
Davide Pesavento152ef442023-04-22 02:02:29 -040031BOOST_CONCEPT_ASSERT((WireEncodable<ControlParameters>));
32BOOST_CONCEPT_ASSERT((WireDecodable<ControlParameters>));
33static_assert(std::is_convertible_v<ControlParameters::Error*, tlv::Error*>,
34 "ControlParameters::Error must inherit from tlv::Error");
35
Junxiao Shi7357ef22016-09-07 02:39:37 +000036BOOST_AUTO_TEST_SUITE(Mgmt)
37BOOST_AUTO_TEST_SUITE(Nfd)
38BOOST_AUTO_TEST_SUITE(TestControlParameters)
Junxiao Shibc19b372014-03-23 16:59:25 -070039
Junxiao Shi22f85682018-01-22 19:23:22 +000040BOOST_AUTO_TEST_CASE(Fields)
Junxiao Shibc19b372014-03-23 16:59:25 -070041{
Junxiao Shi22f85682018-01-22 19:23:22 +000042 ControlParameters input;
Junxiao Shibc19b372014-03-23 16:59:25 -070043
Junxiao Shi22f85682018-01-22 19:23:22 +000044 ControlParameters decoded(input.wireEncode());
Junxiao Shibc19b372014-03-23 16:59:25 -070045 BOOST_CHECK_EQUAL(decoded.hasName(), false);
46 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070047 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -070048 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070049 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shi22f85682018-01-22 19:23:22 +000050 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
51 BOOST_CHECK_EQUAL(decoded.hasCapacity(), false);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050052 BOOST_CHECK_EQUAL(decoded.hasCount(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070053 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -070054 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070055 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070056 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080057 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070058
Junxiao Shi22f85682018-01-22 19:23:22 +000059 input.setName("/name");
60 input.setFaceId(2634);
61 input.setUri("udp4://192.0.2.1:6363");
62 input.setLocalUri("udp4://192.0.2.2:6363");
63 input.setOrigin(ROUTE_ORIGIN_NLSR);
64 input.setCost(1388);
65 input.setCapacity(2632);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050066 input.setCount(3100);
Junxiao Shi22f85682018-01-22 19:23:22 +000067 input.setFlags(0xAFC4);
68 input.setMask(0xF7A1);
69 input.setStrategy("/strategy-name");
70 input.setExpirationPeriod(1800000_ms);
71 input.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shibc19b372014-03-23 16:59:25 -070072
Junxiao Shi22f85682018-01-22 19:23:22 +000073 decoded.wireDecode(input.wireEncode());
74 BOOST_CHECK_EQUAL(decoded.hasName(), true);
75 BOOST_CHECK_EQUAL(decoded.hasFaceId(), true);
76 BOOST_CHECK_EQUAL(decoded.hasUri(), true);
77 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), true);
78 BOOST_CHECK_EQUAL(decoded.hasOrigin(), true);
79 BOOST_CHECK_EQUAL(decoded.hasCost(), true);
80 BOOST_CHECK_EQUAL(decoded.hasCapacity(), true);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050081 BOOST_CHECK_EQUAL(decoded.hasCount(), true);
Junxiao Shi22f85682018-01-22 19:23:22 +000082 BOOST_CHECK_EQUAL(decoded.hasFlags(), true);
83 BOOST_CHECK_EQUAL(decoded.hasMask(), true);
84 BOOST_CHECK_EQUAL(decoded.hasStrategy(), true);
85 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), true);
86 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), true);
Junxiao Shibc19b372014-03-23 16:59:25 -070087
Junxiao Shi22f85682018-01-22 19:23:22 +000088 BOOST_CHECK_EQUAL(decoded.getName(), "/name");
89 BOOST_CHECK_EQUAL(decoded.getFaceId(), 2634);
90 BOOST_CHECK_EQUAL(decoded.getUri(), "udp4://192.0.2.1:6363");
91 BOOST_CHECK_EQUAL(decoded.getLocalUri(), "udp4://192.0.2.2:6363");
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040092 BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR);
Junxiao Shi22f85682018-01-22 19:23:22 +000093 BOOST_CHECK_EQUAL(decoded.getCost(), 1388);
94 BOOST_CHECK_EQUAL(decoded.getCapacity(), 2632);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050095 BOOST_CHECK_EQUAL(decoded.getCount(), 3100);
Junxiao Shi22f85682018-01-22 19:23:22 +000096 BOOST_CHECK_EQUAL(decoded.getFlags(), 0xAFC4);
97 BOOST_CHECK_EQUAL(decoded.getMask(), 0xF7A1);
98 BOOST_CHECK_EQUAL(decoded.getStrategy(), "/strategy-name");
Davide Pesavento0f830802018-01-16 23:58:58 -050099 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), 1800000_ms);
Junxiao Shi22f85682018-01-22 19:23:22 +0000100 BOOST_CHECK_EQUAL(decoded.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700101
Junxiao Shi22f85682018-01-22 19:23:22 +0000102 input.unsetName();
103 input.unsetFaceId();
104 input.unsetUri();
105 input.unsetLocalUri();
106 input.unsetOrigin();
107 input.unsetCost();
108 input.unsetCapacity();
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500109 input.unsetCount();
Junxiao Shi22f85682018-01-22 19:23:22 +0000110 input.unsetFlags();
111 input.unsetMask();
112 input.unsetStrategy();
113 input.unsetExpirationPeriod();
114 input.unsetFacePersistency();
115 BOOST_CHECK_EQUAL(input.hasName(), false);
116 BOOST_CHECK_EQUAL(input.hasFaceId(), false);
117 BOOST_CHECK_EQUAL(input.hasUri(), false);
118 BOOST_CHECK_EQUAL(input.hasLocalUri(), false);
119 BOOST_CHECK_EQUAL(input.hasOrigin(), false);
120 BOOST_CHECK_EQUAL(input.hasCost(), false);
121 BOOST_CHECK_EQUAL(input.hasCapacity(), false);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500122 BOOST_CHECK_EQUAL(input.hasCount(), false);
Junxiao Shi22f85682018-01-22 19:23:22 +0000123 BOOST_CHECK_EQUAL(input.hasFlags(), false);
124 BOOST_CHECK_EQUAL(input.hasMask(), false);
125 BOOST_CHECK_EQUAL(input.hasStrategy(), false);
126 BOOST_CHECK_EQUAL(input.hasExpirationPeriod(), false);
127 BOOST_CHECK_EQUAL(input.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700128}
129
Eric Newberryda916d62016-08-11 23:04:34 -0700130BOOST_AUTO_TEST_CASE(FlagsAndMask)
131{
132 ControlParameters p;
133
134 BOOST_CHECK(!p.hasFlags());
135 BOOST_CHECK(!p.hasMask());
136 BOOST_CHECK(!p.hasFlagBit(0));
137 BOOST_CHECK(!p.getFlagBit(0));
138
139 // Set bit 2 to true (change Mask)
140 p.setFlagBit(2, true);
141 // 2^2 = 4
142 BOOST_CHECK_EQUAL(p.getFlags(), 4);
143 BOOST_CHECK_EQUAL(p.getMask(), 4);
144 BOOST_CHECK(p.hasFlagBit(2));
145 BOOST_CHECK(p.getFlagBit(2));
146 BOOST_CHECK(!p.hasFlagBit(1));
147 BOOST_CHECK(!p.getFlagBit(1));
148
149 // Set bit 3 to true (no change to Mask)
150 p.setFlagBit(3, true, false);
151 // 2^3 + 2^2 = 12
152 BOOST_CHECK_EQUAL(p.getFlags(), 12);
153 // 2^2 = 4
154 BOOST_CHECK_EQUAL(p.getMask(), 4);
155 BOOST_CHECK(!p.hasFlagBit(3));
156 BOOST_CHECK(p.getFlagBit(3));
157 BOOST_CHECK(p.hasFlagBit(2));
158 BOOST_CHECK(p.getFlagBit(2));
159
160 // Set bit 1 to false (change Mask)
161 p.setFlagBit(1, false);
162 // 2^3 + 2^2 = 12
163 BOOST_CHECK_EQUAL(p.getFlags(), 12);
164 // 2^2 + 2^1 = 6
165 BOOST_CHECK_EQUAL(p.getMask(), 6);
166 BOOST_CHECK(!p.hasFlagBit(3));
167 BOOST_CHECK(p.getFlagBit(3));
168 BOOST_CHECK(p.hasFlagBit(2));
169 BOOST_CHECK(p.getFlagBit(2));
170 BOOST_CHECK(p.hasFlagBit(1));
171 BOOST_CHECK(!p.getFlagBit(1));
172
173 // Set bit 2 to false (change Mask)
174 p.setFlagBit(2, false);
175 // 2^3 = 8
176 BOOST_CHECK_EQUAL(p.getFlags(), 8);
177 // 2^2 + 2^1 = 6
178 BOOST_CHECK_EQUAL(p.getMask(), 6);
179 BOOST_CHECK(!p.hasFlagBit(3));
180 BOOST_CHECK(p.getFlagBit(3));
181 BOOST_CHECK(p.hasFlagBit(2));
182 BOOST_CHECK(!p.getFlagBit(2));
183 BOOST_CHECK(p.hasFlagBit(1));
184 BOOST_CHECK(!p.getFlagBit(1));
185
186 // Set bit 0 to true (change Mask)
187 p.setFlagBit(0, true);
188 // 2^3 + 2^0 = 9
189 BOOST_CHECK_EQUAL(p.getFlags(), 9);
190 // 2^2 + 2^1 + 2^0 = 7
191 BOOST_CHECK_EQUAL(p.getMask(), 7);
192 BOOST_CHECK(p.hasFlagBit(0));
193 BOOST_CHECK(p.getFlagBit(0));
194
195 // Unset bit 0
196 p.unsetFlagBit(0);
197 BOOST_REQUIRE(p.hasFlags());
198 BOOST_REQUIRE(p.hasMask());
199 // 2^3 + 2^0 = 9
200 BOOST_CHECK_EQUAL(p.getFlags(), 9);
201 // 2^2 + 2^1 = 6
202 BOOST_CHECK_EQUAL(p.getMask(), 6);
203 BOOST_CHECK(p.hasFlagBit(1));
204 BOOST_CHECK(!p.hasFlagBit(0));
205 BOOST_CHECK(p.getFlagBit(0));
206
207 // Unset bit 3 (already unset in Mask, so no change)
208 p.unsetFlagBit(3);
209 BOOST_REQUIRE(p.hasFlags());
210 BOOST_REQUIRE(p.hasMask());
211 // 2^3 + 2^0 = 9
212 BOOST_CHECK_EQUAL(p.getFlags(), 9);
213 // 2^2 + 2^1 = 6
214 BOOST_CHECK_EQUAL(p.getMask(), 6);
215 BOOST_CHECK(!p.hasFlagBit(3));
216 BOOST_CHECK(p.getFlagBit(3));
217
218 // Unset bit 2
219 p.unsetFlagBit(2);
220 BOOST_REQUIRE(p.hasFlags());
221 BOOST_REQUIRE(p.hasMask());
222 // 2^3 + 2^0 = 9
223 BOOST_CHECK_EQUAL(p.getFlags(), 9);
224 // 2^1 = 2
225 BOOST_CHECK_EQUAL(p.getMask(), 2);
226 BOOST_CHECK(!p.hasFlagBit(2));
227 BOOST_CHECK(!p.getFlagBit(2));
228
229 // Unset bit 1
230 // Flags and Mask fields will be deleted as Mask is now 0
231 p.unsetFlagBit(1);
232 BOOST_CHECK(!p.hasFlags());
233 BOOST_CHECK(!p.hasMask());
234 BOOST_CHECK(!p.hasFlagBit(3));
235 BOOST_CHECK(!p.getFlagBit(3));
236 BOOST_CHECK(!p.hasFlagBit(2));
237 BOOST_CHECK(!p.getFlagBit(2));
238 BOOST_CHECK(!p.hasFlagBit(1));
239 BOOST_CHECK(!p.getFlagBit(1));
240 BOOST_CHECK(!p.hasFlagBit(0));
241 BOOST_CHECK(!p.getFlagBit(0));
242
243 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
244 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
245}
246
Junxiao Shi7357ef22016-09-07 02:39:37 +0000247BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
248BOOST_AUTO_TEST_SUITE_END() // Nfd
249BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700250
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800251} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700252} // namespace nfd
253} // namespace ndn