blob: e4e82f7f6f1f2f52abb567da67677853dd53a6b6 [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 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
Eric Newberry7b0071e2017-07-03 17:33:31 +0000402BOOST_AUTO_TEST_CASE(SendInterest)
Eric Newberry185ab292017-03-28 06:45:39 +0000403{
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>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000419 BOOST_CHECK(interest1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000420}
421
Eric Newberry7b0071e2017-07-03 17:33:31 +0000422BOOST_AUTO_TEST_CASE(SendData)
Eric Newberry185ab292017-03-28 06:45:39 +0000423{
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>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000439 BOOST_CHECK(data1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000440}
441
Eric Newberry7b0071e2017-07-03 17:33:31 +0000442BOOST_AUTO_TEST_CASE(SendNack)
Eric Newberry185ab292017-03-28 06:45:39 +0000443{
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>());
Eric Newberry7b0071e2017-07-03 17:33:31 +0000460 BOOST_CHECK(nack1pkt.has<lp::TxSequenceField>());
Eric Newberry185ab292017-03-28 06:45:39 +0000461}
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
Junxiao Shi6eb02712017-05-27 22:48:02 +0000540BOOST_AUTO_TEST_CASE(ReceiveCachePolicy)
Eric Newberrya98bf932015-09-21 00:58:47 -0700541{
Junxiao Shi6eb02712017-05-27 22:48:02 +0000542 // Initialize with Options that disables local fields
Eric Newberry86d31872015-09-23 16:24:59 -0700543 GenericLinkService::Options options;
Junxiao Shi6eb02712017-05-27 22:48:02 +0000544 options.allowLocalFields = false;
Eric Newberry86d31872015-09-23 16:24:59 -0700545 initialize(options);
Junxiao Shi6eb02712017-05-27 22:48:02 +0000546 // CachePolicy is unprivileged and does not require allowLocalFields option.
Eric Newberry86d31872015-09-23 16:24:59 -0700547
548 shared_ptr<Data> data = makeData("/12345678");
549 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000550 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700551
552 transport->receivePacket(packet.wireEncode());
553
554 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000555 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
556 BOOST_REQUIRE(tag != nullptr);
557 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700558}
559
Junxiao Shi6eb02712017-05-27 22:48:02 +0000560BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropInterest)
Eric Newberrya98bf932015-09-21 00:58:47 -0700561{
Eric Newberry86d31872015-09-23 16:24:59 -0700562 // Initialize with Options that enables local fields
563 GenericLinkService::Options options;
564 options.allowLocalFields = true;
565 initialize(options);
566
567 shared_ptr<Interest> interest = makeInterest("/12345678");
568 lp::Packet packet(interest->wireEncode());
569 lp::CachePolicy policy;
570 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
571 packet.set<lp::CachePolicyField>(policy);
572
573 transport->receivePacket(packet.wireEncode());
574
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700575 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700576 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700577}
578
Junxiao Shi6eb02712017-05-27 22:48:02 +0000579BOOST_AUTO_TEST_CASE(ReceiveCachePolicyDropNack)
Eric Newberrya98bf932015-09-21 00:58:47 -0700580{
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 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
587 lp::Packet packet(nack.getInterest().wireEncode());
588 packet.set<lp::NackField>(nack.getHeader());
589 lp::CachePolicy policy;
590 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
591 packet.set<lp::CachePolicyField>(policy);
592
593 transport->receivePacket(packet.wireEncode());
594
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700595 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700596 BOOST_CHECK(receivedNacks.empty());
597}
598
599BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
600{
601 // Initialize with Options that enables local fields
602 GenericLinkService::Options options;
603 options.allowLocalFields = true;
604 initialize(options);
605
606 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000607 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700608
609 face->sendInterest(*interest);
610
611 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
612 lp::Packet sent(transport->sentPackets.back().packet);
613 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
614 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
615}
616
617BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
618{
619 // Initialize with Options that disables local fields
620 GenericLinkService::Options options;
621 options.allowLocalFields = false;
622 initialize(options);
623
624 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000625 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700626
627 face->sendInterest(*interest);
628
629 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
630 lp::Packet sent(transport->sentPackets.back().packet);
631 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
632}
633
634BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
635{
636 // Initialize with Options that enables local fields
637 GenericLinkService::Options options;
638 options.allowLocalFields = true;
639 initialize(options);
640
641 shared_ptr<Interest> interest = makeInterest("/12345678");
642 lp::Packet packet(interest->wireEncode());
643 packet.set<lp::IncomingFaceIdField>(1000);
644
645 transport->receivePacket(packet.wireEncode());
646
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700647 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700648 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000649 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700650}
651
652BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
653{
654 // Initialize with Options that enables local fields
655 GenericLinkService::Options options;
656 options.allowLocalFields = true;
657 initialize(options);
658
659 shared_ptr<Data> data = makeData("/z1megUh9Bj");
660 lp::Packet packet(data->wireEncode());
661 packet.set<lp::IncomingFaceIdField>(1000);
662
663 transport->receivePacket(packet.wireEncode());
664
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700665 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700666 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000667 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700668}
669
670BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
671{
672 // Initialize with Options that enables local fields
673 GenericLinkService::Options options;
674 options.allowLocalFields = true;
675 initialize(options);
676
677 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
678 lp::Packet packet(nack.getInterest().wireEncode());
679 packet.set<lp::NackField>(nack.getHeader());
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(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000686 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700687}
688
Eric Newberryee400b52016-11-24 14:12:48 +0000689BOOST_AUTO_TEST_CASE(SendCongestionMarkInterest)
690{
691 shared_ptr<Interest> interest = makeInterest("/12345678");
692 interest->setTag(make_shared<lp::CongestionMarkTag>(1));
693
694 face->sendInterest(*interest);
695
696 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
697 lp::Packet sent(transport->sentPackets.back().packet);
698 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
699 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 1);
700}
701
702BOOST_AUTO_TEST_CASE(SendCongestionMarkData)
703{
704 shared_ptr<Data> data = makeData("/12345678");
705 data->setTag(make_shared<lp::CongestionMarkTag>(0));
706
707 face->sendData(*data);
708
709 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
710 lp::Packet sent(transport->sentPackets.back().packet);
711 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
712 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), 0);
713}
714
715BOOST_AUTO_TEST_CASE(SendCongestionMarkNack)
716{
717 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
718 nack.setTag(make_shared<lp::CongestionMarkTag>(std::numeric_limits<uint64_t>::max()));
719
720 face->sendNack(nack);
721
722 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
723 lp::Packet sent(transport->sentPackets.back().packet);
724 BOOST_REQUIRE(sent.has<lp::CongestionMarkField>());
725 BOOST_CHECK_EQUAL(sent.get<lp::CongestionMarkField>(), std::numeric_limits<uint64_t>::max());
726}
727
728BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkInterest)
729{
730 shared_ptr<Interest> interest = makeInterest("/12345678");
731 lp::Packet packet(interest->wireEncode());
732 packet.set<lp::CongestionMarkField>(1);
733
734 transport->receivePacket(packet.wireEncode());
735
736 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
737 shared_ptr<lp::CongestionMarkTag> tag = receivedInterests.back().getTag<lp::CongestionMarkTag>();
738 BOOST_REQUIRE(tag != nullptr);
739 BOOST_CHECK_EQUAL(*tag, 1);
740}
741
742BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkData)
743{
744 shared_ptr<Data> data = makeData("/12345678");
745 lp::Packet packet(data->wireEncode());
746 packet.set<lp::CongestionMarkField>(1);
747
748 transport->receivePacket(packet.wireEncode());
749
750 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
751 shared_ptr<lp::CongestionMarkTag> tag = receivedData.back().getTag<lp::CongestionMarkTag>();
752 BOOST_REQUIRE(tag != nullptr);
753 BOOST_CHECK_EQUAL(*tag, 1);
754}
755
756BOOST_AUTO_TEST_CASE(ReceiveCongestionMarkNack)
757{
758 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
759 lp::Packet packet;
760 packet.set<lp::FragmentField>(std::make_pair(
761 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
762 packet.set<lp::NackField>(nack.getHeader());
763 packet.set<lp::CongestionMarkField>(1);
764
765 transport->receivePacket(packet.wireEncode());
766
767 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
768 shared_ptr<lp::CongestionMarkTag> tag = receivedNacks.back().getTag<lp::CongestionMarkTag>();
769 BOOST_REQUIRE(tag != nullptr);
770 BOOST_CHECK_EQUAL(*tag, 1);
771}
772
773BOOST_AUTO_TEST_SUITE_END() // LpFields
Eric Newberrya98bf932015-09-21 00:58:47 -0700774
775
Eric Newberrya1939ba2015-10-09 12:35:03 -0700776BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
777
778BOOST_AUTO_TEST_CASE(WrongTlvType)
779{
780 // Initialize with Options that disables all services
781 GenericLinkService::Options options;
782 options.allowLocalFields = false;
783 initialize(options);
784
785 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
786
787 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
788
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700789 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700790 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
791 BOOST_CHECK_EQUAL(receivedData.size(), 0);
792 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
793}
794
795BOOST_AUTO_TEST_CASE(Unparsable)
796{
797 // Initialize with Options that disables all services
798 GenericLinkService::Options options;
799 options.allowLocalFields = false;
800 initialize(options);
801
802 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
803 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
804
805 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
806
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700807 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700808 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
809 BOOST_CHECK_EQUAL(receivedData.size(), 0);
810 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
811}
812
813BOOST_AUTO_TEST_SUITE_END() // Malformed
814
815
Eric Newberry86d31872015-09-23 16:24:59 -0700816BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
817BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700818
819} // namespace tests
820} // namespace face
821} // namespace nfd