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 | /* |
| 3 | * Copyright (c) 2013-2018 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 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 22 | #include "mgmt/nfd/control-parameters.hpp" |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 25 | |
| 26 | namespace ndn { |
| 27 | namespace nfd { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 28 | namespace tests { |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 29 | |
Junxiao Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 30 | BOOST_AUTO_TEST_SUITE(Mgmt) |
| 31 | BOOST_AUTO_TEST_SUITE(Nfd) |
| 32 | BOOST_AUTO_TEST_SUITE(TestControlParameters) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 33 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 34 | BOOST_AUTO_TEST_CASE(Fields) |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 35 | { |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 36 | ControlParameters input; |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 37 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 38 | ControlParameters decoded(input.wireEncode()); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 39 | BOOST_CHECK_EQUAL(decoded.hasName(), false); |
| 40 | BOOST_CHECK_EQUAL(decoded.hasFaceId(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 41 | BOOST_CHECK_EQUAL(decoded.hasUri(), false); |
Eric Newberry | d7f5b28 | 2017-03-28 19:55:20 -0700 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(decoded.hasLocalUri(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(decoded.hasOrigin(), false); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(decoded.hasCost(), false); |
| 45 | BOOST_CHECK_EQUAL(decoded.hasCapacity(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(decoded.hasFlags(), false); |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 47 | BOOST_CHECK_EQUAL(decoded.hasMask(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(decoded.hasStrategy(), false); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(decoded.hasExpirationPeriod(), false); |
Yukai Tu | d93c5fc | 2015-08-25 11:37:16 +0800 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(decoded.hasFacePersistency(), false); |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 51 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 52 | 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 Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 64 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 65 | 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 Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 78 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 79 | 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 Pesavento | e8e48c2 | 2017-04-13 00:35:33 -0400 | [diff] [blame] | 83 | BOOST_CHECK_EQUAL(decoded.getOrigin(), ROUTE_ORIGIN_NLSR); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 84 | 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 Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 89 | BOOST_CHECK_EQUAL(decoded.getExpirationPeriod(), 1800000_ms); |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 90 | BOOST_CHECK_EQUAL(decoded.getFacePersistency(), FacePersistency::FACE_PERSISTENCY_PERSISTENT); |
Junxiao Shi | 5f6c74f | 2014-04-18 16:29:44 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 22f8568 | 2018-01-22 19:23:22 +0000 | [diff] [blame] | 92 | 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 Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Eric Newberry | da916d6 | 2016-08-11 23:04:34 -0700 | [diff] [blame] | 118 | BOOST_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 Shi | 7357ef2 | 2016-09-07 02:39:37 +0000 | [diff] [blame] | 235 | BOOST_AUTO_TEST_SUITE_END() // TestControlParameters |
| 236 | BOOST_AUTO_TEST_SUITE_END() // Nfd |
| 237 | BOOST_AUTO_TEST_SUITE_END() // Mgmt |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 238 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 239 | } // namespace tests |
Junxiao Shi | bc19b37 | 2014-03-23 16:59:25 -0700 | [diff] [blame] | 240 | } // namespace nfd |
| 241 | } // namespace ndn |