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