blob: 43e4523cc77251394ec5ba23027d73d95a7e6289 [file] [log] [blame]
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * 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 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060024
25#include "mgmt/segment-publisher.hpp"
26#include "mgmt/internal-face.hpp"
27#include "mgmt/app-face.hpp"
28
29#include "tests/test-common.hpp"
Alexander Afanasyev4a771362014-04-24 21:29:33 -070030#include <ndn-cxx/encoding/tlv.hpp>
Steve DiBenedetto9f6c3642014-03-10 17:02:27 -060031
32namespace nfd {
33namespace tests {
34
35NFD_LOG_INIT("SegmentPublisherTest");
36
37class TestSegmentPublisher : public SegmentPublisher
38{
39public:
40 TestSegmentPublisher(shared_ptr<AppFace> face,
41 const Name& prefix,
42 const uint64_t limit=10000)
43 : SegmentPublisher(face, prefix)
44 , m_limit((limit == 0)?(1):(limit))
45 {
46
47 }
48
49 virtual
50 ~TestSegmentPublisher()
51 {
52
53 }
54
55 uint16_t
56 getLimit() const
57 {
58 return m_limit;
59 }
60
61protected:
62
63 virtual size_t
64 generate(ndn::EncodingBuffer& outBuffer)
65 {
66 size_t totalLength = 0;
67 for (uint64_t i = 0; i < m_limit; i++)
68 {
69 totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::Tlv::Content, i);
70 }
71 return totalLength;
72 }
73
74protected:
75 const uint64_t m_limit;
76};
77
78class SegmentPublisherFixture : public BaseFixture
79{
80public:
81 SegmentPublisherFixture()
82 : m_face(make_shared<InternalFace>())
83 , m_publisher(m_face, "/localhost/nfd/SegmentPublisherFixture")
84 , m_finished(false)
85 {
86
87 }
88
89 void
90 validate(const Data& data)
91 {
92 Block payload = data.getContent();
93 NFD_LOG_DEBUG("payload size (w/o Content TLV): " << payload.value_size());
94
95 m_buffer.appendByteArray(payload.value(), payload.value_size());
96
97 uint64_t segmentNo = data.getName()[-1].toSegment();
98 if (data.getFinalBlockId() != data.getName()[-1])
99 {
100 return;
101 }
102
103 NFD_LOG_DEBUG("got final block: #" << segmentNo);
104
105 // wrap data in a single Content TLV for easy parsing
106 m_buffer.prependVarNumber(m_buffer.size());
107 m_buffer.prependVarNumber(ndn::Tlv::Content);
108
109 BOOST_TEST_CHECKPOINT("creating parser");
110 ndn::Block parser(m_buffer.buf(), m_buffer.size());
111 BOOST_TEST_CHECKPOINT("parsing aggregated response");
112 parser.parse();
113
114 BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit());
115
116 uint64_t expectedNo = m_publisher.getLimit() - 1;
117 for (Block::element_const_iterator i = parser.elements_begin();
118 i != parser.elements_end();
119 ++i)
120 {
121 uint64_t number = readNonNegativeInteger(*i);
122 BOOST_REQUIRE_EQUAL(number, expectedNo);
123 --expectedNo;
124 }
125 m_finished = true;
126 }
127
128protected:
129 shared_ptr<InternalFace> m_face;
130 TestSegmentPublisher m_publisher;
131 ndn::EncodingBuffer m_buffer;
132
133protected:
134 bool m_finished;
135};
136
137BOOST_FIXTURE_TEST_SUITE(MgmtSegmentPublisher, SegmentPublisherFixture)
138
139BOOST_AUTO_TEST_CASE(Generate)
140{
141 m_face->onReceiveData +=
142 bind(&SegmentPublisherFixture::validate, this, _1);
143
144 m_publisher.publish();
145 BOOST_REQUIRE(m_finished);
146}
147
148BOOST_AUTO_TEST_SUITE_END()
149
150} // namespace tests
151} // namespace nfd