blob: e1a72324b6adb42fc5632adfce86c64a99fddf83 [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Nick Gordonfeae5572017-01-13 12:06:26 -06003 * Copyright (c) 2014-2017, Regents of the University of California,
Jiewen Tana0497d82015-02-02 21:59:18 -08004 * 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.
10 *
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 "publisher/segment-publisher.hpp"
27
28#include "../boost-test.hpp"
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050029#include "../test-common.hpp"
Jiewen Tana0497d82015-02-02 21:59:18 -080030
31#include <ndn-cxx/encoding/tlv.hpp>
32#include <ndn-cxx/util/dummy-client-face.hpp>
33
34#include <boost/mpl/int.hpp>
35#include <boost/mpl/vector.hpp>
36
37namespace nlsr {
38namespace tests {
39
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050040using namespace nlsr::test;
41
Jiewen Tana0497d82015-02-02 21:59:18 -080042template<int64_t N=10000>
43class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace>
44{
45public:
46 TestSegmentPublisher(ndn::util::DummyClientFace& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080047 ndn::KeyChain& keyChain,
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050048 ndn::security::SigningInfo& signingInfo,
Jiewen Tana0497d82015-02-02 21:59:18 -080049 const ndn::time::milliseconds freshnessPeriod)
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050050 : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod)
Jiewen Tana0497d82015-02-02 21:59:18 -080051 , m_totalPayloadLength(0)
52 {
53
54 }
55
56 virtual
57 ~TestSegmentPublisher()
58 {
59 }
60
61 uint16_t
62 getLimit() const
63 {
64 return N;
65 }
66
67 size_t
68 getTotalPayloadLength() const
69 {
70 return m_totalPayloadLength;
71 }
72
73protected:
74
75 virtual size_t
76 generate(ndn::EncodingBuffer& outBuffer)
77 {
78 size_t totalLength = 0;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050079 for (int64_t i = 0; i < N; i++) {
Vince Lehmanf4772d42015-06-29 14:02:22 -050080 totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::tlv::Content, i);
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050081 }
82
Jiewen Tana0497d82015-02-02 21:59:18 -080083 m_totalPayloadLength += totalLength;
84 return totalLength;
85 }
86
87protected:
88 size_t m_totalPayloadLength;
89};
90
91template<int64_t N>
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050092class SegmentPublisherFixture : public BaseFixture
Jiewen Tana0497d82015-02-02 21:59:18 -080093{
94public:
95 SegmentPublisherFixture()
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050096 : m_face(std::make_shared<ndn::util::DummyClientFace>(m_ioService, m_keyChain))
Jiewen Tana0497d82015-02-02 21:59:18 -080097 , m_expectedFreshnessPeriod(ndn::time::milliseconds(111))
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -050098 , m_publisher(*m_face, m_keyChain, m_signingInfo, m_expectedFreshnessPeriod)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050099 , m_publishingPrefix("/localhost/nfd/SegmentPublisherFixture")
Jiewen Tana0497d82015-02-02 21:59:18 -0800100 {
101 }
102
103 void
104 validate(const ndn::Data& data)
105 {
106 BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod);
107
108 ndn::Block payload = data.getContent();
109
110 m_buffer.appendByteArray(payload.value(), payload.value_size());
111
112 // uint64_t segmentNo = data.getName()[-1].toSegment();
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500113 if (data.getFinalBlockId() != data.getName()[-1]) {
Jiewen Tana0497d82015-02-02 21:59:18 -0800114 return;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500115 }
Jiewen Tana0497d82015-02-02 21:59:18 -0800116
117 // wrap data in a single Content TLV for easy parsing
118 m_buffer.prependVarNumber(m_buffer.size());
119 m_buffer.prependVarNumber(ndn::tlv::Content);
120
121 BOOST_TEST_CHECKPOINT("creating parser");
122 ndn::Block parser(m_buffer.buf(), m_buffer.size());
123 BOOST_TEST_CHECKPOINT("parsing aggregated response");
124 parser.parse();
125
126 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
127
128 uint64_t expectedNo = m_publisher.getLimit() - 1;
129 for (ndn::Block::element_const_iterator i = parser.elements_begin();
130 i != parser.elements_end();
131 ++i)
132 {
133 uint64_t number = readNonNegativeInteger(*i);
134 BOOST_REQUIRE_EQUAL(number, expectedNo);
135 --expectedNo;
136 }
137 }
138
139protected:
dmcoomes9f936662017-03-02 10:33:09 -0600140 std::shared_ptr<ndn::util::DummyClientFace> m_face;
Jiewen Tana0497d82015-02-02 21:59:18 -0800141 const ndn::time::milliseconds m_expectedFreshnessPeriod;
142 TestSegmentPublisher<N> m_publisher;
143 ndn::EncodingBuffer m_buffer;
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -0500144 ndn::security::SigningInfo m_signingInfo;
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500145 const ndn::Name m_publishingPrefix;
Jiewen Tana0497d82015-02-02 21:59:18 -0800146};
147
148using boost::mpl::int_;
149typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes;
150
151BOOST_AUTO_TEST_SUITE(PublisherTestSegmentPublisher)
152
153BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
154{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500155 this->m_publisher.publish(this->m_publishingPrefix);
Jiewen Tana0497d82015-02-02 21:59:18 -0800156 this->m_face->processEvents();
157
158 size_t nSegments = this->m_publisher.getTotalPayloadLength() /
159 this->m_publisher.getMaxSegmentSize();
160 if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
161 nSegments == 0)
162 ++nSegments;
163
Junxiao Shic778e812016-07-14 15:44:26 +0000164 BOOST_CHECK_EQUAL(this->m_face->sentData.size(), nSegments);
165 for (const ndn::Data& data : this->m_face->sentData) {
Jiewen Tana0497d82015-02-02 21:59:18 -0800166 this->validate(data);
167 }
168}
169
170BOOST_AUTO_TEST_SUITE_END()
171
172} // namespace tests
173} // namespace nlsr