blob: edbb98f24fb2856e4ed6bd7eb1422eed5547c268 [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
Junxiao Shi46a9bd32017-07-14 19:12:11 +000026#include <boost/concept_archetype.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070027#include <boost/iostreams/stream.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028#include <boost/iostreams/device/array.hpp>
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -080029#include <boost/lexical_cast.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070030
31namespace ndn {
32namespace tlv {
33namespace tests {
34
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(Encoding)
36BOOST_AUTO_TEST_SUITE(TestTlv)
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070037
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
Junxiao Shi46a9bd32017-07-14 19:12:11 +000039using StreamIterator = std::istream_iterator<uint8_t>;
40
41#define ASSERT_READ_NUMBER_IS_FAST(T) \
42 static_assert(std::is_base_of<detail::ReadNumberFast<T>, detail::ReadNumber<T>>::value, \
43 # T " should use ReadNumberFast")
44#define ASSERT_READ_NUMBER_IS_SLOW(T) \
45 static_assert(std::is_base_of<detail::ReadNumberSlow<T>, detail::ReadNumber<T>>::value, \
46 # T " should use ReadNumberSlow")
47
48ASSERT_READ_NUMBER_IS_FAST(const uint8_t*);
49ASSERT_READ_NUMBER_IS_FAST(uint8_t*);
50ASSERT_READ_NUMBER_IS_FAST(int8_t*);
51ASSERT_READ_NUMBER_IS_FAST(char*);
52ASSERT_READ_NUMBER_IS_FAST(unsigned char*);
53ASSERT_READ_NUMBER_IS_FAST(signed char*);
54ASSERT_READ_NUMBER_IS_FAST(const uint8_t[]);
55ASSERT_READ_NUMBER_IS_FAST(uint8_t[]);
56ASSERT_READ_NUMBER_IS_FAST(const uint8_t[12]);
57ASSERT_READ_NUMBER_IS_FAST(uint8_t[12]);
58using Uint8Array = std::array<uint8_t, 87>;
59ASSERT_READ_NUMBER_IS_FAST(Uint8Array::const_iterator);
60ASSERT_READ_NUMBER_IS_FAST(Uint8Array::iterator);
61using CharArray = std::array<char, 87>;
62ASSERT_READ_NUMBER_IS_FAST(CharArray::iterator);
63ASSERT_READ_NUMBER_IS_FAST(std::string::const_iterator);
64ASSERT_READ_NUMBER_IS_FAST(std::string::iterator);
65ASSERT_READ_NUMBER_IS_FAST(Buffer::const_iterator);
66ASSERT_READ_NUMBER_IS_FAST(Buffer::iterator);
67ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::const_iterator);
68ASSERT_READ_NUMBER_IS_FAST(std::vector<uint8_t>::iterator);
69ASSERT_READ_NUMBER_IS_FAST(std::vector<int8_t>::iterator);
70ASSERT_READ_NUMBER_IS_FAST(std::vector<char>::iterator);
71ASSERT_READ_NUMBER_IS_FAST(std::vector<unsigned char>::iterator);
72ASSERT_READ_NUMBER_IS_FAST(std::vector<signed char>::iterator);
73ASSERT_READ_NUMBER_IS_SLOW(std::vector<bool>::iterator);
74ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint16_t>::iterator);
75ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint32_t>::iterator);
76ASSERT_READ_NUMBER_IS_SLOW(std::vector<uint64_t>::iterator);
77ASSERT_READ_NUMBER_IS_SLOW(std::list<uint8_t>::iterator);
78ASSERT_READ_NUMBER_IS_SLOW(StreamIterator);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070079
80BOOST_AUTO_TEST_SUITE(VarNumber)
81
Junxiao Shi46a9bd32017-07-14 19:12:11 +000082// This check ensures readVarNumber and readType only require InputIterator concept and nothing
83// more. This function should compile, but should never be executed.
84void
85checkArchetype()
86{
87 boost::input_iterator_archetype<uint8_t> begin, end;
88 uint64_t number = readVarNumber(begin, end);
89 uint32_t type = readType(begin, end);;
90 readVarNumber(begin, end, number);
91 readType(begin, end, type);
92}
93
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070094static const uint8_t BUFFER[] = {
95 0x01, // == 1
96 0xfc, // == 252
97 0xfd, 0x00, 0xfd, // == 253
98 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
Junxiao Shi46a9bd32017-07-14 19:12:11 +000099 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700100};
101
102BOOST_AUTO_TEST_CASE(SizeOf)
103{
104 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
105 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
106 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
107 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000108 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296), 9);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700109}
110
111BOOST_AUTO_TEST_CASE(Write)
112{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100113 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700114
115 writeVarNumber(os, 1);
116 writeVarNumber(os, 252);
117 writeVarNumber(os, 253);
118 writeVarNumber(os, 65536);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000119 writeVarNumber(os, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700120
121 std::string buffer = os.str();
122 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
123
124 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
125 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
126 actual, actual + sizeof(BUFFER));
127}
128
129BOOST_AUTO_TEST_CASE(ReadFromBuffer)
130{
131 const uint8_t* begin;
132 uint64_t value;
133
134 begin = BUFFER;
135 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
136 begin = BUFFER;
137 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
138 BOOST_CHECK_EQUAL(value, 1);
139
140 begin = BUFFER + 1;
141 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
142 begin = BUFFER + 1;
143 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
144 BOOST_CHECK_EQUAL(value, 252);
145
146 begin = BUFFER + 2;
147 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
148 begin = BUFFER + 2;
149 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
150
151 begin = BUFFER + 2;
152 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
153 begin = BUFFER + 2;
154 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
155
156 begin = BUFFER + 2;
157 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
158 begin = BUFFER + 2;
159 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
160 BOOST_CHECK_EQUAL(value, 253);
161
162
163 begin = BUFFER + 5;
164 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
165 begin = BUFFER + 5;
166 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
167
168 begin = BUFFER + 5;
169 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
170 begin = BUFFER + 5;
171 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
172
173 begin = BUFFER + 5;
174 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
175 begin = BUFFER + 5;
176 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
177 BOOST_CHECK_EQUAL(value, 65536);
178
179 begin = BUFFER + 10;
180 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
181 begin = BUFFER + 10;
182 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
183
184 begin = BUFFER + 10;
185 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
186 begin = BUFFER + 10;
187 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
188
189 begin = BUFFER + 10;
190 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
191 begin = BUFFER + 10;
192 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000193 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700194}
195
196BOOST_AUTO_TEST_CASE(ReadFromStream)
197{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000198 StreamIterator end; // end of stream
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700199 uint64_t value;
200 {
201 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000202 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700203 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
204 }
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_NO_THROW(readVarNumber(begin, end));
209 BOOST_CHECK_EQUAL(value, 1);
210 }
211
212 {
213 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000214 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700215 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
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_NO_THROW(readVarNumber(begin, end));
221 BOOST_CHECK_EQUAL(value, 252);
222 }
223
224 {
225 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000226 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700227 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
233 }
234
235 {
236 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000237 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700238 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
244 }
245
246 {
247 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000248 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700249 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
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_NO_THROW(readVarNumber(begin, end));
255 BOOST_CHECK_EQUAL(value, 253);
256 }
257
258 {
259 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000260 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700261 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
267 }
268
269 {
270 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000271 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700272 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
278 }
279
280 {
281 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000282 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700283 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
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_NO_THROW(readVarNumber(begin, end));
289 BOOST_CHECK_EQUAL(value, 65536);
290 }
291
292 {
293 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000294 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700295 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
301 }
302
303 {
304 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000305 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700306 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
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_THROW(readVarNumber(begin, end), Error);
312 }
313
314 {
315 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000316 StreamIterator begin(stream);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700317 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
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_NO_THROW(readVarNumber(begin, end));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000323 BOOST_CHECK_EQUAL(value, 4294967296);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700324 }
325}
326
327BOOST_AUTO_TEST_SUITE_END() // VarNumber
328
329BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
330
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000331// This check ensures readNonNegativeInteger only requires InputIterator concept and nothing more.
332// This function should compile, but should never be executed.
333void
334checkArchetype()
335{
336 boost::input_iterator_archetype<uint8_t> begin, end;
337 readNonNegativeInteger(0, begin, end);
338}
339
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700340static const uint8_t BUFFER[] = {
341 0x01, // 1
Junxiao Shic18aa192017-07-07 06:06:38 +0000342 0xff, // 255
343 0x01, 0x02, // 258
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000344 0x01, 0x01, 0x01, 0x02, // 16843010
345 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700346};
347
348BOOST_AUTO_TEST_CASE(SizeOf)
349{
350 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000351 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
352 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
353 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700354 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000355 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009), 4);
356 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296), 8);
357 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673), 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700358}
359
360BOOST_AUTO_TEST_CASE(Write)
361{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100362 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700363
364 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000365 writeNonNegativeInteger(os, 255);
366 writeNonNegativeInteger(os, 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000367 writeNonNegativeInteger(os, 16843010);
368 writeNonNegativeInteger(os, 72340172838076674);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700369
370 std::string buffer = os.str();
371 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
372
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700373 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
374 actual, actual + sizeof(BUFFER));
375}
376
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700377BOOST_AUTO_TEST_CASE(ReadFromBuffer)
378{
Junxiao Shic18aa192017-07-07 06:06:38 +0000379 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700380
381 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000382 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
383 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700384
385 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000386 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
387 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700388
Junxiao Shic18aa192017-07-07 06:06:38 +0000389 begin = BUFFER + 2;
390 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
391 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700392
Junxiao Shic18aa192017-07-07 06:06:38 +0000393 begin = BUFFER + 4;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000394 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010);
Junxiao Shic18aa192017-07-07 06:06:38 +0000395 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700396
Junxiao Shic18aa192017-07-07 06:06:38 +0000397 begin = BUFFER + 8;
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000398 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000399 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
400
401 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700402 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000403 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
404
405 // available buffer smaller than size
406 begin = BUFFER;
407 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
408 begin = BUFFER;
409 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
410 begin = BUFFER;
411 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
412 begin = BUFFER;
413 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700414}
415
416BOOST_AUTO_TEST_CASE(ReadFromStream)
417{
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000418 StreamIterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000419
420 {
421 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000422 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000423 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
424 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
425 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000426 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010);
427 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674);
Junxiao Shic18aa192017-07-07 06:06:38 +0000428 BOOST_CHECK(begin == end);
429 }
430
431 // invalid size
432 {
433 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000434 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000435 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
436 }
437
438 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700439 {
440 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000441 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000442 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700443 }
444 {
445 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000446 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000447 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700448 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700449 {
450 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000451 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000452 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
453 }
454 {
455 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
Junxiao Shi46a9bd32017-07-14 19:12:11 +0000456 StreamIterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000457 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700458 }
459}
460
461BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
462
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800463BOOST_AUTO_TEST_SUITE(PrintHelpers)
464
465BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
466{
467 SignatureTypeValue value = DigestSha256;
468 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
469
470 value = SignatureSha256WithRsa;
471 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
472
473 value = SignatureSha256WithEcdsa;
474 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
475
476 value = static_cast<SignatureTypeValue>(-1);
477 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
478}
479
Junxiao Shic18aa192017-07-07 06:06:38 +0000480BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800481
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100482BOOST_AUTO_TEST_SUITE_END() // TestTlv
483BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700484
485} // namespace tests
486} // namespace tlv
487} // namespace ndn