blob: 219154e47cacee4af656a2d7fc9e2a8e2afe9abb [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
Junxiao Shi9d727852019-05-14 13:44:22 -0600196 auto nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
197 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;
232 lpPacket.set<lp::FragmentField>(std::make_pair(
233 interest1->wireEncode().begin(), interest1->wireEncode().end()));
234 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
235
236 transport->receivePacket(lpPacket.wireEncode());
237
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700238 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700239 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400240 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700241}
242
243BOOST_AUTO_TEST_CASE(ReceiveBareData)
244{
Eric Newberry86d31872015-09-23 16:24:59 -0700245 // Initialize with Options that disables all services
246 GenericLinkService::Options options;
247 options.allowLocalFields = false;
248 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700249
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400250 auto data1 = makeData("/12345678");
Eric Newberrya98bf932015-09-21 00:58:47 -0700251 transport->receivePacket(data1->wireEncode());
252
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700253 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700254 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400255 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700256}
257
258BOOST_AUTO_TEST_CASE(ReceiveData)
259{
Eric Newberry86d31872015-09-23 16:24:59 -0700260 // Initialize with Options that disables all services
261 GenericLinkService::Options options;
262 options.allowLocalFields = false;
263 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700264
Junxiao Shi9d727852019-05-14 13:44:22 -0600265 auto data1 = makeData("/12345689");
Eric Newberrya98bf932015-09-21 00:58:47 -0700266 lp::Packet lpPacket;
267 lpPacket.set<lp::FragmentField>(std::make_pair(
268 data1->wireEncode().begin(), data1->wireEncode().end()));
269 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
270
271 transport->receivePacket(lpPacket.wireEncode());
272
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700273 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700274 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400275 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700276}
277
278BOOST_AUTO_TEST_CASE(ReceiveNack)
279{
Eric Newberry86d31872015-09-23 16:24:59 -0700280 // Initialize with Options that disables all services
281 GenericLinkService::Options options;
282 options.allowLocalFields = false;
283 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700284
Junxiao Shi9d727852019-05-14 13:44:22 -0600285 lp::Nack nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
286 lp::NackReason::NO_ROUTE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700287 lp::Packet lpPacket;
288 lpPacket.set<lp::FragmentField>(std::make_pair(
289 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
290 lpPacket.set<lp::NackField>(nack1.getHeader());
291
292 transport->receivePacket(lpPacket.wireEncode());
293
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700294 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700295 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400296 BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), nack1.getReason());
297 BOOST_CHECK_EQUAL(receivedNacks.back().getInterest().wireEncode(), nack1.getInterest().wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700298}
299
Eric Newberrya1939ba2015-10-09 12:35:03 -0700300BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
301{
302 // Initialize with Options that disables all services
303 GenericLinkService::Options options;
304 options.allowLocalFields = false;
305 initialize(options);
306
307 lp::Packet lpPacket;
308 lpPacket.set<lp::SequenceField>(0);
309
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400310 transport->receivePacket(lpPacket.wireEncode());
Eric Newberrya1939ba2015-10-09 12:35:03 -0700311
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700312 // IDLE packet should be ignored, but is not an error
313 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700314 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
315 BOOST_CHECK_EQUAL(receivedData.size(), 0);
316 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
317}
318
Eric Newberrya98bf932015-09-21 00:58:47 -0700319BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
320
Eric Newberry86d31872015-09-23 16:24:59 -0700321BOOST_AUTO_TEST_SUITE(Fragmentation)
322
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700323BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
324{
325 // Initialize with Options that disable fragmentation
326 GenericLinkService::Options options;
327 options.allowFragmentation = false;
328 initialize(options);
329
330 transport->setMtu(55);
331
Junxiao Shi9d727852019-05-14 13:44:22 -0600332 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700333 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700334
335 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
336 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
337}
338
Eric Newberryf3ee8082020-01-28 13:44:18 -0800339// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
340// This test case ensures that packets are dropped if the MTU is zero
341BOOST_AUTO_TEST_CASE(FragmentationDisabledZeroMtuDrop)
342{
343 // Initialize with Options that disable fragmentation
344 GenericLinkService::Options options;
345 options.allowFragmentation = false;
346 initialize(options);
347
348 transport->setMtu(0);
349
350 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700351 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800352
353 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
354 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
355}
356
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700357BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
358{
359 // Initialize with Options that enable fragmentation
360 GenericLinkService::Options options;
361 options.allowFragmentation = true;
362 initialize(options);
363
364 transport->setMtu(MTU_UNLIMITED);
365
Junxiao Shi9d727852019-05-14 13:44:22 -0600366 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700367 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700368
369 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
370}
371
372BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
373{
374 // Initialize with Options that enable fragmentation
375 GenericLinkService::Options options;
376 options.allowFragmentation = true;
377 initialize(options);
378
379 transport->setMtu(105);
380
Junxiao Shi9d727852019-05-14 13:44:22 -0600381 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700382 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700383
384 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
385}
386
387BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
388{
389 // Initialize with Options that enable fragmentation
390 GenericLinkService::Options options;
391 options.allowFragmentation = true;
392 initialize(options);
393
394 transport->setMtu(60);
395
Junxiao Shi9d727852019-05-14 13:44:22 -0600396 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700397 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700398
399 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
400}
401
Eric Newberryf3ee8082020-01-28 13:44:18 -0800402// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
403// This test case ensures that packets are dropped if the MTU is zero
404BOOST_AUTO_TEST_CASE(FragmentationZeroMtuDrop)
405{
406 // Initialize with Options that enable fragmentation
407 GenericLinkService::Options options;
408 options.allowFragmentation = true;
409 initialize(options);
410
411 transport->setMtu(0);
412
413 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700414 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800415
416 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
417 BOOST_CHECK_EQUAL(service->getCounters().nFragmentationErrors, 1);
418}
419
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700420BOOST_AUTO_TEST_CASE(ReassembleFragments)
421{
422 // Initialize with Options that enables reassembly
423 GenericLinkService::Options options;
424 options.allowReassembly = true;
425 initialize(options);
426
Junxiao Shi9d727852019-05-14 13:44:22 -0600427 auto interest = makeInterest(
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700428 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
429 lp::Packet packet(interest->wireEncode());
430
431 // fragment the packet
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400432 LpFragmenter fragmenter({});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700433 size_t mtu = 100;
434 bool isOk = false;
435 std::vector<lp::Packet> frags;
436 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
437 BOOST_REQUIRE(isOk);
438 BOOST_CHECK_GT(frags.size(), 1);
439
440 // receive the fragments
441 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
442 size_t sequence = 1000 + fragIndex;
443 frags[fragIndex].add<lp::SequenceField>(sequence);
444
445 transport->receivePacket(frags[fragIndex].wireEncode());
446
447 if (fragIndex > 0) {
448 BOOST_CHECK(receivedInterests.empty());
449 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
450 }
451 else {
452 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400453 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest->wireEncode());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700454 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
455 }
456 }
457}
458
Eric Newberry86d31872015-09-23 16:24:59 -0700459BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
460{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700461 // Initialize with Options that disables reassembly
462 GenericLinkService::Options options;
463 options.allowReassembly = false;
464 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700465
Junxiao Shi9d727852019-05-14 13:44:22 -0600466 auto interest = makeInterest("/IgFe6NvH");
Eric Newberry86d31872015-09-23 16:24:59 -0700467 lp::Packet packet(interest->wireEncode());
468 packet.set<lp::FragIndexField>(140);
469
470 transport->receivePacket(packet.wireEncode());
471
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700472 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700473 BOOST_CHECK(receivedInterests.empty());
474}
475
476BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
477{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700478 // Initialize with Options that disables reassembly
479 GenericLinkService::Options options;
480 options.allowReassembly = false;
481 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700482
Junxiao Shi9d727852019-05-14 13:44:22 -0600483 auto interest = makeInterest("/SeGmEjvIVX");
Eric Newberry86d31872015-09-23 16:24:59 -0700484 lp::Packet packet(interest->wireEncode());
485 packet.set<lp::FragCountField>(276);
486
487 transport->receivePacket(packet.wireEncode());
488
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700489 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700490 BOOST_CHECK(receivedInterests.empty());
491}
492
493BOOST_AUTO_TEST_SUITE_END() // Fragmentation
494
Eric Newberry185ab292017-03-28 06:45:39 +0000495BOOST_AUTO_TEST_SUITE(Reliability)
496
Eric Newberry7b0071e2017-07-03 17:33:31 +0000497BOOST_AUTO_TEST_CASE(SendInterest)
Eric Newberry185ab292017-03-28 06:45:39 +0000498{
499 // Initialize with Options that enables reliability
500 GenericLinkService::Options options;
501 options.allowLocalFields = false;
502 options.reliabilityOptions.isEnabled = true;
503 initialize(options);
504
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400505 auto interest1 = makeInterest("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700506 face->sendInterest(*interest1);
Eric Newberry185ab292017-03-28 06:45:39 +0000507
508 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
509 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700510 lp::Packet interest1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000511 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000512 BOOST_CHECK(interest1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000513}
514
Eric Newberry7b0071e2017-07-03 17:33:31 +0000515BOOST_AUTO_TEST_CASE(SendData)
Eric Newberry185ab292017-03-28 06:45:39 +0000516{
517 // Initialize with Options that enables reliability
518 GenericLinkService::Options options;
519 options.allowLocalFields = false;
520 options.reliabilityOptions.isEnabled = true;
521 initialize(options);
522
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400523 auto data1 = makeData("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700524 face->sendData(*data1);
Eric Newberry185ab292017-03-28 06:45:39 +0000525
526 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
527 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700528 lp::Packet data1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000529 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000530 BOOST_CHECK(data1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000531}
532
Eric Newberry7b0071e2017-07-03 17:33:31 +0000533BOOST_AUTO_TEST_CASE(SendNack)
Eric Newberry185ab292017-03-28 06:45:39 +0000534{
535 // Initialize with Options that enables reliability
536 GenericLinkService::Options options;
537 options.allowLocalFields = false;
538 options.reliabilityOptions.isEnabled = true;
539 initialize(options);
540
Junxiao Shi9d727852019-05-14 13:44:22 -0600541 auto nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
542 lp::NackReason::NO_ROUTE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700543 face->sendNack(nack1);
Eric Newberry185ab292017-03-28 06:45:39 +0000544
545 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
546 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700547 lp::Packet nack1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000548 BOOST_CHECK(nack1pkt.has<lp::NackField>());
549 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000550 BOOST_CHECK(nack1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000551}
552
Eric Newberry32f7eac2020-02-07 14:40:17 -0800553BOOST_AUTO_TEST_CASE(DropDuplicatePacket)
554{
555 // Initialize with Options that enables reliability
556 GenericLinkService::Options options;
557 options.allowLocalFields = false;
558 options.reliabilityOptions.isEnabled = true;
559 initialize(options);
560
561 Interest interest("/test/prefix");
Eric Newberry32f7eac2020-02-07 14:40:17 -0800562 lp::Packet pkt1;
563 pkt1.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
564 pkt1.add<lp::SequenceField>(7);
565 pkt1.add<lp::TxSequenceField>(12);
566 transport->receivePacket(pkt1.wireEncode());
567 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
568 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 0);
569
570 lp::Packet pkt2;
571 pkt2.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
572 pkt2.add<lp::SequenceField>(7);
573 pkt2.add<lp::TxSequenceField>(13);
574 transport->receivePacket(pkt2.wireEncode());
575 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
576 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 1);
577}
578
Eric Newberry185ab292017-03-28 06:45:39 +0000579BOOST_AUTO_TEST_SUITE_END() // Reliability
Eric Newberry86d31872015-09-23 16:24:59 -0700580
Eric Newberryb49313d2017-12-24 20:22:27 -0700581// congestion detection and marking
582BOOST_AUTO_TEST_SUITE(CongestionMark)
583
584BOOST_AUTO_TEST_CASE(NoCongestion)
585{
586 GenericLinkService::Options options;
587 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400588 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700589 initialize(options, MTU_UNLIMITED, 65536);
590 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
591 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
592 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
593
Junxiao Shi9d727852019-05-14 13:44:22 -0600594 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700595
596 // congestion threshold will be 32768 bytes, since min(65536, 65536 / 2) = 32768 bytes
597
598 // no congestion
599 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700600 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700601 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700602 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700603 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
604 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
605 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
606 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
607
608 // no congestion
609 transport->setSendQueueLength(32768);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700610 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700611 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700612 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700613 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
614 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
615 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
616 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
617}
618
619BOOST_AUTO_TEST_CASE(CongestionCoDel)
620{
621 GenericLinkService::Options options;
622 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400623 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700624 initialize(options, MTU_UNLIMITED, 65536);
625 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700626 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
627 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
628
Junxiao Shi9d727852019-05-14 13:44:22 -0600629 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700630
Klaus Schneider380668b2019-10-02 01:21:13 -0700631 // first congested packet, will not be marked
632 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700633 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700634 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700635 lp::Packet pkt0(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700636 BOOST_REQUIRE_EQUAL(pkt0.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400637 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700638 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700639
640 time::nanoseconds markingInterval(
641 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
642 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
643
644 advanceClocks(markingInterval + 1_ms);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700645 face->sendInterest(*interest);
Teng Liang13d582a2020-07-21 20:23:11 -0700646 lp::Packet pkt1(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700647
648 // First congestion mark appears after one interval (100 ms)
649 BOOST_REQUIRE_EQUAL(pkt1.count<lp::CongestionMarkField>(), 1);
650 BOOST_CHECK_EQUAL(pkt1.get<lp::CongestionMarkField>(), 1);
Eric Newberryb49313d2017-12-24 20:22:27 -0700651 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
652 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
653
654 // advance clock to half of marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700655 advanceClocks(markingInterval / 2); // 50ms
Eric Newberryb49313d2017-12-24 20:22:27 -0700656
657 // second congested packet, but within marking interval, will not be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700658 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700659 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700660 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700661 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700662 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700663
664 markingInterval = time::nanoseconds(
665 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
666 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
667 nextMarkTime += markingInterval;
668
Eric Newberryb49313d2017-12-24 20:22:27 -0700669 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700670 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
671 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
672
673 // advance clocks past end of initial interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700674 this->advanceClocks((markingInterval / 2) + 1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700675
676 // first congested packet after waiting marking interval, will be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700677 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700678 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700679 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 4);
Teng Liang13d582a2020-07-21 20:23:11 -0700680 lp::Packet pkt3(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700681 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 1);
682 BOOST_CHECK_EQUAL(pkt3.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700683 markingInterval = time::nanoseconds(
684 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
685 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700686 nextMarkTime += markingInterval;
687 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700688 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
689 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
690
691 // advance clock partway through current marking interval
Klaus Schneider380668b2019-10-02 01:21:13 -0700692 this->advanceClocks(markingInterval - 20_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700693
694 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700695 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700696 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700697 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 5);
Teng Liang13d582a2020-07-21 20:23:11 -0700698 lp::Packet pkt4(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700699 BOOST_CHECK_EQUAL(pkt4.count<lp::CongestionMarkField>(), 0);
700 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700701 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
702 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
703
704 // advance clocks past end of current marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700705 this->advanceClocks(21_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700706
707 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700708 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700709 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700710 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 6);
Teng Liang13d582a2020-07-21 20:23:11 -0700711 lp::Packet pkt5(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700712 BOOST_REQUIRE_EQUAL(pkt5.count<lp::CongestionMarkField>(), 1);
713 BOOST_CHECK_EQUAL(pkt5.get<lp::CongestionMarkField>(), 1);
714 markingInterval = time::nanoseconds(
715 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700716 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700717 nextMarkTime += markingInterval;
718 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700719 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
720 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
721
Davide Pesavento14e71f02019-03-28 17:35:25 -0400722 this->advanceClocks(1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700723
724 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700725 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700726 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700727 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 7);
Teng Liang13d582a2020-07-21 20:23:11 -0700728 lp::Packet pkt6(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700729 BOOST_CHECK_EQUAL(pkt6.count<lp::CongestionMarkField>(), 0);
730 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700731 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
732 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
733
734 this->advanceClocks(markingInterval);
735
736 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700737 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700738 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700739 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 8);
Teng Liang13d582a2020-07-21 20:23:11 -0700740 lp::Packet pkt7(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700741 BOOST_REQUIRE_EQUAL(pkt7.count<lp::CongestionMarkField>(), 1);
742 BOOST_CHECK_EQUAL(pkt7.get<lp::CongestionMarkField>(), 1);
743 markingInterval = time::nanoseconds(
744 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700745 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700746 nextMarkTime += markingInterval;
747 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700748 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 4);
749 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
750
751 // no more congestion
752 transport->setSendQueueLength(30000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700753 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700754 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 9);
Teng Liang13d582a2020-07-21 20:23:11 -0700755 lp::Packet pkt8(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700756 BOOST_CHECK_EQUAL(pkt8.count<lp::CongestionMarkField>(), 0);
757 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700758 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
759 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
760
Davide Pesavento14e71f02019-03-28 17:35:25 -0400761 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700762
763 // send queue congested again, but can't mark packet because within one full interval of last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700764 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700765 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700766 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 10);
Teng Liang13d582a2020-07-21 20:23:11 -0700767 lp::Packet pkt9(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700768 BOOST_CHECK_EQUAL(pkt9.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700769 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
770 markingInterval = time::nanoseconds(
771 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
772 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
773 nextMarkTime = time::steady_clock::now() + markingInterval;
774 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700775 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
776 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
777
778 // advance clock past full 100ms interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700779 this->advanceClocks(markingInterval + 2_ms);
780 BOOST_CHECK_GT(time::steady_clock::now(), nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700781
Klaus Schneider380668b2019-10-02 01:21:13 -0700782 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700783 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700784 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 11);
Teng Liang13d582a2020-07-21 20:23:11 -0700785 lp::Packet pkt10(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700786 BOOST_REQUIRE_EQUAL(pkt10.count<lp::CongestionMarkField>(), 1);
787 BOOST_CHECK_EQUAL(pkt10.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700788 markingInterval = time::nanoseconds(
789 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
790 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
791 nextMarkTime += markingInterval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700792 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700793 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
794 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
795
796 // advance clock partway through 100ms marking interval
Davide Pesavento14e71f02019-03-28 17:35:25 -0400797 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700798
799 // not marked since within 100ms window before can mark again
Klaus Schneider380668b2019-10-02 01:21:13 -0700800 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700801 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700802 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 12);
Teng Liang13d582a2020-07-21 20:23:11 -0700803 lp::Packet pkt11(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700804 BOOST_CHECK_EQUAL(pkt11.count<lp::CongestionMarkField>(), 0);
805 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700806 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
807 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
808
809 // advance clocks past m_nextMarkTime
Davide Pesavento14e71f02019-03-28 17:35:25 -0400810 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700811
812 // markable packet, queue length still above threshold
Klaus Schneider380668b2019-10-02 01:21:13 -0700813 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700814 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700815 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 13);
Teng Liang13d582a2020-07-21 20:23:11 -0700816 lp::Packet pkt12(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700817 BOOST_REQUIRE_EQUAL(pkt12.count<lp::CongestionMarkField>(), 1);
818 BOOST_CHECK_EQUAL(pkt12.get<lp::CongestionMarkField>(), 1);
819 markingInterval = time::nanoseconds(
820 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700821 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700822 nextMarkTime += markingInterval;
823 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700824 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
825 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
826
827 // no more congestion
828 transport->setSendQueueLength(10000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700829 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700830 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 14);
Teng Liang13d582a2020-07-21 20:23:11 -0700831 lp::Packet pkt13(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700832 BOOST_CHECK_EQUAL(pkt13.count<lp::CongestionMarkField>(), 0);
833 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700834 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
835 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
836
837 // advance clocks past one full interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700838 this->advanceClocks(102_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700839
840 // start congestion again
Klaus Schneider380668b2019-10-02 01:21:13 -0700841 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700842 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700843 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 15);
Teng Liang13d582a2020-07-21 20:23:11 -0700844 lp::Packet pkt14(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700845 BOOST_REQUIRE_EQUAL(pkt14.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400846 nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700847 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700848 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
849 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700850
851 // no more congestion, cancel marking interval
852 transport->setSendQueueLength(5000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700853 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700854 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 16);
Teng Liang13d582a2020-07-21 20:23:11 -0700855 lp::Packet pkt15(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700856 BOOST_CHECK_EQUAL(pkt15.count<lp::CongestionMarkField>(), 0);
857 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700858 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700859 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700860}
861
862BOOST_AUTO_TEST_CASE(DefaultThreshold)
863{
864 GenericLinkService::Options options;
865 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400866 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700867 initialize(options, MTU_UNLIMITED, QUEUE_UNSUPPORTED);
868 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
869 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
870 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
871
Junxiao Shi9d727852019-05-14 13:44:22 -0600872 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700873
874 // congestion threshold will be 65536 bytes, since the transport reports that it cannot measure
875 // the queue capacity
876
877 // no congestion
878 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700879 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700880 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700881 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700882 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
883 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
884 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
885 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
886
887 // no congestion
888 transport->setSendQueueLength(65536);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700889 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700890 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700891 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700892 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
893 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
894 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
895 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
896
Klaus Schneider380668b2019-10-02 01:21:13 -0700897 // first congested (not marked yet) packet
Eric Newberryb49313d2017-12-24 20:22:27 -0700898 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700899 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700900 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700901 lp::Packet pkt3(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700902 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400903 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700904 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700905 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
906 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
Eric Newberryb49313d2017-12-24 20:22:27 -0700907}
908
909BOOST_AUTO_TEST_SUITE_END() // CongestionMark
910
Eric Newberryee400b52016-11-24 14:12:48 +0000911BOOST_AUTO_TEST_SUITE(LpFields)
Eric Newberrya98bf932015-09-21 00:58:47 -0700912
Eric Newberrya98bf932015-09-21 00:58:47 -0700913BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
914{
Eric Newberry86d31872015-09-23 16:24:59 -0700915 // Initialize with Options that enables local fields
916 GenericLinkService::Options options;
917 options.allowLocalFields = true;
918 initialize(options);
919
Junxiao Shi9d727852019-05-14 13:44:22 -0600920 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700921 lp::Packet packet(interest->wireEncode());
922 packet.set<lp::NextHopFaceIdField>(1000);
923
924 transport->receivePacket(packet.wireEncode());
925
926 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400927 auto tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +0000928 BOOST_REQUIRE(tag != nullptr);
929 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700930}
931
932BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
933{
Eric Newberry86d31872015-09-23 16:24:59 -0700934 // Initialize with Options that disables local fields
935 GenericLinkService::Options options;
936 options.allowLocalFields = false;
937 initialize(options);
938
Junxiao Shi9d727852019-05-14 13:44:22 -0600939 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700940 lp::Packet packet(interest->wireEncode());
941 packet.set<lp::NextHopFaceIdField>(1000);
942
943 transport->receivePacket(packet.wireEncode());
944
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700945 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700946 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700947}
948
949BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
950{
Eric Newberry86d31872015-09-23 16:24:59 -0700951 // Initialize with Options that enables local fields
952 GenericLinkService::Options options;
953 options.allowLocalFields = true;
954 initialize(options);
955
Junxiao Shi9d727852019-05-14 13:44:22 -0600956 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700957 lp::Packet packet(data->wireEncode());
958 packet.set<lp::NextHopFaceIdField>(1000);
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(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700964}
965
966BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
967{
Eric Newberry86d31872015-09-23 16:24:59 -0700968 // Initialize with Options that enables local fields
969 GenericLinkService::Options options;
970 options.allowLocalFields = true;
971 initialize(options);
972
Junxiao Shi9d727852019-05-14 13:44:22 -0600973 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
974 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -0700975 lp::Packet packet;
976 packet.set<lp::FragmentField>(std::make_pair(
977 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
978 packet.set<lp::NackField>(nack.getHeader());
979 packet.set<lp::NextHopFaceIdField>(1000);
980
981 transport->receivePacket(packet.wireEncode());
982
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700983 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700984 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700985}
986
Junxiao Shi6eb02712017-05-27 22:48:02 +0000987BOOST_AUTO_TEST_CASE(ReceiveCachePolicy)
Eric Newberrya98bf932015-09-21 00:58:47 -0700988{
Junxiao Shi6eb02712017-05-27 22:48:02 +0000989 // Initialize with Options that disables local fields
Eric Newberry86d31872015-09-23 16:24:59 -0700990 GenericLinkService::Options options;
Junxiao Shi6eb02712017-05-27 22:48:02 +0000991 options.allowLocalFields = false;
Eric Newberry86d31872015-09-23 16:24:59 -0700992 initialize(options);
Junxiao Shi6eb02712017-05-27 22:48:02 +0000993 // CachePolicy is unprivileged and does not require allowLocalFields option.
Eric Newberry86d31872015-09-23 16:24:59 -0700994
Junxiao Shi9d727852019-05-14 13:44:22 -0600995 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700996 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000997 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700998
999 transport->receivePacket(packet.wireEncode());
1000
1001 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001002 auto tag = receivedData.back().getTag<lp::CachePolicyTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +00001003 BOOST_REQUIRE(tag != nullptr);
1004 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -07001005}
1006
Junxiao Shi6eb02712017-05-27 22:48:02 +00001007BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropInterest)
Eric Newberrya98bf932015-09-21 00:58:47 -07001008{
Eric Newberry86d31872015-09-23 16:24:59 -07001009 // Initialize with Options that enables local fields
1010 GenericLinkService::Options options;
1011 options.allowLocalFields = true;
1012 initialize(options);
1013
Junxiao Shi9d727852019-05-14 13:44:22 -06001014 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001015 lp::Packet packet(interest->wireEncode());
1016 lp::CachePolicy policy;
1017 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1018 packet.set<lp::CachePolicyField>(policy);
1019
1020 transport->receivePacket(packet.wireEncode());
1021
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001022 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001023 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -07001024}
1025
Junxiao Shi6eb02712017-05-27 22:48:02 +00001026BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropNack)
Eric Newberrya98bf932015-09-21 00:58:47 -07001027{
Eric Newberry86d31872015-09-23 16:24:59 -07001028 // Initialize with Options that enables local fields
1029 GenericLinkService::Options options;
1030 options.allowLocalFields = true;
1031 initialize(options);
1032
Junxiao Shi9d727852019-05-14 13:44:22 -06001033 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1034 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -07001035 lp::Packet packet(nack.getInterest().wireEncode());
1036 packet.set<lp::NackField>(nack.getHeader());
1037 lp::CachePolicy policy;
1038 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1039 packet.set<lp::CachePolicyField>(policy);
1040
1041 transport->receivePacket(packet.wireEncode());
1042
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001043 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001044 BOOST_CHECK(receivedNacks.empty());
1045}
1046
1047BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
1048{
1049 // Initialize with Options that enables local fields
1050 GenericLinkService::Options options;
1051 options.allowLocalFields = true;
1052 initialize(options);
1053
Junxiao Shi9d727852019-05-14 13:44:22 -06001054 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001055 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001056
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001057 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001058
1059 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001060 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001061 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
1062 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
1063}
1064
1065BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
1066{
1067 // Initialize with Options that disables local fields
1068 GenericLinkService::Options options;
1069 options.allowLocalFields = false;
1070 initialize(options);
1071
Junxiao Shi9d727852019-05-14 13:44:22 -06001072 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001073 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001074
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001075 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001076
1077 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001078 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001079 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
1080}
1081
1082BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
1083{
1084 // Initialize with Options that enables local fields
1085 GenericLinkService::Options options;
1086 options.allowLocalFields = true;
1087 initialize(options);
1088
Junxiao Shi9d727852019-05-14 13:44:22 -06001089 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001090 lp::Packet packet(interest->wireEncode());
1091 packet.set<lp::IncomingFaceIdField>(1000);
1092
1093 transport->receivePacket(packet.wireEncode());
1094
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001095 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001096 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001097 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001098}
1099
1100BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
1101{
1102 // Initialize with Options that enables local fields
1103 GenericLinkService::Options options;
1104 options.allowLocalFields = true;
1105 initialize(options);
1106
Junxiao Shi9d727852019-05-14 13:44:22 -06001107 auto data = makeData("/z1megUh9Bj");
Eric Newberry86d31872015-09-23 16:24:59 -07001108 lp::Packet packet(data->wireEncode());
1109 packet.set<lp::IncomingFaceIdField>(1000);
1110
1111 transport->receivePacket(packet.wireEncode());
1112
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001113 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001114 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001115 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001116}
1117
1118BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
1119{
1120 // Initialize with Options that enables local fields
1121 GenericLinkService::Options options;
1122 options.allowLocalFields = true;
1123 initialize(options);
1124
Junxiao Shi9d727852019-05-14 13:44:22 -06001125 lp::Nack nack = makeNack(*makeInterest("/TPAhdiHz", false, nullopt, 278),
1126 lp::NackReason::CONGESTION);
Eric Newberry86d31872015-09-23 16:24:59 -07001127 lp::Packet packet(nack.getInterest().wireEncode());
1128 packet.set<lp::NackField>(nack.getHeader());
1129 packet.set<lp::IncomingFaceIdField>(1000);
1130
1131 transport->receivePacket(packet.wireEncode());
1132
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001133 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001134 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001135 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -07001136}
1137
Eric Newberryee400b52016-11-24 14:12:48 +00001138BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
1139{
Junxiao Shi9d727852019-05-14 13:44:22 -06001140 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001141 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
1142
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001143 face->sendInterest(*interest);
Eric Newberryee400b52016-11-24 14:12:48 +00001144
1145 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001146 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001147 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1148 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
1149}
1150
1151BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
1152{
Junxiao Shi9d727852019-05-14 13:44:22 -06001153 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001154 data->setTag(make_shared<lp::CongestionMarkTag>(0));
1155
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001156 face->sendData(*data);
Eric Newberryee400b52016-11-24 14:12:48 +00001157
1158 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001159 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001160 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1161 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
1162}
1163
1164BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
1165{
Junxiao Shi9d727852019-05-14 13:44:22 -06001166 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1167 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001168 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
1169
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001170 face->sendNack(nack);
Eric Newberryee400b52016-11-24 14:12:48 +00001171
1172 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001173 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001174 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1175 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
1176}
1177
1178BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
1179{
Junxiao Shi9d727852019-05-14 13:44:22 -06001180 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001181 lp::Packet packet(interest->wireEncode());
1182 packet.set<lp::CongestionMarkField>(1);
1183
1184 transport->receivePacket(packet.wireEncode());
1185
1186 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001187 auto tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001188 BOOST_REQUIRE(tag != nullptr);
1189 BOOST_CHECK_EQUAL(*tag, 1);
1190}
1191
1192BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
1193{
Junxiao Shi9d727852019-05-14 13:44:22 -06001194 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001195 lp::Packet packet(data->wireEncode());
1196 packet.set<lp::CongestionMarkField>(1);
1197
1198 transport->receivePacket(packet.wireEncode());
1199
1200 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001201 auto tag = receivedData.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001202 BOOST_REQUIRE(tag != nullptr);
1203 BOOST_CHECK_EQUAL(*tag, 1);
1204}
1205
1206BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
1207{
Junxiao Shi9d727852019-05-14 13:44:22 -06001208 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1209 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001210 lp::Packet packet;
1211 packet.set<lp::FragmentField>(std::make_pair(
1212 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1213 packet.set<lp::NackField>(nack.getHeader());
1214 packet.set<lp::CongestionMarkField>(1);
1215
1216 transport->receivePacket(packet.wireEncode());
1217
1218 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001219 auto tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001220 BOOST_REQUIRE(tag != nullptr);
1221 BOOST_CHECK_EQUAL(*tag, 1);
1222}
1223
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001224BOOST_AUTO_TEST_CASE(SendNonDiscovery)
1225{
1226 GenericLinkService::Options options;
1227 options.allowSelfLearning = true;
1228 initialize(options);
1229
Junxiao Shi9d727852019-05-14 13:44:22 -06001230 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001231 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1232
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001233 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001234
1235 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001236 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001237 BOOST_CHECK(sent.has<lp::NonDiscoveryField>());
1238}
1239
1240BOOST_AUTO_TEST_CASE(SendNonDiscoveryDisabled)
1241{
1242 GenericLinkService::Options options;
1243 options.allowSelfLearning = false;
1244 initialize(options);
1245
Junxiao Shi9d727852019-05-14 13:44:22 -06001246 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001247 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1248
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001249 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001250
1251 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001252 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001253 BOOST_CHECK(!sent.has<lp::NonDiscoveryField>());
1254}
1255
1256BOOST_AUTO_TEST_CASE(ReceiveNonDiscovery)
1257{
1258 GenericLinkService::Options options;
1259 options.allowSelfLearning = true;
1260 initialize(options);
1261
Junxiao Shi9d727852019-05-14 13:44:22 -06001262 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001263 lp::Packet packet(interest->wireEncode());
1264 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1265
1266 transport->receivePacket(packet.wireEncode());
1267
1268 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001269 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001270 BOOST_CHECK(tag != nullptr);
1271}
1272
1273BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDisabled)
1274{
1275 GenericLinkService::Options options;
1276 options.allowSelfLearning = false;
1277 initialize(options);
1278
Junxiao Shi9d727852019-05-14 13:44:22 -06001279 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001280 lp::Packet packet(interest->wireEncode());
1281 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1282
1283 transport->receivePacket(packet.wireEncode());
1284
1285 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1286 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001287 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001288 BOOST_CHECK(tag == nullptr);
1289}
1290
1291BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropData)
1292{
1293 GenericLinkService::Options options;
1294 options.allowSelfLearning = true;
1295 initialize(options);
1296
Junxiao Shi9d727852019-05-14 13:44:22 -06001297 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001298 lp::Packet packet(data->wireEncode());
1299 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1300
1301 transport->receivePacket(packet.wireEncode());
1302
1303 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1304 BOOST_CHECK(receivedData.empty());
1305}
1306
1307BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropNack)
1308{
1309 GenericLinkService::Options options;
1310 options.allowSelfLearning = true;
1311 initialize(options);
1312
Junxiao Shi9d727852019-05-14 13:44:22 -06001313 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1314 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001315 lp::Packet packet;
1316 packet.set<lp::FragmentField>(std::make_pair(
1317 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1318 packet.set<lp::NackField>(nack.getHeader());
1319 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1320
1321 transport->receivePacket(packet.wireEncode());
1322
1323 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1324 BOOST_CHECK(receivedNacks.empty());
1325}
1326
1327BOOST_AUTO_TEST_CASE(SendPrefixAnnouncement)
1328{
1329 GenericLinkService::Options options;
1330 options.allowSelfLearning = true;
1331 initialize(options);
1332
Junxiao Shi9d727852019-05-14 13:44:22 -06001333 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001334 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1335 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001336
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001337 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001338
1339 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001340 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001341 BOOST_CHECK(sent.has<lp::PrefixAnnouncementField>());
1342}
1343
1344BOOST_AUTO_TEST_CASE(SendPrefixAnnouncementDisabled)
1345{
1346 GenericLinkService::Options options;
1347 options.allowSelfLearning = false;
1348 initialize(options);
1349
Junxiao Shi9d727852019-05-14 13:44:22 -06001350 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001351 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1352 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001353
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001354 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001355
1356 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001357 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001358 BOOST_CHECK(!sent.has<lp::PrefixAnnouncementField>());
1359}
1360
1361BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncement)
1362{
1363 GenericLinkService::Options options;
1364 options.allowSelfLearning = true;
1365 initialize(options);
1366
Junxiao Shi9d727852019-05-14 13:44:22 -06001367 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001368 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001369 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1370 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001371
1372 transport->receivePacket(packet.wireEncode());
1373
1374 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001375 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangbce3cb52018-09-05 20:08:27 -07001376 BOOST_CHECK_EQUAL(tag->get().getPrefixAnn()->getAnnouncedName(), "/local/ndn/prefix");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001377}
1378
1379BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDisabled)
1380{
1381 GenericLinkService::Options options;
1382 options.allowSelfLearning = false;
1383 initialize(options);
1384
Junxiao Shi9d727852019-05-14 13:44:22 -06001385 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001386 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001387 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1388 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001389
1390 transport->receivePacket(packet.wireEncode());
1391
1392 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1393 BOOST_CHECK_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001394 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001395 BOOST_CHECK(tag == nullptr);
1396}
1397
1398BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropInterest)
1399{
1400 GenericLinkService::Options options;
1401 options.allowSelfLearning = true;
1402 initialize(options);
1403
Junxiao Shi9d727852019-05-14 13:44:22 -06001404 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001405 lp::Packet packet(interest->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001406 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1407 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001408
1409 transport->receivePacket(packet.wireEncode());
1410
1411 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1412 BOOST_CHECK(receivedInterests.empty());
1413}
1414
1415BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropNack)
1416{
1417 GenericLinkService::Options options;
1418 options.allowSelfLearning = true;
1419 initialize(options);
1420
Junxiao Shi9d727852019-05-14 13:44:22 -06001421 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1422 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001423 lp::Packet packet;
1424 packet.set<lp::FragmentField>(std::make_pair(
1425 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1426 packet.set<lp::NackField>(nack.getHeader());
Teng Liangbce3cb52018-09-05 20:08:27 -07001427 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1428 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001429
1430 transport->receivePacket(packet.wireEncode());
1431
1432 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1433 BOOST_CHECK(receivedNacks.empty());
1434}
1435
Eric Newberryee400b52016-11-24 14:12:48 +00001436BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -07001437
Eric Newberrya1939ba2015-10-09 12:35:03 -07001438BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
1439
1440BOOST_AUTO_TEST_CASE(WrongTlvType)
1441{
1442 // Initialize with Options that disables all services
1443 GenericLinkService::Options options;
1444 options.allowLocalFields = false;
1445 initialize(options);
1446
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001447 auto packet = ndn::encoding::makeEmptyBlock(tlv::Name);
1448 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001449
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001450 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001451 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1452 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1453 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1454}
1455
1456BOOST_AUTO_TEST_CASE(Unparsable)
1457{
1458 // Initialize with Options that disables all services
1459 GenericLinkService::Options options;
1460 options.allowLocalFields = false;
1461 initialize(options);
1462
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001463 auto packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
Eric Newberrya1939ba2015-10-09 12:35:03 -07001464 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001465 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001466
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001467 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001468 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1469 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1470 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1471}
1472
1473BOOST_AUTO_TEST_SUITE_END() // Malformed
1474
Eric Newberry86d31872015-09-23 16:24:59 -07001475BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
1476BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -07001477
1478} // namespace tests
1479} // namespace face
1480} // namespace nfd