blob: a3459e7af8e7cb9e80f95b7561964e68bc04977f [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 Pesaventoeee3e822016-11-26 19:19:34 +010022#include "encoding/block.hpp"
Joao Pereira7476ebf2015-07-07 14:54:39 -040023#include "encoding/block-helpers.hpp"
Alexander Afanasyev233750e2014-02-16 00:50:07 -080024
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070025#include "boost-test.hpp"
Junxiao Shi72c0c642018-04-20 15:41:09 +000026#include <boost/lexical_cast.hpp>
Junxiao Shidc4277a2017-07-17 11:34:02 +000027#include <cstring>
28#include <sstream>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010029
Alexander Afanasyev233750e2014-02-16 00:50:07 -080030namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080031namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080032
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Encoding)
34BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080035
Junxiao Shidc4277a2017-07-17 11:34:02 +000036BOOST_AUTO_TEST_SUITE(Construction)
37
38static const uint8_t TEST_BUFFER[] = {
39 0x00, 0x01, 0xfa, // ok
40 0x01, 0x01, 0xfb, // ok
41 0x03, 0x02, 0xff // bad: TLV-LENGTH is 2 but there's only 1-octet TLV-VALUE
Alexander Afanasyev74633892015-02-08 18:08:46 -080042};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080043
Junxiao Shidc4277a2017-07-17 11:34:02 +000044BOOST_AUTO_TEST_CASE(Empty)
Alexander Afanasyev74633892015-02-08 18:08:46 -080045{
Junxiao Shidc4277a2017-07-17 11:34:02 +000046 Block b;
47 BOOST_CHECK_EQUAL(b.empty(), true);
Alexander Afanasyev74633892015-02-08 18:08:46 -080048}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070049
Junxiao Shidc4277a2017-07-17 11:34:02 +000050BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080051{
Junxiao Shidc4277a2017-07-17 11:34:02 +000052 const uint8_t VALUE[4] = {0x11, 0x12, 0x13, 0x14};
53
54 EncodingBuffer encoder;
55 size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE));
56 encoder.prependVarNumber(length);
57 encoder.prependVarNumber(0xe0);
58
59 Block b = encoder.block();
60 BOOST_CHECK_EQUAL(b.type(), 0xe0);
61 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
62 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
63 VALUE, VALUE + sizeof(VALUE));
64
65 b = Block(encoder);
66 BOOST_CHECK_EQUAL(b.type(), 0xe0);
67 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
68 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
69 VALUE, VALUE + sizeof(VALUE));
Alexander Afanasyev74633892015-02-08 18:08:46 -080070}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070071
Junxiao Shidc4277a2017-07-17 11:34:02 +000072BOOST_AUTO_TEST_CASE(FromEmptyEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080073{
Junxiao Shidc4277a2017-07-17 11:34:02 +000074 EncodingBuffer encoder;
75 Block b;
76 BOOST_CHECK_THROW(b = Block(encoder), tlv::Error);
Alexander Afanasyev74633892015-02-08 18:08:46 -080077}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080078
Davide Pesaventoeee3e822016-11-26 19:19:34 +010079BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -070080{
81 static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
82 Block block(buffer, sizeof(buffer));
83
84 Block derivedBlock(block, block.begin(), block.end());
85 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
86 BOOST_CHECK(derivedBlock == block); // blocks should match
87
88 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
89 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
90 BOOST_CHECK(derivedBlock == Block(buffer + 2, 3));
91
92 Buffer otherBuffer(buffer, sizeof(buffer));
Junxiao Shidb7464d2017-07-13 03:11:17 +000093 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument);
94 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument);
95 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument);
Alexander Afanasyev4448d292015-08-09 20:11:37 -070096}
97
Davide Pesaventoeee3e822016-11-26 19:19:34 +010098BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -070099{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000100 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700101 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
102 };
103
Junxiao Shidc4277a2017-07-17 11:34:02 +0000104 Block b1(BUFFER, sizeof(BUFFER));
Eric Newberry6235aac2015-08-13 16:03:47 -0700105
Junxiao Shidc4277a2017-07-17 11:34:02 +0000106 Block b2(b1, b1.begin(), b1.end());
107 auto buf2 = b2.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700108
Junxiao Shidc4277a2017-07-17 11:34:02 +0000109 b1.parse();
110 b1.remove(tlv::Name);
111 b1.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700112
Junxiao Shidc4277a2017-07-17 11:34:02 +0000113 b2.parse();
Eric Newberry6235aac2015-08-13 16:03:47 -0700114
Junxiao Shidc4277a2017-07-17 11:34:02 +0000115 BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER));
116 BOOST_CHECK_EQUAL(buf2, b2.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700117}
118
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100119BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700120{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000121 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700122 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
123 };
124
Junxiao Shidc4277a2017-07-17 11:34:02 +0000125 Block b1(BUFFER, sizeof(BUFFER));
126 auto buf1 = b1.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700127
Junxiao Shidc4277a2017-07-17 11:34:02 +0000128 Block b2(b1, b1.begin(), b1.end());
Eric Newberry6235aac2015-08-13 16:03:47 -0700129
Junxiao Shidc4277a2017-07-17 11:34:02 +0000130 b2.parse();
131 b2.remove(tlv::Name);
132 b2.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700133
Junxiao Shidc4277a2017-07-17 11:34:02 +0000134 b1.parse();
135 BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER));
136 BOOST_CHECK_EQUAL(buf1, b1.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700137}
138
Junxiao Shidc4277a2017-07-17 11:34:02 +0000139BOOST_AUTO_TEST_CASE(FromType)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800140{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000141 Block b1(4);
142 BOOST_CHECK_EQUAL(b1.empty(), false);
143 BOOST_CHECK_EQUAL(b1.type(), 4);
144 BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH
145 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800146
Junxiao Shidc4277a2017-07-17 11:34:02 +0000147 Block b2(258);
148 BOOST_CHECK_EQUAL(b2.type(), 258);
149 BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH
150 BOOST_CHECK_EQUAL(b2.value_size(), 0);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700151}
152
Junxiao Shif0da7892015-04-04 22:16:16 -0700153BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700154{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000155 std::stringstream stream;
156 stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
157 stream.seekg(0);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700158
Junxiao Shidc4277a2017-07-17 11:34:02 +0000159 Block b = Block::fromStream(stream);
160 BOOST_CHECK_EQUAL(b.type(), 0);
161 BOOST_CHECK_EQUAL(b.size(), 3);
162 BOOST_CHECK_EQUAL(b.value_size(), 1);
163 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
164 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700165
Junxiao Shidc4277a2017-07-17 11:34:02 +0000166 b = Block::fromStream(stream);
167 BOOST_CHECK_EQUAL(b.type(), 1);
168 BOOST_CHECK_EQUAL(b.size(), 3);
169 BOOST_CHECK_EQUAL(b.value_size(), 1);
170 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
171 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700172
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600173 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700174}
175
Junxiao Shif0da7892015-04-04 22:16:16 -0700176BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
177{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000178 const uint8_t PACKET[] = {
Junxiao Shif0da7892015-04-04 22:16:16 -0700179 0x06, 0x20, // Data
180 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000181 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
182 0x08, 0x01, 0x31, // GenericNameComponent '1'
183 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shif0da7892015-04-04 22:16:16 -0700184 0x14, 0x00, // MetaInfo empty
185 0x15, 0x00, // Content empty
186 0x16, 0x05, // SignatureInfo
Junxiao Shidc4277a2017-07-17 11:34:02 +0000187 0x1b, 0x01, 0x01, // SignatureType RSA
188 0x1c, 0x00, // KeyLocator empty
Junxiao Shif0da7892015-04-04 22:16:16 -0700189 0x17, 0x00 // SignatureValue empty
190 };
191 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
192
193 std::stringstream stream;
194 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
195 stream.seekg(0);
196
Junxiao Shidc4277a2017-07-17 11:34:02 +0000197 Block b = Block::fromStream(stream);
198 BOOST_CHECK_EQUAL(b.type(), 6);
199 BOOST_CHECK_EQUAL(b.value_size(), 32);
200 b.parse();
Junxiao Shif0da7892015-04-04 22:16:16 -0700201}
202
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000203BOOST_AUTO_TEST_CASE(FromStreamZeroLength)
Junxiao Shif0da7892015-04-04 22:16:16 -0700204{
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000205 const uint8_t BUFFER[] = {0x70, 0x00,
206 0x71, 0x03, 0x86, 0x11, 0x24,
207 0x72, 0x00};
Junxiao Shif0da7892015-04-04 22:16:16 -0700208
209 std::stringstream stream;
210 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
211 stream.seekg(0);
212
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000213 Block b1 = Block::fromStream(stream);
214 BOOST_CHECK_EQUAL(b1.type(), 0x70);
215 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000216
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000217 Block b2 = Block::fromStream(stream);
218 BOOST_CHECK_EQUAL(b2.type(), 0x71);
219 BOOST_CHECK_EQUAL(b2.value_size(), 3);
220 const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24};
221 BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(),
222 EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2));
223
224 Block b3 = Block::fromStream(stream);
225 BOOST_CHECK_EQUAL(b3.type(), 0x72);
226 BOOST_CHECK_EQUAL(b3.value_size(), 0);
227
228 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
229}
230
231BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge)
232{
233 const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00};
234
235 std::stringstream stream;
236 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
237 for (int i = 0; i < 0x10000; ++i) {
238 stream.put('\0');
239 }
240 stream.seekg(0);
241
242 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000243}
244
245BOOST_AUTO_TEST_CASE(FromWireBuffer)
246{
247 ConstBufferPtr buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
248
249 size_t offset = 0;
250 bool isOk = false;
251 Block b;
252 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
253 BOOST_CHECK(isOk);
254 BOOST_CHECK_EQUAL(b.type(), 0);
255 BOOST_CHECK_EQUAL(b.size(), 3);
256 BOOST_CHECK_EQUAL(b.value_size(), 1);
257 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
258 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
259 offset += b.size();
260
261 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
262 BOOST_CHECK(isOk);
263 BOOST_CHECK_EQUAL(b.type(), 1);
264 BOOST_CHECK_EQUAL(b.size(), 3);
265 BOOST_CHECK_EQUAL(b.value_size(), 1);
266 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
267 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
268 offset += b.size();
269
270 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
271 BOOST_CHECK(!isOk);
272}
273
274BOOST_AUTO_TEST_CASE(FromRawBuffer)
275{
276 size_t offset = 0;
277 bool isOk = false;
278 Block b;
279 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
280 BOOST_CHECK(isOk);
281 BOOST_CHECK_EQUAL(b.type(), 0);
282 BOOST_CHECK_EQUAL(b.size(), 3);
283 BOOST_CHECK_EQUAL(b.value_size(), 1);
284 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
285 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
286 offset += b.size();
287
288 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
289 BOOST_CHECK(isOk);
290 BOOST_CHECK_EQUAL(b.type(), 1);
291 BOOST_CHECK_EQUAL(b.size(), 3);
292 BOOST_CHECK_EQUAL(b.value_size(), 1);
293 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
294 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
295 offset += b.size();
296
297 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
298 BOOST_CHECK(!isOk);
Junxiao Shif0da7892015-04-04 22:16:16 -0700299}
300
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100301BOOST_AUTO_TEST_SUITE_END() // Construction
302
Junxiao Shidc4277a2017-07-17 11:34:02 +0000303BOOST_AUTO_TEST_SUITE(SubElements)
304
305BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700306{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000307 const uint8_t PACKET[] = {
308 0x06, 0x20, // Data
309 0x07, 0x11, // Name
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000310 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // GenericNameComponent 'hello'
311 0x08, 0x01, 0x31, // GenericNameComponent '1'
312 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // GenericNameComponent 'world'
Junxiao Shidc4277a2017-07-17 11:34:02 +0000313 0x14, 0x00, // MetaInfo empty
314 0x15, 0x00, // Content empty
315 0x16, 0x05, // SignatureInfo
316 0x1b, 0x01, 0x01, // SignatureType RSA
317 0x1c, 0x00, // KeyLocator empty
318 0x17, 0x00 // SignatureValue empty
319 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700320
Junxiao Shidc4277a2017-07-17 11:34:02 +0000321 Block data(PACKET, sizeof(PACKET));
322 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700323
Junxiao Shidc4277a2017-07-17 11:34:02 +0000324 BOOST_CHECK_EQUAL(data.elements_size(), 5);
325 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
326 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700327
Junxiao Shidc4277a2017-07-17 11:34:02 +0000328 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
329 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
330
331 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
332 BOOST_CHECK(data.find(0x01) == data.elements_end());
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700333}
334
Joao Pereira7476ebf2015-07-07 14:54:39 -0400335BOOST_AUTO_TEST_CASE(InsertBeginning)
336{
337 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000338 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
339 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
340 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400341
342 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
343 masterBlock.push_back(secondBlock);
344 masterBlock.push_back(thirdBlock);
345 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000346 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400347 BOOST_CHECK_EQUAL(*it == secondBlock, true);
348
349 it = masterBlock.insert(it, firstBlock);
350
351 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
352 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
353 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
354}
355
356BOOST_AUTO_TEST_CASE(InsertEnd)
357{
358 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000359 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
360 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
361 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400362
363 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
364 masterBlock.push_back(firstBlock);
365 masterBlock.push_back(secondBlock);
366 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
367 Block::element_const_iterator it = masterBlock.elements_end();
368 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
369
370 it = masterBlock.insert(it, thirdBlock);
371
372 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
373 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
374 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
375}
376
377BOOST_AUTO_TEST_CASE(InsertMiddle)
378{
379 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000380 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
381 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
382 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400383
384 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
385 masterBlock.push_back(firstBlock);
386 masterBlock.push_back(thirdBlock);
387 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000388 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400389 BOOST_CHECK_EQUAL(*it == firstBlock, true);
390
Junxiao Shidc4277a2017-07-17 11:34:02 +0000391 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400392
393 BOOST_CHECK_EQUAL(*it == secondBlock, true);
394 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
395 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
396}
397
398BOOST_AUTO_TEST_CASE(EraseSingleElement)
399{
400 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000401 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
402 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
403 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400404
405 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
406 masterBlock.push_back(firstBlock);
407 masterBlock.push_back(secondBlock);
408 masterBlock.push_back(thirdBlock);
409 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000410 Block::element_const_iterator it = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400411 it++;
412 BOOST_CHECK_EQUAL(*it == secondBlock, true);
413
414 it = masterBlock.erase(it);
415
416 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
417 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
418 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
419}
420
421BOOST_AUTO_TEST_CASE(EraseRange)
422{
423 Block masterBlock(tlv::Name);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000424 Block firstBlock = makeStringBlock(tlv::GenericNameComponent, "firstName");
425 Block secondBlock = makeStringBlock(tlv::GenericNameComponent, "secondName");
426 Block thirdBlock = makeStringBlock(tlv::GenericNameComponent, "thirdName");
427 Block fourthBlock = makeStringBlock(tlv::GenericNameComponent, "fourthName");
428 Block fifthBlock = makeStringBlock(tlv::GenericNameComponent, "fifthName");
429 Block sixthBlock = makeStringBlock(tlv::GenericNameComponent, "sixthName");
Joao Pereira7476ebf2015-07-07 14:54:39 -0400430
431 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
432 masterBlock.push_back(firstBlock);
433 masterBlock.push_back(secondBlock);
434 masterBlock.push_back(thirdBlock);
435 masterBlock.push_back(fourthBlock);
436 masterBlock.push_back(fifthBlock);
437 masterBlock.push_back(sixthBlock);
438 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
Junxiao Shi4ffbb9d2018-03-31 17:16:35 +0000439 Block::element_const_iterator itStart = masterBlock.find(tlv::GenericNameComponent);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400440 itStart++;
441 Block::element_const_iterator itEnd = itStart + 3;
442 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
443 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
444
445 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
446
447 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
448 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
449 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
450}
451
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700452BOOST_AUTO_TEST_CASE(Remove)
453{
454 Block block(tlv::Data);
455 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
456 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
457 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
458 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
459 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
460
461 BOOST_CHECK_EQUAL(5, block.elements_size());
462 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
463 BOOST_CHECK_EQUAL(2, block.elements_size());
464
465 Block::element_container elements = block.elements();
466
467 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
468 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
469 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
470 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
471}
472
Junxiao Shidc4277a2017-07-17 11:34:02 +0000473BOOST_AUTO_TEST_SUITE_END() // SubElements
474
475BOOST_AUTO_TEST_CASE(Equality)
476{
Davide Pesaventob10024c2017-09-22 01:36:44 -0400477 const uint8_t one[] = {0x08, 0x00};
478 Block a(one, sizeof(one));
479 Block b(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000480 BOOST_CHECK_EQUAL(a == b, true);
481 BOOST_CHECK_EQUAL(a != b, false);
482
Davide Pesaventob10024c2017-09-22 01:36:44 -0400483 const uint8_t two[] = {0x06, 0x00};
484 Block c(two, sizeof(two));
485 Block d(one, sizeof(one));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000486 BOOST_CHECK_EQUAL(c == d, false);
487 BOOST_CHECK_EQUAL(c != d, true);
488
Davide Pesaventob10024c2017-09-22 01:36:44 -0400489 const uint8_t three[] = {0x06, 0x01, 0xcc};
490 Block e(two, sizeof(two));
491 Block f(three, sizeof(three));
Junxiao Shidc4277a2017-07-17 11:34:02 +0000492 BOOST_CHECK_EQUAL(e == f, false);
493 BOOST_CHECK_EQUAL(e != f, true);
494}
495
Junxiao Shi72c0c642018-04-20 15:41:09 +0000496BOOST_AUTO_TEST_CASE(Print)
497{
498 // default constructed
499 Block b;
500 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "[invalid]");
501
502 // zero length
503 b = "0700"_block;
504 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b), "7[empty]");
505
506 // unparsed
507 b = "0E10FF7E4E6B3B21C902660F16ED589FCCCC"_block;
508 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
509 "14[16]=FF7E4E6B3B21C902660F16ED589FCCCC");
510 // set and restore format flags
511 {
512 std::ostringstream oss;
513 oss << std::showbase << std::hex << 0xd23c4 << b << 0x4981e;
514 BOOST_CHECK_EQUAL(oss.str(), "0xd23c414[16]=FF7E4E6B3B21C902660F16ED589FCCCC0x4981e");
515 }
516
517 // parsed
518 b = "FD010808 0502CADD 59024E42"_block;
519 b.parse();
520 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
521 "264[8]={5[2]=CADD,89[2]=4E42}");
522
523 // parsed then modified: print modified sub-elements
524 b = "FD010808 0502CADD 59024E42"_block;
525 b.parse();
526 b.erase(b.elements_begin());
527 b.push_back("10022386"_block);
528 BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(b),
529 "264[8]={89[2]=4E42,16[2]=2386}");
530}
531
Junxiao Shi2b322eb2018-08-24 10:43:27 -0600532BOOST_AUTO_TEST_SUITE(BlockLiteral)
533
534BOOST_AUTO_TEST_CASE(Simple)
535{
536 Block b0 = "0000"_block;
537 BOOST_CHECK_EQUAL(b0.type(), 0x00);
538 BOOST_CHECK_EQUAL(b0.value_size(), 0);
539
540 Block b1 = "0101A0"_block;
541 BOOST_CHECK_EQUAL(b1.type(), 0x01);
542 BOOST_REQUIRE_EQUAL(b1.value_size(), 1);
543 BOOST_CHECK_EQUAL(b1.value()[0], 0xA0);
544}
545
546BOOST_AUTO_TEST_CASE(Comment)
547{
548 Block b0 = "a2b0c0d2eBf0G.B 1+"_block;
549 BOOST_CHECK_EQUAL(b0.type(), 0x20);
550 BOOST_REQUIRE_EQUAL(b0.value_size(), 2);
551 BOOST_CHECK_EQUAL(b0.value()[0], 0xB0);
552 BOOST_CHECK_EQUAL(b0.value()[1], 0xB1);
553}
554
555BOOST_AUTO_TEST_CASE(BadInput)
556{
557 BOOST_CHECK_THROW(""_block, std::invalid_argument);
558 BOOST_CHECK_THROW("1"_block, std::invalid_argument);
559 BOOST_CHECK_THROW("333"_block, std::invalid_argument);
560
561 BOOST_CHECK_THROW("0202C0"_block, tlv::Error);
562 BOOST_CHECK_THROW("0201C0C1"_block, tlv::Error);
563}
564
565BOOST_AUTO_TEST_SUITE_END() // BlockLiteral
566
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100567BOOST_AUTO_TEST_SUITE_END() // TestBlock
568BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800569
Alexander Afanasyev74633892015-02-08 18:08:46 -0800570} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800571} // namespace ndn