blob: dd7e3b035bb3ca1959202f34343736c968371daa [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/*
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +00003 * Copyright (c) 2013-2018 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>
Junxiao Shidc4277a2017-07-17 11:34:02 +000028#include <cstring>
29#include <sstream>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030
Alexander Afanasyev233750e2014-02-16 00:50:07 -080031namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080032namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080033
Davide Pesaventoeee3e822016-11-26 19:19:34 +010034BOOST_AUTO_TEST_SUITE(Encoding)
35BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080036
Junxiao Shidc4277a2017-07-17 11:34:02 +000037BOOST_AUTO_TEST_SUITE(Construction)
38
39static const uint8_t TEST_BUFFER[] = {
40 0x00, 0x01, 0xfa, // ok
41 0x01, 0x01, 0xfb, // ok
42 0x03, 0x02, 0xff // bad: TLV-LENGTH is 2 but there's only 1-octet TLV-VALUE
Alexander Afanasyev74633892015-02-08 18:08:46 -080043};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080044
Junxiao Shidc4277a2017-07-17 11:34:02 +000045BOOST_AUTO_TEST_CASE(Empty)
Alexander Afanasyev74633892015-02-08 18:08:46 -080046{
Junxiao Shidc4277a2017-07-17 11:34:02 +000047 Block b;
48 BOOST_CHECK_EQUAL(b.empty(), true);
Alexander Afanasyev74633892015-02-08 18:08:46 -080049}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070050
Junxiao Shidc4277a2017-07-17 11:34:02 +000051BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080052{
Junxiao Shidc4277a2017-07-17 11:34:02 +000053 const uint8_t VALUE[4] = {0x11, 0x12, 0x13, 0x14};
54
55 EncodingBuffer encoder;
56 size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE));
57 encoder.prependVarNumber(length);
58 encoder.prependVarNumber(0xe0);
59
60 Block b = encoder.block();
61 BOOST_CHECK_EQUAL(b.type(), 0xe0);
62 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
63 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
64 VALUE, VALUE + sizeof(VALUE));
65
66 b = Block(encoder);
67 BOOST_CHECK_EQUAL(b.type(), 0xe0);
68 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
69 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
70 VALUE, VALUE + sizeof(VALUE));
Alexander Afanasyev74633892015-02-08 18:08:46 -080071}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070072
Junxiao Shidc4277a2017-07-17 11:34:02 +000073BOOST_AUTO_TEST_CASE(FromEmptyEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080074{
Junxiao Shidc4277a2017-07-17 11:34:02 +000075 EncodingBuffer encoder;
76 Block b;
77 BOOST_CHECK_THROW(b = Block(encoder), tlv::Error);
Alexander Afanasyev74633892015-02-08 18:08:46 -080078}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080079
Davide Pesaventoeee3e822016-11-26 19:19:34 +010080BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -070081{
82 static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
83 Block block(buffer, sizeof(buffer));
84
85 Block derivedBlock(block, block.begin(), block.end());
86 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
87 BOOST_CHECK(derivedBlock == block); // blocks should match
88
89 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
90 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
91 BOOST_CHECK(derivedBlock == Block(buffer + 2, 3));
92
93 Buffer otherBuffer(buffer, sizeof(buffer));
Junxiao Shidb7464d2017-07-13 03:11:17 +000094 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument);
95 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument);
96 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument);
Alexander Afanasyev4448d292015-08-09 20:11:37 -070097}
98
Davide Pesaventoeee3e822016-11-26 19:19:34 +010099BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -0700100{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000101 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700102 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
103 };
104
Junxiao Shidc4277a2017-07-17 11:34:02 +0000105 Block b1(BUFFER, sizeof(BUFFER));
Eric Newberry6235aac2015-08-13 16:03:47 -0700106
Junxiao Shidc4277a2017-07-17 11:34:02 +0000107 Block b2(b1, b1.begin(), b1.end());
108 auto buf2 = b2.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700109
Junxiao Shidc4277a2017-07-17 11:34:02 +0000110 b1.parse();
111 b1.remove(tlv::Name);
112 b1.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700113
Junxiao Shidc4277a2017-07-17 11:34:02 +0000114 b2.parse();
Eric Newberry6235aac2015-08-13 16:03:47 -0700115
Junxiao Shidc4277a2017-07-17 11:34:02 +0000116 BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER));
117 BOOST_CHECK_EQUAL(buf2, b2.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700118}
119
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100120BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700121{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000122 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700123 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
124 };
125
Junxiao Shidc4277a2017-07-17 11:34:02 +0000126 Block b1(BUFFER, sizeof(BUFFER));
127 auto buf1 = b1.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700128
Junxiao Shidc4277a2017-07-17 11:34:02 +0000129 Block b2(b1, b1.begin(), b1.end());
Eric Newberry6235aac2015-08-13 16:03:47 -0700130
Junxiao Shidc4277a2017-07-17 11:34:02 +0000131 b2.parse();
132 b2.remove(tlv::Name);
133 b2.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700134
Junxiao Shidc4277a2017-07-17 11:34:02 +0000135 b1.parse();
136 BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER));
137 BOOST_CHECK_EQUAL(buf1, b1.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700138}
139
Junxiao Shidc4277a2017-07-17 11:34:02 +0000140BOOST_AUTO_TEST_CASE(FromType)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800141{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000142 Block b1(4);
143 BOOST_CHECK_EQUAL(b1.empty(), false);
144 BOOST_CHECK_EQUAL(b1.type(), 4);
145 BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH
146 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800147
Junxiao Shidc4277a2017-07-17 11:34:02 +0000148 Block b2(258);
149 BOOST_CHECK_EQUAL(b2.type(), 258);
150 BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH
151 BOOST_CHECK_EQUAL(b2.value_size(), 0);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700152}
153
Junxiao Shif0da7892015-04-04 22:16:16 -0700154BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700155{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000156 std::stringstream stream;
157 stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
158 stream.seekg(0);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700159
Junxiao Shidc4277a2017-07-17 11:34:02 +0000160 Block b = Block::fromStream(stream);
161 BOOST_CHECK_EQUAL(b.type(), 0);
162 BOOST_CHECK_EQUAL(b.size(), 3);
163 BOOST_CHECK_EQUAL(b.value_size(), 1);
164 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
165 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700166
Junxiao Shidc4277a2017-07-17 11:34:02 +0000167 b = Block::fromStream(stream);
168 BOOST_CHECK_EQUAL(b.type(), 1);
169 BOOST_CHECK_EQUAL(b.size(), 3);
170 BOOST_CHECK_EQUAL(b.value_size(), 1);
171 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
172 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700173
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600174 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700175}
176
Junxiao Shif0da7892015-04-04 22:16:16 -0700177BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
178{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000179 const uint8_t PACKET[] = {
Junxiao Shif0da7892015-04-04 22:16:16 -0700180 0x06, 0x20, // Data
181 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000182 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
183 0x08, 0x01, 0x31, // GenericNameComponent '1'
184 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shif0da7892015-04-04 22:16:16 -0700185 0x14, 0x00, // MetaInfo empty
186 0x15, 0x00, // Content empty
187 0x16, 0x05, // SignatureInfo
Junxiao Shidc4277a2017-07-17 11:34:02 +0000188 0x1b, 0x01, 0x01, // SignatureType RSA
189 0x1c, 0x00, // KeyLocator empty
Junxiao Shif0da7892015-04-04 22:16:16 -0700190 0x17, 0x00 // SignatureValue empty
191 };
192 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
193
194 std::stringstream stream;
195 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
196 stream.seekg(0);
197
Junxiao Shidc4277a2017-07-17 11:34:02 +0000198 Block b = Block::fromStream(stream);
199 BOOST_CHECK_EQUAL(b.type(), 6);
200 BOOST_CHECK_EQUAL(b.value_size(), 32);
201 b.parse();
Junxiao Shif0da7892015-04-04 22:16:16 -0700202}
203
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000204BOOST_AUTO_TEST_CASE(FromStreamZeroLength)
Junxiao Shif0da7892015-04-04 22:16:16 -0700205{
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000206 const uint8_t BUFFER[] = {0x70, 0x00,
207 0x71, 0x03, 0x86, 0x11, 0x24,
208 0x72, 0x00};
Junxiao Shif0da7892015-04-04 22:16:16 -0700209
210 std::stringstream stream;
211 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
212 stream.seekg(0);
213
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000214 Block b1 = Block::fromStream(stream);
215 BOOST_CHECK_EQUAL(b1.type(), 0x70);
216 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000217
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000218 Block b2 = Block::fromStream(stream);
219 BOOST_CHECK_EQUAL(b2.type(), 0x71);
220 BOOST_CHECK_EQUAL(b2.value_size(), 3);
221 const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24};
222 BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(),
223 EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2));
224
225 Block b3 = Block::fromStream(stream);
226 BOOST_CHECK_EQUAL(b3.type(), 0x72);
227 BOOST_CHECK_EQUAL(b3.value_size(), 0);
228
229 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
230}
231
232BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge)
233{
234 const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00};
235
236 std::stringstream stream;
237 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
238 for (int i = 0; i < 0x10000; ++i) {
239 stream.put('\0');
240 }
241 stream.seekg(0);
242
243 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000244}
245
246BOOST_AUTO_TEST_CASE(FromWireBuffer)
247{
248 ConstBufferPtr buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
249
250 size_t offset = 0;
251 bool isOk = false;
252 Block b;
253 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
254 BOOST_CHECK(isOk);
255 BOOST_CHECK_EQUAL(b.type(), 0);
256 BOOST_CHECK_EQUAL(b.size(), 3);
257 BOOST_CHECK_EQUAL(b.value_size(), 1);
258 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
259 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
260 offset += b.size();
261
262 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
263 BOOST_CHECK(isOk);
264 BOOST_CHECK_EQUAL(b.type(), 1);
265 BOOST_CHECK_EQUAL(b.size(), 3);
266 BOOST_CHECK_EQUAL(b.value_size(), 1);
267 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
268 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
269 offset += b.size();
270
271 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
272 BOOST_CHECK(!isOk);
273}
274
275BOOST_AUTO_TEST_CASE(FromRawBuffer)
276{
277 size_t offset = 0;
278 bool isOk = false;
279 Block b;
280 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
281 BOOST_CHECK(isOk);
282 BOOST_CHECK_EQUAL(b.type(), 0);
283 BOOST_CHECK_EQUAL(b.size(), 3);
284 BOOST_CHECK_EQUAL(b.value_size(), 1);
285 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
286 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
287 offset += b.size();
288
289 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
290 BOOST_CHECK(isOk);
291 BOOST_CHECK_EQUAL(b.type(), 1);
292 BOOST_CHECK_EQUAL(b.size(), 3);
293 BOOST_CHECK_EQUAL(b.value_size(), 1);
294 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
295 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
296 offset += b.size();
297
298 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
299 BOOST_CHECK(!isOk);
Junxiao Shif0da7892015-04-04 22:16:16 -0700300}
301
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100302BOOST_AUTO_TEST_SUITE_END() // Construction
303
Junxiao Shidc4277a2017-07-17 11:34:02 +0000304BOOST_AUTO_TEST_SUITE(SubElements)
305
306BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700307{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000308 const uint8_t PACKET[] = {
309 0x06, 0x20, // Data
310 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000311 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
312 0x08, 0x01, 0x31, // GenericNameComponent '1'
313 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shidc4277a2017-07-17 11:34:02 +0000314 0x14, 0x00, // MetaInfo empty
315 0x15, 0x00, // Content empty
316 0x16, 0x05, // SignatureInfo
317 0x1b, 0x01, 0x01, // SignatureType RSA
318 0x1c, 0x00, // KeyLocator empty
319 0x17, 0x00 // SignatureValue empty
320 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700321
Junxiao Shidc4277a2017-07-17 11:34:02 +0000322 Block data(PACKET, sizeof(PACKET));
323 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700324
Junxiao Shidc4277a2017-07-17 11:34:02 +0000325 BOOST_CHECK_EQUAL(data.elements_size(), 5);
326 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
327 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700328
Junxiao Shidc4277a2017-07-17 11:34:02 +0000329 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
330 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
331
332 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
333 BOOST_CHECK(data.find(0x01) == data.elements_end());
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700334}
335
Joao Pereira7476ebf2015-07-07 14:54:39 -0400336BOOST_AUTO_TEST_CASE(InsertBeginning)
337{
338 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000339 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
340 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
341 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400342
343 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
344 masterBlock.push_back(secondBlock);
345 masterBlock.push_back(thirdBlock);
346 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000347 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400348 BOOST_CHECK_EQUAL(*it == secondBlock, true);
349
350 it = masterBlock.insert(it, firstBlock);
351
352 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
353 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
354 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
355}
356
357BOOST_AUTO_TEST_CASE(InsertEnd)
358{
359 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000360 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
361 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
362 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400363
364 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
365 masterBlock.push_back(firstBlock);
366 masterBlock.push_back(secondBlock);
367 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
368 Block::element_const_iterator it = masterBlock.elements_end();
369 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
370
371 it = masterBlock.insert(it, thirdBlock);
372
373 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
374 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
375 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
376}
377
378BOOST_AUTO_TEST_CASE(InsertMiddle)
379{
380 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000381 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
382 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
383 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400384
385 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
386 masterBlock.push_back(firstBlock);
387 masterBlock.push_back(thirdBlock);
388 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000389 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400390 BOOST_CHECK_EQUAL(*it == firstBlock, true);
391
Junxiao Shidc4277a2017-07-17 11:34:02 +0000392 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400393
394 BOOST_CHECK_EQUAL(*it == secondBlock, true);
395 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
396 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
397}
398
399BOOST_AUTO_TEST_CASE(EraseSingleElement)
400{
401 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000402 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
403 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
404 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400405
406 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
407 masterBlock.push_back(firstBlock);
408 masterBlock.push_back(secondBlock);
409 masterBlock.push_back(thirdBlock);
410 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000411 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400412 it++;
413 BOOST_CHECK_EQUAL(*it == secondBlock, true);
414
415 it = masterBlock.erase(it);
416
417 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
418 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
419 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
420}
421
422BOOST_AUTO_TEST_CASE(EraseRange)
423{
424 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000425 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
426 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
427 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
428 Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName");
429 Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName");
430 Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400431
432 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
433 masterBlock.push_back(firstBlock);
434 masterBlock.push_back(secondBlock);
435 masterBlock.push_back(thirdBlock);
436 masterBlock.push_back(fourthBlock);
437 masterBlock.push_back(fifthBlock);
438 masterBlock.push_back(sixthBlock);
439 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000440 Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400441 itStart++;
442 Block::element_const_iterator itEnd = itStart + 3;
443 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
444 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
445
446 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
447
448 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
449 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
450 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
451}
452
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700453BOOST_AUTO_TEST_CASE(Remove)
454{
455 Block block(tlv::Data);
456 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
457 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
458 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
459 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
460 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
461
462 BOOST_CHECK_EQUAL(5, block.elements_size());
463 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
464 BOOST_CHECK_EQUAL(2, block.elements_size());
465
466 Block::element_container elements = block.elements();
467
468 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
469 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
470 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
471 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
472}
473
Junxiao Shidc4277a2017-07-17 11:34:02 +0000474BOOST_AUTO_TEST_SUITE_END() // SubElements
475
476BOOST_AUTO_TEST_CASE(Equality)
477{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400478 const uint8_t one[] = {0x08, 0x00};
479 Block a(one, sizeof(one));
480 Block b(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000481 BOOST_CHECK_EQUAL(a == b, true);
482 BOOST_CHECK_EQUAL(a != b, false);
483
Davide Pesaventob10024c2017-09-22 01:36:44 -0400484 const uint8_t two[] = {0x06, 0x00};
485 Block c(two, sizeof(two));
486 Block d(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000487 BOOST_CHECK_EQUAL(c == d, false);
488 BOOST_CHECK_EQUAL(c != d, true);
489
Davide Pesaventob10024c2017-09-22 01:36:44 -0400490 const uint8_t three[] = {0x06, 0x01, 0xcc};
491 Block e(two, sizeof(two));
492 Block f(three, sizeof(three));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000493 BOOST_CHECK_EQUAL(e == f, false);
494 BOOST_CHECK_EQUAL(e != f, true);
495}
496
Junxiao Shi72c0c642018-04-20 15:41:09 +0000497BOOST_AUTO_TEST_CASE(Print)
498{
499 // default constructed
500 Block b;
501 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
502
503 // zero length
504 b = "0700"_block;
505 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
506
507 // unparsed
508 b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
509 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
510 "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
511 // set and restore format flags
512 {
513 std::ostringstream oss;
514 oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
515 BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
516 }
517
518 // parsed
519 b = "FD010808 0502CADD 59024E42"_block;
520 b.parse();
521 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
522 "264[8]={5[2]=CADD,89[2]=4E42}");
523
524 // parsed then modified: print modified sub-elements
525 b = "FD010808 0502CADD 59024E42"_block;
526 b.parse();
527 b.erase(b.elements_begin());
528 b.push_back("10022386"_block);
529 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
530 "264[8]={89[2]=4E42,16[2]=2386}");
531}
532
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600533BOOST_AUTO_TEST_SUITE(BlockLiteral)
534
535BOOST_AUTO_TEST_CASE(Simple)
536{
537 Block b0 = "0000"_block;
538 BOOST_CHECK_EQUAL(b0.type(), 0x00);
539 BOOST_CHECK_EQUAL(b0.value_size(), 0);
540
541 Block b1 = "0101A0"_block;
542 BOOST_CHECK_EQUAL(b1.type(), 0x01);
543 BOOST_REQUIRE_EQUAL(b1.value_size(), 1);
544 BOOST_CHECK_EQUAL(b1.value()[0], 0xA0);
545}
546
547BOOST_AUTO_TEST_CASE(Comment)
548{
549 Block b0 = "a2b0c0d2eBf0G.B 1+"_block;
550 BOOST_CHECK_EQUAL(b0.type(), 0x20);
551 BOOST_REQUIRE_EQUAL(b0.value_size(), 2);
552 BOOST_CHECK_EQUAL(b0.value()[0], 0xB0);
553 BOOST_CHECK_EQUAL(b0.value()[1], 0xB1);
554}
555
556BOOST_AUTO_TEST_CASE(BadInput)
557{
558 BOOST_CHECK_THROW(""_block, std::invalid_argument);
559 BOOST_CHECK_THROW("1"_block, std::invalid_argument);
560 BOOST_CHECK_THROW("333"_block, std::invalid_argument);
561
562 BOOST_CHECK_THROW("0202C0"_block, tlv::Error);
563 BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error);
564}
565
566BOOST_AUTO_TEST_SUITE_END() // BlockLiteral
567
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100568BOOST_AUTO_TEST_SUITE_END() // TestBlock
569BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800570
Alexander Afanasyev74633892015-02-08 18:08:46 -0800571} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800572} // namespace ndn