blob: a82ffbe86c6a809bee5fe65b8df1761643623b9d [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/*
Junxiao Shia2550a92018-04-10 05:07:48 +00003 * Copyright (c) 2013-2018 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
22#include "encoding/tlv.hpp"
Davide Pesavento570b20d2018-07-15 21:53:14 -040023#include "encoding/buffer.hpp"
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070024
25#include "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[] = {
115 0x01, // == 1
116 0xfc, // == 252
117 0xfd, 0x00, 0xfd, // == 253
118 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000119 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700120};
121
122BOOST_AUTO_TEST_CASE(SizeOf)
123{
124 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
125 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
126 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
127 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000128 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700129}
130
131BOOST_AUTO_TEST_CASE(Write)
132{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100133 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700134
135 writeVarNumber(os, 1);
136 writeVarNumber(os, 252);
137 writeVarNumber(os, 253);
138 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000139 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700140
141 std::string buffer = os.str();
142 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
143
144 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
145 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
146 actual, actual + sizeof(BUFFER));
147}
148
149BOOST_AUTO_TEST_CASE(ReadFromBuffer)
150{
151 const uint8_t* begin;
152 uint64_t value;
153
154 begin = BUFFER;
155 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
156 begin = BUFFER;
157 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
158 BOOST_CHECK_EQUAL(value, 1);
159
160 begin = BUFFER + 1;
161 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
162 begin = BUFFER + 1;
163 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
164 BOOST_CHECK_EQUAL(value, 252);
165
166 begin = BUFFER + 2;
167 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
168 begin = BUFFER + 2;
169 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
170
171 begin = BUFFER + 2;
172 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
173 begin = BUFFER + 2;
174 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
175
176 begin = BUFFER + 2;
177 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
178 begin = BUFFER + 2;
179 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
180 BOOST_CHECK_EQUAL(value, 253);
181
182
183 begin = BUFFER + 5;
184 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
185 begin = BUFFER + 5;
186 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
187
188 begin = BUFFER + 5;
189 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
190 begin = BUFFER + 5;
191 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
192
193 begin = BUFFER + 5;
194 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
195 begin = BUFFER + 5;
196 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
197 BOOST_CHECK_EQUAL(value, 65536);
198
199 begin = BUFFER + 10;
200 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
201 begin = BUFFER + 10;
202 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
203
204 begin = BUFFER + 10;
205 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
206 begin = BUFFER + 10;
207 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
208
209 begin = BUFFER + 10;
210 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
211 begin = BUFFER + 10;
212 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000213 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700214}
215
216BOOST_AUTO_TEST_CASE(ReadFromStream)
217{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000218 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700219 uint64_t value;
220 {
221 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000222 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700223 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
224 }
225 {
226 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000227 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700228 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
229 BOOST_CHECK_EQUAL(value, 1);
230 }
231
232 {
233 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000234 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700235 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
236 }
237 {
238 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000239 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700240 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
241 BOOST_CHECK_EQUAL(value, 252);
242 }
243
244 {
245 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000246 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700247 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
248 }
249 {
250 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000251 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700252 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
253 }
254
255 {
256 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
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), false);
259 }
260 {
261 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000262 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700263 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
264 }
265
266 {
267 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
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), true);
270 }
271 {
272 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000273 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700274 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
275 BOOST_CHECK_EQUAL(value, 253);
276 }
277
278 {
279 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
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 {
284 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
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 {
290 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
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), false);
293 }
294 {
295 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000296 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700297 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
298 }
299
300 {
301 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
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), true);
304 }
305 {
306 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000307 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700308 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
309 BOOST_CHECK_EQUAL(value, 65536);
310 }
311
312 {
313 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
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 {
318 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
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 {
324 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
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), false);
327 }
328 {
329 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000330 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700331 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
332 }
333
334 {
335 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
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), true);
338 }
339 {
340 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000341 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700342 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000343 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700344 }
345}
346
347BOOST_AUTO_TEST_SUITE_END() // VarNumber
348
349BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
350
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000351// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
352// This function should compile, but should never be executed.
353void
354checkArchetype()
355{
356 boost::input_iterator_archetype<uint8_t> begin, end;
357 readNonNegativeInteger(0, begin, end);
358}
359
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700360static const uint8_t BUFFER[] = {
361 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000362 0xff, // 255
363 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000364 0x01, 0x01, 0x01, 0x02, // 16843010
365 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700366};
367
368BOOST_AUTO_TEST_CASE(SizeOf)
369{
370 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000371 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
372 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
373 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700374 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000375 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
376 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
377 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700378}
379
380BOOST_AUTO_TEST_CASE(Write)
381{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100382 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700383
384 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000385 writeNonNegativeInteger(os, 255);
386 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000387 writeNonNegativeInteger(os, 16843010);
388 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700389
390 std::string buffer = os.str();
391 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
392
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700393 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
394 actual, actual + sizeof(BUFFER));
395}
396
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700397BOOST_AUTO_TEST_CASE(ReadFromBuffer)
398{
Junxiao Shic18aa192017-07-07 06:06:38 +0000399 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700400
401 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000402 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
403 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700404
405 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000406 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
407 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700408
Junxiao Shic18aa192017-07-07 06:06:38 +0000409 begin = BUFFER + 2;
410 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
411 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700412
Junxiao Shic18aa192017-07-07 06:06:38 +0000413 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000414 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000415 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700416
Junxiao Shic18aa192017-07-07 06:06:38 +0000417 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000418 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000419 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
420
421 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700422 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000423 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
424
425 // available buffer smaller than size
426 begin = BUFFER;
427 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
428 begin = BUFFER;
429 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
430 begin = BUFFER;
431 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
432 begin = BUFFER;
433 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700434}
435
436BOOST_AUTO_TEST_CASE(ReadFromStream)
437{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000438 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000439
440 {
441 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000442 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000443 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
444 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
445 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000446 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
447 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000448 BOOST_CHECK(begin == end);
449 }
450
451 // invalid size
452 {
453 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000454 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000455 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
456 }
457
458 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700459 {
460 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000461 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000462 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700463 }
464 {
465 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000466 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000467 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700468 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700469 {
470 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000471 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000472 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
473 }
474 {
475 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000476 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000477 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700478 }
479}
480
481BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
482
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800483BOOST_AUTO_TEST_SUITE(PrintHelpers)
484
485BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
486{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400487 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256");
488 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000489 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(2)), "Unknown(2)");
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400490 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa");
Junxiao Shi426d5002018-08-23 10:52:29 +0000491 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureHmacWithSha256), "SignatureHmacWithSha256");
492 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(5)), "Unknown(5)");
493 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(200)), "Unknown(200)");
494}
495
496BOOST_AUTO_TEST_CASE(PrintContentTypeValue)
497{
498 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Blob), "Blob");
499 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Link), "Link");
500 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Key), "Key");
501 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Nack), "Nack");
502 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Manifest), "Manifest");
503 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_PrefixAnn), "PrefixAnn");
504 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(6)), "Reserved(6)");
505 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1023)), "Reserved(1023)");
506 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(ContentType_Flic), "FLIC");
507 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(1025)), "Unknown(1025)");
508 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(8999)), "Unknown(8999)");
509 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9000)), "Experimental(9000)");
510 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(9999)), "Experimental(9999)");
511 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(10000)), "Unknown(10000)");
512 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<ContentTypeValue>(19910118)), "Unknown(19910118)");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800513}
514
Junxiao Shic18aa192017-07-07 06:06:38 +0000515BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800516
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100517BOOST_AUTO_TEST_SUITE_END() // TestTlv
518BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700519
520} // namespace tests
521} // namespace tlv
522} // namespace ndn