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