blob: 5dafc20624858157bbf2ad241a838f95e54fc456 [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib6e276f2017-08-14 20:10:04 +00002/*
Teng Liang5b323d12018-01-31 18:50:45 -07003 * Copyright (c) 2013-2018 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"
Teng Liang5b323d12018-01-31 18:50:45 -070023#include "security/signature-sha256-with-rsa.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070024
25#include "boost-test.hpp"
26
27namespace ndn {
28namespace lp {
29namespace tests {
30
Davide Pesaventoeee3e822016-11-26 19:19:34 +010031BOOST_AUTO_TEST_SUITE(Lp)
32BOOST_AUTO_TEST_SUITE(TestPacket)
Eric Newberry261dbc22015-07-22 23:18:18 -070033
34BOOST_AUTO_TEST_CASE(FieldAccess)
35{
36 Packet packet;
37
Junxiao Shib6e276f2017-08-14 20:10:04 +000038 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070039 BOOST_CHECK(!packet.has<FragIndexField>());
40 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000041
42 packet.set<FragIndexField>(1234);
43 BOOST_CHECK(!packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070044 BOOST_CHECK(packet.has<FragIndexField>());
45 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error);
46 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
47 BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0));
48 BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range);
49 BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range);
Junxiao Shib6e276f2017-08-14 20:10:04 +000050
51 packet.remove<FragIndexField>(0);
Eric Newberry261dbc22015-07-22 23:18:18 -070052 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000053
54 packet.add<FragIndexField>(832);
55 std::vector<uint64_t> fragIndexes = packet.list<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070056 BOOST_CHECK_EQUAL(1, fragIndexes.size());
57 BOOST_CHECK_EQUAL(832, fragIndexes.at(0));
Junxiao Shib6e276f2017-08-14 20:10:04 +000058
59 packet.clear<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070060 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000061 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070062}
63
64/// \todo test field access methods with a REPEATABLE field
65
66BOOST_AUTO_TEST_CASE(EncodeFragment)
67{
68 static const uint8_t expectedBlock[] = {
Eric Newberry83872fd2015-08-06 17:01:24 -070069 0x64, 0x08, // LpPacket
70 0x51, 0x02, // Sequence
71 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070072 0x50, 0x02, // Fragment
73 0x03, 0xe8,
74 };
75
76 Buffer buf(2);
77 buf[0] = 0x03;
78 buf[1] = 0xe8;
79
80 Packet packet;
81 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end())));
Eric Newberry83872fd2015-08-06 17:01:24 -070082 BOOST_CHECK_NO_THROW(packet.add<SequenceField>(1000));
Eric Newberry261dbc22015-07-22 23:18:18 -070083 Block wire;
Eric Newberry83872fd2015-08-06 17:01:24 -070084 BOOST_CHECK_NO_THROW(wire = packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -070085 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
86 wire.begin(), wire.end());
87}
88
89BOOST_AUTO_TEST_CASE(EncodeSubTlv)
90{
91 static const uint8_t expectedBlock[] = {
92 0x64, 0x09, // LpPacket
93 0xfd, 0x03, 0x20, 0x05, // Nack
94 0xfd, 0x03, 0x21, 0x01, // NackReason
95 0x64,
96 };
97
98 NackHeader nack;
99 nack.setReason(NackReason::DUPLICATE);
100
101 Packet packet;
102 BOOST_CHECK_NO_THROW(packet.add<NackField>(nack));
103 Block wire;
104 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
105 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
106 wire.begin(), wire.end());
107}
108
Teng Liang02960742017-10-24 00:36:45 -0700109BOOST_AUTO_TEST_CASE(EncodeZeroLengthTlv)
110{
111 static const uint8_t expectedBlock[] = {
112 0x64, 0x04, // LpPacket
113 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
114 };
115
116 Packet packet1, packet2;
117 BOOST_CHECK_NO_THROW(packet1.set<NonDiscoveryField>(EmptyValue{}));
118 Block wire;
119 BOOST_REQUIRE_NO_THROW(wire = packet1.wireEncode());
120 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
121 wire.begin(), wire.end());
122
123 BOOST_CHECK_NO_THROW(packet2.add<NonDiscoveryField>(EmptyValue{}));
124 BOOST_REQUIRE_NO_THROW(wire = packet2.wireEncode());
125 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
126 wire.begin(), wire.end());
127}
128
Eric Newberry261dbc22015-07-22 23:18:18 -0700129BOOST_AUTO_TEST_CASE(EncodeSortOrder)
130{
131 static const uint8_t expectedBlock[] = {
Eric Newberry8422f572017-02-04 21:53:58 -0700132 0x64, 0x19, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700133 0x52, 0x01, // FragIndex
134 0x00,
135 0x53, 0x01, // FragCount
136 0x01,
Eric Newberry8422f572017-02-04 21:53:58 -0700137 0xfd, 0x03, 0x44, 0x01, // Ack
138 0x02,
139 0xfd, 0x03, 0x44, 0x01, // Ack
140 0x04,
141 0xfd, 0x03, 0x44, 0x01, // Ack
142 0x03,
Eric Newberry261dbc22015-07-22 23:18:18 -0700143 0x50, 0x02, // Fragment
144 0x03, 0xe8,
145 };
146
147 Buffer frag(2);
148 frag[0] = 0x03;
149 frag[1] = 0xe8;
150
151 Packet packet;
152 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
153 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
Eric Newberry8422f572017-02-04 21:53:58 -0700154 BOOST_CHECK_NO_THROW(packet.add<AckField>(2));
Eric Newberry2a890772017-06-26 12:06:15 -0700155 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700156 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
Eric Newberry2a890772017-06-26 12:06:15 -0700157 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700158 BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
Eric Newberry2a890772017-06-26 12:06:15 -0700159 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700160 BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
Eric Newberry2a890772017-06-26 12:06:15 -0700161 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700162 Block wire;
163 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
164 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
165 wire.begin(), wire.end());
166}
167
168BOOST_AUTO_TEST_CASE(DecodeNormal)
169{
170 static const uint8_t inputBlock[] = {
171 0x64, 0x0a, // LpPacket
172 0x52, 0x01, // FragIndex
173 0x00,
174 0x53, 0x01, // FragCount
175 0x01,
176 0x50, 0x02, // Fragment
177 0x03, 0xe8,
178 };
179
180 Packet packet;
181 Block wire(inputBlock, sizeof(inputBlock));
182 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
183 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
184 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
185 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
186 Buffer::const_iterator first, last;
187 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
188 BOOST_CHECK_EQUAL(2, last - first);
189 BOOST_CHECK_EQUAL(0x03, *first);
190 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
191 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
192 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
193}
194
195BOOST_AUTO_TEST_CASE(DecodeIdle)
196{
197 static const uint8_t inputBlock[] = {
198 0x64, 0x06, // LpPacket
199 0x52, 0x01, // FragIndex
200 0x00,
201 0x53, 0x01, // FragCount
202 0x01,
203 };
204
205 Packet packet;
206 Block wire(inputBlock, sizeof(inputBlock));
207 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
208 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
209 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
210 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
211 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
212 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
213}
214
215BOOST_AUTO_TEST_CASE(DecodeFragment)
216{
217 static const uint8_t inputBlock[] = {
218 0x64, 0x04, // LpPacket
219 0x50, 0x02, // Fragment
220 0x03, 0xe8,
221 };
222
223 Packet packet;
224 Block wire(inputBlock, sizeof(inputBlock));
225 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
226 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
227 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
228 Buffer::const_iterator first, last;
229 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
230 BOOST_CHECK_EQUAL(2, last - first);
231 BOOST_CHECK_EQUAL(0x03, *first);
232 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
233}
234
Teng Liang02960742017-10-24 00:36:45 -0700235BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader)
236{
237 static const uint8_t inputBlock[] = {
238 0x64, 0x04, // LpPacket
239 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
240 };
241
242 Packet packet;
243 Block wire(inputBlock, sizeof(inputBlock));
244 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
245 BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>());
246 BOOST_CHECK_NO_THROW(packet.get<NonDiscoveryField>());
247}
248
Eric Newberry261dbc22015-07-22 23:18:18 -0700249BOOST_AUTO_TEST_CASE(DecodeEmpty)
250{
251 static const uint8_t inputBlock[] = {
252 0x64, 0x00, // LpPacket
253 };
254
255 Packet packet;
256 Block wire(inputBlock, sizeof(inputBlock));
257 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
258 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
259 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Teng Liang02960742017-10-24 00:36:45 -0700260 BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>());
Eric Newberry261dbc22015-07-22 23:18:18 -0700261}
262
263BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
264{
265 static const uint8_t inputBlock[] = {
266 0x64, 0x06, // LpPacket
267 0x52, 0x01, // FragIndex
268 0x00,
269 0x52, 0x01, // FragIndex
270 0x01,
271 };
272
273 Packet packet;
274 Block wire(inputBlock, sizeof(inputBlock));
275 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
276}
277
Eric Newberry8422f572017-02-04 21:53:58 -0700278BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader)
279{
280 static const uint8_t inputBlock[] = {
281 0x64, 0x0f, // LpPacket
282 0xfd, 0x03, 0x44, 0x01, // Ack
283 0x01,
284 0xfd, 0x03, 0x44, 0x01, // Ack
285 0x03,
286 0xfd, 0x03, 0x44, 0x01, // Ack
287 0x02,
288 };
289
290 Packet packet;
291 Block wire(inputBlock, sizeof(inputBlock));
292 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
293 BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3);
294 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
295 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
296 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
297 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
298}
299
Eric Newberry261dbc22015-07-22 23:18:18 -0700300BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
301{
302 static const uint8_t inputBlock[] = {
303 0x64, 0x08, // LpPacket
304 0x50, 0x02, // Fragment
305 0x03, 0xe8,
306 0x50, 0x02, // Fragment
307 0x03, 0xe9,
308 };
309
310 Packet packet;
311 Block wire(inputBlock, sizeof(inputBlock));
312 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
313}
314
315BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
316{
317 static const uint8_t inputBlock[] = {
318 0x64, 0x0a, // LpPacket
319 0x53, 0x01, // FragCount
320 0x01,
321 0x52, 0x01, // FragIndex
322 0x00,
323 0x50, 0x02, // Fragment
324 0x03, 0xe8,
325 };
326
327 Packet packet;
328 Block wire(inputBlock, sizeof(inputBlock));
329 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
330}
331
332BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
333{
334 static const uint8_t inputBlock[] = {
335 0x64, 0x0a, // LpPacket
336 0x52, 0x01, // FragIndex
337 0x00,
338 0x50, 0x02, // Fragment
339 0x03, 0xe8,
340 0x53, 0x01, // FragCount
341 0x01,
342 };
343
344 Packet packet;
345 Block wire(inputBlock, sizeof(inputBlock));
346 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
347}
348
349BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
350{
351 static const uint8_t inputBlock[] = {
352 0x64, 0x0c, // LpPacket
353 0x52, 0x01, // FragIndex
354 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700355 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700356 0x02,
357 0x50, 0x02, // Fragment
358 0x03, 0xe8,
359 };
360
361 Packet packet;
362 Block wire(inputBlock, sizeof(inputBlock));
363 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
364 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
365 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
366}
367
368BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
369{
370 static const uint8_t inputBlock[] = {
371 0x64, 0x0c, // LpPacket
372 0x52, 0x01, // FragIndex
373 0x00,
374 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
375 0x02,
376 0x50, 0x02, // Fragment
377 0x03, 0xe8,
378 };
379
380 Packet packet;
381 Block wire(inputBlock, sizeof(inputBlock));
382 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
383}
384
385BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
386{
387 static const uint8_t inputBlock[] = {
388 0x05, 0x0a, // Interest
389 0x07, 0x02, // Name
390 0x03, 0xe8,
391 0x0a, 0x04, // Nonce
392 0x01, 0x02, 0x03, 0x04,
393 };
394
395 Packet packet;
396 Block wire(inputBlock, sizeof(inputBlock));
397 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
398 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
399
Eric Newberry261dbc22015-07-22 23:18:18 -0700400 Block encoded;
401 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700402 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700403 encoded.begin(), encoded.end());
404}
405
Teng Liang5b323d12018-01-31 18:50:45 -0700406BOOST_AUTO_TEST_CASE(DecodePrefixAnnouncement)
407{
408 static const uint8_t inputBlock[] = {
409 0x64, 0x70, // LpPacket
410 0xfd, 0x03, 0x50, 0x3a, // PrefixAnnouncement
411 0x06, 0x38, // Data
412 0x07, 0x29, 0x08, 0x0d, 0x73, 0x65, 0x6c, 0x66, 0x2d, 0x6c,
413 0x65, 0x61, 0x72, 0x6e, 0x69, 0x6e, 0x67, 0x08, 0x03, 0x65,
414 0x64, 0x75, 0x08, 0x02, 0x75, 0x61, 0x08, 0x02, 0x63, 0x73,
415 0x08, 0x04, 0x6e, 0x65, 0x77, 0x73, 0x08, 0x05, 0xfd, 0x00,
416 0x03, 0xa5, 0xfe, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b,
417 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00,
418 0x50, 0x30, // Fragment
419 0x06, 0x2e, // Data
420 0x07, 0x1f, 0x08, 0x03, 0x65, 0x64, 0x75, 0x08, 0x02, 0x75,
421 0x61, 0x08, 0x02, 0x63, 0x73, 0x08, 0x04, 0x6e, 0x65, 0x77,
422 0x73, 0x08, 0x0a, 0x69, 0x6e, 0x64, 0x65, 0x78, 0x2e, 0x68,
423 0x74, 0x6d, 0x6c, 0x14, 0x00, 0x15, 0x00, 0x16, 0x05, 0x1b,
424 0x01, 0x01, 0x1c, 0x00, 0x17, 0x00,
425 };
426
427 Data data1("/edu/ua/cs/news/index.html");
428 ndn::SignatureSha256WithRsa fakeSignature;
429 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue));
430 data1.setSignature(fakeSignature);
431
432 Block wire;
433 wire = data1.wireEncode();
434 Packet packet;
435 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
436
437 Name name("/self-learning/edu/ua/cs/news");
438 name.appendVersion(239102);
439 Data data2(name);
440 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue));
441 data2.setSignature(fakeSignature);
442 data2.wireEncode();
443
444 PrefixAnnouncement pa;
445 pa.setData(make_shared<Data>(data2));
446
447 BOOST_CHECK_NO_THROW(packet.add<PrefixAnnouncementField>(pa));
448 Block encoded;
449 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
450 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
451 encoded.begin(), encoded.end());
452}
453
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700454BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
455{
456 Packet packet;
457 Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
458 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
459}
460
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100461BOOST_AUTO_TEST_SUITE_END() // TestPacket
462BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700463
464} // namespace tests
465} // namespace lp
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100466} // namespace ndn