blob: 035f5e2453e5a36878f9f3e29de16a0d3387909f [file] [log] [blame]
Jiewen Tana0497d82015-02-02 21:59:18 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
29
30#include <ndn-cxx/encoding/tlv.hpp>
31#include <ndn-cxx/util/dummy-client-face.hpp>
32
33#include <boost/mpl/int.hpp>
34#include <boost/mpl/vector.hpp>
35
36namespace nlsr {
37namespace tests {
38
39template<int64_t N=10000>
40class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace>
41{
42public:
43 TestSegmentPublisher(ndn::util::DummyClientFace& face,
Jiewen Tana0497d82015-02-02 21:59:18 -080044 ndn::KeyChain& keyChain,
45 const ndn::time::milliseconds freshnessPeriod)
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050046 : SegmentPublisher(face, keyChain, freshnessPeriod)
Jiewen Tana0497d82015-02-02 21:59:18 -080047 , m_totalPayloadLength(0)
48 {
49
50 }
51
52 virtual
53 ~TestSegmentPublisher()
54 {
55 }
56
57 uint16_t
58 getLimit() const
59 {
60 return N;
61 }
62
63 size_t
64 getTotalPayloadLength() const
65 {
66 return m_totalPayloadLength;
67 }
68
69protected:
70
71 virtual size_t
72 generate(ndn::EncodingBuffer& outBuffer)
73 {
74 size_t totalLength = 0;
75 for (int64_t i = 0; i < N; i++)
76 {
Vince Lehmanf4772d42015-06-29 14:02:22 -050077 totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::tlv::Content, i);
Jiewen Tana0497d82015-02-02 21:59:18 -080078 }
79 m_totalPayloadLength += totalLength;
80 return totalLength;
81 }
82
83protected:
84 size_t m_totalPayloadLength;
85};
86
87template<int64_t N>
88class SegmentPublisherFixture
89{
90public:
91 SegmentPublisherFixture()
Muktadir R Chowdhuryc69da0a2015-12-18 13:24:38 -060092 : m_face(std::make_shared<ndn::util::DummyClientFace>())
Jiewen Tana0497d82015-02-02 21:59:18 -080093 , m_expectedFreshnessPeriod(ndn::time::milliseconds(111))
Vince Lehmand6bb3fa2015-04-24 14:21:39 -050094 , m_publisher(*m_face, m_keyChain, m_expectedFreshnessPeriod)
95 , m_publishingPrefix("/localhost/nfd/SegmentPublisherFixture")
Jiewen Tana0497d82015-02-02 21:59:18 -080096 {
97 }
98
99 void
100 validate(const ndn::Data& data)
101 {
102 BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod);
103
104 ndn::Block payload = data.getContent();
105
106 m_buffer.appendByteArray(payload.value(), payload.value_size());
107
108 // uint64_t segmentNo = data.getName()[-1].toSegment();
109 if (data.getFinalBlockId() != data.getName()[-1])
110 {
111 return;
112 }
113
114 // wrap data in a single Content TLV for easy parsing
115 m_buffer.prependVarNumber(m_buffer.size());
116 m_buffer.prependVarNumber(ndn::tlv::Content);
117
118 BOOST_TEST_CHECKPOINT("creating parser");
119 ndn::Block parser(m_buffer.buf(), m_buffer.size());
120 BOOST_TEST_CHECKPOINT("parsing aggregated response");
121 parser.parse();
122
123 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
124
125 uint64_t expectedNo = m_publisher.getLimit() - 1;
126 for (ndn::Block::element_const_iterator i = parser.elements_begin();
127 i != parser.elements_end();
128 ++i)
129 {
130 uint64_t number = readNonNegativeInteger(*i);
131 BOOST_REQUIRE_EQUAL(number, expectedNo);
132 --expectedNo;
133 }
134 }
135
136protected:
137 ndn::shared_ptr<ndn::util::DummyClientFace> m_face;
138 const ndn::time::milliseconds m_expectedFreshnessPeriod;
139 TestSegmentPublisher<N> m_publisher;
140 ndn::EncodingBuffer m_buffer;
141 ndn::KeyChain m_keyChain;
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500142 const ndn::Name m_publishingPrefix;
Jiewen Tana0497d82015-02-02 21:59:18 -0800143};
144
145using boost::mpl::int_;
146typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes;
147
148BOOST_AUTO_TEST_SUITE(PublisherTestSegmentPublisher)
149
150BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
151{
Vince Lehmand6bb3fa2015-04-24 14:21:39 -0500152 this->m_publisher.publish(this->m_publishingPrefix);
Jiewen Tana0497d82015-02-02 21:59:18 -0800153 this->m_face->processEvents();
154
155 size_t nSegments = this->m_publisher.getTotalPayloadLength() /
156 this->m_publisher.getMaxSegmentSize();
157 if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
158 nSegments == 0)
159 ++nSegments;
160
161 BOOST_CHECK_EQUAL(this->m_face->sentDatas.size(), nSegments);
162 for (const ndn::Data& data : this->m_face->sentDatas) {
163 this->validate(data);
164 }
165}
166
167BOOST_AUTO_TEST_SUITE_END()
168
169} // namespace tests
170} // namespace nlsr