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