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