blob: 603006011408f9355b0a23b0c2008ac224587b88 [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/*
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.
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
Junxiao Shi22f85682018-01-22 19:23:22 +000034BOOST_AUTO_TEST_CASE(Fields)
Junxiao Shibc19b372014-03-23 16:59:25 -070035{
Junxiao Shi22f85682018-01-22 19:23:22 +000036 ControlParameters input;
Junxiao Shibc19b372014-03-23 16:59:25 -070037
Junxiao Shi22f85682018-01-22 19:23:22 +000038 ControlParameters decoded(input.wireEncode());
Junxiao Shibc19b372014-03-23 16:59:25 -070039 BOOST_CHECK_EQUAL(decoded.hasName(), false);
40 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070041 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Eric Newberryd7f5b282017-03-28 19:55:20 -070042 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070043 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shi22f85682018-01-22 19:23:22 +000044 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
45 BOOST_CHECK_EQUAL(decoded.hasCapacity(), false);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050046 BOOST_CHECK_EQUAL(decoded.hasCount(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070047 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -070048 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070049 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070050 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080051 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070052
Junxiao Shi22f85682018-01-22 19:23:22 +000053 input.setName("/name");
54 input.setFaceId(2634);
55 input.setUri("udp4://192.0.2.1:6363");
56 input.setLocalUri("udp4://192.0.2.2:6363");
57 input.setOrigin(ROUTE_ORIGIN_NLSR);
58 input.setCost(1388);
59 input.setCapacity(2632);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050060 input.setCount(3100);
Junxiao Shi22f85682018-01-22 19:23:22 +000061 input.setFlags(0xAFC4);
62 input.setMask(0xF7A1);
63 input.setStrategy("/strategy-name");
64 input.setExpirationPeriod(1800000_ms);
65 input.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shibc19b372014-03-23 16:59:25 -070066
Junxiao Shi22f85682018-01-22 19:23:22 +000067 decoded.wireDecode(input.wireEncode());
68 BOOST_CHECK_EQUAL(decoded.hasName(), true);
69 BOOST_CHECK_EQUAL(decoded.hasFaceId(), true);
70 BOOST_CHECK_EQUAL(decoded.hasUri(), true);
71 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), true);
72 BOOST_CHECK_EQUAL(decoded.hasOrigin(), true);
73 BOOST_CHECK_EQUAL(decoded.hasCost(), true);
74 BOOST_CHECK_EQUAL(decoded.hasCapacity(), true);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050075 BOOST_CHECK_EQUAL(decoded.hasCount(), true);
Junxiao Shi22f85682018-01-22 19:23:22 +000076 BOOST_CHECK_EQUAL(decoded.hasFlags(), true);
77 BOOST_CHECK_EQUAL(decoded.hasMask(), true);
78 BOOST_CHECK_EQUAL(decoded.hasStrategy(), true);
79 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), true);
80 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), true);
Junxiao Shibc19b372014-03-23 16:59:25 -070081
Junxiao Shi22f85682018-01-22 19:23:22 +000082 BOOST_CHECK_EQUAL(decoded.getName(), "/name");
83 BOOST_CHECK_EQUAL(decoded.getFaceId(), 2634);
84 BOOST_CHECK_EQUAL(decoded.getUri(), "udp4://192.0.2.1:6363");
85 BOOST_CHECK_EQUAL(decoded.getLocalUri(), "udp4://192.0.2.2:6363");
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040086 BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR);
Junxiao Shi22f85682018-01-22 19:23:22 +000087 BOOST_CHECK_EQUAL(decoded.getCost(), 1388);
88 BOOST_CHECK_EQUAL(decoded.getCapacity(), 2632);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -050089 BOOST_CHECK_EQUAL(decoded.getCount(), 3100);
Junxiao Shi22f85682018-01-22 19:23:22 +000090 BOOST_CHECK_EQUAL(decoded.getFlags(), 0xAFC4);
91 BOOST_CHECK_EQUAL(decoded.getMask(), 0xF7A1);
92 BOOST_CHECK_EQUAL(decoded.getStrategy(), "/strategy-name");
Davide Pesavento0f830802018-01-16 23:58:58 -050093 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), 1800000_ms);
Junxiao Shi22f85682018-01-22 19:23:22 +000094 BOOST_CHECK_EQUAL(decoded.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070095
Junxiao Shi22f85682018-01-22 19:23:22 +000096 input.unsetName();
97 input.unsetFaceId();
98 input.unsetUri();
99 input.unsetLocalUri();
100 input.unsetOrigin();
101 input.unsetCost();
102 input.unsetCapacity();
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500103 input.unsetCount();
Junxiao Shi22f85682018-01-22 19:23:22 +0000104 input.unsetFlags();
105 input.unsetMask();
106 input.unsetStrategy();
107 input.unsetExpirationPeriod();
108 input.unsetFacePersistency();
109 BOOST_CHECK_EQUAL(input.hasName(), false);
110 BOOST_CHECK_EQUAL(input.hasFaceId(), false);
111 BOOST_CHECK_EQUAL(input.hasUri(), false);
112 BOOST_CHECK_EQUAL(input.hasLocalUri(), false);
113 BOOST_CHECK_EQUAL(input.hasOrigin(), false);
114 BOOST_CHECK_EQUAL(input.hasCost(), false);
115 BOOST_CHECK_EQUAL(input.hasCapacity(), false);
Davide Pesavento5e2ccca2018-03-06 19:00:15 -0500116 BOOST_CHECK_EQUAL(input.hasCount(), false);
Junxiao Shi22f85682018-01-22 19:23:22 +0000117 BOOST_CHECK_EQUAL(input.hasFlags(), false);
118 BOOST_CHECK_EQUAL(input.hasMask(), false);
119 BOOST_CHECK_EQUAL(input.hasStrategy(), false);
120 BOOST_CHECK_EQUAL(input.hasExpirationPeriod(), false);
121 BOOST_CHECK_EQUAL(input.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700122}
123
Eric Newberryda916d62016-08-11 23:04:34 -0700124BOOST_AUTO_TEST_CASE(FlagsAndMask)
125{
126 ControlParameters p;
127
128 BOOST_CHECK(!p.hasFlags());
129 BOOST_CHECK(!p.hasMask());
130 BOOST_CHECK(!p.hasFlagBit(0));
131 BOOST_CHECK(!p.getFlagBit(0));
132
133 // Set bit 2 to true (change Mask)
134 p.setFlagBit(2, true);
135 // 2^2 = 4
136 BOOST_CHECK_EQUAL(p.getFlags(), 4);
137 BOOST_CHECK_EQUAL(p.getMask(), 4);
138 BOOST_CHECK(p.hasFlagBit(2));
139 BOOST_CHECK(p.getFlagBit(2));
140 BOOST_CHECK(!p.hasFlagBit(1));
141 BOOST_CHECK(!p.getFlagBit(1));
142
143 // Set bit 3 to true (no change to Mask)
144 p.setFlagBit(3, true, false);
145 // 2^3 + 2^2 = 12
146 BOOST_CHECK_EQUAL(p.getFlags(), 12);
147 // 2^2 = 4
148 BOOST_CHECK_EQUAL(p.getMask(), 4);
149 BOOST_CHECK(!p.hasFlagBit(3));
150 BOOST_CHECK(p.getFlagBit(3));
151 BOOST_CHECK(p.hasFlagBit(2));
152 BOOST_CHECK(p.getFlagBit(2));
153
154 // Set bit 1 to false (change Mask)
155 p.setFlagBit(1, false);
156 // 2^3 + 2^2 = 12
157 BOOST_CHECK_EQUAL(p.getFlags(), 12);
158 // 2^2 + 2^1 = 6
159 BOOST_CHECK_EQUAL(p.getMask(), 6);
160 BOOST_CHECK(!p.hasFlagBit(3));
161 BOOST_CHECK(p.getFlagBit(3));
162 BOOST_CHECK(p.hasFlagBit(2));
163 BOOST_CHECK(p.getFlagBit(2));
164 BOOST_CHECK(p.hasFlagBit(1));
165 BOOST_CHECK(!p.getFlagBit(1));
166
167 // Set bit 2 to false (change Mask)
168 p.setFlagBit(2, false);
169 // 2^3 = 8
170 BOOST_CHECK_EQUAL(p.getFlags(), 8);
171 // 2^2 + 2^1 = 6
172 BOOST_CHECK_EQUAL(p.getMask(), 6);
173 BOOST_CHECK(!p.hasFlagBit(3));
174 BOOST_CHECK(p.getFlagBit(3));
175 BOOST_CHECK(p.hasFlagBit(2));
176 BOOST_CHECK(!p.getFlagBit(2));
177 BOOST_CHECK(p.hasFlagBit(1));
178 BOOST_CHECK(!p.getFlagBit(1));
179
180 // Set bit 0 to true (change Mask)
181 p.setFlagBit(0, true);
182 // 2^3 + 2^0 = 9
183 BOOST_CHECK_EQUAL(p.getFlags(), 9);
184 // 2^2 + 2^1 + 2^0 = 7
185 BOOST_CHECK_EQUAL(p.getMask(), 7);
186 BOOST_CHECK(p.hasFlagBit(0));
187 BOOST_CHECK(p.getFlagBit(0));
188
189 // Unset bit 0
190 p.unsetFlagBit(0);
191 BOOST_REQUIRE(p.hasFlags());
192 BOOST_REQUIRE(p.hasMask());
193 // 2^3 + 2^0 = 9
194 BOOST_CHECK_EQUAL(p.getFlags(), 9);
195 // 2^2 + 2^1 = 6
196 BOOST_CHECK_EQUAL(p.getMask(), 6);
197 BOOST_CHECK(p.hasFlagBit(1));
198 BOOST_CHECK(!p.hasFlagBit(0));
199 BOOST_CHECK(p.getFlagBit(0));
200
201 // Unset bit 3 (already unset in Mask, so no change)
202 p.unsetFlagBit(3);
203 BOOST_REQUIRE(p.hasFlags());
204 BOOST_REQUIRE(p.hasMask());
205 // 2^3 + 2^0 = 9
206 BOOST_CHECK_EQUAL(p.getFlags(), 9);
207 // 2^2 + 2^1 = 6
208 BOOST_CHECK_EQUAL(p.getMask(), 6);
209 BOOST_CHECK(!p.hasFlagBit(3));
210 BOOST_CHECK(p.getFlagBit(3));
211
212 // Unset bit 2
213 p.unsetFlagBit(2);
214 BOOST_REQUIRE(p.hasFlags());
215 BOOST_REQUIRE(p.hasMask());
216 // 2^3 + 2^0 = 9
217 BOOST_CHECK_EQUAL(p.getFlags(), 9);
218 // 2^1 = 2
219 BOOST_CHECK_EQUAL(p.getMask(), 2);
220 BOOST_CHECK(!p.hasFlagBit(2));
221 BOOST_CHECK(!p.getFlagBit(2));
222
223 // Unset bit 1
224 // Flags and Mask fields will be deleted as Mask is now 0
225 p.unsetFlagBit(1);
226 BOOST_CHECK(!p.hasFlags());
227 BOOST_CHECK(!p.hasMask());
228 BOOST_CHECK(!p.hasFlagBit(3));
229 BOOST_CHECK(!p.getFlagBit(3));
230 BOOST_CHECK(!p.hasFlagBit(2));
231 BOOST_CHECK(!p.getFlagBit(2));
232 BOOST_CHECK(!p.hasFlagBit(1));
233 BOOST_CHECK(!p.getFlagBit(1));
234 BOOST_CHECK(!p.hasFlagBit(0));
235 BOOST_CHECK(!p.getFlagBit(0));
236
237 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
238 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
239}
240
Junxiao Shi7357ef22016-09-07 02:39:37 +0000241BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
242BOOST_AUTO_TEST_SUITE_END() // Nfd
243BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700244
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800245} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700246} // namespace nfd
247} // namespace ndn