Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | f14e4e2 | 2025-04-02 23:00:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2025 Regents of the University of California. |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [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/encoding/tlv.hpp" |
| 23 | #include "ndn-cxx/encoding/buffer.hpp" |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 24 | #include "ndn-cxx/util/span.hpp" |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 25 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 26 | #include "tests/boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 27 | |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 28 | #include <array> |
| 29 | #include <deque> |
| 30 | #include <list> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 31 | #include <sstream> |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 32 | #include <boost/concept_archetype.hpp> |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 33 | #include <boost/iostreams/stream.hpp> |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 34 | #include <boost/iostreams/device/array.hpp> |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 35 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 36 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 37 | namespace ndn::tests { |
| 38 | |
| 39 | using namespace ndn::tlv; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 40 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 41 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 42 | BOOST_AUTO_TEST_SUITE(TestTlv) |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 43 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 44 | BOOST_AUTO_TEST_CASE(CriticalType) |
| 45 | { |
| 46 | BOOST_CHECK_EQUAL(isCriticalType(0), true); |
| 47 | BOOST_CHECK_EQUAL(isCriticalType(1), true); |
| 48 | BOOST_CHECK_EQUAL(isCriticalType(2), true); |
| 49 | BOOST_CHECK_EQUAL(isCriticalType(30), true); |
| 50 | BOOST_CHECK_EQUAL(isCriticalType(31), true); |
| 51 | BOOST_CHECK_EQUAL(isCriticalType(32), false); |
| 52 | BOOST_CHECK_EQUAL(isCriticalType(33), true); |
| 53 | BOOST_CHECK_EQUAL(isCriticalType(34), false); |
| 54 | BOOST_CHECK_EQUAL(isCriticalType(10000), false); |
| 55 | BOOST_CHECK_EQUAL(isCriticalType(10001), true); |
| 56 | } |
| 57 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 58 | using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 59 | using StreamIterator = std::istream_iterator<uint8_t>; |
| 60 | |
| 61 | #define ASSERT_READ_NUMBER_IS_FAST(T) \ |
Davide Pesavento | f14e4e2 | 2025-04-02 23:00:23 -0400 | [diff] [blame] | 62 | static_assert(ndn::tlv::detail::IsContiguousIterator<T>(), #T " is not fast") |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 63 | #define ASSERT_READ_NUMBER_IS_SLOW(T) \ |
Davide Pesavento | f14e4e2 | 2025-04-02 23:00:23 -0400 | [diff] [blame] | 64 | static_assert(!ndn::tlv::detail::IsContiguousIterator<T>(), #T " is not slow") |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 65 | |
| 66 | ASSERT_READ_NUMBER_IS_FAST(const uint8_t*); |
| 67 | ASSERT_READ_NUMBER_IS_FAST(uint8_t*); |
| 68 | ASSERT_READ_NUMBER_IS_FAST(int8_t*); |
| 69 | ASSERT_READ_NUMBER_IS_FAST(char*); |
| 70 | ASSERT_READ_NUMBER_IS_FAST(unsigned char*); |
| 71 | ASSERT_READ_NUMBER_IS_FAST(signed char*); |
| 72 | ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]); |
| 73 | ASSERT_READ_NUMBER_IS_FAST(uint8_t[]); |
| 74 | ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]); |
| 75 | ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]); |
| 76 | using Uint8Array = std::array<uint8_t, 87>; |
| 77 | ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator); |
| 78 | ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator); |
| 79 | using CharArray = std::array<char, 87>; |
| 80 | ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator); |
Davide Pesavento | 21b7f1b | 2023-04-28 23:21:02 -0400 | [diff] [blame] | 81 | ASSERT_READ_NUMBER_IS_FAST(span<const uint8_t>::iterator); |
| 82 | ASSERT_READ_NUMBER_IS_FAST(span<uint8_t>::iterator); |
Davide Pesavento | f14e4e2 | 2025-04-02 23:00:23 -0400 | [diff] [blame] | 83 | ASSERT_READ_NUMBER_IS_FAST(span<int8_t>::iterator); |
| 84 | ASSERT_READ_NUMBER_IS_FAST(span<char>::iterator); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 85 | ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator); |
| 86 | ASSERT_READ_NUMBER_IS_FAST(std::string::iterator); |
| 87 | ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator); |
| 88 | ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator); |
| 89 | ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator); |
| 90 | ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator); |
| 91 | ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator); |
| 92 | ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator); |
| 93 | ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator); |
| 94 | ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator); |
| 95 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator); |
| 96 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator); |
| 97 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator); |
| 98 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator); |
Davide Pesavento | f14e4e2 | 2025-04-02 23:00:23 -0400 | [diff] [blame] | 99 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<std::vector<uint8_t>>::iterator); |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 100 | ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 101 | ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator); |
| 102 | ASSERT_READ_NUMBER_IS_SLOW(StreamIterator); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 103 | |
| 104 | BOOST_AUTO_TEST_SUITE(VarNumber) |
| 105 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 106 | // This check ensures readVarNumber and readType only require InputIterator concept and nothing |
| 107 | // more. This function should compile, but should never be executed. |
| 108 | void |
| 109 | checkArchetype() |
| 110 | { |
| 111 | boost::input_iterator_archetype<uint8_t> begin, end; |
| 112 | uint64_t number = readVarNumber(begin, end); |
Davide Pesavento | 140e9cb | 2019-08-19 00:02:05 -0400 | [diff] [blame] | 113 | uint32_t type = readType(begin, end); |
| 114 | bool ok = readVarNumber(begin, end, number); |
| 115 | ok = readType(begin, end, type); |
| 116 | static_cast<void>(ok); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 119 | BOOST_AUTO_TEST_CASE(SizeOf) |
| 120 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 121 | BOOST_CHECK_EQUAL(sizeOfVarNumber(0), 1); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1); |
| 123 | BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1); |
| 124 | BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3); |
| 125 | BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 126 | BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967295), 5); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 127 | BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 130 | static const uint8_t BUFFER[] = { |
| 131 | 0x00, // == 0 |
| 132 | 0x01, // == 1 |
| 133 | 0xfc, // == 252 |
| 134 | 0xfd, 0x00, 0xfd, // == 253 |
| 135 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
| 136 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296 |
| 137 | }; |
| 138 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 139 | BOOST_AUTO_TEST_CASE(Write) |
| 140 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 141 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 142 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 143 | writeVarNumber(os, 0); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 144 | writeVarNumber(os, 1); |
| 145 | writeVarNumber(os, 252); |
| 146 | writeVarNumber(os, 253); |
| 147 | writeVarNumber(os, 65536); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 148 | writeVarNumber(os, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 149 | |
| 150 | std::string buffer = os.str(); |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 151 | BOOST_TEST(make_span(reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size()) == BUFFER, |
| 152 | boost::test_tools::per_element()); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | BOOST_AUTO_TEST_CASE(ReadFromBuffer) |
| 156 | { |
| 157 | const uint8_t* begin; |
| 158 | uint64_t value; |
| 159 | |
| 160 | begin = BUFFER; |
| 161 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 162 | begin = BUFFER; |
| 163 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(value, 0); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 165 | |
| 166 | begin = BUFFER + 1; |
| 167 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 168 | begin = BUFFER + 1; |
| 169 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 170 | BOOST_CHECK_EQUAL(value, 1); |
| 171 | |
| 172 | begin = BUFFER + 2; |
| 173 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 174 | begin = BUFFER + 2; |
| 175 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 176 | BOOST_CHECK_EQUAL(value, 252); |
| 177 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 178 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 179 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 180 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 181 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 182 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 183 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 184 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 185 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 186 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error); |
| 187 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 188 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 189 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 190 | begin = BUFFER + 3; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 191 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3)); |
| 192 | BOOST_CHECK_EQUAL(value, 253); |
| 193 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 194 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 195 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 196 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 197 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 198 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 199 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 200 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 201 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 202 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error); |
| 203 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 204 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 205 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 206 | begin = BUFFER + 6; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 207 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5)); |
| 208 | BOOST_CHECK_EQUAL(value, 65536); |
| 209 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 210 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 211 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 212 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 213 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 214 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 215 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 216 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 217 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 218 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error); |
| 219 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 220 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 221 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 222 | begin = BUFFER + 11; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 223 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 224 | BOOST_CHECK_EQUAL(value, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 228 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 229 | StreamIterator end; // end of stream |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 230 | uint64_t value; |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 231 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 232 | { |
| 233 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 234 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 235 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 236 | } |
| 237 | { |
| 238 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 239 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 240 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 241 | BOOST_CHECK_EQUAL(value, 0); |
| 242 | } |
| 243 | |
| 244 | { |
| 245 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 246 | StreamIterator begin(stream); |
| 247 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 248 | } |
| 249 | { |
| 250 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
| 251 | StreamIterator begin(stream); |
| 252 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 253 | BOOST_CHECK_EQUAL(value, 1); |
| 254 | } |
| 255 | |
| 256 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 257 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 258 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 259 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 260 | } |
| 261 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 262 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 263 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 264 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 265 | BOOST_CHECK_EQUAL(value, 252); |
| 266 | } |
| 267 | |
| 268 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 269 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 270 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 271 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 272 | } |
| 273 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 274 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 275 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 276 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 277 | } |
| 278 | |
| 279 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 280 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 281 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 282 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 283 | } |
| 284 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 285 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 286 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 287 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 288 | } |
| 289 | |
| 290 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 291 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 292 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 293 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 294 | } |
| 295 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 296 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 297 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 298 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 299 | BOOST_CHECK_EQUAL(value, 253); |
| 300 | } |
| 301 | |
| 302 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 303 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 304 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 305 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 306 | } |
| 307 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 308 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 309 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 310 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 311 | } |
| 312 | |
| 313 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 314 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 315 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 316 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 317 | } |
| 318 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 319 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 320 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 321 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 322 | } |
| 323 | |
| 324 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 325 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 326 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 327 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 328 | } |
| 329 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 330 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 331 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 332 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 333 | BOOST_CHECK_EQUAL(value, 65536); |
| 334 | } |
| 335 | |
| 336 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 337 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 338 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 339 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 340 | } |
| 341 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 342 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 343 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 344 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 345 | } |
| 346 | |
| 347 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 348 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 349 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 350 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 351 | } |
| 352 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 353 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 354 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 355 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 356 | } |
| 357 | |
| 358 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 359 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 360 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 361 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 362 | } |
| 363 | { |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 364 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 365 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 366 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 367 | BOOST_CHECK_EQUAL(value, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | BOOST_AUTO_TEST_SUITE_END() // VarNumber |
| 372 | |
Davide Pesavento | 6b33040 | 2019-04-24 00:14:01 -0400 | [diff] [blame] | 373 | BOOST_AUTO_TEST_SUITE(Type) |
| 374 | |
| 375 | static const uint8_t BUFFER[] = { |
| 376 | 0x00, // == 0 (illegal) |
| 377 | 0x01, // == 1 |
| 378 | 0xfd, 0x00, 0xfd, // == 253 |
| 379 | 0xfe, 0xff, 0xff, 0xff, 0xff, // == 4294967295 |
| 380 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296 (illegal) |
| 381 | }; |
| 382 | |
| 383 | BOOST_AUTO_TEST_CASE(Read) |
| 384 | { |
| 385 | const uint8_t* begin; |
| 386 | uint32_t type; |
| 387 | |
| 388 | begin = BUFFER; |
| 389 | BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false); |
| 390 | begin = BUFFER; |
| 391 | BOOST_CHECK_THROW(readType(begin, begin + 1), Error); |
| 392 | |
| 393 | begin = BUFFER + 1; |
| 394 | BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), true); |
| 395 | begin = BUFFER + 1; |
| 396 | BOOST_CHECK_NO_THROW(readType(begin, begin + 1)); |
| 397 | BOOST_CHECK_EQUAL(type, 1); |
| 398 | |
| 399 | begin = BUFFER + 2; |
| 400 | BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false); |
| 401 | begin = BUFFER + 2; |
| 402 | BOOST_CHECK_THROW(readType(begin, begin + 1), Error); |
| 403 | |
| 404 | begin = BUFFER + 2; |
| 405 | BOOST_CHECK_EQUAL(readType(begin, begin + 2, type), false); |
| 406 | begin = BUFFER + 2; |
| 407 | BOOST_CHECK_THROW(readType(begin, begin + 2), Error); |
| 408 | |
| 409 | begin = BUFFER + 2; |
| 410 | BOOST_CHECK_EQUAL(readType(begin, begin + 3, type), true); |
| 411 | begin = BUFFER + 2; |
| 412 | BOOST_CHECK_NO_THROW(readType(begin, begin + 3)); |
| 413 | BOOST_CHECK_EQUAL(type, 253); |
| 414 | |
| 415 | begin = BUFFER + 5; |
| 416 | BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false); |
| 417 | begin = BUFFER + 5; |
| 418 | BOOST_CHECK_THROW(readType(begin, begin + 1), Error); |
| 419 | |
| 420 | begin = BUFFER + 5; |
| 421 | BOOST_CHECK_EQUAL(readType(begin, begin + 5, type), true); |
| 422 | begin = BUFFER + 5; |
| 423 | BOOST_CHECK_NO_THROW(readType(begin, begin + 5)); |
| 424 | BOOST_CHECK_EQUAL(type, 4294967295); |
| 425 | |
| 426 | begin = BUFFER + 10; |
| 427 | BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false); |
| 428 | begin = BUFFER + 10; |
| 429 | BOOST_CHECK_THROW(readType(begin, begin + 1), Error); |
| 430 | |
| 431 | begin = BUFFER + 10; |
| 432 | BOOST_CHECK_EQUAL(readType(begin, begin + 9, type), false); |
| 433 | begin = BUFFER + 10; |
| 434 | BOOST_CHECK_THROW(readType(begin, begin + 9), Error); |
| 435 | } |
| 436 | |
| 437 | BOOST_AUTO_TEST_SUITE_END() // Type |
| 438 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 439 | BOOST_AUTO_TEST_SUITE(NonNegativeInteger) |
| 440 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 441 | // This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more. |
| 442 | // This function should compile, but should never be executed. |
| 443 | void |
| 444 | checkArchetype() |
| 445 | { |
| 446 | boost::input_iterator_archetype<uint8_t> begin, end; |
| 447 | readNonNegativeInteger(0, begin, end); |
| 448 | } |
| 449 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 450 | BOOST_AUTO_TEST_CASE(SizeOf) |
| 451 | { |
| 452 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 453 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1); |
| 454 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1); |
| 455 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 456 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 457 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4); |
| 458 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8); |
| 459 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 462 | static const uint8_t BUFFER[] = { |
| 463 | 0x01, // 1 |
| 464 | 0xff, // 255 |
| 465 | 0x01, 0x02, // 258 |
| 466 | 0x01, 0x01, 0x01, 0x02, // 16843010 |
| 467 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674 |
| 468 | }; |
| 469 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 470 | BOOST_AUTO_TEST_CASE(Write) |
| 471 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 472 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 473 | |
| 474 | writeNonNegativeInteger(os, 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 475 | writeNonNegativeInteger(os, 255); |
| 476 | writeNonNegativeInteger(os, 258); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 477 | writeNonNegativeInteger(os, 16843010); |
| 478 | writeNonNegativeInteger(os, 72340172838076674); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 479 | |
| 480 | std::string buffer = os.str(); |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 481 | BOOST_TEST(make_span(reinterpret_cast<const uint8_t*>(buffer.data()), buffer.size()) == BUFFER, |
| 482 | boost::test_tools::per_element()); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 483 | } |
| 484 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 485 | BOOST_AUTO_TEST_CASE(ReadFromBuffer) |
| 486 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 487 | const uint8_t* begin = nullptr; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 488 | |
| 489 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 490 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1); |
| 491 | BOOST_CHECK_EQUAL(begin, BUFFER + 1); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 492 | |
| 493 | begin = BUFFER + 1; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 494 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255); |
| 495 | BOOST_CHECK_EQUAL(begin, BUFFER + 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 496 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 497 | begin = BUFFER + 2; |
| 498 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258); |
| 499 | BOOST_CHECK_EQUAL(begin, BUFFER + 4); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 500 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 501 | begin = BUFFER + 4; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 502 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 503 | BOOST_CHECK_EQUAL(begin, BUFFER + 8); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 504 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 505 | begin = BUFFER + 8; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 506 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 507 | BOOST_CHECK_EQUAL(begin, BUFFER + 16); |
| 508 | |
| 509 | // invalid size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 510 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 511 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error); |
| 512 | |
| 513 | // available buffer smaller than size |
| 514 | begin = BUFFER; |
| 515 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error); |
| 516 | begin = BUFFER; |
| 517 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error); |
| 518 | begin = BUFFER; |
| 519 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error); |
| 520 | begin = BUFFER; |
| 521 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 525 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 526 | StreamIterator end; // end of stream |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 527 | |
| 528 | { |
| 529 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 530 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 531 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1); |
| 532 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255); |
| 533 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 534 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010); |
| 535 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 536 | BOOST_CHECK(begin == end); |
| 537 | } |
| 538 | |
| 539 | // invalid size |
| 540 | { |
| 541 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 542 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 543 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error); |
| 544 | } |
| 545 | |
| 546 | // available buffer smaller than size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 547 | { |
| 548 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 549 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 550 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 551 | } |
| 552 | { |
| 553 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 554 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 555 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 556 | } |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 557 | { |
| 558 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 559 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 560 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error); |
| 561 | } |
| 562 | { |
| 563 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 564 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 565 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger |
| 570 | |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 571 | BOOST_AUTO_TEST_SUITE(OutputOperators) |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 572 | |
| 573 | BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue) |
| 574 | { |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 575 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256"); |
| 576 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa"); |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame] | 577 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(2)), "Unknown(2)"); |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 578 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa"); |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame] | 579 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureHmacWithSha256), "SignatureHmacWithSha256"); |
Davide Pesavento | ffddbbf | 2022-11-21 16:33:47 -0500 | [diff] [blame] | 580 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureEd25519), "SignatureEd25519"); |
| 581 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(6)), "Unknown(6)"); |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 582 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(199)), "Unknown(199)"); |
| 583 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(NullSignature), "NullSignature"); |
| 584 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(201)), "Unknown(201)"); |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | BOOST_AUTO_TEST_CASE(PrintContentTypeValue) |
| 588 | { |
| 589 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Blob), "Blob"); |
| 590 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Link), "Link"); |
| 591 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Key), "Key"); |
| 592 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Nack), "Nack"); |
| 593 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Manifest), "Manifest"); |
| 594 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_PrefixAnn), "PrefixAnn"); |
| 595 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(6)), "Reserved(6)"); |
| 596 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1023)), "Reserved(1023)"); |
| 597 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Flic), "FLIC"); |
| 598 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1025)), "Unknown(1025)"); |
| 599 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(8999)), "Unknown(8999)"); |
| 600 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9000)), "Experimental(9000)"); |
| 601 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9999)), "Experimental(9999)"); |
| 602 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(10000)), "Unknown(10000)"); |
| 603 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(19910118)), "Unknown(19910118)"); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Davide Pesavento | 287a7fa | 2020-10-01 22:46:43 -0400 | [diff] [blame] | 606 | BOOST_AUTO_TEST_SUITE_END() // OutputOperators |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 607 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 608 | BOOST_AUTO_TEST_SUITE_END() // TestTlv |
| 609 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 610 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 611 | } // namespace ndn::tests |