blob: 7a9ee1d071c25f8a920c38c039c3202e229db148 [file] [log] [blame]
Vince Lehman5144f822014-07-23 15:12:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Steve DiBenedetto8919abe2015-01-28 09:34:54 -07003 * 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.
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"
30#include <ndn-cxx/util/dummy-client-face.hpp>
Vince Lehman5144f822014-07-23 15:12:56 -070031
32namespace nfd {
33namespace tests {
34
35NFD_LOG_INIT("SegmentPublisherTest");
36
Wentao Shange612e2f2014-08-04 10:24:59 -040037template<int64_t N=10000>
Junxiao Shi376f7372014-11-17 18:03:31 -070038class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace>
Vince Lehman5144f822014-07-23 15:12:56 -070039{
40public:
Junxiao Shi376f7372014-11-17 18:03:31 -070041 TestSegmentPublisher(ndn::util::DummyClientFace& face,
Vince Lehman5144f822014-07-23 15:12:56 -070042 const Name& prefix,
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070043 ndn::KeyChain& keyChain,
44 const time::milliseconds freshnessPeriod)
45 : SegmentPublisher(face, prefix, keyChain, freshnessPeriod)
Vince Lehman5144f822014-07-23 15:12:56 -070046 , m_totalPayloadLength(0)
47 {
48
49 }
50
51 virtual
52 ~TestSegmentPublisher()
53 {
54 }
55
56 uint16_t
57 getLimit() const
58 {
59 return N;
60 }
61
62 size_t
63 getTotalPayloadLength() const
64 {
65 return m_totalPayloadLength;
66 }
67
68protected:
69
70 virtual size_t
71 generate(ndn::EncodingBuffer& outBuffer)
72 {
73 size_t totalLength = 0;
Wentao Shange612e2f2014-08-04 10:24:59 -040074 for (int64_t i = 0; i < N; i++)
Vince Lehman5144f822014-07-23 15:12:56 -070075 {
Junxiao Shi67f11ac2014-10-19 09:29:13 -070076 totalLength += prependNonNegativeIntegerBlock(outBuffer, tlv::Content, i);
Vince Lehman5144f822014-07-23 15:12:56 -070077 }
78 m_totalPayloadLength += totalLength;
79 return totalLength;
80 }
81
82protected:
83 size_t m_totalPayloadLength;
84};
85
Wentao Shange612e2f2014-08-04 10:24:59 -040086template<int64_t N>
Vince Lehman5144f822014-07-23 15:12:56 -070087class SegmentPublisherFixture : public BaseFixture
88{
89public:
90 SegmentPublisherFixture()
Junxiao Shi376f7372014-11-17 18:03:31 -070091 : m_face(ndn::util::makeDummyClientFace())
Steve DiBenedetto8919abe2015-01-28 09:34:54 -070092 , m_expectedFreshnessPeriod(time::milliseconds(111))
93 , m_publisher(*m_face, "/localhost/nfd/SegmentPublisherFixture",
94 m_keyChain, m_expectedFreshnessPeriod)
Vince Lehman5144f822014-07-23 15:12:56 -070095 {
96 }
97
98 void
99 validate(const Data& data)
100 {
Steve DiBenedetto8919abe2015-01-28 09:34:54 -0700101 BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod);
102
Vince Lehman5144f822014-07-23 15:12:56 -0700103 Block payload = data.getContent();
104 NFD_LOG_DEBUG("payload size (w/o Content TLV): " << payload.value_size());
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 NFD_LOG_DEBUG("got final block: #" << segmentNo);
115
116 // wrap data in a single Content TLV for easy parsing
117 m_buffer.prependVarNumber(m_buffer.size());
Junxiao Shi67f11ac2014-10-19 09:29:13 -0700118 m_buffer.prependVarNumber(tlv::Content);
Vince Lehman5144f822014-07-23 15:12:56 -0700119
120 BOOST_TEST_CHECKPOINT("creating parser");
121 ndn::Block parser(m_buffer.buf(), m_buffer.size());
122 BOOST_TEST_CHECKPOINT("parsing aggregated response");
123 parser.parse();
124
125 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
126
127 uint64_t expectedNo = m_publisher.getLimit() - 1;
128 for (Block::element_const_iterator i = parser.elements_begin();
129 i != parser.elements_end();
130 ++i)
131 {
132 uint64_t number = readNonNegativeInteger(*i);
133 BOOST_REQUIRE_EQUAL(number, expectedNo);
134 --expectedNo;
135 }
136 }
137
138protected:
Junxiao Shi376f7372014-11-17 18:03:31 -0700139 shared_ptr<ndn::util::DummyClientFace> m_face;
Steve DiBenedetto8919abe2015-01-28 09:34:54 -0700140 const time::milliseconds m_expectedFreshnessPeriod;
Vince Lehman5144f822014-07-23 15:12:56 -0700141 TestSegmentPublisher<N> m_publisher;
142 ndn::EncodingBuffer m_buffer;
143 ndn::KeyChain m_keyChain;
144};
145
146using boost::mpl::int_;
Alexander Afanasyev4e9a98f2014-07-23 11:12:30 -0700147typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes;
Vince Lehman5144f822014-07-23 15:12:56 -0700148
149BOOST_AUTO_TEST_SUITE(SegmentPublisher)
150
151BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
152{
153 this->m_publisher.publish();
154 this->m_face->processEvents();
155
156 size_t nSegments = this->m_publisher.getTotalPayloadLength() /
Junxiao Shi376f7372014-11-17 18:03:31 -0700157 this->m_publisher.getMaxSegmentSize();
Vince Lehman5144f822014-07-23 15:12:56 -0700158 if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
159 nSegments == 0)
160 ++nSegments;
161
Junxiao Shi376f7372014-11-17 18:03:31 -0700162 BOOST_CHECK_EQUAL(this->m_face->sentDatas.size(), nSegments);
163 for (const Data& data : this->m_face->sentDatas) {
Vince Lehman5144f822014-07-23 15:12:56 -0700164 this->validate(data);
165 }
166}
167
168BOOST_AUTO_TEST_SUITE_END()
169
170} // namespace tests
171} // namespace nfd