blob: 52ad9c14250d75817b80e4fdda10389c7bf07834 [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 Pesavento1a4a7bf2020-12-04 22:30:46 -050028#include <boost/test/data/test_case.hpp>
Davide Pesavento6b330402019-04-24 00:14:01 -040029
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
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500361static const Buffer MalformedInputs[] = {
362 {0x00, 0x00}, // invalid TLV type (zero)
363 {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, // TLV type too large
364 {0x01, 0xff, 0x42, 0x42}, // bad TLV length
365 {0x01, 0x02, 0x03}, // truncated TLV value
Davide Pesavento6b330402019-04-24 00:14:01 -0400366};
367
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500368BOOST_DATA_TEST_CASE(Malformed, MalformedInputs)
Davide Pesavento6b330402019-04-24 00:14:01 -0400369{
370 // constructor from raw buffer
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500371 BOOST_CHECK_THROW(Block(sample.data(), sample.size()), tlv::Error);
Davide Pesavento6b330402019-04-24 00:14:01 -0400372
373 // fromStream()
374 std::stringstream stream;
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500375 stream.write(sample.template get<char>(), sample.size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400376 stream.seekg(0);
377 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
378
379 // fromBuffer(), ConstBufferPtr overload
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500380 auto buf = make_shared<Buffer>(sample.begin(), sample.end());
Davide Pesavento6b330402019-04-24 00:14:01 -0400381 bool isOk;
382 Block b;
383 std::tie(isOk, b) = Block::fromBuffer(buf, 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000384 BOOST_CHECK(!isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400385 BOOST_CHECK(!b.isValid());
386
387 // fromBuffer(), raw buffer overload
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500388 std::tie(isOk, b) = Block::fromBuffer(sample.data(), sample.size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400389 BOOST_CHECK(!isOk);
390 BOOST_CHECK(!b.isValid());
Junxiao Shif0da7892015-04-04 22:16:16 -0700391}
392
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100393BOOST_AUTO_TEST_SUITE_END() // Construction
394
Junxiao Shidc4277a2017-07-17 11:34:02 +0000395BOOST_AUTO_TEST_SUITE(SubElements)
396
397BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700398{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000399 const uint8_t PACKET[] = {
400 0x06, 0x20, // Data
401 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000402 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
403 0x08, 0x01, 0x31, // GenericNameComponent '1'
404 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shidc4277a2017-07-17 11:34:02 +0000405 0x14, 0x00, // MetaInfo empty
406 0x15, 0x00, // Content empty
407 0x16, 0x05, // SignatureInfo
408 0x1b, 0x01, 0x01, // SignatureType RSA
409 0x1c, 0x00, // KeyLocator empty
410 0x17, 0x00 // SignatureValue empty
411 };
Junxiao Shidc4277a2017-07-17 11:34:02 +0000412 Block data(PACKET, sizeof(PACKET));
413 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700414
Junxiao Shidc4277a2017-07-17 11:34:02 +0000415 BOOST_CHECK_EQUAL(data.elements_size(), 5);
416 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
417 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700418
Junxiao Shidc4277a2017-07-17 11:34:02 +0000419 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
420 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
421
422 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
423 BOOST_CHECK(data.find(0x01) == data.elements_end());
Davide Pesavento6b330402019-04-24 00:14:01 -0400424
425 const uint8_t MALFORMED[] = {
426 // TLV-LENGTH of nested element is greater than TLV-LENGTH of enclosing element
427 0x05, 0x05, 0x07, 0x07, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f
428 };
429 Block bad(MALFORMED, sizeof(MALFORMED));
430 BOOST_CHECK_THROW(bad.parse(), Block::Error);
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700431}
432
Joao Pereira7476ebf2015-07-07 14:54:39 -0400433BOOST_AUTO_TEST_CASE(InsertBeginning)
434{
435 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000436 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
437 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
438 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400439
440 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
441 masterBlock.push_back(secondBlock);
442 masterBlock.push_back(thirdBlock);
443 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000444 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400445 BOOST_CHECK_EQUAL(*it == secondBlock, true);
446
447 it = masterBlock.insert(it, firstBlock);
448
449 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
450 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
451 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
452}
453
454BOOST_AUTO_TEST_CASE(InsertEnd)
455{
456 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000457 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
458 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
459 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400460
461 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
462 masterBlock.push_back(firstBlock);
463 masterBlock.push_back(secondBlock);
464 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
465 Block::element_const_iterator it = masterBlock.elements_end();
466 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
467
468 it = masterBlock.insert(it, thirdBlock);
469
470 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
471 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
472 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
473}
474
475BOOST_AUTO_TEST_CASE(InsertMiddle)
476{
477 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000478 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
479 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
480 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400481
482 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
483 masterBlock.push_back(firstBlock);
484 masterBlock.push_back(thirdBlock);
485 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000486 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400487 BOOST_CHECK_EQUAL(*it == firstBlock, true);
488
Junxiao Shidc4277a2017-07-17 11:34:02 +0000489 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400490
491 BOOST_CHECK_EQUAL(*it == secondBlock, true);
492 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
493 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
494}
495
496BOOST_AUTO_TEST_CASE(EraseSingleElement)
497{
498 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000499 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
500 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
501 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400502
503 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
504 masterBlock.push_back(firstBlock);
505 masterBlock.push_back(secondBlock);
506 masterBlock.push_back(thirdBlock);
507 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000508 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400509 it++;
510 BOOST_CHECK_EQUAL(*it == secondBlock, true);
511
512 it = masterBlock.erase(it);
513
514 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
515 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
516 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
517}
518
519BOOST_AUTO_TEST_CASE(EraseRange)
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");
525 Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName");
526 Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName");
527 Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400528
529 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
530 masterBlock.push_back(firstBlock);
531 masterBlock.push_back(secondBlock);
532 masterBlock.push_back(thirdBlock);
533 masterBlock.push_back(fourthBlock);
534 masterBlock.push_back(fifthBlock);
535 masterBlock.push_back(sixthBlock);
536 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000537 Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400538 itStart++;
539 Block::element_const_iterator itEnd = itStart + 3;
540 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
541 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
542
543 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
544
545 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
546 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
547 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
548}
549
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700550BOOST_AUTO_TEST_CASE(Remove)
551{
552 Block block(tlv::Data);
553 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
554 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
555 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
556 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
557 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
558
559 BOOST_CHECK_EQUAL(5, block.elements_size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400560 BOOST_CHECK_NO_THROW(block.remove(tlv::ContentType));
561 BOOST_REQUIRE_EQUAL(2, block.elements_size());
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700562
Davide Pesavento6b330402019-04-24 00:14:01 -0400563 auto elements = block.elements();
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700564 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
565 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
566 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
Davide Pesavento6b330402019-04-24 00:14:01 -0400567 BOOST_CHECK_EQUAL(readString(elements[1]).compare("ndn:/test-prefix"), 0);
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700568}
569
Junxiao Shidc4277a2017-07-17 11:34:02 +0000570BOOST_AUTO_TEST_SUITE_END() // SubElements
571
572BOOST_AUTO_TEST_CASE(Equality)
573{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400574 const uint8_t one[] = {0x08, 0x00};
575 Block a(one, sizeof(one));
576 Block b(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000577 BOOST_CHECK_EQUAL(a == b, true);
578 BOOST_CHECK_EQUAL(a != b, false);
579
Davide Pesaventob10024c2017-09-22 01:36:44 -0400580 const uint8_t two[] = {0x06, 0x00};
581 Block c(two, sizeof(two));
582 Block d(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000583 BOOST_CHECK_EQUAL(c == d, false);
584 BOOST_CHECK_EQUAL(c != d, true);
585
Davide Pesaventob10024c2017-09-22 01:36:44 -0400586 const uint8_t three[] = {0x06, 0x01, 0xcc};
587 Block e(two, sizeof(two));
588 Block f(three, sizeof(three));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000589 BOOST_CHECK_EQUAL(e == f, false);
590 BOOST_CHECK_EQUAL(e != f, true);
591}
592
Junxiao Shi72c0c642018-04-20 15:41:09 +0000593BOOST_AUTO_TEST_CASE(Print)
594{
595 // default constructed
596 Block b;
597 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
598
599 // zero length
600 b = "0700"_block;
601 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
602
603 // unparsed
604 b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
605 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
606 "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
607 // set and restore format flags
608 {
609 std::ostringstream oss;
610 oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
611 BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
612 }
613
614 // parsed
615 b = "FD010808 0502CADD 59024E42"_block;
616 b.parse();
617 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
618 "264[8]={5[2]=CADD,89[2]=4E42}");
619
620 // parsed then modified: print modified sub-elements
621 b = "FD010808 0502CADD 59024E42"_block;
622 b.parse();
623 b.erase(b.elements_begin());
624 b.push_back("10022386"_block);
625 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
626 "264[8]={89[2]=4E42,16[2]=2386}");
627}
628
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600629BOOST_AUTO_TEST_SUITE(BlockLiteral)
630
631BOOST_AUTO_TEST_CASE(Simple)
632{
Davide Pesavento6b330402019-04-24 00:14:01 -0400633 Block b0 = "4200"_block;
634 BOOST_CHECK_EQUAL(b0.type(), 0x42);
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600635 BOOST_CHECK_EQUAL(b0.value_size(), 0);
636
637 Block b1 = "0101A0"_block;
638 BOOST_CHECK_EQUAL(b1.type(), 0x01);
639 BOOST_REQUIRE_EQUAL(b1.value_size(), 1);
640 BOOST_CHECK_EQUAL(b1.value()[0], 0xA0);
641}
642
643BOOST_AUTO_TEST_CASE(Comment)
644{
645 Block b0 = "a2b0c0d2eBf0G.B 1+"_block;
646 BOOST_CHECK_EQUAL(b0.type(), 0x20);
647 BOOST_REQUIRE_EQUAL(b0.value_size(), 2);
648 BOOST_CHECK_EQUAL(b0.value()[0], 0xB0);
649 BOOST_CHECK_EQUAL(b0.value()[1], 0xB1);
650}
651
652BOOST_AUTO_TEST_CASE(BadInput)
653{
654 BOOST_CHECK_THROW(""_block, std::invalid_argument);
655 BOOST_CHECK_THROW("1"_block, std::invalid_argument);
656 BOOST_CHECK_THROW("333"_block, std::invalid_argument);
Davide Pesavento6b330402019-04-24 00:14:01 -0400657 BOOST_CHECK_THROW("xx yy zz"_block, std::invalid_argument); // only comments
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600658
Davide Pesavento6b330402019-04-24 00:14:01 -0400659 BOOST_CHECK_THROW("0000"_block, tlv::Error); // invalid type
660 BOOST_CHECK_THROW("0202C0"_block, tlv::Error); // truncated value
661 BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error); // trailing garbage
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600662}
663
664BOOST_AUTO_TEST_SUITE_END() // BlockLiteral
665
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100666BOOST_AUTO_TEST_SUITE_END() // TestBlock
667BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800668
Alexander Afanasyev74633892015-02-08 18:08:46 -0800669} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800670} // namespace ndn