blob: f6eb2a8dccc2844d3b9e9904001a57d275a7b915 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2013-2023 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 "ndn-cxx/util/segmenter.hpp"
#include "tests/boost-test.hpp"
#include "tests/key-chain-fixture.hpp"
namespace ndn::tests {
BOOST_AUTO_TEST_SUITE(Util)
BOOST_FIXTURE_TEST_SUITE(TestSegmenter, KeyChainFixture)
BOOST_AUTO_TEST_CASE(Invalid)
{
Segmenter segmenter(m_keyChain, security::SigningInfo{});
std::vector<std::shared_ptr<Data>> v;
const uint8_t arr[] = {1, 2, 3, 4};
// maxSegmentSize == 0
BOOST_CHECK_THROW(v = segmenter.segment(arr, "/foo", 0, 1_s), std::invalid_argument);
std::istringstream ss1("foo");
BOOST_CHECK_THROW(v = segmenter.segment(ss1, "/foo", 0, 1_s), std::invalid_argument);
// negative freshnessPeriod
BOOST_CHECK_THROW(v = segmenter.segment(arr, "/foo", 100, -1_s), std::invalid_argument);
std::istringstream ss2("foo");
BOOST_CHECK_THROW(v = segmenter.segment(ss2, "/foo", 100, -1_s), std::invalid_argument);
}
BOOST_AUTO_TEST_CASE(EmptyInput)
{
Segmenter segmenter(m_keyChain, security::SigningInfo{});
auto check = [] (const auto& v) {
BOOST_REQUIRE_EQUAL(v.size(), 1);
BOOST_TEST(v[0]->getName() == "/empty/seg=0");
BOOST_TEST(v[0]->getContentType() == tlv::ContentType_Nack);
BOOST_TEST(v[0]->getFreshnessPeriod() == 1_s);
BOOST_TEST(v[0]->getFinalBlock().value() == name::Component::fromSegment(0));
BOOST_TEST(v[0]->getContent().value_size() == 0);
};
check(segmenter.segment(span<uint8_t>{}, "/empty", 1000, 1_s, tlv::ContentType_Nack));
std::istringstream ss;
check(segmenter.segment(ss, "/empty", 1000, 1_s, tlv::ContentType_Nack));
}
BOOST_AUTO_TEST_CASE(OneSegment)
{
Segmenter segmenter(m_keyChain, security::SigningInfo{});
auto check = [] (const auto& v, size_t expectedContentSize) {
BOOST_REQUIRE_EQUAL(v.size(), 1);
BOOST_TEST(v[0]->getName() == "/one/seg=0");
BOOST_TEST(v[0]->getContentType() == tlv::ContentType_Blob);
BOOST_TEST(v[0]->getFreshnessPeriod() == 1_min);
BOOST_TEST(v[0]->getFinalBlock().value() == name::Component::fromSegment(0));
BOOST_TEST(v[0]->getContent().value_size() == expectedContentSize);
};
const uint8_t arr1[] = {1, 2, 3, 4};
check(segmenter.segment(arr1, "/one", 10, 1_min), sizeof(arr1));
std::istringstream ss1("1234");
check(segmenter.segment(ss1, "/one", 10, 1_min), 4);
const uint8_t arr2[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
check(segmenter.segment(arr2, "/one", 10, 1_min), sizeof(arr2));
std::istringstream ss2("1234567890");
check(segmenter.segment(ss2, "/one", 10, 1_min), 10);
}
const uint8_t BLOB[] = { // 300 bytes
0x06, 0xFD, 0x01, 0xBB, 0x07, 0x33, 0x08, 0x03, 0x6E, 0x64, 0x6E, 0x08, 0x05, 0x73, 0x69, 0x74,
0x65, 0x31, 0x08, 0x03, 0x4B, 0x45, 0x59, 0x08, 0x11, 0x6B, 0x73, 0x6B, 0x2D, 0x31, 0x34, 0x31,
0x36, 0x34, 0x32, 0x35, 0x33, 0x37, 0x37, 0x30, 0x39, 0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0x01, 0x11, 0x16, 0x55, 0x1B, 0x01, 0x01, 0x1C, 0x26, 0x07, 0x24, 0x08,
0x03, 0x6E, 0x64, 0x6E, 0x08, 0x05, 0x73, 0x69, 0x74, 0x65, 0x31, 0x08, 0x03, 0x4B, 0x45, 0x59,
0x08, 0x11, 0x6B, 0x73, 0x6B, 0x2D, 0x32, 0x35, 0x31, 0x36, 0x34, 0x32, 0x35, 0x33, 0x37, 0x37,
0x30, 0x39, 0x34, 0xFD, 0x00, 0xFD, 0x26, 0xFD, 0x00, 0xFE, 0x0F, 0x17, 0x80, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
BOOST_AUTO_TEST_CASE(MultipleSegments)
{
Segmenter segmenter(m_keyChain, security::SigningInfo{});
auto check = [] (const auto& v) {
BOOST_TEST(v.size() == 8);
size_t segNo = 0;
for (const auto& seg : v) {
BOOST_TEST(seg->getName().getPrefix(-1) == "/many");
BOOST_TEST(seg->getName().at(-1).toSegment() == segNo);
BOOST_TEST(seg->getContentType() == tlv::ContentType_Blob);
BOOST_TEST(seg->getFreshnessPeriod() == 30_s);
BOOST_TEST(seg->getFinalBlock().value() == name::Component::fromSegment(7));
// last segment is shorter
const size_t expectedContentSize = segNo == 7 ? (sizeof(BLOB) % 42) : 42;
BOOST_TEST(seg->getContent().value_bytes() == make_span(BLOB).subspan(segNo * 42, expectedContentSize),
boost::test_tools::per_element());
segNo++;
}
};
check(segmenter.segment(BLOB, "/many", 42, 30_s));
std::istringstream ss(std::string(reinterpret_cast<const char*>(BLOB), sizeof(BLOB)));
check(segmenter.segment(ss, "/many", 42, 30_s));
}
BOOST_AUTO_TEST_SUITE_END() // TestSegmenter
BOOST_AUTO_TEST_SUITE_END() // Util
} // namespace ndn::tests