blob: 28f007b2deb47ad2c86ec81c373f0de3416ea7ca [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
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
Junxiao Shic18aa192017-07-07 06:06:38 +0000281 0xff, // 255
282 0x01, 0x02, // 258
283 0x01, 0x01, 0x01, 0x02, // 16843010LL
284 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02 // 72340172838076674LL
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700285};
286
287BOOST_AUTO_TEST_CASE(SizeOf)
288{
289 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(1), 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000290 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(253), 1);
291 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(255), 1);
292 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(256), 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700293 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(65536), 4);
294 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(16843009LL), 4);
295 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(4294967296LL), 8);
296 BOOST_CHECK_EQUAL(sizeOfNonNegativeInteger(72340172838076673LL), 8);
297}
298
299BOOST_AUTO_TEST_CASE(Write)
300{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100301 std::ostringstream os;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700302
303 writeNonNegativeInteger(os, 1);
Junxiao Shic18aa192017-07-07 06:06:38 +0000304 writeNonNegativeInteger(os, 255);
305 writeNonNegativeInteger(os, 258);
306 writeNonNegativeInteger(os, 16843010LL);
307 writeNonNegativeInteger(os, 72340172838076674LL);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700308
309 std::string buffer = os.str();
310 const uint8_t* actual = reinterpret_cast<const uint8_t*>(buffer.c_str());
311
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700312 BOOST_CHECK_EQUAL_COLLECTIONS(BUFFER, BUFFER + sizeof(BUFFER),
313 actual, actual + sizeof(BUFFER));
314}
315
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700316BOOST_AUTO_TEST_CASE(ReadFromBuffer)
317{
Junxiao Shic18aa192017-07-07 06:06:38 +0000318 const uint8_t* begin = nullptr;
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700319
320 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000321 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 1);
322 BOOST_CHECK_EQUAL(begin, BUFFER + 1);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700323
324 begin = BUFFER + 1;
Junxiao Shic18aa192017-07-07 06:06:38 +0000325 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, begin + 1), 255);
326 BOOST_CHECK_EQUAL(begin, BUFFER + 2);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700327
Junxiao Shic18aa192017-07-07 06:06:38 +0000328 begin = BUFFER + 2;
329 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, begin + 2), 258);
330 BOOST_CHECK_EQUAL(begin, BUFFER + 4);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700331
Junxiao Shic18aa192017-07-07 06:06:38 +0000332 begin = BUFFER + 4;
333 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, begin + 4), 16843010LL);
334 BOOST_CHECK_EQUAL(begin, BUFFER + 8);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700335
Junxiao Shic18aa192017-07-07 06:06:38 +0000336 begin = BUFFER + 8;
337 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, begin + 8), 72340172838076674LL);
338 BOOST_CHECK_EQUAL(begin, BUFFER + 16);
339
340 // invalid size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700341 begin = BUFFER;
Junxiao Shic18aa192017-07-07 06:06:38 +0000342 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, begin + 3), Error);
343
344 // available buffer smaller than size
345 begin = BUFFER;
346 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, begin + 0), Error);
347 begin = BUFFER;
348 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, begin + 1), Error);
349 begin = BUFFER;
350 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, begin + 3), Error);
351 begin = BUFFER;
352 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, begin + 7), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700353}
354
355BOOST_AUTO_TEST_CASE(ReadFromStream)
356{
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700357 Iterator end; // end of stream
Junxiao Shic18aa192017-07-07 06:06:38 +0000358
359 {
360 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
361 Iterator begin(stream);
362 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 1);
363 BOOST_CHECK_EQUAL(readNonNegativeInteger(1, begin, end), 255);
364 BOOST_CHECK_EQUAL(readNonNegativeInteger(2, begin, end), 258);
365 BOOST_CHECK_EQUAL(readNonNegativeInteger(4, begin, end), 16843010LL);
366 BOOST_CHECK_EQUAL(readNonNegativeInteger(8, begin, end), 72340172838076674LL);
367 BOOST_CHECK(begin == end);
368 }
369
370 // invalid size
371 {
372 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
373 Iterator begin(stream);
374 BOOST_CHECK_THROW(readNonNegativeInteger(3, begin, end), Error);
375 }
376
377 // available buffer smaller than size
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700378 {
379 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 0);
380 Iterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000381 BOOST_CHECK_THROW(readNonNegativeInteger(1, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700382 }
383 {
384 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 1);
385 Iterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000386 BOOST_CHECK_THROW(readNonNegativeInteger(2, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700387 }
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700388 {
389 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 3);
390 Iterator begin(stream);
Junxiao Shic18aa192017-07-07 06:06:38 +0000391 BOOST_CHECK_THROW(readNonNegativeInteger(4, begin, end), Error);
392 }
393 {
394 ArrayStream stream(reinterpret_cast<const char*>(BUFFER), 7);
395 Iterator begin(stream);
396 BOOST_CHECK_THROW(readNonNegativeInteger(8, begin, end), Error);
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700397 }
398}
399
400BOOST_AUTO_TEST_SUITE_END() // NonNegativeInteger
401
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800402BOOST_AUTO_TEST_SUITE(PrintHelpers)
403
404BOOST_AUTO_TEST_CASE(PrintSignatureTypeValue)
405{
406 SignatureTypeValue value = DigestSha256;
407 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "DigestSha256");
408
409 value = SignatureSha256WithRsa;
410 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithRsa");
411
412 value = SignatureSha256WithEcdsa;
413 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "SignatureSha256WithEcdsa");
414
415 value = static_cast<SignatureTypeValue>(-1);
416 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(value), "Unknown Signature Type");
417}
418
Junxiao Shic18aa192017-07-07 06:06:38 +0000419BOOST_AUTO_TEST_SUITE_END() // PrintHelpers
Alexander Afanasyev5f1820e2017-01-04 18:12:42 -0800420
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100421BOOST_AUTO_TEST_SUITE_END() // TestTlv
422BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyevd1de3972014-08-14 19:47:41 -0700423
424} // namespace tests
425} // namespace tlv
426} // namespace ndn