blob: 479ea4308da3075b90e6a6a0ae133f2c98666f64 [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 Shidc4277a2017-07-17 11:34:02 +0000202BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(FromStreamZeroLength, 1)
Junxiao Shif0da7892015-04-04 22:16:16 -0700203BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729
204{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000205 const uint8_t BUFFER[] = {0x07, 0x00, // TLV-LENGTH is zero
206 0x09, 0x01, 0x01};
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 Shidc4277a2017-07-17 11:34:02 +0000212 Block b = Block::fromStream(stream);
213 BOOST_CHECK_EQUAL(b.type(), 0x07);
214 BOOST_CHECK_EQUAL(b.value_size(), 0);
215
216 BOOST_CHECK_NO_THROW(b = Block::fromStream(stream)); // expected failure Bug 4180
217}
218
219BOOST_AUTO_TEST_CASE(FromWireBuffer)
220{
221 ConstBufferPtr buffer = make_shared<Buffer>(TEST_BUFFER, sizeof(TEST_BUFFER));
222
223 size_t offset = 0;
224 bool isOk = false;
225 Block b;
226 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
227 BOOST_CHECK(isOk);
228 BOOST_CHECK_EQUAL(b.type(), 0);
229 BOOST_CHECK_EQUAL(b.size(), 3);
230 BOOST_CHECK_EQUAL(b.value_size(), 1);
231 BOOST_CHECK_EQUAL(*b.wire(), 0x00);
232 BOOST_CHECK_EQUAL(*b.value(), 0xfa);
233 offset += b.size();
234
235 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
236 BOOST_CHECK(isOk);
237 BOOST_CHECK_EQUAL(b.type(), 1);
238 BOOST_CHECK_EQUAL(b.size(), 3);
239 BOOST_CHECK_EQUAL(b.value_size(), 1);
240 BOOST_CHECK_EQUAL(*b.wire(), 0x01);
241 BOOST_CHECK_EQUAL(*b.value(), 0xfb);
242 offset += b.size();
243
244 std::tie(isOk, b) = Block::fromBuffer(buffer, offset);
245 BOOST_CHECK(!isOk);
246}
247
248BOOST_AUTO_TEST_CASE(FromRawBuffer)
249{
250 size_t offset = 0;
251 bool isOk = false;
252 Block b;
253 std::tie(isOk, b) = Block::fromBuffer(TEST_BUFFER + offset, sizeof(TEST_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(TEST_BUFFER + offset, sizeof(TEST_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(TEST_BUFFER + offset, sizeof(TEST_BUFFER) - offset);
272 BOOST_CHECK(!isOk);
Junxiao Shif0da7892015-04-04 22:16:16 -0700273}
274
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100275BOOST_AUTO_TEST_SUITE_END() // Construction
276
Junxiao Shidc4277a2017-07-17 11:34:02 +0000277BOOST_AUTO_TEST_SUITE(SubElements)
278
279BOOST_AUTO_TEST_CASE(Parse)
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700280{
Junxiao Shidc4277a2017-07-17 11:34:02 +0000281 const uint8_t PACKET[] = {
282 0x06, 0x20, // Data
283 0x07, 0x11, // Name
284 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello'
285 0x08, 0x01, 0x31, // NameComponent '1'
286 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world'
287 0x14, 0x00, // MetaInfo empty
288 0x15, 0x00, // Content empty
289 0x16, 0x05, // SignatureInfo
290 0x1b, 0x01, 0x01, // SignatureType RSA
291 0x1c, 0x00, // KeyLocator empty
292 0x17, 0x00 // SignatureValue empty
293 };
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700294
Junxiao Shidc4277a2017-07-17 11:34:02 +0000295 Block data(PACKET, sizeof(PACKET));
296 data.parse();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700297
Junxiao Shidc4277a2017-07-17 11:34:02 +0000298 BOOST_CHECK_EQUAL(data.elements_size(), 5);
299 BOOST_CHECK_EQUAL(data.elements().at(0).type(), 0x07);
300 BOOST_CHECK_EQUAL(data.elements().at(0).elements().size(), 0); // parse is not recursive
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700301
Junxiao Shidc4277a2017-07-17 11:34:02 +0000302 BOOST_CHECK(data.get(0x15) == data.elements().at(2));
303 BOOST_CHECK_THROW(data.get(0x01), Block::Error);
304
305 BOOST_CHECK(data.find(0x15) == data.elements_begin() + 2);
306 BOOST_CHECK(data.find(0x01) == data.elements_end());
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700307}
308
Joao Pereira7476ebf2015-07-07 14:54:39 -0400309BOOST_AUTO_TEST_CASE(InsertBeginning)
310{
311 Block masterBlock(tlv::Name);
312 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
313 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
314 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
315
316 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
317 masterBlock.push_back(secondBlock);
318 masterBlock.push_back(thirdBlock);
319 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
320 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
321 BOOST_CHECK_EQUAL(*it == secondBlock, true);
322
323 it = masterBlock.insert(it, firstBlock);
324
325 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
326 BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true);
327 BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true);
328}
329
330BOOST_AUTO_TEST_CASE(InsertEnd)
331{
332 Block masterBlock(tlv::Name);
333 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
334 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
335 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
336
337 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
338 masterBlock.push_back(firstBlock);
339 masterBlock.push_back(secondBlock);
340 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
341 Block::element_const_iterator it = masterBlock.elements_end();
342 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
343
344 it = masterBlock.insert(it, thirdBlock);
345
346 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
347 BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true);
348 BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true);
349}
350
351BOOST_AUTO_TEST_CASE(InsertMiddle)
352{
353 Block masterBlock(tlv::Name);
354 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
355 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
356 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
357
358 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
359 masterBlock.push_back(firstBlock);
360 masterBlock.push_back(thirdBlock);
361 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
362 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
363 BOOST_CHECK_EQUAL(*it == firstBlock, true);
364
Junxiao Shidc4277a2017-07-17 11:34:02 +0000365 it = masterBlock.insert(it + 1, secondBlock);
Joao Pereira7476ebf2015-07-07 14:54:39 -0400366
367 BOOST_CHECK_EQUAL(*it == secondBlock, true);
368 BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true);
369 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
370}
371
372BOOST_AUTO_TEST_CASE(EraseSingleElement)
373{
374 Block masterBlock(tlv::Name);
375 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
376 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
377 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
378
379 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
380 masterBlock.push_back(firstBlock);
381 masterBlock.push_back(secondBlock);
382 masterBlock.push_back(thirdBlock);
383 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
384 Block::element_const_iterator it = masterBlock.find(tlv::NameComponent);
385 it++;
386 BOOST_CHECK_EQUAL(*it == secondBlock, true);
387
388 it = masterBlock.erase(it);
389
390 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2);
391 BOOST_CHECK_EQUAL(*(it) == thirdBlock, true);
392 BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true);
393}
394
395BOOST_AUTO_TEST_CASE(EraseRange)
396{
397 Block masterBlock(tlv::Name);
398 Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName");
399 Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName");
400 Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName");
401 Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName");
402 Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName");
403 Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName");
404
405 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0);
406 masterBlock.push_back(firstBlock);
407 masterBlock.push_back(secondBlock);
408 masterBlock.push_back(thirdBlock);
409 masterBlock.push_back(fourthBlock);
410 masterBlock.push_back(fifthBlock);
411 masterBlock.push_back(sixthBlock);
412 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6);
413 Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent);
414 itStart++;
415 Block::element_const_iterator itEnd = itStart + 3;
416 BOOST_CHECK_EQUAL(*itStart == secondBlock, true);
417 BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true);
418
419 Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd);
420
421 BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3);
422 BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true);
423 BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true);
424}
425
Eric Newberrya3d9fc02015-07-26 10:55:44 -0700426BOOST_AUTO_TEST_CASE(Remove)
427{
428 Block block(tlv::Data);
429 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0));
430 block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123));
431 block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix"));
432 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2));
433 block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1));
434
435 BOOST_CHECK_EQUAL(5, block.elements_size());
436 BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType));
437 BOOST_CHECK_EQUAL(2, block.elements_size());
438
439 Block::element_container elements = block.elements();
440
441 BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type());
442 BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0]));
443 BOOST_CHECK_EQUAL(tlv::Name, elements[1].type());
444 BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0);
445}
446
Junxiao Shidc4277a2017-07-17 11:34:02 +0000447BOOST_AUTO_TEST_SUITE_END() // SubElements
448
449BOOST_AUTO_TEST_CASE(Equality)
450{
451 Block a("\x08\x00", 2);
452 Block b("\x08\x00", 2);;
453 BOOST_CHECK_EQUAL(a == b, true);
454 BOOST_CHECK_EQUAL(a != b, false);
455
456 Block c("\x06\x00", 2);
457 Block d("\x08\x00", 2);;
458 BOOST_CHECK_EQUAL(c == d, false);
459 BOOST_CHECK_EQUAL(c != d, true);
460
461 Block e("\x06\x00", 2);
462 Block f("\x06\x01\xcc", 3);;
463 BOOST_CHECK_EQUAL(e == f, false);
464 BOOST_CHECK_EQUAL(e != f, true);
465}
466
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100467BOOST_AUTO_TEST_SUITE_END() // TestBlock
468BOOST_AUTO_TEST_SUITE_END() // Encoding
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800469
Alexander Afanasyev74633892015-02-08 18:08:46 -0800470} // namespace tests
Alexander Afanasyev233750e2014-02-16 00:50:07 -0800471} // namespace ndn