blob: 1583772a6a459b5ff26aa3ef54fe35b65f279b4f [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 Liange3ecad72018-08-28 21:12:53 -070023#include "prefix-announcement.hpp"
Teng Liang5b323d12018-01-31 18:50:45 -070024#include "security/signature-sha256-with-rsa.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070025
26#include "boost-test.hpp"
Teng Liange3ecad72018-08-28 21:12:53 -070027#include "identity-management-fixture.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070028
29namespace ndn {
30namespace lp {
31namespace tests {
32
Davide Pesaventoeee3e822016-11-26 19:19:34 +010033BOOST_AUTO_TEST_SUITE(Lp)
34BOOST_AUTO_TEST_SUITE(TestPacket)
Eric Newberry261dbc22015-07-22 23:18:18 -070035
36BOOST_AUTO_TEST_CASE(FieldAccess)
37{
38 Packet packet;
39
Junxiao Shib6e276f2017-08-14 20:10:04 +000040 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070041 BOOST_CHECK(!packet.has<FragIndexField>());
42 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000043
44 packet.set<FragIndexField>(1234);
45 BOOST_CHECK(!packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070046 BOOST_CHECK(packet.has<FragIndexField>());
47 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::length_error);
48 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
49 BOOST_CHECK_EQUAL(1234, packet.get<FragIndexField>(0));
50 BOOST_CHECK_THROW(packet.get<FragIndexField>(1), std::out_of_range);
51 BOOST_CHECK_THROW(packet.remove<FragIndexField>(1), std::out_of_range);
Junxiao Shib6e276f2017-08-14 20:10:04 +000052
53 packet.remove<FragIndexField>(0);
Eric Newberry261dbc22015-07-22 23:18:18 -070054 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000055
56 packet.add<FragIndexField>(832);
57 std::vector<uint64_t> fragIndexes = packet.list<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070058 BOOST_CHECK_EQUAL(1, fragIndexes.size());
59 BOOST_CHECK_EQUAL(832, fragIndexes.at(0));
Junxiao Shib6e276f2017-08-14 20:10:04 +000060
61 packet.clear<FragIndexField>();
Eric Newberry261dbc22015-07-22 23:18:18 -070062 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Junxiao Shib6e276f2017-08-14 20:10:04 +000063 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070064}
65
66/// \todo test field access methods with a REPEATABLE field
67
68BOOST_AUTO_TEST_CASE(EncodeFragment)
69{
70 static const uint8_t expectedBlock[] = {
Junxiao Shi974b81a2018-04-21 01:37:03 +000071 0x64, 0x0e, // LpPacket
72 0x51, 0x08, // Sequence
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070074 0x50, 0x02, // Fragment
75 0x03, 0xe8,
76 };
77
78 Buffer buf(2);
79 buf[0] = 0x03;
80 buf[1] = 0xe8;
81
82 Packet packet;
Junxiao Shi974b81a2018-04-21 01:37:03 +000083 packet.add<FragmentField>(std::make_pair(buf.begin(), buf.end()));
84 packet.add<SequenceField>(1000);
85 Block wire = packet.wireEncode();
Eric Newberry261dbc22015-07-22 23:18:18 -070086 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
87 wire.begin(), wire.end());
88}
89
90BOOST_AUTO_TEST_CASE(EncodeSubTlv)
91{
92 static const uint8_t expectedBlock[] = {
93 0x64, 0x09, // LpPacket
94 0xfd, 0x03, 0x20, 0x05, // Nack
95 0xfd, 0x03, 0x21, 0x01, // NackReason
96 0x64,
97 };
98
99 NackHeader nack;
100 nack.setReason(NackReason::DUPLICATE);
101
102 Packet packet;
103 BOOST_CHECK_NO_THROW(packet.add<NackField>(nack));
104 Block wire;
105 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
106 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
107 wire.begin(), wire.end());
108}
109
Teng Liang02960742017-10-24 00:36:45 -0700110BOOST_AUTO_TEST_CASE(EncodeZeroLengthTlv)
111{
112 static const uint8_t expectedBlock[] = {
113 0x64, 0x04, // LpPacket
114 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
115 };
116
117 Packet packet1, packet2;
118 BOOST_CHECK_NO_THROW(packet1.set<NonDiscoveryField>(EmptyValue{}));
119 Block wire;
120 BOOST_REQUIRE_NO_THROW(wire = packet1.wireEncode());
121 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
122 wire.begin(), wire.end());
123
124 BOOST_CHECK_NO_THROW(packet2.add<NonDiscoveryField>(EmptyValue{}));
125 BOOST_REQUIRE_NO_THROW(wire = packet2.wireEncode());
126 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
127 wire.begin(), wire.end());
128}
129
Eric Newberry261dbc22015-07-22 23:18:18 -0700130BOOST_AUTO_TEST_CASE(EncodeSortOrder)
131{
132 static const uint8_t expectedBlock[] = {
Junxiao Shi974b81a2018-04-21 01:37:03 +0000133 0x64, 0x2e, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700134 0x52, 0x01, // FragIndex
135 0x00,
136 0x53, 0x01, // FragCount
137 0x01,
Junxiao Shi974b81a2018-04-21 01:37:03 +0000138 0xfd, 0x03, 0x44, 0x08, // Ack
139 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
140 0xfd, 0x03, 0x44, 0x08, // Ack
141 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
142 0xfd, 0x03, 0x44, 0x08, // Ack
143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
Eric Newberry261dbc22015-07-22 23:18:18 -0700144 0x50, 0x02, // Fragment
145 0x03, 0xe8,
146 };
147
148 Buffer frag(2);
149 frag[0] = 0x03;
150 frag[1] = 0xe8;
151
152 Packet packet;
153 BOOST_CHECK_NO_THROW(packet.add<FragmentField>(std::make_pair(frag.begin(), frag.end())));
154 BOOST_CHECK_NO_THROW(packet.add<FragIndexField>(0));
Eric Newberry8422f572017-02-04 21:53:58 -0700155 BOOST_CHECK_NO_THROW(packet.add<AckField>(2));
Eric Newberry2a890772017-06-26 12:06:15 -0700156 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700157 BOOST_CHECK_NO_THROW(packet.add<FragCountField>(1));
Eric Newberry2a890772017-06-26 12:06:15 -0700158 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700159 BOOST_CHECK_NO_THROW(packet.add<AckField>(4));
Eric Newberry2a890772017-06-26 12:06:15 -0700160 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry8422f572017-02-04 21:53:58 -0700161 BOOST_CHECK_NO_THROW(packet.add<AckField>(3));
Eric Newberry2a890772017-06-26 12:06:15 -0700162 BOOST_REQUIRE_NO_THROW(packet.wireEncode());
Eric Newberry261dbc22015-07-22 23:18:18 -0700163 Block wire;
164 BOOST_REQUIRE_NO_THROW(wire = packet.wireEncode());
165 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
166 wire.begin(), wire.end());
167}
168
169BOOST_AUTO_TEST_CASE(DecodeNormal)
170{
171 static const uint8_t inputBlock[] = {
172 0x64, 0x0a, // LpPacket
173 0x52, 0x01, // FragIndex
174 0x00,
175 0x53, 0x01, // FragCount
176 0x01,
177 0x50, 0x02, // Fragment
178 0x03, 0xe8,
179 };
180
181 Packet packet;
182 Block wire(inputBlock, sizeof(inputBlock));
183 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
184 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
185 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
186 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
187 Buffer::const_iterator first, last;
188 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
189 BOOST_CHECK_EQUAL(2, last - first);
190 BOOST_CHECK_EQUAL(0x03, *first);
191 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
192 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
193 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
194}
195
196BOOST_AUTO_TEST_CASE(DecodeIdle)
197{
198 static const uint8_t inputBlock[] = {
199 0x64, 0x06, // LpPacket
200 0x52, 0x01, // FragIndex
201 0x00,
202 0x53, 0x01, // FragCount
203 0x01,
204 };
205
206 Packet packet;
207 Block wire(inputBlock, sizeof(inputBlock));
208 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
209 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
210 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
211 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
212 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
213 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
214}
215
216BOOST_AUTO_TEST_CASE(DecodeFragment)
217{
218 static const uint8_t inputBlock[] = {
219 0x64, 0x04, // LpPacket
220 0x50, 0x02, // Fragment
221 0x03, 0xe8,
222 };
223
224 Packet packet;
225 Block wire(inputBlock, sizeof(inputBlock));
226 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
227 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
228 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
229 Buffer::const_iterator first, last;
230 BOOST_REQUIRE_NO_THROW(std::tie(first, last) = packet.get<FragmentField>(0));
231 BOOST_CHECK_EQUAL(2, last - first);
232 BOOST_CHECK_EQUAL(0x03, *first);
233 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
234}
235
Teng Liang02960742017-10-24 00:36:45 -0700236BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader)
237{
238 static const uint8_t inputBlock[] = {
239 0x64, 0x04, // LpPacket
240 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
241 };
242
243 Packet packet;
244 Block wire(inputBlock, sizeof(inputBlock));
245 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
246 BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>());
247 BOOST_CHECK_NO_THROW(packet.get<NonDiscoveryField>());
248}
249
Eric Newberry261dbc22015-07-22 23:18:18 -0700250BOOST_AUTO_TEST_CASE(DecodeEmpty)
251{
252 static const uint8_t inputBlock[] = {
253 0x64, 0x00, // LpPacket
254 };
255
256 Packet packet;
257 Block wire(inputBlock, sizeof(inputBlock));
258 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
259 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
260 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Teng Liang02960742017-10-24 00:36:45 -0700261 BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>());
Eric Newberry261dbc22015-07-22 23:18:18 -0700262}
263
264BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
265{
266 static const uint8_t inputBlock[] = {
267 0x64, 0x06, // LpPacket
268 0x52, 0x01, // FragIndex
269 0x00,
270 0x52, 0x01, // FragIndex
271 0x01,
272 };
273
274 Packet packet;
275 Block wire(inputBlock, sizeof(inputBlock));
276 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
277}
278
Eric Newberry8422f572017-02-04 21:53:58 -0700279BOOST_AUTO_TEST_CASE(DecodeRepeatedRepeatableHeader)
280{
281 static const uint8_t inputBlock[] = {
282 0x64, 0x0f, // LpPacket
283 0xfd, 0x03, 0x44, 0x01, // Ack
284 0x01,
285 0xfd, 0x03, 0x44, 0x01, // Ack
286 0x03,
287 0xfd, 0x03, 0x44, 0x01, // Ack
288 0x02,
289 };
290
291 Packet packet;
292 Block wire(inputBlock, sizeof(inputBlock));
293 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
294 BOOST_REQUIRE_EQUAL(packet.count<AckField>(), 3);
295 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
296 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
297 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
298 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
299}
300
Eric Newberry261dbc22015-07-22 23:18:18 -0700301BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
302{
303 static const uint8_t inputBlock[] = {
304 0x64, 0x08, // LpPacket
305 0x50, 0x02, // Fragment
306 0x03, 0xe8,
307 0x50, 0x02, // Fragment
308 0x03, 0xe9,
309 };
310
311 Packet packet;
312 Block wire(inputBlock, sizeof(inputBlock));
313 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
314}
315
316BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
317{
318 static const uint8_t inputBlock[] = {
319 0x64, 0x0a, // LpPacket
320 0x53, 0x01, // FragCount
321 0x01,
322 0x52, 0x01, // FragIndex
323 0x00,
324 0x50, 0x02, // Fragment
325 0x03, 0xe8,
326 };
327
328 Packet packet;
329 Block wire(inputBlock, sizeof(inputBlock));
330 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
331}
332
333BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
334{
335 static const uint8_t inputBlock[] = {
336 0x64, 0x0a, // LpPacket
337 0x52, 0x01, // FragIndex
338 0x00,
339 0x50, 0x02, // Fragment
340 0x03, 0xe8,
341 0x53, 0x01, // FragCount
342 0x01,
343 };
344
345 Packet packet;
346 Block wire(inputBlock, sizeof(inputBlock));
347 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
348}
349
350BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
351{
352 static const uint8_t inputBlock[] = {
353 0x64, 0x0c, // LpPacket
354 0x52, 0x01, // FragIndex
355 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700356 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700357 0x02,
358 0x50, 0x02, // Fragment
359 0x03, 0xe8,
360 };
361
362 Packet packet;
363 Block wire(inputBlock, sizeof(inputBlock));
364 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
365 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
366 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
367}
368
369BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
370{
371 static const uint8_t inputBlock[] = {
372 0x64, 0x0c, // LpPacket
373 0x52, 0x01, // FragIndex
374 0x00,
375 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
376 0x02,
377 0x50, 0x02, // Fragment
378 0x03, 0xe8,
379 };
380
381 Packet packet;
382 Block wire(inputBlock, sizeof(inputBlock));
383 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
384}
385
386BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
387{
388 static const uint8_t inputBlock[] = {
389 0x05, 0x0a, // Interest
390 0x07, 0x02, // Name
391 0x03, 0xe8,
392 0x0a, 0x04, // Nonce
393 0x01, 0x02, 0x03, 0x04,
394 };
395
396 Packet packet;
397 Block wire(inputBlock, sizeof(inputBlock));
398 BOOST_CHECK_NO_THROW(packet.wireDecode(wire));
399 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
400
Eric Newberry261dbc22015-07-22 23:18:18 -0700401 Block encoded;
402 BOOST_CHECK_NO_THROW(encoded = packet.wireEncode());
Eric Newberry83872fd2015-08-06 17:01:24 -0700403 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700404 encoded.begin(), encoded.end());
405}
406
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700407BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
408{
409 Packet packet;
410 Block wire = encoding::makeEmptyBlock(ndn::tlv::Name);
411 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
412}
413
Teng Liange3ecad72018-08-28 21:12:53 -0700414BOOST_FIXTURE_TEST_CASE(DecodePrefixAnnouncement, ndn::tests::IdentityManagementFixture)
415{
416 // Construct Data which prefix announcement is attached to
417 Data data0("/edu/ua/cs/news/index.html");
418 ndn::SignatureSha256WithRsa fakeSignature;
419 fakeSignature.setValue(ndn::encoding::makeEmptyBlock(ndn::tlv::SignatureValue));
420 data0.setSignature(fakeSignature);
421
422 Block wire;
423 wire = data0.wireEncode();
424 Packet packet0;
425 packet0.wireDecode(wire);
426
427 // Construct Prefix Announcement
428 PrefixAnnouncement pa;
429 pa.setAnnouncedName("/net/example");
430 pa.setExpiration(5_min);
431 pa.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20181030T000000"),
432 time::fromIsoString("20181124T235959")));
433 pa.toData(m_keyChain, signingWithSha256(), 1);
434 PrefixAnnouncementHeader pah0(pa);
435 BOOST_CHECK_NO_THROW(packet0.add<PrefixAnnouncementField>(pah0));
436 Block encoded;
437 BOOST_CHECK_NO_THROW(encoded = packet0.wireEncode());
438
439 // check decoding
440 Packet packet1;
441 BOOST_CHECK_NO_THROW(packet1.wireDecode(encoded));
442 BOOST_CHECK_EQUAL(true, packet1.has<PrefixAnnouncementField>());
443 PrefixAnnouncementHeader pah1;
444 BOOST_CHECK_NO_THROW(pah1 = packet1.get<PrefixAnnouncementField>());
445 BOOST_CHECK_EQUAL(pah1.getPrefixAnn()->getAnnouncedName(), "/net/example");
446}
447
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100448BOOST_AUTO_TEST_SUITE_END() // TestPacket
449BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700450
451} // namespace tests
452} // namespace lp
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100453} // namespace ndn