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