blob: 79294872993ab51daf9a5db2f4c4c8cba155fe94 [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 Newberry261dbc22015-07-22 23:18:18 -0700128 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
Eric Newberry8422f572017-02-04 21:53:58 -0700129 BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
130 BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
Eric Newberry261dbc22015-07-22 23:18:18 -0700131 Block wire;
132 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
133 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
134 wire.begin(), wire.end());
135}
136
137BOOST_AUTO_TEST_CASE(DecodeNormal)
138{
139 static const uint8_t inputBlock[] = {
140 0x64, 0x0a, // LpPacket
141 0x52, 0x01, // FragIndex
142 0x00,
143 0x53, 0x01, // FragCount
144 0x01,
145 0x50, 0x02, // Fragment
146 0x03, 0xe8,
147 };
148
149 Packet packet;
150 Block wire(inputBlock, sizeof(inputBlock));
151 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
152 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
153 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
154 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
155 Buffer::const_iterator first, last;
156 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
157 BOOST_CHECK_EQUAL(2, last - first);
158 BOOST_CHECK_EQUAL(0x03, *first);
159 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
160 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
161 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
162}
163
164BOOST_AUTO_TEST_CASE(DecodeIdle)
165{
166 static const uint8_t inputBlock[] = {
167 0x64, 0x06, // LpPacket
168 0x52, 0x01, // FragIndex
169 0x00,
170 0x53, 0x01, // FragCount
171 0x01,
172 };
173
174 Packet packet;
175 Block wire(inputBlock, sizeof(inputBlock));
176 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
177 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
178 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
179 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
180 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
181 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
182}
183
184BOOST_AUTO_TEST_CASE(DecodeFragment)
185{
186 static const uint8_t inputBlock[] = {
187 0x64, 0x04, // LpPacket
188 0x50, 0x02, // Fragment
189 0x03, 0xe8,
190 };
191
192 Packet packet;
193 Block wire(inputBlock, sizeof(inputBlock));
194 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
195 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
196 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
197 Buffer::const_iterator first, last;
198 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
199 BOOST_CHECK_EQUAL(2, last - first);
200 BOOST_CHECK_EQUAL(0x03, *first);
201 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
202}
203
204BOOST_AUTO_TEST_CASE(DecodeEmpty)
205{
206 static const uint8_t inputBlock[] = {
207 0x64, 0x00, // LpPacket
208 };
209
210 Packet packet;
211 Block wire(inputBlock, sizeof(inputBlock));
212 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
213 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
214 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
215}
216
217BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
218{
219 static const uint8_t inputBlock[] = {
220 0x64, 0x06, // LpPacket
221 0x52, 0x01, // FragIndex
222 0x00,
223 0x52, 0x01, // FragIndex
224 0x01,
225 };
226
227 Packet packet;
228 Block wire(inputBlock, sizeof(inputBlock));
229 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
230}
231
Eric Newberry8422f572017-02-04 21:53:58 -0700232BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader)
233{
234 static const uint8_t inputBlock[] = {
235 0x64, 0x0f, // LpPacket
236 0xfd, 0x03, 0x44, 0x01, // Ack
237 0x01,
238 0xfd, 0x03, 0x44, 0x01, // Ack
239 0x03,
240 0xfd, 0x03, 0x44, 0x01, // Ack
241 0x02,
242 };
243
244 Packet packet;
245 Block wire(inputBlock, sizeof(inputBlock));
246 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
247 BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3);
248 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
249 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
250 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
251 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
252}
253
Eric Newberry261dbc22015-07-22 23:18:18 -0700254BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
255{
256 static const uint8_t inputBlock[] = {
257 0x64, 0x08, // LpPacket
258 0x50, 0x02, // Fragment
259 0x03, 0xe8,
260 0x50, 0x02, // Fragment
261 0x03, 0xe9,
262 };
263
264 Packet packet;
265 Block wire(inputBlock, sizeof(inputBlock));
266 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
267}
268
269BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
270{
271 static const uint8_t inputBlock[] = {
272 0x64, 0x0a, // LpPacket
273 0x53, 0x01, // FragCount
274 0x01,
275 0x52, 0x01, // FragIndex
276 0x00,
277 0x50, 0x02, // Fragment
278 0x03, 0xe8,
279 };
280
281 Packet packet;
282 Block wire(inputBlock, sizeof(inputBlock));
283 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
284}
285
286BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
287{
288 static const uint8_t inputBlock[] = {
289 0x64, 0x0a, // LpPacket
290 0x52, 0x01, // FragIndex
291 0x00,
292 0x50, 0x02, // Fragment
293 0x03, 0xe8,
294 0x53, 0x01, // FragCount
295 0x01,
296 };
297
298 Packet packet;
299 Block wire(inputBlock, sizeof(inputBlock));
300 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
301}
302
303BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
304{
305 static const uint8_t inputBlock[] = {
306 0x64, 0x0c, // LpPacket
307 0x52, 0x01, // FragIndex
308 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700309 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700310 0x02,
311 0x50, 0x02, // Fragment
312 0x03, 0xe8,
313 };
314
315 Packet packet;
316 Block wire(inputBlock, sizeof(inputBlock));
317 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
318 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
319 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
320}
321
322BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
323{
324 static const uint8_t inputBlock[] = {
325 0x64, 0x0c, // LpPacket
326 0x52, 0x01, // FragIndex
327 0x00,
328 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
329 0x02,
330 0x50, 0x02, // Fragment
331 0x03, 0xe8,
332 };
333
334 Packet packet;
335 Block wire(inputBlock, sizeof(inputBlock));
336 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
337}
338
339BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
340{
341 static const uint8_t inputBlock[] = {
342 0x05, 0x0a, // Interest
343 0x07, 0x02, // Name
344 0x03, 0xe8,
345 0x0a, 0x04, // Nonce
346 0x01, 0x02, 0x03, 0x04,
347 };
348
349 Packet packet;
350 Block wire(inputBlock, sizeof(inputBlock));
351 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
352 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
353
Eric Newberry261dbc22015-07-22 23:18:18 -0700354 Block encoded;
355 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700356 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700357 encoded.begin(), encoded.end());
358}
359
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700360BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
361{
362 Packet packet;
363 Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
364 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
365}
366
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100367BOOST_AUTO_TEST_SUITE_END() // TestPacket
368BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700369
370} // namespace tests
371} // namespace lp
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100372} // namespace ndn