Jiewen Tan | a0497d8 | 2015-02-02 21:59:18 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, 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 "publisher/segment-publisher.hpp" |
| 27 | |
| 28 | #include "../boost-test.hpp" |
| 29 | |
| 30 | #include <ndn-cxx/encoding/tlv.hpp> |
| 31 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 32 | |
| 33 | #include <boost/mpl/int.hpp> |
| 34 | #include <boost/mpl/vector.hpp> |
| 35 | |
| 36 | namespace nlsr { |
| 37 | namespace tests { |
| 38 | |
| 39 | template<int64_t N=10000> |
| 40 | class TestSegmentPublisher : public SegmentPublisher<ndn::util::DummyClientFace> |
| 41 | { |
| 42 | public: |
| 43 | TestSegmentPublisher(ndn::util::DummyClientFace& face, |
| 44 | const ndn::Name& prefix, |
| 45 | ndn::KeyChain& keyChain, |
| 46 | const ndn::time::milliseconds freshnessPeriod) |
| 47 | : SegmentPublisher(face, prefix, keyChain, freshnessPeriod) |
| 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 (int64_t i = 0; i < N; i++) |
| 77 | { |
| 78 | totalLength += ndn::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<int64_t N> |
| 89 | class SegmentPublisherFixture |
| 90 | { |
| 91 | public: |
| 92 | SegmentPublisherFixture() |
| 93 | : m_face(ndn::util::makeDummyClientFace()) |
| 94 | , m_expectedFreshnessPeriod(ndn::time::milliseconds(111)) |
| 95 | , m_publisher(*m_face, "/localhost/nfd/SegmentPublisherFixture", |
| 96 | m_keyChain, m_expectedFreshnessPeriod) |
| 97 | { |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | validate(const ndn::Data& data) |
| 102 | { |
| 103 | BOOST_CHECK_EQUAL(data.getFreshnessPeriod(), m_expectedFreshnessPeriod); |
| 104 | |
| 105 | ndn::Block payload = data.getContent(); |
| 106 | |
| 107 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 108 | |
| 109 | // uint64_t segmentNo = data.getName()[-1].toSegment(); |
| 110 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // wrap data in a single Content TLV for easy parsing |
| 116 | m_buffer.prependVarNumber(m_buffer.size()); |
| 117 | m_buffer.prependVarNumber(ndn::tlv::Content); |
| 118 | |
| 119 | BOOST_TEST_CHECKPOINT("creating parser"); |
| 120 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 121 | BOOST_TEST_CHECKPOINT("parsing aggregated response"); |
| 122 | parser.parse(); |
| 123 | |
| 124 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit()); |
| 125 | |
| 126 | uint64_t expectedNo = m_publisher.getLimit() - 1; |
| 127 | for (ndn::Block::element_const_iterator i = parser.elements_begin(); |
| 128 | i != parser.elements_end(); |
| 129 | ++i) |
| 130 | { |
| 131 | uint64_t number = readNonNegativeInteger(*i); |
| 132 | BOOST_REQUIRE_EQUAL(number, expectedNo); |
| 133 | --expectedNo; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | protected: |
| 138 | ndn::shared_ptr<ndn::util::DummyClientFace> m_face; |
| 139 | const ndn::time::milliseconds m_expectedFreshnessPeriod; |
| 140 | TestSegmentPublisher<N> m_publisher; |
| 141 | ndn::EncodingBuffer m_buffer; |
| 142 | ndn::KeyChain m_keyChain; |
| 143 | }; |
| 144 | |
| 145 | using boost::mpl::int_; |
| 146 | typedef boost::mpl::vector<int_<10000>, int_<100>, int_<10>, int_<0> > DatasetSizes; |
| 147 | |
| 148 | BOOST_AUTO_TEST_SUITE(PublisherTestSegmentPublisher) |
| 149 | |
| 150 | BOOST_FIXTURE_TEST_CASE_TEMPLATE(Generate, T, DatasetSizes, SegmentPublisherFixture<T::value>) |
| 151 | { |
| 152 | this->m_publisher.publish(); |
| 153 | this->m_face->processEvents(); |
| 154 | |
| 155 | size_t nSegments = this->m_publisher.getTotalPayloadLength() / |
| 156 | this->m_publisher.getMaxSegmentSize(); |
| 157 | if (this->m_publisher.getTotalPayloadLength() % this->m_publisher.getMaxSegmentSize() != 0 || |
| 158 | nSegments == 0) |
| 159 | ++nSegments; |
| 160 | |
| 161 | BOOST_CHECK_EQUAL(this->m_face->sentDatas.size(), nSegments); |
| 162 | for (const ndn::Data& data : this->m_face->sentDatas) { |
| 163 | this->validate(data); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | BOOST_AUTO_TEST_SUITE_END() |
| 168 | |
| 169 | } // namespace tests |
| 170 | } // namespace nlsr |