blob: c173ed2b465725c0c5f411e1ca468254c1be455e [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry8422f572017-02-04 21:53:58 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Eric Newberry261dbc22015-07-22 23:18:18 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "lp/packet.hpp"
23
24#include "boost-test.hpp"
25
26namespace ndn {
27namespace lp {
28namespace tests {
29
Davide Pesaventoeee3e822016-11-26 19:19:34 +010030BOOST_AUTO_TEST_SUITE(Lp)
31BOOST_AUTO_TEST_SUITE(TestPacket)
Eric Newberry261dbc22015-07-22 23:18:18 -070032
33BOOST_AUTO_TEST_CASE(FieldAccess)
34{
35 Packet packet;
36
37 BOOST_CHECK(!packet.has<FragIndexField>());
38 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
39 BOOST_CHECK_NO_THROW(packet.set<FragIndexField>(1234));
40 BOOST_CHECK(packet.has<FragIndexField>());
41 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error);
42 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
43 BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0));
44 BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range);
45 BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range);
46 BOOST_CHECK_NO_THROW(packet.remove<FragIndexField>(0));
47 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
48 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(832));
49 std::vector<uint64_t> fragIndexes;
50 BOOST_REQUIRE_NO_THROW(fragIndexes = packet.list<FragIndexField>());
51 BOOST_CHECK_EQUAL(1, fragIndexes.size());
52 BOOST_CHECK_EQUAL(832, fragIndexes.at(0));
53 BOOST_CHECK_NO_THROW(packet.clear<FragIndexField>());
54 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
55}
56
57/// \todo test field access methods with a REPEATABLE field
58
59BOOST_AUTO_TEST_CASE(EncodeFragment)
60{
61 static const uint8_t expectedBlock[] = {
Eric Newberry83872fd2015-08-06 17:01:24 -070062 0x64, 0x08, // LpPacket
63 0x51, 0x02, // Sequence
64 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070065 0x50, 0x02, // Fragment
66 0x03, 0xe8,
67 };
68
69 Buffer buf(2);
70 buf[0] = 0x03;
71 buf[1] = 0xe8;
72
73 Packet packet;
74 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end())));
Eric Newberry83872fd2015-08-06 17:01:24 -070075 BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000));
Eric Newberry261dbc22015-07-22 23:18:18 -070076 Block wire;
Eric Newberry83872fd2015-08-06 17:01:24 -070077 BOOST_CHECK_NO_THROW(wire = packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -070078 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
79 wire.begin(), wire.end());
80}
81
82BOOST_AUTO_TEST_CASE(EncodeSubTlv)
83{
84 static const uint8_t expectedBlock[] = {
85 0x64, 0x09, // LpPacket
86 0xfd, 0x03, 0x20, 0x05, // Nack
87 0xfd, 0x03, 0x21, 0x01, // NackReason
88 0x64,
89 };
90
91 NackHeader nack;
92 nack.setReason(NackReason::DUPLICATE);
93
94 Packet packet;
95 BOOST_CHECK_NO_THROW(packet.add<NackField>(nack));
96 Block wire;
97 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
98 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
99 wire.begin(), wire.end());
100}
101
102BOOST_AUTO_TEST_CASE(EncodeSortOrder)
103{
104 static const uint8_t expectedBlock[] = {
Eric Newberry8422f572017-02-04 21:53:58 -0700105 0x64, 0x19, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700106 0x52, 0x01, // FragIndex
107 0x00,
108 0x53, 0x01, // FragCount
109 0x01,
Eric Newberry8422f572017-02-04 21:53:58 -0700110 0xfd, 0x03, 0x44, 0x01, // Ack
111 0x02,
112 0xfd, 0x03, 0x44, 0x01, // Ack
113 0x04,
114 0xfd, 0x03, 0x44, 0x01, // Ack
115 0x03,
Eric Newberry261dbc22015-07-22 23:18:18 -0700116 0x50, 0x02, // Fragment
117 0x03, 0xe8,
118 };
119
120 Buffer frag(2);
121 frag[0] = 0x03;
122 frag[1] = 0xe8;
123
124 Packet packet;
125 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
126 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
Eric Newberry8422f572017-02-04 21:53:58 -0700127 BOOST_CHECK_NO_THROW(packet.add<AckField>(2));
Eric Newberry2a890772017-06-26 12:06:15 -0700128 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700129 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
Eric Newberry2a890772017-06-26 12:06:15 -0700130 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700131 BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
Eric Newberry2a890772017-06-26 12:06:15 -0700132 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700133 BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
Eric Newberry2a890772017-06-26 12:06:15 -0700134 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700135 Block wire;
136 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
137 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
138 wire.begin(), wire.end());
139}
140
141BOOST_AUTO_TEST_CASE(DecodeNormal)
142{
143 static const uint8_t inputBlock[] = {
144 0x64, 0x0a, // LpPacket
145 0x52, 0x01, // FragIndex
146 0x00,
147 0x53, 0x01, // FragCount
148 0x01,
149 0x50, 0x02, // Fragment
150 0x03, 0xe8,
151 };
152
153 Packet packet;
154 Block wire(inputBlock, sizeof(inputBlock));
155 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
156 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
157 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
158 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
159 Buffer::const_iterator first, last;
160 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
161 BOOST_CHECK_EQUAL(2, last - first);
162 BOOST_CHECK_EQUAL(0x03, *first);
163 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
164 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
165 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
166}
167
168BOOST_AUTO_TEST_CASE(DecodeIdle)
169{
170 static const uint8_t inputBlock[] = {
171 0x64, 0x06, // LpPacket
172 0x52, 0x01, // FragIndex
173 0x00,
174 0x53, 0x01, // FragCount
175 0x01,
176 };
177
178 Packet packet;
179 Block wire(inputBlock, sizeof(inputBlock));
180 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
181 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
182 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
183 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
184 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
185 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
186}
187
188BOOST_AUTO_TEST_CASE(DecodeFragment)
189{
190 static const uint8_t inputBlock[] = {
191 0x64, 0x04, // LpPacket
192 0x50, 0x02, // Fragment
193 0x03, 0xe8,
194 };
195
196 Packet packet;
197 Block wire(inputBlock, sizeof(inputBlock));
198 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
199 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
200 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
201 Buffer::const_iterator first, last;
202 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
203 BOOST_CHECK_EQUAL(2, last - first);
204 BOOST_CHECK_EQUAL(0x03, *first);
205 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
206}
207
208BOOST_AUTO_TEST_CASE(DecodeEmpty)
209{
210 static const uint8_t inputBlock[] = {
211 0x64, 0x00, // LpPacket
212 };
213
214 Packet packet;
215 Block wire(inputBlock, sizeof(inputBlock));
216 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
217 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
218 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
219}
220
221BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
222{
223 static const uint8_t inputBlock[] = {
224 0x64, 0x06, // LpPacket
225 0x52, 0x01, // FragIndex
226 0x00,
227 0x52, 0x01, // FragIndex
228 0x01,
229 };
230
231 Packet packet;
232 Block wire(inputBlock, sizeof(inputBlock));
233 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
234}
235
Eric Newberry8422f572017-02-04 21:53:58 -0700236BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader)
237{
238 static const uint8_t inputBlock[] = {
239 0x64, 0x0f, // LpPacket
240 0xfd, 0x03, 0x44, 0x01, // Ack
241 0x01,
242 0xfd, 0x03, 0x44, 0x01, // Ack
243 0x03,
244 0xfd, 0x03, 0x44, 0x01, // Ack
245 0x02,
246 };
247
248 Packet packet;
249 Block wire(inputBlock, sizeof(inputBlock));
250 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
251 BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3);
252 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
253 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
254 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
255 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
256}
257
Eric Newberry261dbc22015-07-22 23:18:18 -0700258BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
259{
260 static const uint8_t inputBlock[] = {
261 0x64, 0x08, // LpPacket
262 0x50, 0x02, // Fragment
263 0x03, 0xe8,
264 0x50, 0x02, // Fragment
265 0x03, 0xe9,
266 };
267
268 Packet packet;
269 Block wire(inputBlock, sizeof(inputBlock));
270 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
271}
272
273BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
274{
275 static const uint8_t inputBlock[] = {
276 0x64, 0x0a, // LpPacket
277 0x53, 0x01, // FragCount
278 0x01,
279 0x52, 0x01, // FragIndex
280 0x00,
281 0x50, 0x02, // Fragment
282 0x03, 0xe8,
283 };
284
285 Packet packet;
286 Block wire(inputBlock, sizeof(inputBlock));
287 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
288}
289
290BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
291{
292 static const uint8_t inputBlock[] = {
293 0x64, 0x0a, // LpPacket
294 0x52, 0x01, // FragIndex
295 0x00,
296 0x50, 0x02, // Fragment
297 0x03, 0xe8,
298 0x53, 0x01, // FragCount
299 0x01,
300 };
301
302 Packet packet;
303 Block wire(inputBlock, sizeof(inputBlock));
304 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
305}
306
307BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
308{
309 static const uint8_t inputBlock[] = {
310 0x64, 0x0c, // LpPacket
311 0x52, 0x01, // FragIndex
312 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700313 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700314 0x02,
315 0x50, 0x02, // Fragment
316 0x03, 0xe8,
317 };
318
319 Packet packet;
320 Block wire(inputBlock, sizeof(inputBlock));
321 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
322 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
323 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
324}
325
326BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
327{
328 static const uint8_t inputBlock[] = {
329 0x64, 0x0c, // LpPacket
330 0x52, 0x01, // FragIndex
331 0x00,
332 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
333 0x02,
334 0x50, 0x02, // Fragment
335 0x03, 0xe8,
336 };
337
338 Packet packet;
339 Block wire(inputBlock, sizeof(inputBlock));
340 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
341}
342
343BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
344{
345 static const uint8_t inputBlock[] = {
346 0x05, 0x0a, // Interest
347 0x07, 0x02, // Name
348 0x03, 0xe8,
349 0x0a, 0x04, // Nonce
350 0x01, 0x02, 0x03, 0x04,
351 };
352
353 Packet packet;
354 Block wire(inputBlock, sizeof(inputBlock));
355 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
356 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
357
Eric Newberry261dbc22015-07-22 23:18:18 -0700358 Block encoded;
359 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700360 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700361 encoded.begin(), encoded.end());
362}
363
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700364BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
365{
366 Packet packet;
367 Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
368 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
369}
370
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100371BOOST_AUTO_TEST_SUITE_END() // TestPacket
372BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700373
374} // namespace tests
375} // namespace lp
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100376} // namespace ndn