Steve DiBenedetto | 9f6c364 | 2014-03-10 17:02:27 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "mgmt/segment-publisher.hpp" |
| 8 | #include "mgmt/internal-face.hpp" |
| 9 | #include "mgmt/app-face.hpp" |
| 10 | |
| 11 | #include "tests/test-common.hpp" |
| 12 | #include <ndn-cpp-dev/encoding/tlv.hpp> |
| 13 | |
| 14 | namespace nfd { |
| 15 | namespace tests { |
| 16 | |
| 17 | NFD_LOG_INIT("SegmentPublisherTest"); |
| 18 | |
| 19 | class TestSegmentPublisher : public SegmentPublisher |
| 20 | { |
| 21 | public: |
| 22 | TestSegmentPublisher(shared_ptr<AppFace> face, |
| 23 | const Name& prefix, |
| 24 | const uint64_t limit=10000) |
| 25 | : SegmentPublisher(face, prefix) |
| 26 | , m_limit((limit == 0)?(1):(limit)) |
| 27 | { |
| 28 | |
| 29 | } |
| 30 | |
| 31 | virtual |
| 32 | ~TestSegmentPublisher() |
| 33 | { |
| 34 | |
| 35 | } |
| 36 | |
| 37 | uint16_t |
| 38 | getLimit() const |
| 39 | { |
| 40 | return m_limit; |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | |
| 45 | virtual size_t |
| 46 | generate(ndn::EncodingBuffer& outBuffer) |
| 47 | { |
| 48 | size_t totalLength = 0; |
| 49 | for (uint64_t i = 0; i < m_limit; i++) |
| 50 | { |
| 51 | totalLength += prependNonNegativeIntegerBlock(outBuffer, ndn::Tlv::Content, i); |
| 52 | } |
| 53 | return totalLength; |
| 54 | } |
| 55 | |
| 56 | protected: |
| 57 | const uint64_t m_limit; |
| 58 | }; |
| 59 | |
| 60 | class SegmentPublisherFixture : public BaseFixture |
| 61 | { |
| 62 | public: |
| 63 | SegmentPublisherFixture() |
| 64 | : m_face(make_shared<InternalFace>()) |
| 65 | , m_publisher(m_face, "/localhost/nfd/SegmentPublisherFixture") |
| 66 | , m_finished(false) |
| 67 | { |
| 68 | |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | validate(const Data& data) |
| 73 | { |
| 74 | Block payload = data.getContent(); |
| 75 | NFD_LOG_DEBUG("payload size (w/o Content TLV): " << payload.value_size()); |
| 76 | |
| 77 | m_buffer.appendByteArray(payload.value(), payload.value_size()); |
| 78 | |
| 79 | uint64_t segmentNo = data.getName()[-1].toSegment(); |
| 80 | if (data.getFinalBlockId() != data.getName()[-1]) |
| 81 | { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | NFD_LOG_DEBUG("got final block: #" << segmentNo); |
| 86 | |
| 87 | // wrap data in a single Content TLV for easy parsing |
| 88 | m_buffer.prependVarNumber(m_buffer.size()); |
| 89 | m_buffer.prependVarNumber(ndn::Tlv::Content); |
| 90 | |
| 91 | BOOST_TEST_CHECKPOINT("creating parser"); |
| 92 | ndn::Block parser(m_buffer.buf(), m_buffer.size()); |
| 93 | BOOST_TEST_CHECKPOINT("parsing aggregated response"); |
| 94 | parser.parse(); |
| 95 | |
| 96 | BOOST_REQUIRE_EQUAL(parser.elements_size(), m_publisher.getLimit()); |
| 97 | |
| 98 | uint64_t expectedNo = m_publisher.getLimit() - 1; |
| 99 | for (Block::element_const_iterator i = parser.elements_begin(); |
| 100 | i != parser.elements_end(); |
| 101 | ++i) |
| 102 | { |
| 103 | uint64_t number = readNonNegativeInteger(*i); |
| 104 | BOOST_REQUIRE_EQUAL(number, expectedNo); |
| 105 | --expectedNo; |
| 106 | } |
| 107 | m_finished = true; |
| 108 | } |
| 109 | |
| 110 | protected: |
| 111 | shared_ptr<InternalFace> m_face; |
| 112 | TestSegmentPublisher m_publisher; |
| 113 | ndn::EncodingBuffer m_buffer; |
| 114 | |
| 115 | protected: |
| 116 | bool m_finished; |
| 117 | }; |
| 118 | |
| 119 | BOOST_FIXTURE_TEST_SUITE(MgmtSegmentPublisher, SegmentPublisherFixture) |
| 120 | |
| 121 | BOOST_AUTO_TEST_CASE(Generate) |
| 122 | { |
| 123 | m_face->onReceiveData += |
| 124 | bind(&SegmentPublisherFixture::validate, this, _1); |
| 125 | |
| 126 | m_publisher.publish(); |
| 127 | BOOST_REQUIRE(m_finished); |
| 128 | } |
| 129 | |
| 130 | BOOST_AUTO_TEST_SUITE_END() |
| 131 | |
| 132 | } // namespace tests |
| 133 | } // namespace nfd |