Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 1 | /* -*- 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" |
| 29 | #include "tests/dummy-face.hpp" |
| 30 | |
| 31 | #include <ndn-cxx/encoding/tlv.hpp> |
| 32 | |
| 33 | #include <boost/foreach.hpp> |
| 34 | |
| 35 | namespace nfd { |
| 36 | namespace tests { |
| 37 | |
| 38 | NFD_LOG_INIT("SegmentPublisherTest"); |
| 39 | |
| 40 | template<size_t N=10000> |
| 41 | class TestSegmentPublisher : public SegmentPublisher<DummyFace> |
| 42 | { |
| 43 | public: |
| 44 | TestSegmentPublisher(DummyFace& face, |
| 45 | 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 | |
| 70 | protected: |
| 71 | |
| 72 | virtual size_t |
| 73 | generate(ndn::EncodingBuffer& outBuffer) |
| 74 | { |
| 75 | size_t totalLength = 0; |
| 76 | for (uint64_t i = 0; i < N; i++) |
| 77 | { |
| 78 | totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::Tlv::Content, i); |
| 79 | } |
| 80 | m_totalPayloadLength += totalLength; |
| 81 | return totalLength; |
| 82 | } |
| 83 | |
| 84 | protected: |
| 85 | size_t m_totalPayloadLength; |
| 86 | }; |
| 87 | |
| 88 | template<size_t N> |
| 89 | class SegmentPublisherFixture : public BaseFixture |
| 90 | { |
| 91 | public: |
| 92 | SegmentPublisherFixture() |
| 93 | : m_face(makeDummyFace()) |
| 94 | , 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 | |
| 136 | protected: |
| 137 | shared_ptr<DummyFace> m_face; |
| 138 | TestSegmentPublisher<N> m_publisher; |
| 139 | ndn::EncodingBuffer m_buffer; |
| 140 | ndn::KeyChain m_keyChain; |
| 141 | }; |
| 142 | |
| 143 | using boost::mpl::int_; |
Alexander Afanasyev | 4e9a98f | 2014-07-23 11:12:30 -0700 | [diff] [blame^] | 144 | typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 145 | |
| 146 | BOOST_AUTO_TEST_SUITE(SegmentPublisher) |
| 147 | |
| 148 | BOOST_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 | |
| 165 | BOOST_AUTO_TEST_SUITE_END() |
| 166 | |
| 167 | } // namespace tests |
| 168 | } // namespace nfd |