Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Nick Gordon | feae557 | 2017-01-13 12:06:26 -0600 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 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 "publisher/segment-publisher.hpp" |
| 27 | |
| 28 | #include "../boost-test.hpp" |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 29 | #include "../test-common.hpp" |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <ndn-cxx/encoding/tlv.hpp> |
| 32 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 33 | |
| 34 | #include <boost/mpl/int.hpp> |
| 35 | #include <boost/mpl/vector.hpp> |
| 36 | |
| 37 | namespace nlsr { |
| 38 | namespace tests { |
| 39 | |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 40 | using namespace nlsr::test; |
| 41 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 42 | template<int64_t N=10000> |
| 43 | class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace> |
| 44 | { |
| 45 | public: |
| 46 | TestSegmentPublisher(ndn::util::DummyClientFace& face, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 47 | ndn::KeyChain& keyChain, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 48 | ndn::security::SigningInfo& signingInfo, |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 49 | const ndn::time::milliseconds freshnessPeriod) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 50 | : SegmentPublisher(face, keyChain, signingInfo, freshnessPeriod) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 51 | , m_totalPayloadLength(0) |
| 52 | { |
| 53 | |
| 54 | } |
| 55 | |
| 56 | virtual |
| 57 | ~TestSegmentPublisher() |
| 58 | { |
| 59 | } |
| 60 | |
| 61 | uint16_t |
| 62 | getLimit() const |
| 63 | { |
| 64 | return N; |
| 65 | } |
| 66 | |
| 67 | size_t |
| 68 | getTotalPayloadLength() const |
| 69 | { |
| 70 | return m_totalPayloadLength; |
| 71 | } |
| 72 | |
| 73 | protected: |
| 74 | |
| 75 | virtual size_t |
| 76 | generate(ndn::EncodingBuffer& outBuffer) |
| 77 | { |
| 78 | size_t totalLength = 0; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 79 | for (int64_t i = 0; i < N; i++) { |
Vince Lehman | f4772d4 | 2015-06-29 14:02:22 -0500 | [diff] [blame] | 80 | totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::tlv::Content, i); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 81 | } |
| 82 | |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 83 | m_totalPayloadLength += totalLength; |
| 84 | return totalLength; |
| 85 | } |
| 86 | |
| 87 | protected: |
| 88 | size_t m_totalPayloadLength; |
| 89 | }; |
| 90 | |
| 91 | template<int64_t N> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 92 | class SegmentPublisherFixture : public BaseFixture |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 93 | { |
| 94 | public: |
| 95 | SegmentPublisherFixture() |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 96 | : m_face(std::make_shared<ndn::util::DummyClientFace>(m_ioService, m_keyChain)) |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 97 | , m_expectedFreshnessPeriod(ndn::time::milliseconds(111)) |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 98 | , m_publisher(*m_face, m_keyChain, m_signingInfo, m_expectedFreshnessPeriod) |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 99 | , m_publishingPrefix("/localhost/nfd/SegmentPublisherFixture") |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 100 | { |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | validate(const ndn::Data& data) |
| 105 | { |
| 106 | BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod); |
| 107 | |
| 108 | ndn::Block payload = data.getContent(); |
| 109 | |
| 110 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 111 | |
| 112 | // uint64_t segmentNo = data.getName()[-1].toSegment(); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 113 | if (data.getFinalBlockId() != data.getName()[-1]) { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 114 | return; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 115 | } |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 116 | |
| 117 | // wrap data in a single Content TLV for easy parsing |
| 118 | m_buffer.prependVarNumber(m_buffer.size()); |
| 119 | m_buffer.prependVarNumber(ndn::tlv::Content); |
| 120 | |
| 121 | BOOST_TEST_CHECKPOINT("creating parser"); |
| 122 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 123 | BOOST_TEST_CHECKPOINT("parsing aggregated response"); |
| 124 | parser.parse(); |
| 125 | |
| 126 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit()); |
| 127 | |
| 128 | uint64_t expectedNo = m_publisher.getLimit() - 1; |
| 129 | for (ndn::Block::element_const_iterator i = parser.elements_begin(); |
| 130 | i != parser.elements_end(); |
| 131 | ++i) |
| 132 | { |
| 133 | uint64_t number = readNonNegativeInteger(*i); |
| 134 | BOOST_REQUIRE_EQUAL(number, expectedNo); |
| 135 | --expectedNo; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | protected: |
dmcoomes | 9f93666 | 2017-03-02 10:33:09 -0600 | [diff] [blame] | 140 | std::shared_ptr<ndn::util::DummyClientFace> m_face; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 141 | const ndn::time::milliseconds m_expectedFreshnessPeriod; |
| 142 | TestSegmentPublisher<N> m_publisher; |
| 143 | ndn::EncodingBuffer m_buffer; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame^] | 144 | ndn::security::SigningInfo m_signingInfo; |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 145 | const ndn::Name m_publishingPrefix; |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | using boost::mpl::int_; |
| 149 | typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes; |
| 150 | |
| 151 | BOOST_AUTO_TEST_SUITE(PublisherTestSegmentPublisher) |
| 152 | |
| 153 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>) |
| 154 | { |
Vince Lehman | d6bb3fa | 2015-04-24 14:21:39 -0500 | [diff] [blame] | 155 | this->m_publisher.publish(this->m_publishingPrefix); |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 156 | this->m_face->processEvents(); |
| 157 | |
| 158 | size_t nSegments = this->m_publisher.getTotalPayloadLength() / |
| 159 | this->m_publisher.getMaxSegmentSize(); |
| 160 | if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 || |
| 161 | nSegments == 0) |
| 162 | ++nSegments; |
| 163 | |
Junxiao Shi | c778e81 | 2016-07-14 15:44:26 +0000 | [diff] [blame] | 164 | BOOST_CHECK_EQUAL(this->m_face->sentData.size(), nSegments); |
| 165 | for (const ndn::Data& data : this->m_face->sentData) { |
Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame] | 166 | this->validate(data); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | BOOST_AUTO_TEST_SUITE_END() |
| 171 | |
| 172 | } // namespace tests |
| 173 | } // namespace nlsr |