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 | /* |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 22 | #include "encoding/tlv.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 25 | |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 26 | #include <array> |
| 27 | #include <deque> |
| 28 | #include <list> |
Davide Pesavento | a84f464 | 2017-08-23 16:14:51 -0400 | [diff] [blame] | 29 | #include <sstream> |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 30 | #include <boost/concept_archetype.hpp> |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 31 | #include <boost/iostreams/stream.hpp> |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 32 | #include <boost/iostreams/device/array.hpp> |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 33 | #include <boost/lexical_cast.hpp> |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace tlv { |
| 37 | namespace tests { |
| 38 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 39 | BOOST_AUTO_TEST_SUITE(Encoding) |
| 40 | BOOST_AUTO_TEST_SUITE(TestTlv) |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 41 | |
Junxiao Shi | a2550a9 | 2018-04-10 05:07:48 +0000 | [diff] [blame] | 42 | BOOST_AUTO_TEST_CASE(CriticalType) |
| 43 | { |
| 44 | BOOST_CHECK_EQUAL(isCriticalType(0), true); |
| 45 | BOOST_CHECK_EQUAL(isCriticalType(1), true); |
| 46 | BOOST_CHECK_EQUAL(isCriticalType(2), true); |
| 47 | BOOST_CHECK_EQUAL(isCriticalType(30), true); |
| 48 | BOOST_CHECK_EQUAL(isCriticalType(31), true); |
| 49 | BOOST_CHECK_EQUAL(isCriticalType(32), false); |
| 50 | BOOST_CHECK_EQUAL(isCriticalType(33), true); |
| 51 | BOOST_CHECK_EQUAL(isCriticalType(34), false); |
| 52 | BOOST_CHECK_EQUAL(isCriticalType(10000), false); |
| 53 | BOOST_CHECK_EQUAL(isCriticalType(10001), true); |
| 54 | } |
| 55 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 56 | using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 57 | using StreamIterator = std::istream_iterator<uint8_t>; |
| 58 | |
| 59 | #define ASSERT_READ_NUMBER_IS_FAST(T) \ |
| 60 | static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \ |
| 61 | # T " should use ReadNumberFast") |
| 62 | #define ASSERT_READ_NUMBER_IS_SLOW(T) \ |
| 63 | static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \ |
| 64 | # T " should use ReadNumberSlow") |
| 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); |
| 81 | ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator); |
| 82 | ASSERT_READ_NUMBER_IS_FAST(std::string::iterator); |
| 83 | ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator); |
| 84 | ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator); |
| 85 | ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator); |
| 86 | ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator); |
| 87 | ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator); |
| 88 | ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator); |
| 89 | ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator); |
| 90 | ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator); |
| 91 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator); |
| 92 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator); |
| 93 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator); |
| 94 | ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator); |
Davide Pesavento | 4ab3be2 | 2017-07-18 00:38:52 -0400 | [diff] [blame] | 95 | ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 96 | ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator); |
| 97 | ASSERT_READ_NUMBER_IS_SLOW(StreamIterator); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 98 | |
| 99 | BOOST_AUTO_TEST_SUITE(VarNumber) |
| 100 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 101 | // This check ensures readVarNumber and readType only require InputIterator concept and nothing |
| 102 | // more. This function should compile, but should never be executed. |
| 103 | void |
| 104 | checkArchetype() |
| 105 | { |
| 106 | boost::input_iterator_archetype<uint8_t> begin, end; |
| 107 | uint64_t number = readVarNumber(begin, end); |
| 108 | uint32_t type = readType(begin, end);; |
| 109 | readVarNumber(begin, end, number); |
| 110 | readType(begin, end, type); |
| 111 | } |
| 112 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 113 | static const uint8_t BUFFER[] = { |
| 114 | 0x01, // == 1 |
| 115 | 0xfc, // == 252 |
| 116 | 0xfd, 0x00, 0xfd, // == 253 |
| 117 | 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 118 | 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296 |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(SizeOf) |
| 122 | { |
| 123 | BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1); |
| 124 | BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1); |
| 125 | BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3); |
| 126 | BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 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 | |
| 130 | BOOST_AUTO_TEST_CASE(Write) |
| 131 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 132 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 133 | |
| 134 | writeVarNumber(os, 1); |
| 135 | writeVarNumber(os, 252); |
| 136 | writeVarNumber(os, 253); |
| 137 | writeVarNumber(os, 65536); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 138 | writeVarNumber(os, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 139 | |
| 140 | std::string buffer = os.str(); |
| 141 | const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str()); |
| 142 | |
| 143 | BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER)); |
| 144 | BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER), |
| 145 | actual, actual + sizeof(BUFFER)); |
| 146 | } |
| 147 | |
| 148 | BOOST_AUTO_TEST_CASE(ReadFromBuffer) |
| 149 | { |
| 150 | const uint8_t* begin; |
| 151 | uint64_t value; |
| 152 | |
| 153 | begin = BUFFER; |
| 154 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 155 | begin = BUFFER; |
| 156 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 157 | BOOST_CHECK_EQUAL(value, 1); |
| 158 | |
| 159 | begin = BUFFER + 1; |
| 160 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true); |
| 161 | begin = BUFFER + 1; |
| 162 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1)); |
| 163 | BOOST_CHECK_EQUAL(value, 252); |
| 164 | |
| 165 | begin = BUFFER + 2; |
| 166 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 167 | begin = BUFFER + 2; |
| 168 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 169 | |
| 170 | begin = BUFFER + 2; |
| 171 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false); |
| 172 | begin = BUFFER + 2; |
| 173 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error); |
| 174 | |
| 175 | begin = BUFFER + 2; |
| 176 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true); |
| 177 | begin = BUFFER + 2; |
| 178 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3)); |
| 179 | BOOST_CHECK_EQUAL(value, 253); |
| 180 | |
| 181 | |
| 182 | begin = BUFFER + 5; |
| 183 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 184 | begin = BUFFER + 5; |
| 185 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 186 | |
| 187 | begin = BUFFER + 5; |
| 188 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false); |
| 189 | begin = BUFFER + 5; |
| 190 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error); |
| 191 | |
| 192 | begin = BUFFER + 5; |
| 193 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true); |
| 194 | begin = BUFFER + 5; |
| 195 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5)); |
| 196 | BOOST_CHECK_EQUAL(value, 65536); |
| 197 | |
| 198 | begin = BUFFER + 10; |
| 199 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false); |
| 200 | begin = BUFFER + 10; |
| 201 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error); |
| 202 | |
| 203 | begin = BUFFER + 10; |
| 204 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false); |
| 205 | begin = BUFFER + 10; |
| 206 | BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error); |
| 207 | |
| 208 | begin = BUFFER + 10; |
| 209 | BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true); |
| 210 | begin = BUFFER + 10; |
| 211 | BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 212 | BOOST_CHECK_EQUAL(value, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 216 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 217 | StreamIterator end; // end of stream |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 218 | uint64_t value; |
| 219 | { |
| 220 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 221 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 222 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 223 | } |
| 224 | { |
| 225 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 226 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 227 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 228 | BOOST_CHECK_EQUAL(value, 1); |
| 229 | } |
| 230 | |
| 231 | { |
| 232 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 233 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 234 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true); |
| 235 | } |
| 236 | { |
| 237 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 238 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 239 | BOOST_CHECK_NO_THROW(readVarNumber(begin, end)); |
| 240 | BOOST_CHECK_EQUAL(value, 252); |
| 241 | } |
| 242 | |
| 243 | { |
| 244 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 245 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 246 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 247 | } |
| 248 | { |
| 249 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 250 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 251 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 252 | } |
| 253 | |
| 254 | { |
| 255 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 256 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 257 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 258 | } |
| 259 | { |
| 260 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 261 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 262 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 263 | } |
| 264 | |
| 265 | { |
| 266 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
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), true); |
| 269 | } |
| 270 | { |
| 271 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3); |
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_NO_THROW(readVarNumber(begin, end)); |
| 274 | BOOST_CHECK_EQUAL(value, 253); |
| 275 | } |
| 276 | |
| 277 | { |
| 278 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 279 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 280 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 281 | } |
| 282 | { |
| 283 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 284 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 285 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 286 | } |
| 287 | |
| 288 | { |
| 289 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 290 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 291 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 292 | } |
| 293 | { |
| 294 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 295 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 296 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 297 | } |
| 298 | |
| 299 | { |
| 300 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
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), true); |
| 303 | } |
| 304 | { |
| 305 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5); |
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_NO_THROW(readVarNumber(begin, end)); |
| 308 | BOOST_CHECK_EQUAL(value, 65536); |
| 309 | } |
| 310 | |
| 311 | { |
| 312 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 313 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 314 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 315 | } |
| 316 | { |
| 317 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 318 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 319 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 320 | } |
| 321 | |
| 322 | { |
| 323 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 324 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 325 | BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false); |
| 326 | } |
| 327 | { |
| 328 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 329 | StreamIterator begin(stream); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 330 | BOOST_CHECK_THROW(readVarNumber(begin, end), Error); |
| 331 | } |
| 332 | |
| 333 | { |
| 334 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
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), true); |
| 337 | } |
| 338 | { |
| 339 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9); |
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_NO_THROW(readVarNumber(begin, end)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 342 | BOOST_CHECK_EQUAL(value, 4294967296); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
| 346 | BOOST_AUTO_TEST_SUITE_END() // VarNumber |
| 347 | |
| 348 | BOOST_AUTO_TEST_SUITE(NonNegativeInteger) |
| 349 | |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 350 | // This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more. |
| 351 | // This function should compile, but should never be executed. |
| 352 | void |
| 353 | checkArchetype() |
| 354 | { |
| 355 | boost::input_iterator_archetype<uint8_t> begin, end; |
| 356 | readNonNegativeInteger(0, begin, end); |
| 357 | } |
| 358 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 359 | static const uint8_t BUFFER[] = { |
| 360 | 0x01, // 1 |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 361 | 0xff, // 255 |
| 362 | 0x01, 0x02, // 258 |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 363 | 0x01, 0x01, 0x01, 0x02, // 16843010 |
| 364 | 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674 |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 365 | }; |
| 366 | |
| 367 | BOOST_AUTO_TEST_CASE(SizeOf) |
| 368 | { |
| 369 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 370 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1); |
| 371 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1); |
| 372 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 373 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 374 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4); |
| 375 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8); |
| 376 | BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | BOOST_AUTO_TEST_CASE(Write) |
| 380 | { |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 381 | std::ostringstream os; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 382 | |
| 383 | writeNonNegativeInteger(os, 1); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 384 | writeNonNegativeInteger(os, 255); |
| 385 | writeNonNegativeInteger(os, 258); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 386 | writeNonNegativeInteger(os, 16843010); |
| 387 | writeNonNegativeInteger(os, 72340172838076674); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 388 | |
| 389 | std::string buffer = os.str(); |
| 390 | const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str()); |
| 391 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 392 | BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER), |
| 393 | actual, actual + sizeof(BUFFER)); |
| 394 | } |
| 395 | |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 396 | BOOST_AUTO_TEST_CASE(ReadFromBuffer) |
| 397 | { |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 398 | const uint8_t* begin = nullptr; |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 399 | |
| 400 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 401 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1); |
| 402 | BOOST_CHECK_EQUAL(begin, BUFFER + 1); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 403 | |
| 404 | begin = BUFFER + 1; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 405 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255); |
| 406 | BOOST_CHECK_EQUAL(begin, BUFFER + 2); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 407 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 408 | begin = BUFFER + 2; |
| 409 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258); |
| 410 | BOOST_CHECK_EQUAL(begin, BUFFER + 4); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 411 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 412 | begin = BUFFER + 4; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 413 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 414 | BOOST_CHECK_EQUAL(begin, BUFFER + 8); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 415 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 416 | begin = BUFFER + 8; |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 417 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 418 | BOOST_CHECK_EQUAL(begin, BUFFER + 16); |
| 419 | |
| 420 | // invalid size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 421 | begin = BUFFER; |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 422 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error); |
| 423 | |
| 424 | // available buffer smaller than size |
| 425 | begin = BUFFER; |
| 426 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error); |
| 427 | begin = BUFFER; |
| 428 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error); |
| 429 | begin = BUFFER; |
| 430 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error); |
| 431 | begin = BUFFER; |
| 432 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | BOOST_AUTO_TEST_CASE(ReadFromStream) |
| 436 | { |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 437 | StreamIterator end; // end of stream |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 438 | |
| 439 | { |
| 440 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 441 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 442 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1); |
| 443 | BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255); |
| 444 | BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 445 | BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010); |
| 446 | BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 447 | BOOST_CHECK(begin == end); |
| 448 | } |
| 449 | |
| 450 | // invalid size |
| 451 | { |
| 452 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 453 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 454 | BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error); |
| 455 | } |
| 456 | |
| 457 | // available buffer smaller than size |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 458 | { |
| 459 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 460 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 461 | BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 462 | } |
| 463 | { |
| 464 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 465 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 466 | BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 467 | } |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 468 | { |
| 469 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 470 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 471 | BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error); |
| 472 | } |
| 473 | { |
| 474 | ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7); |
Junxiao Shi | 46a9bd3 | 2017-07-14 19:12:11 +0000 | [diff] [blame] | 475 | StreamIterator begin(stream); |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 476 | BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error); |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
| 480 | BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger |
| 481 | |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 482 | BOOST_AUTO_TEST_SUITE(PrintHelpers) |
| 483 | |
| 484 | BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue) |
| 485 | { |
Davide Pesavento | 7e6f6f8 | 2017-10-31 18:05:28 -0400 | [diff] [blame] | 486 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256"); |
| 487 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa"); |
| 488 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa"); |
| 489 | BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(200)), "Unknown Signature Type"); |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 490 | } |
| 491 | |
Junxiao Shi | c18aa19 | 2017-07-07 06:06:38 +0000 | [diff] [blame] | 492 | BOOST_AUTO_TEST_SUITE_END() // PrintHelpers |
Alexander Afanasyev | 5f1820e | 2017-01-04 18:12:42 -0800 | [diff] [blame] | 493 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 494 | BOOST_AUTO_TEST_SUITE_END() // TestTlv |
| 495 | BOOST_AUTO_TEST_SUITE_END() // Encoding |
Alexander Afanasyev | d1de397 | 2014-08-14 19:47:41 -0700 | [diff] [blame] | 496 | |
| 497 | } // namespace tests |
| 498 | } // namespace tlv |
| 499 | } // namespace ndn |