blob: 8138e8a2af3a84d97329cde0828c4df20488c8c6 [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>
Junxiao Shi46a9bd32017-07-14 19:12:11 +000029#include <boost/concept_archetype.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070030#include <boost/iostreams/stream.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031#include <boost/iostreams/device/array.hpp>
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080032#include <boost/lexical_cast.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070033
34namespace ndn {
35namespace tlv {
36namespace tests {
37
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038BOOST_AUTO_TEST_SUITE(Encoding)
39BOOST_AUTO_TEST_SUITE(TestTlv)
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070040
Davide Pesaventoeee3e822016-11-26 19:19:34 +010041using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
Junxiao Shi46a9bd32017-07-14 19:12:11 +000042using StreamIterator = std::istream_iterator<uint8_t>;
43
44#define ASSERT_READ_NUMBER_IS_FAST(T) \
45 static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \
46 # T " should use ReadNumberFast")
47#define ASSERT_READ_NUMBER_IS_SLOW(T) \
48 static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \
49 # T " should use ReadNumberSlow")
50
51ASSERT_READ_NUMBER_IS_FAST(const uint8_t*);
52ASSERT_READ_NUMBER_IS_FAST(uint8_t*);
53ASSERT_READ_NUMBER_IS_FAST(int8_t*);
54ASSERT_READ_NUMBER_IS_FAST(char*);
55ASSERT_READ_NUMBER_IS_FAST(unsigned char*);
56ASSERT_READ_NUMBER_IS_FAST(signed char*);
57ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]);
58ASSERT_READ_NUMBER_IS_FAST(uint8_t[]);
59ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]);
60ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]);
61using Uint8Array = std::array<uint8_t, 87>;
62ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator);
63ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator);
64using CharArray = std::array<char, 87>;
65ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator);
66ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator);
67ASSERT_READ_NUMBER_IS_FAST(std::string::iterator);
68ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator);
69ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator);
70ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator);
71ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator);
72ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator);
73ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator);
74ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator);
75ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator);
76ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator);
77ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator);
78ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator);
79ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator);
Davide Pesavento4ab3be22017-07-18 00:38:52 -040080ASSERT_READ_NUMBER_IS_SLOW(std::deque<uint8_t>::iterator);
Junxiao Shi46a9bd32017-07-14 19:12:11 +000081ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator);
82ASSERT_READ_NUMBER_IS_SLOW(StreamIterator);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070083
84BOOST_AUTO_TEST_SUITE(VarNumber)
85
Junxiao Shi46a9bd32017-07-14 19:12:11 +000086// This check ensures readVarNumber and readType only require InputIterator concept and nothing
87// more. This function should compile, but should never be executed.
88void
89checkArchetype()
90{
91 boost::input_iterator_archetype<uint8_t> begin, end;
92 uint64_t number = readVarNumber(begin, end);
93 uint32_t type = readType(begin, end);;
94 readVarNumber(begin, end, number);
95 readType(begin, end, type);
96}
97
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070098static const uint8_t BUFFER[] = {
99 0x01, // == 1
100 0xfc, // == 252
101 0xfd, 0x00, 0xfd, // == 253
102 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000103 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700104};
105
106BOOST_AUTO_TEST_CASE(SizeOf)
107{
108 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
109 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
110 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
111 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000112 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700113}
114
115BOOST_AUTO_TEST_CASE(Write)
116{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100117 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700118
119 writeVarNumber(os, 1);
120 writeVarNumber(os, 252);
121 writeVarNumber(os, 253);
122 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000123 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700124
125 std::string buffer = os.str();
126 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
127
128 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
129 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
130 actual, actual + sizeof(BUFFER));
131}
132
133BOOST_AUTO_TEST_CASE(ReadFromBuffer)
134{
135 const uint8_t* begin;
136 uint64_t value;
137
138 begin = BUFFER;
139 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
140 begin = BUFFER;
141 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
142 BOOST_CHECK_EQUAL(value, 1);
143
144 begin = BUFFER + 1;
145 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
146 begin = BUFFER + 1;
147 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
148 BOOST_CHECK_EQUAL(value, 252);
149
150 begin = BUFFER + 2;
151 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
152 begin = BUFFER + 2;
153 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
154
155 begin = BUFFER + 2;
156 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
157 begin = BUFFER + 2;
158 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
159
160 begin = BUFFER + 2;
161 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
162 begin = BUFFER + 2;
163 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
164 BOOST_CHECK_EQUAL(value, 253);
165
166
167 begin = BUFFER + 5;
168 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
169 begin = BUFFER + 5;
170 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
171
172 begin = BUFFER + 5;
173 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
174 begin = BUFFER + 5;
175 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
176
177 begin = BUFFER + 5;
178 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
179 begin = BUFFER + 5;
180 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
181 BOOST_CHECK_EQUAL(value, 65536);
182
183 begin = BUFFER + 10;
184 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
185 begin = BUFFER + 10;
186 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
187
188 begin = BUFFER + 10;
189 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
190 begin = BUFFER + 10;
191 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
192
193 begin = BUFFER + 10;
194 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
195 begin = BUFFER + 10;
196 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000197 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700198}
199
200BOOST_AUTO_TEST_CASE(ReadFromStream)
201{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000202 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700203 uint64_t value;
204 {
205 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000206 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700207 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
208 }
209 {
210 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000211 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700212 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
213 BOOST_CHECK_EQUAL(value, 1);
214 }
215
216 {
217 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000218 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700219 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
220 }
221 {
222 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000223 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700224 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
225 BOOST_CHECK_EQUAL(value, 252);
226 }
227
228 {
229 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000230 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700231 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
232 }
233 {
234 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000235 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700236 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
237 }
238
239 {
240 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000241 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700242 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
243 }
244 {
245 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000246 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700247 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
248 }
249
250 {
251 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000252 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700253 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
254 }
255 {
256 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000257 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700258 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
259 BOOST_CHECK_EQUAL(value, 253);
260 }
261
262 {
263 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000264 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700265 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
266 }
267 {
268 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000269 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700270 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
271 }
272
273 {
274 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000275 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700276 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
277 }
278 {
279 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000280 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700281 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
282 }
283
284 {
285 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000286 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700287 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
288 }
289 {
290 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000291 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700292 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
293 BOOST_CHECK_EQUAL(value, 65536);
294 }
295
296 {
297 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000298 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700299 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
300 }
301 {
302 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000303 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700304 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
305 }
306
307 {
308 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000309 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700310 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
311 }
312 {
313 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000314 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700315 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
316 }
317
318 {
319 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000320 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700321 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
322 }
323 {
324 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000325 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700326 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000327 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700328 }
329}
330
331BOOST_AUTO_TEST_SUITE_END() // VarNumber
332
333BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
334
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000335// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
336// This function should compile, but should never be executed.
337void
338checkArchetype()
339{
340 boost::input_iterator_archetype<uint8_t> begin, end;
341 readNonNegativeInteger(0, begin, end);
342}
343
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700344static const uint8_t BUFFER[] = {
345 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000346 0xff, // 255
347 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000348 0x01, 0x01, 0x01, 0x02, // 16843010
349 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700350};
351
352BOOST_AUTO_TEST_CASE(SizeOf)
353{
354 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000355 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
356 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
357 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700358 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000359 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
360 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
361 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700362}
363
364BOOST_AUTO_TEST_CASE(Write)
365{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100366 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700367
368 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000369 writeNonNegativeInteger(os, 255);
370 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000371 writeNonNegativeInteger(os, 16843010);
372 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700373
374 std::string buffer = os.str();
375 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
376
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700377 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
378 actual, actual + sizeof(BUFFER));
379}
380
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700381BOOST_AUTO_TEST_CASE(ReadFromBuffer)
382{
Junxiao Shic18aa192017-07-07 06:06:38 +0000383 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700384
385 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000386 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
387 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700388
389 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000390 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
391 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700392
Junxiao Shic18aa192017-07-07 06:06:38 +0000393 begin = BUFFER + 2;
394 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
395 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700396
Junxiao Shic18aa192017-07-07 06:06:38 +0000397 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000398 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000399 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700400
Junxiao Shic18aa192017-07-07 06:06:38 +0000401 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000402 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000403 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
404
405 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700406 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000407 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
408
409 // available buffer smaller than size
410 begin = BUFFER;
411 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
412 begin = BUFFER;
413 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
414 begin = BUFFER;
415 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
416 begin = BUFFER;
417 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700418}
419
420BOOST_AUTO_TEST_CASE(ReadFromStream)
421{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000422 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000423
424 {
425 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000426 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000427 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
428 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
429 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000430 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
431 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000432 BOOST_CHECK(begin == end);
433 }
434
435 // invalid size
436 {
437 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000438 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000439 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
440 }
441
442 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700443 {
444 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000445 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000446 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700447 }
448 {
449 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000450 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000451 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700452 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700453 {
454 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000455 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000456 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
457 }
458 {
459 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000460 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000461 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700462 }
463}
464
465BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
466
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800467BOOST_AUTO_TEST_SUITE(PrintHelpers)
468
469BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
470{
471 SignatureTypeValue value = DigestSha256;
472 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
473
474 value = SignatureSha256WithRsa;
475 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
476
477 value = SignatureSha256WithEcdsa;
478 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
479
480 value = static_cast<SignatureTypeValue>(-1);
481 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
482}
483
Junxiao Shic18aa192017-07-07 06:06:38 +0000484BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800485
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100486BOOST_AUTO_TEST_SUITE_END() // TestTlv
487BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700488
489} // namespace tests
490} // namespace tlv
491} // namespace ndn