blob: 0036175da8a28c4be5a0b076427f4a63176d6604 [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/*
3 * Copyright (c) 2013-2017 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 Shidc4277a2017-07-17 11:34:02 +000026#include <cstring>
27#include <sstream>
Davide Pesaventoeee3e822016-11-26 19:19:34 +010028
Alexander Afanasyev233750e2014-02-16 00:50:07 -080029namespace ndn {
Alexander Afanasyev74633892015-02-08 18:08:46 -080030namespace tests {
Alexander Afanasyev233750e2014-02-16 00:50:07 -080031
Davide Pesaventoeee3e822016-11-26 19:19:34 +010032BOOST_AUTO_TEST_SUITE(Encoding)
33BOOST_AUTO_TEST_SUITE(TestBlock)
Alexander Afanasyev233750e2014-02-16 00:50:07 -080034
Junxiao Shidc4277a2017-07-17 11:34:02 +000035BOOST_AUTO_TEST_SUITE(Construction)
36
37static const uint8_t TEST_BUFFER[] = {
38 0x00, 0x01, 0xfa, // ok
39 0x01, 0x01, 0xfb, // ok
40 0x03, 0x02, 0xff // bad: TLV-LENGTH is 2 but there's only 1-octet TLV-VALUE
Alexander Afanasyev74633892015-02-08 18:08:46 -080041};
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080042
Junxiao Shidc4277a2017-07-17 11:34:02 +000043BOOST_AUTO_TEST_CASE(Empty)
Alexander Afanasyev74633892015-02-08 18:08:46 -080044{
Junxiao Shidc4277a2017-07-17 11:34:02 +000045 Block b;
46 BOOST_CHECK_EQUAL(b.empty(), true);
Alexander Afanasyev74633892015-02-08 18:08:46 -080047}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070048
Junxiao Shidc4277a2017-07-17 11:34:02 +000049BOOST_AUTO_TEST_CASE(FromEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080050{
Junxiao Shidc4277a2017-07-17 11:34:02 +000051 const uint8_t VALUE[4] = {0x11, 0x12, 0x13, 0x14};
52
53 EncodingBuffer encoder;
54 size_t length = encoder.prependByteArray(VALUE, sizeof(VALUE));
55 encoder.prependVarNumber(length);
56 encoder.prependVarNumber(0xe0);
57
58 Block b = encoder.block();
59 BOOST_CHECK_EQUAL(b.type(), 0xe0);
60 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
61 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
62 VALUE, VALUE + sizeof(VALUE));
63
64 b = Block(encoder);
65 BOOST_CHECK_EQUAL(b.type(), 0xe0);
66 BOOST_CHECK_EQUAL(b.value_size(), sizeof(VALUE));
67 BOOST_CHECK_EQUAL_COLLECTIONS(b.value_begin(), b.value_end(),
68 VALUE, VALUE + sizeof(VALUE));
Alexander Afanasyev74633892015-02-08 18:08:46 -080069}
Alexander Afanasyev937aa782014-03-21 13:17:57 -070070
Junxiao Shidc4277a2017-07-17 11:34:02 +000071BOOST_AUTO_TEST_CASE(FromEmptyEncodingBuffer)
Alexander Afanasyev74633892015-02-08 18:08:46 -080072{
Junxiao Shidc4277a2017-07-17 11:34:02 +000073 EncodingBuffer encoder;
74 Block b;
75 BOOST_CHECK_THROW(b = Block(encoder), tlv::Error);
Alexander Afanasyev74633892015-02-08 18:08:46 -080076}
Alexander Afanasyev2d0b4572014-02-24 16:02:12 -080077
Davide Pesaventoeee3e822016-11-26 19:19:34 +010078BOOST_AUTO_TEST_CASE(FromBlock)
Alexander Afanasyev4448d292015-08-09 20:11:37 -070079{
80 static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01};
81 Block block(buffer, sizeof(buffer));
82
83 Block derivedBlock(block, block.begin(), block.end());
84 BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match
85 BOOST_CHECK(derivedBlock == block); // blocks should match
86
87 derivedBlock = Block(block, block.begin() + 2, block.begin() + 5);
88 BOOST_CHECK(derivedBlock.begin() == block.begin() + 2);
89 BOOST_CHECK(derivedBlock == Block(buffer + 2, 3));
90
91 Buffer otherBuffer(buffer, sizeof(buffer));
Junxiao Shidb7464d2017-07-13 03:11:17 +000092 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument);
93 BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument);
94 BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument);
Alexander Afanasyev4448d292015-08-09 20:11:37 -070095}
96
Davide Pesaventoeee3e822016-11-26 19:19:34 +010097BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal)
Eric Newberry6235aac2015-08-13 16:03:47 -070098{
Junxiao Shidc4277a2017-07-17 11:34:02 +000099 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700100 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
101 };
102
Junxiao Shidc4277a2017-07-17 11:34:02 +0000103 Block b1(BUFFER, sizeof(BUFFER));
Eric Newberry6235aac2015-08-13 16:03:47 -0700104
Junxiao Shidc4277a2017-07-17 11:34:02 +0000105 Block b2(b1, b1.begin(), b1.end());
106 auto buf2 = b2.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700107
Junxiao Shidc4277a2017-07-17 11:34:02 +0000108 b1.parse();
109 b1.remove(tlv::Name);
110 b1.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700111
Junxiao Shidc4277a2017-07-17 11:34:02 +0000112 b2.parse();
Eric Newberry6235aac2015-08-13 16:03:47 -0700113
Junxiao Shidc4277a2017-07-17 11:34:02 +0000114 BOOST_CHECK_EQUAL_COLLECTIONS(b2.begin(), b2.end(), BUFFER, BUFFER + sizeof(BUFFER));
115 BOOST_CHECK_EQUAL(buf2, b2.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700116}
117
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100118BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy)
Eric Newberry6235aac2015-08-13 16:03:47 -0700119{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000120 const uint8_t BUFFER[] = {
Eric Newberry6235aac2015-08-13 16:03:47 -0700121 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07,
122 };
123
Junxiao Shidc4277a2017-07-17 11:34:02 +0000124 Block b1(BUFFER, sizeof(BUFFER));
125 auto buf1 = b1.getBuffer();
Eric Newberry6235aac2015-08-13 16:03:47 -0700126
Junxiao Shidc4277a2017-07-17 11:34:02 +0000127 Block b2(b1, b1.begin(), b1.end());
Eric Newberry6235aac2015-08-13 16:03:47 -0700128
Junxiao Shidc4277a2017-07-17 11:34:02 +0000129 b2.parse();
130 b2.remove(tlv::Name);
131 b2.encode();
Eric Newberry6235aac2015-08-13 16:03:47 -0700132
Junxiao Shidc4277a2017-07-17 11:34:02 +0000133 b1.parse();
134 BOOST_CHECK_EQUAL_COLLECTIONS(b1.begin(), b1.end(), BUFFER, BUFFER + sizeof(BUFFER));
135 BOOST_CHECK_EQUAL(buf1, b1.getBuffer());
Eric Newberry6235aac2015-08-13 16:03:47 -0700136}
137
Junxiao Shidc4277a2017-07-17 11:34:02 +0000138BOOST_AUTO_TEST_CASE(FromType)
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800139{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000140 Block b1(4);
141 BOOST_CHECK_EQUAL(b1.empty(), false);
142 BOOST_CHECK_EQUAL(b1.type(), 4);
143 BOOST_CHECK_EQUAL(b1.size(), 2); // 1-octet TLV-TYPE and 1-octet TLV-LENGTH
144 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800145
Junxiao Shidc4277a2017-07-17 11:34:02 +0000146 Block b2(258);
147 BOOST_CHECK_EQUAL(b2.type(), 258);
148 BOOST_CHECK_EQUAL(b2.size(), 4); // 3-octet TLV-TYPE and 1-octet TLV-LENGTH
149 BOOST_CHECK_EQUAL(b2.value_size(), 0);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700150}
151
Junxiao Shif0da7892015-04-04 22:16:16 -0700152BOOST_AUTO_TEST_CASE(FromStream)
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700153{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000154 std::stringstream stream;
155 stream.write(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER));
156 stream.seekg(0);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700157
Junxiao Shidc4277a2017-07-17 11:34:02 +0000158 Block b = Block::fromStream(stream);
159 BOOST_CHECK_EQUAL(b.type(), 0);
160 BOOST_CHECK_EQUAL(b.size(), 3);
161 BOOST_CHECK_EQUAL(b.value_size(), 1);
162 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
163 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700164
Junxiao Shidc4277a2017-07-17 11:34:02 +0000165 b = Block::fromStream(stream);
166 BOOST_CHECK_EQUAL(b.type(), 1);
167 BOOST_CHECK_EQUAL(b.size(), 3);
168 BOOST_CHECK_EQUAL(b.value_size(), 1);
169 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
170 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700171
Steve DiBenedetto54ce6682014-07-22 13:22:57 -0600172 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Alexander Afanasyev21ef2392014-03-25 12:40:22 -0700173}
174
Junxiao Shif0da7892015-04-04 22:16:16 -0700175BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728
176{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000177 const uint8_t PACKET[] = {
Junxiao Shif0da7892015-04-04 22:16:16 -0700178 0x06, 0x20, // Data
179 0x07, 0x11, // Name
180 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
181 0x08, 0x01, 0x31, // NameComponent '1'
182 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
183 0x14, 0x00, // MetaInfo empty
184 0x15, 0x00, // Content empty
185 0x16, 0x05, // SignatureInfo
Junxiao Shidc4277a2017-07-17 11:34:02 +0000186 0x1b, 0x01, 0x01, // SignatureType RSA
187 0x1c, 0x00, // KeyLocator empty
Junxiao Shif0da7892015-04-04 22:16:16 -0700188 0x17, 0x00 // SignatureValue empty
189 };
190 // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace
191
192 std::stringstream stream;
193 stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET));
194 stream.seekg(0);
195
Junxiao Shidc4277a2017-07-17 11:34:02 +0000196 Block b = Block::fromStream(stream);
197 BOOST_CHECK_EQUAL(b.type(), 6);
198 BOOST_CHECK_EQUAL(b.value_size(), 32);
199 b.parse();
Junxiao Shif0da7892015-04-04 22:16:16 -0700200}
201
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000202BOOST_AUTO_TEST_CASE(FromStreamZeroLength)
Junxiao Shif0da7892015-04-04 22:16:16 -0700203{
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000204 const uint8_t BUFFER[] = {0x70, 0x00,
205 0x71, 0x03, 0x86, 0x11, 0x24,
206 0x72, 0x00};
Junxiao Shif0da7892015-04-04 22:16:16 -0700207
208 std::stringstream stream;
209 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
210 stream.seekg(0);
211
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000212 Block b1 = Block::fromStream(stream);
213 BOOST_CHECK_EQUAL(b1.type(), 0x70);
214 BOOST_CHECK_EQUAL(b1.value_size(), 0);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000215
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000216 Block b2 = Block::fromStream(stream);
217 BOOST_CHECK_EQUAL(b2.type(), 0x71);
218 BOOST_CHECK_EQUAL(b2.value_size(), 3);
219 const uint8_t EXPECTED_VALUE2[] = {0x86, 0x11, 0x24};
220 BOOST_CHECK_EQUAL_COLLECTIONS(b2.value_begin(), b2.value_end(),
221 EXPECTED_VALUE2, EXPECTED_VALUE2 + sizeof(EXPECTED_VALUE2));
222
223 Block b3 = Block::fromStream(stream);
224 BOOST_CHECK_EQUAL(b3.type(), 0x72);
225 BOOST_CHECK_EQUAL(b3.value_size(), 0);
226
227 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
228}
229
230BOOST_AUTO_TEST_CASE(FromStreamPacketTooLarge)
231{
232 const uint8_t BUFFER[] = {0x07, 0xfe, 0x00, 0x01, 0x00, 0x00};
233
234 std::stringstream stream;
235 stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER));
236 for (int i = 0; i < 0x10000; ++i) {
237 stream.put('\0');
238 }
239 stream.seekg(0);
240
241 BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error);
Junxiao Shidc4277a2017-07-17 11:34:02 +0000242}
243
244BOOST_AUTO_TEST_CASE(FromWireBuffer)
245{
246 ConstBufferPtr buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
247
248 size_t offset = 0;
249 bool isOk = false;
250 Block b;
251 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
252 BOOST_CHECK(isOk);
253 BOOST_CHECK_EQUAL(b.type(), 0);
254 BOOST_CHECK_EQUAL(b.size(), 3);
255 BOOST_CHECK_EQUAL(b.value_size(), 1);
256 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
257 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
258 offset += b.size();
259
260 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
261 BOOST_CHECK(isOk);
262 BOOST_CHECK_EQUAL(b.type(), 1);
263 BOOST_CHECK_EQUAL(b.size(), 3);
264 BOOST_CHECK_EQUAL(b.value_size(), 1);
265 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
266 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
267 offset += b.size();
268
269 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
270 BOOST_CHECK(!isOk);
271}
272
273BOOST_AUTO_TEST_CASE(FromRawBuffer)
274{
275 size_t offset = 0;
276 bool isOk = false;
277 Block b;
278 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
279 BOOST_CHECK(isOk);
280 BOOST_CHECK_EQUAL(b.type(), 0);
281 BOOST_CHECK_EQUAL(b.size(), 3);
282 BOOST_CHECK_EQUAL(b.value_size(), 1);
283 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
284 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
285 offset += b.size();
286
287 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
288 BOOST_CHECK(isOk);
289 BOOST_CHECK_EQUAL(b.type(), 1);
290 BOOST_CHECK_EQUAL(b.size(), 3);
291 BOOST_CHECK_EQUAL(b.value_size(), 1);
292 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
293 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
294 offset += b.size();
295
296 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
297 BOOST_CHECK(!isOk);
Junxiao Shif0da7892015-04-04 22:16:16 -0700298}
299
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100300BOOST_AUTO_TEST_SUITE_END() // Construction
301
Junxiao Shidc4277a2017-07-17 11:34:02 +0000302BOOST_AUTO_TEST_SUITE(SubElements)
303
304BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700305{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000306 const uint8_t PACKET[] = {
307 0x06, 0x20, // Data
308 0x07, 0x11, // Name
309 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
310 0x08, 0x01, 0x31, // NameComponent '1'
311 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
312 0x14, 0x00, // MetaInfo empty
313 0x15, 0x00, // Content empty
314 0x16, 0x05, // SignatureInfo
315 0x1b, 0x01, 0x01, // SignatureType RSA
316 0x1c, 0x00, // KeyLocator empty
317 0x17, 0x00 // SignatureValue empty
318 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700319
Junxiao Shidc4277a2017-07-17 11:34:02 +0000320 Block data(PACKET, sizeof(PACKET));
321 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700322
Junxiao Shidc4277a2017-07-17 11:34:02 +0000323 BOOST_CHECK_EQUAL(data.elements_size(), 5);
324 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
325 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700326
Junxiao Shidc4277a2017-07-17 11:34:02 +0000327 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
328 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
329
330 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
331 BOOST_CHECK(data.find(0x01) == data.elements_end());
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700332}
333
Joao Pereira7476ebf2015-07-07 14:54:39 -0400334BOOST_AUTO_TEST_CASE(InsertBeginning)
335{
336 Block masterBlock(tlv::Name);
337 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
338 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
339 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
340
341 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
342 masterBlock.push_back(secondBlock);
343 masterBlock.push_back(thirdBlock);
344 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
345 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
346 BOOST_CHECK_EQUAL(*it == secondBlock, true);
347
348 it = masterBlock.insert(it, firstBlock);
349
350 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
351 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
352 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
353}
354
355BOOST_AUTO_TEST_CASE(InsertEnd)
356{
357 Block masterBlock(tlv::Name);
358 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
359 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
360 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
361
362 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
363 masterBlock.push_back(firstBlock);
364 masterBlock.push_back(secondBlock);
365 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
366 Block::element_const_iterator it = masterBlock.elements_end();
367 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
368
369 it = masterBlock.insert(it, thirdBlock);
370
371 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
372 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
373 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
374}
375
376BOOST_AUTO_TEST_CASE(InsertMiddle)
377{
378 Block masterBlock(tlv::Name);
379 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
380 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
381 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
382
383 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
384 masterBlock.push_back(firstBlock);
385 masterBlock.push_back(thirdBlock);
386 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
387 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
388 BOOST_CHECK_EQUAL(*it == firstBlock, true);
389
Junxiao Shidc4277a2017-07-17 11:34:02 +0000390 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400391
392 BOOST_CHECK_EQUAL(*it == secondBlock, true);
393 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
394 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
395}
396
397BOOST_AUTO_TEST_CASE(EraseSingleElement)
398{
399 Block masterBlock(tlv::Name);
400 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
401 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
402 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
403
404 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
405 masterBlock.push_back(firstBlock);
406 masterBlock.push_back(secondBlock);
407 masterBlock.push_back(thirdBlock);
408 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
409 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
410 it++;
411 BOOST_CHECK_EQUAL(*it == secondBlock, true);
412
413 it = masterBlock.erase(it);
414
415 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
416 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
417 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
418}
419
420BOOST_AUTO_TEST_CASE(EraseRange)
421{
422 Block masterBlock(tlv::Name);
423 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
424 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
425 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
426 Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName");
427 Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName");
428 Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName");
429
430 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
431 masterBlock.push_back(firstBlock);
432 masterBlock.push_back(secondBlock);
433 masterBlock.push_back(thirdBlock);
434 masterBlock.push_back(fourthBlock);
435 masterBlock.push_back(fifthBlock);
436 masterBlock.push_back(sixthBlock);
437 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
438 Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent);
439 itStart++;
440 Block::element_const_iterator itEnd = itStart + 3;
441 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
442 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
443
444 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
445
446 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
447 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
448 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
449}
450
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700451BOOST_AUTO_TEST_CASE(Remove)
452{
453 Block block(tlv::Data);
454 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
455 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
456 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
457 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
458 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
459
460 BOOST_CHECK_EQUAL(5, block.elements_size());
461 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
462 BOOST_CHECK_EQUAL(2, block.elements_size());
463
464 Block::element_container elements = block.elements();
465
466 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
467 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
468 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
469 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
470}
471
Junxiao Shidc4277a2017-07-17 11:34:02 +0000472BOOST_AUTO_TEST_SUITE_END() // SubElements
473
474BOOST_AUTO_TEST_CASE(Equality)
475{
476 Block a("\x08\x00", 2);
477 Block b("\x08\x00", 2);;
478 BOOST_CHECK_EQUAL(a == b, true);
479 BOOST_CHECK_EQUAL(a != b, false);
480
481 Block c("\x06\x00", 2);
482 Block d("\x08\x00", 2);;
483 BOOST_CHECK_EQUAL(c == d, false);
484 BOOST_CHECK_EQUAL(c != d, true);
485
486 Block e("\x06\x00", 2);
487 Block f("\x06\x01\xcc", 3);;
488 BOOST_CHECK_EQUAL(e == f, false);
489 BOOST_CHECK_EQUAL(e != f, true);
490}
491
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100492BOOST_AUTO_TEST_SUITE_END() // TestBlock
493BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800494
Alexander Afanasyev74633892015-02-08 18:08:46 -0800495} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800496} // namespace ndn