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