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 | /* |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "name-component.hpp" |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 23 | #include "name.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 24 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 25 | #include "block-literal.hpp" |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 26 | #include "boost-test.hpp" |
| 27 | #include <boost/mpl/vector.hpp> |
| 28 | |
| 29 | namespace ndn { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 30 | namespace name { |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 31 | namespace tests { |
| 32 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 33 | using namespace ndn::tests; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 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 | |
| 84 | BOOST_AUTO_TEST_CASE(Digest) |
| 85 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 86 | std::string uriPrefix = "sha256digest="; |
| 87 | std::string hexLower = "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"; |
| 88 | std::string hexUpper = "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"; |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame^] | 89 | std::string hexPct = "%28%BA%D4%B5%27%5B%D3%92%DB%B6%70%C7%5C%F0%B6%6F" |
| 90 | "%13%F7%94%2B%21%E8%0F%55%C0%E8%6B%37%47%53%A5%48"; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 91 | |
| 92 | Component comp("0120 28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548"_block); |
| 93 | BOOST_CHECK_EQUAL(comp.toUri(), uriPrefix + hexLower); |
| 94 | BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexLower), comp); |
| 95 | BOOST_CHECK_EQUAL(Component::fromEscapedString(uriPrefix + hexUpper), comp); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame^] | 96 | BOOST_CHECK_EQUAL(Component::fromEscapedString("1=" + hexPct), comp); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 97 | |
| 98 | BOOST_CHECK_THROW(comp.wireDecode("0108 A791806951F25C4D"_block), Component::Error); |
| 99 | BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix), Component::Error); |
| 100 | BOOST_CHECK_THROW(Component::fromEscapedString(uriPrefix + "=a791806951f25c4d"), |
| 101 | Component::Error); |
| 102 | BOOST_CHECK_THROW(Component::fromEscapedString("1=" + hexLower), Component::Error); |
| 103 | BOOST_CHECK_THROW(Component::fromEscapedString("SHA256DIGEST=" + hexLower), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 106 | BOOST_AUTO_TEST_CASE(OtherType) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 107 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 108 | Component comp("0907 6E646E2D637878"_block); |
| 109 | BOOST_CHECK_EQUAL(comp.type(), 0x09); |
| 110 | BOOST_CHECK_EQUAL(comp.toUri(), "9=ndn-cxx"); |
| 111 | BOOST_CHECK_EQUAL(Component::fromEscapedString("9=ndn-cxx"), comp); |
| 112 | |
| 113 | comp.wireDecode("FDFFFF00"_block); |
| 114 | BOOST_CHECK_EQUAL(comp.type(), 0xFFFF); |
| 115 | BOOST_CHECK_EQUAL(comp.toUri(), "65535=..."); |
| 116 | BOOST_CHECK_EQUAL(Component::fromEscapedString("65535=..."), comp); |
| 117 | |
| 118 | comp.wireDecode("FD576501 2E"_block); |
| 119 | BOOST_CHECK_EQUAL(comp.type(), 0x5765); |
| 120 | BOOST_CHECK_EQUAL(comp.toUri(), "22373=...."); |
| 121 | BOOST_CHECK_EQUAL(Component::fromEscapedString("22373=...."), comp); |
| 122 | |
| 123 | BOOST_CHECK_THROW(Component::fromEscapedString("3="), Component::Error); |
| 124 | BOOST_CHECK_THROW(Component::fromEscapedString("3=."), Component::Error); |
| 125 | BOOST_CHECK_THROW(Component::fromEscapedString("3=.."), Component::Error); |
| 126 | } |
| 127 | |
| 128 | BOOST_AUTO_TEST_CASE(InvalidType) |
| 129 | { |
| 130 | Component comp; |
| 131 | BOOST_CHECK_THROW(comp.wireDecode("0001 80"_block), Component::Error); |
| 132 | BOOST_CHECK_THROW(comp.wireDecode("FE0001000001 80"_block), Component::Error); |
| 133 | |
| 134 | BOOST_CHECK_THROW(Component::fromEscapedString("0=A"), Component::Error); |
| 135 | BOOST_CHECK_THROW(Component::fromEscapedString("65536=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame^] | 136 | BOOST_CHECK_THROW(Component::fromEscapedString("4294967296=A"), Component::Error); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 137 | BOOST_CHECK_THROW(Component::fromEscapedString("-1=A"), Component::Error); |
| 138 | BOOST_CHECK_THROW(Component::fromEscapedString("+=A"), Component::Error); |
| 139 | BOOST_CHECK_THROW(Component::fromEscapedString("=A"), Component::Error); |
| 140 | BOOST_CHECK_THROW(Component::fromEscapedString("0x1=A"), Component::Error); |
| 141 | BOOST_CHECK_THROW(Component::fromEscapedString("Z=A"), Component::Error); |
| 142 | BOOST_CHECK_THROW(Component::fromEscapedString("09=A"), Component::Error); |
| 143 | BOOST_CHECK_THROW(Component::fromEscapedString("0x2=A"), Component::Error); |
| 144 | BOOST_CHECK_THROW(Component::fromEscapedString("+9=A"), Component::Error); |
Junxiao Shi | d2e6063 | 2018-08-10 10:48:44 -0600 | [diff] [blame^] | 145 | BOOST_CHECK_THROW(Component::fromEscapedString(" 9=A"), Component::Error); |
| 146 | BOOST_CHECK_THROW(Component::fromEscapedString("9 =A"), Component::Error); |
| 147 | BOOST_CHECK_THROW(Component::fromEscapedString("9.0=A"), Component::Error); |
| 148 | BOOST_CHECK_THROW(Component::fromEscapedString("9E0=A"), Component::Error); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | BOOST_AUTO_TEST_SUITE_END() // Decode |
| 152 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 153 | BOOST_AUTO_TEST_CASE(Compare) |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 154 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 155 | std::vector<Component> comps = { |
| 156 | Component("0120 0000000000000000000000000000000000000000000000000000000000000000"_block), |
| 157 | Component("0120 0000000000000000000000000000000000000000000000000000000000000001"_block), |
| 158 | Component("0120 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"_block), |
| 159 | Component(0x02), |
| 160 | Component("0201 44"_block), |
| 161 | Component("0201 46"_block), |
| 162 | Component("0202 4141"_block), |
| 163 | Component(), |
| 164 | Component("D"), |
| 165 | Component("F"), |
| 166 | Component("AA"), |
| 167 | Component(0x53B2), |
| 168 | Component("FD53B201 44"_block), |
| 169 | Component("FD53B201 46"_block), |
| 170 | Component("FD53B202 4141"_block), |
| 171 | }; |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 172 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 173 | for (size_t i = 0; i < comps.size(); ++i) { |
| 174 | for (size_t j = 0; j < comps.size(); ++j) { |
| 175 | Component lhs = comps[i]; |
| 176 | Component rhs = comps[j]; |
| 177 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 178 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 179 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 180 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 181 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 182 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 183 | } |
| 184 | } |
Davide Pesavento | 08378cb | 2018-02-01 16:10:54 -0500 | [diff] [blame] | 185 | } |
| 186 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 187 | BOOST_AUTO_TEST_SUITE(CreateFromIterators) // Bug 2490 |
| 188 | |
| 189 | typedef boost::mpl::vector< |
| 190 | std::vector<uint8_t>, |
| 191 | std::list<uint8_t>, |
| 192 | std::vector<int8_t>, |
| 193 | std::list<int8_t> |
| 194 | > ContainerTypes; |
| 195 | |
| 196 | BOOST_AUTO_TEST_CASE_TEMPLATE(ZeroOctet, T, ContainerTypes) |
| 197 | { |
| 198 | T bytes; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 199 | Component c(bytes.begin(), bytes.end()); |
| 200 | BOOST_CHECK_EQUAL(c.type(), tlv::GenericNameComponent); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 201 | BOOST_CHECK_EQUAL(c.value_size(), 0); |
| 202 | BOOST_CHECK_EQUAL(c.size(), 2); |
| 203 | } |
| 204 | |
| 205 | BOOST_AUTO_TEST_CASE_TEMPLATE(OneOctet, T, ContainerTypes) |
| 206 | { |
| 207 | T bytes{1}; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 208 | Component c(0x09, bytes.begin(), bytes.end()); |
| 209 | BOOST_CHECK_EQUAL(c.type(), 0x09); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 210 | BOOST_CHECK_EQUAL(c.value_size(), 1); |
| 211 | BOOST_CHECK_EQUAL(c.size(), 3); |
| 212 | } |
| 213 | |
| 214 | BOOST_AUTO_TEST_CASE_TEMPLATE(FourOctets, T, ContainerTypes) |
| 215 | { |
| 216 | T bytes{1, 2, 3, 4}; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 217 | Component c(0xFCEC, bytes.begin(), bytes.end()); |
| 218 | BOOST_CHECK_EQUAL(c.type(), 0xFCEC); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 219 | BOOST_CHECK_EQUAL(c.value_size(), 4); |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 220 | BOOST_CHECK_EQUAL(c.size(), 8); |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | BOOST_AUTO_TEST_SUITE_END() // CreateFromIterators |
| 224 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 225 | BOOST_AUTO_TEST_SUITE(NamingConvention) |
| 226 | |
| 227 | template<typename ArgType> |
| 228 | struct ConventionTest |
| 229 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 230 | function<Component(ArgType)> makeComponent; |
| 231 | function<ArgType(const Component&)> getValue; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 232 | function<Name&(Name&, ArgType)> append; |
| 233 | Name expected; |
| 234 | ArgType value; |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 235 | function<bool(const Component&)> isComponent; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | class NumberWithMarker |
| 239 | { |
| 240 | public: |
| 241 | ConventionTest<uint64_t> |
| 242 | operator()() const |
| 243 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 244 | return {bind(&Component::fromNumberWithMarker, 0xAA, _1), |
| 245 | bind(&Component::toNumberWithMarker, _1, 0xAA), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 246 | bind(&Name::appendNumberWithMarker, _1, 0xAA, _2), |
| 247 | Name("/%AA%03%E8"), |
| 248 | 1000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 249 | bind(&Component::isNumberWithMarker, _1, 0xAA)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 250 | } |
| 251 | }; |
| 252 | |
| 253 | class Segment |
| 254 | { |
| 255 | public: |
| 256 | ConventionTest<uint64_t> |
| 257 | operator()() const |
| 258 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 259 | return {&Component::fromSegment, |
| 260 | bind(&Component::toSegment, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 261 | bind(&Name::appendSegment, _1, _2), |
| 262 | Name("/%00%27%10"), |
| 263 | 10000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 264 | bind(&Component::isSegment, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | |
| 268 | class SegmentOffset |
| 269 | { |
| 270 | public: |
| 271 | ConventionTest<uint64_t> |
| 272 | operator()() const |
| 273 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 274 | return {&Component::fromSegmentOffset, |
| 275 | bind(&Component::toSegmentOffset, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 276 | bind(&Name::appendSegmentOffset, _1, _2), |
| 277 | Name("/%FB%00%01%86%A0"), |
| 278 | 100000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 279 | bind(&Component::isSegmentOffset, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 280 | } |
| 281 | }; |
| 282 | |
| 283 | class Version |
| 284 | { |
| 285 | public: |
| 286 | ConventionTest<uint64_t> |
| 287 | operator()() const |
| 288 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 289 | return {&Component::fromVersion, |
| 290 | bind(&Component::toVersion, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 291 | [] (Name& name, uint64_t version) -> Name& { return name.appendVersion(version); }, |
| 292 | Name("/%FD%00%0FB%40"), |
| 293 | 1000000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 294 | bind(&Component::isVersion, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 295 | } |
| 296 | }; |
| 297 | |
| 298 | class Timestamp |
| 299 | { |
| 300 | public: |
| 301 | ConventionTest<time::system_clock::TimePoint> |
| 302 | operator()() const |
| 303 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 304 | return {&Component::fromTimestamp, |
| 305 | bind(&Component::toTimestamp, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 306 | [] (Name& name, time::system_clock::TimePoint t) -> Name& { return name.appendTimestamp(t); }, |
| 307 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 308 | time::getUnixEpoch() + 14600_days, // 40 years |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 309 | bind(&Component::isTimestamp, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 310 | } |
| 311 | }; |
| 312 | |
| 313 | class SequenceNumber |
| 314 | { |
| 315 | public: |
| 316 | ConventionTest<uint64_t> |
| 317 | operator()() const |
| 318 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 319 | return {&Component::fromSequenceNumber, |
| 320 | bind(&Component::toSequenceNumber, _1), |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 321 | bind(&Name::appendSequenceNumber, _1, _2), |
| 322 | Name("/%FE%00%98%96%80"), |
| 323 | 10000000, |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 324 | bind(&Component::isSequenceNumber, _1)}; |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 325 | } |
| 326 | }; |
| 327 | |
| 328 | using ConventionTests = boost::mpl::vector< |
| 329 | NumberWithMarker, |
| 330 | Segment, |
| 331 | SegmentOffset, |
| 332 | Version, |
| 333 | Timestamp, |
| 334 | SequenceNumber |
| 335 | >; |
| 336 | |
| 337 | BOOST_AUTO_TEST_CASE_TEMPLATE(Convention, T, ConventionTests) |
| 338 | { |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 339 | Component invalidComponent1; |
| 340 | Component invalidComponent2("1234567890"); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 341 | |
| 342 | auto test = T()(); |
| 343 | |
| 344 | const Name& expected = test.expected; |
| 345 | BOOST_TEST_MESSAGE("Check " << expected[0].toUri()); |
| 346 | |
| 347 | BOOST_CHECK_EQUAL(expected[0].isGeneric(), true); |
| 348 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 349 | Component actualComponent = test.makeComponent(test.value); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 350 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 351 | |
| 352 | Name actualName; |
| 353 | test.append(actualName, test.value); |
| 354 | BOOST_CHECK_EQUAL(actualName, expected); |
| 355 | |
| 356 | BOOST_CHECK_EQUAL(test.isComponent(expected[0]), true); |
| 357 | BOOST_CHECK_EQUAL(test.getValue(expected[0]), test.value); |
| 358 | |
| 359 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent1), false); |
| 360 | BOOST_CHECK_EQUAL(test.isComponent(invalidComponent2), false); |
| 361 | |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 362 | BOOST_CHECK_THROW(test.getValue(invalidComponent1), Component::Error); |
| 363 | BOOST_CHECK_THROW(test.getValue(invalidComponent2), Component::Error); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | BOOST_AUTO_TEST_SUITE_END() // NamingConvention |
| 367 | |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 368 | BOOST_AUTO_TEST_SUITE_END() // TestNameComponent |
| 369 | |
| 370 | } // namespace tests |
Junxiao Shi | cf4ac5b | 2018-03-28 22:46:06 +0000 | [diff] [blame] | 371 | } // namespace name |
Junxiao Shi | df4b24e | 2016-07-14 21:41:43 +0000 | [diff] [blame] | 372 | } // namespace ndn |