blob: b6a841b25218b5b43ab33f8e122b1bc9f73e0043 [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 Pesavento5f6865c2021-04-23 23:24:11 -04003 * Copyright (c) 2013-2021 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
Davide Pesaventoa76ea9a2021-04-24 00:06:02 -040027#include <boost/asio/buffer.hpp>
Junxiao Shi72c0c642018-04-20 15:41:09 +000028#include <boost/lexical_cast.hpp>
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -050029#include <boost/test/data/test_case.hpp>
Davide Pesavento6b330402019-04-24 00:14:01 -040030
Junxiao Shidc4277a2017-07-17 11:34:02 +000031#include <cstring>
32#include <sstream>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033
Alexander Afanasyev233750e2014-02-16 00:50:07 -080034namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080035namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080036
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_SUITE(Encoding)
38BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080039
Junxiao Shidc4277a2017-07-17 11:34:02 +000040BOOST_AUTO_TEST_SUITE(Construction)
41
42static const uint8_t TEST_BUFFER[] = {
Davide Pesavento6b330402019-04-24 00:14:01 -040043 0x42, 0x01, 0xfa,
44 0x01, 0x01, 0xfb,
45 0xfe, 0xff, 0xff, 0xff, 0xff, 0x00, // bug #4726
Alexander Afanasyev74633892015-02-08 18:08:46 -080046};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080047
Davide Pesavento6b330402019-04-24 00:14:01 -040048BOOST_AUTO_TEST_CASE(Default)
Alexander Afanasyev74633892015-02-08 18:08:46 -080049{
Junxiao Shidc4277a2017-07-17 11:34:02 +000050 Block b;
Davide Pesavento6b330402019-04-24 00:14:01 -040051
52 BOOST_CHECK_EQUAL(b.isValid(), false);
Davide Pesavento6b330402019-04-24 00:14:01 -040053 BOOST_CHECK_EQUAL(b.type(), tlv::Invalid);
54 BOOST_CHECK_EQUAL(b.hasValue(), false);
55 BOOST_CHECK_EQUAL(b.value_size(), 0);
56 BOOST_CHECK(b.value() == nullptr);
57
58 BOOST_CHECK_THROW(b.size(), Block::Error);
59 BOOST_CHECK_THROW(b.begin(), Block::Error);
60 BOOST_CHECK_THROW(b.end(), Block::Error);
61 BOOST_CHECK_THROW(b.wire(), Block::Error);
62 BOOST_CHECK_THROW(b.blockFromValue(), Block::Error);
Alexander Afanasyev74633892015-02-08 18:08:46 -080063}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070064
Junxiao Shidc4277a2017-07-17 11:34:02 +000065BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080066{
Junxiao Shidc4277a2017-07-17 11:34:02 +000067 EncodingBuffer encoder;
Davide Pesavento5c803b92019-02-02 19:23:48 -050068 BOOST_CHECK_THROW(Block{encoder}, tlv::Error);
69
70 const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14};
Junxiao Shidc4277a2017-07-17 11:34:02 +000071 size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE));
72 encoder.prependVarNumber(length);
73 encoder.prependVarNumber(0xe0);
74
75 Block b = encoder.block();
76 BOOST_CHECK_EQUAL(b.type(), 0xe0);
77 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
78 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
79 VALUE, VALUE + sizeof(VALUE));
80
81 b = Block(encoder);
82 BOOST_CHECK_EQUAL(b.type(), 0xe0);
83 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
84 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
85 VALUE, VALUE + sizeof(VALUE));
Alexander Afanasyev74633892015-02-08 18:08:46 -080086}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070087
Davide Pesaventoeee3e822016-11-26 19:19:34 +010088BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -070089{
Davide Pesavento5c803b92019-02-02 19:23:48 -050090 const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
91 Block block(BUFFER, sizeof(BUFFER));
Alexander Afanasyev4448d292015-08-09 20:11:37 -070092
93 Block derivedBlock(block, block.begin(), block.end());
94 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
95 BOOST_CHECK(derivedBlock == block); // blocks should match
96
97 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
98 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
Davide Pesavento5c803b92019-02-02 19:23:48 -050099 BOOST_CHECK(derivedBlock == Block(BUFFER + 2, 3));
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700100
Davide Pesavento5c803b92019-02-02 19:23:48 -0500101 Buffer otherBuffer(BUFFER, sizeof(BUFFER));
Junxiao Shidb7464d2017-07-13 03:11:17 +0000102 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument);
103 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument);
104 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument);
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700105}
106
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100107BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -0700108{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000109 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700110 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
111 };
112
Junxiao Shidc4277a2017-07-17 11:34:02 +0000113 Block b1(BUFFER, sizeof(BUFFER));
Eric Newberry6235aac2015-08-13 16:03:47 -0700114
Junxiao Shidc4277a2017-07-17 11:34:02 +0000115 Block b2(b1, b1.begin(), b1.end());
116 auto buf2 = b2.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700117
Junxiao Shidc4277a2017-07-17 11:34:02 +0000118 b1.parse();
119 b1.remove(tlv::Name);
120 b1.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700121
Junxiao Shidc4277a2017-07-17 11:34:02 +0000122 b2.parse();
Eric Newberry6235aac2015-08-13 16:03:47 -0700123
Junxiao Shidc4277a2017-07-17 11:34:02 +0000124 BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER));
125 BOOST_CHECK_EQUAL(buf2, b2.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700126}
127
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100128BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700129{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000130 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700131 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
132 };
133
Junxiao Shidc4277a2017-07-17 11:34:02 +0000134 Block b1(BUFFER, sizeof(BUFFER));
135 auto buf1 = b1.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700136
Junxiao Shidc4277a2017-07-17 11:34:02 +0000137 Block b2(b1, b1.begin(), b1.end());
Eric Newberry6235aac2015-08-13 16:03:47 -0700138
Junxiao Shidc4277a2017-07-17 11:34:02 +0000139 b2.parse();
140 b2.remove(tlv::Name);
141 b2.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700142
Junxiao Shidc4277a2017-07-17 11:34:02 +0000143 b1.parse();
144 BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER));
145 BOOST_CHECK_EQUAL(buf1, b1.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700146}
147
Junxiao Shidc4277a2017-07-17 11:34:02 +0000148BOOST_AUTO_TEST_CASE(FromType)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800149{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000150 Block b1(4);
Davide Pesavento6b330402019-04-24 00:14:01 -0400151 BOOST_CHECK_EQUAL(b1.isValid(), true);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000152 BOOST_CHECK_EQUAL(b1.type(), 4);
153 BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH
Davide Pesavento5c803b92019-02-02 19:23:48 -0500154 BOOST_CHECK_EQUAL(b1.hasValue(), false);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000155 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400156 BOOST_CHECK(b1.value() == nullptr);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800157
Junxiao Shidc4277a2017-07-17 11:34:02 +0000158 Block b2(258);
Davide Pesavento6b330402019-04-24 00:14:01 -0400159 BOOST_CHECK_EQUAL(b2.isValid(), true);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000160 BOOST_CHECK_EQUAL(b2.type(), 258);
161 BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH
Davide Pesavento5c803b92019-02-02 19:23:48 -0500162 BOOST_CHECK_EQUAL(b2.hasValue(), false);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000163 BOOST_CHECK_EQUAL(b2.value_size(), 0);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400164 BOOST_CHECK(b2.value() == nullptr);
Davide Pesavento6b330402019-04-24 00:14:01 -0400165
166 Block b3(tlv::Invalid);
167 BOOST_CHECK_EQUAL(b3.isValid(), false);
168 BOOST_CHECK_EQUAL(b3.type(), tlv::Invalid);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400169 BOOST_CHECK_EXCEPTION(b3.size(), Block::Error, [] (const auto& e) {
170 return e.what() == "Cannot determine size of invalid block"s;
171 });
Davide Pesavento6b330402019-04-24 00:14:01 -0400172 BOOST_CHECK_EQUAL(b3.hasValue(), false);
173 BOOST_CHECK_EQUAL(b3.value_size(), 0);
174 BOOST_CHECK(b3.value() == nullptr);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700175}
176
Davide Pesavento5c803b92019-02-02 19:23:48 -0500177BOOST_AUTO_TEST_CASE(FromTypeAndBuffer)
178{
179 const uint8_t VALUE[] = {0x11, 0x12, 0x13, 0x14};
180 auto bufferPtr = make_shared<Buffer>(VALUE, sizeof(VALUE));
181
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400182 Block b1(42, std::move(bufferPtr));
183 BOOST_CHECK_EQUAL(b1.isValid(), true);
184 BOOST_CHECK_EQUAL(b1.type(), 42);
185 BOOST_CHECK_EQUAL(b1.size(), 6);
186 BOOST_CHECK_EQUAL(b1.hasValue(), true);
187 BOOST_CHECK_EQUAL(b1.value_size(), sizeof(VALUE));
188 BOOST_CHECK(b1.value() != nullptr);
189
190 // empty buffer as TLV-VALUE
191 Block b2(63, make_shared<Buffer>());
192 BOOST_CHECK_EQUAL(b2.isValid(), true);
193 BOOST_CHECK_EQUAL(b2.type(), 63);
194 BOOST_CHECK_EQUAL(b2.size(), 2);
195 BOOST_CHECK_EQUAL(b2.hasValue(), true);
196 BOOST_CHECK_EQUAL(b2.value_size(), 0);
197 BOOST_CHECK(b2.value() == nullptr);
Davide Pesavento5c803b92019-02-02 19:23:48 -0500198}
199
200BOOST_AUTO_TEST_CASE(FromTypeAndBlock)
201{
202 const uint8_t BUFFER[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
203 Block nested(BUFFER, sizeof(BUFFER));
204
205 Block b(84, nested);
Davide Pesavento6b330402019-04-24 00:14:01 -0400206 BOOST_CHECK_EQUAL(b.isValid(), true);
Davide Pesavento5c803b92019-02-02 19:23:48 -0500207 BOOST_CHECK_EQUAL(b.type(), 84);
208 BOOST_CHECK_EQUAL(b.size(), 10);
209 BOOST_CHECK_EQUAL(b.hasValue(), true);
210 BOOST_CHECK_EQUAL(b.value_size(), sizeof(BUFFER));
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400211 BOOST_CHECK(b.value() != nullptr);
Davide Pesavento5c803b92019-02-02 19:23:48 -0500212}
213
Junxiao Shif0da7892015-04-04 22:16:16 -0700214BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700215{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000216 std::stringstream stream;
217 stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
218 stream.seekg(0);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700219
Junxiao Shidc4277a2017-07-17 11:34:02 +0000220 Block b = Block::fromStream(stream);
Davide Pesavento6b330402019-04-24 00:14:01 -0400221 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000222 BOOST_CHECK_EQUAL(b.size(), 3);
223 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400224 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000225 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700226
Junxiao Shidc4277a2017-07-17 11:34:02 +0000227 b = Block::fromStream(stream);
228 BOOST_CHECK_EQUAL(b.type(), 1);
229 BOOST_CHECK_EQUAL(b.size(), 3);
230 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400231 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000232 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700233
Davide Pesavento6b330402019-04-24 00:14:01 -0400234 b = Block::fromStream(stream);
235 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
236 BOOST_CHECK_EQUAL(b.size(), 6);
237 BOOST_CHECK_EQUAL(b.value_size(), 0);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400238 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
239 BOOST_CHECK(b.value() == nullptr);
Davide Pesavento6b330402019-04-24 00:14:01 -0400240
241 BOOST_CHECK(stream.eof());
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400242 BOOST_CHECK_EXCEPTION(Block::fromStream(stream), tlv::Error, [] (const auto& e) {
243 return e.what() == "Empty buffer during TLV parsing"s;
244 });
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700245}
246
Junxiao Shif0da7892015-04-04 22:16:16 -0700247BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
248{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000249 const uint8_t PACKET[] = {
Junxiao Shif0da7892015-04-04 22:16:16 -0700250 0x06, 0x20, // Data
251 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000252 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
253 0x08, 0x01, 0x31, // GenericNameComponent '1'
254 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shif0da7892015-04-04 22:16:16 -0700255 0x14, 0x00, // MetaInfo empty
256 0x15, 0x00, // Content empty
257 0x16, 0x05, // SignatureInfo
Junxiao Shidc4277a2017-07-17 11:34:02 +0000258 0x1b, 0x01, 0x01, // SignatureType RSA
259 0x1c, 0x00, // KeyLocator empty
Junxiao Shif0da7892015-04-04 22:16:16 -0700260 0x17, 0x00 // SignatureValue empty
261 };
262 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
263
264 std::stringstream stream;
265 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
266 stream.seekg(0);
267
Junxiao Shidc4277a2017-07-17 11:34:02 +0000268 Block b = Block::fromStream(stream);
269 BOOST_CHECK_EQUAL(b.type(), 6);
270 BOOST_CHECK_EQUAL(b.value_size(), 32);
271 b.parse();
Davide Pesavento6b330402019-04-24 00:14:01 -0400272 BOOST_CHECK_EQUAL(b.elements_size(), 5);
Junxiao Shif0da7892015-04-04 22:16:16 -0700273}
274
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000275BOOST_AUTO_TEST_CASE(FromStreamZeroLength)
Junxiao Shif0da7892015-04-04 22:16:16 -0700276{
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000277 const uint8_t BUFFER[] = {0x70, 0x00,
278 0x71, 0x03, 0x86, 0x11, 0x24,
279 0x72, 0x00};
Junxiao Shif0da7892015-04-04 22:16:16 -0700280
281 std::stringstream stream;
282 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
283 stream.seekg(0);
284
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000285 Block b1 = Block::fromStream(stream);
286 BOOST_CHECK_EQUAL(b1.type(), 0x70);
287 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400288 BOOST_CHECK(b1.value() == nullptr);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000289
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000290 Block b2 = Block::fromStream(stream);
291 BOOST_CHECK_EQUAL(b2.type(), 0x71);
292 BOOST_CHECK_EQUAL(b2.value_size(), 3);
293 const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24};
294 BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(),
295 EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2));
296
297 Block b3 = Block::fromStream(stream);
298 BOOST_CHECK_EQUAL(b3.type(), 0x72);
299 BOOST_CHECK_EQUAL(b3.value_size(), 0);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400300 BOOST_CHECK(b3.value() == nullptr);
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000301
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400302 BOOST_CHECK_EXCEPTION(Block::fromStream(stream), tlv::Error, [] (const auto& e) {
303 return e.what() == "Empty buffer during TLV parsing"s;
304 });
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000305}
306
307BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge)
308{
309 const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00};
310
311 std::stringstream stream;
312 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
313 for (int i = 0; i < 0x10000; ++i) {
314 stream.put('\0');
315 }
316 stream.seekg(0);
317
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400318 BOOST_CHECK_EXCEPTION(Block::fromStream(stream), tlv::Error, [] (const auto& e) {
319 return e.what() == "TLV-LENGTH from stream exceeds limit"s;
320 });
Junxiao Shidc4277a2017-07-17 11:34:02 +0000321}
322
323BOOST_AUTO_TEST_CASE(FromWireBuffer)
324{
Davide Pesavento6b330402019-04-24 00:14:01 -0400325 auto buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000326
327 size_t offset = 0;
328 bool isOk = false;
329 Block b;
330 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
331 BOOST_CHECK(isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400332 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000333 BOOST_CHECK_EQUAL(b.size(), 3);
334 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento6b330402019-04-24 00:14:01 -0400335 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000336 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
337 offset += b.size();
338
339 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
340 BOOST_CHECK(isOk);
341 BOOST_CHECK_EQUAL(b.type(), 1);
342 BOOST_CHECK_EQUAL(b.size(), 3);
343 BOOST_CHECK_EQUAL(b.value_size(), 1);
344 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
345 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
346 offset += b.size();
347
348 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
Davide Pesavento6b330402019-04-24 00:14:01 -0400349 BOOST_CHECK(isOk);
350 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
351 BOOST_CHECK_EQUAL(b.size(), 6);
352 BOOST_CHECK_EQUAL(b.value_size(), 0);
353 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400354 BOOST_CHECK(b.value() == nullptr);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000355}
356
357BOOST_AUTO_TEST_CASE(FromRawBuffer)
358{
359 size_t offset = 0;
360 bool isOk = false;
361 Block b;
362 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
363 BOOST_CHECK(isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400364 BOOST_CHECK_EQUAL(b.type(), 66);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000365 BOOST_CHECK_EQUAL(b.size(), 3);
366 BOOST_CHECK_EQUAL(b.value_size(), 1);
Davide Pesavento6b330402019-04-24 00:14:01 -0400367 BOOST_CHECK_EQUAL(*b.wire(), 0x42);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000368 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
369 offset += b.size();
370
371 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
372 BOOST_CHECK(isOk);
373 BOOST_CHECK_EQUAL(b.type(), 1);
374 BOOST_CHECK_EQUAL(b.size(), 3);
375 BOOST_CHECK_EQUAL(b.value_size(), 1);
376 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
377 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
378 offset += b.size();
379
380 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
Davide Pesavento6b330402019-04-24 00:14:01 -0400381 BOOST_CHECK(isOk);
382 BOOST_CHECK_EQUAL(b.type(), 0xffffffff);
383 BOOST_CHECK_EQUAL(b.size(), 6);
384 BOOST_CHECK_EQUAL(b.value_size(), 0);
385 BOOST_CHECK_EQUAL(*b.wire(), 0xfe);
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400386 BOOST_CHECK(b.value() == nullptr);
Davide Pesavento6b330402019-04-24 00:14:01 -0400387}
388
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500389static const Buffer MalformedInputs[] = {
390 {0x00, 0x00}, // invalid TLV type (zero)
391 {0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00}, // TLV type too large
392 {0x01, 0xff, 0x42, 0x42}, // bad TLV length
393 {0x01, 0x02, 0x03}, // truncated TLV value
Davide Pesavento6b330402019-04-24 00:14:01 -0400394};
395
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500396BOOST_DATA_TEST_CASE(Malformed, MalformedInputs)
Davide Pesavento6b330402019-04-24 00:14:01 -0400397{
398 // constructor from raw buffer
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500399 BOOST_CHECK_THROW(Block(sample.data(), sample.size()), tlv::Error);
Davide Pesavento6b330402019-04-24 00:14:01 -0400400
401 // fromStream()
402 std::stringstream stream;
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500403 stream.write(sample.template get<char>(), sample.size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400404 stream.seekg(0);
405 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
406
407 // fromBuffer(), ConstBufferPtr overload
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500408 auto buf = make_shared<Buffer>(sample.begin(), sample.end());
Davide Pesavento6b330402019-04-24 00:14:01 -0400409 bool isOk;
410 Block b;
411 std::tie(isOk, b) = Block::fromBuffer(buf, 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000412 BOOST_CHECK(!isOk);
Davide Pesavento6b330402019-04-24 00:14:01 -0400413 BOOST_CHECK(!b.isValid());
414
415 // fromBuffer(), raw buffer overload
Davide Pesavento1a4a7bf2020-12-04 22:30:46 -0500416 std::tie(isOk, b) = Block::fromBuffer(sample.data(), sample.size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400417 BOOST_CHECK(!isOk);
418 BOOST_CHECK(!b.isValid());
Junxiao Shif0da7892015-04-04 22:16:16 -0700419}
420
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100421BOOST_AUTO_TEST_SUITE_END() // Construction
422
Davide Pesaventoa76ea9a2021-04-24 00:06:02 -0400423BOOST_AUTO_TEST_CASE(BlockFromValue)
424{
425 Block b1(301);
426 BOOST_CHECK_EXCEPTION(b1.blockFromValue(), Block::Error, [] (const auto& e) {
427 return e.what() == "Cannot construct block from empty TLV-VALUE"s;
428 });
429
430 Block b2(302, make_shared<Buffer>());
431 BOOST_CHECK_EXCEPTION(b2.blockFromValue(), Block::Error, [] (const auto& e) {
432 return e.what() == "Cannot construct block from empty TLV-VALUE"s;
433 });
434
435 b1.encode();
436 Block b3(303, b1);
437 b3.encode();
438 Block nested = b3.blockFromValue();
439 BOOST_CHECK_EQUAL(nested.type(), 301);
440 BOOST_CHECK_EQUAL(nested.size(), 4);
441 BOOST_CHECK_EQUAL(nested.value_size(), 0);
442 BOOST_CHECK(nested == b1);
443}
444
Junxiao Shidc4277a2017-07-17 11:34:02 +0000445BOOST_AUTO_TEST_SUITE(SubElements)
446
447BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700448{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000449 const uint8_t PACKET[] = {
450 0x06, 0x20, // Data
451 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000452 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
453 0x08, 0x01, 0x31, // GenericNameComponent '1'
454 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shidc4277a2017-07-17 11:34:02 +0000455 0x14, 0x00, // MetaInfo empty
456 0x15, 0x00, // Content empty
457 0x16, 0x05, // SignatureInfo
458 0x1b, 0x01, 0x01, // SignatureType RSA
459 0x1c, 0x00, // KeyLocator empty
460 0x17, 0x00 // SignatureValue empty
461 };
Junxiao Shidc4277a2017-07-17 11:34:02 +0000462 Block data(PACKET, sizeof(PACKET));
463 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700464
Junxiao Shidc4277a2017-07-17 11:34:02 +0000465 BOOST_CHECK_EQUAL(data.elements_size(), 5);
466 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
467 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700468
Junxiao Shidc4277a2017-07-17 11:34:02 +0000469 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400470 BOOST_CHECK_EXCEPTION(data.get(0x01), Block::Error, [] (const auto& e) {
471 return e.what() == "No sub-element of type 1 found in block of type 6"s;
472 });
Junxiao Shidc4277a2017-07-17 11:34:02 +0000473
474 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
475 BOOST_CHECK(data.find(0x01) == data.elements_end());
Davide Pesavento6b330402019-04-24 00:14:01 -0400476
477 const uint8_t MALFORMED[] = {
478 // TLV-LENGTH of nested element is greater than TLV-LENGTH of enclosing element
479 0x05, 0x05, 0x07, 0x07, 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f
480 };
481 Block bad(MALFORMED, sizeof(MALFORMED));
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400482 BOOST_CHECK_EXCEPTION(bad.parse(), Block::Error, [] (const auto& e) {
483 return e.what() == "TLV-LENGTH of sub-element of type 7 exceeds TLV-VALUE boundary of parent block"s;
484 });
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700485}
486
Joao Pereira7476ebf2015-07-07 14:54:39 -0400487BOOST_AUTO_TEST_CASE(InsertBeginning)
488{
489 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000490 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
491 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
492 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400493
494 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
495 masterBlock.push_back(secondBlock);
496 masterBlock.push_back(thirdBlock);
497 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000498 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400499 BOOST_CHECK_EQUAL(*it == secondBlock, true);
500
501 it = masterBlock.insert(it, firstBlock);
502
503 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
504 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
505 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
506}
507
508BOOST_AUTO_TEST_CASE(InsertEnd)
509{
510 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000511 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
512 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
513 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400514
515 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
516 masterBlock.push_back(firstBlock);
517 masterBlock.push_back(secondBlock);
518 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
519 Block::element_const_iterator it = masterBlock.elements_end();
520 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
521
522 it = masterBlock.insert(it, thirdBlock);
523
524 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
525 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
526 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
527}
528
529BOOST_AUTO_TEST_CASE(InsertMiddle)
530{
531 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000532 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
533 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
534 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400535
536 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
537 masterBlock.push_back(firstBlock);
538 masterBlock.push_back(thirdBlock);
539 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000540 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400541 BOOST_CHECK_EQUAL(*it == firstBlock, true);
542
Junxiao Shidc4277a2017-07-17 11:34:02 +0000543 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400544
545 BOOST_CHECK_EQUAL(*it == secondBlock, true);
546 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
547 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
548}
549
550BOOST_AUTO_TEST_CASE(EraseSingleElement)
551{
552 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000553 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
554 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
555 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400556
557 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
558 masterBlock.push_back(firstBlock);
559 masterBlock.push_back(secondBlock);
560 masterBlock.push_back(thirdBlock);
561 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000562 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400563 it++;
564 BOOST_CHECK_EQUAL(*it == secondBlock, true);
565
566 it = masterBlock.erase(it);
567
568 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
569 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
570 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
571}
572
573BOOST_AUTO_TEST_CASE(EraseRange)
574{
575 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000576 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
577 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
578 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
579 Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName");
580 Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName");
581 Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400582
583 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
584 masterBlock.push_back(firstBlock);
585 masterBlock.push_back(secondBlock);
586 masterBlock.push_back(thirdBlock);
587 masterBlock.push_back(fourthBlock);
588 masterBlock.push_back(fifthBlock);
589 masterBlock.push_back(sixthBlock);
590 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000591 Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400592 itStart++;
593 Block::element_const_iterator itEnd = itStart + 3;
594 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
595 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
596
597 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
598
599 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
600 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
601 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
602}
603
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700604BOOST_AUTO_TEST_CASE(Remove)
605{
606 Block block(tlv::Data);
607 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
608 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400609 block.push_back(makeStringBlock(tlv::Name, "/test-prefix"));
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700610 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
611 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
612
613 BOOST_CHECK_EQUAL(5, block.elements_size());
Davide Pesavento6b330402019-04-24 00:14:01 -0400614 BOOST_CHECK_NO_THROW(block.remove(tlv::ContentType));
615 BOOST_REQUIRE_EQUAL(2, block.elements_size());
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700616
Davide Pesavento6b330402019-04-24 00:14:01 -0400617 auto elements = block.elements();
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700618 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
619 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
620 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
Davide Pesavento5f6865c2021-04-23 23:24:11 -0400621 BOOST_CHECK_EQUAL(readString(elements[1]).compare("/test-prefix"), 0);
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700622}
623
Junxiao Shidc4277a2017-07-17 11:34:02 +0000624BOOST_AUTO_TEST_SUITE_END() // SubElements
625
Davide Pesaventoa76ea9a2021-04-24 00:06:02 -0400626BOOST_AUTO_TEST_CASE(ToAsioConstBuffer)
627{
628 Block block = "0101A0"_block;
629 boost::asio::const_buffer buffer(block);
630 BOOST_CHECK_EQUAL(boost::asio::buffer_cast<const uint8_t*>(buffer), block.wire());
631 BOOST_CHECK_EQUAL(boost::asio::buffer_size(buffer), block.size());
632}
633
Junxiao Shidc4277a2017-07-17 11:34:02 +0000634BOOST_AUTO_TEST_CASE(Equality)
635{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400636 const uint8_t one[] = {0x08, 0x00};
637 Block a(one, sizeof(one));
638 Block b(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000639 BOOST_CHECK_EQUAL(a == b, true);
640 BOOST_CHECK_EQUAL(a != b, false);
641
Davide Pesaventob10024c2017-09-22 01:36:44 -0400642 const uint8_t two[] = {0x06, 0x00};
643 Block c(two, sizeof(two));
644 Block d(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000645 BOOST_CHECK_EQUAL(c == d, false);
646 BOOST_CHECK_EQUAL(c != d, true);
647
Davide Pesaventob10024c2017-09-22 01:36:44 -0400648 const uint8_t three[] = {0x06, 0x01, 0xcc};
649 Block e(two, sizeof(two));
650 Block f(three, sizeof(three));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000651 BOOST_CHECK_EQUAL(e == f, false);
652 BOOST_CHECK_EQUAL(e != f, true);
653}
654
Junxiao Shi72c0c642018-04-20 15:41:09 +0000655BOOST_AUTO_TEST_CASE(Print)
656{
657 // default constructed
658 Block b;
659 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
660
661 // zero length
662 b = "0700"_block;
663 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
664
665 // unparsed
666 b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
667 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
668 "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
669 // set and restore format flags
670 {
671 std::ostringstream oss;
672 oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
673 BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
674 }
675
676 // parsed
677 b = "FD010808 0502CADD 59024E42"_block;
678 b.parse();
679 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
680 "264[8]={5[2]=CADD,89[2]=4E42}");
681
682 // parsed then modified: print modified sub-elements
683 b = "FD010808 0502CADD 59024E42"_block;
684 b.parse();
685 b.erase(b.elements_begin());
686 b.push_back("10022386"_block);
687 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
688 "264[8]={89[2]=4E42,16[2]=2386}");
689}
690
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600691BOOST_AUTO_TEST_SUITE(BlockLiteral)
692
693BOOST_AUTO_TEST_CASE(Simple)
694{
Davide Pesavento6b330402019-04-24 00:14:01 -0400695 Block b0 = "4200"_block;
696 BOOST_CHECK_EQUAL(b0.type(), 0x42);
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600697 BOOST_CHECK_EQUAL(b0.value_size(), 0);
698
699 Block b1 = "0101A0"_block;
700 BOOST_CHECK_EQUAL(b1.type(), 0x01);
701 BOOST_REQUIRE_EQUAL(b1.value_size(), 1);
702 BOOST_CHECK_EQUAL(b1.value()[0], 0xA0);
703}
704
705BOOST_AUTO_TEST_CASE(Comment)
706{
707 Block b0 = "a2b0c0d2eBf0G.B 1+"_block;
708 BOOST_CHECK_EQUAL(b0.type(), 0x20);
709 BOOST_REQUIRE_EQUAL(b0.value_size(), 2);
710 BOOST_CHECK_EQUAL(b0.value()[0], 0xB0);
711 BOOST_CHECK_EQUAL(b0.value()[1], 0xB1);
712}
713
714BOOST_AUTO_TEST_CASE(BadInput)
715{
716 BOOST_CHECK_THROW(""_block, std::invalid_argument);
717 BOOST_CHECK_THROW("1"_block, std::invalid_argument);
718 BOOST_CHECK_THROW("333"_block, std::invalid_argument);
Davide Pesavento6b330402019-04-24 00:14:01 -0400719 BOOST_CHECK_THROW("xx yy zz"_block, std::invalid_argument); // only comments
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600720
Davide Pesavento6b330402019-04-24 00:14:01 -0400721 BOOST_CHECK_THROW("0000"_block, tlv::Error); // invalid type
722 BOOST_CHECK_THROW("0202C0"_block, tlv::Error); // truncated value
723 BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error); // trailing garbage
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600724}
725
726BOOST_AUTO_TEST_SUITE_END() // BlockLiteral
727
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100728BOOST_AUTO_TEST_SUITE_END() // TestBlock
729BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800730
Alexander Afanasyev74633892015-02-08 18:08:46 -0800731} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800732} // namespace ndn