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