blob: 8f8f98adbb377a1c99bbf2c4e49747777fcb0174 [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);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070053 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070054 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
55 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070056 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070057}
58
Junxiao Shibc19b372014-03-23 16:59:25 -070059BOOST_AUTO_TEST_CASE(FibOptions)
60{
61 ControlParameters parameters;
62 parameters.setName("ndn:/example")
63 .setFaceId(4)
64 .setCost(555);
65
66 Block wire = parameters.wireEncode();
67
68 ControlParameters decoded(wire);
69 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
70 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
71 BOOST_CHECK_EQUAL(decoded.getCost(), 555);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070072
Junxiao Shibc19b372014-03-23 16:59:25 -070073 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -070074 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070075 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
76 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -070077 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070078 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070079 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080080 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070081}
82
83BOOST_AUTO_TEST_CASE(StrategyChoiceOptions)
84{
85 ControlParameters parameters;
86 parameters.setName("ndn:/")
87 .setStrategy("ndn:/strategy/A");
88
89 Block wire = parameters.wireEncode();
90
91 ControlParameters decoded(wire);
92 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/"));
93 BOOST_CHECK_EQUAL(decoded.getStrategy(), Name("ndn:/strategy/A"));
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070094
Junxiao Shibc19b372014-03-23 16:59:25 -070095 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
96 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.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070099 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700100 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700101 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700102 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800103 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700104}
105
106BOOST_AUTO_TEST_CASE(RibOptions)
107{
108 ControlParameters parameters;
109 parameters.setName("ndn:/example")
110 .setFaceId(4)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400111 .setOrigin(ROUTE_ORIGIN_NLSR)
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700112 .setCost(6)
113 .setFlags(0x01)
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400114 .setExpirationPeriod(time::minutes(30));
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700115
116 Block wire = parameters.wireEncode();
117
118 ControlParameters decoded(wire);
119 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
120 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
Davide Pesaventoe8e48c22017-04-13 00:35:33 -0400121 BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700122 BOOST_CHECK_EQUAL(decoded.getCost(), 6);
123 BOOST_CHECK_EQUAL(decoded.getFlags(), 0x01);
124 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), time::milliseconds(1800000));
125
126 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -0700127 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700128 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700129 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800130 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700131}
132
Eric Newberryda916d62016-08-11 23:04:34 -0700133BOOST_AUTO_TEST_CASE(FlagsAndMask)
134{
135 ControlParameters p;
136
137 BOOST_CHECK(!p.hasFlags());
138 BOOST_CHECK(!p.hasMask());
139 BOOST_CHECK(!p.hasFlagBit(0));
140 BOOST_CHECK(!p.getFlagBit(0));
141
142 // Set bit 2 to true (change Mask)
143 p.setFlagBit(2, true);
144 // 2^2 = 4
145 BOOST_CHECK_EQUAL(p.getFlags(), 4);
146 BOOST_CHECK_EQUAL(p.getMask(), 4);
147 BOOST_CHECK(p.hasFlagBit(2));
148 BOOST_CHECK(p.getFlagBit(2));
149 BOOST_CHECK(!p.hasFlagBit(1));
150 BOOST_CHECK(!p.getFlagBit(1));
151
152 // Set bit 3 to true (no change to Mask)
153 p.setFlagBit(3, true, false);
154 // 2^3 + 2^2 = 12
155 BOOST_CHECK_EQUAL(p.getFlags(), 12);
156 // 2^2 = 4
157 BOOST_CHECK_EQUAL(p.getMask(), 4);
158 BOOST_CHECK(!p.hasFlagBit(3));
159 BOOST_CHECK(p.getFlagBit(3));
160 BOOST_CHECK(p.hasFlagBit(2));
161 BOOST_CHECK(p.getFlagBit(2));
162
163 // Set bit 1 to false (change Mask)
164 p.setFlagBit(1, false);
165 // 2^3 + 2^2 = 12
166 BOOST_CHECK_EQUAL(p.getFlags(), 12);
167 // 2^2 + 2^1 = 6
168 BOOST_CHECK_EQUAL(p.getMask(), 6);
169 BOOST_CHECK(!p.hasFlagBit(3));
170 BOOST_CHECK(p.getFlagBit(3));
171 BOOST_CHECK(p.hasFlagBit(2));
172 BOOST_CHECK(p.getFlagBit(2));
173 BOOST_CHECK(p.hasFlagBit(1));
174 BOOST_CHECK(!p.getFlagBit(1));
175
176 // Set bit 2 to false (change Mask)
177 p.setFlagBit(2, false);
178 // 2^3 = 8
179 BOOST_CHECK_EQUAL(p.getFlags(), 8);
180 // 2^2 + 2^1 = 6
181 BOOST_CHECK_EQUAL(p.getMask(), 6);
182 BOOST_CHECK(!p.hasFlagBit(3));
183 BOOST_CHECK(p.getFlagBit(3));
184 BOOST_CHECK(p.hasFlagBit(2));
185 BOOST_CHECK(!p.getFlagBit(2));
186 BOOST_CHECK(p.hasFlagBit(1));
187 BOOST_CHECK(!p.getFlagBit(1));
188
189 // Set bit 0 to true (change Mask)
190 p.setFlagBit(0, true);
191 // 2^3 + 2^0 = 9
192 BOOST_CHECK_EQUAL(p.getFlags(), 9);
193 // 2^2 + 2^1 + 2^0 = 7
194 BOOST_CHECK_EQUAL(p.getMask(), 7);
195 BOOST_CHECK(p.hasFlagBit(0));
196 BOOST_CHECK(p.getFlagBit(0));
197
198 // Unset bit 0
199 p.unsetFlagBit(0);
200 BOOST_REQUIRE(p.hasFlags());
201 BOOST_REQUIRE(p.hasMask());
202 // 2^3 + 2^0 = 9
203 BOOST_CHECK_EQUAL(p.getFlags(), 9);
204 // 2^2 + 2^1 = 6
205 BOOST_CHECK_EQUAL(p.getMask(), 6);
206 BOOST_CHECK(p.hasFlagBit(1));
207 BOOST_CHECK(!p.hasFlagBit(0));
208 BOOST_CHECK(p.getFlagBit(0));
209
210 // Unset bit 3 (already unset in Mask, so no change)
211 p.unsetFlagBit(3);
212 BOOST_REQUIRE(p.hasFlags());
213 BOOST_REQUIRE(p.hasMask());
214 // 2^3 + 2^0 = 9
215 BOOST_CHECK_EQUAL(p.getFlags(), 9);
216 // 2^2 + 2^1 = 6
217 BOOST_CHECK_EQUAL(p.getMask(), 6);
218 BOOST_CHECK(!p.hasFlagBit(3));
219 BOOST_CHECK(p.getFlagBit(3));
220
221 // Unset bit 2
222 p.unsetFlagBit(2);
223 BOOST_REQUIRE(p.hasFlags());
224 BOOST_REQUIRE(p.hasMask());
225 // 2^3 + 2^0 = 9
226 BOOST_CHECK_EQUAL(p.getFlags(), 9);
227 // 2^1 = 2
228 BOOST_CHECK_EQUAL(p.getMask(), 2);
229 BOOST_CHECK(!p.hasFlagBit(2));
230 BOOST_CHECK(!p.getFlagBit(2));
231
232 // Unset bit 1
233 // Flags and Mask fields will be deleted as Mask is now 0
234 p.unsetFlagBit(1);
235 BOOST_CHECK(!p.hasFlags());
236 BOOST_CHECK(!p.hasMask());
237 BOOST_CHECK(!p.hasFlagBit(3));
238 BOOST_CHECK(!p.getFlagBit(3));
239 BOOST_CHECK(!p.hasFlagBit(2));
240 BOOST_CHECK(!p.getFlagBit(2));
241 BOOST_CHECK(!p.hasFlagBit(1));
242 BOOST_CHECK(!p.getFlagBit(1));
243 BOOST_CHECK(!p.hasFlagBit(0));
244 BOOST_CHECK(!p.getFlagBit(0));
245
246 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
247 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
248}
249
Junxiao Shi7357ef22016-09-07 02:39:37 +0000250BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
251BOOST_AUTO_TEST_SUITE_END() // Nfd
252BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700253
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800254} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700255} // namespace nfd
256} // namespace ndn