blob: 7ef364e8a4ea7b810a4a246ebdd2cb73dbc21c28 [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 Newberryda916d62016-08-11 23:04:34 -07003 * Copyright (c) 2013-2016 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");
Yukai Tud93c5fc2015-08-25 11:37:16 +080038 parameters.setFacePersistency(ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberryda916d62016-08-11 23:04:34 -070039 parameters.setFlags(1);
40 parameters.setMask(1);
Junxiao Shibc19b372014-03-23 16:59:25 -070041 Block wire = parameters.wireEncode();
42
43 ControlParameters decoded(wire);
44 BOOST_CHECK_EQUAL(decoded.getUri(), "tcp4://192.0.2.1:6363");
Yukai Tud93c5fc2015-08-25 11:37:16 +080045 BOOST_CHECK_EQUAL(decoded.getFacePersistency(), ndn::nfd::FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Eric Newberryda916d62016-08-11 23:04:34 -070046 BOOST_CHECK_EQUAL(decoded.getFlags(), 1);
47 BOOST_CHECK_EQUAL(decoded.getMask(), 1);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070048
Junxiao Shibc19b372014-03-23 16:59:25 -070049 BOOST_CHECK_EQUAL(decoded.hasName(), false);
50 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
51 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070052 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070053 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
54 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070055 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070056}
57
58BOOST_AUTO_TEST_CASE(FaceLocalControlOptions)
59{
60 ControlParameters parameters;
61 parameters.setLocalControlFeature(LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
62
63 Block wire = parameters.wireEncode();
64
65 ControlParameters decoded(wire);
66 BOOST_CHECK_EQUAL(decoded.getLocalControlFeature(), LOCAL_CONTROL_FEATURE_INCOMING_FACE_ID);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070067
Junxiao Shibc19b372014-03-23 16:59:25 -070068 BOOST_CHECK_EQUAL(decoded.hasName(), false);
69 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070070 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070071 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
72 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
73 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070074 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070075 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080076 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070077}
78
79BOOST_AUTO_TEST_CASE(FibOptions)
80{
81 ControlParameters parameters;
82 parameters.setName("ndn:/example")
83 .setFaceId(4)
84 .setCost(555);
85
86 Block wire = parameters.wireEncode();
87
88 ControlParameters decoded(wire);
89 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
90 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
91 BOOST_CHECK_EQUAL(decoded.getCost(), 555);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070092
Junxiao Shibc19b372014-03-23 16:59:25 -070093 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070094 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
95 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
96 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -070097 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070098 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070099 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800100 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700101}
102
103BOOST_AUTO_TEST_CASE(StrategyChoiceOptions)
104{
105 ControlParameters parameters;
106 parameters.setName("ndn:/")
107 .setStrategy("ndn:/strategy/A");
108
109 Block wire = parameters.wireEncode();
110
111 ControlParameters decoded(wire);
112 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/"));
113 BOOST_CHECK_EQUAL(decoded.getStrategy(), Name("ndn:/strategy/A"));
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700114
Junxiao Shibc19b372014-03-23 16:59:25 -0700115 BOOST_CHECK_EQUAL(decoded.hasFaceId(), false);
116 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
117 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700118 BOOST_CHECK_EQUAL(decoded.hasOrigin(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700119 BOOST_CHECK_EQUAL(decoded.hasCost(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700120 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700121 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700122 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800123 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700124}
125
126BOOST_AUTO_TEST_CASE(RibOptions)
127{
128 ControlParameters parameters;
129 parameters.setName("ndn:/example")
130 .setFaceId(4)
131 .setOrigin(128)
132 .setCost(6)
133 .setFlags(0x01)
134 .setExpirationPeriod(time::milliseconds(1800000));
135
136 Block wire = parameters.wireEncode();
137
138 ControlParameters decoded(wire);
139 BOOST_CHECK_EQUAL(decoded.getName(), Name("ndn:/example"));
140 BOOST_CHECK_EQUAL(decoded.getFaceId(), 4);
141 BOOST_CHECK_EQUAL(decoded.getOrigin(), 128);
142 BOOST_CHECK_EQUAL(decoded.getCost(), 6);
143 BOOST_CHECK_EQUAL(decoded.getFlags(), 0x01);
144 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), time::milliseconds(1800000));
145
146 BOOST_CHECK_EQUAL(decoded.hasUri(), false);
147 BOOST_CHECK_EQUAL(decoded.hasLocalControlFeature(), false);
Eric Newberryda916d62016-08-11 23:04:34 -0700148 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -0700149 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +0800150 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700151}
152
Eric Newberryda916d62016-08-11 23:04:34 -0700153BOOST_AUTO_TEST_CASE(FlagsAndMask)
154{
155 ControlParameters p;
156
157 BOOST_CHECK(!p.hasFlags());
158 BOOST_CHECK(!p.hasMask());
159 BOOST_CHECK(!p.hasFlagBit(0));
160 BOOST_CHECK(!p.getFlagBit(0));
161
162 // Set bit 2 to true (change Mask)
163 p.setFlagBit(2, true);
164 // 2^2 = 4
165 BOOST_CHECK_EQUAL(p.getFlags(), 4);
166 BOOST_CHECK_EQUAL(p.getMask(), 4);
167 BOOST_CHECK(p.hasFlagBit(2));
168 BOOST_CHECK(p.getFlagBit(2));
169 BOOST_CHECK(!p.hasFlagBit(1));
170 BOOST_CHECK(!p.getFlagBit(1));
171
172 // Set bit 3 to true (no change to Mask)
173 p.setFlagBit(3, true, false);
174 // 2^3 + 2^2 = 12
175 BOOST_CHECK_EQUAL(p.getFlags(), 12);
176 // 2^2 = 4
177 BOOST_CHECK_EQUAL(p.getMask(), 4);
178 BOOST_CHECK(!p.hasFlagBit(3));
179 BOOST_CHECK(p.getFlagBit(3));
180 BOOST_CHECK(p.hasFlagBit(2));
181 BOOST_CHECK(p.getFlagBit(2));
182
183 // Set bit 1 to false (change Mask)
184 p.setFlagBit(1, false);
185 // 2^3 + 2^2 = 12
186 BOOST_CHECK_EQUAL(p.getFlags(), 12);
187 // 2^2 + 2^1 = 6
188 BOOST_CHECK_EQUAL(p.getMask(), 6);
189 BOOST_CHECK(!p.hasFlagBit(3));
190 BOOST_CHECK(p.getFlagBit(3));
191 BOOST_CHECK(p.hasFlagBit(2));
192 BOOST_CHECK(p.getFlagBit(2));
193 BOOST_CHECK(p.hasFlagBit(1));
194 BOOST_CHECK(!p.getFlagBit(1));
195
196 // Set bit 2 to false (change Mask)
197 p.setFlagBit(2, false);
198 // 2^3 = 8
199 BOOST_CHECK_EQUAL(p.getFlags(), 8);
200 // 2^2 + 2^1 = 6
201 BOOST_CHECK_EQUAL(p.getMask(), 6);
202 BOOST_CHECK(!p.hasFlagBit(3));
203 BOOST_CHECK(p.getFlagBit(3));
204 BOOST_CHECK(p.hasFlagBit(2));
205 BOOST_CHECK(!p.getFlagBit(2));
206 BOOST_CHECK(p.hasFlagBit(1));
207 BOOST_CHECK(!p.getFlagBit(1));
208
209 // Set bit 0 to true (change Mask)
210 p.setFlagBit(0, true);
211 // 2^3 + 2^0 = 9
212 BOOST_CHECK_EQUAL(p.getFlags(), 9);
213 // 2^2 + 2^1 + 2^0 = 7
214 BOOST_CHECK_EQUAL(p.getMask(), 7);
215 BOOST_CHECK(p.hasFlagBit(0));
216 BOOST_CHECK(p.getFlagBit(0));
217
218 // Unset bit 0
219 p.unsetFlagBit(0);
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^2 + 2^1 = 6
225 BOOST_CHECK_EQUAL(p.getMask(), 6);
226 BOOST_CHECK(p.hasFlagBit(1));
227 BOOST_CHECK(!p.hasFlagBit(0));
228 BOOST_CHECK(p.getFlagBit(0));
229
230 // Unset bit 3 (already unset in Mask, so no change)
231 p.unsetFlagBit(3);
232 BOOST_REQUIRE(p.hasFlags());
233 BOOST_REQUIRE(p.hasMask());
234 // 2^3 + 2^0 = 9
235 BOOST_CHECK_EQUAL(p.getFlags(), 9);
236 // 2^2 + 2^1 = 6
237 BOOST_CHECK_EQUAL(p.getMask(), 6);
238 BOOST_CHECK(!p.hasFlagBit(3));
239 BOOST_CHECK(p.getFlagBit(3));
240
241 // Unset bit 2
242 p.unsetFlagBit(2);
243 BOOST_REQUIRE(p.hasFlags());
244 BOOST_REQUIRE(p.hasMask());
245 // 2^3 + 2^0 = 9
246 BOOST_CHECK_EQUAL(p.getFlags(), 9);
247 // 2^1 = 2
248 BOOST_CHECK_EQUAL(p.getMask(), 2);
249 BOOST_CHECK(!p.hasFlagBit(2));
250 BOOST_CHECK(!p.getFlagBit(2));
251
252 // Unset bit 1
253 // Flags and Mask fields will be deleted as Mask is now 0
254 p.unsetFlagBit(1);
255 BOOST_CHECK(!p.hasFlags());
256 BOOST_CHECK(!p.hasMask());
257 BOOST_CHECK(!p.hasFlagBit(3));
258 BOOST_CHECK(!p.getFlagBit(3));
259 BOOST_CHECK(!p.hasFlagBit(2));
260 BOOST_CHECK(!p.getFlagBit(2));
261 BOOST_CHECK(!p.hasFlagBit(1));
262 BOOST_CHECK(!p.getFlagBit(1));
263 BOOST_CHECK(!p.hasFlagBit(0));
264 BOOST_CHECK(!p.getFlagBit(0));
265
266 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
267 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
268}
269
Junxiao Shi7357ef22016-09-07 02:39:37 +0000270BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
271BOOST_AUTO_TEST_SUITE_END() // Nfd
272BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700273
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800274} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700275} // namespace nfd
276} // namespace ndn