blob: 64682c05a8083ffd10ad6b44c8e4a0bd0d79ffab [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberry7b0071e2017-07-03 17:33:31 +00002/*
Davide Pesavento1d12d2f2019-03-22 12:44:14 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face/generic-link-service.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face/face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028
Davide Pesavento0064c1d2018-03-03 18:43:53 -050029#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/key-chain-fixture.hpp"
31#include "tests/daemon/global-io-fixture.hpp"
32#include "dummy-transport.hpp"
Eric Newberryb49313d2017-12-24 20:22:27 -070033
Teng Liangfdcbb4d2018-01-27 16:01:35 -070034#include <ndn-cxx/lp/empty-value.hpp>
Teng Liangbce3cb52018-09-05 20:08:27 -070035#include <ndn-cxx/lp/prefix-announcement-header.hpp>
Eric Newberryb49313d2017-12-24 20:22:27 -070036#include <ndn-cxx/lp/tags.hpp>
Teng Liangbce3cb52018-09-05 20:08:27 -070037#include <ndn-cxx/security/signing-helpers.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070038
39namespace nfd {
40namespace face {
41namespace tests {
42
43using namespace nfd::tests;
44
45BOOST_AUTO_TEST_SUITE(Face)
46
Junxiao Shicde37ad2015-12-24 01:02:05 -070047using nfd::Face;
48
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040049class GenericLinkServiceFixture : public GlobalIoTimeFixture, public KeyChainFixture
Eric Newberrya98bf932015-09-21 00:58:47 -070050{
51protected:
52 GenericLinkServiceFixture()
53 : service(nullptr)
54 , transport(nullptr)
55 {
Eric Newberrya98bf932015-09-21 00:58:47 -070056 // By default, GenericLinkService is created with default options.
57 // Test cases may invoke .initialize with alternate options.
Davide Pesaventoe4b22382018-06-10 14:37:24 -040058 this->initialize({});
Eric Newberrya98bf932015-09-21 00:58:47 -070059 }
60
61 void
Eric Newberryb49313d2017-12-24 20:22:27 -070062 initialize(const GenericLinkService::Options& options,
63 ssize_t mtu = MTU_UNLIMITED,
64 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070065 {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040066 face = make_unique<Face>(make_unique<GenericLinkService>(options),
67 make_unique<DummyTransport>("dummy://",
68 "dummy://",
69 ndn::nfd::FACE_SCOPE_NON_LOCAL,
70 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
71 ndn::nfd::LINK_TYPE_POINT_TO_POINT,
72 mtu,
73 sendQueueCapacity));
Eric Newberrya98bf932015-09-21 00:58:47 -070074 service = static_cast<GenericLinkService*>(face->getLinkService());
75 transport = static_cast<DummyTransport*>(face->getTransport());
76
77 face->afterReceiveInterest.connect(
78 [this] (const Interest& interest) { receivedInterests.push_back(interest); });
79 face->afterReceiveData.connect(
80 [this] (const Data& data) { receivedData.push_back(data); });
81 face->afterReceiveNack.connect(
82 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
83 }
84
Teng Liangbce3cb52018-09-05 20:08:27 -070085 lp::PrefixAnnouncementHeader
86 makePrefixAnnHeader(const Name& announcedName)
87 {
88 return lp::PrefixAnnouncementHeader{signPrefixAnn(
89 makePrefixAnn(announcedName, 1_h), m_keyChain, ndn::signingWithSha256())};
90 }
91
Eric Newberrya98bf932015-09-21 00:58:47 -070092protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070093 unique_ptr<Face> face;
Eric Newberrya98bf932015-09-21 00:58:47 -070094 GenericLinkService* service;
95 DummyTransport* transport;
96 std::vector<Interest> receivedInterests;
97 std::vector<Data> receivedData;
98 std::vector<lp::Nack> receivedNacks;
99};
100
101BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
102
103
104BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
105
106BOOST_AUTO_TEST_CASE(SendInterest)
107{
Eric Newberry86d31872015-09-23 16:24:59 -0700108 // Initialize with Options that disables all services
109 GenericLinkService::Options options;
110 options.allowLocalFields = false;
111 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700112
113 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
114
115 face->sendInterest(*interest1);
116
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700117 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700118 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700119 lp::Packet interest1pkt;
120 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
121 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
122 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700123}
124
125BOOST_AUTO_TEST_CASE(SendData)
126{
Eric Newberry86d31872015-09-23 16:24:59 -0700127 // Initialize with Options that disables all services
128 GenericLinkService::Options options;
129 options.allowLocalFields = false;
130 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700131
132 shared_ptr<Data> data1 = makeData("/localhost/test");
133
134 face->sendData(*data1);
135
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700136 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700137 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700138 lp::Packet data1pkt;
139 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
140 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
141 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700142}
143
144BOOST_AUTO_TEST_CASE(SendNack)
145{
Eric Newberry86d31872015-09-23 16:24:59 -0700146 // Initialize with Options that disables all services
147 GenericLinkService::Options options;
148 options.allowLocalFields = false;
149 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700150
151 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
152
153 face->sendNack(nack1);
154
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700155 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700156 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
157 lp::Packet nack1pkt;
158 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159 BOOST_CHECK(nack1pkt.has<lp::NackField>());
160 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
161 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700162}
163
164BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
165{
Eric Newberry86d31872015-09-23 16:24:59 -0700166 // Initialize with Options that disables all services
167 GenericLinkService::Options options;
168 options.allowLocalFields = false;
169 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700170
171 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
172
173 transport->receivePacket(interest1->wireEncode());
174
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700175 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700176 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
177 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
178}
179
180BOOST_AUTO_TEST_CASE(ReceiveInterest)
181{
Eric Newberry86d31872015-09-23 16:24:59 -0700182 // Initialize with Options that disables all services
183 GenericLinkService::Options options;
184 options.allowLocalFields = false;
185 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700186
187 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
188 lp::Packet lpPacket;
189 lpPacket.set<lp::FragmentField>(std::make_pair(
190 interest1->wireEncode().begin(), interest1->wireEncode().end()));
191 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
192
193 transport->receivePacket(lpPacket.wireEncode());
194
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700195 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700196 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
197 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
198}
199
200BOOST_AUTO_TEST_CASE(ReceiveBareData)
201{
Eric Newberry86d31872015-09-23 16:24:59 -0700202 // Initialize with Options that disables all services
203 GenericLinkService::Options options;
204 options.allowLocalFields = false;
205 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700206
207 shared_ptr<Data> data1 = makeData("/12345678");
208
209 transport->receivePacket(data1->wireEncode());
210
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700211 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700212 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
213 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
214}
215
216BOOST_AUTO_TEST_CASE(ReceiveData)
217{
Eric Newberry86d31872015-09-23 16:24:59 -0700218 // Initialize with Options that disables all services
219 GenericLinkService::Options options;
220 options.allowLocalFields = false;
221 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700222
223 shared_ptr<Data> data1 = makeData("/12345689");
224 lp::Packet lpPacket;
225 lpPacket.set<lp::FragmentField>(std::make_pair(
226 data1->wireEncode().begin(), data1->wireEncode().end()));
227 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
228
229 transport->receivePacket(lpPacket.wireEncode());
230
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700231 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700232 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
233 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
234}
235
236BOOST_AUTO_TEST_CASE(ReceiveNack)
237{
Eric Newberry86d31872015-09-23 16:24:59 -0700238 // Initialize with Options that disables all services
239 GenericLinkService::Options options;
240 options.allowLocalFields = false;
241 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700242
243 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
244 lp::Packet lpPacket;
245 lpPacket.set<lp::FragmentField>(std::make_pair(
246 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
247 lpPacket.set<lp::NackField>(nack1.getHeader());
248
249 transport->receivePacket(lpPacket.wireEncode());
250
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700251 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700252 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700253 BOOST_CHECK(receivedNacks.back().getReason() == nack1.getReason());
254 BOOST_CHECK(receivedNacks.back().getInterest() == nack1.getInterest());
Eric Newberrya98bf932015-09-21 00:58:47 -0700255}
256
Eric Newberrya1939ba2015-10-09 12:35:03 -0700257BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
258{
259 // Initialize with Options that disables all services
260 GenericLinkService::Options options;
261 options.allowLocalFields = false;
262 initialize(options);
263
264 lp::Packet lpPacket;
265 lpPacket.set<lp::SequenceField>(0);
266
267 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
268
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700269 // IDLE packet should be ignored, but is not an error
270 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700271 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
272 BOOST_CHECK_EQUAL(receivedData.size(), 0);
273 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
274}
275
Eric Newberrya98bf932015-09-21 00:58:47 -0700276BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
277
Eric Newberry86d31872015-09-23 16:24:59 -0700278BOOST_AUTO_TEST_SUITE(Fragmentation)
279
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700280BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
281{
282 // Initialize with Options that disable fragmentation
283 GenericLinkService::Options options;
284 options.allowFragmentation = false;
285 initialize(options);
286
287 transport->setMtu(55);
288
289 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
290 face->sendData(*data);
291
292 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
293 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
294}
295
296BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
297{
298 // Initialize with Options that enable fragmentation
299 GenericLinkService::Options options;
300 options.allowFragmentation = true;
301 initialize(options);
302
303 transport->setMtu(MTU_UNLIMITED);
304
305 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
306 face->sendData(*data);
307
308 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
309}
310
311BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
312{
313 // Initialize with Options that enable fragmentation
314 GenericLinkService::Options options;
315 options.allowFragmentation = true;
316 initialize(options);
317
318 transport->setMtu(105);
319
320 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
321 face->sendData(*data);
322
323 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
324}
325
326BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
327{
328 // Initialize with Options that enable fragmentation
329 GenericLinkService::Options options;
330 options.allowFragmentation = true;
331 initialize(options);
332
333 transport->setMtu(60);
334
335 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
336 face->sendData(*data);
337
338 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
339}
340
341BOOST_AUTO_TEST_CASE(ReassembleFragments)
342{
343 // Initialize with Options that enables reassembly
344 GenericLinkService::Options options;
345 options.allowReassembly = true;
346 initialize(options);
347
348 shared_ptr<Interest> interest = makeInterest(
349 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
350 lp::Packet packet(interest->wireEncode());
351
352 // fragment the packet
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400353 LpFragmenter fragmenter({});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700354 size_t mtu = 100;
355 bool isOk = false;
356 std::vector<lp::Packet> frags;
357 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
358 BOOST_REQUIRE(isOk);
359 BOOST_CHECK_GT(frags.size(), 1);
360
361 // receive the fragments
362 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
363 size_t sequence = 1000 + fragIndex;
364 frags[fragIndex].add<lp::SequenceField>(sequence);
365
366 transport->receivePacket(frags[fragIndex].wireEncode());
367
368 if (fragIndex > 0) {
369 BOOST_CHECK(receivedInterests.empty());
370 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
371 }
372 else {
373 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
374 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest);
375 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
376 }
377 }
378}
379
Eric Newberry86d31872015-09-23 16:24:59 -0700380BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
381{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700382 // Initialize with Options that disables reassembly
383 GenericLinkService::Options options;
384 options.allowReassembly = false;
385 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700386
387 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
388 lp::Packet packet(interest->wireEncode());
389 packet.set<lp::FragIndexField>(140);
390
391 transport->receivePacket(packet.wireEncode());
392
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700393 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700394 BOOST_CHECK(receivedInterests.empty());
395}
396
397BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
398{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700399 // Initialize with Options that disables reassembly
400 GenericLinkService::Options options;
401 options.allowReassembly = false;
402 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700403
404 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
405 lp::Packet packet(interest->wireEncode());
406 packet.set<lp::FragCountField>(276);
407
408 transport->receivePacket(packet.wireEncode());
409
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700410 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700411 BOOST_CHECK(receivedInterests.empty());
412}
413
414BOOST_AUTO_TEST_SUITE_END() // Fragmentation
415
Eric Newberry185ab292017-03-28 06:45:39 +0000416BOOST_AUTO_TEST_SUITE(Reliability)
417
Eric Newberry7b0071e2017-07-03 17:33:31 +0000418BOOST_AUTO_TEST_CASE(SendInterest)
Eric Newberry185ab292017-03-28 06:45:39 +0000419{
420 // Initialize with Options that enables reliability
421 GenericLinkService::Options options;
422 options.allowLocalFields = false;
423 options.reliabilityOptions.isEnabled = true;
424 initialize(options);
425
426 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
427
428 face->sendInterest(*interest1);
429
430 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
431 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
432 lp::Packet interest1pkt;
433 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
434 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000435 BOOST_CHECK(interest1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000436}
437
Eric Newberry7b0071e2017-07-03 17:33:31 +0000438BOOST_AUTO_TEST_CASE(SendData)
Eric Newberry185ab292017-03-28 06:45:39 +0000439{
440 // Initialize with Options that enables reliability
441 GenericLinkService::Options options;
442 options.allowLocalFields = false;
443 options.reliabilityOptions.isEnabled = true;
444 initialize(options);
445
446 shared_ptr<Data> data1 = makeData("/localhost/test");
447
448 face->sendData(*data1);
449
450 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
451 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
452 lp::Packet data1pkt;
453 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
454 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000455 BOOST_CHECK(data1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000456}
457
Eric Newberry7b0071e2017-07-03 17:33:31 +0000458BOOST_AUTO_TEST_CASE(SendNack)
Eric Newberry185ab292017-03-28 06:45:39 +0000459{
460 // Initialize with Options that enables reliability
461 GenericLinkService::Options options;
462 options.allowLocalFields = false;
463 options.reliabilityOptions.isEnabled = true;
464 initialize(options);
465
466 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
467
468 face->sendNack(nack1);
469
470 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
471 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
472 lp::Packet nack1pkt;
473 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
474 BOOST_CHECK(nack1pkt.has<lp::NackField>());
475 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000476 BOOST_CHECK(nack1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000477}
478
479BOOST_AUTO_TEST_SUITE_END() // Reliability
Eric Newberry86d31872015-09-23 16:24:59 -0700480
Eric Newberryb49313d2017-12-24 20:22:27 -0700481// congestion detection and marking
482BOOST_AUTO_TEST_SUITE(CongestionMark)
483
484BOOST_AUTO_TEST_CASE(NoCongestion)
485{
486 GenericLinkService::Options options;
487 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400488 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700489 initialize(options, MTU_UNLIMITED, 65536);
490 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
491 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
492 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
493
494 shared_ptr<Interest> interest = makeInterest("/12345678");
495
496 // congestion threshold will be 32768 bytes, since min(65536, 65536 / 2) = 32768 bytes
497
498 // no congestion
499 transport->setSendQueueLength(0);
500 face->sendInterest(*interest);
501 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
502 lp::Packet pkt1;
503 BOOST_REQUIRE_NO_THROW(pkt1.wireDecode(transport->sentPackets.back().packet));
504 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
505 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
506 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
507 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
508
509 // no congestion
510 transport->setSendQueueLength(32768);
511 face->sendInterest(*interest);
512 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
513 lp::Packet pkt2;
514 BOOST_REQUIRE_NO_THROW(pkt2.wireDecode(transport->sentPackets.back().packet));
515 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
516 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
517 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
518 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
519}
520
521BOOST_AUTO_TEST_CASE(CongestionCoDel)
522{
523 GenericLinkService::Options options;
524 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400525 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700526 initialize(options, MTU_UNLIMITED, 65536);
527 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
528 BOOST_CHECK_EQUAL(service->m_lastMarkTime, time::steady_clock::TimePoint::min());
529 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
530 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
531
532 shared_ptr<Interest> interest = makeInterest("/12345678");
533
534 // congestion threshold will be 32768 bytes, since min(65536, 65536 / 2) = 32768 bytes
535
536 // first congested packet, will be marked
537 transport->setSendQueueLength(32769);
538 face->sendInterest(*interest);
539 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
540 lp::Packet pkt1;
541 BOOST_REQUIRE_NO_THROW(pkt1.wireDecode(transport->sentPackets.back().packet));
542 BOOST_REQUIRE_EQUAL(pkt1.count<lp::CongestionMarkField>(), 1);
543 BOOST_CHECK_EQUAL(pkt1.get<lp::CongestionMarkField>(), 1);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400544 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700545 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
546 time::steady_clock::TimePoint lastMarkTime = time::steady_clock::now();
547 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
548 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
549 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
550
551 // advance clock to half of marking interval cycle
Davide Pesavento14e71f02019-03-28 17:35:25 -0400552 advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700553
554 // second congested packet, but within marking interval, will not be marked
555 transport->setSendQueueLength(33000);
556 face->sendInterest(*interest);
557 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
558 lp::Packet pkt2;
559 BOOST_REQUIRE_NO_THROW(pkt2.wireDecode(transport->sentPackets.back().packet));
560 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
561 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
562 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
563 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
564 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
565
566 // advance clocks past end of initial interval cycle
Davide Pesavento14e71f02019-03-28 17:35:25 -0400567 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700568
569 // first congested packet after waiting marking interval, will be marked
570 transport->setSendQueueLength(40000);
571 face->sendInterest(*interest);
572 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
573 lp::Packet pkt3;
574 BOOST_REQUIRE_NO_THROW(pkt3.wireDecode(transport->sentPackets.back().packet));
575 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 1);
576 BOOST_CHECK_EQUAL(pkt3.get<lp::CongestionMarkField>(), 1);
577 time::nanoseconds markingInterval(
578 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
579 std::sqrt(service->m_nMarkedSinceInMarkingState)));
580 nextMarkTime += markingInterval;
581 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
582 lastMarkTime = time::steady_clock::now();
583 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
584 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
585 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
586
587 // advance clock partway through current marking interval
Davide Pesavento14e71f02019-03-28 17:35:25 -0400588 this->advanceClocks(markingInterval - 10_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700589
590 // still congested, but within marking interval cycle
591 transport->setSendQueueLength(38000);
592 face->sendInterest(*interest);
593 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 4);
594 lp::Packet pkt4;
595 BOOST_REQUIRE_NO_THROW(pkt4.wireDecode(transport->sentPackets.back().packet));
596 BOOST_CHECK_EQUAL(pkt4.count<lp::CongestionMarkField>(), 0);
597 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
598 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
599 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
600 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
601
602 // advance clocks past end of current marking interval cycle
Davide Pesavento14e71f02019-03-28 17:35:25 -0400603 this->advanceClocks(11_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700604
605 // still congested, after marking interval cycle
606 transport->setSendQueueLength(39000);
607 face->sendInterest(*interest);
608 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 5);
609 lp::Packet pkt5;
610 BOOST_REQUIRE_NO_THROW(pkt5.wireDecode(transport->sentPackets.back().packet));
611 BOOST_REQUIRE_EQUAL(pkt5.count<lp::CongestionMarkField>(), 1);
612 BOOST_CHECK_EQUAL(pkt5.get<lp::CongestionMarkField>(), 1);
613 markingInterval = time::nanoseconds(
614 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
615 std::sqrt(service->m_nMarkedSinceInMarkingState)));
616 nextMarkTime += markingInterval;
617 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
618 lastMarkTime = time::steady_clock::now();
619 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
620 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
621 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
622
Davide Pesavento14e71f02019-03-28 17:35:25 -0400623 this->advanceClocks(1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700624
625 // still congested, but within marking interval cycle
626 transport->setSendQueueLength(38000);
627 face->sendInterest(*interest);
628 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 6);
629 lp::Packet pkt6;
630 BOOST_REQUIRE_NO_THROW(pkt6.wireDecode(transport->sentPackets.back().packet));
631 BOOST_CHECK_EQUAL(pkt6.count<lp::CongestionMarkField>(), 0);
632 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
633 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
634 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
635 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
636
637 this->advanceClocks(markingInterval);
638
639 // still congested, after marking interval cycle
640 transport->setSendQueueLength(34000);
641 face->sendInterest(*interest);
642 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 7);
643 lp::Packet pkt7;
644 BOOST_REQUIRE_NO_THROW(pkt7.wireDecode(transport->sentPackets.back().packet));
645 BOOST_REQUIRE_EQUAL(pkt7.count<lp::CongestionMarkField>(), 1);
646 BOOST_CHECK_EQUAL(pkt7.get<lp::CongestionMarkField>(), 1);
647 markingInterval = time::nanoseconds(
648 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
649 std::sqrt(service->m_nMarkedSinceInMarkingState)));
650 nextMarkTime += markingInterval;
651 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
652 lastMarkTime = time::steady_clock::now();
653 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
654 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 4);
655 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
656
657 // no more congestion
658 transport->setSendQueueLength(30000);
659 face->sendInterest(*interest);
660 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 8);
661 lp::Packet pkt8;
662 BOOST_REQUIRE_NO_THROW(pkt8.wireDecode(transport->sentPackets.back().packet));
663 BOOST_CHECK_EQUAL(pkt8.count<lp::CongestionMarkField>(), 0);
664 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
665 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
666 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
667 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
668
Davide Pesavento14e71f02019-03-28 17:35:25 -0400669 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700670
671 // send queue congested again, but can't mark packet because within one full interval of last mark
672 transport->setSendQueueLength(50000);
673 face->sendInterest(*interest);
674 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 9);
675 lp::Packet pkt9;
676 BOOST_REQUIRE_NO_THROW(pkt9.wireDecode(transport->sentPackets.back().packet));
677 BOOST_CHECK_EQUAL(pkt9.count<lp::CongestionMarkField>(), 0);
678 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
679 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
680 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
681 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
682
683 // advance clock past full 100ms interval since last mark
Davide Pesavento14e71f02019-03-28 17:35:25 -0400684 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700685
686 transport->setSendQueueLength(40000);
687 face->sendInterest(*interest);
688 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 10);
689 lp::Packet pkt10;
690 BOOST_REQUIRE_NO_THROW(pkt10.wireDecode(transport->sentPackets.back().packet));
691 BOOST_REQUIRE_EQUAL(pkt10.count<lp::CongestionMarkField>(), 1);
692 BOOST_CHECK_EQUAL(pkt10.get<lp::CongestionMarkField>(), 1);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400693 nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700694 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
695 lastMarkTime = time::steady_clock::now();
696 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
697 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
698 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
699
700 // advance clock partway through 100ms marking interval
Davide Pesavento14e71f02019-03-28 17:35:25 -0400701 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700702
703 // not marked since within 100ms window before can mark again
704 transport->setSendQueueLength(50000);
705 face->sendInterest(*interest);
706 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 11);
707 lp::Packet pkt11;
708 BOOST_REQUIRE_NO_THROW(pkt11.wireDecode(transport->sentPackets.back().packet));
709 BOOST_CHECK_EQUAL(pkt11.count<lp::CongestionMarkField>(), 0);
710 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
711 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
712 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
713 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
714
715 // advance clocks past m_nextMarkTime
Davide Pesavento14e71f02019-03-28 17:35:25 -0400716 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700717
718 // markable packet, queue length still above threshold
719 transport->setSendQueueLength(33000);
720 face->sendInterest(*interest);
721 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 12);
722 lp::Packet pkt12;
723 BOOST_REQUIRE_NO_THROW(pkt12.wireDecode(transport->sentPackets.back().packet));
724 BOOST_REQUIRE_EQUAL(pkt12.count<lp::CongestionMarkField>(), 1);
725 BOOST_CHECK_EQUAL(pkt12.get<lp::CongestionMarkField>(), 1);
726 markingInterval = time::nanoseconds(
727 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
728 std::sqrt(service->m_nMarkedSinceInMarkingState)));
729 nextMarkTime += markingInterval;
730 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
731 lastMarkTime = time::steady_clock::now();
732 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
733 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
734 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
735
736 // no more congestion
737 transport->setSendQueueLength(10000);
738 face->sendInterest(*interest);
739 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 13);
740 lp::Packet pkt13;
741 BOOST_REQUIRE_NO_THROW(pkt13.wireDecode(transport->sentPackets.back().packet));
742 BOOST_CHECK_EQUAL(pkt13.count<lp::CongestionMarkField>(), 0);
743 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
744 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
745 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
746 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
747
748 // advance clocks past one full interval since last mark
Davide Pesavento14e71f02019-03-28 17:35:25 -0400749 this->advanceClocks(101_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700750
751 // start congestion again
752 transport->setSendQueueLength(50000);
753 face->sendInterest(*interest);
754 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 14);
755 lp::Packet pkt14;
756 BOOST_REQUIRE_NO_THROW(pkt14.wireDecode(transport->sentPackets.back().packet));
757 BOOST_REQUIRE_EQUAL(pkt14.count<lp::CongestionMarkField>(), 1);
758 BOOST_CHECK_EQUAL(pkt14.get<lp::CongestionMarkField>(), 1);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400759 nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700760 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
761 lastMarkTime = time::steady_clock::now();
762 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
763 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
764 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 7);
765
766 // no more congestion, cancel marking interval
767 transport->setSendQueueLength(5000);
768 face->sendInterest(*interest);
769 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 15);
770 lp::Packet pkt15;
771 BOOST_REQUIRE_NO_THROW(pkt15.wireDecode(transport->sentPackets.back().packet));
772 BOOST_CHECK_EQUAL(pkt15.count<lp::CongestionMarkField>(), 0);
773 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
774 BOOST_CHECK_EQUAL(service->m_lastMarkTime, lastMarkTime);
775 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
776 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 7);
777}
778
779BOOST_AUTO_TEST_CASE(DefaultThreshold)
780{
781 GenericLinkService::Options options;
782 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400783 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700784 initialize(options, MTU_UNLIMITED, QUEUE_UNSUPPORTED);
785 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
786 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
787 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
788
789 shared_ptr<Interest> interest = makeInterest("/12345678");
790
791 // congestion threshold will be 65536 bytes, since the transport reports that it cannot measure
792 // the queue capacity
793
794 // no congestion
795 transport->setSendQueueLength(0);
796 face->sendInterest(*interest);
797 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
798 lp::Packet pkt1;
799 BOOST_REQUIRE_NO_THROW(pkt1.wireDecode(transport->sentPackets.back().packet));
800 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
801 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
802 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
803 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
804
805 // no congestion
806 transport->setSendQueueLength(65536);
807 face->sendInterest(*interest);
808 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
809 lp::Packet pkt2;
810 BOOST_REQUIRE_NO_THROW(pkt2.wireDecode(transport->sentPackets.back().packet));
811 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
812 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
813 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
814 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
815
816 // first congested (and marked) packet
817 transport->setSendQueueLength(65537);
818 face->sendInterest(*interest);
819 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
820 lp::Packet pkt3;
821 BOOST_REQUIRE_NO_THROW(pkt3.wireDecode(transport->sentPackets.back().packet));
822 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 1);
823 BOOST_CHECK_EQUAL(pkt3.get<lp::CongestionMarkField>(), 1);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400824 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700825 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
826 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
827 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
828}
829
830BOOST_AUTO_TEST_SUITE_END() // CongestionMark
831
Eric Newberryee400b52016-11-24 14:12:48 +0000832BOOST_AUTO_TEST_SUITE(LpFields)
Eric Newberrya98bf932015-09-21 00:58:47 -0700833
Eric Newberrya98bf932015-09-21 00:58:47 -0700834BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
835{
Eric Newberry86d31872015-09-23 16:24:59 -0700836 // Initialize with Options that enables local fields
837 GenericLinkService::Options options;
838 options.allowLocalFields = true;
839 initialize(options);
840
841 shared_ptr<Interest> interest = makeInterest("/12345678");
842 lp::Packet packet(interest->wireEncode());
843 packet.set<lp::NextHopFaceIdField>(1000);
844
845 transport->receivePacket(packet.wireEncode());
846
847 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000848 shared_ptr<lp::NextHopFaceIdTag> tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
849 BOOST_REQUIRE(tag != nullptr);
850 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700851}
852
853BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
854{
Eric Newberry86d31872015-09-23 16:24:59 -0700855 // Initialize with Options that disables local fields
856 GenericLinkService::Options options;
857 options.allowLocalFields = false;
858 initialize(options);
859
860 shared_ptr<Interest> interest = makeInterest("/12345678");
861 lp::Packet packet(interest->wireEncode());
862 packet.set<lp::NextHopFaceIdField>(1000);
863
864 transport->receivePacket(packet.wireEncode());
865
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700866 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700867 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700868}
869
870BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
871{
Eric Newberry86d31872015-09-23 16:24:59 -0700872 // Initialize with Options that enables local fields
873 GenericLinkService::Options options;
874 options.allowLocalFields = true;
875 initialize(options);
876
877 shared_ptr<Data> data = makeData("/12345678");
878 lp::Packet packet(data->wireEncode());
879 packet.set<lp::NextHopFaceIdField>(1000);
880
881 transport->receivePacket(packet.wireEncode());
882
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700883 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700884 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700885}
886
887BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
888{
Eric Newberry86d31872015-09-23 16:24:59 -0700889 // Initialize with Options that enables local fields
890 GenericLinkService::Options options;
891 options.allowLocalFields = true;
892 initialize(options);
893
894 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
895 lp::Packet packet;
896 packet.set<lp::FragmentField>(std::make_pair(
897 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
898 packet.set<lp::NackField>(nack.getHeader());
899 packet.set<lp::NextHopFaceIdField>(1000);
900
901 transport->receivePacket(packet.wireEncode());
902
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700903 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700904 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700905}
906
Junxiao Shi6eb02712017-05-27 22:48:02 +0000907BOOST_AUTO_TEST_CASE(ReceiveCachePolicy)
Eric Newberrya98bf932015-09-21 00:58:47 -0700908{
Junxiao Shi6eb02712017-05-27 22:48:02 +0000909 // Initialize with Options that disables local fields
Eric Newberry86d31872015-09-23 16:24:59 -0700910 GenericLinkService::Options options;
Junxiao Shi6eb02712017-05-27 22:48:02 +0000911 options.allowLocalFields = false;
Eric Newberry86d31872015-09-23 16:24:59 -0700912 initialize(options);
Junxiao Shi6eb02712017-05-27 22:48:02 +0000913 // CachePolicy is unprivileged and does not require allowLocalFields option.
Eric Newberry86d31872015-09-23 16:24:59 -0700914
915 shared_ptr<Data> data = makeData("/12345678");
916 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000917 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700918
919 transport->receivePacket(packet.wireEncode());
920
921 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000922 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
923 BOOST_REQUIRE(tag != nullptr);
924 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700925}
926
Junxiao Shi6eb02712017-05-27 22:48:02 +0000927BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropInterest)
Eric Newberrya98bf932015-09-21 00:58:47 -0700928{
Eric Newberry86d31872015-09-23 16:24:59 -0700929 // Initialize with Options that enables local fields
930 GenericLinkService::Options options;
931 options.allowLocalFields = true;
932 initialize(options);
933
934 shared_ptr<Interest> interest = makeInterest("/12345678");
935 lp::Packet packet(interest->wireEncode());
936 lp::CachePolicy policy;
937 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
938 packet.set<lp::CachePolicyField>(policy);
939
940 transport->receivePacket(packet.wireEncode());
941
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700942 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700943 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700944}
945
Junxiao Shi6eb02712017-05-27 22:48:02 +0000946BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropNack)
Eric Newberrya98bf932015-09-21 00:58:47 -0700947{
Eric Newberry86d31872015-09-23 16:24:59 -0700948 // Initialize with Options that enables local fields
949 GenericLinkService::Options options;
950 options.allowLocalFields = true;
951 initialize(options);
952
953 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
954 lp::Packet packet(nack.getInterest().wireEncode());
955 packet.set<lp::NackField>(nack.getHeader());
956 lp::CachePolicy policy;
957 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
958 packet.set<lp::CachePolicyField>(policy);
959
960 transport->receivePacket(packet.wireEncode());
961
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700962 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700963 BOOST_CHECK(receivedNacks.empty());
964}
965
966BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
967{
968 // Initialize with Options that enables local fields
969 GenericLinkService::Options options;
970 options.allowLocalFields = true;
971 initialize(options);
972
973 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000974 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700975
976 face->sendInterest(*interest);
977
978 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
979 lp::Packet sent(transport->sentPackets.back().packet);
980 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
981 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
982}
983
984BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
985{
986 // Initialize with Options that disables local fields
987 GenericLinkService::Options options;
988 options.allowLocalFields = false;
989 initialize(options);
990
991 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000992 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700993
994 face->sendInterest(*interest);
995
996 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
997 lp::Packet sent(transport->sentPackets.back().packet);
998 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
999}
1000
1001BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
1002{
1003 // Initialize with Options that enables local fields
1004 GenericLinkService::Options options;
1005 options.allowLocalFields = true;
1006 initialize(options);
1007
1008 shared_ptr<Interest> interest = makeInterest("/12345678");
1009 lp::Packet packet(interest->wireEncode());
1010 packet.set<lp::IncomingFaceIdField>(1000);
1011
1012 transport->receivePacket(packet.wireEncode());
1013
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001014 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001015 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001016 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001017}
1018
1019BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
1020{
1021 // Initialize with Options that enables local fields
1022 GenericLinkService::Options options;
1023 options.allowLocalFields = true;
1024 initialize(options);
1025
1026 shared_ptr<Data> data = makeData("/z1megUh9Bj");
1027 lp::Packet packet(data->wireEncode());
1028 packet.set<lp::IncomingFaceIdField>(1000);
1029
1030 transport->receivePacket(packet.wireEncode());
1031
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001032 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001033 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001034 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001035}
1036
1037BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
1038{
1039 // Initialize with Options that enables local fields
1040 GenericLinkService::Options options;
1041 options.allowLocalFields = true;
1042 initialize(options);
1043
1044 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
1045 lp::Packet packet(nack.getInterest().wireEncode());
1046 packet.set<lp::NackField>(nack.getHeader());
1047 packet.set<lp::IncomingFaceIdField>(1000);
1048
1049 transport->receivePacket(packet.wireEncode());
1050
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001051 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001052 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001053 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -07001054}
1055
Eric Newberryee400b52016-11-24 14:12:48 +00001056BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
1057{
1058 shared_ptr<Interest> interest = makeInterest("/12345678");
1059 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
1060
1061 face->sendInterest(*interest);
1062
1063 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1064 lp::Packet sent(transport->sentPackets.back().packet);
1065 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1066 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
1067}
1068
1069BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
1070{
1071 shared_ptr<Data> data = makeData("/12345678");
1072 data->setTag(make_shared<lp::CongestionMarkTag>(0));
1073
1074 face->sendData(*data);
1075
1076 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1077 lp::Packet sent(transport->sentPackets.back().packet);
1078 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1079 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
1080}
1081
1082BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
1083{
1084 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
1085 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
1086
1087 face->sendNack(nack);
1088
1089 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1090 lp::Packet sent(transport->sentPackets.back().packet);
1091 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1092 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
1093}
1094
1095BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
1096{
1097 shared_ptr<Interest> interest = makeInterest("/12345678");
1098 lp::Packet packet(interest->wireEncode());
1099 packet.set<lp::CongestionMarkField>(1);
1100
1101 transport->receivePacket(packet.wireEncode());
1102
1103 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
1104 shared_ptr<lp::CongestionMarkTag> tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
1105 BOOST_REQUIRE(tag != nullptr);
1106 BOOST_CHECK_EQUAL(*tag, 1);
1107}
1108
1109BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
1110{
1111 shared_ptr<Data> data = makeData("/12345678");
1112 lp::Packet packet(data->wireEncode());
1113 packet.set<lp::CongestionMarkField>(1);
1114
1115 transport->receivePacket(packet.wireEncode());
1116
1117 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
1118 shared_ptr<lp::CongestionMarkTag> tag = receivedData.back().getTag<lp::CongestionMarkTag>();
1119 BOOST_REQUIRE(tag != nullptr);
1120 BOOST_CHECK_EQUAL(*tag, 1);
1121}
1122
1123BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
1124{
1125 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
1126 lp::Packet packet;
1127 packet.set<lp::FragmentField>(std::make_pair(
1128 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1129 packet.set<lp::NackField>(nack.getHeader());
1130 packet.set<lp::CongestionMarkField>(1);
1131
1132 transport->receivePacket(packet.wireEncode());
1133
1134 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
1135 shared_ptr<lp::CongestionMarkTag> tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
1136 BOOST_REQUIRE(tag != nullptr);
1137 BOOST_CHECK_EQUAL(*tag, 1);
1138}
1139
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001140BOOST_AUTO_TEST_CASE(SendNonDiscovery)
1141{
1142 GenericLinkService::Options options;
1143 options.allowSelfLearning = true;
1144 initialize(options);
1145
1146 shared_ptr<Interest> interest = makeInterest("/12345678");
1147 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1148
1149 face->sendInterest(*interest);
1150
1151 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1152 lp::Packet sent(transport->sentPackets.back().packet);
1153 BOOST_CHECK(sent.has<lp::NonDiscoveryField>());
1154}
1155
1156BOOST_AUTO_TEST_CASE(SendNonDiscoveryDisabled)
1157{
1158 GenericLinkService::Options options;
1159 options.allowSelfLearning = false;
1160 initialize(options);
1161
1162 shared_ptr<Interest> interest = makeInterest("/12345678");
1163 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1164
1165 face->sendInterest(*interest);
1166
1167 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1168 lp::Packet sent(transport->sentPackets.back().packet);
1169 BOOST_CHECK(!sent.has<lp::NonDiscoveryField>());
1170}
1171
1172BOOST_AUTO_TEST_CASE(ReceiveNonDiscovery)
1173{
1174 GenericLinkService::Options options;
1175 options.allowSelfLearning = true;
1176 initialize(options);
1177
1178 shared_ptr<Interest> interest = makeInterest("/12345678");
1179 lp::Packet packet(interest->wireEncode());
1180 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1181
1182 transport->receivePacket(packet.wireEncode());
1183
1184 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
1185 shared_ptr<lp::NonDiscoveryTag> tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
1186 BOOST_CHECK(tag != nullptr);
1187}
1188
1189BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDisabled)
1190{
1191 GenericLinkService::Options options;
1192 options.allowSelfLearning = false;
1193 initialize(options);
1194
1195 shared_ptr<Interest> interest = makeInterest("/12345678");
1196 lp::Packet packet(interest->wireEncode());
1197 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1198
1199 transport->receivePacket(packet.wireEncode());
1200
1201 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1202 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
1203
1204 shared_ptr<lp::NonDiscoveryTag> tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
1205 BOOST_CHECK(tag == nullptr);
1206}
1207
1208BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropData)
1209{
1210 GenericLinkService::Options options;
1211 options.allowSelfLearning = true;
1212 initialize(options);
1213
1214 shared_ptr<Data> data = makeData("/12345678");
1215 lp::Packet packet(data->wireEncode());
1216 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1217
1218 transport->receivePacket(packet.wireEncode());
1219
1220 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1221 BOOST_CHECK(receivedData.empty());
1222}
1223
1224BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropNack)
1225{
1226 GenericLinkService::Options options;
1227 options.allowSelfLearning = true;
1228 initialize(options);
1229
1230 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
1231 lp::Packet packet;
1232 packet.set<lp::FragmentField>(std::make_pair(
1233 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1234 packet.set<lp::NackField>(nack.getHeader());
1235 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1236
1237 transport->receivePacket(packet.wireEncode());
1238
1239 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1240 BOOST_CHECK(receivedNacks.empty());
1241}
1242
1243BOOST_AUTO_TEST_CASE(SendPrefixAnnouncement)
1244{
1245 GenericLinkService::Options options;
1246 options.allowSelfLearning = true;
1247 initialize(options);
1248
1249 shared_ptr<Data> data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001250 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1251 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001252
1253 face->sendData(*data);
1254
1255 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1256 lp::Packet sent(transport->sentPackets.back().packet);
1257 BOOST_CHECK(sent.has<lp::PrefixAnnouncementField>());
1258}
1259
1260BOOST_AUTO_TEST_CASE(SendPrefixAnnouncementDisabled)
1261{
1262 GenericLinkService::Options options;
1263 options.allowSelfLearning = false;
1264 initialize(options);
1265
1266 shared_ptr<Data> data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001267 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1268 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001269
1270 face->sendData(*data);
1271
1272 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
1273 lp::Packet sent(transport->sentPackets.back().packet);
1274 BOOST_CHECK(!sent.has<lp::PrefixAnnouncementField>());
1275}
1276
1277BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncement)
1278{
1279 GenericLinkService::Options options;
1280 options.allowSelfLearning = true;
1281 initialize(options);
1282
1283 shared_ptr<Data> data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001284 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001285 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1286 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001287
1288 transport->receivePacket(packet.wireEncode());
1289
1290 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
1291 shared_ptr<lp::PrefixAnnouncementTag> tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangbce3cb52018-09-05 20:08:27 -07001292 BOOST_CHECK_EQUAL(tag->get().getPrefixAnn()->getAnnouncedName(), "/local/ndn/prefix");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001293}
1294
1295BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDisabled)
1296{
1297 GenericLinkService::Options options;
1298 options.allowSelfLearning = false;
1299 initialize(options);
1300
1301 shared_ptr<Data> data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001302 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001303 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1304 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001305
1306 transport->receivePacket(packet.wireEncode());
1307
1308 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1309 BOOST_CHECK_EQUAL(receivedData.size(), 1);
1310
Teng Liangbce3cb52018-09-05 20:08:27 -07001311 shared_ptr<lp::PrefixAnnouncementTag> tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001312 BOOST_CHECK(tag == nullptr);
1313}
1314
1315BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropInterest)
1316{
1317 GenericLinkService::Options options;
1318 options.allowSelfLearning = true;
1319 initialize(options);
1320
1321 shared_ptr<Interest> interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001322 lp::Packet packet(interest->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001323 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1324 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001325
1326 transport->receivePacket(packet.wireEncode());
1327
1328 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1329 BOOST_CHECK(receivedInterests.empty());
1330}
1331
1332BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropNack)
1333{
1334 GenericLinkService::Options options;
1335 options.allowSelfLearning = true;
1336 initialize(options);
1337
1338 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001339 lp::Packet packet;
1340 packet.set<lp::FragmentField>(std::make_pair(
1341 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1342 packet.set<lp::NackField>(nack.getHeader());
Teng Liangbce3cb52018-09-05 20:08:27 -07001343 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1344 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001345
1346 transport->receivePacket(packet.wireEncode());
1347
1348 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1349 BOOST_CHECK(receivedNacks.empty());
1350}
1351
Eric Newberryee400b52016-11-24 14:12:48 +00001352BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -07001353
Eric Newberrya1939ba2015-10-09 12:35:03 -07001354BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
1355
1356BOOST_AUTO_TEST_CASE(WrongTlvType)
1357{
1358 // Initialize with Options that disables all services
1359 GenericLinkService::Options options;
1360 options.allowLocalFields = false;
1361 initialize(options);
1362
1363 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
1364
Davide Pesavento16916ae2019-03-29 23:53:26 -04001365 BOOST_CHECK_NO_THROW(transport->receivePacket(std::move(packet)));
Eric Newberrya1939ba2015-10-09 12:35:03 -07001366
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001367 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001368 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1369 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1370 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1371}
1372
1373BOOST_AUTO_TEST_CASE(Unparsable)
1374{
1375 // Initialize with Options that disables all services
1376 GenericLinkService::Options options;
1377 options.allowLocalFields = false;
1378 initialize(options);
1379
1380 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
1381 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
1382
Davide Pesavento16916ae2019-03-29 23:53:26 -04001383 BOOST_CHECK_NO_THROW(transport->receivePacket(std::move(packet)));
Eric Newberrya1939ba2015-10-09 12:35:03 -07001384
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001385 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001386 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1387 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1388 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1389}
1390
1391BOOST_AUTO_TEST_SUITE_END() // Malformed
1392
1393
Eric Newberry86d31872015-09-23 16:24:59 -07001394BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
1395BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -07001396
1397} // namespace tests
1398} // namespace face
1399} // namespace nfd