blob: 5961af5862058bca12b3a8f8a1889d172fc4b2a7 [file] [log] [blame]
/* -*- 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 "delegation-list.hpp"
#include "boost-test.hpp"
#include <boost/lexical_cast.hpp>
namespace ndn {
namespace tests {
BOOST_AUTO_TEST_SUITE(TestDelegationList)
const uint8_t DEL1A[] = {
0x1f, 0x08, // Delegation
0x1e, 0x01, 0x01, // Preference=1
0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
};
const uint8_t DEL1B[] = {
0x1f, 0x08, // Delegation
0x1e, 0x01, 0x01, // Preference=1
0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
};
const uint8_t DEL2A[] = {
0x1f, 0x08, // Delegation
0x1e, 0x01, 0x02, // Preference=2
0x07, 0x03, 0x08, 0x01, 0x41 // Name=/A
};
const uint8_t DEL2B[] = {
0x1f, 0x08, // Delegation
0x1e, 0x01, 0x02, // Preference=2
0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
};
Block
makeDelegationListBlock(uint32_t type, std::initializer_list<const uint8_t*> dels)
{
Block block(type);
for (const uint8_t* del : dels) {
block.push_back(Block(del, 2 + del[1]));
}
block.encode();
return block;
}
BOOST_AUTO_TEST_SUITE(Decode)
BOOST_AUTO_TEST_CASE(DecodeUnsorted)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false);
BOOST_CHECK_EQUAL(dl.size(), 3);
BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
BOOST_CHECK_EQUAL(dl.at(1).name, "/B");
BOOST_CHECK_EQUAL(dl.at(2).preference, 1);
BOOST_CHECK_EQUAL(dl.at(2).name, "/A");
}
BOOST_AUTO_TEST_CASE(DecodeSorted)
{
DelegationList dl(makeDelegationListBlock(tlv::Content, {DEL2A, DEL2B, DEL1A}));
BOOST_CHECK_EQUAL(dl.size(), 3);
BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
BOOST_CHECK_EQUAL(dl.at(1).name, "/A");
BOOST_CHECK_EQUAL(dl.at(2).preference, 2);
BOOST_CHECK_EQUAL(dl.at(2).name, "/B");
}
BOOST_AUTO_TEST_CASE(DecodeEmpty)
{
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeBadType)
{
DelegationList dl;
Block block = makeDelegationListBlock(tlv::Selectors, {DEL1A, DEL2B});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeNotDelegation)
{
const uint8_t BAD_DEL[] = {
0x09, 0x00 // Selectors
};
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeMissingPreference)
{
const uint8_t BAD_DEL[] = {
0x1f, 0x05, // Delegation
0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
};
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeMissingName)
{
const uint8_t BAD_DEL[] = {
0x1f, 0x03, // Delegation
0x1e, 0x01, 0x02, // Preference=2
};
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeUnknownField)
{
const uint8_t BAD_DEL[] = {
0x1f, 0x0a, // Delegation
0x1e, 0x01, 0x02, // Preference=2
0x09, 0x00, // Selectors
0x07, 0x03, 0x08, 0x01, 0x42 // Name=/B
};
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_CASE(DecodeWrongOrder)
{
const uint8_t BAD_DEL[] = {
0x1f, 0x08, // Delegation
0x07, 0x03, 0x08, 0x01, 0x42, // Name=/B
0x1e, 0x01, 0x02 // Preference=2
};
DelegationList dl;
Block block = makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, BAD_DEL});
BOOST_CHECK_THROW(dl.wireDecode(block), DelegationList::Error);
}
BOOST_AUTO_TEST_SUITE_END() // Decode
BOOST_AUTO_TEST_SUITE(InsertEncode)
BOOST_AUTO_TEST_CASE(InsertSimple)
{
DelegationList dl;
dl.insert(2, "/A");
dl.insert(1, "/B");
BOOST_CHECK_EQUAL(dl.size(), 2);
EncodingBuffer encoder;
dl.wireEncode(encoder);
BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1B, DEL2A}));
}
BOOST_AUTO_TEST_CASE(InsertReplace)
{
DelegationList dl;
dl.insert(2, "/A");
dl.insert(Delegation{1, "/A"}, DelegationList::INS_REPLACE);
BOOST_CHECK_EQUAL(dl.size(), 1);
BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
BOOST_CHECK_EQUAL(dl[0].name, "/A");
EncodingBuffer encoder;
dl.wireEncode(encoder);
BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
}
BOOST_AUTO_TEST_CASE(InsertAppend)
{
DelegationList dl;
dl.insert(2, "/A");
dl.insert(Delegation{1, "/A"}, DelegationList::INS_APPEND);
BOOST_CHECK_EQUAL(dl.size(), 2);
BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
EncodingBuffer encoder;
dl.wireEncode(encoder);
BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL1A, DEL2A}));
}
BOOST_AUTO_TEST_CASE(InsertSkip)
{
DelegationList dl;
dl.insert(2, "/A");
dl.insert(Delegation{1, "/A"}, DelegationList::INS_SKIP);
BOOST_CHECK_EQUAL(dl.size(), 1);
BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
EncodingBuffer encoder;
dl.wireEncode(encoder);
BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
}
BOOST_AUTO_TEST_CASE(Unsorted)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}), false);
dl.insert(1, "/B");
BOOST_CHECK_EQUAL(dl.size(), 2);
BOOST_CHECK_EQUAL(dl.at(0).preference, 2);
BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
BOOST_CHECK_EQUAL(dl.at(1).preference, 1);
BOOST_CHECK_EQUAL(dl.at(1).name, "/B");
EncodingBuffer encoder;
dl.wireEncode(encoder, tlv::Content);
BOOST_CHECK(encoder.block() == makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}));
}
BOOST_AUTO_TEST_CASE(EncodeBadType)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A}));
EncodingBuffer encoder;
BOOST_CHECK_THROW(dl.wireEncode(encoder, tlv::Selectors), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(EncodeEmpty)
{
DelegationList dl;
EncodingBuffer encoder;
BOOST_CHECK_THROW(dl.wireEncode(encoder), DelegationList::Error);
}
BOOST_AUTO_TEST_SUITE_END() // InsertEncode
BOOST_AUTO_TEST_SUITE(Erase)
BOOST_AUTO_TEST_CASE(EraseNoop)
{
DelegationList dl;
dl.insert(1, "/A");
BOOST_CHECK_EQUAL(dl.erase(2, "/A"), 0);
BOOST_CHECK_EQUAL(dl.erase(Delegation{1, "/B"}), 0);
BOOST_CHECK_EQUAL(dl.size(), 1);
BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
}
BOOST_AUTO_TEST_CASE(EraseOne)
{
DelegationList dl;
dl.insert(1, "/A");
BOOST_CHECK_EQUAL(dl.erase(1, "/A"), 1);
BOOST_CHECK_EQUAL(dl.size(), 0);
}
BOOST_AUTO_TEST_CASE(EraseByName)
{
DelegationList dl;
dl.insert(1, "/A");
dl.insert(2, "/A", DelegationList::INS_APPEND);
BOOST_CHECK_EQUAL(dl.size(), 2);
BOOST_CHECK_EQUAL(dl.erase("/A"), 2);
BOOST_CHECK_EQUAL(dl.size(), 0);
}
BOOST_AUTO_TEST_SUITE_END() // Erase
BOOST_AUTO_TEST_SUITE(Sort)
BOOST_AUTO_TEST_CASE(Noop)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL1A}));
BOOST_CHECK_EQUAL(dl.isSorted(), true);
dl.sort();
BOOST_CHECK_EQUAL(dl.isSorted(), true);
}
BOOST_AUTO_TEST_CASE(Sort)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL2B, DEL1A}), false);
BOOST_CHECK_EQUAL(dl.isSorted(), false);
dl.sort();
BOOST_CHECK_EQUAL(dl.isSorted(), true);
BOOST_CHECK_EQUAL(dl.size(), 3);
BOOST_CHECK_EQUAL(dl.at(0).preference, 1);
BOOST_CHECK_EQUAL(dl.at(0).name, "/A");
BOOST_CHECK_EQUAL(dl.at(1).preference, 2);
BOOST_CHECK_EQUAL(dl.at(1).name, "/A");
BOOST_CHECK_EQUAL(dl.at(2).preference, 2);
BOOST_CHECK_EQUAL(dl.at(2).name, "/B");
}
BOOST_AUTO_TEST_SUITE_END() // Sort
BOOST_AUTO_TEST_SUITE(Compare)
BOOST_AUTO_TEST_CASE(Empty)
{
DelegationList dl1, dl2;
BOOST_CHECK_EQUAL(dl1, dl2);
}
BOOST_AUTO_TEST_CASE(SortedEqual)
{
DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}));
BOOST_CHECK_EQUAL(dl1, dl2);
}
BOOST_AUTO_TEST_CASE(SortedUnequal)
{
DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B})),
dl2(makeDelegationListBlock(tlv::Content, {DEL1A, DEL2B}));
BOOST_CHECK_NE(dl1, dl2);
}
BOOST_AUTO_TEST_CASE(UnsortedSameOrder)
{
DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
dl2(makeDelegationListBlock(tlv::Content, {DEL2A, DEL1B}), false);
BOOST_CHECK_EQUAL(dl1, dl2);
}
BOOST_AUTO_TEST_CASE(UnsortedDifferentOrder)
{
DelegationList dl1(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}), false),
dl2(makeDelegationListBlock(tlv::Content, {DEL1B, DEL2A}), false);
BOOST_CHECK_NE(dl1, dl2);
}
BOOST_AUTO_TEST_SUITE_END() // Compare
BOOST_AUTO_TEST_SUITE(Print)
BOOST_AUTO_TEST_CASE(PrintEmpty)
{
DelegationList dl;
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[]");
}
BOOST_AUTO_TEST_CASE(PrintNormal)
{
DelegationList dl(makeDelegationListBlock(tlv::ForwardingHint, {DEL2A, DEL1B}));
BOOST_CHECK_EQUAL(boost::lexical_cast<std::string>(dl), "[/B(1),/A(2)]");
}
BOOST_AUTO_TEST_SUITE_END() // Print
BOOST_AUTO_TEST_SUITE_END() // TestDelegationList
} // namespace tests
} // namespace ndn