Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2023 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * 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 Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/mgmt/nfd/control-parameters.hpp" |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 23 | #include "ndn-cxx/util/concepts.hpp" |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace nfd { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 29 | namespace tests { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 30 | |
Davide Pesavento | 152ef44 | 2023-04-22 02:02:29 -0400 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireEncodable<ControlParameters>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<ControlParameters>)); |
| 33 | static_assert(std::is_convertible_v<ControlParameters::Error*, tlv::Error*>, |
| 34 | "ControlParameters::Error must inherit from tlv::Error"); |
| 35 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 37 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 38 | BOOST_AUTO_TEST_SUITE(TestControlParameters) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 40 | BOOST_AUTO_TEST_CASE(Fields) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 41 | { |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 42 | ControlParameters input; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 44 | ControlParameters decoded(input.wireEncode()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(decoded.hasName(), false); |
| 46 | BOOST_CHECK_EQUAL(decoded.hasFaceId(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(decoded.hasUri(), false); |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(decoded.hasOrigin(), false); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(decoded.hasCost(), false); |
| 51 | BOOST_CHECK_EQUAL(decoded.hasCapacity(), false); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(decoded.hasCount(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(decoded.hasFlags(), false); |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 54 | BOOST_CHECK_EQUAL(decoded.hasMask(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 55 | BOOST_CHECK_EQUAL(decoded.hasStrategy(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false); |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 58 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 59 | input.setName("/name"); |
| 60 | input.setFaceId(2634); |
| 61 | input.setUri("udp4://192.0.2.1:6363"); |
| 62 | input.setLocalUri("udp4://192.0.2.2:6363"); |
| 63 | input.setOrigin(ROUTE_ORIGIN_NLSR); |
| 64 | input.setCost(1388); |
| 65 | input.setCapacity(2632); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 66 | input.setCount(3100); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 67 | input.setFlags(0xAFC4); |
| 68 | input.setMask(0xF7A1); |
| 69 | input.setStrategy("/strategy-name"); |
| 70 | input.setExpirationPeriod(1800000_ms); |
| 71 | input.setFacePersistency(FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 72 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 73 | decoded.wireDecode(input.wireEncode()); |
| 74 | BOOST_CHECK_EQUAL(decoded.hasName(), true); |
| 75 | BOOST_CHECK_EQUAL(decoded.hasFaceId(), true); |
| 76 | BOOST_CHECK_EQUAL(decoded.hasUri(), true); |
| 77 | BOOST_CHECK_EQUAL(decoded.hasLocalUri(), true); |
| 78 | BOOST_CHECK_EQUAL(decoded.hasOrigin(), true); |
| 79 | BOOST_CHECK_EQUAL(decoded.hasCost(), true); |
| 80 | BOOST_CHECK_EQUAL(decoded.hasCapacity(), true); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 81 | BOOST_CHECK_EQUAL(decoded.hasCount(), true); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(decoded.hasFlags(), true); |
| 83 | BOOST_CHECK_EQUAL(decoded.hasMask(), true); |
| 84 | BOOST_CHECK_EQUAL(decoded.hasStrategy(), true); |
| 85 | BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), true); |
| 86 | BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), true); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 87 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(decoded.getName(), "/name"); |
| 89 | BOOST_CHECK_EQUAL(decoded.getFaceId(), 2634); |
| 90 | BOOST_CHECK_EQUAL(decoded.getUri(), "udp4://192.0.2.1:6363"); |
| 91 | BOOST_CHECK_EQUAL(decoded.getLocalUri(), "udp4://192.0.2.2:6363"); |
Davide Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 93 | BOOST_CHECK_EQUAL(decoded.getCost(), 1388); |
| 94 | BOOST_CHECK_EQUAL(decoded.getCapacity(), 2632); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(decoded.getCount(), 3100); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(decoded.getFlags(), 0xAFC4); |
| 97 | BOOST_CHECK_EQUAL(decoded.getMask(), 0xF7A1); |
| 98 | BOOST_CHECK_EQUAL(decoded.getStrategy(), "/strategy-name"); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), 1800000_ms); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 100 | BOOST_CHECK_EQUAL(decoded.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 101 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 102 | input.unsetName(); |
| 103 | input.unsetFaceId(); |
| 104 | input.unsetUri(); |
| 105 | input.unsetLocalUri(); |
| 106 | input.unsetOrigin(); |
| 107 | input.unsetCost(); |
| 108 | input.unsetCapacity(); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 109 | input.unsetCount(); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 110 | input.unsetFlags(); |
| 111 | input.unsetMask(); |
| 112 | input.unsetStrategy(); |
| 113 | input.unsetExpirationPeriod(); |
| 114 | input.unsetFacePersistency(); |
| 115 | BOOST_CHECK_EQUAL(input.hasName(), false); |
| 116 | BOOST_CHECK_EQUAL(input.hasFaceId(), false); |
| 117 | BOOST_CHECK_EQUAL(input.hasUri(), false); |
| 118 | BOOST_CHECK_EQUAL(input.hasLocalUri(), false); |
| 119 | BOOST_CHECK_EQUAL(input.hasOrigin(), false); |
| 120 | BOOST_CHECK_EQUAL(input.hasCost(), false); |
| 121 | BOOST_CHECK_EQUAL(input.hasCapacity(), false); |
Davide Pesavento | 5e2ccca | 2018-03-06 19:00:15 -0500 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(input.hasCount(), false); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 123 | BOOST_CHECK_EQUAL(input.hasFlags(), false); |
| 124 | BOOST_CHECK_EQUAL(input.hasMask(), false); |
| 125 | BOOST_CHECK_EQUAL(input.hasStrategy(), false); |
| 126 | BOOST_CHECK_EQUAL(input.hasExpirationPeriod(), false); |
| 127 | BOOST_CHECK_EQUAL(input.hasFacePersistency(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 130 | BOOST_AUTO_TEST_CASE(FlagsAndMask) |
| 131 | { |
| 132 | ControlParameters p; |
| 133 | |
| 134 | BOOST_CHECK(!p.hasFlags()); |
| 135 | BOOST_CHECK(!p.hasMask()); |
| 136 | BOOST_CHECK(!p.hasFlagBit(0)); |
| 137 | BOOST_CHECK(!p.getFlagBit(0)); |
| 138 | |
| 139 | // Set bit 2 to true (change Mask) |
| 140 | p.setFlagBit(2, true); |
| 141 | // 2^2 = 4 |
| 142 | BOOST_CHECK_EQUAL(p.getFlags(), 4); |
| 143 | BOOST_CHECK_EQUAL(p.getMask(), 4); |
| 144 | BOOST_CHECK(p.hasFlagBit(2)); |
| 145 | BOOST_CHECK(p.getFlagBit(2)); |
| 146 | BOOST_CHECK(!p.hasFlagBit(1)); |
| 147 | BOOST_CHECK(!p.getFlagBit(1)); |
| 148 | |
| 149 | // Set bit 3 to true (no change to Mask) |
| 150 | p.setFlagBit(3, true, false); |
| 151 | // 2^3 + 2^2 = 12 |
| 152 | BOOST_CHECK_EQUAL(p.getFlags(), 12); |
| 153 | // 2^2 = 4 |
| 154 | BOOST_CHECK_EQUAL(p.getMask(), 4); |
| 155 | BOOST_CHECK(!p.hasFlagBit(3)); |
| 156 | BOOST_CHECK(p.getFlagBit(3)); |
| 157 | BOOST_CHECK(p.hasFlagBit(2)); |
| 158 | BOOST_CHECK(p.getFlagBit(2)); |
| 159 | |
| 160 | // Set bit 1 to false (change Mask) |
| 161 | p.setFlagBit(1, false); |
| 162 | // 2^3 + 2^2 = 12 |
| 163 | BOOST_CHECK_EQUAL(p.getFlags(), 12); |
| 164 | // 2^2 + 2^1 = 6 |
| 165 | BOOST_CHECK_EQUAL(p.getMask(), 6); |
| 166 | BOOST_CHECK(!p.hasFlagBit(3)); |
| 167 | BOOST_CHECK(p.getFlagBit(3)); |
| 168 | BOOST_CHECK(p.hasFlagBit(2)); |
| 169 | BOOST_CHECK(p.getFlagBit(2)); |
| 170 | BOOST_CHECK(p.hasFlagBit(1)); |
| 171 | BOOST_CHECK(!p.getFlagBit(1)); |
| 172 | |
| 173 | // Set bit 2 to false (change Mask) |
| 174 | p.setFlagBit(2, false); |
| 175 | // 2^3 = 8 |
| 176 | BOOST_CHECK_EQUAL(p.getFlags(), 8); |
| 177 | // 2^2 + 2^1 = 6 |
| 178 | BOOST_CHECK_EQUAL(p.getMask(), 6); |
| 179 | BOOST_CHECK(!p.hasFlagBit(3)); |
| 180 | BOOST_CHECK(p.getFlagBit(3)); |
| 181 | BOOST_CHECK(p.hasFlagBit(2)); |
| 182 | BOOST_CHECK(!p.getFlagBit(2)); |
| 183 | BOOST_CHECK(p.hasFlagBit(1)); |
| 184 | BOOST_CHECK(!p.getFlagBit(1)); |
| 185 | |
| 186 | // Set bit 0 to true (change Mask) |
| 187 | p.setFlagBit(0, true); |
| 188 | // 2^3 + 2^0 = 9 |
| 189 | BOOST_CHECK_EQUAL(p.getFlags(), 9); |
| 190 | // 2^2 + 2^1 + 2^0 = 7 |
| 191 | BOOST_CHECK_EQUAL(p.getMask(), 7); |
| 192 | BOOST_CHECK(p.hasFlagBit(0)); |
| 193 | BOOST_CHECK(p.getFlagBit(0)); |
| 194 | |
| 195 | // Unset bit 0 |
| 196 | p.unsetFlagBit(0); |
| 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(1)); |
| 204 | BOOST_CHECK(!p.hasFlagBit(0)); |
| 205 | BOOST_CHECK(p.getFlagBit(0)); |
| 206 | |
| 207 | // Unset bit 3 (already unset in Mask, so no change) |
| 208 | p.unsetFlagBit(3); |
| 209 | BOOST_REQUIRE(p.hasFlags()); |
| 210 | BOOST_REQUIRE(p.hasMask()); |
| 211 | // 2^3 + 2^0 = 9 |
| 212 | BOOST_CHECK_EQUAL(p.getFlags(), 9); |
| 213 | // 2^2 + 2^1 = 6 |
| 214 | BOOST_CHECK_EQUAL(p.getMask(), 6); |
| 215 | BOOST_CHECK(!p.hasFlagBit(3)); |
| 216 | BOOST_CHECK(p.getFlagBit(3)); |
| 217 | |
| 218 | // Unset bit 2 |
| 219 | p.unsetFlagBit(2); |
| 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^1 = 2 |
| 225 | BOOST_CHECK_EQUAL(p.getMask(), 2); |
| 226 | BOOST_CHECK(!p.hasFlagBit(2)); |
| 227 | BOOST_CHECK(!p.getFlagBit(2)); |
| 228 | |
| 229 | // Unset bit 1 |
| 230 | // Flags and Mask fields will be deleted as Mask is now 0 |
| 231 | p.unsetFlagBit(1); |
| 232 | BOOST_CHECK(!p.hasFlags()); |
| 233 | BOOST_CHECK(!p.hasMask()); |
| 234 | BOOST_CHECK(!p.hasFlagBit(3)); |
| 235 | BOOST_CHECK(!p.getFlagBit(3)); |
| 236 | BOOST_CHECK(!p.hasFlagBit(2)); |
| 237 | BOOST_CHECK(!p.getFlagBit(2)); |
| 238 | BOOST_CHECK(!p.hasFlagBit(1)); |
| 239 | BOOST_CHECK(!p.getFlagBit(1)); |
| 240 | BOOST_CHECK(!p.hasFlagBit(0)); |
| 241 | BOOST_CHECK(!p.getFlagBit(0)); |
| 242 | |
| 243 | BOOST_CHECK_THROW(p.setFlagBit(64, true), std::out_of_range); |
| 244 | BOOST_CHECK_THROW(p.unsetFlagBit(64), std::out_of_range); |
| 245 | } |
| 246 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 247 | BOOST_AUTO_TEST_SUITE_END() // TestControlParameters |
| 248 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 249 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 250 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 251 | } // namespace tests |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 252 | } // namespace nfd |
| 253 | } // namespace ndn |