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" |
| 9 | |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | |
| 12 | namespace nfd { |
| 13 | |
| 14 | BOOST_AUTO_TEST_SUITE(FaceNdnlp) |
| 15 | |
| 16 | BOOST_AUTO_TEST_CASE(SequenceBlock) |
| 17 | { |
| 18 | ndnlp::SequenceBlock sb(0x8000, 2); |
| 19 | BOOST_CHECK_EQUAL(sb.count(), 2); |
| 20 | BOOST_CHECK_EQUAL(sb[0], 0x8000); |
| 21 | BOOST_CHECK_EQUAL(sb[1], 0x8001); |
| 22 | BOOST_CHECK_THROW(sb[2], std::out_of_range); |
| 23 | } |
| 24 | |
| 25 | // sequence number can safely wrap around |
| 26 | BOOST_AUTO_TEST_CASE(SequenceBlockWrap) |
| 27 | { |
| 28 | ndnlp::SequenceBlock sb(std::numeric_limits<uint64_t>::max(), 2); |
| 29 | BOOST_CHECK_EQUAL(sb[0], std::numeric_limits<uint64_t>::max()); |
| 30 | BOOST_CHECK_EQUAL(sb[1], std::numeric_limits<uint64_t>::min()); |
| 31 | BOOST_CHECK_EQUAL(sb[1] - sb[0], 1); |
| 32 | } |
| 33 | |
| 34 | BOOST_AUTO_TEST_CASE(SequenceGenerator) |
| 35 | { |
| 36 | ndnlp::SequenceGenerator seqgen; |
| 37 | |
| 38 | ndnlp::SequenceBlock sb1 = seqgen.nextBlock(2); |
| 39 | BOOST_CHECK_EQUAL(sb1.count(), 2); |
| 40 | |
| 41 | ndnlp::SequenceBlock sb2 = seqgen.nextBlock(1); |
| 42 | BOOST_CHECK_NE(sb1[0], sb2[0]); |
| 43 | BOOST_CHECK_NE(sb1[1], sb2[0]); |
| 44 | } |
| 45 | |
| 46 | // slice a Block to one NDNLP packet |
| 47 | BOOST_AUTO_TEST_CASE(Slice1) |
| 48 | { |
| 49 | uint8_t blockValue[60]; |
| 50 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 51 | Block block = ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
| 52 | |
| 53 | ndnlp::Slicer slicer(9000); |
| 54 | ndnlp::PacketArray pa = slicer.slice(block); |
| 55 | |
| 56 | BOOST_REQUIRE_EQUAL(pa->size(), 1); |
| 57 | |
| 58 | const Block& pkt = pa->at(0); |
| 59 | BOOST_CHECK_EQUAL(pkt.type(), static_cast<uint32_t>(tlv::NdnlpData)); |
| 60 | pkt.parse(); |
| 61 | |
| 62 | const Block::element_container& elements = pkt.elements(); |
| 63 | BOOST_REQUIRE_EQUAL(elements.size(), 2); |
| 64 | |
| 65 | const Block& sequenceElement = elements[0]; |
| 66 | BOOST_CHECK_EQUAL(sequenceElement.type(), static_cast<uint32_t>(tlv::NdnlpSequence)); |
| 67 | BOOST_REQUIRE_EQUAL(sequenceElement.value_size(), sizeof(uint64_t)); |
| 68 | |
| 69 | const Block& payloadElement = elements[1]; |
| 70 | BOOST_CHECK_EQUAL(payloadElement.type(), static_cast<uint32_t>(tlv::NdnlpPayload)); |
| 71 | size_t payloadSize = payloadElement.value_size(); |
| 72 | BOOST_CHECK_EQUAL(payloadSize, block.size()); |
| 73 | |
| 74 | BOOST_CHECK_EQUAL_COLLECTIONS(payloadElement.value_begin(), payloadElement.value_end(), |
| 75 | block.begin(), block.end()); |
| 76 | } |
| 77 | |
| 78 | // slice a Block to four NDNLP packets |
| 79 | BOOST_AUTO_TEST_CASE(SliceData4) |
| 80 | { |
| 81 | uint8_t blockValue[5050]; |
| 82 | memset(blockValue, 0xcc, sizeof(blockValue)); |
| 83 | Block block = ndn::dataBlock(0x01, blockValue, sizeof(blockValue)); |
| 84 | |
| 85 | ndnlp::Slicer slicer(1500); |
| 86 | ndnlp::PacketArray pa = slicer.slice(block); |
| 87 | |
| 88 | BOOST_REQUIRE_EQUAL(pa->size(), 4); |
| 89 | |
| 90 | uint64_t seq0 = 0xdddd; |
| 91 | |
| 92 | size_t totalPayloadSize = 0; |
| 93 | |
| 94 | for (size_t i = 0; i < 4; ++i) { |
| 95 | const Block& pkt = pa->at(i); |
| 96 | BOOST_CHECK_EQUAL(pkt.type(), static_cast<uint32_t>(tlv::NdnlpData)); |
| 97 | pkt.parse(); |
| 98 | |
| 99 | const Block::element_container& elements = pkt.elements(); |
| 100 | BOOST_REQUIRE_EQUAL(elements.size(), 4); |
| 101 | |
| 102 | const Block& sequenceElement = elements[0]; |
| 103 | BOOST_CHECK_EQUAL(sequenceElement.type(), static_cast<uint32_t>(tlv::NdnlpSequence)); |
| 104 | BOOST_REQUIRE_EQUAL(sequenceElement.value_size(), sizeof(uint64_t)); |
| 105 | uint64_t seq = be64toh(*reinterpret_cast<const uint64_t*>( |
| 106 | &*sequenceElement.value_begin())); |
| 107 | if (i == 0) { |
| 108 | seq0 = seq; |
| 109 | } |
| 110 | BOOST_CHECK_EQUAL(seq, seq0 + i); |
| 111 | |
| 112 | const Block& fragIndexElement = elements[1]; |
| 113 | BOOST_CHECK_EQUAL(fragIndexElement.type(), static_cast<uint32_t>(tlv::NdnlpFragIndex)); |
| 114 | BOOST_REQUIRE_EQUAL(fragIndexElement.value_size(), sizeof(uint16_t)); |
| 115 | uint16_t fragIndex = be16toh(*reinterpret_cast<const uint16_t*>( |
| 116 | &*fragIndexElement.value_begin())); |
| 117 | BOOST_CHECK_EQUAL(fragIndex, i); |
| 118 | |
| 119 | const Block& fragCountElement = elements[2]; |
| 120 | BOOST_CHECK_EQUAL(fragCountElement.type(), static_cast<uint32_t>(tlv::NdnlpFragCount)); |
| 121 | BOOST_REQUIRE_EQUAL(fragCountElement.value_size(), sizeof(uint16_t)); |
| 122 | uint16_t fragCount = be16toh(*reinterpret_cast<const uint16_t*>( |
| 123 | &*fragCountElement.value_begin())); |
| 124 | BOOST_CHECK_EQUAL(fragCount, 4); |
| 125 | |
| 126 | const Block& payloadElement = elements[3]; |
| 127 | BOOST_CHECK_EQUAL(payloadElement.type(), static_cast<uint32_t>(tlv::NdnlpPayload)); |
| 128 | size_t payloadSize = payloadElement.value_size(); |
| 129 | totalPayloadSize += payloadSize; |
| 130 | } |
| 131 | |
| 132 | BOOST_CHECK_EQUAL(totalPayloadSize, block.size()); |
| 133 | } |
| 134 | |
| 135 | BOOST_AUTO_TEST_SUITE_END() |
| 136 | |
| 137 | } // namespace nfd |