blob: 881766991586c598c2a3b2d44e1971e65d427244 [file] [log] [blame]
Vince Lehman5144f822014-07-23 15:12:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, 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 "core/segment-publisher.hpp"
27
28#include "tests/test-common.hpp"
Junxiao Shi15b12e72014-08-09 19:56:24 -070029#include "tests/dummy-client-face.hpp"
Vince Lehman5144f822014-07-23 15:12:56 -070030
31#include <ndn-cxx/encoding/tlv.hpp>
32
33#include <boost/foreach.hpp>
34
35namespace nfd {
36namespace tests {
37
38NFD_LOG_INIT("SegmentPublisherTest");
39
Wentao Shange612e2f2014-08-04 10:24:59 -040040template<int64_t N=10000>
Junxiao Shi15b12e72014-08-09 19:56:24 -070041class TestSegmentPublisher : public SegmentPublisher<DummyClientFace>
Vince Lehman5144f822014-07-23 15:12:56 -070042{
43public:
Junxiao Shi15b12e72014-08-09 19:56:24 -070044 TestSegmentPublisher(DummyClientFace& face,
Vince Lehman5144f822014-07-23 15:12:56 -070045 const Name& prefix,
46 ndn::KeyChain& keyChain)
47 : SegmentPublisher(face, prefix, keyChain)
48 , m_totalPayloadLength(0)
49 {
50
51 }
52
53 virtual
54 ~TestSegmentPublisher()
55 {
56 }
57
58 uint16_t
59 getLimit() const
60 {
61 return N;
62 }
63
64 size_t
65 getTotalPayloadLength() const
66 {
67 return m_totalPayloadLength;
68 }
69
70protected:
71
72 virtual size_t
73 generate(ndn::EncodingBuffer& outBuffer)
74 {
75 size_t totalLength = 0;
Wentao Shange612e2f2014-08-04 10:24:59 -040076 for (int64_t i = 0; i < N; i++)
Vince Lehman5144f822014-07-23 15:12:56 -070077 {
78 totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::Tlv::Content, i);
79 }
80 m_totalPayloadLength += totalLength;
81 return totalLength;
82 }
83
84protected:
85 size_t m_totalPayloadLength;
86};
87
Wentao Shange612e2f2014-08-04 10:24:59 -040088template<int64_t N>
Vince Lehman5144f822014-07-23 15:12:56 -070089class SegmentPublisherFixture : public BaseFixture
90{
91public:
92 SegmentPublisherFixture()
Junxiao Shi15b12e72014-08-09 19:56:24 -070093 : m_face(makeDummyClientFace())
Vince Lehman5144f822014-07-23 15:12:56 -070094 , m_publisher(*m_face, "/localhost/nfd/SegmentPublisherFixture", m_keyChain)
95 {
96 }
97
98 void
99 validate(const Data& data)
100 {
101 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();
107 if (data.getFinalBlockId() != data.getName()[-1])
108 {
109 return;
110 }
111
112 NFD_LOG_DEBUG("got final block: #" << segmentNo);
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 (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:
Junxiao Shi15b12e72014-08-09 19:56:24 -0700137 shared_ptr<DummyClientFace> m_face;
Vince Lehman5144f822014-07-23 15:12:56 -0700138 TestSegmentPublisher<N> m_publisher;
139 ndn::EncodingBuffer m_buffer;
140 ndn::KeyChain m_keyChain;
141};
142
143using boost::mpl::int_;
Alexander Afanasyev4e9a98f2014-07-23 11:12:30 -0700144typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes;
Vince Lehman5144f822014-07-23 15:12:56 -0700145
146BOOST_AUTO_TEST_SUITE(SegmentPublisher)
147
148BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>)
149{
150 this->m_publisher.publish();
151 this->m_face->processEvents();
152
153 size_t nSegments = this->m_publisher.getTotalPayloadLength() /
154 this->m_publisher.getMaxSegmentSize();
155 if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 ||
156 nSegments == 0)
157 ++nSegments;
158
159 BOOST_CHECK_EQUAL(this->m_face->m_sentDatas.size(), nSegments);
160 BOOST_FOREACH(const Data& data, this->m_face->m_sentDatas) {
161 this->validate(data);
162 }
163}
164
165BOOST_AUTO_TEST_SUITE_END()
166
167} // namespace tests
168} // namespace nfd