blob: 30db9edb116dcf417645f8b078f053b46b14c7e9 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shibc19b372014-03-23 16:59:25 -07002/**
Eric Newberryd7f5b282017-03-28 19:55:20 -07003 * Copyright (c) 2013-2017 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
Junxiao Shi7357ef22016-09-07 02:39:37 +000022#include "mgmt/nfd/control-parameters.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070023
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070024#include "boost-test.hpp"
Junxiao Shibc19b372014-03-23 16:59:25 -070025
26namespace ndn {
27namespace nfd {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080028namespace tests {
Junxiao Shibc19b372014-03-23 16:59:25 -070029
Junxiao Shi7357ef22016-09-07 02:39:37 +000030BOOST_AUTO_TEST_SUITE(Mgmt)
31BOOST_AUTO_TEST_SUITE(Nfd)
32BOOST_AUTO_TEST_SUITE(TestControlParameters)
Junxiao Shibc19b372014-03-23 16:59:25 -070033
34BOOST_AUTO_TEST_CASE(FaceOptions)
35{
36 ControlParameters parameters;
37 parameters.setUri("tcp4://192.0.2.1:6363");
Eric Newberryd7f5b282017-03-28 19:55:20 -070038 parameters.setLocalUri("dev://eth0");
Yukai Tud93c5fc2015-08-25 11:37:16 +080039 parameters.setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberryda916d62016-08-11 23:04:34 -070040 parameters.setFlags(1);
41 parameters.setMask(1);
Junxiao Shibc19b372014-03-23 16:59:25 -070042 Block wire = parameters.wireEncode();
43
44 ControlParameters decoded(wire);
45 BOOST_CHECK_EQUAL(decoded.getUri(), "tcp4://192.0.2.1:6363");
Eric Newberryd7f5b282017-03-28 19:55:20 -070046 BOOST_CHECK_EQUAL(decoded.getLocalUri(), "dev://eth0");
Yukai Tud93c5fc2015-08-25 11:37:16 +080047 BOOST_CHECK_EQUAL(decoded.getFacePersistency(), ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberryda916d62016-08-11 23:04:34 -070048 BOOST_CHECK_EQUAL(decoded.getFlags(), 1);
49 BOOST_CHECK_EQUAL(decoded.getMask(), 1);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070050
Junxiao Shibc19b372014-03-23 16:59:25 -070051 BOOST_CHECK_EQUAL(decoded.hasName(), false);
52 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
53 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070054 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070055 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
56 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070057 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070058}
59
60BOOST_AUTO_TEST_CASE(FaceLocalControlOptions)
61{
62 ControlParameters parameters;
63 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
64
65 Block wire = parameters.wireEncode();
66
67 ControlParameters decoded(wire);
68 BOOST_CHECK_EQUAL(decoded.getLocalControlFeature(), LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070069
Junxiao Shibc19b372014-03-23 16:59:25 -070070 BOOST_CHECK_EQUAL(decoded.hasName(), false);
71 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070072 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -070073 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070074 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
75 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
76 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070077 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070078 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080079 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070080}
81
82BOOST_AUTO_TEST_CASE(FibOptions)
83{
84 ControlParameters parameters;
85 parameters.setName("ndn:/example")
86 .setFaceId(4)
87 .setCost(555);
88
89 Block wire = parameters.wireEncode();
90
91 ControlParameters decoded(wire);
92 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
93 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
94 BOOST_CHECK_EQUAL(decoded.getCost(), 555);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070095
Junxiao Shibc19b372014-03-23 16:59:25 -070096 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -070097 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070098 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
99 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
100 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700101 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700102 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700103 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800104 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700105}
106
107BOOST_AUTO_TEST_CASE(StrategyChoiceOptions)
108{
109 ControlParameters parameters;
110 parameters.setName("ndn:/")
111 .setStrategy("ndn:/strategy/A");
112
113 Block wire = parameters.wireEncode();
114
115 ControlParameters decoded(wire);
116 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/"));
117 BOOST_CHECK_EQUAL(decoded.getStrategy(), Name("ndn:/strategy/A"));
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700118
Junxiao Shibc19b372014-03-23 16:59:25 -0700119 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
120 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -0700121 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700122 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700123 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700124 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700125 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700126 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700127 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800128 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700129}
130
131BOOST_AUTO_TEST_CASE(RibOptions)
132{
133 ControlParameters parameters;
134 parameters.setName("ndn:/example")
135 .setFaceId(4)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400136 .setOrigin(ROUTE_ORIGIN_NLSR)
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700137 .setCost(6)
138 .setFlags(0x01)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400139 .setExpirationPeriod(time::minutes(30));
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700140
141 Block wire = parameters.wireEncode();
142
143 ControlParameters decoded(wire);
144 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
145 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400146 BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700147 BOOST_CHECK_EQUAL(decoded.getCost(), 6);
148 BOOST_CHECK_EQUAL(decoded.getFlags(), 0x01);
149 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), time::milliseconds(1800000));
150
151 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -0700152 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700153 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700154 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700155 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800156 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700157}
158
Eric Newberryda916d62016-08-11 23:04:34 -0700159BOOST_AUTO_TEST_CASE(FlagsAndMask)
160{
161 ControlParameters p;
162
163 BOOST_CHECK(!p.hasFlags());
164 BOOST_CHECK(!p.hasMask());
165 BOOST_CHECK(!p.hasFlagBit(0));
166 BOOST_CHECK(!p.getFlagBit(0));
167
168 // Set bit 2 to true (change Mask)
169 p.setFlagBit(2, true);
170 // 2^2 = 4
171 BOOST_CHECK_EQUAL(p.getFlags(), 4);
172 BOOST_CHECK_EQUAL(p.getMask(), 4);
173 BOOST_CHECK(p.hasFlagBit(2));
174 BOOST_CHECK(p.getFlagBit(2));
175 BOOST_CHECK(!p.hasFlagBit(1));
176 BOOST_CHECK(!p.getFlagBit(1));
177
178 // Set bit 3 to true (no change to Mask)
179 p.setFlagBit(3, true, false);
180 // 2^3 + 2^2 = 12
181 BOOST_CHECK_EQUAL(p.getFlags(), 12);
182 // 2^2 = 4
183 BOOST_CHECK_EQUAL(p.getMask(), 4);
184 BOOST_CHECK(!p.hasFlagBit(3));
185 BOOST_CHECK(p.getFlagBit(3));
186 BOOST_CHECK(p.hasFlagBit(2));
187 BOOST_CHECK(p.getFlagBit(2));
188
189 // Set bit 1 to false (change Mask)
190 p.setFlagBit(1, false);
191 // 2^3 + 2^2 = 12
192 BOOST_CHECK_EQUAL(p.getFlags(), 12);
193 // 2^2 + 2^1 = 6
194 BOOST_CHECK_EQUAL(p.getMask(), 6);
195 BOOST_CHECK(!p.hasFlagBit(3));
196 BOOST_CHECK(p.getFlagBit(3));
197 BOOST_CHECK(p.hasFlagBit(2));
198 BOOST_CHECK(p.getFlagBit(2));
199 BOOST_CHECK(p.hasFlagBit(1));
200 BOOST_CHECK(!p.getFlagBit(1));
201
202 // Set bit 2 to false (change Mask)
203 p.setFlagBit(2, false);
204 // 2^3 = 8
205 BOOST_CHECK_EQUAL(p.getFlags(), 8);
206 // 2^2 + 2^1 = 6
207 BOOST_CHECK_EQUAL(p.getMask(), 6);
208 BOOST_CHECK(!p.hasFlagBit(3));
209 BOOST_CHECK(p.getFlagBit(3));
210 BOOST_CHECK(p.hasFlagBit(2));
211 BOOST_CHECK(!p.getFlagBit(2));
212 BOOST_CHECK(p.hasFlagBit(1));
213 BOOST_CHECK(!p.getFlagBit(1));
214
215 // Set bit 0 to true (change Mask)
216 p.setFlagBit(0, true);
217 // 2^3 + 2^0 = 9
218 BOOST_CHECK_EQUAL(p.getFlags(), 9);
219 // 2^2 + 2^1 + 2^0 = 7
220 BOOST_CHECK_EQUAL(p.getMask(), 7);
221 BOOST_CHECK(p.hasFlagBit(0));
222 BOOST_CHECK(p.getFlagBit(0));
223
224 // Unset bit 0
225 p.unsetFlagBit(0);
226 BOOST_REQUIRE(p.hasFlags());
227 BOOST_REQUIRE(p.hasMask());
228 // 2^3 + 2^0 = 9
229 BOOST_CHECK_EQUAL(p.getFlags(), 9);
230 // 2^2 + 2^1 = 6
231 BOOST_CHECK_EQUAL(p.getMask(), 6);
232 BOOST_CHECK(p.hasFlagBit(1));
233 BOOST_CHECK(!p.hasFlagBit(0));
234 BOOST_CHECK(p.getFlagBit(0));
235
236 // Unset bit 3 (already unset in Mask, so no change)
237 p.unsetFlagBit(3);
238 BOOST_REQUIRE(p.hasFlags());
239 BOOST_REQUIRE(p.hasMask());
240 // 2^3 + 2^0 = 9
241 BOOST_CHECK_EQUAL(p.getFlags(), 9);
242 // 2^2 + 2^1 = 6
243 BOOST_CHECK_EQUAL(p.getMask(), 6);
244 BOOST_CHECK(!p.hasFlagBit(3));
245 BOOST_CHECK(p.getFlagBit(3));
246
247 // Unset bit 2
248 p.unsetFlagBit(2);
249 BOOST_REQUIRE(p.hasFlags());
250 BOOST_REQUIRE(p.hasMask());
251 // 2^3 + 2^0 = 9
252 BOOST_CHECK_EQUAL(p.getFlags(), 9);
253 // 2^1 = 2
254 BOOST_CHECK_EQUAL(p.getMask(), 2);
255 BOOST_CHECK(!p.hasFlagBit(2));
256 BOOST_CHECK(!p.getFlagBit(2));
257
258 // Unset bit 1
259 // Flags and Mask fields will be deleted as Mask is now 0
260 p.unsetFlagBit(1);
261 BOOST_CHECK(!p.hasFlags());
262 BOOST_CHECK(!p.hasMask());
263 BOOST_CHECK(!p.hasFlagBit(3));
264 BOOST_CHECK(!p.getFlagBit(3));
265 BOOST_CHECK(!p.hasFlagBit(2));
266 BOOST_CHECK(!p.getFlagBit(2));
267 BOOST_CHECK(!p.hasFlagBit(1));
268 BOOST_CHECK(!p.getFlagBit(1));
269 BOOST_CHECK(!p.hasFlagBit(0));
270 BOOST_CHECK(!p.getFlagBit(0));
271
272 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
273 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
274}
275
Junxiao Shi7357ef22016-09-07 02:39:37 +0000276BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
277BOOST_AUTO_TEST_SUITE_END() // Nfd
278BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700279
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800280} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700281} // namespace nfd
282} // namespace ndn