Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 2 | /* |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2019 Regents of the University of California. |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/name-component.hpp" |
| 23 | #include "ndn-cxx/name.hpp" |
| 24 | #include "ndn-cxx/util/string-helper.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
| 27 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 28 | #include <boost/algorithm/string/case_conv.hpp> |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 29 | #include <boost/mpl/vector.hpp> |
| 30 | |
| 31 | namespace ndn { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 32 | namespace name { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 33 | namespace tests { |
| 34 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 35 | BOOST_AUTO_TEST_SUITE(TestNameComponent) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 36 | |
| 37 | BOOST_AUTO_TEST_SUITE(Decode) |
| 38 | |
| 39 | BOOST_AUTO_TEST_CASE(Generic) |
| 40 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 41 | Component comp("0807 6E646E2D637878"_block); |
| 42 | BOOST_CHECK_EQUAL(comp.type(), tlv::GenericNameComponent); |
| 43 | BOOST_CHECK_EQUAL(comp.toUri(), "ndn-cxx"); |
| 44 | BOOST_CHECK_EQUAL(Component::fromEscapedString("ndn-cxx"), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=ndn-cxx"), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 46 | |
| 47 | comp.wireDecode("0800"_block); |
| 48 | BOOST_CHECK_EQUAL(comp.toUri(), "..."); |
| 49 | BOOST_CHECK_EQUAL(Component::fromEscapedString("..."), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=..."), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(Component::fromEscapedString(".%2E."), comp); |
| 52 | |
| 53 | comp.wireDecode("0801 2E"_block); |
| 54 | BOOST_CHECK_EQUAL(comp.toUri(), "...."); |
| 55 | BOOST_CHECK_EQUAL(Component::fromEscapedString("...."), comp); |
| 56 | BOOST_CHECK_EQUAL(Component::fromEscapedString("%2E..%2E"), comp); |
| 57 | |
| 58 | comp.wireDecode("0803 2E412E"_block); |
| 59 | BOOST_CHECK_EQUAL(comp.toUri(), ".A."); |
| 60 | BOOST_CHECK_EQUAL(Component::fromEscapedString(".A."), comp); |
| 61 | |
| 62 | comp.wireDecode("0807 666F6F25626172"_block); |
| 63 | BOOST_CHECK_EQUAL(comp.toUri(), "foo%25bar"); |
| 64 | BOOST_CHECK_EQUAL(Component::fromEscapedString("foo%25bar"), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 65 | BOOST_CHECK_EQUAL(Component::fromEscapedString("8=foo%25bar"), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 66 | |
| 67 | comp.wireDecode("0804 2D2E5F7E"_block); |
| 68 | BOOST_CHECK_EQUAL(comp.toUri(), "-._~"); |
| 69 | BOOST_CHECK_EQUAL(Component::fromEscapedString("-._~"), comp); |
| 70 | |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 71 | comp.wireDecode("0803 393D41"_block); |
| 72 | BOOST_CHECK_EQUAL(comp.toUri(), "9%3DA"); |
| 73 | BOOST_CHECK_EQUAL(Component::fromEscapedString("9%3DA"), comp); |
| 74 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 75 | comp = Component(":/?#[]@"); |
| 76 | BOOST_CHECK_EQUAL(comp.toUri(), "%3A%2F%3F%23%5B%5D%40"); |
| 77 | BOOST_CHECK_EQUAL(Component::fromEscapedString("%3A%2F%3F%23%5B%5D%40"), comp); |
| 78 | |
| 79 | BOOST_CHECK_THROW(Component::fromEscapedString(""), Component::Error); |
| 80 | BOOST_CHECK_THROW(Component::fromEscapedString("."), Component::Error); |
| 81 | BOOST_CHECK_THROW(Component::fromEscapedString(".."), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 84 | static void |
| 85 | testSha256(uint32_t type, const std::string& uriPrefix) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 86 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 87 | std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"; |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 88 | std::string hexUpper = boost::to_upper_copy(hexLower); |
| 89 | std::string hexPct; |
| 90 | for (size_t i = 0; i < hexUpper.size(); i += 2) { |
| 91 | hexPct += "%" + hexUpper.substr(i, 2); |
| 92 | } |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 93 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 94 | Component comp(Block(type, fromHex(hexLower))); |
| 95 | BOOST_CHECK_EQUAL(comp.type(), type); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 96 | BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower); |
| 97 | BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexLower), comp); |
| 98 | BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexUpper), comp); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(Component::fromEscapedString(to_string(type) + "=" + hexPct), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 100 | |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 101 | BOOST_CHECK_THROW(comp.wireDecode(Block(type, fromHex("A791806951F25C4D"))), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 102 | BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix), Component::Error); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 103 | BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix + "a791806951f25c4d"), |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 104 | Component::Error); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 105 | BOOST_CHECK_THROW(Component::fromEscapedString(boost::to_upper_copy(uriPrefix) + hexLower), |
| 106 | Component::Error); |
| 107 | } |
| 108 | |
| 109 | BOOST_AUTO_TEST_CASE(Digest) |
| 110 | { |
| 111 | testSha256(tlv::ImplicitSha256DigestComponent, "sha256digest="); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(Params) |
| 115 | { |
| 116 | testSha256(tlv::ParametersSha256DigestComponent, "params-sha256="); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 119 | BOOST_AUTO_TEST_CASE(OtherType) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 120 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 121 | Component comp("0907 6E646E2D637878"_block); |
| 122 | BOOST_CHECK_EQUAL(comp.type(), 0x09); |
| 123 | BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx"); |
| 124 | BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp); |
| 125 | |
| 126 | comp.wireDecode("FDFFFF00"_block); |
| 127 | BOOST_CHECK_EQUAL(comp.type(), 0xFFFF); |
| 128 | BOOST_CHECK_EQUAL(comp.toUri(), "65535=..."); |
| 129 | BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp); |
| 130 | |
| 131 | comp.wireDecode("FD576501 2E"_block); |
| 132 | BOOST_CHECK_EQUAL(comp.type(), 0x5765); |
| 133 | BOOST_CHECK_EQUAL(comp.toUri(), "22373=...."); |
| 134 | BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp); |
| 135 | |
| 136 | BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error); |
| 137 | BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error); |
| 138 | BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error); |
| 139 | } |
| 140 | |
| 141 | BOOST_AUTO_TEST_CASE(InvalidType) |
| 142 | { |
| 143 | Component comp; |
| 144 | BOOST_CHECK_THROW(comp.wireDecode("0001 80"_block), Component::Error); |
| 145 | BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error); |
| 146 | |
| 147 | BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error); |
| 148 | BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 149 | BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 150 | BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error); |
| 151 | BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error); |
| 152 | BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error); |
| 153 | BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error); |
| 154 | BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error); |
| 155 | BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 156 | BOOST_CHECK_THROW(Component::fromEscapedString("0x3=A"), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 157 | BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame] | 158 | BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error); |
| 159 | BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error); |
| 160 | BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error); |
| 161 | BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | BOOST_AUTO_TEST_SUITE_END() // Decode |
| 165 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 166 | BOOST_AUTO_TEST_CASE(Compare) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 167 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 168 | std::vector<Component> comps = { |
| 169 | Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block), |
| 170 | Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block), |
| 171 | Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 172 | Component("0220 0000000000000000000000000000000000000000000000000000000000000000"_block), |
| 173 | Component("0220 0000000000000000000000000000000000000000000000000000000000000001"_block), |
| 174 | Component("0220 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block), |
| 175 | Component(0x03), |
| 176 | Component("0301 44"_block), |
| 177 | Component("0301 46"_block), |
| 178 | Component("0302 4141"_block), |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 179 | Component(), |
| 180 | Component("D"), |
| 181 | Component("F"), |
| 182 | Component("AA"), |
| 183 | Component(0x53B2), |
| 184 | Component("FD53B201 44"_block), |
| 185 | Component("FD53B201 46"_block), |
| 186 | Component("FD53B202 4141"_block), |
| 187 | }; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 188 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 189 | for (size_t i = 0; i < comps.size(); ++i) { |
| 190 | for (size_t j = 0; j < comps.size(); ++j) { |
| 191 | Component lhs = comps[i]; |
| 192 | Component rhs = comps[j]; |
| 193 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 194 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 195 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 196 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 197 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 198 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 199 | } |
| 200 | } |
Davide Pesavento | 08378cb | 2018-02-01 16:10:54 -0500 | [diff] [blame] | 201 | } |
| 202 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 203 | BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490 |
| 204 | |
| 205 | typedef boost::mpl::vector< |
| 206 | std::vector<uint8_t>, |
| 207 | std::list<uint8_t>, |
| 208 | std::vector<int8_t>, |
| 209 | std::list<int8_t> |
| 210 | > ContainerTypes; |
| 211 | |
| 212 | BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes) |
| 213 | { |
| 214 | T bytes; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 215 | Component c(bytes.begin(), bytes.end()); |
| 216 | BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 217 | BOOST_CHECK_EQUAL(c.value_size(), 0); |
| 218 | BOOST_CHECK_EQUAL(c.size(), 2); |
| 219 | } |
| 220 | |
| 221 | BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes) |
| 222 | { |
| 223 | T bytes{1}; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 224 | Component c(0x09, bytes.begin(), bytes.end()); |
| 225 | BOOST_CHECK_EQUAL(c.type(), 0x09); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 226 | BOOST_CHECK_EQUAL(c.value_size(), 1); |
| 227 | BOOST_CHECK_EQUAL(c.size(), 3); |
| 228 | } |
| 229 | |
| 230 | BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes) |
| 231 | { |
| 232 | T bytes{1, 2, 3, 4}; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 233 | Component c(0xFCEC, bytes.begin(), bytes.end()); |
| 234 | BOOST_CHECK_EQUAL(c.type(), 0xFCEC); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 235 | BOOST_CHECK_EQUAL(c.value_size(), 4); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 236 | BOOST_CHECK_EQUAL(c.size(), 8); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators |
| 240 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 241 | BOOST_AUTO_TEST_SUITE(NamingConvention) |
| 242 | |
| 243 | template<typename ArgType> |
| 244 | struct ConventionTest |
| 245 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 246 | function<Component(ArgType)> makeComponent; |
| 247 | function<ArgType(const Component&)> getValue; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 248 | function<Name&(Name&, ArgType)> append; |
| 249 | Name expected; |
| 250 | ArgType value; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 251 | function<bool(const Component&)> isComponent; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 254 | class ConventionMarker |
| 255 | { |
| 256 | }; |
| 257 | |
| 258 | class ConventionTyped |
| 259 | { |
| 260 | public: |
| 261 | ConventionTyped() |
| 262 | { |
| 263 | name::setConventionEncoding(name::Convention::TYPED); |
| 264 | } |
| 265 | |
| 266 | ~ConventionTyped() |
| 267 | { |
| 268 | name::setConventionEncoding(name::Convention::MARKER); |
| 269 | } |
| 270 | }; |
| 271 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 272 | class NumberWithMarker |
| 273 | { |
| 274 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 275 | using ConventionRev = ConventionMarker; |
| 276 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 277 | ConventionTest<uint64_t> |
| 278 | operator()() const |
| 279 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 280 | return {bind(&Component::fromNumberWithMarker, 0xAA, _1), |
| 281 | bind(&Component::toNumberWithMarker, _1, 0xAA), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 282 | bind(&Name::appendNumberWithMarker, _1, 0xAA, _2), |
| 283 | Name("/%AA%03%E8"), |
| 284 | 1000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 285 | bind(&Component::isNumberWithMarker, _1, 0xAA)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 286 | } |
| 287 | }; |
| 288 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 289 | class SegmentMarker |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 290 | { |
| 291 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 292 | using ConventionRev = ConventionMarker; |
| 293 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 294 | ConventionTest<uint64_t> |
| 295 | operator()() const |
| 296 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 297 | return {&Component::fromSegment, |
| 298 | bind(&Component::toSegment, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 299 | bind(&Name::appendSegment, _1, _2), |
| 300 | Name("/%00%27%10"), |
| 301 | 10000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 302 | bind(&Component::isSegment, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 303 | } |
| 304 | }; |
| 305 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 306 | class SegmentTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 307 | { |
| 308 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 309 | using ConventionRev = ConventionTyped; |
| 310 | |
| 311 | ConventionTest<uint64_t> |
| 312 | operator()() const |
| 313 | { |
| 314 | return {&Component::fromSegment, |
| 315 | bind(&Component::toSegment, _1), |
| 316 | bind(&Name::appendSegment, _1, _2), |
| 317 | Name("/33=%27%10"), |
| 318 | 10000, |
| 319 | bind(&Component::isSegment, _1)}; |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | class SegmentOffsetMarker |
| 324 | { |
| 325 | public: |
| 326 | using ConventionRev = ConventionMarker; |
| 327 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 328 | ConventionTest<uint64_t> |
| 329 | operator()() const |
| 330 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 331 | return {&Component::fromSegmentOffset, |
| 332 | bind(&Component::toSegmentOffset, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 333 | bind(&Name::appendSegmentOffset, _1, _2), |
| 334 | Name("/%FB%00%01%86%A0"), |
| 335 | 100000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 336 | bind(&Component::isSegmentOffset, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 337 | } |
| 338 | }; |
| 339 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 340 | class ByteOffsetTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 341 | { |
| 342 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 343 | using ConventionRev = ConventionTyped; |
| 344 | |
| 345 | ConventionTest<uint64_t> |
| 346 | operator()() const |
| 347 | { |
| 348 | return {&Component::fromByteOffset, |
| 349 | bind(&Component::toByteOffset, _1), |
| 350 | bind(&Name::appendByteOffset, _1, _2), |
| 351 | Name("/34=%00%01%86%A0"), |
| 352 | 100000, |
| 353 | bind(&Component::isByteOffset, _1)}; |
| 354 | } |
| 355 | }; |
| 356 | |
| 357 | class VersionMarker |
| 358 | { |
| 359 | public: |
| 360 | using ConventionRev = ConventionMarker; |
| 361 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 362 | ConventionTest<uint64_t> |
| 363 | operator()() const |
| 364 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 365 | return {&Component::fromVersion, |
| 366 | bind(&Component::toVersion, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 367 | [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); }, |
| 368 | Name("/%FD%00%0FB%40"), |
| 369 | 1000000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 370 | bind(&Component::isVersion, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 371 | } |
| 372 | }; |
| 373 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 374 | class VersionTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 375 | { |
| 376 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 377 | using ConventionRev = ConventionTyped; |
| 378 | |
| 379 | ConventionTest<uint64_t> |
| 380 | operator()() const |
| 381 | { |
| 382 | return {&Component::fromVersion, |
| 383 | bind(&Component::toVersion, _1), |
| 384 | [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); }, |
| 385 | Name("/35=%00%0FB%40"), |
| 386 | 1000000, |
| 387 | bind(&Component::isVersion, _1)}; |
| 388 | } |
| 389 | }; |
| 390 | |
| 391 | class TimestampMarker |
| 392 | { |
| 393 | public: |
| 394 | using ConventionRev = ConventionMarker; |
| 395 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 396 | ConventionTest<time::system_clock::TimePoint> |
| 397 | operator()() const |
| 398 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 399 | return {&Component::fromTimestamp, |
| 400 | bind(&Component::toTimestamp, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 401 | [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); }, |
| 402 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 403 | time::getUnixEpoch() + 14600_days, // 40 years |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 404 | bind(&Component::isTimestamp, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 405 | } |
| 406 | }; |
| 407 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 408 | class TimestampTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 409 | { |
| 410 | public: |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 411 | using ConventionRev = ConventionTyped; |
| 412 | |
| 413 | ConventionTest<time::system_clock::TimePoint> |
| 414 | operator()() const |
| 415 | { |
| 416 | return {&Component::fromTimestamp, |
| 417 | bind(&Component::toTimestamp, _1), |
| 418 | [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); }, |
| 419 | Name("/36=%00%04%7BE%E3%1B%00%00"), |
| 420 | time::getUnixEpoch() + 14600_days, // 40 years |
| 421 | bind(&Component::isTimestamp, _1)}; |
| 422 | } |
| 423 | }; |
| 424 | |
| 425 | class SequenceNumberMarker |
| 426 | { |
| 427 | public: |
| 428 | using ConventionRev = ConventionMarker; |
| 429 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 430 | ConventionTest<uint64_t> |
| 431 | operator()() const |
| 432 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 433 | return {&Component::fromSequenceNumber, |
| 434 | bind(&Component::toSequenceNumber, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 435 | bind(&Name::appendSequenceNumber, _1, _2), |
| 436 | Name("/%FE%00%98%96%80"), |
| 437 | 10000000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 438 | bind(&Component::isSequenceNumber, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 439 | } |
| 440 | }; |
| 441 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 442 | class SequenceNumberTyped |
| 443 | { |
| 444 | public: |
| 445 | using ConventionRev = ConventionTyped; |
| 446 | |
| 447 | ConventionTest<uint64_t> |
| 448 | operator()() const |
| 449 | { |
| 450 | return {&Component::fromSequenceNumber, |
| 451 | bind(&Component::toSequenceNumber, _1), |
| 452 | bind(&Name::appendSequenceNumber, _1, _2), |
| 453 | Name("/37=%00%98%96%80"), |
| 454 | 10000000, |
| 455 | bind(&Component::isSequenceNumber, _1)}; |
| 456 | } |
| 457 | }; |
| 458 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 459 | using ConventionTests = boost::mpl::vector< |
| 460 | NumberWithMarker, |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 461 | SegmentMarker, |
| 462 | SegmentTyped, |
| 463 | SegmentOffsetMarker, |
| 464 | ByteOffsetTyped, |
| 465 | VersionMarker, |
| 466 | VersionTyped, |
| 467 | TimestampMarker, |
| 468 | TimestampTyped, |
| 469 | SequenceNumberMarker, |
| 470 | SequenceNumberTyped |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 471 | >; |
| 472 | |
Junxiao Shi | e209961 | 2019-02-15 14:46:27 +0000 | [diff] [blame^] | 473 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Convention, T, ConventionTests, T::ConventionRev) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 474 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 475 | Component invalidComponent1; |
| 476 | Component invalidComponent2("1234567890"); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 477 | |
| 478 | auto test = T()(); |
| 479 | |
| 480 | const Name& expected = test.expected; |
| 481 | BOOST_TEST_MESSAGE("Check " << expected[0].toUri()); |
| 482 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 483 | Component actualComponent = test.makeComponent(test.value); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 484 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 485 | |
| 486 | Name actualName; |
| 487 | test.append(actualName, test.value); |
| 488 | BOOST_CHECK_EQUAL(actualName, expected); |
| 489 | |
| 490 | BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true); |
| 491 | BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value); |
| 492 | |
| 493 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false); |
| 494 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false); |
| 495 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 496 | BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error); |
| 497 | BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | BOOST_AUTO_TEST_SUITE_END() // NamingConvention |
| 501 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 502 | BOOST_AUTO_TEST_SUITE_END() // TestNameComponent |
| 503 | |
| 504 | } // namespace tests |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 505 | } // namespace name |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 506 | } // namespace ndn |