blob: 50cc681ec993d8e11eb746e1c65a2ecc0c352ec1 [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/*
Davide Pesavento187e9f92023-03-20 22:46:22 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/lp/packet.hpp"
Davide Pesavento49e1e872023-11-11 00:45:23 -050023#include "ndn-cxx/lp/fields.hpp"
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "ndn-cxx/prefix-announcement.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070025
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050026#include "tests/key-chain-fixture.hpp"
27#include "tests/test-common.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
30
31using namespace ndn::lp;
Eric Newberry261dbc22015-07-22 23:18:18 -070032
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>());
Junxiao Shic53df032019-01-14 23:33:25 +000042 BOOST_CHECK_EQUAL(packet.count<FragIndexField>(), 0);
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>());
Davide Pesavento923ba442019-02-12 22:00:38 -050047 BOOST_CHECK_THROW(packet.add<FragIndexField>(5678), std::invalid_argument);
Junxiao Shic53df032019-01-14 23:33:25 +000048 BOOST_CHECK_EQUAL(packet.count<FragIndexField>(), 1);
49 BOOST_CHECK_EQUAL(packet.get<FragIndexField>(0), 1234);
Eric Newberry261dbc22015-07-22 23:18:18 -070050 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);
Junxiao Shic53df032019-01-14 23:33:25 +000054 BOOST_CHECK_EQUAL(packet.count<FragIndexField>(), 0);
Junxiao Shib6e276f2017-08-14 20:10:04 +000055
56 packet.add<FragIndexField>(832);
57 std::vector<uint64_t> fragIndexes = packet.list<FragIndexField>();
Junxiao Shic53df032019-01-14 23:33:25 +000058 BOOST_CHECK_EQUAL(fragIndexes.size(), 1);
59 BOOST_CHECK_EQUAL(fragIndexes.at(0), 832);
Junxiao Shib6e276f2017-08-14 20:10:04 +000060
61 packet.clear<FragIndexField>();
Junxiao Shic53df032019-01-14 23:33:25 +000062 BOOST_CHECK_EQUAL(packet.count<FragIndexField>(), 0);
63 BOOST_CHECK(packet.empty());
64
65 packet.add<AckField>(4001);
66 packet.add<AckField>(4002);
67 packet.add<AckField>(4003);
68 BOOST_CHECK_EQUAL(packet.count<AckField>(), 3);
69 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 4001);
70 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 4002);
71 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 4003);
72
73 packet.remove<AckField>(1);
74 BOOST_CHECK_EQUAL(packet.count<AckField>(), 2);
75 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 4001);
76 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 4003);
77
78 packet.remove<AckField>(0);
79 packet.remove<AckField>(0);
80 BOOST_CHECK_EQUAL(packet.count<AckField>(), 0);
Junxiao Shib6e276f2017-08-14 20:10:04 +000081 BOOST_CHECK(packet.empty());
Eric Newberry261dbc22015-07-22 23:18:18 -070082}
83
Eric Newberry261dbc22015-07-22 23:18:18 -070084BOOST_AUTO_TEST_CASE(EncodeFragment)
85{
86 static const uint8_t expectedBlock[] = {
Junxiao Shi974b81a2018-04-21 01:37:03 +000087 0x64, 0x0e, // LpPacket
88 0x51, 0x08, // Sequence
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xe8,
Eric Newberry261dbc22015-07-22 23:18:18 -070090 0x50, 0x02, // Fragment
91 0x03, 0xe8,
92 };
93
94 Buffer buf(2);
95 buf[0] = 0x03;
96 buf[1] = 0xe8;
97
98 Packet packet;
Davide Pesavento258d51a2022-02-27 21:26:28 -050099 packet.add<FragmentField>({buf.begin(), buf.end()});
Junxiao Shi974b81a2018-04-21 01:37:03 +0000100 packet.add<SequenceField>(1000);
101 Block wire = packet.wireEncode();
Eric Newberry261dbc22015-07-22 23:18:18 -0700102 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
103 wire.begin(), wire.end());
104}
105
106BOOST_AUTO_TEST_CASE(EncodeSubTlv)
107{
108 static const uint8_t expectedBlock[] = {
109 0x64, 0x09, // LpPacket
110 0xfd, 0x03, 0x20, 0x05, // Nack
111 0xfd, 0x03, 0x21, 0x01, // NackReason
112 0x64,
113 };
114
115 NackHeader nack;
116 nack.setReason(NackReason::DUPLICATE);
117
118 Packet packet;
Junxiao Shic53df032019-01-14 23:33:25 +0000119 packet.add<NackField>(nack);
120 Block wire = packet.wireEncode();
Eric Newberry261dbc22015-07-22 23:18:18 -0700121 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
122 wire.begin(), wire.end());
123}
124
Teng Liang02960742017-10-24 00:36:45 -0700125BOOST_AUTO_TEST_CASE(EncodeZeroLengthTlv)
126{
127 static const uint8_t expectedBlock[] = {
128 0x64, 0x04, // LpPacket
129 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
130 };
131
Junxiao Shic53df032019-01-14 23:33:25 +0000132 Packet packet1;
133 packet1.set<NonDiscoveryField>(EmptyValue{});
134 Block wire = packet1.wireEncode();
Teng Liang02960742017-10-24 00:36:45 -0700135 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
136 wire.begin(), wire.end());
137
Junxiao Shic53df032019-01-14 23:33:25 +0000138 Packet packet2;
139 packet2.add<NonDiscoveryField>(EmptyValue{});
140 wire = packet2.wireEncode();
Teng Liang02960742017-10-24 00:36:45 -0700141 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
142 wire.begin(), wire.end());
143}
144
Eric Newberry261dbc22015-07-22 23:18:18 -0700145BOOST_AUTO_TEST_CASE(EncodeSortOrder)
146{
147 static const uint8_t expectedBlock[] = {
Junxiao Shi974b81a2018-04-21 01:37:03 +0000148 0x64, 0x2e, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700149 0x52, 0x01, // FragIndex
150 0x00,
151 0x53, 0x01, // FragCount
152 0x01,
Junxiao Shi974b81a2018-04-21 01:37:03 +0000153 0xfd, 0x03, 0x44, 0x08, // Ack
154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
155 0xfd, 0x03, 0x44, 0x08, // Ack
156 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
157 0xfd, 0x03, 0x44, 0x08, // Ack
158 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
Eric Newberry261dbc22015-07-22 23:18:18 -0700159 0x50, 0x02, // Fragment
160 0x03, 0xe8,
161 };
162
163 Buffer frag(2);
164 frag[0] = 0x03;
165 frag[1] = 0xe8;
166
167 Packet packet;
Davide Pesavento258d51a2022-02-27 21:26:28 -0500168 packet.add<FragmentField>({frag.begin(), frag.end()});
Junxiao Shic53df032019-01-14 23:33:25 +0000169 packet.add<FragIndexField>(0);
170 packet.add<AckField>(2);
171 packet.wireEncode();
172 packet.add<FragCountField>(1);
173 packet.wireEncode();
174 packet.add<AckField>(4);
175 packet.wireEncode();
176 packet.add<AckField>(3);
177 Block wire = packet.wireEncode();
Eric Newberry261dbc22015-07-22 23:18:18 -0700178 BOOST_CHECK_EQUAL_COLLECTIONS(expectedBlock, expectedBlock + sizeof(expectedBlock),
179 wire.begin(), wire.end());
180}
181
182BOOST_AUTO_TEST_CASE(DecodeNormal)
183{
184 static const uint8_t inputBlock[] = {
Junxiao Shic53df032019-01-14 23:33:25 +0000185 0x64, 0x2e, // LpPacket
Eric Newberry261dbc22015-07-22 23:18:18 -0700186 0x52, 0x01, // FragIndex
187 0x00,
188 0x53, 0x01, // FragCount
189 0x01,
Junxiao Shic53df032019-01-14 23:33:25 +0000190 0xfd, 0x03, 0x44, 0x08, // Ack
191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
192 0xfd, 0x03, 0x44, 0x08, // Ack
193 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
194 0xfd, 0x03, 0x44, 0x08, // Ack
195 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
Eric Newberry261dbc22015-07-22 23:18:18 -0700196 0x50, 0x02, // Fragment
197 0x03, 0xe8,
198 };
199
200 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500201 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000202 packet.wireDecode(wire);
203
204 BOOST_CHECK_EQUAL(packet.count<FragIndexField>(), 1);
205 BOOST_CHECK_EQUAL(packet.get<FragIndexField>(), 0);
206
207 BOOST_CHECK_EQUAL(packet.count<FragCountField>(), 1);
208 BOOST_CHECK_EQUAL(packet.get<FragCountField>(), 1);
209
210 BOOST_CHECK_EQUAL(packet.count<AckField>(), 3);
211 BOOST_CHECK_EQUAL(packet.get<AckField>(), 1);
212 BOOST_CHECK_EQUAL(packet.get<AckField>(0), 1);
213 BOOST_CHECK_EQUAL(packet.get<AckField>(1), 3);
214 BOOST_CHECK_EQUAL(packet.get<AckField>(2), 2);
215
216 BOOST_CHECK_EQUAL(packet.count<FragmentField>(), 1);
Davide Pesavento187e9f92023-03-20 22:46:22 -0400217 auto [first, last] = packet.get<FragmentField>(0);
Eric Newberry261dbc22015-07-22 23:18:18 -0700218 BOOST_CHECK_EQUAL(2, last - first);
219 BOOST_CHECK_EQUAL(0x03, *first);
220 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
Eric Newberry261dbc22015-07-22 23:18:18 -0700221}
222
223BOOST_AUTO_TEST_CASE(DecodeIdle)
224{
225 static const uint8_t inputBlock[] = {
226 0x64, 0x06, // LpPacket
227 0x52, 0x01, // FragIndex
228 0x00,
229 0x53, 0x01, // FragCount
230 0x01,
231 };
232
233 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500234 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000235 packet.wireDecode(wire);
Eric Newberry261dbc22015-07-22 23:18:18 -0700236 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
237 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
238 BOOST_CHECK_EQUAL(1, packet.count<FragCountField>());
239 BOOST_CHECK_EQUAL(0, packet.get<FragIndexField>(0));
240 BOOST_CHECK_EQUAL(1, packet.get<FragCountField>(0));
241}
242
243BOOST_AUTO_TEST_CASE(DecodeFragment)
244{
245 static const uint8_t inputBlock[] = {
246 0x64, 0x04, // LpPacket
247 0x50, 0x02, // Fragment
248 0x03, 0xe8,
249 };
250
251 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500252 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000253 packet.wireDecode(wire);
Eric Newberry261dbc22015-07-22 23:18:18 -0700254 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
255 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Davide Pesavento187e9f92023-03-20 22:46:22 -0400256 auto [first, last] = packet.get<FragmentField>(0);
Eric Newberry261dbc22015-07-22 23:18:18 -0700257 BOOST_CHECK_EQUAL(2, last - first);
258 BOOST_CHECK_EQUAL(0x03, *first);
259 BOOST_CHECK_EQUAL(0xe8, *(last - 1));
260}
261
Teng Liang02960742017-10-24 00:36:45 -0700262BOOST_AUTO_TEST_CASE(DecodeNonDiscoveryHeader)
263{
264 static const uint8_t inputBlock[] = {
265 0x64, 0x04, // LpPacket
266 0xfd, 0x03, 0x4c, 0x00, // NonDiscovery
267 };
268
269 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500270 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000271 packet.wireDecode(wire);
Teng Liang02960742017-10-24 00:36:45 -0700272 BOOST_CHECK_EQUAL(true, packet.has<NonDiscoveryField>());
Junxiao Shic53df032019-01-14 23:33:25 +0000273 packet.get<NonDiscoveryField>();
Teng Liang02960742017-10-24 00:36:45 -0700274}
275
Eric Newberry261dbc22015-07-22 23:18:18 -0700276BOOST_AUTO_TEST_CASE(DecodeEmpty)
277{
278 static const uint8_t inputBlock[] = {
279 0x64, 0x00, // LpPacket
280 };
281
282 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500283 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000284 packet.wireDecode(wire);
Eric Newberry261dbc22015-07-22 23:18:18 -0700285 BOOST_CHECK_EQUAL(0, packet.count<FragmentField>());
286 BOOST_CHECK_EQUAL(0, packet.count<FragIndexField>());
Teng Liang02960742017-10-24 00:36:45 -0700287 BOOST_CHECK_EQUAL(false, packet.has<NonDiscoveryField>());
Eric Newberry261dbc22015-07-22 23:18:18 -0700288}
289
290BOOST_AUTO_TEST_CASE(DecodeRepeatedNonRepeatableHeader)
291{
292 static const uint8_t inputBlock[] = {
293 0x64, 0x06, // LpPacket
294 0x52, 0x01, // FragIndex
295 0x00,
296 0x52, 0x01, // FragIndex
297 0x01,
298 };
299
300 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500301 Block wire(inputBlock);
Eric Newberry261dbc22015-07-22 23:18:18 -0700302 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
303}
304
305BOOST_AUTO_TEST_CASE(DecodeRepeatedFragment)
306{
307 static const uint8_t inputBlock[] = {
308 0x64, 0x08, // LpPacket
309 0x50, 0x02, // Fragment
310 0x03, 0xe8,
311 0x50, 0x02, // Fragment
312 0x03, 0xe9,
313 };
314
315 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500316 Block wire(inputBlock);
Eric Newberry261dbc22015-07-22 23:18:18 -0700317 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
318}
319
320BOOST_AUTO_TEST_CASE(DecodeWrongOrderAmongHeaders)
321{
322 static const uint8_t inputBlock[] = {
323 0x64, 0x0a, // LpPacket
324 0x53, 0x01, // FragCount
325 0x01,
326 0x52, 0x01, // FragIndex
327 0x00,
328 0x50, 0x02, // Fragment
329 0x03, 0xe8,
330 };
331
332 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500333 Block wire(inputBlock);
Eric Newberry261dbc22015-07-22 23:18:18 -0700334 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
335}
336
337BOOST_AUTO_TEST_CASE(DecodeWrongOrderFragment)
338{
339 static const uint8_t inputBlock[] = {
340 0x64, 0x0a, // LpPacket
341 0x52, 0x01, // FragIndex
342 0x00,
343 0x50, 0x02, // Fragment
344 0x03, 0xe8,
345 0x53, 0x01, // FragCount
346 0x01,
347 };
348
349 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500350 Block wire(inputBlock);
Eric Newberry261dbc22015-07-22 23:18:18 -0700351 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
352}
353
354BOOST_AUTO_TEST_CASE(DecodeIgnoredHeader)
355{
356 static const uint8_t inputBlock[] = {
357 0x64, 0x0c, // LpPacket
358 0x52, 0x01, // FragIndex
359 0x00,
Eric Newberry3ed62472016-12-11 22:11:38 -0700360 0xfd, 0x03, 0x24, 0x01, // unknown TLV-TYPE 804 (ignored)
Eric Newberry261dbc22015-07-22 23:18:18 -0700361 0x02,
362 0x50, 0x02, // Fragment
363 0x03, 0xe8,
364 };
365
366 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500367 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000368 packet.wireDecode(wire);
Eric Newberry261dbc22015-07-22 23:18:18 -0700369 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
370 BOOST_CHECK_EQUAL(1, packet.count<FragIndexField>());
371}
372
373BOOST_AUTO_TEST_CASE(DecodeUnrecognizedHeader)
374{
375 static const uint8_t inputBlock[] = {
376 0x64, 0x0c, // LpPacket
377 0x52, 0x01, // FragIndex
378 0x00,
379 0xfd, 0x03, 0x22, 0x01, // unknown TLV-TYPE 802 (cannot ignore)
380 0x02,
381 0x50, 0x02, // Fragment
382 0x03, 0xe8,
383 };
384
385 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500386 Block wire(inputBlock);
Eric Newberry261dbc22015-07-22 23:18:18 -0700387 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
388}
389
390BOOST_AUTO_TEST_CASE(DecodeBareNetworkLayerPacket)
391{
392 static const uint8_t inputBlock[] = {
393 0x05, 0x0a, // Interest
394 0x07, 0x02, // Name
395 0x03, 0xe8,
396 0x0a, 0x04, // Nonce
397 0x01, 0x02, 0x03, 0x04,
398 };
399
400 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500401 Block wire(inputBlock);
Junxiao Shic53df032019-01-14 23:33:25 +0000402 packet.wireDecode(wire);
Eric Newberry261dbc22015-07-22 23:18:18 -0700403 BOOST_CHECK_EQUAL(1, packet.count<FragmentField>());
404
Junxiao Shic53df032019-01-14 23:33:25 +0000405 Block encoded = packet.wireEncode();
Eric Newberry83872fd2015-08-06 17:01:24 -0700406 BOOST_CHECK_EQUAL_COLLECTIONS(inputBlock, inputBlock + sizeof(inputBlock),
Eric Newberry261dbc22015-07-22 23:18:18 -0700407 encoded.begin(), encoded.end());
408}
409
Junxiao Shic53df032019-01-14 23:33:25 +0000410BOOST_AUTO_TEST_CASE(DecodeSeqNum)
411{
412 Packet packet;
413
414 // Sequence number is fixed-width, not NonNegativeInteger
415 packet.wireDecode("640A 5104A4A5A6A7 5002FFFF"_block);
416 BOOST_CHECK_THROW(packet.get<SequenceField>(), ndn::tlv::Error);
417
418 packet.wireDecode("640E 5108A0A1A2A3A4A5A6A7 5002FFFF"_block);
419 BOOST_CHECK_EQUAL(packet.get<SequenceField>(), 0xA0A1A2A3A4A5A6A7);
420}
421
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700422BOOST_AUTO_TEST_CASE(DecodeUnrecognizedTlvType)
423{
424 Packet packet;
Davide Pesaventofbea4fc2022-02-08 07:26:04 -0500425 Block wire = makeEmptyBlock(ndn::tlv::Name);
Eric Newberry43bf6bb2015-10-09 16:12:09 -0700426 BOOST_CHECK_THROW(packet.wireDecode(wire), Packet::Error);
427}
428
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400429BOOST_FIXTURE_TEST_CASE(DecodePrefixAnnouncement, KeyChainFixture)
Teng Liange3ecad72018-08-28 21:12:53 -0700430{
431 // Construct Data which prefix announcement is attached to
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400432 auto data0 = makeData("/edu/ua/cs/news/index.html");
Teng Liange3ecad72018-08-28 21:12:53 -0700433
Teng Liange3ecad72018-08-28 21:12:53 -0700434 Packet packet0;
Davide Pesavento14c56cd2020-05-21 01:44:03 -0400435 packet0.wireDecode(data0->wireEncode());
Teng Liange3ecad72018-08-28 21:12:53 -0700436
437 // Construct Prefix Announcement
438 PrefixAnnouncement pa;
439 pa.setAnnouncedName("/net/example");
440 pa.setExpiration(5_min);
441 pa.setValidityPeriod(security::ValidityPeriod(time::fromIsoString("20181030T000000"),
442 time::fromIsoString("20181124T235959")));
443 pa.toData(m_keyChain, signingWithSha256(), 1);
444 PrefixAnnouncementHeader pah0(pa);
Junxiao Shic53df032019-01-14 23:33:25 +0000445 packet0.add<PrefixAnnouncementField>(pah0);
446 Block encoded = packet0.wireEncode();
Teng Liange3ecad72018-08-28 21:12:53 -0700447
448 // check decoding
449 Packet packet1;
Junxiao Shic53df032019-01-14 23:33:25 +0000450 packet1.wireDecode(encoded);
Teng Liange3ecad72018-08-28 21:12:53 -0700451 BOOST_CHECK_EQUAL(true, packet1.has<PrefixAnnouncementField>());
Junxiao Shic53df032019-01-14 23:33:25 +0000452 PrefixAnnouncementHeader pah1 = packet1.get<PrefixAnnouncementField>();
Teng Liange3ecad72018-08-28 21:12:53 -0700453 BOOST_CHECK_EQUAL(pah1.getPrefixAnn()->getAnnouncedName(), "/net/example");
454}
455
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100456BOOST_AUTO_TEST_SUITE_END() // TestPacket
457BOOST_AUTO_TEST_SUITE_END() // Lp
Eric Newberry261dbc22015-07-22 23:18:18 -0700458
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400459} // namespace ndn::tests