blob: 1acffa575e00c9b07c825297aa932922145d1d97 [file] [log] [blame]
Alexander Afanasyevd1de3972014-08-14 19:47:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shic18aa192017-07-07 06:06:38 +00002/*
Davide Pesavento287a7fa2020-10-01 22:46:43 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Alexander Afanasyevd1de3972014-08-14 19:47:41 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/encoding/tlv.hpp"
23#include "ndn-cxx/encoding/buffer.hpp"
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010026
Davide Pesavento4ab3be22017-07-18 00:38:52 -040027#include <array>
28#include <deque>
29#include <list>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040030#include <sstream>
Junxiao Shi46a9bd32017-07-14 19:12:11 +000031#include <boost/concept_archetype.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070032#include <boost/iostreams/stream.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033#include <boost/iostreams/device/array.hpp>
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080034#include <boost/lexical_cast.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070035
36namespace ndn {
37namespace tlv {
38namespace tests {
39
Davide Pesaventoeee3e822016-11-26 19:19:34 +010040BOOST_AUTO_TEST_SUITE(Encoding)
41BOOST_AUTO_TEST_SUITE(TestTlv)
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070042
Junxiao Shia2550a92018-04-10 05:07:48 +000043BOOST_AUTO_TEST_CASE(CriticalType)
44{
45 BOOST_CHECK_EQUAL(isCriticalType(0), true);
46 BOOST_CHECK_EQUAL(isCriticalType(1), true);
47 BOOST_CHECK_EQUAL(isCriticalType(2), true);
48 BOOST_CHECK_EQUAL(isCriticalType(30), true);
49 BOOST_CHECK_EQUAL(isCriticalType(31), true);
50 BOOST_CHECK_EQUAL(isCriticalType(32), false);
51 BOOST_CHECK_EQUAL(isCriticalType(33), true);
52 BOOST_CHECK_EQUAL(isCriticalType(34), false);
53 BOOST_CHECK_EQUAL(isCriticalType(10000), false);
54 BOOST_CHECK_EQUAL(isCriticalType(10001), true);
55}
56
Davide Pesaventoeee3e822016-11-26 19:19:34 +010057using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
Junxiao Shi46a9bd32017-07-14 19:12:11 +000058using StreamIterator = std::istream_iterator<uint8_t>;
59
60#define ASSERT_READ_NUMBER_IS_FAST(T) \
61 static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \
62 # T " should use ReadNumberFast")
63#define ASSERT_READ_NUMBER_IS_SLOW(T) \
64 static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \
65 # T " should use ReadNumberSlow")
66
67ASSERT_READ_NUMBER_IS_FAST(const uint8_t*);
68ASSERT_READ_NUMBER_IS_FAST(uint8_t*);
69ASSERT_READ_NUMBER_IS_FAST(int8_t*);
70ASSERT_READ_NUMBER_IS_FAST(char*);
71ASSERT_READ_NUMBER_IS_FAST(unsigned char*);
72ASSERT_READ_NUMBER_IS_FAST(signed char*);
73ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]);
74ASSERT_READ_NUMBER_IS_FAST(uint8_t[]);
75ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]);
76ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]);
77using Uint8Array = std::array<uint8_t, 87>;
78ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator);
79ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator);
80using CharArray = std::array<char, 87>;
81ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator);
82ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator);
83ASSERT_READ_NUMBER_IS_FAST(std::string::iterator);
84ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator);
85ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator);
86ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator);
87ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator);
88ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator);
89ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator);
90ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator);
91ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator);
92ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator);
93ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator);
94ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator);
95ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator);
Davide Pesavento4ab3be22017-07-18 00:38:52 -040096ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator);
Junxiao Shi46a9bd32017-07-14 19:12:11 +000097ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator);
98ASSERT_READ_NUMBER_IS_SLOW(StreamIterator);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070099
100BOOST_AUTO_TEST_SUITE(VarNumber)
101
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000102// This check ensures readVarNumber and readType only require InputIterator concept and nothing
103// more. This function should compile, but should never be executed.
104void
105checkArchetype()
106{
107 boost::input_iterator_archetype<uint8_t> begin, end;
108 uint64_t number = readVarNumber(begin, end);
Davide Pesavento140e9cb2019-08-19 00:02:05 -0400109 uint32_t type = readType(begin, end);
110 bool ok = readVarNumber(begin, end, number);
111 ok = readType(begin, end, type);
112 static_cast<void>(ok);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000113}
114
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700115static const uint8_t BUFFER[] = {
Davide Pesavento6b330402019-04-24 00:14:01 -0400116 0x00, // == 0
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700117 0x01, // == 1
118 0xfc, // == 252
119 0xfd, 0x00, 0xfd, // == 253
120 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000121 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700122};
123
124BOOST_AUTO_TEST_CASE(SizeOf)
125{
Davide Pesavento6b330402019-04-24 00:14:01 -0400126 BOOST_CHECK_EQUAL(sizeOfVarNumber(0), 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700127 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
128 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
129 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
130 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Davide Pesavento6b330402019-04-24 00:14:01 -0400131 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967295), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000132 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700133}
134
135BOOST_AUTO_TEST_CASE(Write)
136{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100137 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700138
Davide Pesavento6b330402019-04-24 00:14:01 -0400139 writeVarNumber(os, 0);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700140 writeVarNumber(os, 1);
141 writeVarNumber(os, 252);
142 writeVarNumber(os, 253);
143 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000144 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700145
146 std::string buffer = os.str();
Davide Pesavento6b330402019-04-24 00:14:01 -0400147 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.data());
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700148
149 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
150 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
151 actual, actual + sizeof(BUFFER));
152}
153
154BOOST_AUTO_TEST_CASE(ReadFromBuffer)
155{
156 const uint8_t* begin;
157 uint64_t value;
158
159 begin = BUFFER;
160 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
161 begin = BUFFER;
162 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Davide Pesavento6b330402019-04-24 00:14:01 -0400163 BOOST_CHECK_EQUAL(value, 0);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700164
165 begin = BUFFER + 1;
166 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
167 begin = BUFFER + 1;
168 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Davide Pesavento6b330402019-04-24 00:14:01 -0400169 BOOST_CHECK_EQUAL(value, 1);
170
171 begin = BUFFER + 2;
172 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
173 begin = BUFFER + 2;
174 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700175 BOOST_CHECK_EQUAL(value, 252);
176
Davide Pesavento6b330402019-04-24 00:14:01 -0400177 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700178 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400179 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700180 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
181
Davide Pesavento6b330402019-04-24 00:14:01 -0400182 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700183 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400184 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700185 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
186
Davide Pesavento6b330402019-04-24 00:14:01 -0400187 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700188 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400189 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700190 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
191 BOOST_CHECK_EQUAL(value, 253);
192
Davide Pesavento6b330402019-04-24 00:14:01 -0400193 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700194 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400195 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700196 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
197
Davide Pesavento6b330402019-04-24 00:14:01 -0400198 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700199 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400200 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700201 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
202
Davide Pesavento6b330402019-04-24 00:14:01 -0400203 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700204 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400205 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700206 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
207 BOOST_CHECK_EQUAL(value, 65536);
208
Davide Pesavento6b330402019-04-24 00:14:01 -0400209 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700210 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400211 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700212 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
213
Davide Pesavento6b330402019-04-24 00:14:01 -0400214 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700215 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400216 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700217 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
218
Davide Pesavento6b330402019-04-24 00:14:01 -0400219 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700220 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400221 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700222 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000223 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700224}
225
226BOOST_AUTO_TEST_CASE(ReadFromStream)
227{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000228 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700229 uint64_t value;
Davide Pesavento6b330402019-04-24 00:14:01 -0400230
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700231 {
232 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000233 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700234 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
235 }
236 {
237 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000238 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700239 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Davide Pesavento6b330402019-04-24 00:14:01 -0400240 BOOST_CHECK_EQUAL(value, 0);
241 }
242
243 {
244 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
245 StreamIterator begin(stream);
246 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
247 }
248 {
249 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
250 StreamIterator begin(stream);
251 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700252 BOOST_CHECK_EQUAL(value, 1);
253 }
254
255 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400256 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000257 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700258 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
259 }
260 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400261 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000262 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700263 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
264 BOOST_CHECK_EQUAL(value, 252);
265 }
266
267 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400268 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000269 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700270 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
271 }
272 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400273 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000274 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700275 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
276 }
277
278 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400279 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000280 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700281 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
282 }
283 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400284 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000285 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700286 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
287 }
288
289 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400290 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000291 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700292 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
293 }
294 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400295 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000296 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700297 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
298 BOOST_CHECK_EQUAL(value, 253);
299 }
300
301 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400302 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000303 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700304 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
305 }
306 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400307 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000308 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700309 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
310 }
311
312 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400313 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000314 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700315 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
316 }
317 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400318 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000319 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700320 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
321 }
322
323 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400324 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000325 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700326 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
327 }
328 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400329 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000330 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700331 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
332 BOOST_CHECK_EQUAL(value, 65536);
333 }
334
335 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400336 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000337 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700338 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
339 }
340 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400341 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000342 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700343 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
344 }
345
346 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400347 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000348 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700349 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
350 }
351 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400352 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000353 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700354 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
355 }
356
357 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400358 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000359 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700360 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
361 }
362 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400363 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000364 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700365 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000366 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700367 }
368}
369
370BOOST_AUTO_TEST_SUITE_END() // VarNumber
371
Davide Pesavento6b330402019-04-24 00:14:01 -0400372BOOST_AUTO_TEST_SUITE(Type)
373
374static const uint8_t BUFFER[] = {
375 0x00, // == 0 (illegal)
376 0x01, // == 1
377 0xfd, 0x00, 0xfd, // == 253
378 0xfe, 0xff, 0xff, 0xff, 0xff, // == 4294967295
379 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296 (illegal)
380};
381
382BOOST_AUTO_TEST_CASE(Read)
383{
384 const uint8_t* begin;
385 uint32_t type;
386
387 begin = BUFFER;
388 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
389 begin = BUFFER;
390 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
391
392 begin = BUFFER + 1;
393 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), true);
394 begin = BUFFER + 1;
395 BOOST_CHECK_NO_THROW(readType(begin, begin + 1));
396 BOOST_CHECK_EQUAL(type, 1);
397
398 begin = BUFFER + 2;
399 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
400 begin = BUFFER + 2;
401 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
402
403 begin = BUFFER + 2;
404 BOOST_CHECK_EQUAL(readType(begin, begin + 2, type), false);
405 begin = BUFFER + 2;
406 BOOST_CHECK_THROW(readType(begin, begin + 2), Error);
407
408 begin = BUFFER + 2;
409 BOOST_CHECK_EQUAL(readType(begin, begin + 3, type), true);
410 begin = BUFFER + 2;
411 BOOST_CHECK_NO_THROW(readType(begin, begin + 3));
412 BOOST_CHECK_EQUAL(type, 253);
413
414 begin = BUFFER + 5;
415 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
416 begin = BUFFER + 5;
417 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
418
419 begin = BUFFER + 5;
420 BOOST_CHECK_EQUAL(readType(begin, begin + 5, type), true);
421 begin = BUFFER + 5;
422 BOOST_CHECK_NO_THROW(readType(begin, begin + 5));
423 BOOST_CHECK_EQUAL(type, 4294967295);
424
425 begin = BUFFER + 10;
426 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
427 begin = BUFFER + 10;
428 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
429
430 begin = BUFFER + 10;
431 BOOST_CHECK_EQUAL(readType(begin, begin + 9, type), false);
432 begin = BUFFER + 10;
433 BOOST_CHECK_THROW(readType(begin, begin + 9), Error);
434}
435
436BOOST_AUTO_TEST_SUITE_END() // Type
437
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700438BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
439
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000440// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
441// This function should compile, but should never be executed.
442void
443checkArchetype()
444{
445 boost::input_iterator_archetype<uint8_t> begin, end;
446 readNonNegativeInteger(0, begin, end);
447}
448
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700449static const uint8_t BUFFER[] = {
450 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000451 0xff, // 255
452 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000453 0x01, 0x01, 0x01, 0x02, // 16843010
454 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700455};
456
457BOOST_AUTO_TEST_CASE(SizeOf)
458{
459 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000460 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
461 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
462 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700463 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000464 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
465 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
466 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700467}
468
469BOOST_AUTO_TEST_CASE(Write)
470{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100471 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700472
473 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000474 writeNonNegativeInteger(os, 255);
475 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000476 writeNonNegativeInteger(os, 16843010);
477 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700478
479 std::string buffer = os.str();
Davide Pesavento6b330402019-04-24 00:14:01 -0400480 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.data());
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700481
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700482 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
483 actual, actual + sizeof(BUFFER));
484}
485
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700486BOOST_AUTO_TEST_CASE(ReadFromBuffer)
487{
Junxiao Shic18aa192017-07-07 06:06:38 +0000488 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700489
490 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000491 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
492 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700493
494 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000495 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
496 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700497
Junxiao Shic18aa192017-07-07 06:06:38 +0000498 begin = BUFFER + 2;
499 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
500 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700501
Junxiao Shic18aa192017-07-07 06:06:38 +0000502 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000503 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000504 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700505
Junxiao Shic18aa192017-07-07 06:06:38 +0000506 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000507 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000508 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
509
510 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700511 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000512 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
513
514 // available buffer smaller than size
515 begin = BUFFER;
516 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
517 begin = BUFFER;
518 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
519 begin = BUFFER;
520 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
521 begin = BUFFER;
522 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700523}
524
525BOOST_AUTO_TEST_CASE(ReadFromStream)
526{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000527 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000528
529 {
530 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000531 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000532 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
533 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
534 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000535 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
536 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000537 BOOST_CHECK(begin == end);
538 }
539
540 // invalid size
541 {
542 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000543 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000544 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
545 }
546
547 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700548 {
549 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000550 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000551 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700552 }
553 {
554 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000555 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000556 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700557 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700558 {
559 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000560 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000561 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
562 }
563 {
564 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000565 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000566 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700567 }
568}
569
570BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
571
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400572BOOST_AUTO_TEST_SUITE(OutputOperators)
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800573
574BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
575{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400576 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256");
577 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000578 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(2)), "Unknown(2)");
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400579 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000580 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureHmacWithSha256), "SignatureHmacWithSha256");
581 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(5)), "Unknown(5)");
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400582 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(199)), "Unknown(199)");
583 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(NullSignature), "NullSignature");
584 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(201)), "Unknown(201)");
Junxiao Shi426d5002018-08-23 10:52:29 +0000585}
586
587BOOST_AUTO_TEST_CASE(PrintContentTypeValue)
588{
589 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Blob), "Blob");
590 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Link), "Link");
591 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Key), "Key");
592 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Nack), "Nack");
593 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Manifest), "Manifest");
594 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_PrefixAnn), "PrefixAnn");
595 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(6)), "Reserved(6)");
596 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1023)), "Reserved(1023)");
597 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Flic), "FLIC");
598 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1025)), "Unknown(1025)");
599 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(8999)), "Unknown(8999)");
600 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9000)), "Experimental(9000)");
601 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9999)), "Experimental(9999)");
602 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(10000)), "Unknown(10000)");
603 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(19910118)), "Unknown(19910118)");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800604}
605
Davide Pesavento287a7fa2020-10-01 22:46:43 -0400606BOOST_AUTO_TEST_SUITE_END() // OutputOperators
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800607
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100608BOOST_AUTO_TEST_SUITE_END() // TestTlv
609BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700610
611} // namespace tests
612} // namespace tlv
613} // namespace ndn