Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [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 "face/ndnlp-sequence-generator.hpp" |
| 8 | #include "face/ndnlp-slicer.hpp" |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 9 | #include "face/ndnlp-partial-message-store.hpp" |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 10 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 11 | #include "tests/test-common.hpp" |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 12 | |
| 13 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 14 | namespace tests { |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 15 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 16 | BOOST_FIXTURE_TEST_SUITE(FaceNdnlp, BaseFixture) |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 17 | |
| 18 | BOOST_AUTO_TEST_CASE(SequenceBlock) |
| 19 | { |
| 20 | ndnlp::SequenceBlock sb(0x8000, 2); |
| 21 | BOOST_CHECK_EQUAL(sb.count(), 2); |
| 22 | BOOST_CHECK_EQUAL(sb[0], 0x8000); |
| 23 | BOOST_CHECK_EQUAL(sb[1], 0x8001); |
| 24 | BOOST_CHECK_THROW(sb[2], std::out_of_range); |
| 25 | } |
| 26 | |
| 27 | // sequence number can safely wrap around |
| 28 | BOOST_AUTO_TEST_CASE(SequenceBlockWrap) |
| 29 | { |
| 30 | ndnlp::SequenceBlock sb(std::numeric_limits<uint64_t>::max(), 2); |
| 31 | BOOST_CHECK_EQUAL(sb[0], std::numeric_limits<uint64_t>::max()); |
| 32 | BOOST_CHECK_EQUAL(sb[1], std::numeric_limits<uint64_t>::min()); |
| 33 | BOOST_CHECK_EQUAL(sb[1] - sb[0], 1); |
| 34 | } |
| 35 | |
| 36 | BOOST_AUTO_TEST_CASE(SequenceGenerator) |
| 37 | { |
| 38 | ndnlp::SequenceGenerator seqgen; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 39 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 40 | ndnlp::SequenceBlock sb1 = seqgen.nextBlock(2); |
| 41 | BOOST_CHECK_EQUAL(sb1.count(), 2); |
| 42 | |
| 43 | ndnlp::SequenceBlock sb2 = seqgen.nextBlock(1); |
| 44 | BOOST_CHECK_NE(sb1[0], sb2[0]); |
| 45 | BOOST_CHECK_NE(sb1[1], sb2[0]); |
| 46 | } |
| 47 | |
| 48 | // slice a Block to one NDNLP packet |
| 49 | BOOST_AUTO_TEST_CASE(Slice1) |
| 50 | { |
| 51 | uint8_t blockValue[60]; |
| 52 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 53 | Block block = ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 54 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 55 | ndnlp::Slicer slicer(9000); |
| 56 | ndnlp::PacketArray pa = slicer.slice(block); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 57 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 58 | BOOST_REQUIRE_EQUAL(pa->size(), 1); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 59 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 60 | const Block& pkt = pa->at(0); |
| 61 | BOOST_CHECK_EQUAL(pkt.type(), static_cast<uint32_t>(tlv::NdnlpData)); |
| 62 | pkt.parse(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 63 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 64 | const Block::element_container& elements = pkt.elements(); |
| 65 | BOOST_REQUIRE_EQUAL(elements.size(), 2); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 66 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 67 | const Block& sequenceElement = elements[0]; |
| 68 | BOOST_CHECK_EQUAL(sequenceElement.type(), static_cast<uint32_t>(tlv::NdnlpSequence)); |
| 69 | BOOST_REQUIRE_EQUAL(sequenceElement.value_size(), sizeof(uint64_t)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 70 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 71 | const Block& payloadElement = elements[1]; |
| 72 | BOOST_CHECK_EQUAL(payloadElement.type(), static_cast<uint32_t>(tlv::NdnlpPayload)); |
| 73 | size_t payloadSize = payloadElement.value_size(); |
| 74 | BOOST_CHECK_EQUAL(payloadSize, block.size()); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 75 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 76 | BOOST_CHECK_EQUAL_COLLECTIONS(payloadElement.value_begin(), payloadElement.value_end(), |
| 77 | block.begin(), block.end()); |
| 78 | } |
| 79 | |
| 80 | // slice a Block to four NDNLP packets |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 81 | BOOST_AUTO_TEST_CASE(Slice4) |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 82 | { |
| 83 | uint8_t blockValue[5050]; |
| 84 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 85 | Block block = ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 86 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 87 | ndnlp::Slicer slicer(1500); |
| 88 | ndnlp::PacketArray pa = slicer.slice(block); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 89 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 90 | BOOST_REQUIRE_EQUAL(pa->size(), 4); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 91 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 92 | uint64_t seq0 = 0xdddd; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 93 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 94 | size_t totalPayloadSize = 0; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 95 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 96 | for (size_t i = 0; i < 4; ++i) { |
| 97 | const Block& pkt = pa->at(i); |
| 98 | BOOST_CHECK_EQUAL(pkt.type(), static_cast<uint32_t>(tlv::NdnlpData)); |
| 99 | pkt.parse(); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 100 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 101 | const Block::element_container& elements = pkt.elements(); |
| 102 | BOOST_REQUIRE_EQUAL(elements.size(), 4); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 103 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 104 | const Block& sequenceElement = elements[0]; |
| 105 | BOOST_CHECK_EQUAL(sequenceElement.type(), static_cast<uint32_t>(tlv::NdnlpSequence)); |
| 106 | BOOST_REQUIRE_EQUAL(sequenceElement.value_size(), sizeof(uint64_t)); |
| 107 | uint64_t seq = be64toh(*reinterpret_cast<const uint64_t*>( |
| 108 | &*sequenceElement.value_begin())); |
| 109 | if (i == 0) { |
| 110 | seq0 = seq; |
| 111 | } |
| 112 | BOOST_CHECK_EQUAL(seq, seq0 + i); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 113 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 114 | const Block& fragIndexElement = elements[1]; |
| 115 | BOOST_CHECK_EQUAL(fragIndexElement.type(), static_cast<uint32_t>(tlv::NdnlpFragIndex)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 116 | uint64_t fragIndex = ndn::readNonNegativeInteger(fragIndexElement); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 117 | BOOST_CHECK_EQUAL(fragIndex, i); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 118 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 119 | const Block& fragCountElement = elements[2]; |
| 120 | BOOST_CHECK_EQUAL(fragCountElement.type(), static_cast<uint32_t>(tlv::NdnlpFragCount)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 121 | uint64_t fragCount = ndn::readNonNegativeInteger(fragCountElement); |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 122 | BOOST_CHECK_EQUAL(fragCount, 4); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 124 | const Block& payloadElement = elements[3]; |
| 125 | BOOST_CHECK_EQUAL(payloadElement.type(), static_cast<uint32_t>(tlv::NdnlpPayload)); |
| 126 | size_t payloadSize = payloadElement.value_size(); |
| 127 | totalPayloadSize += payloadSize; |
| 128 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 129 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(totalPayloadSize, block.size()); |
| 131 | } |
| 132 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 133 | class ReassembleFixture : protected BaseFixture |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 134 | { |
| 135 | protected: |
| 136 | ReassembleFixture() |
Junxiao Shi | c0d0591 | 2014-02-17 19:06:21 -0700 | [diff] [blame] | 137 | : m_scheduler(m_io) |
| 138 | , m_slicer(1500) |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 139 | , m_partialMessageStore(m_scheduler) |
| 140 | { |
| 141 | m_partialMessageStore.onReceive += bind(&std::vector<Block>::push_back, |
| 142 | &m_received, _1); |
| 143 | } |
| 144 | |
| 145 | Block |
| 146 | makeBlock(size_t valueLength) |
| 147 | { |
| 148 | uint8_t blockValue[valueLength]; |
| 149 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 150 | return ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
| 151 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 152 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 153 | protected: |
| 154 | boost::asio::io_service m_io; |
| 155 | Scheduler m_scheduler; |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 156 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 157 | ndnlp::Slicer m_slicer; |
| 158 | ndnlp::PartialMessageStore m_partialMessageStore; |
| 159 | |
| 160 | // received network layer packets |
| 161 | std::vector<Block> m_received; |
| 162 | }; |
| 163 | |
| 164 | // reassemble one NDNLP packets into one Block |
| 165 | BOOST_FIXTURE_TEST_CASE(Reassemble1, ReassembleFixture) |
| 166 | { |
| 167 | Block block = makeBlock(60); |
| 168 | ndnlp::PacketArray pa = m_slicer.slice(block); |
| 169 | BOOST_REQUIRE_EQUAL(pa->size(), 1); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 170 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 171 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 172 | m_partialMessageStore.receiveNdnlpData(pa->at(0)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 173 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 174 | BOOST_REQUIRE_EQUAL(m_received.size(), 1); |
| 175 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), |
| 176 | block.begin(), block.end()); |
| 177 | } |
| 178 | |
| 179 | // reassemble four and two NDNLP packets into two Blocks |
| 180 | BOOST_FIXTURE_TEST_CASE(Reassemble4and2, ReassembleFixture) |
| 181 | { |
| 182 | Block block = makeBlock(5050); |
| 183 | ndnlp::PacketArray pa = m_slicer.slice(block); |
| 184 | BOOST_REQUIRE_EQUAL(pa->size(), 4); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 185 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 186 | Block block2 = makeBlock(2000); |
| 187 | ndnlp::PacketArray pa2 = m_slicer.slice(block2); |
| 188 | BOOST_REQUIRE_EQUAL(pa2->size(), 2); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 189 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 190 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 191 | m_partialMessageStore.receiveNdnlpData(pa->at(0)); |
| 192 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 193 | m_partialMessageStore.receiveNdnlpData(pa->at(1)); |
| 194 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 195 | m_partialMessageStore.receiveNdnlpData(pa2->at(1)); |
| 196 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 197 | m_partialMessageStore.receiveNdnlpData(pa->at(1)); |
| 198 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 199 | m_partialMessageStore.receiveNdnlpData(pa2->at(0)); |
| 200 | BOOST_CHECK_EQUAL(m_received.size(), 1); |
| 201 | m_partialMessageStore.receiveNdnlpData(pa->at(3)); |
| 202 | BOOST_CHECK_EQUAL(m_received.size(), 1); |
| 203 | m_partialMessageStore.receiveNdnlpData(pa->at(2)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 204 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 205 | BOOST_REQUIRE_EQUAL(m_received.size(), 2); |
| 206 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(1).begin(), m_received.at(1).end(), |
| 207 | block.begin(), block.end()); |
| 208 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), |
| 209 | block2.begin(), block2.end()); |
| 210 | } |
| 211 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 212 | BOOST_AUTO_TEST_SUITE_END() |
| 213 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 214 | } // namespace tests |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 215 | } // namespace nfd |