blob: 568dafc40b48d7bc0076aebf3d071d22dbb8e28e [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/*
Eric Newberryf3ee8082020-01-28 13:44:18 -08003 * Copyright (c) 2014-2020, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "face/generic-link-service.hpp"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face/face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028
Davide Pesavento0064c1d2018-03-03 18:43:53 -050029#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040030#include "tests/key-chain-fixture.hpp"
31#include "tests/daemon/global-io-fixture.hpp"
32#include "dummy-transport.hpp"
Eric Newberryb49313d2017-12-24 20:22:27 -070033
Teng Liangfdcbb4d2018-01-27 16:01:35 -070034#include <ndn-cxx/lp/empty-value.hpp>
Teng Liangbce3cb52018-09-05 20:08:27 -070035#include <ndn-cxx/lp/prefix-announcement-header.hpp>
Eric Newberryb49313d2017-12-24 20:22:27 -070036#include <ndn-cxx/lp/tags.hpp>
Teng Liangbce3cb52018-09-05 20:08:27 -070037#include <ndn-cxx/security/signing-helpers.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070038
39namespace nfd {
40namespace face {
41namespace tests {
42
43using namespace nfd::tests;
44
45BOOST_AUTO_TEST_SUITE(Face)
46
Junxiao Shicde37ad2015-12-24 01:02:05 -070047using nfd::Face;
48
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040049class GenericLinkServiceFixture : public GlobalIoTimeFixture, public KeyChainFixture
Eric Newberrya98bf932015-09-21 00:58:47 -070050{
51protected:
52 GenericLinkServiceFixture()
Eric Newberrya98bf932015-09-21 00:58:47 -070053 {
Eric Newberrya98bf932015-09-21 00:58:47 -070054 // By default, GenericLinkService is created with default options.
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040055 // Test cases may invoke initialize() with alternate options.
56 initialize({});
Eric Newberrya98bf932015-09-21 00:58:47 -070057 }
58
59 void
Eric Newberryb49313d2017-12-24 20:22:27 -070060 initialize(const GenericLinkService::Options& options,
61 ssize_t mtu = MTU_UNLIMITED,
62 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
Eric Newberrya98bf932015-09-21 00:58:47 -070063 {
Davide Pesaventoe4b22382018-06-10 14:37:24 -040064 face = make_unique<Face>(make_unique<GenericLinkService>(options),
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040065 make_unique<DummyTransport>("dummy://", "dummy://",
Davide Pesaventoe4b22382018-06-10 14:37:24 -040066 ndn::nfd::FACE_SCOPE_NON_LOCAL,
67 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
68 ndn::nfd::LINK_TYPE_POINT_TO_POINT,
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040069 mtu, sendQueueCapacity));
Eric Newberrya98bf932015-09-21 00:58:47 -070070 service = static_cast<GenericLinkService*>(face->getLinkService());
71 transport = static_cast<DummyTransport*>(face->getTransport());
72
73 face->afterReceiveInterest.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000074 [this] (const Interest& interest, const EndpointId&) { receivedInterests.push_back(interest); });
Eric Newberrya98bf932015-09-21 00:58:47 -070075 face->afterReceiveData.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000076 [this] (const Data& data, const EndpointId&) { receivedData.push_back(data); });
Eric Newberrya98bf932015-09-21 00:58:47 -070077 face->afterReceiveNack.connect(
ashiqopu075bb7d2019-03-10 01:38:21 +000078 [this] (const lp::Nack& nack, const EndpointId&) { receivedNacks.push_back(nack); });
Eric Newberrya98bf932015-09-21 00:58:47 -070079 }
80
Teng Liangbce3cb52018-09-05 20:08:27 -070081 lp::PrefixAnnouncementHeader
82 makePrefixAnnHeader(const Name& announcedName)
83 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040084 return lp::PrefixAnnouncementHeader{signPrefixAnn(makePrefixAnn(announcedName, 1_h),
85 m_keyChain, ndn::signingWithSha256())};
Teng Liangbce3cb52018-09-05 20:08:27 -070086 }
87
Eric Newberrya98bf932015-09-21 00:58:47 -070088protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070089 unique_ptr<Face> face;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040090 GenericLinkService* service = nullptr;
91 DummyTransport* transport = nullptr;
Eric Newberrya98bf932015-09-21 00:58:47 -070092 std::vector<Interest> receivedInterests;
93 std::vector<Data> receivedData;
94 std::vector<lp::Nack> receivedNacks;
95};
96
97BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
98
Eric Newberrya98bf932015-09-21 00:58:47 -070099BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
100
101BOOST_AUTO_TEST_CASE(SendInterest)
102{
Eric Newberry86d31872015-09-23 16:24:59 -0700103 // Initialize with Options that disables all services
104 GenericLinkService::Options options;
105 options.allowLocalFields = false;
106 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700107
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400108 auto interest1 = makeInterest("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700109 face->sendInterest(*interest1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700110
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700111 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700112 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700113 lp::Packet interest1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700114 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
115 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700116}
117
118BOOST_AUTO_TEST_CASE(SendData)
119{
Eric Newberry86d31872015-09-23 16:24:59 -0700120 // Initialize with Options that disables all services
121 GenericLinkService::Options options;
122 options.allowLocalFields = false;
123 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700124
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400125 auto data1 = makeData("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700126 face->sendData(*data1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700127
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700128 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700129 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700130 lp::Packet data1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700131 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
132 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700133}
134
Eric Newberrycb6551e2020-03-02 14:12:16 -0800135BOOST_AUTO_TEST_CASE(SendDataOverrideMtu)
136{
137 // Initialize with Options that disables all services and does not override MTU
138 GenericLinkService::Options options;
139 options.allowLocalFields = false;
140 initialize(options);
141
142 BOOST_CHECK_EQUAL(transport->getMtu(), MTU_UNLIMITED);
143 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), MTU_UNLIMITED);
144 BOOST_CHECK_EQUAL(face->getMtu(), MTU_UNLIMITED);
145 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_UNLIMITED), false);
146 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_INVALID), false);
147 // Attempts to override MTU will fail when transport MTU is unlimited
148 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(4000), false);
149
150 // Initialize with Options that disables all services and overrides MTU (Transport MTU 8800)
151 options.overrideMtu = MIN_MTU;
152 initialize(options, ndn::MAX_NDN_PACKET_SIZE);
153
154 // Ensure effective MTU is override value
155 BOOST_CHECK_EQUAL(transport->getMtu(), ndn::MAX_NDN_PACKET_SIZE);
156 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), MIN_MTU);
157 BOOST_CHECK_EQUAL(face->getMtu(), MIN_MTU);
158
159 // Check MTU overrides with Transport MTU finite
160 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_UNLIMITED), false);
161 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MTU_INVALID), false);
162 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MIN_MTU - 1), false);
163 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(MIN_MTU), true);
164 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(4000), true);
165 BOOST_CHECK_EQUAL(service->canOverrideMtuTo(20000), true);
166
167 // Send Data with less than MIN_MTU octets
168 auto data1 = makeData("/localhost");
169 BOOST_CHECK_LE(data1->wireEncode().size(), MIN_MTU);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700170 face->sendData(*data1);
Eric Newberrycb6551e2020-03-02 14:12:16 -0800171
172 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
173 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 0);
174
175 // Send Data with more than MIN_MTU octets
176 auto data2 = makeData("/localhost/test/1234567890/1234567890/1234567890/1234567890");
177 BOOST_CHECK_GT(data2->wireEncode().size(), MIN_MTU);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700178 face->sendData(*data2);
Eric Newberrycb6551e2020-03-02 14:12:16 -0800179
180 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 2);
181 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
182
183 // Override MTU greater than the Transport's MTU will not be utilized
184 options.overrideMtu = 5000;
185 initialize(options, 4000);
186 BOOST_CHECK_EQUAL(service->getEffectiveMtu(), 4000);
187 BOOST_CHECK_EQUAL(face->getMtu(), 4000);
188}
189
Eric Newberrya98bf932015-09-21 00:58:47 -0700190BOOST_AUTO_TEST_CASE(SendNack)
191{
Eric Newberry86d31872015-09-23 16:24:59 -0700192 // Initialize with Options that disables all services
193 GenericLinkService::Options options;
194 options.allowLocalFields = false;
195 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700196
Junxiao Shi9d727852019-05-14 13:44:22 -0600197 auto nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
198 lp::NackReason::NO_ROUTE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700199 face->sendNack(nack1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700200
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700201 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700202 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700203 lp::Packet nack1pkt(transport->sentPackets.back());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700204 BOOST_CHECK(nack1pkt.has<lp::NackField>());
205 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
206 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700207}
208
209BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
210{
Eric Newberry86d31872015-09-23 16:24:59 -0700211 // Initialize with Options that disables all services
212 GenericLinkService::Options options;
213 options.allowLocalFields = false;
214 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700215
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400216 auto interest1 = makeInterest("/23Rd9hEiR");
Eric Newberrya98bf932015-09-21 00:58:47 -0700217 transport->receivePacket(interest1->wireEncode());
218
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700219 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700220 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400221 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700222}
223
224BOOST_AUTO_TEST_CASE(ReceiveInterest)
225{
Eric Newberry86d31872015-09-23 16:24:59 -0700226 // Initialize with Options that disables all services
227 GenericLinkService::Options options;
228 options.allowLocalFields = false;
229 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700230
Junxiao Shi9d727852019-05-14 13:44:22 -0600231 auto interest1 = makeInterest("/23Rd9hEiR");
Eric Newberrya98bf932015-09-21 00:58:47 -0700232 lp::Packet lpPacket;
233 lpPacket.set<lp::FragmentField>(std::make_pair(
234 interest1->wireEncode().begin(), interest1->wireEncode().end()));
235 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
236
237 transport->receivePacket(lpPacket.wireEncode());
238
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700239 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700240 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400241 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700242}
243
244BOOST_AUTO_TEST_CASE(ReceiveBareData)
245{
Eric Newberry86d31872015-09-23 16:24:59 -0700246 // Initialize with Options that disables all services
247 GenericLinkService::Options options;
248 options.allowLocalFields = false;
249 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700250
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400251 auto data1 = makeData("/12345678");
Eric Newberrya98bf932015-09-21 00:58:47 -0700252 transport->receivePacket(data1->wireEncode());
253
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700254 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700255 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400256 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700257}
258
259BOOST_AUTO_TEST_CASE(ReceiveData)
260{
Eric Newberry86d31872015-09-23 16:24:59 -0700261 // Initialize with Options that disables all services
262 GenericLinkService::Options options;
263 options.allowLocalFields = false;
264 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700265
Junxiao Shi9d727852019-05-14 13:44:22 -0600266 auto data1 = makeData("/12345689");
Eric Newberrya98bf932015-09-21 00:58:47 -0700267 lp::Packet lpPacket;
268 lpPacket.set<lp::FragmentField>(std::make_pair(
269 data1->wireEncode().begin(), data1->wireEncode().end()));
270 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
271
272 transport->receivePacket(lpPacket.wireEncode());
273
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700274 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700275 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400276 BOOST_CHECK_EQUAL(receivedData.back().wireEncode(), data1->wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700277}
278
279BOOST_AUTO_TEST_CASE(ReceiveNack)
280{
Eric Newberry86d31872015-09-23 16:24:59 -0700281 // Initialize with Options that disables all services
282 GenericLinkService::Options options;
283 options.allowLocalFields = false;
284 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700285
Junxiao Shi9d727852019-05-14 13:44:22 -0600286 lp::Nack nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
287 lp::NackReason::NO_ROUTE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700288 lp::Packet lpPacket;
289 lpPacket.set<lp::FragmentField>(std::make_pair(
290 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
291 lpPacket.set<lp::NackField>(nack1.getHeader());
292
293 transport->receivePacket(lpPacket.wireEncode());
294
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700295 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700296 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400297 BOOST_CHECK_EQUAL(receivedNacks.back().getReason(), nack1.getReason());
298 BOOST_CHECK_EQUAL(receivedNacks.back().getInterest().wireEncode(), nack1.getInterest().wireEncode());
Eric Newberrya98bf932015-09-21 00:58:47 -0700299}
300
Eric Newberrya1939ba2015-10-09 12:35:03 -0700301BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
302{
303 // Initialize with Options that disables all services
304 GenericLinkService::Options options;
305 options.allowLocalFields = false;
306 initialize(options);
307
308 lp::Packet lpPacket;
309 lpPacket.set<lp::SequenceField>(0);
310
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400311 transport->receivePacket(lpPacket.wireEncode());
Eric Newberrya1939ba2015-10-09 12:35:03 -0700312
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700313 // IDLE packet should be ignored, but is not an error
314 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700315 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
316 BOOST_CHECK_EQUAL(receivedData.size(), 0);
317 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
318}
319
Eric Newberrya98bf932015-09-21 00:58:47 -0700320BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
321
Eric Newberry86d31872015-09-23 16:24:59 -0700322BOOST_AUTO_TEST_SUITE(Fragmentation)
323
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700324BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
325{
326 // Initialize with Options that disable fragmentation
327 GenericLinkService::Options options;
328 options.allowFragmentation = false;
329 initialize(options);
330
331 transport->setMtu(55);
332
Junxiao Shi9d727852019-05-14 13:44:22 -0600333 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700334 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700335
336 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
337 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
338}
339
Eric Newberryf3ee8082020-01-28 13:44:18 -0800340// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
341// This test case ensures that packets are dropped if the MTU is zero
342BOOST_AUTO_TEST_CASE(FragmentationDisabledZeroMtuDrop)
343{
344 // Initialize with Options that disable fragmentation
345 GenericLinkService::Options options;
346 options.allowFragmentation = false;
347 initialize(options);
348
349 transport->setMtu(0);
350
351 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700352 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800353
354 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
355 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
356}
357
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700358BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
359{
360 // Initialize with Options that enable fragmentation
361 GenericLinkService::Options options;
362 options.allowFragmentation = true;
363 initialize(options);
364
365 transport->setMtu(MTU_UNLIMITED);
366
Junxiao Shi9d727852019-05-14 13:44:22 -0600367 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700368 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700369
370 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
371}
372
373BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
374{
375 // Initialize with Options that enable fragmentation
376 GenericLinkService::Options options;
377 options.allowFragmentation = true;
378 initialize(options);
379
380 transport->setMtu(105);
381
Junxiao Shi9d727852019-05-14 13:44:22 -0600382 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700383 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700384
385 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
386}
387
388BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
389{
390 // Initialize with Options that enable fragmentation
391 GenericLinkService::Options options;
392 options.allowFragmentation = true;
393 initialize(options);
394
395 transport->setMtu(60);
396
Junxiao Shi9d727852019-05-14 13:44:22 -0600397 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700398 face->sendData(*data);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700399
400 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
401}
402
Eric Newberryf3ee8082020-01-28 13:44:18 -0800403// It is possible for some interfaces (e.g., virtual Ethernet) to have their MTU set to zero
404// This test case ensures that packets are dropped if the MTU is zero
405BOOST_AUTO_TEST_CASE(FragmentationZeroMtuDrop)
406{
407 // Initialize with Options that enable fragmentation
408 GenericLinkService::Options options;
409 options.allowFragmentation = true;
410 initialize(options);
411
412 transport->setMtu(0);
413
414 auto data = makeData("/test/data/123456789/987654321/123456789");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700415 face->sendData(*data);
Eric Newberryf3ee8082020-01-28 13:44:18 -0800416
417 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
418 BOOST_CHECK_EQUAL(service->getCounters().nFragmentationErrors, 1);
419}
420
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700421BOOST_AUTO_TEST_CASE(ReassembleFragments)
422{
423 // Initialize with Options that enables reassembly
424 GenericLinkService::Options options;
425 options.allowReassembly = true;
426 initialize(options);
427
Junxiao Shi9d727852019-05-14 13:44:22 -0600428 auto interest = makeInterest(
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700429 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
430 lp::Packet packet(interest->wireEncode());
431
432 // fragment the packet
Davide Pesaventoe4b22382018-06-10 14:37:24 -0400433 LpFragmenter fragmenter({});
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700434 size_t mtu = 100;
435 bool isOk = false;
436 std::vector<lp::Packet> frags;
437 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
438 BOOST_REQUIRE(isOk);
439 BOOST_CHECK_GT(frags.size(), 1);
440
441 // receive the fragments
442 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
443 size_t sequence = 1000 + fragIndex;
444 frags[fragIndex].add<lp::SequenceField>(sequence);
445
446 transport->receivePacket(frags[fragIndex].wireEncode());
447
448 if (fragIndex > 0) {
449 BOOST_CHECK(receivedInterests.empty());
450 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
451 }
452 else {
453 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400454 BOOST_CHECK_EQUAL(receivedInterests.back().wireEncode(), interest->wireEncode());
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700455 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
456 }
457 }
458}
459
Eric Newberry86d31872015-09-23 16:24:59 -0700460BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
461{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700462 // Initialize with Options that disables reassembly
463 GenericLinkService::Options options;
464 options.allowReassembly = false;
465 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700466
Junxiao Shi9d727852019-05-14 13:44:22 -0600467 auto interest = makeInterest("/IgFe6NvH");
Eric Newberry86d31872015-09-23 16:24:59 -0700468 lp::Packet packet(interest->wireEncode());
469 packet.set<lp::FragIndexField>(140);
470
471 transport->receivePacket(packet.wireEncode());
472
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700473 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700474 BOOST_CHECK(receivedInterests.empty());
475}
476
477BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
478{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700479 // Initialize with Options that disables reassembly
480 GenericLinkService::Options options;
481 options.allowReassembly = false;
482 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700483
Junxiao Shi9d727852019-05-14 13:44:22 -0600484 auto interest = makeInterest("/SeGmEjvIVX");
Eric Newberry86d31872015-09-23 16:24:59 -0700485 lp::Packet packet(interest->wireEncode());
486 packet.set<lp::FragCountField>(276);
487
488 transport->receivePacket(packet.wireEncode());
489
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700490 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700491 BOOST_CHECK(receivedInterests.empty());
492}
493
494BOOST_AUTO_TEST_SUITE_END() // Fragmentation
495
Eric Newberry185ab292017-03-28 06:45:39 +0000496BOOST_AUTO_TEST_SUITE(Reliability)
497
Eric Newberry7b0071e2017-07-03 17:33:31 +0000498BOOST_AUTO_TEST_CASE(SendInterest)
Eric Newberry185ab292017-03-28 06:45:39 +0000499{
500 // Initialize with Options that enables reliability
501 GenericLinkService::Options options;
502 options.allowLocalFields = false;
503 options.reliabilityOptions.isEnabled = true;
504 initialize(options);
505
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400506 auto interest1 = makeInterest("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700507 face->sendInterest(*interest1);
Eric Newberry185ab292017-03-28 06:45:39 +0000508
509 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
510 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700511 lp::Packet interest1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000512 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000513 BOOST_CHECK(interest1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000514}
515
Eric Newberry7b0071e2017-07-03 17:33:31 +0000516BOOST_AUTO_TEST_CASE(SendData)
Eric Newberry185ab292017-03-28 06:45:39 +0000517{
518 // Initialize with Options that enables reliability
519 GenericLinkService::Options options;
520 options.allowLocalFields = false;
521 options.reliabilityOptions.isEnabled = true;
522 initialize(options);
523
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400524 auto data1 = makeData("/localhost/test");
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700525 face->sendData(*data1);
Eric Newberry185ab292017-03-28 06:45:39 +0000526
527 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
528 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700529 lp::Packet data1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000530 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000531 BOOST_CHECK(data1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000532}
533
Eric Newberry7b0071e2017-07-03 17:33:31 +0000534BOOST_AUTO_TEST_CASE(SendNack)
Eric Newberry185ab292017-03-28 06:45:39 +0000535{
536 // Initialize with Options that enables reliability
537 GenericLinkService::Options options;
538 options.allowLocalFields = false;
539 options.reliabilityOptions.isEnabled = true;
540 initialize(options);
541
Junxiao Shi9d727852019-05-14 13:44:22 -0600542 auto nack1 = makeNack(*makeInterest("/localhost/test", false, nullopt, 323),
543 lp::NackReason::NO_ROUTE);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700544 face->sendNack(nack1);
Eric Newberry185ab292017-03-28 06:45:39 +0000545
546 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
547 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700548 lp::Packet nack1pkt(transport->sentPackets.back());
Eric Newberry185ab292017-03-28 06:45:39 +0000549 BOOST_CHECK(nack1pkt.has<lp::NackField>());
550 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000551 BOOST_CHECK(nack1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000552}
553
Eric Newberry32f7eac2020-02-07 14:40:17 -0800554BOOST_AUTO_TEST_CASE(DropDuplicatePacket)
555{
556 // Initialize with Options that enables reliability
557 GenericLinkService::Options options;
558 options.allowLocalFields = false;
559 options.reliabilityOptions.isEnabled = true;
560 initialize(options);
561
562 Interest interest("/test/prefix");
563 interest.setCanBePrefix(false);
564 lp::Packet pkt1;
565 pkt1.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
566 pkt1.add<lp::SequenceField>(7);
567 pkt1.add<lp::TxSequenceField>(12);
568 transport->receivePacket(pkt1.wireEncode());
569 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
570 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 0);
571
572 lp::Packet pkt2;
573 pkt2.add<lp::FragmentField>({interest.wireEncode().begin(), interest.wireEncode().end()});
574 pkt2.add<lp::SequenceField>(7);
575 pkt2.add<lp::TxSequenceField>(13);
576 transport->receivePacket(pkt2.wireEncode());
577 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
578 BOOST_CHECK_EQUAL(service->getCounters().nDuplicateSequence, 1);
579}
580
Eric Newberry185ab292017-03-28 06:45:39 +0000581BOOST_AUTO_TEST_SUITE_END() // Reliability
Eric Newberry86d31872015-09-23 16:24:59 -0700582
Eric Newberryb49313d2017-12-24 20:22:27 -0700583// congestion detection and marking
584BOOST_AUTO_TEST_SUITE(CongestionMark)
585
586BOOST_AUTO_TEST_CASE(NoCongestion)
587{
588 GenericLinkService::Options options;
589 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400590 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700591 initialize(options, MTU_UNLIMITED, 65536);
592 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
593 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
594 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
595
Junxiao Shi9d727852019-05-14 13:44:22 -0600596 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700597
598 // congestion threshold will be 32768 bytes, since min(65536, 65536 / 2) = 32768 bytes
599
600 // no congestion
601 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700602 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700603 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700604 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700605 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
606 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
607 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
608 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
609
610 // no congestion
611 transport->setSendQueueLength(32768);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700612 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700613 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700614 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700615 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
616 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
617 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
618 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
619}
620
621BOOST_AUTO_TEST_CASE(CongestionCoDel)
622{
623 GenericLinkService::Options options;
624 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400625 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700626 initialize(options, MTU_UNLIMITED, 65536);
627 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700628 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
629 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
630
Junxiao Shi9d727852019-05-14 13:44:22 -0600631 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700632
Klaus Schneider380668b2019-10-02 01:21:13 -0700633 // first congested packet, will not be marked
634 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700635 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700636 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700637 lp::Packet pkt0(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700638 BOOST_REQUIRE_EQUAL(pkt0.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400639 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700640 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700641
642 time::nanoseconds markingInterval(
643 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
644 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
645
646 advanceClocks(markingInterval + 1_ms);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700647 face->sendInterest(*interest);
Teng Liang13d582a2020-07-21 20:23:11 -0700648 lp::Packet pkt1(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700649
650 // First congestion mark appears after one interval (100 ms)
651 BOOST_REQUIRE_EQUAL(pkt1.count<lp::CongestionMarkField>(), 1);
652 BOOST_CHECK_EQUAL(pkt1.get<lp::CongestionMarkField>(), 1);
Eric Newberryb49313d2017-12-24 20:22:27 -0700653 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
654 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
655
656 // advance clock to half of marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700657 advanceClocks(markingInterval / 2); // 50ms
Eric Newberryb49313d2017-12-24 20:22:27 -0700658
659 // second congested packet, but within marking interval, will not be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700660 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700661 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700662 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700663 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700664 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700665
666 markingInterval = time::nanoseconds(
667 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
668 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
669 nextMarkTime += markingInterval;
670
Eric Newberryb49313d2017-12-24 20:22:27 -0700671 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700672 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
673 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 1);
674
675 // advance clocks past end of initial interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700676 this->advanceClocks((markingInterval / 2) + 1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700677
678 // first congested packet after waiting marking interval, will be marked
Klaus Schneider380668b2019-10-02 01:21:13 -0700679 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700680 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700681 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 4);
Teng Liang13d582a2020-07-21 20:23:11 -0700682 lp::Packet pkt3(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700683 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 1);
684 BOOST_CHECK_EQUAL(pkt3.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700685 markingInterval = time::nanoseconds(
686 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
687 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700688 nextMarkTime += markingInterval;
689 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700690 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
691 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
692
693 // advance clock partway through current marking interval
Klaus Schneider380668b2019-10-02 01:21:13 -0700694 this->advanceClocks(markingInterval - 20_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700695
696 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700697 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700698 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700699 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 5);
Teng Liang13d582a2020-07-21 20:23:11 -0700700 lp::Packet pkt4(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700701 BOOST_CHECK_EQUAL(pkt4.count<lp::CongestionMarkField>(), 0);
702 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700703 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
704 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 2);
705
706 // advance clocks past end of current marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700707 this->advanceClocks(21_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700708
709 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700710 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700711 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700712 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 6);
Teng Liang13d582a2020-07-21 20:23:11 -0700713 lp::Packet pkt5(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700714 BOOST_REQUIRE_EQUAL(pkt5.count<lp::CongestionMarkField>(), 1);
715 BOOST_CHECK_EQUAL(pkt5.get<lp::CongestionMarkField>(), 1);
716 markingInterval = time::nanoseconds(
717 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700718 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700719 nextMarkTime += markingInterval;
720 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700721 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
722 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
723
Davide Pesavento14e71f02019-03-28 17:35:25 -0400724 this->advanceClocks(1_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700725
726 // still congested, but within marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700727 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700728 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700729 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 7);
Teng Liang13d582a2020-07-21 20:23:11 -0700730 lp::Packet pkt6(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700731 BOOST_CHECK_EQUAL(pkt6.count<lp::CongestionMarkField>(), 0);
732 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700733 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 3);
734 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 3);
735
736 this->advanceClocks(markingInterval);
737
738 // still congested, after marking interval cycle
Klaus Schneider380668b2019-10-02 01:21:13 -0700739 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700740 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700741 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 8);
Teng Liang13d582a2020-07-21 20:23:11 -0700742 lp::Packet pkt7(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700743 BOOST_REQUIRE_EQUAL(pkt7.count<lp::CongestionMarkField>(), 1);
744 BOOST_CHECK_EQUAL(pkt7.get<lp::CongestionMarkField>(), 1);
745 markingInterval = time::nanoseconds(
746 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700747 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700748 nextMarkTime += markingInterval;
749 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700750 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 4);
751 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
752
753 // no more congestion
754 transport->setSendQueueLength(30000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700755 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700756 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 9);
Teng Liang13d582a2020-07-21 20:23:11 -0700757 lp::Packet pkt8(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700758 BOOST_CHECK_EQUAL(pkt8.count<lp::CongestionMarkField>(), 0);
759 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700760 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
761 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
762
Davide Pesavento14e71f02019-03-28 17:35:25 -0400763 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700764
765 // send queue congested again, but can't mark packet because within one full interval of last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700766 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700767 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700768 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 10);
Teng Liang13d582a2020-07-21 20:23:11 -0700769 lp::Packet pkt9(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700770 BOOST_CHECK_EQUAL(pkt9.count<lp::CongestionMarkField>(), 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700771 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
772 markingInterval = time::nanoseconds(
773 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
774 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
775 nextMarkTime = time::steady_clock::now() + markingInterval;
776 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700777 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
778 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 4);
779
780 // advance clock past full 100ms interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700781 this->advanceClocks(markingInterval + 2_ms);
782 BOOST_CHECK_GT(time::steady_clock::now(), nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700783
Klaus Schneider380668b2019-10-02 01:21:13 -0700784 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700785 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700786 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 11);
Teng Liang13d582a2020-07-21 20:23:11 -0700787 lp::Packet pkt10(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700788 BOOST_REQUIRE_EQUAL(pkt10.count<lp::CongestionMarkField>(), 1);
789 BOOST_CHECK_EQUAL(pkt10.get<lp::CongestionMarkField>(), 1);
Klaus Schneider380668b2019-10-02 01:21:13 -0700790 markingInterval = time::nanoseconds(
791 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
792 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
793 nextMarkTime += markingInterval;
Eric Newberryb49313d2017-12-24 20:22:27 -0700794 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700795 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
796 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
797
798 // advance clock partway through 100ms marking interval
Davide Pesavento14e71f02019-03-28 17:35:25 -0400799 this->advanceClocks(50_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700800
801 // not marked since within 100ms window before can mark again
Klaus Schneider380668b2019-10-02 01:21:13 -0700802 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700803 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700804 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 12);
Teng Liang13d582a2020-07-21 20:23:11 -0700805 lp::Packet pkt11(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700806 BOOST_CHECK_EQUAL(pkt11.count<lp::CongestionMarkField>(), 0);
807 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700808 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 1);
809 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 5);
810
811 // advance clocks past m_nextMarkTime
Davide Pesavento14e71f02019-03-28 17:35:25 -0400812 this->advanceClocks(51_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700813
814 // markable packet, queue length still above threshold
Klaus Schneider380668b2019-10-02 01:21:13 -0700815 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700816 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700817 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 13);
Teng Liang13d582a2020-07-21 20:23:11 -0700818 lp::Packet pkt12(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700819 BOOST_REQUIRE_EQUAL(pkt12.count<lp::CongestionMarkField>(), 1);
820 BOOST_CHECK_EQUAL(pkt12.get<lp::CongestionMarkField>(), 1);
821 markingInterval = time::nanoseconds(
822 static_cast<time::nanoseconds::rep>(options.baseCongestionMarkingInterval.count() /
Klaus Schneider380668b2019-10-02 01:21:13 -0700823 std::sqrt(service->m_nMarkedSinceInMarkingState + 1)));
Eric Newberryb49313d2017-12-24 20:22:27 -0700824 nextMarkTime += markingInterval;
825 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Eric Newberryb49313d2017-12-24 20:22:27 -0700826 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 2);
827 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
828
829 // no more congestion
830 transport->setSendQueueLength(10000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700831 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700832 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 14);
Teng Liang13d582a2020-07-21 20:23:11 -0700833 lp::Packet pkt13(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700834 BOOST_CHECK_EQUAL(pkt13.count<lp::CongestionMarkField>(), 0);
835 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700836 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
837 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
838
839 // advance clocks past one full interval since last mark
Klaus Schneider380668b2019-10-02 01:21:13 -0700840 this->advanceClocks(102_ms);
Eric Newberryb49313d2017-12-24 20:22:27 -0700841
842 // start congestion again
Klaus Schneider380668b2019-10-02 01:21:13 -0700843 transport->setSendQueueLength(66000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700844 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700845 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 15);
Teng Liang13d582a2020-07-21 20:23:11 -0700846 lp::Packet pkt14(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700847 BOOST_REQUIRE_EQUAL(pkt14.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400848 nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700849 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700850 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
851 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700852
853 // no more congestion, cancel marking interval
854 transport->setSendQueueLength(5000);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700855 face->sendInterest(*interest);
Klaus Schneider380668b2019-10-02 01:21:13 -0700856 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 16);
Teng Liang13d582a2020-07-21 20:23:11 -0700857 lp::Packet pkt15(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700858 BOOST_CHECK_EQUAL(pkt15.count<lp::CongestionMarkField>(), 0);
859 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
Eric Newberryb49313d2017-12-24 20:22:27 -0700860 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
Klaus Schneider380668b2019-10-02 01:21:13 -0700861 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 6);
Eric Newberryb49313d2017-12-24 20:22:27 -0700862}
863
864BOOST_AUTO_TEST_CASE(DefaultThreshold)
865{
866 GenericLinkService::Options options;
867 options.allowCongestionMarking = true;
Davide Pesavento14e71f02019-03-28 17:35:25 -0400868 options.baseCongestionMarkingInterval = 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700869 initialize(options, MTU_UNLIMITED, QUEUE_UNSUPPORTED);
870 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
871 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
872 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
873
Junxiao Shi9d727852019-05-14 13:44:22 -0600874 auto interest = makeInterest("/12345678");
Eric Newberryb49313d2017-12-24 20:22:27 -0700875
876 // congestion threshold will be 65536 bytes, since the transport reports that it cannot measure
877 // the queue capacity
878
879 // no congestion
880 transport->setSendQueueLength(0);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700881 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700882 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -0700883 lp::Packet pkt1(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700884 BOOST_CHECK_EQUAL(pkt1.count<lp::CongestionMarkField>(), 0);
885 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
886 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
887 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
888
889 // no congestion
890 transport->setSendQueueLength(65536);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700891 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700892 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 2);
Teng Liang13d582a2020-07-21 20:23:11 -0700893 lp::Packet pkt2(transport->sentPackets.back());
Eric Newberryb49313d2017-12-24 20:22:27 -0700894 BOOST_CHECK_EQUAL(pkt2.count<lp::CongestionMarkField>(), 0);
895 BOOST_CHECK_EQUAL(service->m_nextMarkTime, time::steady_clock::TimePoint::max());
896 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
897 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
898
Klaus Schneider380668b2019-10-02 01:21:13 -0700899 // first congested (not marked yet) packet
Eric Newberryb49313d2017-12-24 20:22:27 -0700900 transport->setSendQueueLength(65537);
Teng Liangf3bc3ae2020-06-08 10:19:25 -0700901 face->sendInterest(*interest);
Eric Newberryb49313d2017-12-24 20:22:27 -0700902 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 3);
Teng Liang13d582a2020-07-21 20:23:11 -0700903 lp::Packet pkt3(transport->sentPackets.back());
Klaus Schneider380668b2019-10-02 01:21:13 -0700904 BOOST_REQUIRE_EQUAL(pkt3.count<lp::CongestionMarkField>(), 0);
Davide Pesavento14e71f02019-03-28 17:35:25 -0400905 time::steady_clock::TimePoint nextMarkTime = time::steady_clock::now() + 100_ms;
Eric Newberryb49313d2017-12-24 20:22:27 -0700906 BOOST_CHECK_EQUAL(service->m_nextMarkTime, nextMarkTime);
Klaus Schneider380668b2019-10-02 01:21:13 -0700907 BOOST_CHECK_EQUAL(service->m_nMarkedSinceInMarkingState, 0);
908 BOOST_CHECK_EQUAL(service->getCounters().nCongestionMarked, 0);
Eric Newberryb49313d2017-12-24 20:22:27 -0700909}
910
911BOOST_AUTO_TEST_SUITE_END() // CongestionMark
912
Eric Newberryee400b52016-11-24 14:12:48 +0000913BOOST_AUTO_TEST_SUITE(LpFields)
Eric Newberrya98bf932015-09-21 00:58:47 -0700914
Eric Newberrya98bf932015-09-21 00:58:47 -0700915BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
916{
Eric Newberry86d31872015-09-23 16:24:59 -0700917 // Initialize with Options that enables local fields
918 GenericLinkService::Options options;
919 options.allowLocalFields = true;
920 initialize(options);
921
Junxiao Shi9d727852019-05-14 13:44:22 -0600922 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700923 lp::Packet packet(interest->wireEncode());
924 packet.set<lp::NextHopFaceIdField>(1000);
925
926 transport->receivePacket(packet.wireEncode());
927
928 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -0400929 auto tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +0000930 BOOST_REQUIRE(tag != nullptr);
931 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700932}
933
934BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
935{
Eric Newberry86d31872015-09-23 16:24:59 -0700936 // Initialize with Options that disables local fields
937 GenericLinkService::Options options;
938 options.allowLocalFields = false;
939 initialize(options);
940
Junxiao Shi9d727852019-05-14 13:44:22 -0600941 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700942 lp::Packet packet(interest->wireEncode());
943 packet.set<lp::NextHopFaceIdField>(1000);
944
945 transport->receivePacket(packet.wireEncode());
946
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700947 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700948 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700949}
950
951BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
952{
Eric Newberry86d31872015-09-23 16:24:59 -0700953 // Initialize with Options that enables local fields
954 GenericLinkService::Options options;
955 options.allowLocalFields = true;
956 initialize(options);
957
Junxiao Shi9d727852019-05-14 13:44:22 -0600958 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700959 lp::Packet packet(data->wireEncode());
960 packet.set<lp::NextHopFaceIdField>(1000);
961
962 transport->receivePacket(packet.wireEncode());
963
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700964 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700965 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700966}
967
968BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
969{
Eric Newberry86d31872015-09-23 16:24:59 -0700970 // Initialize with Options that enables local fields
971 GenericLinkService::Options options;
972 options.allowLocalFields = true;
973 initialize(options);
974
Junxiao Shi9d727852019-05-14 13:44:22 -0600975 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
976 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -0700977 lp::Packet packet;
978 packet.set<lp::FragmentField>(std::make_pair(
979 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
980 packet.set<lp::NackField>(nack.getHeader());
981 packet.set<lp::NextHopFaceIdField>(1000);
982
983 transport->receivePacket(packet.wireEncode());
984
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700985 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700986 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700987}
988
Junxiao Shi6eb02712017-05-27 22:48:02 +0000989BOOST_AUTO_TEST_CASE(ReceiveCachePolicy)
Eric Newberrya98bf932015-09-21 00:58:47 -0700990{
Junxiao Shi6eb02712017-05-27 22:48:02 +0000991 // Initialize with Options that disables local fields
Eric Newberry86d31872015-09-23 16:24:59 -0700992 GenericLinkService::Options options;
Junxiao Shi6eb02712017-05-27 22:48:02 +0000993 options.allowLocalFields = false;
Eric Newberry86d31872015-09-23 16:24:59 -0700994 initialize(options);
Junxiao Shi6eb02712017-05-27 22:48:02 +0000995 // CachePolicy is unprivileged and does not require allowLocalFields option.
Eric Newberry86d31872015-09-23 16:24:59 -0700996
Junxiao Shi9d727852019-05-14 13:44:22 -0600997 auto data = makeData("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -0700998 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000999 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -07001000
1001 transport->receivePacket(packet.wireEncode());
1002
1003 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001004 auto tag = receivedData.back().getTag<lp::CachePolicyTag>();
Junxiao Shi0de23a22015-12-03 20:07:02 +00001005 BOOST_REQUIRE(tag != nullptr);
1006 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -07001007}
1008
Junxiao Shi6eb02712017-05-27 22:48:02 +00001009BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropInterest)
Eric Newberrya98bf932015-09-21 00:58:47 -07001010{
Eric Newberry86d31872015-09-23 16:24:59 -07001011 // Initialize with Options that enables local fields
1012 GenericLinkService::Options options;
1013 options.allowLocalFields = true;
1014 initialize(options);
1015
Junxiao Shi9d727852019-05-14 13:44:22 -06001016 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001017 lp::Packet packet(interest->wireEncode());
1018 lp::CachePolicy policy;
1019 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1020 packet.set<lp::CachePolicyField>(policy);
1021
1022 transport->receivePacket(packet.wireEncode());
1023
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001024 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001025 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -07001026}
1027
Junxiao Shi6eb02712017-05-27 22:48:02 +00001028BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropNack)
Eric Newberrya98bf932015-09-21 00:58:47 -07001029{
Eric Newberry86d31872015-09-23 16:24:59 -07001030 // Initialize with Options that enables local fields
1031 GenericLinkService::Options options;
1032 options.allowLocalFields = true;
1033 initialize(options);
1034
Junxiao Shi9d727852019-05-14 13:44:22 -06001035 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1036 lp::NackReason::NO_ROUTE);
Eric Newberry86d31872015-09-23 16:24:59 -07001037 lp::Packet packet(nack.getInterest().wireEncode());
1038 packet.set<lp::NackField>(nack.getHeader());
1039 lp::CachePolicy policy;
1040 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
1041 packet.set<lp::CachePolicyField>(policy);
1042
1043 transport->receivePacket(packet.wireEncode());
1044
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001045 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -07001046 BOOST_CHECK(receivedNacks.empty());
1047}
1048
1049BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
1050{
1051 // Initialize with Options that enables local fields
1052 GenericLinkService::Options options;
1053 options.allowLocalFields = true;
1054 initialize(options);
1055
Junxiao Shi9d727852019-05-14 13:44:22 -06001056 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001057 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001058
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001059 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001060
1061 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001062 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001063 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
1064 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
1065}
1066
1067BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
1068{
1069 // Initialize with Options that disables local fields
1070 GenericLinkService::Options options;
1071 options.allowLocalFields = false;
1072 initialize(options);
1073
Junxiao Shi9d727852019-05-14 13:44:22 -06001074 auto interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +00001075 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -07001076
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001077 face->sendInterest(*interest);
Eric Newberry86d31872015-09-23 16:24:59 -07001078
1079 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001080 lp::Packet sent(transport->sentPackets.back());
Eric Newberry86d31872015-09-23 16:24:59 -07001081 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
1082}
1083
1084BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
1085{
1086 // Initialize with Options that enables local fields
1087 GenericLinkService::Options options;
1088 options.allowLocalFields = true;
1089 initialize(options);
1090
Junxiao Shi9d727852019-05-14 13:44:22 -06001091 auto interest = makeInterest("/12345678");
Eric Newberry86d31872015-09-23 16:24:59 -07001092 lp::Packet packet(interest->wireEncode());
1093 packet.set<lp::IncomingFaceIdField>(1000);
1094
1095 transport->receivePacket(packet.wireEncode());
1096
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001097 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001098 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001099 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001100}
1101
1102BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
1103{
1104 // Initialize with Options that enables local fields
1105 GenericLinkService::Options options;
1106 options.allowLocalFields = true;
1107 initialize(options);
1108
Junxiao Shi9d727852019-05-14 13:44:22 -06001109 auto data = makeData("/z1megUh9Bj");
Eric Newberry86d31872015-09-23 16:24:59 -07001110 lp::Packet packet(data->wireEncode());
1111 packet.set<lp::IncomingFaceIdField>(1000);
1112
1113 transport->receivePacket(packet.wireEncode());
1114
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001115 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001116 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001117 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -07001118}
1119
1120BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
1121{
1122 // Initialize with Options that enables local fields
1123 GenericLinkService::Options options;
1124 options.allowLocalFields = true;
1125 initialize(options);
1126
Junxiao Shi9d727852019-05-14 13:44:22 -06001127 lp::Nack nack = makeNack(*makeInterest("/TPAhdiHz", false, nullopt, 278),
1128 lp::NackReason::CONGESTION);
Eric Newberry86d31872015-09-23 16:24:59 -07001129 lp::Packet packet(nack.getInterest().wireEncode());
1130 packet.set<lp::NackField>(nack.getHeader());
1131 packet.set<lp::IncomingFaceIdField>(1000);
1132
1133 transport->receivePacket(packet.wireEncode());
1134
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001135 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -07001136 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +00001137 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -07001138}
1139
Eric Newberryee400b52016-11-24 14:12:48 +00001140BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
1141{
Junxiao Shi9d727852019-05-14 13:44:22 -06001142 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001143 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
1144
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001145 face->sendInterest(*interest);
Eric Newberryee400b52016-11-24 14:12:48 +00001146
1147 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001148 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001149 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1150 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
1151}
1152
1153BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
1154{
Junxiao Shi9d727852019-05-14 13:44:22 -06001155 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001156 data->setTag(make_shared<lp::CongestionMarkTag>(0));
1157
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001158 face->sendData(*data);
Eric Newberryee400b52016-11-24 14:12:48 +00001159
1160 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001161 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001162 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1163 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
1164}
1165
1166BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
1167{
Junxiao Shi9d727852019-05-14 13:44:22 -06001168 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1169 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001170 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
1171
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001172 face->sendNack(nack);
Eric Newberryee400b52016-11-24 14:12:48 +00001173
1174 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001175 lp::Packet sent(transport->sentPackets.back());
Eric Newberryee400b52016-11-24 14:12:48 +00001176 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
1177 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
1178}
1179
1180BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
1181{
Junxiao Shi9d727852019-05-14 13:44:22 -06001182 auto interest = makeInterest("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001183 lp::Packet packet(interest->wireEncode());
1184 packet.set<lp::CongestionMarkField>(1);
1185
1186 transport->receivePacket(packet.wireEncode());
1187
1188 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001189 auto tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001190 BOOST_REQUIRE(tag != nullptr);
1191 BOOST_CHECK_EQUAL(*tag, 1);
1192}
1193
1194BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
1195{
Junxiao Shi9d727852019-05-14 13:44:22 -06001196 auto data = makeData("/12345678");
Eric Newberryee400b52016-11-24 14:12:48 +00001197 lp::Packet packet(data->wireEncode());
1198 packet.set<lp::CongestionMarkField>(1);
1199
1200 transport->receivePacket(packet.wireEncode());
1201
1202 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001203 auto tag = receivedData.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001204 BOOST_REQUIRE(tag != nullptr);
1205 BOOST_CHECK_EQUAL(*tag, 1);
1206}
1207
1208BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
1209{
Junxiao Shi9d727852019-05-14 13:44:22 -06001210 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1211 lp::NackReason::NO_ROUTE);
Eric Newberryee400b52016-11-24 14:12:48 +00001212 lp::Packet packet;
1213 packet.set<lp::FragmentField>(std::make_pair(
1214 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1215 packet.set<lp::NackField>(nack.getHeader());
1216 packet.set<lp::CongestionMarkField>(1);
1217
1218 transport->receivePacket(packet.wireEncode());
1219
1220 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001221 auto tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
Eric Newberryee400b52016-11-24 14:12:48 +00001222 BOOST_REQUIRE(tag != nullptr);
1223 BOOST_CHECK_EQUAL(*tag, 1);
1224}
1225
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001226BOOST_AUTO_TEST_CASE(SendNonDiscovery)
1227{
1228 GenericLinkService::Options options;
1229 options.allowSelfLearning = true;
1230 initialize(options);
1231
Junxiao Shi9d727852019-05-14 13:44:22 -06001232 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001233 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1234
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001235 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001236
1237 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001238 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001239 BOOST_CHECK(sent.has<lp::NonDiscoveryField>());
1240}
1241
1242BOOST_AUTO_TEST_CASE(SendNonDiscoveryDisabled)
1243{
1244 GenericLinkService::Options options;
1245 options.allowSelfLearning = false;
1246 initialize(options);
1247
Junxiao Shi9d727852019-05-14 13:44:22 -06001248 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001249 interest->setTag(make_shared<lp::NonDiscoveryTag>(lp::EmptyValue{}));
1250
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001251 face->sendInterest(*interest);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001252
1253 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001254 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001255 BOOST_CHECK(!sent.has<lp::NonDiscoveryField>());
1256}
1257
1258BOOST_AUTO_TEST_CASE(ReceiveNonDiscovery)
1259{
1260 GenericLinkService::Options options;
1261 options.allowSelfLearning = true;
1262 initialize(options);
1263
Junxiao Shi9d727852019-05-14 13:44:22 -06001264 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001265 lp::Packet packet(interest->wireEncode());
1266 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1267
1268 transport->receivePacket(packet.wireEncode());
1269
1270 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001271 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001272 BOOST_CHECK(tag != nullptr);
1273}
1274
1275BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDisabled)
1276{
1277 GenericLinkService::Options options;
1278 options.allowSelfLearning = false;
1279 initialize(options);
1280
Junxiao Shi9d727852019-05-14 13:44:22 -06001281 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001282 lp::Packet packet(interest->wireEncode());
1283 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1284
1285 transport->receivePacket(packet.wireEncode());
1286
1287 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1288 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001289 auto tag = receivedInterests.back().getTag<lp::NonDiscoveryTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001290 BOOST_CHECK(tag == nullptr);
1291}
1292
1293BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropData)
1294{
1295 GenericLinkService::Options options;
1296 options.allowSelfLearning = true;
1297 initialize(options);
1298
Junxiao Shi9d727852019-05-14 13:44:22 -06001299 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001300 lp::Packet packet(data->wireEncode());
1301 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1302
1303 transport->receivePacket(packet.wireEncode());
1304
1305 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1306 BOOST_CHECK(receivedData.empty());
1307}
1308
1309BOOST_AUTO_TEST_CASE(ReceiveNonDiscoveryDropNack)
1310{
1311 GenericLinkService::Options options;
1312 options.allowSelfLearning = true;
1313 initialize(options);
1314
Junxiao Shi9d727852019-05-14 13:44:22 -06001315 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1316 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001317 lp::Packet packet;
1318 packet.set<lp::FragmentField>(std::make_pair(
1319 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1320 packet.set<lp::NackField>(nack.getHeader());
1321 packet.set<lp::NonDiscoveryField>(lp::EmptyValue{});
1322
1323 transport->receivePacket(packet.wireEncode());
1324
1325 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1326 BOOST_CHECK(receivedNacks.empty());
1327}
1328
1329BOOST_AUTO_TEST_CASE(SendPrefixAnnouncement)
1330{
1331 GenericLinkService::Options options;
1332 options.allowSelfLearning = true;
1333 initialize(options);
1334
Junxiao Shi9d727852019-05-14 13:44:22 -06001335 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001336 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1337 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001338
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001339 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001340
1341 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001342 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001343 BOOST_CHECK(sent.has<lp::PrefixAnnouncementField>());
1344}
1345
1346BOOST_AUTO_TEST_CASE(SendPrefixAnnouncementDisabled)
1347{
1348 GenericLinkService::Options options;
1349 options.allowSelfLearning = false;
1350 initialize(options);
1351
Junxiao Shi9d727852019-05-14 13:44:22 -06001352 auto data = makeData("/12345678");
Teng Liangbce3cb52018-09-05 20:08:27 -07001353 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1354 data->setTag(make_shared<lp::PrefixAnnouncementTag>(pah));
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001355
Teng Liangf3bc3ae2020-06-08 10:19:25 -07001356 face->sendData(*data);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001357
1358 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Teng Liang13d582a2020-07-21 20:23:11 -07001359 lp::Packet sent(transport->sentPackets.back());
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001360 BOOST_CHECK(!sent.has<lp::PrefixAnnouncementField>());
1361}
1362
1363BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncement)
1364{
1365 GenericLinkService::Options options;
1366 options.allowSelfLearning = true;
1367 initialize(options);
1368
Junxiao Shi9d727852019-05-14 13:44:22 -06001369 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001370 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001371 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1372 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001373
1374 transport->receivePacket(packet.wireEncode());
1375
1376 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001377 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangbce3cb52018-09-05 20:08:27 -07001378 BOOST_CHECK_EQUAL(tag->get().getPrefixAnn()->getAnnouncedName(), "/local/ndn/prefix");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001379}
1380
1381BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDisabled)
1382{
1383 GenericLinkService::Options options;
1384 options.allowSelfLearning = false;
1385 initialize(options);
1386
Junxiao Shi9d727852019-05-14 13:44:22 -06001387 auto data = makeData("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001388 lp::Packet packet(data->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001389 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1390 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001391
1392 transport->receivePacket(packet.wireEncode());
1393
1394 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
1395 BOOST_CHECK_EQUAL(receivedData.size(), 1);
Davide Pesavento7890a9f2019-08-25 23:11:18 -04001396 auto tag = receivedData.back().getTag<lp::PrefixAnnouncementTag>();
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001397 BOOST_CHECK(tag == nullptr);
1398}
1399
1400BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropInterest)
1401{
1402 GenericLinkService::Options options;
1403 options.allowSelfLearning = true;
1404 initialize(options);
1405
Junxiao Shi9d727852019-05-14 13:44:22 -06001406 auto interest = makeInterest("/12345678");
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001407 lp::Packet packet(interest->wireEncode());
Teng Liangbce3cb52018-09-05 20:08:27 -07001408 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1409 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001410
1411 transport->receivePacket(packet.wireEncode());
1412
1413 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1414 BOOST_CHECK(receivedInterests.empty());
1415}
1416
1417BOOST_AUTO_TEST_CASE(ReceivePrefixAnnouncementDropNack)
1418{
1419 GenericLinkService::Options options;
1420 options.allowSelfLearning = true;
1421 initialize(options);
1422
Junxiao Shi9d727852019-05-14 13:44:22 -06001423 lp::Nack nack = makeNack(*makeInterest("/localhost/test", false, nullopt, 123),
1424 lp::NackReason::NO_ROUTE);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001425 lp::Packet packet;
1426 packet.set<lp::FragmentField>(std::make_pair(
1427 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
1428 packet.set<lp::NackField>(nack.getHeader());
Teng Liangbce3cb52018-09-05 20:08:27 -07001429 auto pah = makePrefixAnnHeader("/local/ndn/prefix");
1430 packet.set<lp::PrefixAnnouncementField>(pah);
Teng Liangfdcbb4d2018-01-27 16:01:35 -07001431
1432 transport->receivePacket(packet.wireEncode());
1433
1434 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
1435 BOOST_CHECK(receivedNacks.empty());
1436}
1437
Eric Newberryee400b52016-11-24 14:12:48 +00001438BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -07001439
Eric Newberrya1939ba2015-10-09 12:35:03 -07001440BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
1441
1442BOOST_AUTO_TEST_CASE(WrongTlvType)
1443{
1444 // Initialize with Options that disables all services
1445 GenericLinkService::Options options;
1446 options.allowLocalFields = false;
1447 initialize(options);
1448
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001449 auto packet = ndn::encoding::makeEmptyBlock(tlv::Name);
1450 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001451
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001452 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001453 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1454 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1455 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1456}
1457
1458BOOST_AUTO_TEST_CASE(Unparsable)
1459{
1460 // Initialize with Options that disables all services
1461 GenericLinkService::Options options;
1462 options.allowLocalFields = false;
1463 initialize(options);
1464
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001465 auto packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
Eric Newberrya1939ba2015-10-09 12:35:03 -07001466 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
Davide Pesaventob3a23ca2019-05-04 20:40:21 -04001467 transport->receivePacket(packet);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001468
Eric Newberry4c3e6b82015-11-10 16:48:42 -07001469 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -07001470 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
1471 BOOST_CHECK_EQUAL(receivedData.size(), 0);
1472 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
1473}
1474
1475BOOST_AUTO_TEST_SUITE_END() // Malformed
1476
Eric Newberry86d31872015-09-23 16:24:59 -07001477BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
1478BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -07001479
1480} // namespace tests
1481} // namespace face
1482} // namespace nfd