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