blob: 635aecf81b5f38297461641dce295cae6eb48afc [file] [log] [blame]
Alexander Afanasyevd1de3972014-08-14 19:47:41 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070026#include <boost/iostreams/stream.hpp>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include <boost/iostreams/device/array.hpp>
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070028
29namespace ndn {
30namespace tlv {
31namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Encoding)
34BOOST_AUTO_TEST_SUITE(TestTlv)
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070035
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036using ArrayStream = boost::iostreams::stream<boost::iostreams::array_source>;
37using Iterator = std::istream_iterator<uint8_t>;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070038
39BOOST_AUTO_TEST_SUITE(VarNumber)
40
41static const uint8_t BUFFER[] = {
42 0x01, // == 1
43 0xfc, // == 252
44 0xfd, 0x00, 0xfd, // == 253
45 0xfe, 0x00, 0x01, 0x00, 0x00, // == 65536
46 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00 // == 4294967296LL
47};
48
49BOOST_AUTO_TEST_CASE(SizeOf)
50{
51 BOOST_CHECK_EQUAL(sizeOfVarNumber(1), 1);
52 BOOST_CHECK_EQUAL(sizeOfVarNumber(252), 1);
53 BOOST_CHECK_EQUAL(sizeOfVarNumber(253), 3);
54 BOOST_CHECK_EQUAL(sizeOfVarNumber(65536), 5);
55 BOOST_CHECK_EQUAL(sizeOfVarNumber(4294967296LL), 9);
56}
57
58BOOST_AUTO_TEST_CASE(Write)
59{
Davide Pesaventoeee3e822016-11-26 19:19:34 +010060 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -070061
62 writeVarNumber(os, 1);
63 writeVarNumber(os, 252);
64 writeVarNumber(os, 253);
65 writeVarNumber(os, 65536);
66 writeVarNumber(os, 4294967296LL);
67
68 std::string buffer = os.str();
69 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
70
71 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
72 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
73 actual, actual + sizeof(BUFFER));
74}
75
76BOOST_AUTO_TEST_CASE(ReadFromBuffer)
77{
78 const uint8_t* begin;
79 uint64_t value;
80
81 begin = BUFFER;
82 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
83 begin = BUFFER;
84 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
85 BOOST_CHECK_EQUAL(value, 1);
86
87 begin = BUFFER + 1;
88 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), true);
89 begin = BUFFER + 1;
90 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 1));
91 BOOST_CHECK_EQUAL(value, 252);
92
93 begin = BUFFER + 2;
94 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
95 begin = BUFFER + 2;
96 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
97
98 begin = BUFFER + 2;
99 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 2, value), false);
100 begin = BUFFER + 2;
101 BOOST_CHECK_THROW(readVarNumber(begin, begin + 2), Error);
102
103 begin = BUFFER + 2;
104 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 3, value), true);
105 begin = BUFFER + 2;
106 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 3));
107 BOOST_CHECK_EQUAL(value, 253);
108
109
110 begin = BUFFER + 5;
111 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
112 begin = BUFFER + 5;
113 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
114
115 begin = BUFFER + 5;
116 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 4, value), false);
117 begin = BUFFER + 5;
118 BOOST_CHECK_THROW(readVarNumber(begin, begin + 4), Error);
119
120 begin = BUFFER + 5;
121 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 5, value), true);
122 begin = BUFFER + 5;
123 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 5));
124 BOOST_CHECK_EQUAL(value, 65536);
125
126 begin = BUFFER + 10;
127 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 1, value), false);
128 begin = BUFFER + 10;
129 BOOST_CHECK_THROW(readVarNumber(begin, begin + 1), Error);
130
131 begin = BUFFER + 10;
132 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 8, value), false);
133 begin = BUFFER + 10;
134 BOOST_CHECK_THROW(readVarNumber(begin, begin + 8), Error);
135
136 begin = BUFFER + 10;
137 BOOST_CHECK_EQUAL(readVarNumber(begin, begin + 9, value), true);
138 begin = BUFFER + 10;
139 BOOST_CHECK_NO_THROW(readVarNumber(begin, begin + 9));
140 BOOST_CHECK_EQUAL(value, 4294967296LL);
141}
142
143BOOST_AUTO_TEST_CASE(ReadFromStream)
144{
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700145 Iterator end; // end of stream
146 uint64_t value;
147 {
148 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
149 Iterator begin(stream);
150 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
151 }
152 {
153 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
154 Iterator begin(stream);
155 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
156 BOOST_CHECK_EQUAL(value, 1);
157 }
158
159 {
160 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
161 Iterator begin(stream);
162 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
163 }
164 {
165 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 1, 1);
166 Iterator begin(stream);
167 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
168 BOOST_CHECK_EQUAL(value, 252);
169 }
170
171 {
172 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
173 Iterator begin(stream);
174 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
175 }
176 {
177 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 1);
178 Iterator begin(stream);
179 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
180 }
181
182 {
183 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
184 Iterator begin(stream);
185 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
186 }
187 {
188 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 2);
189 Iterator begin(stream);
190 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
191 }
192
193 {
194 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
195 Iterator begin(stream);
196 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
197 }
198 {
199 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 2, 3);
200 Iterator begin(stream);
201 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
202 BOOST_CHECK_EQUAL(value, 253);
203 }
204
205 {
206 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
207 Iterator begin(stream);
208 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
209 }
210 {
211 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 1);
212 Iterator begin(stream);
213 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
214 }
215
216 {
217 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
218 Iterator begin(stream);
219 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
220 }
221 {
222 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 4);
223 Iterator begin(stream);
224 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
225 }
226
227 {
228 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
229 Iterator begin(stream);
230 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
231 }
232 {
233 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 5, 5);
234 Iterator begin(stream);
235 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
236 BOOST_CHECK_EQUAL(value, 65536);
237 }
238
239 {
240 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
241 Iterator begin(stream);
242 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
243 }
244 {
245 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 1);
246 Iterator begin(stream);
247 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
248 }
249
250 {
251 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
252 Iterator begin(stream);
253 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), false);
254 }
255 {
256 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 8);
257 Iterator begin(stream);
258 BOOST_CHECK_THROW(readVarNumber(begin, end), Error);
259 }
260
261 {
262 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
263 Iterator begin(stream);
264 BOOST_CHECK_EQUAL(readVarNumber(begin, end, value), true);
265 }
266 {
267 ArrayStream stream(reinterpret_cast<const char*>(BUFFER) + 10, 9);
268 Iterator begin(stream);
269 BOOST_CHECK_NO_THROW(readVarNumber(begin, end));
270 BOOST_CHECK_EQUAL(value, 4294967296LL);
271 }
272}
273
274BOOST_AUTO_TEST_SUITE_END() // VarNumber
275
276BOOST_AUTO_TEST_SUITE(NonNegativeInteger)
277
278static const uint8_t BUFFER[] = {
279 0x01, // 1
280 0x01, 0x01, // 257
281 0x01, 0x01, 0x01, 0x01, // 16843009LL
282 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 // 72340172838076673LL
283};
284
285BOOST_AUTO_TEST_CASE(SizeOf)
286{
287 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
288 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(252), 1);
289 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 2);
290 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(257), 2);
291 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
292 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009LL), 4);
293 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296LL), 8);
294 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673LL), 8);
295}
296
297BOOST_AUTO_TEST_CASE(Write)
298{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100299 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700300
301 writeNonNegativeInteger(os, 1);
302 writeNonNegativeInteger(os, 257);
303 writeNonNegativeInteger(os, 16843009LL);
304 writeNonNegativeInteger(os, 72340172838076673LL);
305
306 std::string buffer = os.str();
307 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
308
309 BOOST_CHECK_EQUAL(buffer.size(), sizeof(BUFFER));
310 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
311 actual, actual + sizeof(BUFFER));
312}
313
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700314BOOST_AUTO_TEST_CASE(ReadFromBuffer)
315{
316 const uint8_t* begin;
317 uint64_t value;
318
319 begin = BUFFER;
320 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, begin + 0), Error);
321 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, begin + 1));
322 BOOST_CHECK_EQUAL(value, 1);
323
324 begin = BUFFER + 1;
325 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, begin + 1), Error);
326 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, begin + 2));
327 BOOST_CHECK_EQUAL(value, 257);
328
329 begin = BUFFER + 3;
330 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, begin + 3), Error);
331 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, begin + 4));
332 BOOST_CHECK_EQUAL(value, 16843009LL);
333
334 begin = BUFFER + 7;
335 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, begin + 7), Error);
336 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, begin + 8));
337 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
338
339 begin = BUFFER;
340 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, begin + 3), Error);
341}
342
343BOOST_AUTO_TEST_CASE(ReadFromStream)
344{
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700345 Iterator end; // end of stream
346 uint64_t value;
347 {
348 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
349 Iterator begin(stream);
350 BOOST_CHECK_THROW(value = readNonNegativeInteger(1, begin, end), Error);
351 }
352 {
353 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
354 Iterator begin(stream);
355 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(1, begin, end));
356 BOOST_CHECK_EQUAL(value, 1);
357 }
358
359 {
360 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 1), 1);
361 Iterator begin(stream);
362 BOOST_CHECK_THROW(value = readNonNegativeInteger(2, begin, end), Error);
363 }
364 {
365 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 1), 2);
366 Iterator begin(stream);
367 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(2, begin, end));
368 BOOST_CHECK_EQUAL(value, 257);
369 }
370
371 {
372 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 3), 3);
373 Iterator begin(stream);
374 BOOST_CHECK_THROW(value = readNonNegativeInteger(4, begin, end), Error);
375 }
376 {
377 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 3), 4);
378 Iterator begin(stream);
379 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(4, begin, end));
380 BOOST_CHECK_EQUAL(value, 16843009LL);
381 }
382
383 {
384 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 7), 7);
385 Iterator begin(stream);
386 BOOST_CHECK_THROW(value = readNonNegativeInteger(8, begin, end), Error);
387 }
388 {
389 ArrayStream stream(reinterpret_cast<const char*>(BUFFER + 7), 8);
390 Iterator begin(stream);
391 BOOST_CHECK_NO_THROW(value = readNonNegativeInteger(8, begin, end));
392 BOOST_CHECK_EQUAL(value, 72340172838076673LL);
393 }
394
395 {
396 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
397 Iterator begin(stream);
398 BOOST_CHECK_THROW(value = readNonNegativeInteger(3, begin, end), Error);
399 }
400}
401
402BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
403
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100404BOOST_AUTO_TEST_SUITE_END() // TestTlv
405BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700406
407} // namespace tests
408} // namespace tlv
409} // namespace ndn