Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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. |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 22 | #include "name.hpp" |
| 23 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 24 | #include "boost-test.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | #include <boost/tuple/tuple.hpp> |
| 26 | #include <boost/mpl/vector.hpp> |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 27 | #include <unordered_map> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 29 | namespace ndn { |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 30 | namespace tests { |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 31 | |
| 32 | BOOST_AUTO_TEST_SUITE(TestName) |
| 33 | |
| 34 | static const uint8_t TestName[] = { |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 35 | 0x7, 0x14, // Name |
| 36 | 0x8, 0x5, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 37 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 38 | 0x8, 0x3, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 39 | 0x6e, 0x64, 0x6e, |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 40 | 0x8, 0x6, // NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 41 | 0x70, 0x72, 0x65, 0x66, 0x69, 0x78 |
| 42 | }; |
| 43 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 44 | const uint8_t Name1[] = {0x7, 0x7, // Name |
| 45 | 0x8, 0x5, // NameComponent |
| 46 | 0x6c, 0x6f, 0x63, 0x61, 0x6c}; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 48 | const uint8_t Name2[] = {0x7, 0xc, // Name |
| 49 | 0x8, 0x5, // NameComponent |
| 50 | 0x6c, 0x6f, 0x63, 0x61, 0x6c, |
| 51 | 0x8, 0x3, // NameComponent |
| 52 | 0x6e, 0x64, 0x6e}; |
| 53 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 54 | BOOST_AUTO_TEST_CASE(Basic) |
| 55 | { |
| 56 | Name name("/hello/world"); |
| 57 | |
| 58 | BOOST_CHECK_NO_THROW(name.at(0)); |
| 59 | BOOST_CHECK_NO_THROW(name.at(1)); |
| 60 | BOOST_CHECK_NO_THROW(name.at(-1)); |
| 61 | BOOST_CHECK_NO_THROW(name.at(-2)); |
| 62 | |
| 63 | BOOST_CHECK_THROW(name.at(2), Name::Error); |
| 64 | BOOST_CHECK_THROW(name.at(-3), Name::Error); |
| 65 | } |
| 66 | |
| 67 | BOOST_AUTO_TEST_CASE(Encode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 68 | { |
| 69 | Name name("/local/ndn/prefix"); |
| 70 | |
| 71 | const Block &wire = name.wireEncode(); |
| 72 | |
| 73 | // for (Buffer::const_iterator i = wire.begin(); |
| 74 | // i != wire.end(); |
| 75 | // ++i) |
| 76 | // { |
| 77 | // std::ios::fmtflags saveFlags = std::cout.flags(std::ios::hex); |
| 78 | |
| 79 | // if (i != wire.begin()) |
| 80 | // std::cout << ", "; |
| 81 | // std::cout << "0x" << static_cast<uint32_t>(*i); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 83 | // std::cout.flags(saveFlags); |
| 84 | // } |
| 85 | // std::cout << std::endl; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 86 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 87 | BOOST_CHECK_EQUAL_COLLECTIONS(TestName, TestName+sizeof(TestName), |
| 88 | wire.begin(), wire.end()); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(Decode) |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 93 | { |
| 94 | Block block(TestName, sizeof(TestName)); |
| 95 | |
| 96 | Name name(block); |
| 97 | |
| 98 | BOOST_CHECK_EQUAL(name.toUri(), "/local/ndn/prefix"); |
| 99 | } |
| 100 | |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 101 | BOOST_AUTO_TEST_CASE(AppendsAndMultiEncode) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 102 | { |
| 103 | Name name("/local"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 104 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 105 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 106 | Name1, Name1 + sizeof(Name1)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 107 | |
| 108 | name.append("ndn"); |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 109 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
Alexander Afanasyev | 4b45628 | 2014-02-13 00:34:34 -0800 | [diff] [blame] | 111 | Name2, Name2 + sizeof(Name2)); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 112 | |
| 113 | name.append("prefix"); |
| 114 | BOOST_CHECK_EQUAL_COLLECTIONS(name.wireEncode().begin(), name.wireEncode().end(), |
| 115 | TestName, TestName+sizeof(TestName)); |
| 116 | } |
| 117 | |
Junxiao Shi | 2586be4 | 2015-01-30 20:13:01 -0700 | [diff] [blame] | 118 | BOOST_AUTO_TEST_CASE(ZeroLengthComponent) |
| 119 | { |
| 120 | static const uint8_t compOctets[] {0x08, 0x00}; |
| 121 | Block compBlock(compOctets, sizeof(compOctets)); |
| 122 | name::Component comp; |
| 123 | BOOST_REQUIRE_NO_THROW(comp.wireDecode(compBlock)); |
| 124 | BOOST_CHECK_EQUAL(comp.value_size(), 0); |
| 125 | |
| 126 | static const uint8_t nameOctets[] {0x07, 0x08, 0x08, 0x01, 0x41, 0x08, 0x00, 0x08, 0x01, 0x42}; |
| 127 | Block nameBlock(nameOctets, sizeof(nameOctets)); |
| 128 | static const std::string nameUri("/A/.../B"); |
| 129 | Name name; |
| 130 | BOOST_REQUIRE_NO_THROW(name.wireDecode(nameBlock)); |
| 131 | BOOST_CHECK_EQUAL(name.toUri(), nameUri); |
| 132 | Block nameEncoded = name.wireEncode(); |
| 133 | BOOST_CHECK(nameEncoded == nameBlock); |
| 134 | |
| 135 | Name name2; |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 136 | BOOST_REQUIRE_NO_THROW(name2 = Name(nameUri)); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 137 | BOOST_CHECK_EQUAL(name2.toUri(), nameUri); |
Junxiao Shi | 2586be4 | 2015-01-30 20:13:01 -0700 | [diff] [blame] | 138 | Block name2Encoded = name2.wireEncode(); |
| 139 | BOOST_CHECK(name2Encoded == nameBlock); |
| 140 | } |
| 141 | |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 143 | { |
| 144 | Name name; |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 145 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 146 | { |
| 147 | name.appendNumber(i); |
| 148 | } |
| 149 | |
| 150 | BOOST_CHECK_EQUAL(name.size(), 10); |
| 151 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 152 | for (uint32_t i = 0; i < 10; i++) |
Steve DiBenedetto | c145d49 | 2014-03-11 16:35:45 -0600 | [diff] [blame] | 153 | { |
| 154 | BOOST_CHECK_EQUAL(name[i].toNumber(), i); |
| 155 | } |
| 156 | } |
| 157 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 158 | class Numeric |
| 159 | { |
| 160 | public: |
| 161 | typedef std::list<boost::tuple<function<name::Component(uint64_t)>, |
| 162 | function<uint64_t(const name::Component&)>, |
| 163 | function<Name&(Name&, uint64_t)>, |
| 164 | Name/*expected*/, |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 165 | uint64_t/*value*/, |
| 166 | function<bool(const name::Component&)> > > Dataset; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 167 | |
| 168 | Numeric() |
| 169 | { |
| 170 | dataset.push_back(boost::make_tuple(bind(&name::Component::fromNumberWithMarker, |
| 171 | 0xAA, _1), |
| 172 | bind(&name::Component::toNumberWithMarker, _1, 0xAA), |
| 173 | bind(&Name::appendNumberWithMarker, _1, 0xAA, _2), |
| 174 | Name("/%AA%03%E8"), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 175 | 1000, |
| 176 | bind(&name::Component::isNumberWithMarker, _1, 0xAA))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 177 | dataset.push_back(boost::make_tuple(&name::Component::fromSegment, |
| 178 | bind(&name::Component::toSegment, _1), |
| 179 | bind(&Name::appendSegment, _1, _2), |
| 180 | Name("/%00%27%10"), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 181 | 10000, |
| 182 | bind(&name::Component::isSegment, _1))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 183 | dataset.push_back(boost::make_tuple(&name::Component::fromSegmentOffset, |
| 184 | bind(&name::Component::toSegmentOffset, _1), |
| 185 | bind(&Name::appendSegmentOffset, _1, _2), |
| 186 | Name("/%FB%00%01%86%A0"), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 187 | 100000, |
| 188 | bind(&name::Component::isSegmentOffset, _1))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 189 | dataset.push_back(boost::make_tuple(&name::Component::fromVersion, |
| 190 | bind(&name::Component::toVersion, _1), |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 191 | bind(static_cast<Name&(Name::*)(uint64_t)>( |
| 192 | &Name::appendVersion), _1, _2), |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 193 | Name("/%FD%00%0FB%40"), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 194 | 1000000, |
| 195 | bind(&name::Component::isVersion, _1))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 196 | dataset.push_back(boost::make_tuple(&name::Component::fromSequenceNumber, |
| 197 | bind(&name::Component::toSequenceNumber, _1), |
| 198 | bind(&Name::appendSequenceNumber, _1, _2), |
| 199 | Name("/%FE%00%98%96%80"), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 200 | 10000000, |
| 201 | bind(&name::Component::isSequenceNumber, _1))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | Dataset dataset; |
| 205 | }; |
| 206 | |
| 207 | class Timestamp |
| 208 | { |
| 209 | public: |
| 210 | typedef std::list<boost::tuple<function<name::Component(const time::system_clock::TimePoint&)>, |
| 211 | function<time::system_clock::TimePoint(const name::Component&)>, |
| 212 | function<Name&(Name&, const time::system_clock::TimePoint&)>, |
| 213 | Name/*expected*/, |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 214 | time::system_clock::TimePoint/*value*/, |
| 215 | function<bool(const name::Component&)> > > Dataset; |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 216 | Timestamp() |
| 217 | { |
| 218 | dataset.push_back(boost::make_tuple(&name::Component::fromTimestamp, |
Alexander Afanasyev | 6f9ec93 | 2014-10-31 10:34:00 -0700 | [diff] [blame] | 219 | ndn::bind(&name::Component::toTimestamp, _1), |
| 220 | ndn::bind(&Name::appendTimestamp, _1, _2), |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 221 | Name("/%FC%00%04%7BE%E3%1B%00%00"), |
Davide Pesavento | dfe9c6b | 2014-08-25 21:17:10 +0200 | [diff] [blame] | 222 | time::getUnixEpoch() + time::days(14600/*40 years*/), |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 223 | bind(&name::Component::isTimestamp, _1))); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | Dataset dataset; |
| 227 | }; |
| 228 | |
| 229 | typedef boost::mpl::vector<Numeric, Timestamp> ConventionsDatasets; |
| 230 | |
| 231 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(NamingConventions, T, ConventionsDatasets, T) |
| 232 | { |
| 233 | // // These octets are obtained by the snippet below. |
| 234 | // // This check is intended to detect unexpected encoding change in the future. |
| 235 | // for (typename T::Dataset::const_iterator it = this->dataset.begin(); |
| 236 | // it != this->dataset.end(); ++it) { |
| 237 | // Name name; |
| 238 | // name.append(it->template get<0>()(it->template get<4>())); |
| 239 | // std::cout << name << std::endl; |
| 240 | // } |
| 241 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 242 | name::Component invalidComponent1; |
| 243 | name::Component invalidComponent2("1234567890"); |
| 244 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 245 | for (typename T::Dataset::const_iterator it = this->dataset.begin(); |
| 246 | it != this->dataset.end(); ++it) { |
| 247 | const Name& expected = it->template get<3>(); |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 248 | BOOST_TEST_MESSAGE("Check " << expected[0].toUri()); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 249 | |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(expected[0].isGeneric(), true); |
| 251 | |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 252 | name::Component actualComponent = it->template get<0>()(it->template get<4>()); |
| 253 | BOOST_CHECK_EQUAL(actualComponent, expected[0]); |
| 254 | |
| 255 | Name actualName; |
| 256 | it->template get<2>()(actualName, it->template get<4>()); |
| 257 | BOOST_CHECK_EQUAL(actualName, expected); |
| 258 | |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 259 | BOOST_CHECK_EQUAL(it->template get<5>()(expected[0]), true); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 260 | BOOST_REQUIRE_NO_THROW(it->template get<1>()(expected[0])); |
| 261 | BOOST_CHECK_EQUAL(it->template get<1>()(expected[0]), it->template get<4>()); |
Alexander Afanasyev | 0f232c5 | 2014-10-23 13:07:31 -0700 | [diff] [blame] | 262 | |
| 263 | BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent1), false); |
| 264 | BOOST_CHECK_EQUAL(it->template get<5>()(invalidComponent2), false); |
| 265 | |
| 266 | BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent1), name::Component::Error); |
| 267 | BOOST_REQUIRE_THROW(it->template get<1>()(invalidComponent2), name::Component::Error); |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
Shuo Chen | 5aa8c74 | 2014-06-22 15:00:02 +0800 | [diff] [blame] | 271 | BOOST_AUTO_TEST_CASE(GetSuccessor) |
| 272 | { |
| 273 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%02").getSuccessor(), Name("ndn:/%00%01/%01%03")); |
| 274 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%01%FF").getSuccessor(), Name("ndn:/%00%01/%02%00")); |
| 275 | BOOST_CHECK_EQUAL(Name("ndn:/%00%01/%FF%FF").getSuccessor(), Name("ndn:/%00%01/%00%00%00")); |
| 276 | BOOST_CHECK_EQUAL(Name().getSuccessor(), Name("ndn:/%00")); |
| 277 | } |
| 278 | |
Junxiao Shi | 937e461 | 2014-10-22 15:39:07 -0700 | [diff] [blame] | 279 | BOOST_AUTO_TEST_CASE(Markers) |
| 280 | { |
| 281 | Name name; |
| 282 | uint64_t number; |
| 283 | |
| 284 | BOOST_REQUIRE_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment()); |
| 285 | BOOST_CHECK_EQUAL(number, 30923); |
| 286 | |
| 287 | BOOST_REQUIRE_NO_THROW(number = name.appendSegmentOffset(589).at(-1).toSegmentOffset()); |
| 288 | BOOST_CHECK_EQUAL(number, 589); |
| 289 | |
| 290 | BOOST_REQUIRE_NO_THROW(number = name.appendVersion().at(-1).toVersion()); |
| 291 | |
| 292 | BOOST_REQUIRE_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion()); |
| 293 | BOOST_CHECK_EQUAL(number, 25912); |
| 294 | |
| 295 | const time::system_clock::TimePoint tp = time::system_clock::now(); |
| 296 | time::system_clock::TimePoint tp2; |
| 297 | BOOST_REQUIRE_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp()); |
| 298 | BOOST_CHECK_LE(std::abs(time::duration_cast<time::microseconds>(tp2 - tp).count()), 1); |
| 299 | |
| 300 | BOOST_REQUIRE_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber()); |
| 301 | BOOST_CHECK_EQUAL(number, 11676); |
| 302 | } |
| 303 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 304 | BOOST_AUTO_TEST_CASE(UnorderedMap) |
| 305 | { |
| 306 | std::unordered_map<Name, int> map; |
| 307 | Name name1("/1"); |
| 308 | Name name2("/2"); |
| 309 | Name name3("/3"); |
| 310 | map[name1] = 1; |
| 311 | map[name2] = 2; |
| 312 | map[name3] = 3; |
| 313 | |
| 314 | BOOST_CHECK_EQUAL(map[name1], 1); |
| 315 | BOOST_CHECK_EQUAL(map[name2], 2); |
| 316 | BOOST_CHECK_EQUAL(map[name3], 3); |
| 317 | } |
| 318 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 319 | BOOST_AUTO_TEST_CASE(ImplicitSha256Digest) |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 320 | { |
| 321 | Name n; |
| 322 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 323 | static const uint8_t DIGEST[] = {0x28, 0xba, 0xd4, 0xb5, 0x27, 0x5b, 0xd3, 0x92, |
| 324 | 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6, 0x6f, |
| 325 | 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55, |
| 326 | 0xc0, 0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48, |
| 327 | 0x00, 0x00}; |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 328 | |
| 329 | BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(DIGEST, 32)); |
| 330 | BOOST_REQUIRE_NO_THROW(n.appendImplicitSha256Digest(make_shared<Buffer>(DIGEST, 32))); |
| 331 | BOOST_CHECK_EQUAL(n.get(0), n.get(1)); |
| 332 | |
| 333 | BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 34), name::Component::Error); |
| 334 | BOOST_REQUIRE_THROW(n.appendImplicitSha256Digest(DIGEST, 30), name::Component::Error); |
| 335 | |
| 336 | n.append(DIGEST, 32); |
| 337 | BOOST_CHECK_LT(n.get(0), n.get(2)); |
| 338 | BOOST_CHECK_EQUAL_COLLECTIONS(n.get(0).value_begin(), n.get(0).value_end(), |
| 339 | n.get(2).value_begin(), n.get(2).value_end()); |
| 340 | |
| 341 | n.append(DIGEST + 1, 32); |
| 342 | BOOST_CHECK_LT(n.get(0), n.get(3)); |
| 343 | |
| 344 | n.append(DIGEST + 2, 32); |
| 345 | BOOST_CHECK_LT(n.get(0), n.get(4)); |
| 346 | |
| 347 | BOOST_CHECK_EQUAL(n.get(0).toUri(), "sha256digest=" |
| 348 | "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548"); |
| 349 | |
| 350 | BOOST_CHECK_EQUAL(n.get(0).isImplicitSha256Digest(), true); |
| 351 | BOOST_CHECK_EQUAL(n.get(2).isImplicitSha256Digest(), false); |
| 352 | |
| 353 | BOOST_CHECK_THROW(Name("/hello/sha256digest=hmm"), name::Component::Error); |
| 354 | |
| 355 | Name n2; |
| 356 | // check canonical URI encoding (lower case) |
| 357 | BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest=" |
| 358 | "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548")); |
| 359 | BOOST_CHECK_EQUAL(n.get(0), n2.get(1)); |
| 360 | |
| 361 | // will accept hex value in upper case too |
| 362 | BOOST_CHECK_NO_THROW(n2 = Name("/hello/sha256digest=" |
| 363 | "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548")); |
| 364 | BOOST_CHECK_EQUAL(n.get(0), n2.get(1)); |
| 365 | |
| 366 | // this is not valid sha256digest component, will be treated as generic component |
| 367 | BOOST_CHECK_NO_THROW(n2 = Name("/hello/SHA256DIGEST=" |
| 368 | "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548")); |
| 369 | BOOST_CHECK_NE(n.get(0), n2.get(1)); |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 370 | BOOST_CHECK(n2.get(1).isGeneric()); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 373 | BOOST_AUTO_TEST_CASE(Compare) |
| 374 | { |
Marcin Juszkiewicz | 1ddc13d | 2015-11-16 10:20:44 +0100 | [diff] [blame] | 375 | BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0); |
| 376 | BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0); |
| 377 | BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0); |
| 378 | BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0); |
| 379 | BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0); |
| 380 | BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0); |
| 381 | BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0); |
| 382 | BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 383 | |
Marcin Juszkiewicz | 1ddc13d | 2015-11-16 10:20:44 +0100 | [diff] [blame] | 384 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0); |
| 385 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0); |
| 386 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0); |
| 387 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0); |
| 388 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0); |
| 389 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0); |
| 390 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0); |
| 391 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 392 | |
Marcin Juszkiewicz | 1ddc13d | 2015-11-16 10:20:44 +0100 | [diff] [blame] | 393 | BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0); |
| 394 | BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0); |
| 395 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0); |
| 396 | BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0); |
| 397 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0); |
| 398 | BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0); |
| 399 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0); |
| 400 | BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 401 | |
Marcin Juszkiewicz | 1ddc13d | 2015-11-16 10:20:44 +0100 | [diff] [blame] | 402 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 403 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 404 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0); |
| 405 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 406 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0); |
| 407 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 408 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0); |
| 409 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 410 | |
Marcin Juszkiewicz | 1ddc13d | 2015-11-16 10:20:44 +0100 | [diff] [blame] | 411 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 412 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 413 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0); |
| 414 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 415 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0); |
| 416 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 417 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0); |
| 418 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0); |
Junxiao Shi | a6452ac | 2015-01-23 11:21:06 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 421 | BOOST_AUTO_TEST_CASE(NameWithSpaces) |
| 422 | { |
| 423 | Name name("/ hello\t/\tworld \r\n"); |
| 424 | |
| 425 | BOOST_CHECK_EQUAL("/hello/world", name); |
| 426 | BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error); |
| 427 | } |
| 428 | |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 429 | BOOST_AUTO_TEST_CASE(Append) |
| 430 | { |
| 431 | PartialName toAppend("/and"); |
| 432 | PartialName toAppend1("/beyond"); |
| 433 | { |
| 434 | Name name("/hello/world"); |
| 435 | BOOST_CHECK_EQUAL("/hello/world/hello/world", name.append(name)); |
| 436 | BOOST_CHECK_EQUAL("/hello/world/hello/world", name); |
| 437 | } |
| 438 | { |
| 439 | Name name("/hello/world"); |
| 440 | BOOST_CHECK_EQUAL("/hello/world/and", name.append(toAppend)); |
| 441 | } |
| 442 | { |
| 443 | Name name("/hello/world"); |
| 444 | BOOST_CHECK_EQUAL("/hello/world/and/beyond", name.append(toAppend).append(toAppend1)); |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | BOOST_AUTO_TEST_CASE(SubName) |
| 449 | { |
| 450 | Name name("/hello/world"); |
| 451 | |
| 452 | BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0)); |
| 453 | BOOST_CHECK_EQUAL("/world", name.getSubName(1)); |
| 454 | BOOST_CHECK_EQUAL("/hello/", name.getSubName(0, 1)); |
| 455 | } |
| 456 | |
| 457 | BOOST_AUTO_TEST_CASE(SubNameNegativeIndex) |
| 458 | { |
| 459 | Name name("/first/second/third/last"); |
| 460 | |
| 461 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1)); |
| 462 | BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2)); |
| 463 | BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1)); |
| 464 | } |
| 465 | |
| 466 | BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes) |
| 467 | { |
| 468 | Name name("/first/second/last"); |
| 469 | // No length |
| 470 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10)); |
| 471 | BOOST_CHECK_EQUAL("/", name.getSubName(10)); |
| 472 | |
| 473 | // Starting after the max position |
| 474 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 1)); |
| 475 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 10)); |
| 476 | |
| 477 | // Not enough components |
| 478 | BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10)); |
| 479 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10)); |
| 480 | |
| 481 | // Start before first |
| 482 | BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2)); |
| 483 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10)); |
| 484 | } |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 485 | |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 486 | BOOST_AUTO_TEST_CASE(DeepCopy) |
| 487 | { |
| 488 | Name n1("/hello/world"); |
| 489 | Name n2 = n1.deepCopy(); |
| 490 | |
| 491 | BOOST_CHECK_EQUAL(n1, n2); |
| 492 | BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode()); |
| 493 | |
| 494 | EncodingBuffer buffer(1024, 0); |
| 495 | n1.wireEncode(buffer); |
| 496 | Name n3(buffer.block()); |
| 497 | |
| 498 | BOOST_CHECK_EQUAL(n1, n3); |
| 499 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024); |
| 500 | n3 = n3.deepCopy(); |
| 501 | |
| 502 | BOOST_CHECK_LT(n3.wireEncode().size(), 1024); |
| 503 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size()); |
| 504 | } |
| 505 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame^] | 506 | BOOST_AUTO_TEST_SUITE_END() // TestName |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 507 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 508 | } // namespace tests |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 509 | } // namespace ndn |