blob: fea4714581ac4ca9a4c0b18fd77346a11122a9ca [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shidb7464d2017-07-13 03:11:17 +00002/*
Davide Pesavento46cdf902020-06-08 16:38:23 -04003 * Copyright (c) 2013-2020 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyev233750e2014-02-16 00:50:07 -080020 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/encoding/block.hpp"
23#include "ndn-cxx/encoding/block-helpers.hpp"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26
Junxiao Shi72c0c642018-04-20 15:41:09 +000027#include <boost/lexical_cast.hpp>
Davide Pesavento6b330402019-04-24 00:14:01 -040028#include <boost/mpl/vector.hpp>
29
Junxiao Shidc4277a2017-07-17 11:34:02 +000030#include <cstring>
31#include <sstream>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032
Alexander Afanasyev233750e2014-02-16 00:50:07 -080033namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080034namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080035
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Encoding)
37BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080038
Junxiao Shidc4277a2017-07-17 11:34:02 +000039BOOST_AUTO_TEST_SUITE(Construction)
40
41static const uint8_t TEST_BUFFER[] = {
Davide Pesavento6b330402019-04-24 00:14:01 -040042 0x42, 0x01, 0xfa,
43 0x01, 0x01, 0xfb,
44 0xfe, 0xff, 0xff, 0xff, 0xff, 0x00, // bug #4726
Alexander Afanasyev74633892015-02-08 18:08:46 -080045};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080046
Davide Pesavento6b330402019-04-24 00:14:01 -040047BOOST_AUTO_TEST_CASE(Default)
Alexander Afanasyev74633892015-02-08 18:08:46 -080048{
Junxiao Shidc4277a2017-07-17 11:34:02 +000049 Block b;
Davide Pesavento6b330402019-04-24 00:14:01 -040050
51 BOOST_CHECK_EQUAL(b.isValid(), false);
Davide Pesavento6b330402019-04-24 00:14:01 -040052 BOOST_CHECK_EQUAL(b.type(), tlv::Invalid);
53 BOOST_CHECK_EQUAL(b.hasValue(), false);
54 BOOST_CHECK_EQUAL(b.value_size(), 0);
55 BOOST_CHECK(b.value() == nullptr);
56
57 BOOST_CHECK_THROW(b.size(), Block::Error);
58 BOOST_CHECK_THROW(b.begin(), Block::Error);
59 BOOST_CHECK_THROW(b.end(), Block::Error);
60 BOOST_CHECK_THROW(b.wire(), Block::Error);
61 BOOST_CHECK_THROW(b.blockFromValue(), Block::Error);
Alexander Afanasyev74633892015-02-08 18:08:46 -080062}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070063
Junxiao Shidc4277a2017-07-17 11:34:02 +000064BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080065{
Junxiao Shidc4277a2017-07-17 11:34:02 +000066 EncodingBuffer encoder;
Davide Pesavento5c803b92019-02-02 19:23:48 -050067 BOOST_CHECK_THROW(Block{encoder}, tlv::Error);
68
69 const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14};
Junxiao Shidc4277a2017-07-17 11:34:02 +000070 size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE));
71 encoder.prependVarNumber(length);
72 encoder.prependVarNumber(0xe0);
73
74 Block b = encoder.block();
75 BOOST_CHECK_EQUAL(b.type(), 0xe0);
76 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
77 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
78 VALUE, VALUE + sizeof(VALUE));
79
80 b = Block(encoder);
81 BOOST_CHECK_EQUAL(b.type(), 0xe0);
82 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
83 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
84 VALUE, VALUE + sizeof(VALUE));
Alexander Afanasyev74633892015-02-08 18:08:46 -080085}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070086
Davide Pesaventoeee3e822016-11-26 19:19:34 +010087BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -070088{
Davide Pesavento5c803b92019-02-02 19:23:48 -050089 const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
90 Block block(BUFFER, sizeof(BUFFER));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070091
92 Block derivedBlock(block, block.begin(), block.end());
93 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
94 BOOST_CHECK(derivedBlock == block); // blocks should match
95
96 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
97 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
Davide Pesavento5c803b92019-02-02 19:23:48 -050098 BOOST_CHECK(derivedBlock == Block(BUFFER + 2, 3));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070099
Davide Pesavento5c803b92019-02-02 19:23:48 -0500100 Buffer otherBuffer(BUFFER, sizeof(BUFFER));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000101 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument);
102 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument);
103 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument);
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700104}
105
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100106BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -0700107{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000108 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700109 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
110 };
111
Junxiao Shidc4277a2017-07-17 11:34:02 +0000112 Block b1(BUFFER, sizeof(BUFFER));
Eric Newberry6235aac2015-08-13 16:03:47 -0700113
Junxiao Shidc4277a2017-07-17 11:34:02 +0000114 Block b2(b1, b1.begin(), b1.end());
115 auto buf2 = b2.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700116
Junxiao Shidc4277a2017-07-17 11:34:02 +0000117 b1.parse();
118 b1.remove(tlv::Name);
119 b1.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700120
Junxiao Shidc4277a2017-07-17 11:34:02 +0000121 b2.parse();
Eric Newberry6235aac2015-08-13 16:03:47 -0700122
Junxiao Shidc4277a2017-07-17 11:34:02 +0000123 BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER));
124 BOOST_CHECK_EQUAL(buf2, b2.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700125}
126
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100127BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700128{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000129 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700130 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
131 };
132
Junxiao Shidc4277a2017-07-17 11:34:02 +0000133 Block b1(BUFFER, sizeof(BUFFER));
134 auto buf1 = b1.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700135
Junxiao Shidc4277a2017-07-17 11:34:02 +0000136 Block b2(b1, b1.begin(), b1.end());
Eric Newberry6235aac2015-08-13 16:03:47 -0700137
Junxiao Shidc4277a2017-07-17 11:34:02 +0000138 b2.parse();
139 b2.remove(tlv::Name);
140 b2.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700141
Junxiao Shidc4277a2017-07-17 11:34:02 +0000142 b1.parse();
143 BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER));
144 BOOST_CHECK_EQUAL(buf1, b1.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700145}
146
Junxiao Shidc4277a2017-07-17 11:34:02 +0000147BOOST_AUTO_TEST_CASE(FromType)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800148{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000149 Block b1(4);
Davide Pesavento6b330402019-04-24 00:14:01 -0400150 BOOST_CHECK_EQUAL(b1.isValid(), true);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000151 BOOST_CHECK_EQUAL(b1.type(), 4);
152 BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH
Davide Pesavento5c803b92019-02-02 19:23:48 -0500153 BOOST_CHECK_EQUAL(b1.hasValue(), false);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000154 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800155
Junxiao Shidc4277a2017-07-17 11:34:02 +0000156 Block b2(258);
Davide Pesavento6b330402019-04-24 00:14:01 -0400157 BOOST_CHECK_EQUAL(b2.isValid(), true);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000158 BOOST_CHECK_EQUAL(b2.type(), 258);
159 BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH
Davide Pesavento5c803b92019-02-02 19:23:48 -0500160 BOOST_CHECK_EQUAL(b2.hasValue(), false);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000161 BOOST_CHECK_EQUAL(b2.value_size(), 0);
Davide Pesavento6b330402019-04-24 00:14:01 -0400162
163 Block b3(tlv::Invalid);
164 BOOST_CHECK_EQUAL(b3.isValid(), false);
165 BOOST_CHECK_EQUAL(b3.type(), tlv::Invalid);
166 BOOST_CHECK_EQUAL(b3.hasValue(), false);
167 BOOST_CHECK_EQUAL(b3.value_size(), 0);
168 BOOST_CHECK(b3.value() == nullptr);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700169}
170
Davide Pesavento5c803b92019-02-02 19:23:48 -0500171BOOST_AUTO_TEST_CASE(FromTypeAndBuffer)
172{
173 const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14};
174 auto bufferPtr = make_shared<Buffer>(VALUE, sizeof(VALUE));
175
176 Block b(42, bufferPtr);
Davide Pesavento6b330402019-04-24 00:14:01 -0400177 BOOST_CHECK_EQUAL(b.isValid(), true);
Davide Pesavento5c803b92019-02-02 19:23:48 -0500178 BOOST_CHECK_EQUAL(b.type(), 42);
179 BOOST_CHECK_EQUAL(b.size(), 6);
180 BOOST_CHECK_EQUAL(b.hasValue(), true);
181 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
182}
183
184BOOST_AUTO_TEST_CASE(FromTypeAndBlock)
185{
186 const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
187 Block nested(BUFFER, sizeof(BUFFER));
188
189 Block b(84, nested);
Davide Pesavento6b330402019-04-24 00:14:01 -0400190 BOOST_CHECK_EQUAL(b.isValid(), true);
Davide Pesavento5c803b92019-02-02 19:23:48 -0500191 BOOST_CHECK_EQUAL(b.type(), 84);
192 BOOST_CHECK_EQUAL(b.size(), 10);
193 BOOST_CHECK_EQUAL(b.hasValue(), true);
194 BOOST_CHECK_EQUAL(b.value_size(), sizeof(BUFFER));
195}
196
Junxiao Shif0da7892015-04-04 22:16:16 -0700197BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700198{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000199 std::stringstream stream;
200 stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
201 stream.seekg(0);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700202
Junxiao Shidc4277a2017-07-17 11:34:02 +0000203 Block b = Block::fromStream(stream);
Davide Pesavento6b330402019-04-24 00:14:01 -0400204 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000205 BOOST_CHECK_EQUAL(b.size(), 3);
206 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento6b330402019-04-24 00:14:01 -0400207 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000208 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700209
Junxiao Shidc4277a2017-07-17 11:34:02 +0000210 b = Block::fromStream(stream);
211 BOOST_CHECK_EQUAL(b.type(), 1);
212 BOOST_CHECK_EQUAL(b.size(), 3);
213 BOOST_CHECK_EQUAL(b.value_size(), 1);
214 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
215 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700216
Davide Pesavento6b330402019-04-24 00:14:01 -0400217 b = Block::fromStream(stream);
218 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
219 BOOST_CHECK_EQUAL(b.size(), 6);
220 BOOST_CHECK_EQUAL(b.value_size(), 0);
221 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
222
223 BOOST_CHECK(stream.eof());
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600224 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700225}
226
Junxiao Shif0da7892015-04-04 22:16:16 -0700227BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
228{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000229 const uint8_t PACKET[] = {
Junxiao Shif0da7892015-04-04 22:16:16 -0700230 0x06, 0x20, // Data
231 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000232 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
233 0x08, 0x01, 0x31, // GenericNameComponent '1'
234 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shif0da7892015-04-04 22:16:16 -0700235 0x14, 0x00, // MetaInfo empty
236 0x15, 0x00, // Content empty
237 0x16, 0x05, // SignatureInfo
Junxiao Shidc4277a2017-07-17 11:34:02 +0000238 0x1b, 0x01, 0x01, // SignatureType RSA
239 0x1c, 0x00, // KeyLocator empty
Junxiao Shif0da7892015-04-04 22:16:16 -0700240 0x17, 0x00 // SignatureValue empty
241 };
242 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
243
244 std::stringstream stream;
245 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
246 stream.seekg(0);
247
Junxiao Shidc4277a2017-07-17 11:34:02 +0000248 Block b = Block::fromStream(stream);
249 BOOST_CHECK_EQUAL(b.type(), 6);
250 BOOST_CHECK_EQUAL(b.value_size(), 32);
251 b.parse();
Davide Pesavento6b330402019-04-24 00:14:01 -0400252 BOOST_CHECK_EQUAL(b.elements_size(), 5);
Junxiao Shif0da7892015-04-04 22:16:16 -0700253}
254
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000255BOOST_AUTO_TEST_CASE(FromStreamZeroLength)
Junxiao Shif0da7892015-04-04 22:16:16 -0700256{
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000257 const uint8_t BUFFER[] = {0x70, 0x00,
258 0x71, 0x03, 0x86, 0x11, 0x24,
259 0x72, 0x00};
Junxiao Shif0da7892015-04-04 22:16:16 -0700260
261 std::stringstream stream;
262 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
263 stream.seekg(0);
264
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000265 Block b1 = Block::fromStream(stream);
266 BOOST_CHECK_EQUAL(b1.type(), 0x70);
267 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000268
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000269 Block b2 = Block::fromStream(stream);
270 BOOST_CHECK_EQUAL(b2.type(), 0x71);
271 BOOST_CHECK_EQUAL(b2.value_size(), 3);
272 const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24};
273 BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(),
274 EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2));
275
276 Block b3 = Block::fromStream(stream);
277 BOOST_CHECK_EQUAL(b3.type(), 0x72);
278 BOOST_CHECK_EQUAL(b3.value_size(), 0);
279
280 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
281}
282
283BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge)
284{
285 const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00};
286
287 std::stringstream stream;
288 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
289 for (int i = 0; i < 0x10000; ++i) {
290 stream.put('\0');
291 }
292 stream.seekg(0);
293
294 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000295}
296
297BOOST_AUTO_TEST_CASE(FromWireBuffer)
298{
Davide Pesavento6b330402019-04-24 00:14:01 -0400299 auto buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000300
301 size_t offset = 0;
302 bool isOk = false;
303 Block b;
304 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
305 BOOST_CHECK(isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400306 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000307 BOOST_CHECK_EQUAL(b.size(), 3);
308 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento6b330402019-04-24 00:14:01 -0400309 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000310 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
311 offset += b.size();
312
313 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
314 BOOST_CHECK(isOk);
315 BOOST_CHECK_EQUAL(b.type(), 1);
316 BOOST_CHECK_EQUAL(b.size(), 3);
317 BOOST_CHECK_EQUAL(b.value_size(), 1);
318 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
319 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
320 offset += b.size();
321
322 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
Davide Pesavento6b330402019-04-24 00:14:01 -0400323 BOOST_CHECK(isOk);
324 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
325 BOOST_CHECK_EQUAL(b.size(), 6);
326 BOOST_CHECK_EQUAL(b.value_size(), 0);
327 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000328}
329
330BOOST_AUTO_TEST_CASE(FromRawBuffer)
331{
332 size_t offset = 0;
333 bool isOk = false;
334 Block b;
335 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
336 BOOST_CHECK(isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400337 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000338 BOOST_CHECK_EQUAL(b.size(), 3);
339 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento6b330402019-04-24 00:14:01 -0400340 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000341 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
342 offset += b.size();
343
344 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
345 BOOST_CHECK(isOk);
346 BOOST_CHECK_EQUAL(b.type(), 1);
347 BOOST_CHECK_EQUAL(b.size(), 3);
348 BOOST_CHECK_EQUAL(b.value_size(), 1);
349 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
350 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
351 offset += b.size();
352
353 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
Davide Pesavento6b330402019-04-24 00:14:01 -0400354 BOOST_CHECK(isOk);
355 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
356 BOOST_CHECK_EQUAL(b.size(), 6);
357 BOOST_CHECK_EQUAL(b.value_size(), 0);
358 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
359}
360
361template<typename T>
362struct MalformedInput
363{
364 static const std::vector<uint8_t> INPUT;
365};
366
367template<>
368const std::vector<uint8_t> MalformedInput<struct TlvTypeZero>::INPUT{
369 0x00, 0x00
370};
371template<>
372const std::vector<uint8_t> MalformedInput<struct TlvTypeTooLarge>::INPUT{
373 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00
374};
375template<>
376const std::vector<uint8_t> MalformedInput<struct BadTlvLength>::INPUT{
377 0x01, 0xff, 0x42, 0x42
378};
379template<>
380const std::vector<uint8_t> MalformedInput<struct TruncatedTlvValue>::INPUT{
381 0x01, 0x02, 0x03
382};
383
384using MalformedInputs = boost::mpl::vector<
385 MalformedInput<TlvTypeZero>,
386 MalformedInput<TlvTypeTooLarge>,
387 MalformedInput<BadTlvLength>,
388 MalformedInput<TruncatedTlvValue>
389>;
390
391BOOST_AUTO_TEST_CASE_TEMPLATE(Malformed, T, MalformedInputs)
392{
393 // constructor from raw buffer
394 BOOST_CHECK_THROW(Block(T::INPUT.data(), T::INPUT.size()), tlv::Error);
395
396 // fromStream()
397 std::stringstream stream;
398 stream.write(reinterpret_cast<const char*>(T::INPUT.data()), T::INPUT.size());
399 stream.seekg(0);
400 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
401
402 // fromBuffer(), ConstBufferPtr overload
403 auto buf = make_shared<Buffer>(T::INPUT.begin(), T::INPUT.end());
404 bool isOk;
405 Block b;
406 std::tie(isOk, b) = Block::fromBuffer(buf, 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000407 BOOST_CHECK(!isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400408 BOOST_CHECK(!b.isValid());
409
410 // fromBuffer(), raw buffer overload
411 std::tie(isOk, b) = Block::fromBuffer(T::INPUT.data(), T::INPUT.size());
412 BOOST_CHECK(!isOk);
413 BOOST_CHECK(!b.isValid());
Junxiao Shif0da7892015-04-04 22:16:16 -0700414}
415
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100416BOOST_AUTO_TEST_SUITE_END() // Construction
417
Junxiao Shidc4277a2017-07-17 11:34:02 +0000418BOOST_AUTO_TEST_SUITE(SubElements)
419
420BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700421{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000422 const uint8_t PACKET[] = {
423 0x06, 0x20, // Data
424 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000425 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
426 0x08, 0x01, 0x31, // GenericNameComponent '1'
427 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shidc4277a2017-07-17 11:34:02 +0000428 0x14, 0x00, // MetaInfo empty
429 0x15, 0x00, // Content empty
430 0x16, 0x05, // SignatureInfo
431 0x1b, 0x01, 0x01, // SignatureType RSA
432 0x1c, 0x00, // KeyLocator empty
433 0x17, 0x00 // SignatureValue empty
434 };
Junxiao Shidc4277a2017-07-17 11:34:02 +0000435 Block data(PACKET, sizeof(PACKET));
436 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700437
Junxiao Shidc4277a2017-07-17 11:34:02 +0000438 BOOST_CHECK_EQUAL(data.elements_size(), 5);
439 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
440 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700441
Junxiao Shidc4277a2017-07-17 11:34:02 +0000442 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
443 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
444
445 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
446 BOOST_CHECK(data.find(0x01) == data.elements_end());
Davide Pesavento6b330402019-04-24 00:14:01 -0400447
448 const uint8_t MALFORMED[] = {
449 // TLV-LENGTH of nested element is greater than TLV-LENGTH of enclosing element
450 0x05, 0x05, 0x07, 0x07, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f
451 };
452 Block bad(MALFORMED, sizeof(MALFORMED));
453 BOOST_CHECK_THROW(bad.parse(), Block::Error);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700454}
455
Joao Pereira7476ebf2015-07-07 14:54:39 -0400456BOOST_AUTO_TEST_CASE(InsertBeginning)
457{
458 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000459 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
460 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
461 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400462
463 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
464 masterBlock.push_back(secondBlock);
465 masterBlock.push_back(thirdBlock);
466 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000467 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400468 BOOST_CHECK_EQUAL(*it == secondBlock, true);
469
470 it = masterBlock.insert(it, firstBlock);
471
472 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
473 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
474 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
475}
476
477BOOST_AUTO_TEST_CASE(InsertEnd)
478{
479 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000480 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
481 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
482 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400483
484 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
485 masterBlock.push_back(firstBlock);
486 masterBlock.push_back(secondBlock);
487 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
488 Block::element_const_iterator it = masterBlock.elements_end();
489 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
490
491 it = masterBlock.insert(it, thirdBlock);
492
493 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
494 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
495 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
496}
497
498BOOST_AUTO_TEST_CASE(InsertMiddle)
499{
500 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000501 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
502 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
503 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400504
505 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
506 masterBlock.push_back(firstBlock);
507 masterBlock.push_back(thirdBlock);
508 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000509 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400510 BOOST_CHECK_EQUAL(*it == firstBlock, true);
511
Junxiao Shidc4277a2017-07-17 11:34:02 +0000512 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400513
514 BOOST_CHECK_EQUAL(*it == secondBlock, true);
515 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
516 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
517}
518
519BOOST_AUTO_TEST_CASE(EraseSingleElement)
520{
521 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000522 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
523 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
524 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400525
526 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
527 masterBlock.push_back(firstBlock);
528 masterBlock.push_back(secondBlock);
529 masterBlock.push_back(thirdBlock);
530 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000531 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400532 it++;
533 BOOST_CHECK_EQUAL(*it == secondBlock, true);
534
535 it = masterBlock.erase(it);
536
537 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
538 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
539 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
540}
541
542BOOST_AUTO_TEST_CASE(EraseRange)
543{
544 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000545 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
546 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
547 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
548 Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName");
549 Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName");
550 Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400551
552 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
553 masterBlock.push_back(firstBlock);
554 masterBlock.push_back(secondBlock);
555 masterBlock.push_back(thirdBlock);
556 masterBlock.push_back(fourthBlock);
557 masterBlock.push_back(fifthBlock);
558 masterBlock.push_back(sixthBlock);
559 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000560 Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400561 itStart++;
562 Block::element_const_iterator itEnd = itStart + 3;
563 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
564 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
565
566 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
567
568 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
569 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
570 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
571}
572
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700573BOOST_AUTO_TEST_CASE(Remove)
574{
575 Block block(tlv::Data);
576 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
577 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
578 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
579 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
580 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
581
582 BOOST_CHECK_EQUAL(5, block.elements_size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400583 BOOST_CHECK_NO_THROW(block.remove(tlv::ContentType));
584 BOOST_REQUIRE_EQUAL(2, block.elements_size());
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700585
Davide Pesavento6b330402019-04-24 00:14:01 -0400586 auto elements = block.elements();
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700587 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
588 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
589 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
Davide Pesavento6b330402019-04-24 00:14:01 -0400590 BOOST_CHECK_EQUAL(readString(elements[1]).compare("ndn:/test-prefix"), 0);
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700591}
592
Junxiao Shidc4277a2017-07-17 11:34:02 +0000593BOOST_AUTO_TEST_SUITE_END() // SubElements
594
595BOOST_AUTO_TEST_CASE(Equality)
596{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400597 const uint8_t one[] = {0x08, 0x00};
598 Block a(one, sizeof(one));
599 Block b(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000600 BOOST_CHECK_EQUAL(a == b, true);
601 BOOST_CHECK_EQUAL(a != b, false);
602
Davide Pesaventob10024c2017-09-22 01:36:44 -0400603 const uint8_t two[] = {0x06, 0x00};
604 Block c(two, sizeof(two));
605 Block d(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000606 BOOST_CHECK_EQUAL(c == d, false);
607 BOOST_CHECK_EQUAL(c != d, true);
608
Davide Pesaventob10024c2017-09-22 01:36:44 -0400609 const uint8_t three[] = {0x06, 0x01, 0xcc};
610 Block e(two, sizeof(two));
611 Block f(three, sizeof(three));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000612 BOOST_CHECK_EQUAL(e == f, false);
613 BOOST_CHECK_EQUAL(e != f, true);
614}
615
Junxiao Shi72c0c642018-04-20 15:41:09 +0000616BOOST_AUTO_TEST_CASE(Print)
617{
618 // default constructed
619 Block b;
620 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
621
622 // zero length
623 b = "0700"_block;
624 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
625
626 // unparsed
627 b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
628 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
629 "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
630 // set and restore format flags
631 {
632 std::ostringstream oss;
633 oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
634 BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
635 }
636
637 // parsed
638 b = "FD010808 0502CADD 59024E42"_block;
639 b.parse();
640 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
641 "264[8]={5[2]=CADD,89[2]=4E42}");
642
643 // parsed then modified: print modified sub-elements
644 b = "FD010808 0502CADD 59024E42"_block;
645 b.parse();
646 b.erase(b.elements_begin());
647 b.push_back("10022386"_block);
648 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
649 "264[8]={89[2]=4E42,16[2]=2386}");
650}
651
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600652BOOST_AUTO_TEST_SUITE(BlockLiteral)
653
654BOOST_AUTO_TEST_CASE(Simple)
655{
Davide Pesavento6b330402019-04-24 00:14:01 -0400656 Block b0 = "4200"_block;
657 BOOST_CHECK_EQUAL(b0.type(), 0x42);
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600658 BOOST_CHECK_EQUAL(b0.value_size(), 0);
659
660 Block b1 = "0101A0"_block;
661 BOOST_CHECK_EQUAL(b1.type(), 0x01);
662 BOOST_REQUIRE_EQUAL(b1.value_size(), 1);
663 BOOST_CHECK_EQUAL(b1.value()[0], 0xA0);
664}
665
666BOOST_AUTO_TEST_CASE(Comment)
667{
668 Block b0 = "a2b0c0d2eBf0G.B 1+"_block;
669 BOOST_CHECK_EQUAL(b0.type(), 0x20);
670 BOOST_REQUIRE_EQUAL(b0.value_size(), 2);
671 BOOST_CHECK_EQUAL(b0.value()[0], 0xB0);
672 BOOST_CHECK_EQUAL(b0.value()[1], 0xB1);
673}
674
675BOOST_AUTO_TEST_CASE(BadInput)
676{
677 BOOST_CHECK_THROW(""_block, std::invalid_argument);
678 BOOST_CHECK_THROW("1"_block, std::invalid_argument);
679 BOOST_CHECK_THROW("333"_block, std::invalid_argument);
Davide Pesavento6b330402019-04-24 00:14:01 -0400680 BOOST_CHECK_THROW("xx yy zz"_block, std::invalid_argument); // only comments
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600681
Davide Pesavento6b330402019-04-24 00:14:01 -0400682 BOOST_CHECK_THROW("0000"_block, tlv::Error); // invalid type
683 BOOST_CHECK_THROW("0202C0"_block, tlv::Error); // truncated value
684 BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error); // trailing garbage
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600685}
686
687BOOST_AUTO_TEST_SUITE_END() // BlockLiteral
688
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100689BOOST_AUTO_TEST_SUITE_END() // TestBlock
690BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800691
Alexander Afanasyev74633892015-02-08 18:08:46 -0800692} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800693} // namespace ndn