blob: 299b31c4b18a9c90e1a847ea1ad87033effefe48 [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"
23
24#include "boost-test.hpp"
Davide Pesaventoeee3e822016-11-26 19:19:34 +010025
Davide Pesavento4ab3be22017-07-18 00:38:52 -040026#include <array>
27#include <deque>
28#include <list>
Davide Pesaventoa84f4642017-08-23 16:14:51 -040029#include <sstream>
Junxiao Shi46a9bd32017-07-14 19:12:11 +000030#include <boost/concept_archetype.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070031#include <boost/iostreams/stream.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032#include <boost/iostreams/device/array.hpp>
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080033#include <boost/lexical_cast.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070034
35namespace ndn {
36namespace tlv {
37namespace tests {
38
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_SUITE(Encoding)
40BOOST_AUTO_TEST_SUITE(TestTlv)
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070041
Junxiao Shia2550a92018-04-10 05:07:48 +000042BOOST_AUTO_TEST_CASE(CriticalType)
43{
44 BOOST_CHECK_EQUAL(isCriticalType(0), true);
45 BOOST_CHECK_EQUAL(isCriticalType(1), true);
46 BOOST_CHECK_EQUAL(isCriticalType(2), true);
47 BOOST_CHECK_EQUAL(isCriticalType(30), true);
48 BOOST_CHECK_EQUAL(isCriticalType(31), true);
49 BOOST_CHECK_EQUAL(isCriticalType(32), false);
50 BOOST_CHECK_EQUAL(isCriticalType(33), true);
51 BOOST_CHECK_EQUAL(isCriticalType(34), false);
52 BOOST_CHECK_EQUAL(isCriticalType(10000), false);
53 BOOST_CHECK_EQUAL(isCriticalType(10001), true);
54}
55
Davide Pesaventoeee3e822016-11-26 19:19:34 +010056using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
Junxiao Shi46a9bd32017-07-14 19:12:11 +000057using StreamIterator = std::istream_iterator<uint8_t>;
58
59#define ASSERT_READ_NUMBER_IS_FAST(T) \
60 static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \
61 # T " should use ReadNumberFast")
62#define ASSERT_READ_NUMBER_IS_SLOW(T) \
63 static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \
64 # T " should use ReadNumberSlow")
65
66ASSERT_READ_NUMBER_IS_FAST(const uint8_t*);
67ASSERT_READ_NUMBER_IS_FAST(uint8_t*);
68ASSERT_READ_NUMBER_IS_FAST(int8_t*);
69ASSERT_READ_NUMBER_IS_FAST(char*);
70ASSERT_READ_NUMBER_IS_FAST(unsigned char*);
71ASSERT_READ_NUMBER_IS_FAST(signed char*);
72ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]);
73ASSERT_READ_NUMBER_IS_FAST(uint8_t[]);
74ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]);
75ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]);
76using Uint8Array = std::array<uint8_t, 87>;
77ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator);
78ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator);
79using CharArray = std::array<char, 87>;
80ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator);
81ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator);
82ASSERT_READ_NUMBER_IS_FAST(std::string::iterator);
83ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator);
84ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator);
85ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator);
86ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator);
87ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator);
88ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator);
89ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator);
90ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator);
91ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator);
92ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator);
93ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator);
94ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator);
Davide Pesavento4ab3be22017-07-18 00:38:52 -040095ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator);
Junxiao Shi46a9bd32017-07-14 19:12:11 +000096ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator);
97ASSERT_READ_NUMBER_IS_SLOW(StreamIterator);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070098
99BOOST_AUTO_TEST_SUITE(VarNumber)
100
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000101// This check ensures readVarNumber and readType only require InputIterator concept and nothing
102// more. This function should compile, but should never be executed.
103void
104checkArchetype()
105{
106 boost::input_iterator_archetype<uint8_t> begin, end;
107 uint64_t number = readVarNumber(begin, end);
108 uint32_t type = readType(begin, end);;
109 readVarNumber(begin, end, number);
110 readType(begin, end, type);
111}
112
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700113static const uint8_t BUFFER[] = {
114 0x01, // == 1
115 0xfc, // == 252
116 0xfd, 0x00, 0xfd, // == 253
117 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000118 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700119};
120
121BOOST_AUTO_TEST_CASE(SizeOf)
122{
123 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
124 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
125 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
126 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000127 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700128}
129
130BOOST_AUTO_TEST_CASE(Write)
131{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100132 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700133
134 writeVarNumber(os, 1);
135 writeVarNumber(os, 252);
136 writeVarNumber(os, 253);
137 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000138 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700139
140 std::string buffer = os.str();
141 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
142
143 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
144 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
145 actual, actual + sizeof(BUFFER));
146}
147
148BOOST_AUTO_TEST_CASE(ReadFromBuffer)
149{
150 const uint8_t* begin;
151 uint64_t value;
152
153 begin = BUFFER;
154 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
155 begin = BUFFER;
156 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
157 BOOST_CHECK_EQUAL(value, 1);
158
159 begin = BUFFER + 1;
160 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
161 begin = BUFFER + 1;
162 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
163 BOOST_CHECK_EQUAL(value, 252);
164
165 begin = BUFFER + 2;
166 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
167 begin = BUFFER + 2;
168 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
169
170 begin = BUFFER + 2;
171 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
172 begin = BUFFER + 2;
173 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
174
175 begin = BUFFER + 2;
176 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
177 begin = BUFFER + 2;
178 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
179 BOOST_CHECK_EQUAL(value, 253);
180
181
182 begin = BUFFER + 5;
183 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
184 begin = BUFFER + 5;
185 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
186
187 begin = BUFFER + 5;
188 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
189 begin = BUFFER + 5;
190 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
191
192 begin = BUFFER + 5;
193 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
194 begin = BUFFER + 5;
195 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
196 BOOST_CHECK_EQUAL(value, 65536);
197
198 begin = BUFFER + 10;
199 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
200 begin = BUFFER + 10;
201 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
202
203 begin = BUFFER + 10;
204 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
205 begin = BUFFER + 10;
206 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
207
208 begin = BUFFER + 10;
209 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
210 begin = BUFFER + 10;
211 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000212 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700213}
214
215BOOST_AUTO_TEST_CASE(ReadFromStream)
216{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000217 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700218 uint64_t value;
219 {
220 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000221 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700222 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
223 }
224 {
225 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000226 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700227 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
228 BOOST_CHECK_EQUAL(value, 1);
229 }
230
231 {
232 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 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, 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));
240 BOOST_CHECK_EQUAL(value, 252);
241 }
242
243 {
244 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000245 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700246 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
247 }
248 {
249 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000250 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700251 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
252 }
253
254 {
255 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
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), false);
258 }
259 {
260 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000261 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700262 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
263 }
264
265 {
266 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000267 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700268 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
269 }
270 {
271 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000272 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700273 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
274 BOOST_CHECK_EQUAL(value, 253);
275 }
276
277 {
278 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
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 {
283 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
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 {
289 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
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), false);
292 }
293 {
294 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000295 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700296 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
297 }
298
299 {
300 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000301 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700302 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
303 }
304 {
305 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000306 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700307 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
308 BOOST_CHECK_EQUAL(value, 65536);
309 }
310
311 {
312 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
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 {
317 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
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 {
323 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
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), false);
326 }
327 {
328 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000329 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700330 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
331 }
332
333 {
334 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000335 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700336 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
337 }
338 {
339 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000340 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700341 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000342 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700343 }
344}
345
346BOOST_AUTO_TEST_SUITE_END() // VarNumber
347
348BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
349
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000350// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
351// This function should compile, but should never be executed.
352void
353checkArchetype()
354{
355 boost::input_iterator_archetype<uint8_t> begin, end;
356 readNonNegativeInteger(0, begin, end);
357}
358
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700359static const uint8_t BUFFER[] = {
360 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000361 0xff, // 255
362 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000363 0x01, 0x01, 0x01, 0x02, // 16843010
364 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700365};
366
367BOOST_AUTO_TEST_CASE(SizeOf)
368{
369 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000370 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
371 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
372 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700373 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000374 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
375 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
376 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700377}
378
379BOOST_AUTO_TEST_CASE(Write)
380{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100381 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700382
383 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000384 writeNonNegativeInteger(os, 255);
385 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000386 writeNonNegativeInteger(os, 16843010);
387 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700388
389 std::string buffer = os.str();
390 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
391
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700392 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
393 actual, actual + sizeof(BUFFER));
394}
395
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700396BOOST_AUTO_TEST_CASE(ReadFromBuffer)
397{
Junxiao Shic18aa192017-07-07 06:06:38 +0000398 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700399
400 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000401 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
402 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700403
404 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000405 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
406 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700407
Junxiao Shic18aa192017-07-07 06:06:38 +0000408 begin = BUFFER + 2;
409 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
410 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700411
Junxiao Shic18aa192017-07-07 06:06:38 +0000412 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000413 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000414 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700415
Junxiao Shic18aa192017-07-07 06:06:38 +0000416 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000417 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000418 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
419
420 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700421 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000422 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
423
424 // available buffer smaller than size
425 begin = BUFFER;
426 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
427 begin = BUFFER;
428 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
429 begin = BUFFER;
430 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
431 begin = BUFFER;
432 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700433}
434
435BOOST_AUTO_TEST_CASE(ReadFromStream)
436{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000437 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000438
439 {
440 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000441 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000442 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
443 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
444 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000445 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
446 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000447 BOOST_CHECK(begin == end);
448 }
449
450 // invalid size
451 {
452 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000453 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000454 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
455 }
456
457 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700458 {
459 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000460 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000461 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700462 }
463 {
464 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000465 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000466 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700467 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700468 {
469 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000470 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000471 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
472 }
473 {
474 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000475 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000476 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700477 }
478}
479
480BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
481
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800482BOOST_AUTO_TEST_SUITE(PrintHelpers)
483
484BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
485{
Davide Pesavento7e6f6f82017-10-31 18:05:28 -0400486 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(DigestSha256), "DigestSha256");
487 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithRsa), "SignatureSha256WithRsa");
488 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(SignatureSha256WithEcdsa), "SignatureSha256WithEcdsa");
489 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(static_cast<SignatureTypeValue>(200)), "Unknown Signature Type");
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800490}
491
Junxiao Shic18aa192017-07-07 06:06:38 +0000492BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800493
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100494BOOST_AUTO_TEST_SUITE_END() // TestTlv
495BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700496
497} // namespace tests
498} // namespace tlv
499} // namespace ndn