blob: d252078cf7cdc1464fbc11e35e82af9ce1134ff0 [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry185ab292017-03-28 06:45:39 +00003 * Copyright (c) 2014-2017, 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#include "dummy-transport.hpp"
Junxiao Shicbc8e942016-09-06 03:17:45 +000029#include <ndn-cxx/lp/tags.hpp>
Eric Newberrya98bf932015-09-21 00:58:47 -070030
31#include "tests/test-common.hpp"
32
33namespace nfd {
34namespace face {
35namespace tests {
36
37using namespace nfd::tests;
38
39BOOST_AUTO_TEST_SUITE(Face)
40
Junxiao Shicde37ad2015-12-24 01:02:05 -070041using nfd::Face;
42
Eric Newberrya98bf932015-09-21 00:58:47 -070043class GenericLinkServiceFixture : public BaseFixture
44{
45protected:
46 GenericLinkServiceFixture()
47 : service(nullptr)
48 , transport(nullptr)
49 {
Eric Newberry86d31872015-09-23 16:24:59 -070050 this->initialize(GenericLinkService::Options());
Eric Newberrya98bf932015-09-21 00:58:47 -070051 // By default, GenericLinkService is created with default options.
52 // Test cases may invoke .initialize with alternate options.
53 }
54
55 void
Eric Newberry185ab292017-03-28 06:45:39 +000056 initialize(const GenericLinkService::Options& options, ssize_t mtu = MTU_UNLIMITED)
Eric Newberrya98bf932015-09-21 00:58:47 -070057 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070058 face.reset(new Face(make_unique<GenericLinkService>(options),
Eric Newberry185ab292017-03-28 06:45:39 +000059 make_unique<DummyTransport>("dummy://",
60 "dummy://",
61 ndn::nfd::FACE_SCOPE_NON_LOCAL,
62 ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
63 ndn::nfd::LINK_TYPE_POINT_TO_POINT,
64 mtu)));
Eric Newberrya98bf932015-09-21 00:58:47 -070065 service = static_cast<GenericLinkService*>(face->getLinkService());
66 transport = static_cast<DummyTransport*>(face->getTransport());
67
68 face->afterReceiveInterest.connect(
69 [this] (const Interest& interest) { receivedInterests.push_back(interest); });
70 face->afterReceiveData.connect(
71 [this] (const Data& data) { receivedData.push_back(data); });
72 face->afterReceiveNack.connect(
73 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
74 }
75
76protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070077 unique_ptr<Face> face;
Eric Newberrya98bf932015-09-21 00:58:47 -070078 GenericLinkService* service;
79 DummyTransport* transport;
80 std::vector<Interest> receivedInterests;
81 std::vector<Data> receivedData;
82 std::vector<lp::Nack> receivedNacks;
83};
84
85BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
86
87
88BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
89
90BOOST_AUTO_TEST_CASE(SendInterest)
91{
Eric Newberry86d31872015-09-23 16:24:59 -070092 // Initialize with Options that disables all services
93 GenericLinkService::Options options;
94 options.allowLocalFields = false;
95 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -070096
97 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
98
99 face->sendInterest(*interest1);
100
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700101 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700102 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700103 lp::Packet interest1pkt;
104 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
105 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
106 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700107}
108
109BOOST_AUTO_TEST_CASE(SendData)
110{
Eric Newberry86d31872015-09-23 16:24:59 -0700111 // Initialize with Options that disables all services
112 GenericLinkService::Options options;
113 options.allowLocalFields = false;
114 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700115
116 shared_ptr<Data> data1 = makeData("/localhost/test");
117
118 face->sendData(*data1);
119
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700120 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700121 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700122 lp::Packet data1pkt;
123 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
124 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
125 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700126}
127
128BOOST_AUTO_TEST_CASE(SendNack)
129{
Eric Newberry86d31872015-09-23 16:24:59 -0700130 // Initialize with Options that disables all services
131 GenericLinkService::Options options;
132 options.allowLocalFields = false;
133 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700134
135 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
136
137 face->sendNack(nack1);
138
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700139 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700140 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
141 lp::Packet nack1pkt;
142 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700143 BOOST_CHECK(nack1pkt.has<lp::NackField>());
144 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
145 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700146}
147
148BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
149{
Eric Newberry86d31872015-09-23 16:24:59 -0700150 // Initialize with Options that disables all services
151 GenericLinkService::Options options;
152 options.allowLocalFields = false;
153 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700154
155 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
156
157 transport->receivePacket(interest1->wireEncode());
158
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700159 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700160 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
161 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
162}
163
164BOOST_AUTO_TEST_CASE(ReceiveInterest)
165{
Eric Newberry86d31872015-09-23 16:24:59 -0700166 // Initialize with Options that disables all services
167 GenericLinkService::Options options;
168 options.allowLocalFields = false;
169 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700170
171 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
172 lp::Packet lpPacket;
173 lpPacket.set<lp::FragmentField>(std::make_pair(
174 interest1->wireEncode().begin(), interest1->wireEncode().end()));
175 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
176
177 transport->receivePacket(lpPacket.wireEncode());
178
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700179 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700180 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
181 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
182}
183
184BOOST_AUTO_TEST_CASE(ReceiveBareData)
185{
Eric Newberry86d31872015-09-23 16:24:59 -0700186 // Initialize with Options that disables all services
187 GenericLinkService::Options options;
188 options.allowLocalFields = false;
189 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700190
191 shared_ptr<Data> data1 = makeData("/12345678");
192
193 transport->receivePacket(data1->wireEncode());
194
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700195 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700196 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
197 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
198}
199
200BOOST_AUTO_TEST_CASE(ReceiveData)
201{
Eric Newberry86d31872015-09-23 16:24:59 -0700202 // Initialize with Options that disables all services
203 GenericLinkService::Options options;
204 options.allowLocalFields = false;
205 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700206
207 shared_ptr<Data> data1 = makeData("/12345689");
208 lp::Packet lpPacket;
209 lpPacket.set<lp::FragmentField>(std::make_pair(
210 data1->wireEncode().begin(), data1->wireEncode().end()));
211 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
212
213 transport->receivePacket(lpPacket.wireEncode());
214
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700215 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700216 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
217 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
218}
219
220BOOST_AUTO_TEST_CASE(ReceiveNack)
221{
Eric Newberry86d31872015-09-23 16:24:59 -0700222 // Initialize with Options that disables all services
223 GenericLinkService::Options options;
224 options.allowLocalFields = false;
225 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700226
227 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
228 lp::Packet lpPacket;
229 lpPacket.set<lp::FragmentField>(std::make_pair(
230 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
231 lpPacket.set<lp::NackField>(nack1.getHeader());
232
233 transport->receivePacket(lpPacket.wireEncode());
234
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700235 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700236 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700237 BOOST_CHECK(receivedNacks.back().getReason() == nack1.getReason());
238 BOOST_CHECK(receivedNacks.back().getInterest() == nack1.getInterest());
Eric Newberrya98bf932015-09-21 00:58:47 -0700239}
240
Eric Newberrya1939ba2015-10-09 12:35:03 -0700241BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
242{
243 // Initialize with Options that disables all services
244 GenericLinkService::Options options;
245 options.allowLocalFields = false;
246 initialize(options);
247
248 lp::Packet lpPacket;
249 lpPacket.set<lp::SequenceField>(0);
250
251 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
252
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700253 // IDLE packet should be ignored, but is not an error
254 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700255 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
256 BOOST_CHECK_EQUAL(receivedData.size(), 0);
257 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
258}
259
Eric Newberrya98bf932015-09-21 00:58:47 -0700260BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
261
Eric Newberry86d31872015-09-23 16:24:59 -0700262BOOST_AUTO_TEST_SUITE(Fragmentation)
263
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700264BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
265{
266 // Initialize with Options that disable fragmentation
267 GenericLinkService::Options options;
268 options.allowFragmentation = false;
269 initialize(options);
270
271 transport->setMtu(55);
272
273 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
274 face->sendData(*data);
275
276 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
277 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
278}
279
280BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
281{
282 // Initialize with Options that enable fragmentation
283 GenericLinkService::Options options;
284 options.allowFragmentation = true;
285 initialize(options);
286
287 transport->setMtu(MTU_UNLIMITED);
288
289 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
290 face->sendData(*data);
291
292 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
293}
294
295BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
296{
297 // Initialize with Options that enable fragmentation
298 GenericLinkService::Options options;
299 options.allowFragmentation = true;
300 initialize(options);
301
302 transport->setMtu(105);
303
304 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
305 face->sendData(*data);
306
307 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
308}
309
310BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
311{
312 // Initialize with Options that enable fragmentation
313 GenericLinkService::Options options;
314 options.allowFragmentation = true;
315 initialize(options);
316
317 transport->setMtu(60);
318
319 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
320 face->sendData(*data);
321
322 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
323}
324
325BOOST_AUTO_TEST_CASE(ReassembleFragments)
326{
327 // Initialize with Options that enables reassembly
328 GenericLinkService::Options options;
329 options.allowReassembly = true;
330 initialize(options);
331
332 shared_ptr<Interest> interest = makeInterest(
333 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
334 lp::Packet packet(interest->wireEncode());
335
336 // fragment the packet
337 LpFragmenter fragmenter;
338 size_t mtu = 100;
339 bool isOk = false;
340 std::vector<lp::Packet> frags;
341 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
342 BOOST_REQUIRE(isOk);
343 BOOST_CHECK_GT(frags.size(), 1);
344
345 // receive the fragments
346 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
347 size_t sequence = 1000 + fragIndex;
348 frags[fragIndex].add<lp::SequenceField>(sequence);
349
350 transport->receivePacket(frags[fragIndex].wireEncode());
351
352 if (fragIndex > 0) {
353 BOOST_CHECK(receivedInterests.empty());
354 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
355 }
356 else {
357 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
358 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest);
359 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
360 }
361 }
362}
363
Eric Newberry86d31872015-09-23 16:24:59 -0700364BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
365{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700366 // Initialize with Options that disables reassembly
367 GenericLinkService::Options options;
368 options.allowReassembly = false;
369 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700370
371 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
372 lp::Packet packet(interest->wireEncode());
373 packet.set<lp::FragIndexField>(140);
374
375 transport->receivePacket(packet.wireEncode());
376
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700377 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700378 BOOST_CHECK(receivedInterests.empty());
379}
380
381BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
382{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700383 // Initialize with Options that disables reassembly
384 GenericLinkService::Options options;
385 options.allowReassembly = false;
386 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700387
388 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
389 lp::Packet packet(interest->wireEncode());
390 packet.set<lp::FragCountField>(276);
391
392 transport->receivePacket(packet.wireEncode());
393
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700394 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700395 BOOST_CHECK(receivedInterests.empty());
396}
397
398BOOST_AUTO_TEST_SUITE_END() // Fragmentation
399
Eric Newberry185ab292017-03-28 06:45:39 +0000400BOOST_AUTO_TEST_SUITE(Reliability)
401
402BOOST_AUTO_TEST_CASE(SendInterestWithSequence)
403{
404 // Initialize with Options that enables reliability
405 GenericLinkService::Options options;
406 options.allowLocalFields = false;
407 options.reliabilityOptions.isEnabled = true;
408 initialize(options);
409
410 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
411
412 face->sendInterest(*interest1);
413
414 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
415 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
416 lp::Packet interest1pkt;
417 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
418 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
419 BOOST_CHECK(interest1pkt.has<lp::SequenceField>());
420}
421
422BOOST_AUTO_TEST_CASE(SendDataWithSequence)
423{
424 // Initialize with Options that enables reliability
425 GenericLinkService::Options options;
426 options.allowLocalFields = false;
427 options.reliabilityOptions.isEnabled = true;
428 initialize(options);
429
430 shared_ptr<Data> data1 = makeData("/localhost/test");
431
432 face->sendData(*data1);
433
434 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
435 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
436 lp::Packet data1pkt;
437 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
438 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
439 BOOST_CHECK(data1pkt.has<lp::SequenceField>());
440}
441
442BOOST_AUTO_TEST_CASE(SendNackWithSequence)
443{
444 // Initialize with Options that enables reliability
445 GenericLinkService::Options options;
446 options.allowLocalFields = false;
447 options.reliabilityOptions.isEnabled = true;
448 initialize(options);
449
450 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
451
452 face->sendNack(nack1);
453
454 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
455 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
456 lp::Packet nack1pkt;
457 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
458 BOOST_CHECK(nack1pkt.has<lp::NackField>());
459 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
460 BOOST_CHECK(nack1pkt.has<lp::SequenceField>());
461}
462
463BOOST_AUTO_TEST_SUITE_END() // Reliability
Eric Newberry86d31872015-09-23 16:24:59 -0700464
Eric Newberryee400b52016-11-24 14:12:48 +0000465BOOST_AUTO_TEST_SUITE(LpFields)
Eric Newberrya98bf932015-09-21 00:58:47 -0700466
Eric Newberrya98bf932015-09-21 00:58:47 -0700467BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
468{
Eric Newberry86d31872015-09-23 16:24:59 -0700469 // Initialize with Options that enables local fields
470 GenericLinkService::Options options;
471 options.allowLocalFields = true;
472 initialize(options);
473
474 shared_ptr<Interest> interest = makeInterest("/12345678");
475 lp::Packet packet(interest->wireEncode());
476 packet.set<lp::NextHopFaceIdField>(1000);
477
478 transport->receivePacket(packet.wireEncode());
479
480 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000481 shared_ptr<lp::NextHopFaceIdTag> tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
482 BOOST_REQUIRE(tag != nullptr);
483 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700484}
485
486BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
487{
Eric Newberry86d31872015-09-23 16:24:59 -0700488 // Initialize with Options that disables local fields
489 GenericLinkService::Options options;
490 options.allowLocalFields = false;
491 initialize(options);
492
493 shared_ptr<Interest> interest = makeInterest("/12345678");
494 lp::Packet packet(interest->wireEncode());
495 packet.set<lp::NextHopFaceIdField>(1000);
496
497 transport->receivePacket(packet.wireEncode());
498
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700499 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700500 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700501}
502
503BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
504{
Eric Newberry86d31872015-09-23 16:24:59 -0700505 // Initialize with Options that enables local fields
506 GenericLinkService::Options options;
507 options.allowLocalFields = true;
508 initialize(options);
509
510 shared_ptr<Data> data = makeData("/12345678");
511 lp::Packet packet(data->wireEncode());
512 packet.set<lp::NextHopFaceIdField>(1000);
513
514 transport->receivePacket(packet.wireEncode());
515
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700516 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700517 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700518}
519
520BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
521{
Eric Newberry86d31872015-09-23 16:24:59 -0700522 // Initialize with Options that enables local fields
523 GenericLinkService::Options options;
524 options.allowLocalFields = true;
525 initialize(options);
526
527 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
528 lp::Packet packet;
529 packet.set<lp::FragmentField>(std::make_pair(
530 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
531 packet.set<lp::NackField>(nack.getHeader());
532 packet.set<lp::NextHopFaceIdField>(1000);
533
534 transport->receivePacket(packet.wireEncode());
535
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700536 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700537 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700538}
539
540BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
541{
Eric Newberry86d31872015-09-23 16:24:59 -0700542 // Initialize with Options that enables local fields
543 GenericLinkService::Options options;
544 options.allowLocalFields = true;
545 initialize(options);
546
547 shared_ptr<Data> data = makeData("/12345678");
548 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000549 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700550
551 transport->receivePacket(packet.wireEncode());
552
553 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000554 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
555 BOOST_REQUIRE(tag != nullptr);
556 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700557}
558
559BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
560{
Eric Newberry86d31872015-09-23 16:24:59 -0700561 // Initialize with Options that disables local fields
562 GenericLinkService::Options options;
563 options.allowLocalFields = false;
564 initialize(options);
565
566 shared_ptr<Data> data = makeData("/12345678");
567 lp::Packet packet(data->wireEncode());
568 lp::CachePolicy policy;
569 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
570 packet.set<lp::CachePolicyField>(policy);
571
572 transport->receivePacket(packet.wireEncode());
573
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700574 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700575 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000576 BOOST_CHECK(receivedData.back().getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700577}
578
579BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
580{
Eric Newberry86d31872015-09-23 16:24:59 -0700581 // Initialize with Options that enables local fields
582 GenericLinkService::Options options;
583 options.allowLocalFields = true;
584 initialize(options);
585
586 shared_ptr<Interest> interest = makeInterest("/12345678");
587 lp::Packet packet(interest->wireEncode());
588 lp::CachePolicy policy;
589 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
590 packet.set<lp::CachePolicyField>(policy);
591
592 transport->receivePacket(packet.wireEncode());
593
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700594 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700595 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700596}
597
598BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
599{
Eric Newberry86d31872015-09-23 16:24:59 -0700600 // Initialize with Options that enables local fields
601 GenericLinkService::Options options;
602 options.allowLocalFields = true;
603 initialize(options);
604
605 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
606 lp::Packet packet(nack.getInterest().wireEncode());
607 packet.set<lp::NackField>(nack.getHeader());
608 lp::CachePolicy policy;
609 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
610 packet.set<lp::CachePolicyField>(policy);
611
612 transport->receivePacket(packet.wireEncode());
613
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700614 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700615 BOOST_CHECK(receivedNacks.empty());
616}
617
618BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
619{
620 // Initialize with Options that enables local fields
621 GenericLinkService::Options options;
622 options.allowLocalFields = true;
623 initialize(options);
624
625 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000626 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700627
628 face->sendInterest(*interest);
629
630 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
631 lp::Packet sent(transport->sentPackets.back().packet);
632 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
633 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
634}
635
636BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
637{
638 // Initialize with Options that disables local fields
639 GenericLinkService::Options options;
640 options.allowLocalFields = false;
641 initialize(options);
642
643 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000644 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700645
646 face->sendInterest(*interest);
647
648 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
649 lp::Packet sent(transport->sentPackets.back().packet);
650 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
651}
652
653BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
654{
655 // Initialize with Options that enables local fields
656 GenericLinkService::Options options;
657 options.allowLocalFields = true;
658 initialize(options);
659
660 shared_ptr<Interest> interest = makeInterest("/12345678");
661 lp::Packet packet(interest->wireEncode());
662 packet.set<lp::IncomingFaceIdField>(1000);
663
664 transport->receivePacket(packet.wireEncode());
665
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700666 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700667 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000668 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700669}
670
671BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
672{
673 // Initialize with Options that enables local fields
674 GenericLinkService::Options options;
675 options.allowLocalFields = true;
676 initialize(options);
677
678 shared_ptr<Data> data = makeData("/z1megUh9Bj");
679 lp::Packet packet(data->wireEncode());
680 packet.set<lp::IncomingFaceIdField>(1000);
681
682 transport->receivePacket(packet.wireEncode());
683
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700684 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700685 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000686 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700687}
688
689BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
690{
691 // Initialize with Options that enables local fields
692 GenericLinkService::Options options;
693 options.allowLocalFields = true;
694 initialize(options);
695
696 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
697 lp::Packet packet(nack.getInterest().wireEncode());
698 packet.set<lp::NackField>(nack.getHeader());
699 packet.set<lp::IncomingFaceIdField>(1000);
700
701 transport->receivePacket(packet.wireEncode());
702
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700703 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700704 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000705 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700706}
707
Eric Newberryee400b52016-11-24 14:12:48 +0000708BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
709{
710 shared_ptr<Interest> interest = makeInterest("/12345678");
711 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
712
713 face->sendInterest(*interest);
714
715 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
716 lp::Packet sent(transport->sentPackets.back().packet);
717 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
718 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
719}
720
721BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
722{
723 shared_ptr<Data> data = makeData("/12345678");
724 data->setTag(make_shared<lp::CongestionMarkTag>(0));
725
726 face->sendData(*data);
727
728 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
729 lp::Packet sent(transport->sentPackets.back().packet);
730 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
731 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
732}
733
734BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
735{
736 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
737 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
738
739 face->sendNack(nack);
740
741 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
742 lp::Packet sent(transport->sentPackets.back().packet);
743 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
744 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
745}
746
747BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
748{
749 shared_ptr<Interest> interest = makeInterest("/12345678");
750 lp::Packet packet(interest->wireEncode());
751 packet.set<lp::CongestionMarkField>(1);
752
753 transport->receivePacket(packet.wireEncode());
754
755 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
756 shared_ptr<lp::CongestionMarkTag> tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
757 BOOST_REQUIRE(tag != nullptr);
758 BOOST_CHECK_EQUAL(*tag, 1);
759}
760
761BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
762{
763 shared_ptr<Data> data = makeData("/12345678");
764 lp::Packet packet(data->wireEncode());
765 packet.set<lp::CongestionMarkField>(1);
766
767 transport->receivePacket(packet.wireEncode());
768
769 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
770 shared_ptr<lp::CongestionMarkTag> tag = receivedData.back().getTag<lp::CongestionMarkTag>();
771 BOOST_REQUIRE(tag != nullptr);
772 BOOST_CHECK_EQUAL(*tag, 1);
773}
774
775BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
776{
777 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
778 lp::Packet packet;
779 packet.set<lp::FragmentField>(std::make_pair(
780 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
781 packet.set<lp::NackField>(nack.getHeader());
782 packet.set<lp::CongestionMarkField>(1);
783
784 transport->receivePacket(packet.wireEncode());
785
786 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
787 shared_ptr<lp::CongestionMarkTag> tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
788 BOOST_REQUIRE(tag != nullptr);
789 BOOST_CHECK_EQUAL(*tag, 1);
790}
791
792BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -0700793
794
Eric Newberrya1939ba2015-10-09 12:35:03 -0700795BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
796
797BOOST_AUTO_TEST_CASE(WrongTlvType)
798{
799 // Initialize with Options that disables all services
800 GenericLinkService::Options options;
801 options.allowLocalFields = false;
802 initialize(options);
803
804 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
805
806 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
807
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700808 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700809 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
810 BOOST_CHECK_EQUAL(receivedData.size(), 0);
811 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
812}
813
814BOOST_AUTO_TEST_CASE(Unparsable)
815{
816 // Initialize with Options that disables all services
817 GenericLinkService::Options options;
818 options.allowLocalFields = false;
819 initialize(options);
820
821 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
822 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
823
824 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
825
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700826 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700827 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
828 BOOST_CHECK_EQUAL(receivedData.size(), 0);
829 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
830}
831
832BOOST_AUTO_TEST_SUITE_END() // Malformed
833
834
Eric Newberry86d31872015-09-23 16:24:59 -0700835BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
836BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700837
838} // namespace tests
839} // namespace face
840} // namespace nfd