blob: 4223d19677cc2becb7be5d42ecda639011294850 [file] [log] [blame]
Vince Lehman5144f822014-07-23 15:12:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi221b6fe2016-07-14 18:21:56 +00003 * Copyright (c) 2014-2016, Regents of the University of California,
Steve DiBenedetto8919abe2015-01-28 09:34:54 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Vince Lehman5144f822014-07-23 15:12:56 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "core/segment-publisher.hpp"
Vince Lehman5144f822014-07-23 15:12:56 -070027#include <ndn-cxx/encoding/tlv.hpp>
28
Junxiao Shi376f7372014-11-17 18:03:31 -070029#include "tests/test-common.hpp"
Junxiao Shi221b6fe2016-07-14 18:21:56 +000030#include "tests/identity-management-fixture.hpp"
Junxiao Shi376f7372014-11-17 18:03:31 -070031#include <ndn-cxx/util/dummy-client-face.hpp>
Vince Lehman5144f822014-07-23 15:12:56 -070032
33namespace nfd {
34namespace tests {
35
36NFD_LOG_INIT("SegmentPublisherTest");
37
Wentao Shange612e2f2014-08-04 10:24:59 -040038template<int64_t N=10000>
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080039class SegmentPublisherTester : public SegmentPublisher<ndn::util::DummyClientFace>
Vince Lehman5144f822014-07-23 15:12:56 -070040{
41public:
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -080042 SegmentPublisherTester(ndn::util::DummyClientFace& face,
Junxiao Shi221b6fe2016-07-14 18:21:56 +000043 const Name& prefix,
44 ndn::KeyChain& keyChain,
45 const time::milliseconds freshnessPeriod)
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070046 : SegmentPublisher(face, prefix, keyChain, freshnessPeriod)
Vince Lehman5144f822014-07-23 15:12:56 -070047 , m_totalPayloadLength(0)
48 {
49
50 }
51
52 virtual
Junxiao Shi221b6fe2016-07-14 18:21:56 +000053 ~SegmentPublisherTester() = default;
Vince Lehman5144f822014-07-23 15:12:56 -070054
55 uint16_t
56 getLimit() const
57 {
58 return N;
59 }
60
61 size_t
62 getTotalPayloadLength() const
63 {
64 return m_totalPayloadLength;
65 }
66
67protected:
68
69 virtual size_t
70 generate(ndn::EncodingBuffer& outBuffer)
71 {
72 size_t totalLength = 0;
Junxiao Shi221b6fe2016-07-14 18:21:56 +000073 for (int64_t i = 0; i < N; ++i) {
74 totalLength += prependNonNegativeIntegerBlock(outBuffer, tlv::Content, i);
75 }
Vince Lehman5144f822014-07-23 15:12:56 -070076 m_totalPayloadLength += totalLength;
77 return totalLength;
78 }
79
80protected:
81 size_t m_totalPayloadLength;
82};
83
Wentao Shange612e2f2014-08-04 10:24:59 -040084template<int64_t N>
Junxiao Shi221b6fe2016-07-14 18:21:56 +000085class SegmentPublisherFixture : public IdentityManagementFixture
Vince Lehman5144f822014-07-23 15:12:56 -070086{
87public:
88 SegmentPublisherFixture()
Junxiao Shi221b6fe2016-07-14 18:21:56 +000089 : m_face(m_keyChain)
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070090 , m_expectedFreshnessPeriod(time::milliseconds(111))
Junxiao Shi221b6fe2016-07-14 18:21:56 +000091 , m_publisher(m_face, "/localhost/nfd/SegmentPublisherFixture",
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070092 m_keyChain, m_expectedFreshnessPeriod)
Vince Lehman5144f822014-07-23 15:12:56 -070093 {
94 }
95
96 void
97 validate(const Data& data)
98 {
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070099 BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod);
100
Vince Lehman5144f822014-07-23 15:12:56 -0700101 Block payload = data.getContent();
102 NFD_LOG_DEBUG("payload size (w/o Content TLV): " << payload.value_size());
103
104 m_buffer.appendByteArray(payload.value(), payload.value_size());
105
106 uint64_t segmentNo = data.getName()[-1].toSegment();
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000107 if (data.getFinalBlockId() != data.getName()[-1]) {
108 return;
109 }
Vince Lehman5144f822014-07-23 15:12:56 -0700110
111 NFD_LOG_DEBUG("got final block: #" << segmentNo);
112
113 // wrap data in a single Content TLV for easy parsing
114 m_buffer.prependVarNumber(m_buffer.size());
Junxiao Shi67f11ac2014-10-19 09:29:13 -0700115 m_buffer.prependVarNumber(tlv::Content);
Vince Lehman5144f822014-07-23 15:12:56 -0700116
117 BOOST_TEST_CHECKPOINT("creating parser");
118 ndn::Block parser(m_buffer.buf(), m_buffer.size());
119 BOOST_TEST_CHECKPOINT("parsing aggregated response");
120 parser.parse();
121
122 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
123
124 uint64_t expectedNo = m_publisher.getLimit() - 1;
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000125 std::for_each(parser.elements_begin(), parser.elements_end(),
126 [&expectedNo] (const Block& element) {
127 uint64_t number = readNonNegativeInteger(element);
Vince Lehman5144f822014-07-23 15:12:56 -0700128 BOOST_REQUIRE_EQUAL(number, expectedNo);
129 --expectedNo;
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000130 });
Vince Lehman5144f822014-07-23 15:12:56 -0700131 }
132
133protected:
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000134 ndn::util::DummyClientFace m_face;
Steve DiBenedetto8919abe2015-01-28 09:34:54 -0700135 const time::milliseconds m_expectedFreshnessPeriod;
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800136 SegmentPublisherTester<N> m_publisher;
Vince Lehman5144f822014-07-23 15:12:56 -0700137 ndn::EncodingBuffer m_buffer;
138 ndn::KeyChain m_keyChain;
139};
140
141using boost::mpl::int_;
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000142typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0>> DatasetSizes;
Vince Lehman5144f822014-07-23 15:12:56 -0700143
Spyridon Mastorakisd0381c02015-02-19 10:29:41 -0800144BOOST_AUTO_TEST_SUITE(TestSegmentPublisher)
Vince Lehman5144f822014-07-23 15:12:56 -0700145
146BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
147{
148 this->m_publisher.publish();
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000149 this->m_face.processEvents();
Vince Lehman5144f822014-07-23 15:12:56 -0700150
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000151 size_t nSegments =
152 this->m_publisher.getTotalPayloadLength() / this->m_publisher.getMaxSegmentSize() +
153 (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
154 this->m_publisher.getTotalPayloadLength() == 0);
Vince Lehman5144f822014-07-23 15:12:56 -0700155
Junxiao Shi221b6fe2016-07-14 18:21:56 +0000156 BOOST_CHECK_EQUAL(this->m_face.sentData.size(), nSegments);
157 for (const Data& data : this->m_face.sentData) {
Vince Lehman5144f822014-07-23 15:12:56 -0700158 this->validate(data);
159 }
160}
161
162BOOST_AUTO_TEST_SUITE_END()
163
164} // namespace tests
165} // namespace nfd