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 | 98e29f4 | 2014-03-31 10:27:26 -0700 | [diff] [blame^] | 137 | : m_slicer(1500) |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 138 | { |
| 139 | m_partialMessageStore.onReceive += bind(&std::vector<Block>::push_back, |
| 140 | &m_received, _1); |
| 141 | } |
| 142 | |
| 143 | Block |
| 144 | makeBlock(size_t valueLength) |
| 145 | { |
| 146 | uint8_t blockValue[valueLength]; |
| 147 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 148 | return ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
| 149 | } |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 150 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 151 | protected: |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 152 | ndnlp::Slicer m_slicer; |
| 153 | ndnlp::PartialMessageStore m_partialMessageStore; |
| 154 | |
| 155 | // received network layer packets |
| 156 | std::vector<Block> m_received; |
| 157 | }; |
| 158 | |
| 159 | // reassemble one NDNLP packets into one Block |
| 160 | BOOST_FIXTURE_TEST_CASE(Reassemble1, ReassembleFixture) |
| 161 | { |
| 162 | Block block = makeBlock(60); |
| 163 | ndnlp::PacketArray pa = m_slicer.slice(block); |
| 164 | BOOST_REQUIRE_EQUAL(pa->size(), 1); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 165 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 166 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 167 | m_partialMessageStore.receiveNdnlpData(pa->at(0)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 168 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 169 | BOOST_REQUIRE_EQUAL(m_received.size(), 1); |
| 170 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), |
| 171 | block.begin(), block.end()); |
| 172 | } |
| 173 | |
| 174 | // reassemble four and two NDNLP packets into two Blocks |
| 175 | BOOST_FIXTURE_TEST_CASE(Reassemble4and2, ReassembleFixture) |
| 176 | { |
| 177 | Block block = makeBlock(5050); |
| 178 | ndnlp::PacketArray pa = m_slicer.slice(block); |
| 179 | BOOST_REQUIRE_EQUAL(pa->size(), 4); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 180 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 181 | Block block2 = makeBlock(2000); |
| 182 | ndnlp::PacketArray pa2 = m_slicer.slice(block2); |
| 183 | BOOST_REQUIRE_EQUAL(pa2->size(), 2); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 184 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 185 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 186 | m_partialMessageStore.receiveNdnlpData(pa->at(0)); |
| 187 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 188 | m_partialMessageStore.receiveNdnlpData(pa->at(1)); |
| 189 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 190 | m_partialMessageStore.receiveNdnlpData(pa2->at(1)); |
| 191 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 192 | m_partialMessageStore.receiveNdnlpData(pa->at(1)); |
| 193 | BOOST_CHECK_EQUAL(m_received.size(), 0); |
| 194 | m_partialMessageStore.receiveNdnlpData(pa2->at(0)); |
| 195 | BOOST_CHECK_EQUAL(m_received.size(), 1); |
| 196 | m_partialMessageStore.receiveNdnlpData(pa->at(3)); |
| 197 | BOOST_CHECK_EQUAL(m_received.size(), 1); |
| 198 | m_partialMessageStore.receiveNdnlpData(pa->at(2)); |
Junxiao Shi | df3b438 | 2014-02-23 11:28:21 -0700 | [diff] [blame] | 199 | |
Junxiao Shi | d6dcd2c | 2014-02-16 14:49:54 -0700 | [diff] [blame] | 200 | BOOST_REQUIRE_EQUAL(m_received.size(), 2); |
| 201 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(1).begin(), m_received.at(1).end(), |
| 202 | block.begin(), block.end()); |
| 203 | BOOST_CHECK_EQUAL_COLLECTIONS(m_received.at(0).begin(), m_received.at(0).end(), |
| 204 | block2.begin(), block2.end()); |
| 205 | } |
| 206 | |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 207 | BOOST_AUTO_TEST_SUITE_END() |
| 208 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 209 | } // namespace tests |
Junxiao Shi | 9b0d3e9 | 2014-02-15 12:27:12 -0700 | [diff] [blame] | 210 | } // namespace nfd |