blob: 80e8134e30050b96913cf06f9a903fd2a65e121b [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);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070046 BOOST_CHECK_EQUAL(decoded.hasFlags(), false);
Eric Newberryda916d62016-08-11 23:04:34 -070047 BOOST_CHECK_EQUAL(decoded.hasMask(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070048 BOOST_CHECK_EQUAL(decoded.hasStrategy(), false);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070049 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false);
Yukai Tud93c5fc2015-08-25 11:37:16 +080050 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -070051
Junxiao Shi22f85682018-01-22 19:23:22 +000052 input.setName("/name");
53 input.setFaceId(2634);
54 input.setUri("udp4://192.0.2.1:6363");
55 input.setLocalUri("udp4://192.0.2.2:6363");
56 input.setOrigin(ROUTE_ORIGIN_NLSR);
57 input.setCost(1388);
58 input.setCapacity(2632);
59 input.setFlags(0xAFC4);
60 input.setMask(0xF7A1);
61 input.setStrategy("/strategy-name");
62 input.setExpirationPeriod(1800000_ms);
63 input.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shibc19b372014-03-23 16:59:25 -070064
Junxiao Shi22f85682018-01-22 19:23:22 +000065 decoded.wireDecode(input.wireEncode());
66 BOOST_CHECK_EQUAL(decoded.hasName(), true);
67 BOOST_CHECK_EQUAL(decoded.hasFaceId(), true);
68 BOOST_CHECK_EQUAL(decoded.hasUri(), true);
69 BOOST_CHECK_EQUAL(decoded.hasLocalUri(), true);
70 BOOST_CHECK_EQUAL(decoded.hasOrigin(), true);
71 BOOST_CHECK_EQUAL(decoded.hasCost(), true);
72 BOOST_CHECK_EQUAL(decoded.hasCapacity(), true);
73 BOOST_CHECK_EQUAL(decoded.hasFlags(), true);
74 BOOST_CHECK_EQUAL(decoded.hasMask(), true);
75 BOOST_CHECK_EQUAL(decoded.hasStrategy(), true);
76 BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), true);
77 BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), true);
Junxiao Shibc19b372014-03-23 16:59:25 -070078
Junxiao Shi22f85682018-01-22 19:23:22 +000079 BOOST_CHECK_EQUAL(decoded.getName(), "/name");
80 BOOST_CHECK_EQUAL(decoded.getFaceId(), 2634);
81 BOOST_CHECK_EQUAL(decoded.getUri(), "udp4://192.0.2.1:6363");
82 BOOST_CHECK_EQUAL(decoded.getLocalUri(), "udp4://192.0.2.2:6363");
Davide Pesaventoe8e48c22017-04-13 00:35:33 -040083 BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR);
Junxiao Shi22f85682018-01-22 19:23:22 +000084 BOOST_CHECK_EQUAL(decoded.getCost(), 1388);
85 BOOST_CHECK_EQUAL(decoded.getCapacity(), 2632);
86 BOOST_CHECK_EQUAL(decoded.getFlags(), 0xAFC4);
87 BOOST_CHECK_EQUAL(decoded.getMask(), 0xF7A1);
88 BOOST_CHECK_EQUAL(decoded.getStrategy(), "/strategy-name");
Davide Pesavento0f830802018-01-16 23:58:58 -050089 BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), 1800000_ms);
Junxiao Shi22f85682018-01-22 19:23:22 +000090 BOOST_CHECK_EQUAL(decoded.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT);
Junxiao Shi5f6c74f2014-04-18 16:29:44 -070091
Junxiao Shi22f85682018-01-22 19:23:22 +000092 input.unsetName();
93 input.unsetFaceId();
94 input.unsetUri();
95 input.unsetLocalUri();
96 input.unsetOrigin();
97 input.unsetCost();
98 input.unsetCapacity();
99 input.unsetFlags();
100 input.unsetMask();
101 input.unsetStrategy();
102 input.unsetExpirationPeriod();
103 input.unsetFacePersistency();
104 BOOST_CHECK_EQUAL(input.hasName(), false);
105 BOOST_CHECK_EQUAL(input.hasFaceId(), false);
106 BOOST_CHECK_EQUAL(input.hasUri(), false);
107 BOOST_CHECK_EQUAL(input.hasLocalUri(), false);
108 BOOST_CHECK_EQUAL(input.hasOrigin(), false);
109 BOOST_CHECK_EQUAL(input.hasCost(), false);
110 BOOST_CHECK_EQUAL(input.hasCapacity(), false);
111 BOOST_CHECK_EQUAL(input.hasFlags(), false);
112 BOOST_CHECK_EQUAL(input.hasMask(), false);
113 BOOST_CHECK_EQUAL(input.hasStrategy(), false);
114 BOOST_CHECK_EQUAL(input.hasExpirationPeriod(), false);
115 BOOST_CHECK_EQUAL(input.hasFacePersistency(), false);
Junxiao Shibc19b372014-03-23 16:59:25 -0700116}
117
Eric Newberryda916d62016-08-11 23:04:34 -0700118BOOST_AUTO_TEST_CASE(FlagsAndMask)
119{
120 ControlParameters p;
121
122 BOOST_CHECK(!p.hasFlags());
123 BOOST_CHECK(!p.hasMask());
124 BOOST_CHECK(!p.hasFlagBit(0));
125 BOOST_CHECK(!p.getFlagBit(0));
126
127 // Set bit 2 to true (change Mask)
128 p.setFlagBit(2, true);
129 // 2^2 = 4
130 BOOST_CHECK_EQUAL(p.getFlags(), 4);
131 BOOST_CHECK_EQUAL(p.getMask(), 4);
132 BOOST_CHECK(p.hasFlagBit(2));
133 BOOST_CHECK(p.getFlagBit(2));
134 BOOST_CHECK(!p.hasFlagBit(1));
135 BOOST_CHECK(!p.getFlagBit(1));
136
137 // Set bit 3 to true (no change to Mask)
138 p.setFlagBit(3, true, false);
139 // 2^3 + 2^2 = 12
140 BOOST_CHECK_EQUAL(p.getFlags(), 12);
141 // 2^2 = 4
142 BOOST_CHECK_EQUAL(p.getMask(), 4);
143 BOOST_CHECK(!p.hasFlagBit(3));
144 BOOST_CHECK(p.getFlagBit(3));
145 BOOST_CHECK(p.hasFlagBit(2));
146 BOOST_CHECK(p.getFlagBit(2));
147
148 // Set bit 1 to false (change Mask)
149 p.setFlagBit(1, false);
150 // 2^3 + 2^2 = 12
151 BOOST_CHECK_EQUAL(p.getFlags(), 12);
152 // 2^2 + 2^1 = 6
153 BOOST_CHECK_EQUAL(p.getMask(), 6);
154 BOOST_CHECK(!p.hasFlagBit(3));
155 BOOST_CHECK(p.getFlagBit(3));
156 BOOST_CHECK(p.hasFlagBit(2));
157 BOOST_CHECK(p.getFlagBit(2));
158 BOOST_CHECK(p.hasFlagBit(1));
159 BOOST_CHECK(!p.getFlagBit(1));
160
161 // Set bit 2 to false (change Mask)
162 p.setFlagBit(2, false);
163 // 2^3 = 8
164 BOOST_CHECK_EQUAL(p.getFlags(), 8);
165 // 2^2 + 2^1 = 6
166 BOOST_CHECK_EQUAL(p.getMask(), 6);
167 BOOST_CHECK(!p.hasFlagBit(3));
168 BOOST_CHECK(p.getFlagBit(3));
169 BOOST_CHECK(p.hasFlagBit(2));
170 BOOST_CHECK(!p.getFlagBit(2));
171 BOOST_CHECK(p.hasFlagBit(1));
172 BOOST_CHECK(!p.getFlagBit(1));
173
174 // Set bit 0 to true (change Mask)
175 p.setFlagBit(0, true);
176 // 2^3 + 2^0 = 9
177 BOOST_CHECK_EQUAL(p.getFlags(), 9);
178 // 2^2 + 2^1 + 2^0 = 7
179 BOOST_CHECK_EQUAL(p.getMask(), 7);
180 BOOST_CHECK(p.hasFlagBit(0));
181 BOOST_CHECK(p.getFlagBit(0));
182
183 // Unset bit 0
184 p.unsetFlagBit(0);
185 BOOST_REQUIRE(p.hasFlags());
186 BOOST_REQUIRE(p.hasMask());
187 // 2^3 + 2^0 = 9
188 BOOST_CHECK_EQUAL(p.getFlags(), 9);
189 // 2^2 + 2^1 = 6
190 BOOST_CHECK_EQUAL(p.getMask(), 6);
191 BOOST_CHECK(p.hasFlagBit(1));
192 BOOST_CHECK(!p.hasFlagBit(0));
193 BOOST_CHECK(p.getFlagBit(0));
194
195 // Unset bit 3 (already unset in Mask, so no change)
196 p.unsetFlagBit(3);
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(3));
204 BOOST_CHECK(p.getFlagBit(3));
205
206 // Unset bit 2
207 p.unsetFlagBit(2);
208 BOOST_REQUIRE(p.hasFlags());
209 BOOST_REQUIRE(p.hasMask());
210 // 2^3 + 2^0 = 9
211 BOOST_CHECK_EQUAL(p.getFlags(), 9);
212 // 2^1 = 2
213 BOOST_CHECK_EQUAL(p.getMask(), 2);
214 BOOST_CHECK(!p.hasFlagBit(2));
215 BOOST_CHECK(!p.getFlagBit(2));
216
217 // Unset bit 1
218 // Flags and Mask fields will be deleted as Mask is now 0
219 p.unsetFlagBit(1);
220 BOOST_CHECK(!p.hasFlags());
221 BOOST_CHECK(!p.hasMask());
222 BOOST_CHECK(!p.hasFlagBit(3));
223 BOOST_CHECK(!p.getFlagBit(3));
224 BOOST_CHECK(!p.hasFlagBit(2));
225 BOOST_CHECK(!p.getFlagBit(2));
226 BOOST_CHECK(!p.hasFlagBit(1));
227 BOOST_CHECK(!p.getFlagBit(1));
228 BOOST_CHECK(!p.hasFlagBit(0));
229 BOOST_CHECK(!p.getFlagBit(0));
230
231 BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range);
232 BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range);
233}
234
Junxiao Shi7357ef22016-09-07 02:39:37 +0000235BOOST_AUTO_TEST_SUITE_END() // TestControlParameters
236BOOST_AUTO_TEST_SUITE_END() // Nfd
237BOOST_AUTO_TEST_SUITE_END() // Mgmt
Junxiao Shibc19b372014-03-23 16:59:25 -0700238
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800239} // namespace tests
Junxiao Shibc19b372014-03-23 16:59:25 -0700240} // namespace nfd
241} // namespace ndn