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" |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 27 | #include <ndn-cxx/encoding/tlv.hpp> |
| 28 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 29 | #include "tests/test-common.hpp" |
| 30 | #include <ndn-cxx/util/dummy-client-face.hpp> |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | NFD_LOG_INIT("SegmentPublisherTest"); |
| 36 | |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 37 | template<int64_t N=10000> |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 38 | class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace> |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 39 | { |
| 40 | public: |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 41 | TestSegmentPublisher(ndn::util::DummyClientFace& face, |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 42 | const Name& prefix, |
| 43 | ndn::KeyChain& keyChain) |
| 44 | : SegmentPublisher(face, prefix, keyChain) |
| 45 | , m_totalPayloadLength(0) |
| 46 | { |
| 47 | |
| 48 | } |
| 49 | |
| 50 | virtual |
| 51 | ~TestSegmentPublisher() |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | uint16_t |
| 56 | getLimit() const |
| 57 | { |
| 58 | return N; |
| 59 | } |
| 60 | |
| 61 | size_t |
| 62 | getTotalPayloadLength() const |
| 63 | { |
| 64 | return m_totalPayloadLength; |
| 65 | } |
| 66 | |
| 67 | protected: |
| 68 | |
| 69 | virtual size_t |
| 70 | generate(ndn::EncodingBuffer& outBuffer) |
| 71 | { |
| 72 | size_t totalLength = 0; |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 73 | for (int64_t i = 0; i < N; i++) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 74 | { |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame] | 75 | totalLength += prependNonNegativeIntegerBlock(outBuffer, tlv::Content, i); |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 76 | } |
| 77 | m_totalPayloadLength += totalLength; |
| 78 | return totalLength; |
| 79 | } |
| 80 | |
| 81 | protected: |
| 82 | size_t m_totalPayloadLength; |
| 83 | }; |
| 84 | |
Wentao Shang | e612e2f | 2014-08-04 10:24:59 -0400 | [diff] [blame] | 85 | template<int64_t N> |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 86 | class SegmentPublisherFixture : public BaseFixture |
| 87 | { |
| 88 | public: |
| 89 | SegmentPublisherFixture() |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 90 | : m_face(ndn::util::makeDummyClientFace()) |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 91 | , m_publisher(*m_face, "/localhost/nfd/SegmentPublisherFixture", m_keyChain) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void |
| 96 | validate(const Data& data) |
| 97 | { |
| 98 | Block payload = data.getContent(); |
| 99 | NFD_LOG_DEBUG("payload size (w/o Content TLV): " << payload.value_size()); |
| 100 | |
| 101 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 102 | |
| 103 | uint64_t segmentNo = data.getName()[-1].toSegment(); |
| 104 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 105 | { |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | NFD_LOG_DEBUG("got final block: #" << segmentNo); |
| 110 | |
| 111 | // wrap data in a single Content TLV for easy parsing |
| 112 | m_buffer.prependVarNumber(m_buffer.size()); |
Junxiao Shi | 67f11ac | 2014-10-19 09:29:13 -0700 | [diff] [blame] | 113 | m_buffer.prependVarNumber(tlv::Content); |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 114 | |
| 115 | BOOST_TEST_CHECKPOINT("creating parser"); |
| 116 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 117 | BOOST_TEST_CHECKPOINT("parsing aggregated response"); |
| 118 | parser.parse(); |
| 119 | |
| 120 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit()); |
| 121 | |
| 122 | uint64_t expectedNo = m_publisher.getLimit() - 1; |
| 123 | for (Block::element_const_iterator i = parser.elements_begin(); |
| 124 | i != parser.elements_end(); |
| 125 | ++i) |
| 126 | { |
| 127 | uint64_t number = readNonNegativeInteger(*i); |
| 128 | BOOST_REQUIRE_EQUAL(number, expectedNo); |
| 129 | --expectedNo; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | protected: |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 134 | shared_ptr<ndn::util::DummyClientFace> m_face; |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 135 | TestSegmentPublisher<N> m_publisher; |
| 136 | ndn::EncodingBuffer m_buffer; |
| 137 | ndn::KeyChain m_keyChain; |
| 138 | }; |
| 139 | |
| 140 | using boost::mpl::int_; |
Alexander Afanasyev | 4e9a98f | 2014-07-23 11:12:30 -0700 | [diff] [blame] | 141 | 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] | 142 | |
| 143 | BOOST_AUTO_TEST_SUITE(SegmentPublisher) |
| 144 | |
| 145 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>) |
| 146 | { |
| 147 | this->m_publisher.publish(); |
| 148 | this->m_face->processEvents(); |
| 149 | |
| 150 | size_t nSegments = this->m_publisher.getTotalPayloadLength() / |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 151 | this->m_publisher.getMaxSegmentSize(); |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 152 | if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 || |
| 153 | nSegments == 0) |
| 154 | ++nSegments; |
| 155 | |
Junxiao Shi | 376f737 | 2014-11-17 18:03:31 -0700 | [diff] [blame] | 156 | BOOST_CHECK_EQUAL(this->m_face->sentDatas.size(), nSegments); |
| 157 | for (const Data& data : this->m_face->sentDatas) { |
Vince Lehman | 5144f82 | 2014-07-23 15:12:56 -0700 | [diff] [blame] | 158 | this->validate(data); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | BOOST_AUTO_TEST_SUITE_END() |
| 163 | |
| 164 | } // namespace tests |
| 165 | } // namespace nfd |