Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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 | * |
| 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 DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 24 | |
| 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 Afanasyev | 4a77136 | 2014-04-24 21:29:33 -0700 | [diff] [blame] | 30 | #include <ndn-cxx/encoding/tlv.hpp> |
Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 34 | |
| 35 | NFD_LOG_INIT("SegmentPublisherTest"); |
| 36 | |
| 37 | class TestSegmentPublisher : public SegmentPublisher |
| 38 | { |
| 39 | public: |
| 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 | |
| 61 | protected: |
| 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 | |
| 74 | protected: |
| 75 | const uint64_t m_limit; |
| 76 | }; |
| 77 | |
| 78 | class SegmentPublisherFixture : public BaseFixture |
| 79 | { |
| 80 | public: |
| 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 | |
| 128 | protected: |
| 129 | shared_ptr<InternalFace> m_face; |
| 130 | TestSegmentPublisher m_publisher; |
| 131 | ndn::EncodingBuffer m_buffer; |
| 132 | |
| 133 | protected: |
| 134 | bool m_finished; |
| 135 | }; |
| 136 | |
| 137 | BOOST_FIXTURE_TEST_SUITE(MgmtSegmentPublisher, SegmentPublisherFixture) |
| 138 | |
| 139 | BOOST_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 | |
| 148 | BOOST_AUTO_TEST_SUITE_END() |
| 149 | |
| 150 | } // namespace tests |
| 151 | } // namespace nfd |