blob: 52b46adb0c02923e2664f1dbcbf765d2a6ad784f [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/*
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -08003 * Copyright (c) 2013-2017 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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010042using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
Junxiao Shi46a9bd32017-07-14 19:12:11 +000043using StreamIterator = std::istream_iterator<uint8_t>;
44
45#define ASSERT_READ_NUMBER_IS_FAST(T) \
46 static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \
47 # T " should use ReadNumberFast")
48#define ASSERT_READ_NUMBER_IS_SLOW(T) \
49 static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \
50 # T " should use ReadNumberSlow")
51
52ASSERT_READ_NUMBER_IS_FAST(const uint8_t*);
53ASSERT_READ_NUMBER_IS_FAST(uint8_t*);
54ASSERT_READ_NUMBER_IS_FAST(int8_t*);
55ASSERT_READ_NUMBER_IS_FAST(char*);
56ASSERT_READ_NUMBER_IS_FAST(unsigned char*);
57ASSERT_READ_NUMBER_IS_FAST(signed char*);
58ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]);
59ASSERT_READ_NUMBER_IS_FAST(uint8_t[]);
60ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]);
61ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]);
62using Uint8Array = std::array<uint8_t, 87>;
63ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator);
64ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator);
65using CharArray = std::array<char, 87>;
66ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator);
67ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator);
68ASSERT_READ_NUMBER_IS_FAST(std::string::iterator);
69ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator);
70ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator);
71ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator);
72ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator);
73ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator);
74ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator);
75ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator);
76ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator);
77ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator);
78ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator);
79ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator);
80ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator);
Davide Pesavento4ab3be22017-07-18 00:38:52 -040081ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator);
Junxiao Shi46a9bd32017-07-14 19:12:11 +000082ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator);
83ASSERT_READ_NUMBER_IS_SLOW(StreamIterator);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070084
85BOOST_AUTO_TEST_SUITE(VarNumber)
86
Junxiao Shi46a9bd32017-07-14 19:12:11 +000087// This check ensures readVarNumber and readType only require InputIterator concept and nothing
88// more. This function should compile, but should never be executed.
89void
90checkArchetype()
91{
92 boost::input_iterator_archetype<uint8_t> begin, end;
93 uint64_t number = readVarNumber(begin, end);
94 uint32_t type = readType(begin, end);;
95 readVarNumber(begin, end, number);
96 readType(begin, end, type);
97}
98
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070099static const uint8_t BUFFER[] = {
100 0x01, // == 1
101 0xfc, // == 252
102 0xfd, 0x00, 0xfd, // == 253
103 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000104 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700105};
106
107BOOST_AUTO_TEST_CASE(SizeOf)
108{
109 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
110 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
111 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
112 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000113 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700114}
115
116BOOST_AUTO_TEST_CASE(Write)
117{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100118 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700119
120 writeVarNumber(os, 1);
121 writeVarNumber(os, 252);
122 writeVarNumber(os, 253);
123 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000124 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700125
126 std::string buffer = os.str();
127 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
128
129 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
130 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
131 actual, actual + sizeof(BUFFER));
132}
133
134BOOST_AUTO_TEST_CASE(ReadFromBuffer)
135{
136 const uint8_t* begin;
137 uint64_t value;
138
139 begin = BUFFER;
140 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
141 begin = BUFFER;
142 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
143 BOOST_CHECK_EQUAL(value, 1);
144
145 begin = BUFFER + 1;
146 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
147 begin = BUFFER + 1;
148 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
149 BOOST_CHECK_EQUAL(value, 252);
150
151 begin = BUFFER + 2;
152 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
153 begin = BUFFER + 2;
154 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
155
156 begin = BUFFER + 2;
157 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
158 begin = BUFFER + 2;
159 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
160
161 begin = BUFFER + 2;
162 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
163 begin = BUFFER + 2;
164 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
165 BOOST_CHECK_EQUAL(value, 253);
166
167
168 begin = BUFFER + 5;
169 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
170 begin = BUFFER + 5;
171 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
172
173 begin = BUFFER + 5;
174 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
175 begin = BUFFER + 5;
176 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
177
178 begin = BUFFER + 5;
179 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
180 begin = BUFFER + 5;
181 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
182 BOOST_CHECK_EQUAL(value, 65536);
183
184 begin = BUFFER + 10;
185 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
186 begin = BUFFER + 10;
187 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
188
189 begin = BUFFER + 10;
190 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
191 begin = BUFFER + 10;
192 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
193
194 begin = BUFFER + 10;
195 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
196 begin = BUFFER + 10;
197 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000198 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700199}
200
201BOOST_AUTO_TEST_CASE(ReadFromStream)
202{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000203 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700204 uint64_t value;
205 {
206 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000207 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700208 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
209 }
210 {
211 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000212 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700213 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
214 BOOST_CHECK_EQUAL(value, 1);
215 }
216
217 {
218 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000219 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700220 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
221 }
222 {
223 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000224 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700225 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
226 BOOST_CHECK_EQUAL(value, 252);
227 }
228
229 {
230 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000231 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700232 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
233 }
234 {
235 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000236 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700237 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
238 }
239
240 {
241 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000242 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700243 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
244 }
245 {
246 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000247 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700248 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
249 }
250
251 {
252 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000253 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700254 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
255 }
256 {
257 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000258 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700259 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
260 BOOST_CHECK_EQUAL(value, 253);
261 }
262
263 {
264 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000265 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700266 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
267 }
268 {
269 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000270 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700271 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
272 }
273
274 {
275 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000276 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700277 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
278 }
279 {
280 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000281 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700282 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
283 }
284
285 {
286 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000287 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700288 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
289 }
290 {
291 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000292 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700293 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
294 BOOST_CHECK_EQUAL(value, 65536);
295 }
296
297 {
298 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000299 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700300 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
301 }
302 {
303 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000304 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700305 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
306 }
307
308 {
309 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000310 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700311 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
312 }
313 {
314 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000315 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700316 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
317 }
318
319 {
320 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000321 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700322 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
323 }
324 {
325 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000326 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700327 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000328 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700329 }
330}
331
332BOOST_AUTO_TEST_SUITE_END() // VarNumber
333
334BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
335
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000336// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
337// This function should compile, but should never be executed.
338void
339checkArchetype()
340{
341 boost::input_iterator_archetype<uint8_t> begin, end;
342 readNonNegativeInteger(0, begin, end);
343}
344
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700345static const uint8_t BUFFER[] = {
346 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000347 0xff, // 255
348 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000349 0x01, 0x01, 0x01, 0x02, // 16843010
350 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700351};
352
353BOOST_AUTO_TEST_CASE(SizeOf)
354{
355 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000356 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
357 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
358 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700359 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000360 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
361 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
362 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700363}
364
365BOOST_AUTO_TEST_CASE(Write)
366{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100367 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700368
369 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000370 writeNonNegativeInteger(os, 255);
371 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000372 writeNonNegativeInteger(os, 16843010);
373 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700374
375 std::string buffer = os.str();
376 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
377
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700378 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
379 actual, actual + sizeof(BUFFER));
380}
381
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700382BOOST_AUTO_TEST_CASE(ReadFromBuffer)
383{
Junxiao Shic18aa192017-07-07 06:06:38 +0000384 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700385
386 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000387 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
388 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700389
390 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000391 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
392 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700393
Junxiao Shic18aa192017-07-07 06:06:38 +0000394 begin = BUFFER + 2;
395 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
396 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700397
Junxiao Shic18aa192017-07-07 06:06:38 +0000398 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000399 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000400 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700401
Junxiao Shic18aa192017-07-07 06:06:38 +0000402 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000403 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000404 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
405
406 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700407 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000408 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
409
410 // available buffer smaller than size
411 begin = BUFFER;
412 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
413 begin = BUFFER;
414 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
415 begin = BUFFER;
416 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
417 begin = BUFFER;
418 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700419}
420
421BOOST_AUTO_TEST_CASE(ReadFromStream)
422{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000423 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000424
425 {
426 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000427 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000428 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
429 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
430 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000431 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
432 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000433 BOOST_CHECK(begin == end);
434 }
435
436 // invalid size
437 {
438 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000439 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000440 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
441 }
442
443 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700444 {
445 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000446 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000447 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700448 }
449 {
450 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000451 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000452 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700453 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700454 {
455 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000456 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000457 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
458 }
459 {
460 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000461 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000462 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700463 }
464}
465
466BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
467
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800468BOOST_AUTO_TEST_SUITE(PrintHelpers)
469
470BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
471{
472 SignatureTypeValue value = DigestSha256;
473 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
474
475 value = SignatureSha256WithRsa;
476 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
477
478 value = SignatureSha256WithEcdsa;
479 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
480
481 value = static_cast<SignatureTypeValue>(-1);
482 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
483}
484
Junxiao Shic18aa192017-07-07 06:06:38 +0000485BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800486
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100487BOOST_AUTO_TEST_SUITE_END() // TestTlv
488BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700489
490} // namespace tests
491} // namespace tlv
492} // namespace ndn