blob: e4f042527564d63a7bb43ca8cb458049c093d115 [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 Pesavento1b22a8c2022-03-07 21:23:23 -05003 * Copyright (c) 2014-2022, 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>
Eric Newberrya98bf932015-09-21 00:58:47 -070037
38namespace nfd {
39namespace face {
40namespace tests {
41
42using namespace nfd::tests;
43
44BOOST_AUTO_TEST_SUITE(Face)
45
Junxiao Shicde37ad2015-12-24 01:02:05 -070046using nfd::Face;
47
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040048class GenericLinkServiceFixture : public GlobalIoTimeFixture, public KeyChainFixture
Eric Newberrya98bf932015-09-21 00:58:47 -070049{
50protected:
51 GenericLinkServiceFixture()
Eric Newberrya98bf932015-09-21 00:58:47 -070052 {
Eric Newberrya98bf932015-09-21 00:58:47 -070053 // By default, GenericLinkService is created with default options.
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040054 // Test cases may invoke initialize() with alternate options.
55 initialize({});
Eric Newberrya98bf932015-09-21 00:58:47 -070056 }
57
58 void
Eric Newberryb49313d2017-12-24 20:22:27 -070059 initialize(const GenericLinkService::Options& options,
60 ssize_t mtu = MTU_UNLIMITED,
61 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070062 {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040063 face = make_unique<Face>(make_unique<GenericLinkService>(options),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040064 make_unique<DummyTransport>("dummy://", "dummy://",
Davide Pesaventoe4b22382018-06-10 14:37:24 -040065 ndn::nfd::FACE_SCOPE_NON_LOCAL,
66 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
67 ndn::nfd::LINK_TYPE_POINT_TO_POINT,
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040068 mtu, sendQueueCapacity));
Eric Newberrya98bf932015-09-21 00:58:47 -070069 service = static_cast<GenericLinkService*>(face->getLinkService());
70 transport = static_cast<DummyTransport*>(face->getTransport());
71
72 face->afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000073 [this] (const Interest& interest, const EndpointId&) { receivedInterests.push_back(interest); });
Eric Newberrya98bf932015-09-21 00:58:47 -070074 face->afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000075 [this] (const Data& data, const EndpointId&) { receivedData.push_back(data); });
Eric Newberrya98bf932015-09-21 00:58:47 -070076 face->afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000077 [this] (const lp::Nack& nack, const EndpointId&) { receivedNacks.push_back(nack); });
Eric Newberrya98bf932015-09-21 00:58:47 -070078 }
79
Teng Liangbce3cb52018-09-05 20:08:27 -070080 lp::PrefixAnnouncementHeader
81 makePrefixAnnHeader(const Name& announcedName)
82 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040083 return lp::PrefixAnnouncementHeader{signPrefixAnn(makePrefixAnn(announcedName, 1_h),
84 m_keyChain, ndn::signingWithSha256())};
Teng Liangbce3cb52018-09-05 20:08:27 -070085 }
86
Eric Newberrya98bf932015-09-21 00:58:47 -070087protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070088 unique_ptr<Face> face;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040089 GenericLinkService* service = nullptr;
90 DummyTransport* transport = nullptr;
Eric Newberrya98bf932015-09-21 00:58:47 -070091 std::vector<Interest> receivedInterests;
92 std::vector<Data> receivedData;
93 std::vector<lp::Nack> receivedNacks;
94};
95
96BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
97
Eric Newberrya98bf932015-09-21 00:58:47 -070098BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
99
100BOOST_AUTO_TEST_CASE(SendInterest)
101{
Eric Newberry86d31872015-09-23 16:24:59 -0700102 // Initialize with Options that disables all services
103 GenericLinkService::Options options;
104 options.allowLocalFields = false;
105 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700106
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400107 auto interest1 = makeInterest("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700108 face->sendInterest(*interest1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700109
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700110 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700111 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700112 lp::Packet interest1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700113 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
114 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700115}
116
117BOOST_AUTO_TEST_CASE(SendData)
118{
Eric Newberry86d31872015-09-23 16:24:59 -0700119 // Initialize with Options that disables all services
120 GenericLinkService::Options options;
121 options.allowLocalFields = false;
122 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700123
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400124 auto data1 = makeData("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700125 face->sendData(*data1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700126
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700127 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700128 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700129 lp::Packet data1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700130 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
131 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700132}
133
Eric Newberrycb6551e2020-03-02 14:12:16 -0800134BOOST_AUTO_TEST_CASE(SendDataOverrideMtu)
135{
136 // Initialize with Options that disables all services and does not override MTU
137 GenericLinkService::Options options;
138 options.allowLocalFields = false;
139 initialize(options);
140
141 BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
142 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), MTU_UNLIMITED);
143 BOOST_CHECK_EQUAL(face->getMtu(), MTU_UNLIMITED);
144 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_UNLIMITED), false);
145 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_INVALID), false);
146 // Attempts to override MTU will fail when transport MTU is unlimited
147 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(4000), false);
148
149 // Initialize with Options that disables all services and overrides MTU (Transport MTU 8800)
150 options.overrideMtu = MIN_MTU;
151 initialize(options, ndn::MAX_NDN_PACKET_SIZE);
152
153 // Ensure effective MTU is override value
154 BOOST_CHECK_EQUAL(transport->getMtu(), ndn::MAX_NDN_PACKET_SIZE);
155 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), MIN_MTU);
156 BOOST_CHECK_EQUAL(face->getMtu(), MIN_MTU);
157
158 // Check MTU overrides with Transport MTU finite
159 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_UNLIMITED), false);
160 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_INVALID), false);
161 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MIN_MTU - 1), false);
162 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MIN_MTU), true);
163 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(4000), true);
164 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(20000), true);
165
166 // Send Data with less than MIN_MTU octets
167 auto data1 = makeData("/localhost");
168 BOOST_CHECK_LE(data1->wireEncode().size(), MIN_MTU);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700169 face->sendData(*data1);
Eric Newberrycb6551e2020-03-02 14:12:16 -0800170
171 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
172 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 0);
173
174 // Send Data with more than MIN_MTU octets
175 auto data2 = makeData("/localhost/test/1234567890/1234567890/1234567890/1234567890");
176 BOOST_CHECK_GT(data2->wireEncode().size(), MIN_MTU);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700177 face->sendData(*data2);
Eric Newberrycb6551e2020-03-02 14:12:16 -0800178
179 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 2);
180 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
181
182 // Override MTU greater than the Transport's MTU will not be utilized
183 options.overrideMtu = 5000;
184 initialize(options, 4000);
185 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), 4000);
186 BOOST_CHECK_EQUAL(face->getMtu(), 4000);
187}
188
Eric Newberrya98bf932015-09-21 00:58:47 -0700189BOOST_AUTO_TEST_CASE(SendNack)
190{
Eric Newberry86d31872015-09-23 16:24:59 -0700191 // Initialize with Options that disables all services
192 GenericLinkService::Options options;
193 options.allowLocalFields = false;
194 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700195
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400196 auto nack1 = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 323),
Junxiao Shi9d727852019-05-14 13:44:22 -0600197 lp::NackReason::NO_ROUTE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700198 face->sendNack(nack1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700199
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700200 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700201 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700202 lp::Packet nack1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700203 BOOST_CHECK(nack1pkt.has<lp::NackField>());
204 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
205 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700206}
207
208BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
209{
Eric Newberry86d31872015-09-23 16:24:59 -0700210 // Initialize with Options that disables all services
211 GenericLinkService::Options options;
212 options.allowLocalFields = false;
213 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700214
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400215 auto interest1 = makeInterest("/23Rd9hEiR");
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 transport->receivePacket(interest1->wireEncode());
217
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700218 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700219 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400220 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700221}
222
223BOOST_AUTO_TEST_CASE(ReceiveInterest)
224{
Eric Newberry86d31872015-09-23 16:24:59 -0700225 // Initialize with Options that disables all services
226 GenericLinkService::Options options;
227 options.allowLocalFields = false;
228 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700229
Junxiao Shi9d727852019-05-14 13:44:22 -0600230 auto interest1 = makeInterest("/23Rd9hEiR");
Eric Newberrya98bf932015-09-21 00:58:47 -0700231 lp::Packet lpPacket;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400232 lpPacket.set<lp::FragmentField>({interest1->wireEncode().begin(), interest1->wireEncode().end()});
Eric Newberrya98bf932015-09-21 00:58:47 -0700233 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
234
235 transport->receivePacket(lpPacket.wireEncode());
236
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700237 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700238 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400239 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700240}
241
242BOOST_AUTO_TEST_CASE(ReceiveBareData)
243{
Eric Newberry86d31872015-09-23 16:24:59 -0700244 // Initialize with Options that disables all services
245 GenericLinkService::Options options;
246 options.allowLocalFields = false;
247 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700248
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400249 auto data1 = makeData("/12345678");
Eric Newberrya98bf932015-09-21 00:58:47 -0700250 transport->receivePacket(data1->wireEncode());
251
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700252 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700253 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400254 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700255}
256
257BOOST_AUTO_TEST_CASE(ReceiveData)
258{
Eric Newberry86d31872015-09-23 16:24:59 -0700259 // Initialize with Options that disables all services
260 GenericLinkService::Options options;
261 options.allowLocalFields = false;
262 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700263
Junxiao Shi9d727852019-05-14 13:44:22 -0600264 auto data1 = makeData("/12345689");
Eric Newberrya98bf932015-09-21 00:58:47 -0700265 lp::Packet lpPacket;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400266 lpPacket.set<lp::FragmentField>({data1->wireEncode().begin(), data1->wireEncode().end()});
Eric Newberrya98bf932015-09-21 00:58:47 -0700267 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
268
269 transport->receivePacket(lpPacket.wireEncode());
270
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700271 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700272 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400273 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700274}
275
276BOOST_AUTO_TEST_CASE(ReceiveNack)
277{
Eric Newberry86d31872015-09-23 16:24:59 -0700278 // Initialize with Options that disables all services
279 GenericLinkService::Options options;
280 options.allowLocalFields = false;
281 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700282
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400283 auto nack1 = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 323),
284 lp::NackReason::NO_ROUTE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700285 lp::Packet lpPacket;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400286 lpPacket.set<lp::FragmentField>({nack1.getInterest().wireEncode().begin(),
287 nack1.getInterest().wireEncode().end()});
Eric Newberrya98bf932015-09-21 00:58:47 -0700288 lpPacket.set<lp::NackField>(nack1.getHeader());
289
290 transport->receivePacket(lpPacket.wireEncode());
291
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700292 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700293 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400294 BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), nack1.getReason());
295 BOOST_CHECK_EQUAL(receivedNacks.back().getInterest().wireEncode(), nack1.getInterest().wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700296}
297
Eric Newberrya1939ba2015-10-09 12:35:03 -0700298BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
299{
300 // Initialize with Options that disables all services
301 GenericLinkService::Options options;
302 options.allowLocalFields = false;
303 initialize(options);
304
305 lp::Packet lpPacket;
306 lpPacket.set<lp::SequenceField>(0);
307
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400308 transport->receivePacket(lpPacket.wireEncode());
Eric Newberrya1939ba2015-10-09 12:35:03 -0700309
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700310 // IDLE packet should be ignored, but is not an error
311 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700312 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
313 BOOST_CHECK_EQUAL(receivedData.size(), 0);
314 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
315}
316
Eric Newberrya98bf932015-09-21 00:58:47 -0700317BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
318
Eric Newberry86d31872015-09-23 16:24:59 -0700319BOOST_AUTO_TEST_SUITE(Fragmentation)
320
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700321BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
322{
323 // Initialize with Options that disable fragmentation
324 GenericLinkService::Options options;
325 options.allowFragmentation = false;
326 initialize(options);
327
328 transport->setMtu(55);
329
Junxiao Shi9d727852019-05-14 13:44:22 -0600330 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700331 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700332
333 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
334 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
335}
336
Eric Newberryf3ee8082020-01-28 13:44:18 -0800337// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
338// This test case ensures that packets are dropped if the MTU is zero
339BOOST_AUTO_TEST_CASE(FragmentationDisabledZeroMtuDrop)
340{
341 // Initialize with Options that disable fragmentation
342 GenericLinkService::Options options;
343 options.allowFragmentation = false;
344 initialize(options);
345
346 transport->setMtu(0);
347
348 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700349 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800350
351 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
352 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
353}
354
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700355BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
356{
357 // Initialize with Options that enable fragmentation
358 GenericLinkService::Options options;
359 options.allowFragmentation = true;
360 initialize(options);
361
362 transport->setMtu(MTU_UNLIMITED);
363
Junxiao Shi9d727852019-05-14 13:44:22 -0600364 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700365 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700366
367 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
368}
369
370BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
371{
372 // Initialize with Options that enable fragmentation
373 GenericLinkService::Options options;
374 options.allowFragmentation = true;
375 initialize(options);
376
377 transport->setMtu(105);
378
Junxiao Shi9d727852019-05-14 13:44:22 -0600379 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700380 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700381
382 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
383}
384
385BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
386{
387 // Initialize with Options that enable fragmentation
388 GenericLinkService::Options options;
389 options.allowFragmentation = true;
390 initialize(options);
391
392 transport->setMtu(60);
393
Junxiao Shi9d727852019-05-14 13:44:22 -0600394 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700395 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700396
397 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
398}
399
Eric Newberryf3ee8082020-01-28 13:44:18 -0800400// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
401// This test case ensures that packets are dropped if the MTU is zero
402BOOST_AUTO_TEST_CASE(FragmentationZeroMtuDrop)
403{
404 // Initialize with Options that enable fragmentation
405 GenericLinkService::Options options;
406 options.allowFragmentation = true;
407 initialize(options);
408
409 transport->setMtu(0);
410
411 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700412 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800413
414 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
415 BOOST_CHECK_EQUAL(service->getCounters().nFragmentationErrors, 1);
416}
417
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700418BOOST_AUTO_TEST_CASE(ReassembleFragments)
419{
420 // Initialize with Options that enables reassembly
421 GenericLinkService::Options options;
422 options.allowReassembly = true;
423 initialize(options);
424
Junxiao Shi9d727852019-05-14 13:44:22 -0600425 auto interest = makeInterest(
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700426 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
427 lp::Packet packet(interest->wireEncode());
428
429 // fragment the packet
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400430 LpFragmenter fragmenter({});
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400431 auto [isOk, frags] = fragmenter.fragmentPacket(packet, 100);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700432 BOOST_REQUIRE(isOk);
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400433 BOOST_TEST(frags.size() > 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700434
435 // receive the fragments
436 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
437 size_t sequence = 1000 + fragIndex;
438 frags[fragIndex].add<lp::SequenceField>(sequence);
439
440 transport->receivePacket(frags[fragIndex].wireEncode());
441
442 if (fragIndex > 0) {
443 BOOST_CHECK(receivedInterests.empty());
444 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
445 }
446 else {
447 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400448 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest->wireEncode());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700449 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
450 }
451 }
452}
453
Eric Newberry86d31872015-09-23 16:24:59 -0700454BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
455{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700456 // Initialize with Options that disables reassembly
457 GenericLinkService::Options options;
458 options.allowReassembly = false;
459 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700460
Junxiao Shi9d727852019-05-14 13:44:22 -0600461 auto interest = makeInterest("/IgFe6NvH");
Eric Newberry86d31872015-09-23 16:24:59 -0700462 lp::Packet packet(interest->wireEncode());
463 packet.set<lp::FragIndexField>(140);
464
465 transport->receivePacket(packet.wireEncode());
466
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700467 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700468 BOOST_CHECK(receivedInterests.empty());
469}
470
471BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
472{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700473 // Initialize with Options that disables reassembly
474 GenericLinkService::Options options;
475 options.allowReassembly = false;
476 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700477
Junxiao Shi9d727852019-05-14 13:44:22 -0600478 auto interest = makeInterest("/SeGmEjvIVX");
Eric Newberry86d31872015-09-23 16:24:59 -0700479 lp::Packet packet(interest->wireEncode());
480 packet.set<lp::FragCountField>(276);
481
482 transport->receivePacket(packet.wireEncode());
483
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700484 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700485 BOOST_CHECK(receivedInterests.empty());
486}
487
488BOOST_AUTO_TEST_SUITE_END() // Fragmentation
489
Eric Newberry185ab292017-03-28 06:45:39 +0000490BOOST_AUTO_TEST_SUITE(Reliability)
491
Eric Newberry7b0071e2017-07-03 17:33:31 +0000492BOOST_AUTO_TEST_CASE(SendInterest)
Eric Newberry185ab292017-03-28 06:45:39 +0000493{
494 // Initialize with Options that enables reliability
495 GenericLinkService::Options options;
496 options.allowLocalFields = false;
497 options.reliabilityOptions.isEnabled = true;
498 initialize(options);
499
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400500 auto interest1 = makeInterest("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700501 face->sendInterest(*interest1);
Eric Newberry185ab292017-03-28 06:45:39 +0000502
503 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
504 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700505 lp::Packet interest1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000506 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000507 BOOST_CHECK(interest1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000508}
509
Eric Newberry7b0071e2017-07-03 17:33:31 +0000510BOOST_AUTO_TEST_CASE(SendData)
Eric Newberry185ab292017-03-28 06:45:39 +0000511{
512 // Initialize with Options that enables reliability
513 GenericLinkService::Options options;
514 options.allowLocalFields = false;
515 options.reliabilityOptions.isEnabled = true;
516 initialize(options);
517
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400518 auto data1 = makeData("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700519 face->sendData(*data1);
Eric Newberry185ab292017-03-28 06:45:39 +0000520
521 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
522 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700523 lp::Packet data1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000524 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000525 BOOST_CHECK(data1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000526}
527
Eric Newberry7b0071e2017-07-03 17:33:31 +0000528BOOST_AUTO_TEST_CASE(SendNack)
Eric Newberry185ab292017-03-28 06:45:39 +0000529{
530 // Initialize with Options that enables reliability
531 GenericLinkService::Options options;
532 options.allowLocalFields = false;
533 options.reliabilityOptions.isEnabled = true;
534 initialize(options);
535
Davide Pesaventob7bfcb92022-05-22 23:55:23 -0400536 auto nack1 = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 323),
Junxiao Shi9d727852019-05-14 13:44:22 -0600537 lp::NackReason::NO_ROUTE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700538 face->sendNack(nack1);
Eric Newberry185ab292017-03-28 06:45:39 +0000539
540 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
541 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700542 lp::Packet nack1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000543 BOOST_CHECK(nack1pkt.has<lp::NackField>());
544 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000545 BOOST_CHECK(nack1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000546}
547
Eric Newberry32f7eac2020-02-07 14:40:17 -0800548BOOST_AUTO_TEST_CASE(DropDuplicatePacket)
549{
550 // Initialize with Options that enables reliability
551 GenericLinkService::Options options;
552 options.allowLocalFields = false;
553 options.reliabilityOptions.isEnabled = true;
554 initialize(options);
555
556 Interest interest("/test/prefix");
Eric Newberry32f7eac2020-02-07 14:40:17 -0800557 lp::Packet pkt1;
558 pkt1.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
559 pkt1.add<lp::SequenceField>(7);
560 pkt1.add<lp::TxSequenceField>(12);
561 transport->receivePacket(pkt1.wireEncode());
562 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
563 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 0);
564
565 lp::Packet pkt2;
566 pkt2.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
567 pkt2.add<lp::SequenceField>(7);
568 pkt2.add<lp::TxSequenceField>(13);
569 transport->receivePacket(pkt2.wireEncode());
570 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
571 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 1);
572}
573
Eric Newberry185ab292017-03-28 06:45:39 +0000574BOOST_AUTO_TEST_SUITE_END() // Reliability
Eric Newberry86d31872015-09-23 16:24:59 -0700575
Eric Newberryb49313d2017-12-24 20:22:27 -0700576// congestion detection and marking
577BOOST_AUTO_TEST_SUITE(CongestionMark)
578
579BOOST_AUTO_TEST_CASE(NoCongestion)
580{
581 GenericLinkService::Options options;
582 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400583 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700584 initialize(options, MTU_UNLIMITED, 65536);
585 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
586 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
587 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
588
Junxiao Shi9d727852019-05-14 13:44:22 -0600589 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700590
591 // congestion threshold will be 32768 bytes, since min(65536, 65536 / 2) = 32768 bytes
592
593 // no congestion
594 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700595 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700596 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700597 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700598 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
599 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
600 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
601 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
602
603 // no congestion
604 transport->setSendQueueLength(32768);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700605 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700606 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700607 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700608 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
609 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
610 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
611 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
612}
613
614BOOST_AUTO_TEST_CASE(CongestionCoDel)
615{
616 GenericLinkService::Options options;
617 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400618 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700619 initialize(options, MTU_UNLIMITED, 65536);
620 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700621 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
622 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
623
Junxiao Shi9d727852019-05-14 13:44:22 -0600624 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700625
Klaus Schneider380668b2019-10-02 01:21:13 -0700626 // first congested packet, will not be marked
627 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700628 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700629 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700630 lp::Packet pkt0(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700631 BOOST_REQUIRE_EQUAL(pkt0.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400632 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700633 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700634
635 time::nanoseconds markingInterval(
636 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
637 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
638
639 advanceClocks(markingInterval + 1_ms);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700640 face->sendInterest(*interest);
Teng Liang13d582a2020-07-21 20:23:11 -0700641 lp::Packet pkt1(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700642
643 // First congestion mark appears after one interval (100 ms)
644 BOOST_REQUIRE_EQUAL(pkt1.count<lp::CongestionMarkField>(), 1);
645 BOOST_CHECK_EQUAL(pkt1.get<lp::CongestionMarkField>(), 1);
Eric Newberryb49313d2017-12-24 20:22:27 -0700646 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
647 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
648
649 // advance clock to half of marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700650 advanceClocks(markingInterval / 2); // 50ms
Eric Newberryb49313d2017-12-24 20:22:27 -0700651
652 // second congested packet, but within marking interval, will not be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700653 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700654 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700655 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700656 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700657 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700658
659 markingInterval = time::nanoseconds(
660 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
661 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
662 nextMarkTime += markingInterval;
663
Eric Newberryb49313d2017-12-24 20:22:27 -0700664 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700665 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
666 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
667
668 // advance clocks past end of initial interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700669 this->advanceClocks((markingInterval / 2) + 1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700670
671 // first congested packet after waiting marking interval, will be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700672 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700673 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700674 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 4);
Teng Liang13d582a2020-07-21 20:23:11 -0700675 lp::Packet pkt3(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700676 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 1);
677 BOOST_CHECK_EQUAL(pkt3.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700678 markingInterval = time::nanoseconds(
679 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
680 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700681 nextMarkTime += markingInterval;
682 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700683 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
684 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
685
686 // advance clock partway through current marking interval
Klaus Schneider380668b2019-10-02 01:21:13 -0700687 this->advanceClocks(markingInterval - 20_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700688
689 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700690 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700691 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700692 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 5);
Teng Liang13d582a2020-07-21 20:23:11 -0700693 lp::Packet pkt4(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700694 BOOST_CHECK_EQUAL(pkt4.count<lp::CongestionMarkField>(), 0);
695 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700696 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
697 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
698
699 // advance clocks past end of current marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700700 this->advanceClocks(21_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700701
702 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700703 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700704 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700705 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 6);
Teng Liang13d582a2020-07-21 20:23:11 -0700706 lp::Packet pkt5(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700707 BOOST_REQUIRE_EQUAL(pkt5.count<lp::CongestionMarkField>(), 1);
708 BOOST_CHECK_EQUAL(pkt5.get<lp::CongestionMarkField>(), 1);
709 markingInterval = time::nanoseconds(
710 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700711 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700712 nextMarkTime += markingInterval;
713 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700714 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
715 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
716
Davide Pesavento14e71f02019-03-28 17:35:25 -0400717 this->advanceClocks(1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700718
719 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700720 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700721 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700722 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 7);
Teng Liang13d582a2020-07-21 20:23:11 -0700723 lp::Packet pkt6(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700724 BOOST_CHECK_EQUAL(pkt6.count<lp::CongestionMarkField>(), 0);
725 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700726 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
727 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
728
729 this->advanceClocks(markingInterval);
730
731 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700732 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700733 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700734 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 8);
Teng Liang13d582a2020-07-21 20:23:11 -0700735 lp::Packet pkt7(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700736 BOOST_REQUIRE_EQUAL(pkt7.count<lp::CongestionMarkField>(), 1);
737 BOOST_CHECK_EQUAL(pkt7.get<lp::CongestionMarkField>(), 1);
738 markingInterval = time::nanoseconds(
739 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700740 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700741 nextMarkTime += markingInterval;
742 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700743 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 4);
744 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
745
746 // no more congestion
747 transport->setSendQueueLength(30000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700748 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700749 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 9);
Teng Liang13d582a2020-07-21 20:23:11 -0700750 lp::Packet pkt8(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700751 BOOST_CHECK_EQUAL(pkt8.count<lp::CongestionMarkField>(), 0);
752 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700753 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
754 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
755
Davide Pesavento14e71f02019-03-28 17:35:25 -0400756 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700757
758 // send queue congested again, but can't mark packet because within one full interval of last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700759 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700760 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700761 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 10);
Teng Liang13d582a2020-07-21 20:23:11 -0700762 lp::Packet pkt9(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700763 BOOST_CHECK_EQUAL(pkt9.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700764 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
765 markingInterval = time::nanoseconds(
766 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
767 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
768 nextMarkTime = time::steady_clock::now() + markingInterval;
769 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700770 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
771 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
772
773 // advance clock past full 100ms interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700774 this->advanceClocks(markingInterval + 2_ms);
775 BOOST_CHECK_GT(time::steady_clock::now(), nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700776
Klaus Schneider380668b2019-10-02 01:21:13 -0700777 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700778 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700779 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 11);
Teng Liang13d582a2020-07-21 20:23:11 -0700780 lp::Packet pkt10(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700781 BOOST_REQUIRE_EQUAL(pkt10.count<lp::CongestionMarkField>(), 1);
782 BOOST_CHECK_EQUAL(pkt10.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700783 markingInterval = time::nanoseconds(
784 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
785 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
786 nextMarkTime += markingInterval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700787 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700788 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
789 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
790
791 // advance clock partway through 100ms marking interval
Davide Pesavento14e71f02019-03-28 17:35:25 -0400792 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700793
794 // not marked since within 100ms window before can mark again
Klaus Schneider380668b2019-10-02 01:21:13 -0700795 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700796 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700797 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 12);
Teng Liang13d582a2020-07-21 20:23:11 -0700798 lp::Packet pkt11(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700799 BOOST_CHECK_EQUAL(pkt11.count<lp::CongestionMarkField>(), 0);
800 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700801 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
802 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
803
804 // advance clocks past m_nextMarkTime
Davide Pesavento14e71f02019-03-28 17:35:25 -0400805 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700806
807 // markable packet, queue length still above threshold
Klaus Schneider380668b2019-10-02 01:21:13 -0700808 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700809 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700810 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 13);
Teng Liang13d582a2020-07-21 20:23:11 -0700811 lp::Packet pkt12(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700812 BOOST_REQUIRE_EQUAL(pkt12.count<lp::CongestionMarkField>(), 1);
813 BOOST_CHECK_EQUAL(pkt12.get<lp::CongestionMarkField>(), 1);
814 markingInterval = time::nanoseconds(
815 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700816 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700817 nextMarkTime += markingInterval;
818 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700819 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
820 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
821
822 // no more congestion
823 transport->setSendQueueLength(10000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700824 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700825 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 14);
Teng Liang13d582a2020-07-21 20:23:11 -0700826 lp::Packet pkt13(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700827 BOOST_CHECK_EQUAL(pkt13.count<lp::CongestionMarkField>(), 0);
828 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700829 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
830 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
831
832 // advance clocks past one full interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700833 this->advanceClocks(102_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700834
835 // start congestion again
Klaus Schneider380668b2019-10-02 01:21:13 -0700836 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700837 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700838 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 15);
Teng Liang13d582a2020-07-21 20:23:11 -0700839 lp::Packet pkt14(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700840 BOOST_REQUIRE_EQUAL(pkt14.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400841 nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700842 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700843 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
844 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700845
846 // no more congestion, cancel marking interval
847 transport->setSendQueueLength(5000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700848 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700849 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 16);
Teng Liang13d582a2020-07-21 20:23:11 -0700850 lp::Packet pkt15(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700851 BOOST_CHECK_EQUAL(pkt15.count<lp::CongestionMarkField>(), 0);
852 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700853 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700854 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700855}
856
857BOOST_AUTO_TEST_CASE(DefaultThreshold)
858{
859 GenericLinkService::Options options;
860 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400861 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700862 initialize(options, MTU_UNLIMITED, QUEUE_UNSUPPORTED);
863 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
864 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
865 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
866
Junxiao Shi9d727852019-05-14 13:44:22 -0600867 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700868
869 // congestion threshold will be 65536 bytes, since the transport reports that it cannot measure
870 // the queue capacity
871
872 // no congestion
873 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700874 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700875 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700876 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700877 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
878 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
879 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
880 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
881
882 // no congestion
883 transport->setSendQueueLength(65536);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700884 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700885 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700886 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700887 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
888 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
889 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
890 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
891
Klaus Schneider380668b2019-10-02 01:21:13 -0700892 // first congested (not marked yet) packet
Eric Newberryb49313d2017-12-24 20:22:27 -0700893 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700894 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700895 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700896 lp::Packet pkt3(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700897 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400898 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700899 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700900 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
901 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
Eric Newberryb49313d2017-12-24 20:22:27 -0700902}
903
904BOOST_AUTO_TEST_SUITE_END() // CongestionMark
905
Eric Newberryee400b52016-11-24 14:12:48 +0000906BOOST_AUTO_TEST_SUITE(LpFields)
Eric Newberrya98bf932015-09-21 00:58:47 -0700907
Eric Newberrya98bf932015-09-21 00:58:47 -0700908BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
909{
Eric Newberry86d31872015-09-23 16:24:59 -0700910 // Initialize with Options that enables local fields
911 GenericLinkService::Options options;
912 options.allowLocalFields = true;
913 initialize(options);
914
Junxiao Shi9d727852019-05-14 13:44:22 -0600915 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700916 lp::Packet packet(interest->wireEncode());
917 packet.set<lp::NextHopFaceIdField>(1000);
918
919 transport->receivePacket(packet.wireEncode());
920
921 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400922 auto tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +0000923 BOOST_REQUIRE(tag != nullptr);
924 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700925}
926
927BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
928{
Eric Newberry86d31872015-09-23 16:24:59 -0700929 // Initialize with Options that disables local fields
930 GenericLinkService::Options options;
931 options.allowLocalFields = false;
932 initialize(options);
933
Junxiao Shi9d727852019-05-14 13:44:22 -0600934 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700935 lp::Packet packet(interest->wireEncode());
936 packet.set<lp::NextHopFaceIdField>(1000);
937
938 transport->receivePacket(packet.wireEncode());
939
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700940 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700941 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700942}
943
944BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
945{
Eric Newberry86d31872015-09-23 16:24:59 -0700946 // Initialize with Options that enables local fields
947 GenericLinkService::Options options;
948 options.allowLocalFields = true;
949 initialize(options);
950
Junxiao Shi9d727852019-05-14 13:44:22 -0600951 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700952 lp::Packet packet(data->wireEncode());
953 packet.set<lp::NextHopFaceIdField>(1000);
954
955 transport->receivePacket(packet.wireEncode());
956
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700957 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700958 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700959}
960
961BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
962{
Eric Newberry86d31872015-09-23 16:24:59 -0700963 // Initialize with Options that enables local fields
964 GenericLinkService::Options options;
965 options.allowLocalFields = true;
966 initialize(options);
967
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400968 auto nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
969 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -0700970 lp::Packet packet;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -0400971 packet.set<lp::FragmentField>({nack.getInterest().wireEncode().begin(),
972 nack.getInterest().wireEncode().end()});
Eric Newberry86d31872015-09-23 16:24:59 -0700973 packet.set<lp::NackField>(nack.getHeader());
974 packet.set<lp::NextHopFaceIdField>(1000);
975
976 transport->receivePacket(packet.wireEncode());
977
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700978 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700979 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700980}
981
Junxiao Shi6eb02712017-05-27 22:48:02 +0000982BOOST_AUTO_TEST_CASE(ReceiveCachePolicy)
Eric Newberrya98bf932015-09-21 00:58:47 -0700983{
Junxiao Shi6eb02712017-05-27 22:48:02 +0000984 // Initialize with Options that disables local fields
Eric Newberry86d31872015-09-23 16:24:59 -0700985 GenericLinkService::Options options;
Junxiao Shi6eb02712017-05-27 22:48:02 +0000986 options.allowLocalFields = false;
Eric Newberry86d31872015-09-23 16:24:59 -0700987 initialize(options);
Junxiao Shi6eb02712017-05-27 22:48:02 +0000988 // CachePolicy is unprivileged and does not require allowLocalFields option.
Eric Newberry86d31872015-09-23 16:24:59 -0700989
Junxiao Shi9d727852019-05-14 13:44:22 -0600990 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700991 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000992 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700993
994 transport->receivePacket(packet.wireEncode());
995
996 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400997 auto tag = receivedData.back().getTag<lp::CachePolicyTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +0000998 BOOST_REQUIRE(tag != nullptr);
999 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -07001000}
1001
Junxiao Shi6eb02712017-05-27 22:48:02 +00001002BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropInterest)
Eric Newberrya98bf932015-09-21 00:58:47 -07001003{
Eric Newberry86d31872015-09-23 16:24:59 -07001004 // Initialize with Options that enables local fields
1005 GenericLinkService::Options options;
1006 options.allowLocalFields = true;
1007 initialize(options);
1008
Junxiao Shi9d727852019-05-14 13:44:22 -06001009 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001010 lp::Packet packet(interest->wireEncode());
1011 lp::CachePolicy policy;
1012 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1013 packet.set<lp::CachePolicyField>(policy);
1014
1015 transport->receivePacket(packet.wireEncode());
1016
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001017 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001018 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -07001019}
1020
Junxiao Shi6eb02712017-05-27 22:48:02 +00001021BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropNack)
Eric Newberrya98bf932015-09-21 00:58:47 -07001022{
Eric Newberry86d31872015-09-23 16:24:59 -07001023 // Initialize with Options that enables local fields
1024 GenericLinkService::Options options;
1025 options.allowLocalFields = true;
1026 initialize(options);
1027
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04001028 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
Junxiao Shi9d727852019-05-14 13:44:22 -06001029 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -07001030 lp::Packet packet(nack.getInterest().wireEncode());
1031 packet.set<lp::NackField>(nack.getHeader());
1032 lp::CachePolicy policy;
1033 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1034 packet.set<lp::CachePolicyField>(policy);
1035
1036 transport->receivePacket(packet.wireEncode());
1037
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001038 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001039 BOOST_CHECK(receivedNacks.empty());
1040}
1041
1042BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
1043{
1044 // Initialize with Options that enables local fields
1045 GenericLinkService::Options options;
1046 options.allowLocalFields = true;
1047 initialize(options);
1048
Junxiao Shi9d727852019-05-14 13:44:22 -06001049 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001050 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001051
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001052 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001053
1054 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001055 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001056 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
1057 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
1058}
1059
1060BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
1061{
1062 // Initialize with Options that disables local fields
1063 GenericLinkService::Options options;
1064 options.allowLocalFields = false;
1065 initialize(options);
1066
Junxiao Shi9d727852019-05-14 13:44:22 -06001067 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001068 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001069
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001070 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001071
1072 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001073 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001074 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
1075}
1076
1077BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
1078{
1079 // Initialize with Options that enables local fields
1080 GenericLinkService::Options options;
1081 options.allowLocalFields = true;
1082 initialize(options);
1083
Junxiao Shi9d727852019-05-14 13:44:22 -06001084 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001085 lp::Packet packet(interest->wireEncode());
1086 packet.set<lp::IncomingFaceIdField>(1000);
1087
1088 transport->receivePacket(packet.wireEncode());
1089
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001090 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001091 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001092 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001093}
1094
1095BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
1096{
1097 // Initialize with Options that enables local fields
1098 GenericLinkService::Options options;
1099 options.allowLocalFields = true;
1100 initialize(options);
1101
Junxiao Shi9d727852019-05-14 13:44:22 -06001102 auto data = makeData("/z1megUh9Bj");
Eric Newberry86d31872015-09-23 16:24:59 -07001103 lp::Packet packet(data->wireEncode());
1104 packet.set<lp::IncomingFaceIdField>(1000);
1105
1106 transport->receivePacket(packet.wireEncode());
1107
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001108 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001109 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001110 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001111}
1112
1113BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
1114{
1115 // Initialize with Options that enables local fields
1116 GenericLinkService::Options options;
1117 options.allowLocalFields = true;
1118 initialize(options);
1119
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04001120 lp::Nack nack = makeNack(*makeInterest("/TPAhdiHz", false, std::nullopt, 278),
Junxiao Shi9d727852019-05-14 13:44:22 -06001121 lp::NackReason::CONGESTION);
Eric Newberry86d31872015-09-23 16:24:59 -07001122 lp::Packet packet(nack.getInterest().wireEncode());
1123 packet.set<lp::NackField>(nack.getHeader());
1124 packet.set<lp::IncomingFaceIdField>(1000);
1125
1126 transport->receivePacket(packet.wireEncode());
1127
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001128 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001129 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001130 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -07001131}
1132
Eric Newberryee400b52016-11-24 14:12:48 +00001133BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
1134{
Junxiao Shi9d727852019-05-14 13:44:22 -06001135 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001136 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
1137
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001138 face->sendInterest(*interest);
Eric Newberryee400b52016-11-24 14:12:48 +00001139
1140 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001141 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001142 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1143 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
1144}
1145
1146BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
1147{
Junxiao Shi9d727852019-05-14 13:44:22 -06001148 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001149 data->setTag(make_shared<lp::CongestionMarkTag>(0));
1150
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001151 face->sendData(*data);
Eric Newberryee400b52016-11-24 14:12:48 +00001152
1153 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001154 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001155 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1156 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
1157}
1158
1159BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
1160{
Davide Pesaventob7bfcb92022-05-22 23:55:23 -04001161 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
Junxiao Shi9d727852019-05-14 13:44:22 -06001162 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001163 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
1164
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001165 face->sendNack(nack);
Eric Newberryee400b52016-11-24 14:12:48 +00001166
1167 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001168 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001169 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1170 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
1171}
1172
1173BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
1174{
Junxiao Shi9d727852019-05-14 13:44:22 -06001175 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001176 lp::Packet packet(interest->wireEncode());
1177 packet.set<lp::CongestionMarkField>(1);
1178
1179 transport->receivePacket(packet.wireEncode());
1180
1181 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001182 auto tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001183 BOOST_REQUIRE(tag != nullptr);
1184 BOOST_CHECK_EQUAL(*tag, 1);
1185}
1186
1187BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
1188{
Junxiao Shi9d727852019-05-14 13:44:22 -06001189 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001190 lp::Packet packet(data->wireEncode());
1191 packet.set<lp::CongestionMarkField>(1);
1192
1193 transport->receivePacket(packet.wireEncode());
1194
1195 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001196 auto tag = receivedData.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001197 BOOST_REQUIRE(tag != nullptr);
1198 BOOST_CHECK_EQUAL(*tag, 1);
1199}
1200
1201BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
1202{
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001203 auto nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
1204 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001205 lp::Packet packet;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001206 packet.set<lp::FragmentField>({nack.getInterest().wireEncode().begin(),
1207 nack.getInterest().wireEncode().end()});
Eric Newberryee400b52016-11-24 14:12:48 +00001208 packet.set<lp::NackField>(nack.getHeader());
1209 packet.set<lp::CongestionMarkField>(1);
1210
1211 transport->receivePacket(packet.wireEncode());
1212
1213 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001214 auto tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001215 BOOST_REQUIRE(tag != nullptr);
1216 BOOST_CHECK_EQUAL(*tag, 1);
1217}
1218
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001219BOOST_AUTO_TEST_CASE(SendNonDiscovery)
1220{
1221 GenericLinkService::Options options;
1222 options.allowSelfLearning = true;
1223 initialize(options);
1224
Junxiao Shi9d727852019-05-14 13:44:22 -06001225 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001226 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1227
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001228 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001229
1230 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001231 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001232 BOOST_CHECK(sent.has<lp::NonDiscoveryField>());
1233}
1234
1235BOOST_AUTO_TEST_CASE(SendNonDiscoveryDisabled)
1236{
1237 GenericLinkService::Options options;
1238 options.allowSelfLearning = false;
1239 initialize(options);
1240
Junxiao Shi9d727852019-05-14 13:44:22 -06001241 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001242 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1243
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001244 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001245
1246 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001247 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001248 BOOST_CHECK(!sent.has<lp::NonDiscoveryField>());
1249}
1250
1251BOOST_AUTO_TEST_CASE(ReceiveNonDiscovery)
1252{
1253 GenericLinkService::Options options;
1254 options.allowSelfLearning = true;
1255 initialize(options);
1256
Junxiao Shi9d727852019-05-14 13:44:22 -06001257 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001258 lp::Packet packet(interest->wireEncode());
1259 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1260
1261 transport->receivePacket(packet.wireEncode());
1262
1263 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001264 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001265 BOOST_CHECK(tag != nullptr);
1266}
1267
1268BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDisabled)
1269{
1270 GenericLinkService::Options options;
1271 options.allowSelfLearning = false;
1272 initialize(options);
1273
Junxiao Shi9d727852019-05-14 13:44:22 -06001274 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001275 lp::Packet packet(interest->wireEncode());
1276 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1277
1278 transport->receivePacket(packet.wireEncode());
1279
1280 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1281 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001282 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001283 BOOST_CHECK(tag == nullptr);
1284}
1285
1286BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropData)
1287{
1288 GenericLinkService::Options options;
1289 options.allowSelfLearning = true;
1290 initialize(options);
1291
Junxiao Shi9d727852019-05-14 13:44:22 -06001292 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001293 lp::Packet packet(data->wireEncode());
1294 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1295
1296 transport->receivePacket(packet.wireEncode());
1297
1298 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1299 BOOST_CHECK(receivedData.empty());
1300}
1301
1302BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropNack)
1303{
1304 GenericLinkService::Options options;
1305 options.allowSelfLearning = true;
1306 initialize(options);
1307
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001308 auto nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
1309 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001310 lp::Packet packet;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001311 packet.set<lp::FragmentField>({nack.getInterest().wireEncode().begin(),
1312 nack.getInterest().wireEncode().end()});
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001313 packet.set<lp::NackField>(nack.getHeader());
1314 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1315
1316 transport->receivePacket(packet.wireEncode());
1317
1318 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1319 BOOST_CHECK(receivedNacks.empty());
1320}
1321
1322BOOST_AUTO_TEST_CASE(SendPrefixAnnouncement)
1323{
1324 GenericLinkService::Options options;
1325 options.allowSelfLearning = true;
1326 initialize(options);
1327
Junxiao Shi9d727852019-05-14 13:44:22 -06001328 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001329 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1330 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001331
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001332 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001333
1334 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001335 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001336 BOOST_CHECK(sent.has<lp::PrefixAnnouncementField>());
1337}
1338
1339BOOST_AUTO_TEST_CASE(SendPrefixAnnouncementDisabled)
1340{
1341 GenericLinkService::Options options;
1342 options.allowSelfLearning = false;
1343 initialize(options);
1344
Junxiao Shi9d727852019-05-14 13:44:22 -06001345 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001346 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1347 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001348
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001349 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001350
1351 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001352 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001353 BOOST_CHECK(!sent.has<lp::PrefixAnnouncementField>());
1354}
1355
1356BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncement)
1357{
1358 GenericLinkService::Options options;
1359 options.allowSelfLearning = true;
1360 initialize(options);
1361
Junxiao Shi9d727852019-05-14 13:44:22 -06001362 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001363 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001364 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1365 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001366
1367 transport->receivePacket(packet.wireEncode());
1368
1369 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001370 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangbce3cb52018-09-05 20:08:27 -07001371 BOOST_CHECK_EQUAL(tag->get().getPrefixAnn()->getAnnouncedName(), "/local/ndn/prefix");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001372}
1373
1374BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDisabled)
1375{
1376 GenericLinkService::Options options;
1377 options.allowSelfLearning = false;
1378 initialize(options);
1379
Junxiao Shi9d727852019-05-14 13:44:22 -06001380 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001381 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001382 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1383 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001384
1385 transport->receivePacket(packet.wireEncode());
1386
1387 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1388 BOOST_CHECK_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001389 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001390 BOOST_CHECK(tag == nullptr);
1391}
1392
1393BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropInterest)
1394{
1395 GenericLinkService::Options options;
1396 options.allowSelfLearning = true;
1397 initialize(options);
1398
Junxiao Shi9d727852019-05-14 13:44:22 -06001399 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001400 lp::Packet packet(interest->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001401 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1402 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001403
1404 transport->receivePacket(packet.wireEncode());
1405
1406 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1407 BOOST_CHECK(receivedInterests.empty());
1408}
1409
1410BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropNack)
1411{
1412 GenericLinkService::Options options;
1413 options.allowSelfLearning = true;
1414 initialize(options);
1415
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001416 auto nack = makeNack(*makeInterest("/localhost/test", false, std::nullopt, 123),
1417 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001418 lp::Packet packet;
Davide Pesaventoa3a7a4e2022-05-29 16:06:22 -04001419 packet.set<lp::FragmentField>({nack.getInterest().wireEncode().begin(),
1420 nack.getInterest().wireEncode().end()});
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001421 packet.set<lp::NackField>(nack.getHeader());
Teng Liangbce3cb52018-09-05 20:08:27 -07001422 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1423 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001424
1425 transport->receivePacket(packet.wireEncode());
1426
1427 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1428 BOOST_CHECK(receivedNacks.empty());
1429}
1430
Eric Newberryee400b52016-11-24 14:12:48 +00001431BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -07001432
Eric Newberrya1939ba2015-10-09 12:35:03 -07001433BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
1434
1435BOOST_AUTO_TEST_CASE(WrongTlvType)
1436{
1437 // Initialize with Options that disables all services
1438 GenericLinkService::Options options;
1439 options.allowLocalFields = false;
1440 initialize(options);
1441
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001442 auto packet = ndn::encoding::makeEmptyBlock(tlv::Name);
1443 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001444
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001445 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001446 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1447 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1448 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1449}
1450
1451BOOST_AUTO_TEST_CASE(Unparsable)
1452{
1453 // Initialize with Options that disables all services
1454 GenericLinkService::Options options;
1455 options.allowLocalFields = false;
1456 initialize(options);
1457
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001458 auto packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
Eric Newberrya1939ba2015-10-09 12:35:03 -07001459 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001460 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001461
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001462 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001463 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1464 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1465 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1466}
1467
1468BOOST_AUTO_TEST_SUITE_END() // Malformed
1469
Eric Newberry86d31872015-09-23 16:24:59 -07001470BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
1471BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -07001472
1473} // namespace tests
1474} // namespace face
1475} // namespace nfd