Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | fbea4fc | 2022-02-08 07:26:04 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/name.hpp" |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 23 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
| 25 | |
Yingdi Yu | 90e2358 | 2014-11-06 14:21:04 -0800 | [diff] [blame] | 26 | #include <unordered_map> |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 28 | namespace ndn { |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 29 | namespace tests { |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 30 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 31 | using Component = name::Component; |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 32 | using UriFormat = name::UriFormat; |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 33 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 34 | BOOST_AUTO_TEST_SUITE(TestName) |
| 35 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 36 | // ---- encoding, decoding, and URI ---- |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 37 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 38 | BOOST_AUTO_TEST_CASE(EncodeDecode) |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 39 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 40 | std::string uri = "/Emid/25042=P3/.../..../%1C%9F/" |
| 41 | "sha256digest=0415e3624a151850ac686c84f155f29808c0dd73819aa4a4c20be73a4d8a874c"; |
| 42 | Name name(uri); |
| 43 | BOOST_CHECK_EQUAL(name.size(), 6); |
| 44 | BOOST_CHECK_EQUAL(name[0], Component("Emid")); |
| 45 | BOOST_CHECK_EQUAL(name[1], Component("FD61D2025033"_block)); |
| 46 | BOOST_CHECK_EQUAL(name[2], Component("")); |
| 47 | BOOST_CHECK_EQUAL(name[3], Component(".")); |
| 48 | BOOST_CHECK_EQUAL(name[4], Component("\x1C\x9F")); |
| 49 | BOOST_CHECK(name[5].isImplicitSha256Digest()); |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 50 | |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(name.toUri(UriFormat::CANONICAL), |
| 52 | "/8=Emid/25042=P3/8=.../8=..../8=%1C%9F/" |
| 53 | "1=%04%15%E3bJ%15%18P%AChl%84%F1U%F2%98%08%C0%DDs%81%9A%A4%A4%C2%0B%E7%3AM%8A%87L"); |
| 54 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 55 | Block wire = name.wireEncode(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(wire, |
| 57 | "0737 0804456D6964 FD61D2025033 0800 08012E 08021C9F " |
| 58 | "01200415E3624A151850AC686C84F155F29808C0DD73819AA4A4C20BE73A4D8A874C"_block); |
Alexander Afanasyev | c234429 | 2014-03-02 00:08:00 +0000 | [diff] [blame] | 59 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 60 | Name decoded(wire); |
| 61 | BOOST_CHECK_EQUAL(decoded, name); |
Alexander Afanasyev | 6486d52 | 2014-10-23 14:14:11 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 64 | BOOST_AUTO_TEST_CASE(ParseUri) |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 65 | { |
Junxiao Shi | a39c0b5 | 2019-12-31 15:13:15 -0700 | [diff] [blame] | 66 | // canonical URI |
| 67 | BOOST_CHECK_EQUAL(Name("/8=hello/8=world").toUri(), "/hello/world"); |
| 68 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 69 | // URI with correct scheme |
| 70 | BOOST_CHECK_EQUAL(Name("ndn:/hello/world").toUri(), "/hello/world"); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 72 | // URI with incorrect scheme: auto-corrected |
| 73 | BOOST_CHECK_EQUAL(Name("ncc:/hello/world").toUri(), "/hello/world"); |
| 74 | |
| 75 | // URI with authority: authority ignored |
| 76 | BOOST_CHECK_EQUAL(Name("//authority/hello/world").toUri(), "/hello/world"); |
| 77 | BOOST_CHECK_EQUAL(Name("ndn://authority/hello/world").toUri(), "/hello/world"); |
| 78 | |
| 79 | // URI containing unescaped characters: auto-corrected |
| 80 | BOOST_CHECK_EQUAL(Name("/ hello\t/\tworld \r\n").toUri(), "/%20hello%09/%09world%20%0D%0A"); |
| 81 | BOOST_CHECK_EQUAL(Name("/hello/world/ ").toUri(), "/hello/world/%20%20"); |
| 82 | BOOST_CHECK_EQUAL(Name("/:?#[]@").toUri(), "/%3A%3F%23%5B%5D%40"); |
| 83 | |
| 84 | // URI not starting with '/': accepted as PartialName |
| 85 | BOOST_CHECK_EQUAL(Name("").toUri(), "/"); |
| 86 | BOOST_CHECK_EQUAL(Name(" ").toUri(), "/%20"); |
| 87 | BOOST_CHECK_EQUAL(Name(" /hello/world").toUri(), "/%20%20/hello/world"); |
| 88 | BOOST_CHECK_EQUAL(Name("hello/world").toUri(), "/hello/world"); |
| 89 | |
| 90 | // URI ending with '/': auto-corrected |
| 91 | BOOST_CHECK_EQUAL(Name("/hello/world/").toUri(), "/hello/world"); |
| 92 | |
| 93 | // URI containing bad component: rejected |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 94 | BOOST_CHECK_THROW(Name("/hello//world"), name::Component::Error); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 95 | BOOST_CHECK_THROW(Name("/hello/./world"), name::Component::Error); |
| 96 | BOOST_CHECK_THROW(Name("/hello/../world"), name::Component::Error); |
Alexander Afanasyev | d7eacc7 | 2015-04-03 13:06:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 99 | BOOST_AUTO_TEST_CASE(DeepCopy) |
| 100 | { |
| 101 | Name n1("/hello/world"); |
| 102 | Name n2 = n1.deepCopy(); |
| 103 | |
| 104 | BOOST_CHECK_EQUAL(n1, n2); |
| 105 | BOOST_CHECK_NE(&n1.wireEncode(), &n2.wireEncode()); |
| 106 | |
| 107 | EncodingBuffer buffer(1024, 0); |
| 108 | n1.wireEncode(buffer); |
| 109 | Name n3(buffer.block()); |
| 110 | |
| 111 | BOOST_CHECK_EQUAL(n1, n3); |
| 112 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), 1024); |
| 113 | n3 = n3.deepCopy(); |
| 114 | |
| 115 | BOOST_CHECK_LT(n3.wireEncode().size(), 1024); |
| 116 | BOOST_CHECK_EQUAL(n3.wireEncode().getBuffer()->size(), n3.wireEncode().size()); |
| 117 | } |
| 118 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 119 | // ---- access ---- |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(At) |
| 122 | { |
| 123 | Name name("/hello/5=NDN"); |
| 124 | |
| 125 | BOOST_CHECK_EQUAL(name.at(0), name::Component("080568656C6C6F"_block)); |
| 126 | BOOST_CHECK_EQUAL(name.at(1), name::Component("05034E444E"_block)); |
| 127 | BOOST_CHECK_EQUAL(name.at(-1), name::Component("05034E444E"_block)); |
| 128 | BOOST_CHECK_EQUAL(name.at(-2), name::Component("080568656C6C6F"_block)); |
| 129 | |
| 130 | BOOST_CHECK_THROW(name.at(2), Name::Error); |
| 131 | BOOST_CHECK_THROW(name.at(-3), Name::Error); |
| 132 | } |
| 133 | |
| 134 | BOOST_AUTO_TEST_CASE(SubName) |
| 135 | { |
| 136 | Name name("/hello/world"); |
| 137 | |
| 138 | BOOST_CHECK_EQUAL("/hello/world", name.getSubName(0)); |
| 139 | BOOST_CHECK_EQUAL("/world", name.getSubName(1)); |
Junxiao Shi | 9c9672e | 2018-04-18 12:55:08 +0000 | [diff] [blame] | 140 | BOOST_CHECK_EQUAL("/hello", name.getSubName(0, 1)); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(SubNameNegativeIndex) |
| 144 | { |
| 145 | Name name("/first/second/third/last"); |
| 146 | |
| 147 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1)); |
| 148 | BOOST_CHECK_EQUAL("/third/last", name.getSubName(-2)); |
| 149 | BOOST_CHECK_EQUAL("/second", name.getSubName(-3, 1)); |
| 150 | } |
| 151 | |
| 152 | BOOST_AUTO_TEST_CASE(SubNameOutOfRangeIndexes) |
| 153 | { |
| 154 | Name name("/first/second/last"); |
| 155 | // No length |
| 156 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10)); |
| 157 | BOOST_CHECK_EQUAL("/", name.getSubName(10)); |
| 158 | |
| 159 | // Starting after the max position |
| 160 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 1)); |
| 161 | BOOST_CHECK_EQUAL("/", name.getSubName(10, 10)); |
| 162 | |
| 163 | // Not enough components |
| 164 | BOOST_CHECK_EQUAL("/second/last", name.getSubName(1, 10)); |
| 165 | BOOST_CHECK_EQUAL("/last", name.getSubName(-1, 10)); |
| 166 | |
| 167 | // Start before first |
| 168 | BOOST_CHECK_EQUAL("/first/second", name.getSubName(-10, 2)); |
| 169 | BOOST_CHECK_EQUAL("/first/second/last", name.getSubName(-10, 10)); |
| 170 | } |
| 171 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 172 | // ---- iterators ---- |
| 173 | |
| 174 | BOOST_AUTO_TEST_CASE(ForwardIterator) |
| 175 | { |
| 176 | name::Component comps[] { |
| 177 | name::Component("A"), |
| 178 | name::Component("B"), |
| 179 | name::Component("C"), |
| 180 | name::Component("D") |
| 181 | }; |
| 182 | |
| 183 | Name n0; |
| 184 | BOOST_CHECK_EQUAL_COLLECTIONS(n0.begin(), n0.end(), comps, comps + 0); |
| 185 | |
| 186 | Name n4("/A/B/C/D"); |
| 187 | BOOST_CHECK_EQUAL_COLLECTIONS(n4.begin(), n4.end(), comps, comps + 4); |
| 188 | } |
| 189 | |
| 190 | BOOST_AUTO_TEST_CASE(ReverseIterator) |
| 191 | { |
| 192 | name::Component comps[] { |
| 193 | name::Component("D"), |
| 194 | name::Component("C"), |
| 195 | name::Component("B"), |
| 196 | name::Component("A") |
| 197 | }; |
| 198 | |
| 199 | Name n0; |
| 200 | BOOST_CHECK_EQUAL_COLLECTIONS(n0.rbegin(), n0.rend(), comps, comps + 0); |
| 201 | |
| 202 | Name n4("/A/B/C/D"); |
| 203 | BOOST_CHECK_EQUAL_COLLECTIONS(n4.rbegin(), n4.rend(), comps, comps + 4); |
| 204 | } |
| 205 | |
| 206 | // ---- modifiers ---- |
| 207 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 208 | BOOST_AUTO_TEST_CASE(SetComponent) |
| 209 | { |
| 210 | Name name("/A/B"); |
| 211 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080142"_block); |
| 212 | BOOST_CHECK_EQUAL(name.hasWire(), true); |
| 213 | |
| 214 | // pass by const lvalue ref |
| 215 | const Component c("C"); |
| 216 | name.set(0, c); |
| 217 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 218 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080142"_block); |
| 219 | |
| 220 | // pass by rvalue ref |
| 221 | Component d("D"); |
| 222 | name.set(1, std::move(d)); |
| 223 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 224 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080144"_block); |
| 225 | |
| 226 | // negative index |
| 227 | name.set(-1, Component("E")); |
| 228 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080143 080145"_block); |
| 229 | } |
| 230 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 231 | BOOST_AUTO_TEST_CASE(AppendComponent) |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 232 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 233 | Name name; |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Ashlesh Gawande | 7dffab6 | 2017-12-21 16:40:53 -0600 | [diff] [blame] | 235 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 236 | name.append(Component("Emid")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 237 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 0804456D6964"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 238 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 239 | name.append(25042, {'P', '3'}); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 240 | BOOST_CHECK_EQUAL(name.wireEncode(), "070C 0804456D6964 FD61D2025033"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 241 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 242 | name.append(Component(make_span<uint8_t>({'.'}))); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 243 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 0804456D6964 FD61D2025033 08012E"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 244 | |
Davide Pesavento | df8fd8a | 2022-02-21 20:04:21 -0500 | [diff] [blame] | 245 | const std::vector<uint8_t> v1{0x28, 0xF0, 0xA3, 0x6B}; |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 246 | name.append(16, v1.begin(), v1.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 247 | BOOST_CHECK_EQUAL(name.wireEncode(), "0715 0804456D6964 FD61D2025033 08012E 100428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 248 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 249 | name.clear(); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 250 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 251 | |
| 252 | name.append(v1.begin(), v1.end()); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080428F0A36B"_block); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 254 | |
| 255 | name.append("xKh"); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 256 | BOOST_CHECK_EQUAL(name.wireEncode(), "070B 080428F0A36B 0803784B68"_block); |
Joao Pereira | 6f7cfd0 | 2015-06-15 11:36:26 -0400 | [diff] [blame] | 257 | } |
| 258 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 259 | BOOST_AUTO_TEST_CASE(AppendPartialName) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 260 | { |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 261 | Name name("/A/B"); |
| 262 | name.append(PartialName("/6=C/D")) |
| 263 | .append(PartialName("/E")); |
Junxiao Shi | 72c0c64 | 2018-04-20 15:41:09 +0000 | [diff] [blame] | 264 | BOOST_CHECK_EQUAL(name.wireEncode(), "070F 080141 080142 060143 080144 080145"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 265 | |
| 266 | name = "/A/B"; |
| 267 | name.append(name); |
| 268 | BOOST_CHECK_EQUAL(name.wireEncode(), "070C 080141 080142 080141 080142"_block); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | BOOST_AUTO_TEST_CASE(AppendNumber) |
| 272 | { |
| 273 | Name name; |
| 274 | for (uint32_t i = 0; i < 10; i++) { |
| 275 | name.appendNumber(i); |
| 276 | } |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 277 | BOOST_CHECK_EQUAL(name.size(), 10); |
| 278 | |
| 279 | for (uint32_t i = 0; i < 10; i++) { |
| 280 | BOOST_CHECK_EQUAL(name[i].toNumber(), i); |
| 281 | } |
| 282 | } |
| 283 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 284 | BOOST_AUTO_TEST_CASE(AppendParametersSha256Digest) |
| 285 | { |
| 286 | auto digest = make_shared<Buffer>(32); |
| 287 | |
| 288 | Name name("/P"); |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 289 | name.appendParametersSha256Digest(digest); // ConstBufferPtr overload |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 290 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 291 | "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block); |
| 292 | |
| 293 | name = "/P"; |
Davide Pesavento | 765abc9 | 2021-12-27 00:44:04 -0500 | [diff] [blame] | 294 | name.appendParametersSha256Digest(*digest); // span overload |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 295 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 296 | "0725 080150 02200000000000000000000000000000000000000000000000000000000000000000"_block); |
| 297 | |
| 298 | name = "/P"; |
| 299 | name.appendParametersSha256DigestPlaceholder(); |
| 300 | BOOST_CHECK_EQUAL(name.wireEncode(), |
| 301 | "0725 080150 0220E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855"_block); |
| 302 | } |
| 303 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 304 | BOOST_AUTO_TEST_CASE(AppendTypedComponent) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 305 | { |
| 306 | // TestNameComponent/NamingConvention provides additional coverage for these methods, |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 307 | // including verification of the wire format. |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 308 | |
| 309 | Name name; |
| 310 | uint64_t number; |
| 311 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 312 | BOOST_CHECK_NO_THROW(number = name.appendSegment(30923).at(-1).toSegment()); |
| 313 | BOOST_TEST(number == 30923); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 314 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 315 | BOOST_CHECK_NO_THROW(number = name.appendByteOffset(41880).at(-1).toByteOffset()); |
| 316 | BOOST_TEST(number == 41880); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 317 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 318 | auto before = time::toUnixTimestamp(time::system_clock::now()); |
| 319 | BOOST_CHECK_NO_THROW(number = name.appendVersion().at(-1).toVersion()); |
| 320 | auto after = time::toUnixTimestamp(time::system_clock::now()); |
| 321 | BOOST_TEST(number >= before.count()); |
| 322 | BOOST_TEST(number <= after.count()); |
| 323 | |
| 324 | BOOST_CHECK_NO_THROW(number = name.appendVersion(25912).at(-1).toVersion()); |
| 325 | BOOST_TEST(number == 25912); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 326 | |
Davide Pesavento | 46cdf90 | 2020-06-08 16:38:23 -0400 | [diff] [blame] | 327 | const auto tp = time::system_clock::now(); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 328 | time::system_clock::TimePoint tp2; |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 329 | BOOST_CHECK_NO_THROW(tp2 = name.appendTimestamp(tp).at(-1).toTimestamp()); |
| 330 | BOOST_TEST(time::abs(tp2 - tp) <= 1_us); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 331 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 332 | BOOST_CHECK_NO_THROW(number = name.appendSequenceNumber(11676).at(-1).toSequenceNumber()); |
| 333 | BOOST_TEST(number == 11676); |
| 334 | |
| 335 | name.appendKeyword({0xab, 0xcd, 0xef}); |
| 336 | BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=%AB%CD%EF")); |
| 337 | |
| 338 | name.appendKeyword("test-keyword"); |
| 339 | BOOST_TEST(name.at(-1) == Component::fromEscapedString("32=test-keyword")); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 342 | BOOST_AUTO_TEST_CASE(EraseComponent) |
| 343 | { |
| 344 | Name name("/A/B/C"); |
| 345 | BOOST_CHECK_EQUAL(name.wireEncode(), "0709 080141 080142 080143"_block); |
| 346 | BOOST_CHECK_EQUAL(name.hasWire(), true); |
| 347 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 348 | name.erase(-2); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 349 | BOOST_CHECK_EQUAL(name.size(), 2); |
| 350 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 351 | BOOST_CHECK_EQUAL(name.wireEncode(), "0706 080141 080143"_block); |
| 352 | |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 353 | name.erase(1); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 354 | BOOST_CHECK_EQUAL(name.size(), 1); |
| 355 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 356 | BOOST_CHECK_EQUAL(name.wireEncode(), "0703 080141"_block); |
Davide Pesavento | 21ee899 | 2022-02-25 20:46:04 -0500 | [diff] [blame] | 357 | |
| 358 | name.erase(0); |
| 359 | BOOST_CHECK_EQUAL(name.size(), 0); |
| 360 | BOOST_CHECK_EQUAL(name.hasWire(), false); |
| 361 | BOOST_CHECK_EQUAL(name.wireEncode(), "0700"_block); |
Davide Pesavento | adc9aa2 | 2019-06-30 19:00:20 -0400 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | BOOST_AUTO_TEST_CASE(Clear) |
| 365 | { |
| 366 | Name name("/A/B/C"); |
| 367 | BOOST_CHECK_EQUAL(name.empty(), false); |
| 368 | name.clear(); |
| 369 | BOOST_CHECK_EQUAL(name.empty(), true); |
| 370 | BOOST_CHECK(name.begin() == name.end()); |
| 371 | } |
| 372 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 373 | // ---- algorithms ---- |
| 374 | |
| 375 | BOOST_AUTO_TEST_CASE(GetSuccessor) |
| 376 | { |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 377 | BOOST_CHECK_EQUAL(Name().getSuccessor(), "/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"); |
| 378 | BOOST_CHECK_EQUAL(Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(), |
| 379 | "/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"); |
| 380 | BOOST_CHECK_EQUAL(Name("/sha256digest=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 381 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"); |
| 382 | BOOST_CHECK_EQUAL(Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000").getSuccessor(), |
| 383 | "/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"); |
| 384 | BOOST_CHECK_EQUAL(Name("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").getSuccessor(), |
| 385 | "/3=..."); |
Junxiao Shi | cf0aff8 | 2018-07-23 06:42:13 -0600 | [diff] [blame] | 386 | BOOST_CHECK_EQUAL(Name("/P/A").getSuccessor(), "/P/B"); |
| 387 | BOOST_CHECK_EQUAL(Name("/P/AAA").getSuccessor(), "/P/AAB"); |
| 388 | BOOST_CHECK_EQUAL(Name("/Q/...").getSuccessor(), "/Q/%00"); |
| 389 | BOOST_CHECK_EQUAL(Name("/Q/%FF").getSuccessor(), "/Q/%00%00"); |
| 390 | BOOST_CHECK_EQUAL(Name("/Q/%FE%FF").getSuccessor(), "/Q/%FF%00"); |
| 391 | BOOST_CHECK_EQUAL(Name("/Q/%FF%FF").getSuccessor(), "/Q/%00%00%00"); |
| 392 | BOOST_CHECK_EQUAL(Name("/P/3=A").getSuccessor(), "/P/3=B"); |
| 393 | BOOST_CHECK_EQUAL(Name("/P/3=AAA").getSuccessor(), "/P/3=AAB"); |
| 394 | BOOST_CHECK_EQUAL(Name("/Q/3=...").getSuccessor(), "/Q/3=%00"); |
| 395 | BOOST_CHECK_EQUAL(Name("/Q/3=%FF").getSuccessor(), "/Q/3=%00%00"); |
| 396 | BOOST_CHECK_EQUAL(Name("/Q/3=%FE%FF").getSuccessor(), "/Q/3=%FF%00"); |
| 397 | BOOST_CHECK_EQUAL(Name("/Q/3=%FF%FF").getSuccessor(), "/Q/3=%00%00%00"); |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 400 | BOOST_AUTO_TEST_CASE(IsPrefixOf) |
| 401 | { |
| 402 | BOOST_CHECK(Name("/").isPrefixOf("/")); |
| 403 | BOOST_CHECK(Name("/").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 404 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 405 | BOOST_CHECK(Name("/").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 406 | BOOST_CHECK(Name("/").isPrefixOf("/F")); |
| 407 | BOOST_CHECK(Name("/").isPrefixOf("/21426=AA")); |
| 408 | |
| 409 | BOOST_CHECK(Name("/B").isPrefixOf("/B")); |
| 410 | BOOST_CHECK(Name("/B").isPrefixOf("/B/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 411 | BOOST_CHECK(Name("/").isPrefixOf("/B/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 412 | BOOST_CHECK(Name("/B").isPrefixOf("/B/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 413 | BOOST_CHECK(Name("/B").isPrefixOf("/B/F")); |
| 414 | BOOST_CHECK(Name("/B").isPrefixOf("/B/21426=AA")); |
| 415 | |
| 416 | BOOST_CHECK(!Name("/C").isPrefixOf("/")); |
| 417 | BOOST_CHECK(!Name("/C").isPrefixOf("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000")); |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 418 | BOOST_CHECK(Name("/").isPrefixOf("/params-sha256=ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); |
| 419 | BOOST_CHECK(!Name("/C").isPrefixOf("/3=D")); |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 420 | BOOST_CHECK(!Name("/C").isPrefixOf("/F")); |
| 421 | BOOST_CHECK(!Name("/C").isPrefixOf("/21426=AA")); |
| 422 | } |
| 423 | |
| 424 | BOOST_AUTO_TEST_CASE(CompareOp) |
| 425 | { |
| 426 | std::vector<Name> names = { |
| 427 | Name("/"), |
| 428 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 429 | Name("/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 430 | Name("/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 431 | Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 432 | Name("/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"), |
| 433 | Name("/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 434 | Name("/3=..."), |
| 435 | Name("/3=D"), |
| 436 | Name("/3=F"), |
| 437 | Name("/3=AA"), |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 438 | Name("/..."), |
| 439 | Name("/D"), |
| 440 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000000"), |
| 441 | Name("/D/sha256digest=0000000000000000000000000000000000000000000000000000000000000001"), |
| 442 | Name("/D/sha256digest=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
Junxiao Shi | 4053bd5 | 2018-08-16 13:39:25 -0600 | [diff] [blame] | 443 | Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000000"), |
| 444 | Name("/D/params-sha256=0000000000000000000000000000000000000000000000000000000000000001"), |
| 445 | Name("/D/params-sha256=FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"), |
| 446 | Name("/D/3=..."), |
| 447 | Name("/D/3=D"), |
| 448 | Name("/D/3=F"), |
| 449 | Name("/D/3=AA"), |
Junxiao Shi | a36f15d | 2018-04-04 02:22:11 +0000 | [diff] [blame] | 450 | Name("/D/..."), |
| 451 | Name("/D/D"), |
| 452 | Name("/D/F"), |
| 453 | Name("/D/AA"), |
| 454 | Name("/D/21426=..."), |
| 455 | Name("/D/21426=D"), |
| 456 | Name("/D/21426=F"), |
| 457 | Name("/D/21426=AA"), |
| 458 | Name("/F"), |
| 459 | Name("/AA"), |
| 460 | Name("/21426=..."), |
| 461 | Name("/21426=D"), |
| 462 | Name("/21426=F"), |
| 463 | Name("/21426=AA"), |
| 464 | }; |
| 465 | |
| 466 | for (size_t i = 0; i < names.size(); ++i) { |
| 467 | for (size_t j = 0; j < names.size(); ++j) { |
| 468 | Name lhs = names[i]; |
| 469 | Name rhs = names[j]; |
| 470 | BOOST_CHECK_EQUAL(lhs == rhs, i == j); |
| 471 | BOOST_CHECK_EQUAL(lhs != rhs, i != j); |
| 472 | BOOST_CHECK_EQUAL(lhs < rhs, i < j); |
| 473 | BOOST_CHECK_EQUAL(lhs <= rhs, i <= j); |
| 474 | BOOST_CHECK_EQUAL(lhs > rhs, i > j); |
| 475 | BOOST_CHECK_EQUAL(lhs >= rhs, i >= j); |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | BOOST_AUTO_TEST_CASE(CompareFunc) |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 481 | { |
| 482 | BOOST_CHECK_EQUAL(Name("/A") .compare(Name("/A")), 0); |
| 483 | BOOST_CHECK_LT (Name("/A") .compare(Name("/B")), 0); |
| 484 | BOOST_CHECK_GT (Name("/B") .compare(Name("/A")), 0); |
| 485 | BOOST_CHECK_LT (Name("/A") .compare(Name("/AA")), 0); |
| 486 | BOOST_CHECK_GT (Name("/AA") .compare(Name("/A")), 0); |
| 487 | BOOST_CHECK_LT (Name("/A") .compare(Name("/A/C")), 0); |
| 488 | BOOST_CHECK_GT (Name("/A/C").compare(Name("/A")), 0); |
| 489 | |
| 490 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/A")), 0); |
| 491 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/B")), 0); |
| 492 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/A")), 0); |
| 493 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/AA")), 0); |
| 494 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/A")), 0); |
| 495 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/A/C")), 0); |
| 496 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/A")), 0); |
| 497 | |
| 498 | BOOST_CHECK_EQUAL(Name("/Z/A") .compare(1, Name::npos, Name("/A")), 0); |
| 499 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/B")), 0); |
| 500 | BOOST_CHECK_GT (Name("/Z/B") .compare(1, Name::npos, Name("/A")), 0); |
| 501 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/AA")), 0); |
| 502 | BOOST_CHECK_GT (Name("/Z/AA") .compare(1, Name::npos, Name("/A")), 0); |
| 503 | BOOST_CHECK_LT (Name("/Z/A") .compare(1, Name::npos, Name("/A/C")), 0); |
| 504 | BOOST_CHECK_GT (Name("/Z/A/C").compare(1, Name::npos, Name("/A")), 0); |
| 505 | |
| 506 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 507 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B/W"), 1, 1), 0); |
| 508 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 509 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA/W"), 1, 1), 0); |
| 510 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A/W"), 1, 1), 0); |
| 511 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C/W"), 1, 2), 0); |
| 512 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A/W"), 1, 1), 0); |
| 513 | |
| 514 | BOOST_CHECK_EQUAL(Name("/Z/A/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 515 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/B"), 1), 0); |
| 516 | BOOST_CHECK_GT (Name("/Z/B/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 517 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/AA"), 1), 0); |
| 518 | BOOST_CHECK_GT (Name("/Z/AA/Y") .compare(1, 1, Name("/X/A"), 1), 0); |
| 519 | BOOST_CHECK_LT (Name("/Z/A/Y") .compare(1, 1, Name("/X/A/C"), 1), 0); |
| 520 | BOOST_CHECK_GT (Name("/Z/A/C/Y").compare(1, 2, Name("/X/A"), 1), 0); |
| 521 | } |
| 522 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 523 | BOOST_AUTO_TEST_CASE(UnorderedMap) |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 524 | { |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 525 | std::unordered_map<Name, int> map; |
| 526 | Name name1("/1"); |
| 527 | Name name2("/2"); |
| 528 | Name name3("/3"); |
| 529 | map[name1] = 1; |
| 530 | map[name2] = 2; |
| 531 | map[name3] = 3; |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 532 | |
Junxiao Shi | 71ff231 | 2017-07-12 13:32:50 +0000 | [diff] [blame] | 533 | BOOST_CHECK_EQUAL(map[name1], 1); |
| 534 | BOOST_CHECK_EQUAL(map[name2], 2); |
| 535 | BOOST_CHECK_EQUAL(map[name3], 3); |
Alexander Afanasyev | 4f512fb | 2016-05-18 10:47:53 -0700 | [diff] [blame] | 536 | } |
| 537 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 538 | BOOST_AUTO_TEST_SUITE_END() // TestName |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 539 | |
Junxiao Shi | 6bf0adf | 2015-02-15 13:54:56 -0700 | [diff] [blame] | 540 | } // namespace tests |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 541 | } // namespace ndn |