blob: 1810f01bb7a589864fce9dd8c5d81ace7c2a4022 [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 Pesavento6b330402019-04-24 00:14:01 -04003 * Copyright (c) 2013-2019 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);
109 uint32_t type = readType(begin, end);;
110 readVarNumber(begin, end, number);
111 readType(begin, end, type);
112}
113
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700114static const uint8_t BUFFER[] = {
Davide Pesavento6b330402019-04-24 00:14:01 -0400115 0x00, // == 0
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700116 0x01, // == 1
117 0xfc, // == 252
118 0xfd, 0x00, 0xfd, // == 253
119 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000120 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700121};
122
123BOOST_AUTO_TEST_CASE(SizeOf)
124{
Davide Pesavento6b330402019-04-24 00:14:01 -0400125 BOOST_CHECK_EQUAL(sizeOfVarNumber(0), 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700126 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
127 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
128 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
129 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Davide Pesavento6b330402019-04-24 00:14:01 -0400130 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967295), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000131 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700132}
133
134BOOST_AUTO_TEST_CASE(Write)
135{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100136 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700137
Davide Pesavento6b330402019-04-24 00:14:01 -0400138 writeVarNumber(os, 0);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700139 writeVarNumber(os, 1);
140 writeVarNumber(os, 252);
141 writeVarNumber(os, 253);
142 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000143 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700144
145 std::string buffer = os.str();
Davide Pesavento6b330402019-04-24 00:14:01 -0400146 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.data());
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700147
148 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
149 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
150 actual, actual + sizeof(BUFFER));
151}
152
153BOOST_AUTO_TEST_CASE(ReadFromBuffer)
154{
155 const uint8_t* begin;
156 uint64_t value;
157
158 begin = BUFFER;
159 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
160 begin = BUFFER;
161 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Davide Pesavento6b330402019-04-24 00:14:01 -0400162 BOOST_CHECK_EQUAL(value, 0);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700163
164 begin = BUFFER + 1;
165 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
166 begin = BUFFER + 1;
167 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Davide Pesavento6b330402019-04-24 00:14:01 -0400168 BOOST_CHECK_EQUAL(value, 1);
169
170 begin = BUFFER + 2;
171 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
172 begin = BUFFER + 2;
173 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700174 BOOST_CHECK_EQUAL(value, 252);
175
Davide Pesavento6b330402019-04-24 00:14:01 -0400176 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700177 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400178 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700179 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
180
Davide Pesavento6b330402019-04-24 00:14:01 -0400181 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700182 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400183 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700184 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
185
Davide Pesavento6b330402019-04-24 00:14:01 -0400186 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700187 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400188 begin = BUFFER + 3;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700189 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
190 BOOST_CHECK_EQUAL(value, 253);
191
Davide Pesavento6b330402019-04-24 00:14:01 -0400192 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700193 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400194 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700195 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
196
Davide Pesavento6b330402019-04-24 00:14:01 -0400197 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700198 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400199 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700200 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
201
Davide Pesavento6b330402019-04-24 00:14:01 -0400202 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700203 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400204 begin = BUFFER + 6;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700205 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
206 BOOST_CHECK_EQUAL(value, 65536);
207
Davide Pesavento6b330402019-04-24 00:14:01 -0400208 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700209 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400210 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700211 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
212
Davide Pesavento6b330402019-04-24 00:14:01 -0400213 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700214 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
Davide Pesavento6b330402019-04-24 00:14:01 -0400215 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700216 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
217
Davide Pesavento6b330402019-04-24 00:14:01 -0400218 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700219 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
Davide Pesavento6b330402019-04-24 00:14:01 -0400220 begin = BUFFER + 11;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700221 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000222 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700223}
224
225BOOST_AUTO_TEST_CASE(ReadFromStream)
226{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000227 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700228 uint64_t value;
Davide Pesavento6b330402019-04-24 00:14:01 -0400229
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700230 {
231 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000232 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700233 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
234 }
235 {
236 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000237 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700238 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Davide Pesavento6b330402019-04-24 00:14:01 -0400239 BOOST_CHECK_EQUAL(value, 0);
240 }
241
242 {
243 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
244 StreamIterator begin(stream);
245 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
246 }
247 {
248 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
249 StreamIterator begin(stream);
250 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700251 BOOST_CHECK_EQUAL(value, 1);
252 }
253
254 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400255 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000256 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700257 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
258 }
259 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400260 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000261 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700262 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
263 BOOST_CHECK_EQUAL(value, 252);
264 }
265
266 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400267 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000268 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700269 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
270 }
271 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400272 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000273 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700274 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
275 }
276
277 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400278 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000279 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700280 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
281 }
282 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400283 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000284 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700285 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
286 }
287
288 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400289 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000290 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700291 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
292 }
293 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400294 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 3, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000295 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700296 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
297 BOOST_CHECK_EQUAL(value, 253);
298 }
299
300 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400301 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000302 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700303 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
304 }
305 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400306 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000307 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700308 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
309 }
310
311 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400312 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000313 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700314 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
315 }
316 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400317 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000318 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700319 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
320 }
321
322 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400323 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000324 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700325 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
326 }
327 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400328 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 6, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000329 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700330 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
331 BOOST_CHECK_EQUAL(value, 65536);
332 }
333
334 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400335 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000336 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700337 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
338 }
339 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400340 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000341 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700342 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
343 }
344
345 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400346 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000347 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700348 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
349 }
350 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400351 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000352 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700353 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
354 }
355
356 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400357 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000358 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700359 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
360 }
361 {
Davide Pesavento6b330402019-04-24 00:14:01 -0400362 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 11, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000363 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700364 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000365 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700366 }
367}
368
369BOOST_AUTO_TEST_SUITE_END() // VarNumber
370
Davide Pesavento6b330402019-04-24 00:14:01 -0400371BOOST_AUTO_TEST_SUITE(Type)
372
373static const uint8_t BUFFER[] = {
374 0x00, // == 0 (illegal)
375 0x01, // == 1
376 0xfd, 0x00, 0xfd, // == 253
377 0xfe, 0xff, 0xff, 0xff, 0xff, // == 4294967295
378 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296 (illegal)
379};
380
381BOOST_AUTO_TEST_CASE(Read)
382{
383 const uint8_t* begin;
384 uint32_t type;
385
386 begin = BUFFER;
387 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
388 begin = BUFFER;
389 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
390
391 begin = BUFFER + 1;
392 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), true);
393 begin = BUFFER + 1;
394 BOOST_CHECK_NO_THROW(readType(begin, begin + 1));
395 BOOST_CHECK_EQUAL(type, 1);
396
397 begin = BUFFER + 2;
398 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
399 begin = BUFFER + 2;
400 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
401
402 begin = BUFFER + 2;
403 BOOST_CHECK_EQUAL(readType(begin, begin + 2, type), false);
404 begin = BUFFER + 2;
405 BOOST_CHECK_THROW(readType(begin, begin + 2), Error);
406
407 begin = BUFFER + 2;
408 BOOST_CHECK_EQUAL(readType(begin, begin + 3, type), true);
409 begin = BUFFER + 2;
410 BOOST_CHECK_NO_THROW(readType(begin, begin + 3));
411 BOOST_CHECK_EQUAL(type, 253);
412
413 begin = BUFFER + 5;
414 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
415 begin = BUFFER + 5;
416 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
417
418 begin = BUFFER + 5;
419 BOOST_CHECK_EQUAL(readType(begin, begin + 5, type), true);
420 begin = BUFFER + 5;
421 BOOST_CHECK_NO_THROW(readType(begin, begin + 5));
422 BOOST_CHECK_EQUAL(type, 4294967295);
423
424 begin = BUFFER + 10;
425 BOOST_CHECK_EQUAL(readType(begin, begin + 1, type), false);
426 begin = BUFFER + 10;
427 BOOST_CHECK_THROW(readType(begin, begin + 1), Error);
428
429 begin = BUFFER + 10;
430 BOOST_CHECK_EQUAL(readType(begin, begin + 9, type), false);
431 begin = BUFFER + 10;
432 BOOST_CHECK_THROW(readType(begin, begin + 9), Error);
433}
434
435BOOST_AUTO_TEST_SUITE_END() // Type
436
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700437BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
438
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000439// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
440// This function should compile, but should never be executed.
441void
442checkArchetype()
443{
444 boost::input_iterator_archetype<uint8_t> begin, end;
445 readNonNegativeInteger(0, begin, end);
446}
447
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700448static const uint8_t BUFFER[] = {
449 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000450 0xff, // 255
451 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000452 0x01, 0x01, 0x01, 0x02, // 16843010
453 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700454};
455
456BOOST_AUTO_TEST_CASE(SizeOf)
457{
458 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000459 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
460 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
461 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700462 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000463 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
464 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
465 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700466}
467
468BOOST_AUTO_TEST_CASE(Write)
469{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100470 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700471
472 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000473 writeNonNegativeInteger(os, 255);
474 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000475 writeNonNegativeInteger(os, 16843010);
476 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700477
478 std::string buffer = os.str();
Davide Pesavento6b330402019-04-24 00:14:01 -0400479 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.data());
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700480
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700481 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
482 actual, actual + sizeof(BUFFER));
483}
484
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700485BOOST_AUTO_TEST_CASE(ReadFromBuffer)
486{
Junxiao Shic18aa192017-07-07 06:06:38 +0000487 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700488
489 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000490 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
491 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700492
493 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000494 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
495 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700496
Junxiao Shic18aa192017-07-07 06:06:38 +0000497 begin = BUFFER + 2;
498 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
499 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700500
Junxiao Shic18aa192017-07-07 06:06:38 +0000501 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000502 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000503 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700504
Junxiao Shic18aa192017-07-07 06:06:38 +0000505 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000506 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000507 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
508
509 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700510 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000511 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
512
513 // available buffer smaller than size
514 begin = BUFFER;
515 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
516 begin = BUFFER;
517 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
518 begin = BUFFER;
519 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
520 begin = BUFFER;
521 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700522}
523
524BOOST_AUTO_TEST_CASE(ReadFromStream)
525{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000526 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000527
528 {
529 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000530 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000531 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
532 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
533 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000534 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
535 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000536 BOOST_CHECK(begin == end);
537 }
538
539 // invalid size
540 {
541 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000542 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000543 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
544 }
545
546 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700547 {
548 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000549 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000550 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700551 }
552 {
553 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000554 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000555 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700556 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700557 {
558 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000559 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000560 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
561 }
562 {
563 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000564 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000565 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700566 }
567}
568
569BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
570
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800571BOOST_AUTO_TEST_SUITE(PrintHelpers)
572
573BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
574{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400575 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256");
576 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000577 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(2)), "Unknown(2)");
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400578 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000579 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureHmacWithSha256), "SignatureHmacWithSha256");
580 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(5)), "Unknown(5)");
581 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(200)), "Unknown(200)");
582}
583
584BOOST_AUTO_TEST_CASE(PrintContentTypeValue)
585{
586 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Blob), "Blob");
587 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Link), "Link");
588 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Key), "Key");
589 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Nack), "Nack");
590 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Manifest), "Manifest");
591 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_PrefixAnn), "PrefixAnn");
592 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(6)), "Reserved(6)");
593 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1023)), "Reserved(1023)");
594 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Flic), "FLIC");
595 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1025)), "Unknown(1025)");
596 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(8999)), "Unknown(8999)");
597 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9000)), "Experimental(9000)");
598 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9999)), "Experimental(9999)");
599 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(10000)), "Unknown(10000)");
600 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(19910118)), "Unknown(19910118)");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800601}
602
Junxiao Shic18aa192017-07-07 06:06:38 +0000603BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800604
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100605BOOST_AUTO_TEST_SUITE_END() // TestTlv
606BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700607
608} // namespace tests
609} // namespace tlv
610} // namespace ndn