blob: 77ac7e9441ddaf741da0df8e62396f366cc5f73b [file] [log] [blame]
Junxiao Shi9b0d3e92014-02-15 12:27:12 -07001/* -*- 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 Shid6dcd2c2014-02-16 14:49:54 -07009#include "face/ndnlp-partial-message-store.hpp"
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070010
Junxiao Shid9ee45c2014-02-27 15:38:11 -070011#include "tests/test-common.hpp"
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070012
13namespace nfd {
Junxiao Shid9ee45c2014-02-27 15:38:11 -070014namespace tests {
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070015
Junxiao Shid9ee45c2014-02-27 15:38:11 -070016BOOST_FIXTURE_TEST_SUITE(FaceNdnlp, BaseFixture)
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070017
18BOOST_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
28BOOST_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
36BOOST_AUTO_TEST_CASE(SequenceGenerator)
37{
38 ndnlp::SequenceGenerator seqgen;
Junxiao Shidf3b4382014-02-23 11:28:21 -070039
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070040 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
49BOOST_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 Shidf3b4382014-02-23 11:28:21 -070054
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070055 ndnlp::Slicer slicer(9000);
56 ndnlp::PacketArray pa = slicer.slice(block);
Junxiao Shidf3b4382014-02-23 11:28:21 -070057
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070058 BOOST_REQUIRE_EQUAL(pa->size(), 1);
Junxiao Shidf3b4382014-02-23 11:28:21 -070059
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070060 const Block& pkt = pa->at(0);
61 BOOST_CHECK_EQUAL(pkt.type(), static_cast<uint32_t>(tlv::NdnlpData));
62 pkt.parse();
Junxiao Shidf3b4382014-02-23 11:28:21 -070063
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070064 const Block::element_container& elements = pkt.elements();
65 BOOST_REQUIRE_EQUAL(elements.size(), 2);
Junxiao Shidf3b4382014-02-23 11:28:21 -070066
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070067 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 Shidf3b4382014-02-23 11:28:21 -070070
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070071 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 Shidf3b4382014-02-23 11:28:21 -070075
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070076 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 Shid6dcd2c2014-02-16 14:49:54 -070081BOOST_AUTO_TEST_CASE(Slice4)
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070082{
83 uint8_t blockValue[5050];
84 memset(blockValue, 0xcc, sizeof(blockValue));
85 Block block = ndn::dataBlock(0x01, blockValue, sizeof(blockValue));
Junxiao Shidf3b4382014-02-23 11:28:21 -070086
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070087 ndnlp::Slicer slicer(1500);
88 ndnlp::PacketArray pa = slicer.slice(block);
Junxiao Shidf3b4382014-02-23 11:28:21 -070089
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070090 BOOST_REQUIRE_EQUAL(pa->size(), 4);
Junxiao Shidf3b4382014-02-23 11:28:21 -070091
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070092 uint64_t seq0 = 0xdddd;
Junxiao Shidf3b4382014-02-23 11:28:21 -070093
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070094 size_t totalPayloadSize = 0;
Junxiao Shidf3b4382014-02-23 11:28:21 -070095
Junxiao Shi9b0d3e92014-02-15 12:27:12 -070096 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 Shidf3b4382014-02-23 11:28:21 -0700100
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700101 const Block::element_container& elements = pkt.elements();
102 BOOST_REQUIRE_EQUAL(elements.size(), 4);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700103
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700104 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 Shidf3b4382014-02-23 11:28:21 -0700113
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700114 const Block& fragIndexElement = elements[1];
115 BOOST_CHECK_EQUAL(fragIndexElement.type(), static_cast<uint32_t>(tlv::NdnlpFragIndex));
Junxiao Shidf3b4382014-02-23 11:28:21 -0700116 uint64_t fragIndex = ndn::readNonNegativeInteger(fragIndexElement);
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700117 BOOST_CHECK_EQUAL(fragIndex, i);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700118
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700119 const Block& fragCountElement = elements[2];
120 BOOST_CHECK_EQUAL(fragCountElement.type(), static_cast<uint32_t>(tlv::NdnlpFragCount));
Junxiao Shidf3b4382014-02-23 11:28:21 -0700121 uint64_t fragCount = ndn::readNonNegativeInteger(fragCountElement);
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700122 BOOST_CHECK_EQUAL(fragCount, 4);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700123
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700124 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 Shidf3b4382014-02-23 11:28:21 -0700129
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700130 BOOST_CHECK_EQUAL(totalPayloadSize, block.size());
131}
132
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700133class ReassembleFixture : protected BaseFixture
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700134{
135protected:
136 ReassembleFixture()
Junxiao Shic0d05912014-02-17 19:06:21 -0700137 : m_scheduler(m_io)
138 , m_slicer(1500)
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700139 , 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 Shidf3b4382014-02-23 11:28:21 -0700152
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700153protected:
154 boost::asio::io_service m_io;
155 Scheduler m_scheduler;
Junxiao Shidf3b4382014-02-23 11:28:21 -0700156
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700157 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
165BOOST_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 Shidf3b4382014-02-23 11:28:21 -0700170
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700171 BOOST_CHECK_EQUAL(m_received.size(), 0);
172 m_partialMessageStore.receiveNdnlpData(pa->at(0));
Junxiao Shidf3b4382014-02-23 11:28:21 -0700173
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700174 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
180BOOST_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 Shidf3b4382014-02-23 11:28:21 -0700185
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700186 Block block2 = makeBlock(2000);
187 ndnlp::PacketArray pa2 = m_slicer.slice(block2);
188 BOOST_REQUIRE_EQUAL(pa2->size(), 2);
Junxiao Shidf3b4382014-02-23 11:28:21 -0700189
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700190 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 Shidf3b4382014-02-23 11:28:21 -0700204
Junxiao Shid6dcd2c2014-02-16 14:49:54 -0700205 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 Shi9b0d3e92014-02-15 12:27:12 -0700212BOOST_AUTO_TEST_SUITE_END()
213
Junxiao Shid9ee45c2014-02-27 15:38:11 -0700214} // namespace tests
Junxiao Shi9b0d3e92014-02-15 12:27:12 -0700215} // namespace nfd