| /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| /* |
| * Copyright (c) 2013-2017 Regents of the University of California. |
| * |
| * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| * |
| * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| * terms of the GNU Lesser General Public License as published by the Free Software |
| * Foundation, either version 3 of the License, or (at your option) any later version. |
| * |
| * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| * |
| * You should have received copies of the GNU General Public License and GNU Lesser |
| * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| * <http://www.gnu.org/licenses/>. |
| * |
| * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| */ |
| |
| #include "encoding/block.hpp" |
| #include "encoding/block-helpers.hpp" |
| #include "encoding/encoding-buffer.hpp" |
| |
| #include "boost-test.hpp" |
| |
| #include <boost/iostreams/device/array.hpp> |
| #include <boost/iostreams/stream.hpp> |
| |
| namespace ndn { |
| namespace tests { |
| |
| BOOST_AUTO_TEST_SUITE(Encoding) |
| BOOST_AUTO_TEST_SUITE(TestBlock) |
| |
| class BasicBlockFixture |
| { |
| public: |
| EncodingBuffer buffer; |
| EncodingEstimator estimator; |
| }; |
| |
| BOOST_FIXTURE_TEST_SUITE(Basic, BasicBlockFixture) |
| |
| BOOST_AUTO_TEST_CASE(VarNumberOneByte1) |
| { |
| size_t s1 = buffer.prependVarNumber(252); |
| size_t s2 = estimator.prependVarNumber(252); |
| BOOST_CHECK_EQUAL(buffer.size(), 1); |
| BOOST_CHECK_EQUAL(s1, 1); |
| BOOST_CHECK_EQUAL(s2, 1); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberOneByte2) |
| { |
| size_t s1 = buffer.prependVarNumber(253); |
| size_t s2 = estimator.prependVarNumber(253); |
| BOOST_CHECK_EQUAL(buffer.size(), 3); |
| BOOST_CHECK_EQUAL(s1, 3); |
| BOOST_CHECK_EQUAL(s2, 3); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberThreeBytes1) |
| { |
| size_t s1 = buffer.prependVarNumber(255); |
| size_t s2 = estimator.prependVarNumber(255); |
| BOOST_CHECK_EQUAL(buffer.size(), 3); |
| BOOST_CHECK_EQUAL(s1, 3); |
| BOOST_CHECK_EQUAL(s2, 3); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberThreeBytes2) |
| { |
| size_t s1 = buffer.prependVarNumber(65535); |
| size_t s2 = estimator.prependVarNumber(65535); |
| BOOST_CHECK_EQUAL(buffer.size(), 3); |
| BOOST_CHECK_EQUAL(s1, 3); |
| BOOST_CHECK_EQUAL(s2, 3); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberFiveBytes1) |
| { |
| size_t s1 = buffer.prependVarNumber(65536); |
| size_t s2 = estimator.prependVarNumber(65536); |
| BOOST_CHECK_EQUAL(buffer.size(), 5); |
| BOOST_CHECK_EQUAL(s1, 5); |
| BOOST_CHECK_EQUAL(s2, 5); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberFiveBytes2) |
| { |
| size_t s1 = buffer.prependVarNumber(4294967295LL); |
| size_t s2 = estimator.prependVarNumber(4294967295LL); |
| BOOST_CHECK_EQUAL(buffer.size(), 5); |
| BOOST_CHECK_EQUAL(s1, 5); |
| BOOST_CHECK_EQUAL(s2, 5); |
| } |
| |
| BOOST_AUTO_TEST_CASE(VarNumberNineBytes) |
| { |
| size_t s1 = buffer.prependVarNumber(4294967296LL); |
| size_t s2 = estimator.prependVarNumber(4294967296LL); |
| BOOST_CHECK_EQUAL(buffer.size(), 9); |
| BOOST_CHECK_EQUAL(s1, 9); |
| BOOST_CHECK_EQUAL(s2, 9); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte1) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(252); |
| size_t s2 = estimator.prependNonNegativeInteger(252); |
| BOOST_CHECK_EQUAL(buffer.size(), 1); |
| BOOST_CHECK_EQUAL(s1, 1); |
| BOOST_CHECK_EQUAL(s2, 1); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberOneByte2) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(255); |
| size_t s2 = estimator.prependNonNegativeInteger(255); |
| BOOST_CHECK_EQUAL(buffer.size(), 1); |
| BOOST_CHECK_EQUAL(s1, 1); |
| BOOST_CHECK_EQUAL(s2, 1); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes1) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(256); |
| size_t s2 = estimator.prependNonNegativeInteger(256); |
| BOOST_CHECK_EQUAL(buffer.size(), 2); |
| BOOST_CHECK_EQUAL(s1, 2); |
| BOOST_CHECK_EQUAL(s2, 2); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberTwoBytes2) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(65535); |
| size_t s2 = estimator.prependNonNegativeInteger(65535); |
| BOOST_CHECK_EQUAL(buffer.size(), 2); |
| BOOST_CHECK_EQUAL(s1, 2); |
| BOOST_CHECK_EQUAL(s2, 2); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes1) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(65536); |
| size_t s2 = estimator.prependNonNegativeInteger(65536); |
| BOOST_CHECK_EQUAL(buffer.size(), 4); |
| BOOST_CHECK_EQUAL(s1, 4); |
| BOOST_CHECK_EQUAL(s2, 4); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberFourBytes2) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(4294967295LL); |
| size_t s2 = estimator.prependNonNegativeInteger(4294967295LL); |
| BOOST_CHECK_EQUAL(buffer.size(), 4); |
| BOOST_CHECK_EQUAL(s1, 4); |
| BOOST_CHECK_EQUAL(s2, 4); |
| } |
| |
| BOOST_AUTO_TEST_CASE(NonNegativeNumberEightBytes) |
| { |
| size_t s1 = buffer.prependNonNegativeInteger(4294967296LL); |
| size_t s2 = estimator.prependNonNegativeInteger(4294967296LL); |
| BOOST_CHECK_EQUAL(buffer.size(), 8); |
| BOOST_CHECK_EQUAL(s1, 8); |
| BOOST_CHECK_EQUAL(s2, 8); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // Basic |
| |
| BOOST_AUTO_TEST_SUITE(Construction) |
| |
| BOOST_AUTO_TEST_CASE(FromBlock) |
| { |
| static uint8_t buffer[] = {0x80, 0x06, 0x81, 0x01, 0x01, 0x82, 0x01, 0x01}; |
| Block block(buffer, sizeof(buffer)); |
| |
| Block derivedBlock(block, block.begin(), block.end()); |
| BOOST_CHECK_EQUAL(derivedBlock.wire(), block.wire()); // pointers should match |
| BOOST_CHECK(derivedBlock == block); // blocks should match |
| |
| derivedBlock = Block(block, block.begin() + 2, block.begin() + 5); |
| BOOST_CHECK(derivedBlock.begin() == block.begin() + 2); |
| BOOST_CHECK(derivedBlock == Block(buffer + 2, 3)); |
| |
| Buffer otherBuffer(buffer, sizeof(buffer)); |
| BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), block.end()), std::invalid_argument); |
| BOOST_CHECK_THROW(Block(block, block.begin(), otherBuffer.end()), std::invalid_argument); |
| BOOST_CHECK_THROW(Block(block, otherBuffer.begin(), otherBuffer.end()), std::invalid_argument); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyOriginal) |
| { |
| static uint8_t buffer[] = { |
| 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| }; |
| |
| Block block1(buffer, sizeof(buffer)); |
| |
| Block block2(block1, block1.begin(), block1.end()); |
| auto buf2 = block2.getBuffer(); |
| |
| block1.parse(); |
| block1.remove(tlv::Name); |
| block1.encode(); |
| |
| block2.parse(); |
| |
| BOOST_CHECK_EQUAL_COLLECTIONS(block2.begin(), block2.end(), buffer, buffer + sizeof(buffer)); |
| BOOST_CHECK_EQUAL(buf2, block2.getBuffer()); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromBlockCopyOnWriteModifyCopy) |
| { |
| static uint8_t buffer[] = { |
| 0x05, 0x0b, 0x07, 0x03, 0x01, 0x02, 0x03, 0x0a, 0x04, 0x04, 0x05, 0x06, 0x07, |
| }; |
| |
| Block block1(buffer, sizeof(buffer)); |
| auto buf1 = block1.getBuffer(); |
| |
| Block block2(block1, block1.begin(), block1.end()); |
| |
| block2.parse(); |
| block2.remove(tlv::Name); |
| block2.encode(); |
| |
| block1.parse(); |
| |
| BOOST_CHECK_EQUAL_COLLECTIONS(block1.begin(), block1.end(), buffer, buffer + sizeof(buffer)); |
| BOOST_CHECK_EQUAL(buf1, block1.getBuffer()); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromEncodingBuffer) |
| { |
| uint8_t value[4]; |
| |
| EncodingBuffer buffer; |
| size_t length = buffer.prependByteArray(value, sizeof(value)); |
| buffer.prependVarNumber(length); |
| buffer.prependVarNumber(0xe0); |
| |
| Block block; |
| BOOST_REQUIRE_NO_THROW(block = buffer.block()); |
| BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| |
| BOOST_REQUIRE_NO_THROW(block = Block(buffer)); |
| BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| BOOST_CHECK_EQUAL(block.value_size(), sizeof(value)); |
| } |
| |
| BOOST_AUTO_TEST_CASE(ToEncodingBuffer) |
| { |
| shared_ptr<Buffer> buf = make_shared<Buffer>(10); |
| for (int i = 0; i < 10; i++) |
| (*buf)[i] = i; |
| |
| Block block(0xab, buf); |
| block.encode(); |
| |
| { |
| BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| EncodingBuffer buffer(block); |
| BOOST_CHECK_EQUAL(buffer.size(), 12); |
| BOOST_CHECK_EQUAL(buffer.capacity(), 12); |
| } |
| |
| (*buf)[1] = 0xe0; |
| (*buf)[2] = 2; |
| BOOST_REQUIRE_NO_THROW(block = Block(buf, buf->begin() + 1, buf->begin() + 5)); |
| BOOST_CHECK_EQUAL(block.type(), 0xe0); |
| |
| { |
| BOOST_REQUIRE_NO_THROW(EncodingBuffer(block)); |
| EncodingBuffer buffer(block); |
| BOOST_CHECK_EQUAL(buffer.size(), 4); |
| BOOST_CHECK_EQUAL(buffer.capacity(), 10); |
| } |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromBuffer) |
| { |
| const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 0x01, 0x01, 0xfb, // ok |
| 0x03, 0x02, 0xff}; // not ok |
| BufferPtr buffer(new Buffer(TEST_BUFFER, sizeof(TEST_BUFFER))); |
| |
| // using BufferPtr (avoids memory copy) |
| size_t offset = 0; |
| bool isOk = false; |
| Block testBlock; |
| std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| BOOST_CHECK(isOk); |
| BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| offset += testBlock.size(); |
| |
| std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| BOOST_CHECK(isOk); |
| BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| offset += testBlock.size(); |
| |
| std::tie(isOk, testBlock) = Block::fromBuffer(buffer, offset); |
| BOOST_CHECK(!isOk); |
| |
| // just buffer, copies memory |
| offset = 0; |
| std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| sizeof(TEST_BUFFER) - offset); |
| BOOST_CHECK(isOk); |
| BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| offset += testBlock.size(); |
| |
| std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| sizeof(TEST_BUFFER) - offset); |
| BOOST_CHECK(isOk); |
| BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| offset += testBlock.size(); |
| |
| std::tie(isOk, testBlock) = Block::fromBuffer(TEST_BUFFER + offset, |
| sizeof(TEST_BUFFER) - offset); |
| BOOST_CHECK(!isOk); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromStream) |
| { |
| const uint8_t TEST_BUFFER[] = {0x00, 0x01, 0xfa, // ok |
| 0x01, 0x01, 0xfb, // ok |
| 0x03, 0x02, 0xff}; // not ok |
| |
| typedef boost::iostreams::stream<boost::iostreams::array_source> ArrayStream; |
| ArrayStream stream(reinterpret_cast<const char*>(TEST_BUFFER), sizeof(TEST_BUFFER)); |
| |
| Block testBlock; |
| BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
| BOOST_CHECK_EQUAL(testBlock.type(), 0); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x00); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfa); |
| |
| BOOST_REQUIRE_NO_THROW(testBlock = Block::fromStream(stream)); |
| BOOST_CHECK_EQUAL(testBlock.type(), 1); |
| BOOST_CHECK_EQUAL(testBlock.size(), 3); |
| BOOST_CHECK_EQUAL(testBlock.value_size(), 1); |
| BOOST_CHECK_EQUAL(*testBlock.wire(), 0x01); |
| BOOST_CHECK_EQUAL(*testBlock.value(), 0xfb); |
| |
| BOOST_CHECK_THROW(Block::fromStream(stream), tlv::Error); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromStreamWhitespace) // Bug 2728 |
| { |
| uint8_t PACKET[] = { |
| 0x06, 0x20, // Data |
| 0x07, 0x11, // Name |
| 0x08, 0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, // NameComponent 'hello' |
| 0x08, 0x01, 0x31, // NameComponent '1' |
| 0x08, 0x05, 0x77, 0x6f, 0x72, 0x6c, 0x64, // NameComponent 'world' |
| 0x14, 0x00, // MetaInfo empty |
| 0x15, 0x00, // Content empty |
| 0x16, 0x05, // SignatureInfo |
| 0x1b, 0x01, 0x01, // SignatureType RSA |
| 0x1c, 0x00, // KeyLocator empty |
| 0x17, 0x00 // SignatureValue empty |
| }; |
| // TLV-LENGTH of <Data> is 0x20 which happens to be ASCII whitespace |
| |
| std::stringstream stream; |
| stream.write(reinterpret_cast<const char*>(PACKET), sizeof(PACKET)); |
| stream.seekg(0); |
| |
| Block block = Block::fromStream(stream); |
| BOOST_CHECK_NO_THROW(block.parse()); |
| } |
| |
| BOOST_AUTO_TEST_CASE(FromStreamZeroLength) // Bug 2729 |
| { |
| uint8_t BUFFER[] = { 0x07, 0x00 }; // TLV-LENGTH is zero |
| |
| std::stringstream stream; |
| stream.write(reinterpret_cast<const char*>(BUFFER), sizeof(BUFFER)); |
| stream.seekg(0); |
| |
| Block block; |
| BOOST_CHECK_NO_THROW(block = Block::fromStream(stream)); |
| BOOST_CHECK_EQUAL(block.type(), 0x07); |
| BOOST_CHECK_EQUAL(block.value_size(), 0); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // Construction |
| |
| BOOST_AUTO_TEST_CASE(Equality) |
| { |
| BOOST_CONCEPT_ASSERT((boost::EqualityComparable<Block>)); |
| |
| Block a("\x08\x00", 2); |
| Block b("\x08\x00", 2);; |
| BOOST_CHECK_EQUAL(a == b, true); |
| BOOST_CHECK_EQUAL(a != b, false); |
| |
| Block c("\x06\x00", 2); |
| Block d("\x08\x00", 2);; |
| BOOST_CHECK_EQUAL(c == d, false); |
| BOOST_CHECK_EQUAL(c != d, true); |
| |
| Block e("\x06\x00", 2); |
| Block f("\x06\x01\xcc", 3);; |
| BOOST_CHECK_EQUAL(e == f, false); |
| BOOST_CHECK_EQUAL(e != f, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InsertBeginning) |
| { |
| Block masterBlock(tlv::Name); |
| Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| masterBlock.push_back(secondBlock); |
| masterBlock.push_back(thirdBlock); |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| |
| it = masterBlock.insert(it, firstBlock); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| BOOST_CHECK_EQUAL(*(it + 1) == secondBlock, true); |
| BOOST_CHECK_EQUAL(*(masterBlock.elements_begin()) == firstBlock, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InsertEnd) |
| { |
| Block masterBlock(tlv::Name); |
| Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| masterBlock.push_back(firstBlock); |
| masterBlock.push_back(secondBlock); |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| Block::element_const_iterator it = masterBlock.elements_end(); |
| BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| |
| it = masterBlock.insert(it, thirdBlock); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| BOOST_CHECK_EQUAL(*(it - 1) == secondBlock, true); |
| BOOST_CHECK_EQUAL(*(masterBlock.elements_end() - 1) == thirdBlock, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(InsertMiddle) |
| { |
| Block masterBlock(tlv::Name); |
| Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| masterBlock.push_back(firstBlock); |
| masterBlock.push_back(thirdBlock); |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| BOOST_CHECK_EQUAL(*it == firstBlock, true); |
| |
| it = masterBlock.insert(it+1, secondBlock); |
| |
| BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| BOOST_CHECK_EQUAL(*(it + 1) == thirdBlock, true); |
| BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(EraseSingleElement) |
| { |
| Block masterBlock(tlv::Name); |
| Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| masterBlock.push_back(firstBlock); |
| masterBlock.push_back(secondBlock); |
| masterBlock.push_back(thirdBlock); |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| Block::element_const_iterator it = masterBlock.find(tlv::NameComponent); |
| it++; |
| BOOST_CHECK_EQUAL(*it == secondBlock, true); |
| |
| it = masterBlock.erase(it); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 2); |
| BOOST_CHECK_EQUAL(*(it) == thirdBlock, true); |
| BOOST_CHECK_EQUAL(*(it - 1) == firstBlock, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(EraseRange) |
| { |
| Block masterBlock(tlv::Name); |
| Block firstBlock = makeStringBlock(tlv::NameComponent, "firstName"); |
| Block secondBlock = makeStringBlock(tlv::NameComponent, "secondName"); |
| Block thirdBlock = makeStringBlock(tlv::NameComponent, "thirdName"); |
| Block fourthBlock = makeStringBlock(tlv::NameComponent, "fourthName"); |
| Block fifthBlock = makeStringBlock(tlv::NameComponent, "fifthName"); |
| Block sixthBlock = makeStringBlock(tlv::NameComponent, "sixthName"); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 0); |
| masterBlock.push_back(firstBlock); |
| masterBlock.push_back(secondBlock); |
| masterBlock.push_back(thirdBlock); |
| masterBlock.push_back(fourthBlock); |
| masterBlock.push_back(fifthBlock); |
| masterBlock.push_back(sixthBlock); |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 6); |
| Block::element_const_iterator itStart = masterBlock.find(tlv::NameComponent); |
| itStart++; |
| Block::element_const_iterator itEnd = itStart + 3; |
| BOOST_CHECK_EQUAL(*itStart == secondBlock, true); |
| BOOST_CHECK_EQUAL(*itEnd == fifthBlock, true); |
| |
| Block::element_const_iterator newIt = masterBlock.erase(itStart, itEnd); |
| |
| BOOST_CHECK_EQUAL(masterBlock.elements_size(), 3); |
| BOOST_CHECK_EQUAL(*(newIt) == fifthBlock, true); |
| BOOST_CHECK_EQUAL(*(newIt - 1) == firstBlock, true); |
| } |
| |
| BOOST_AUTO_TEST_CASE(Remove) |
| { |
| Block block(tlv::Data); |
| block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 0)); |
| block.push_back(makeNonNegativeIntegerBlock(tlv::FreshnessPeriod, 123)); |
| block.push_back(makeStringBlock(tlv::Name, "ndn:/test-prefix")); |
| block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 2)); |
| block.push_back(makeNonNegativeIntegerBlock(tlv::ContentType, 1)); |
| |
| BOOST_CHECK_EQUAL(5, block.elements_size()); |
| BOOST_REQUIRE_NO_THROW(block.remove(tlv::ContentType)); |
| BOOST_CHECK_EQUAL(2, block.elements_size()); |
| |
| Block::element_container elements = block.elements(); |
| |
| BOOST_CHECK_EQUAL(tlv::FreshnessPeriod, elements[0].type()); |
| BOOST_CHECK_EQUAL(123, readNonNegativeInteger(elements[0])); |
| BOOST_CHECK_EQUAL(tlv::Name, elements[1].type()); |
| BOOST_CHECK(readString(elements[1]).compare("ndn:/test-prefix") == 0); |
| } |
| |
| BOOST_AUTO_TEST_SUITE_END() // TestBlock |
| BOOST_AUTO_TEST_SUITE_END() // Encoding |
| |
| } // namespace tests |
| } // namespace ndn |