blob: dc5a8cf3c062bca7538c2823f29c63d90c2a2c2c [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shicbc8e942016-09-06 03:17:45 +00003 * Copyright (c) 2014-2016, 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 Newberry86d31872015-09-23 16:24:59 -070056 initialize(const GenericLinkService::Options& options)
Eric Newberrya98bf932015-09-21 00:58:47 -070057 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070058 face.reset(new Face(make_unique<GenericLinkService>(options),
59 make_unique<DummyTransport>()));
Eric Newberrya98bf932015-09-21 00:58:47 -070060 service = static_cast<GenericLinkService*>(face->getLinkService());
61 transport = static_cast<DummyTransport*>(face->getTransport());
62
63 face->afterReceiveInterest.connect(
64 [this] (const Interest& interest) { receivedInterests.push_back(interest); });
65 face->afterReceiveData.connect(
66 [this] (const Data& data) { receivedData.push_back(data); });
67 face->afterReceiveNack.connect(
68 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
69 }
70
71protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070072 unique_ptr<Face> face;
Eric Newberrya98bf932015-09-21 00:58:47 -070073 GenericLinkService* service;
74 DummyTransport* transport;
75 std::vector<Interest> receivedInterests;
76 std::vector<Data> receivedData;
77 std::vector<lp::Nack> receivedNacks;
78};
79
80BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
81
82
83BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
84
85BOOST_AUTO_TEST_CASE(SendInterest)
86{
Eric Newberry86d31872015-09-23 16:24:59 -070087 // Initialize with Options that disables all services
88 GenericLinkService::Options options;
89 options.allowLocalFields = false;
90 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -070091
92 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
93
94 face->sendInterest(*interest1);
95
Eric Newberry4c3e6b82015-11-10 16:48:42 -070096 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -070097 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070098 lp::Packet interest1pkt;
99 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
100 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
101 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700102}
103
104BOOST_AUTO_TEST_CASE(SendData)
105{
Eric Newberry86d31872015-09-23 16:24:59 -0700106 // Initialize with Options that disables all services
107 GenericLinkService::Options options;
108 options.allowLocalFields = false;
109 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700110
111 shared_ptr<Data> data1 = makeData("/localhost/test");
112
113 face->sendData(*data1);
114
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700115 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700116 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700117 lp::Packet data1pkt;
118 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
119 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
120 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700121}
122
123BOOST_AUTO_TEST_CASE(SendNack)
124{
Eric Newberry86d31872015-09-23 16:24:59 -0700125 // Initialize with Options that disables all services
126 GenericLinkService::Options options;
127 options.allowLocalFields = false;
128 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700129
130 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
131
132 face->sendNack(nack1);
133
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700134 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700135 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
136 lp::Packet nack1pkt;
137 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700138 BOOST_CHECK(nack1pkt.has<lp::NackField>());
139 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
140 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700141}
142
143BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
144{
Eric Newberry86d31872015-09-23 16:24:59 -0700145 // Initialize with Options that disables all services
146 GenericLinkService::Options options;
147 options.allowLocalFields = false;
148 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700149
150 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
151
152 transport->receivePacket(interest1->wireEncode());
153
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700154 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700155 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
156 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
157}
158
159BOOST_AUTO_TEST_CASE(ReceiveInterest)
160{
Eric Newberry86d31872015-09-23 16:24:59 -0700161 // Initialize with Options that disables all services
162 GenericLinkService::Options options;
163 options.allowLocalFields = false;
164 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700165
166 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
167 lp::Packet lpPacket;
168 lpPacket.set<lp::FragmentField>(std::make_pair(
169 interest1->wireEncode().begin(), interest1->wireEncode().end()));
170 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
171
172 transport->receivePacket(lpPacket.wireEncode());
173
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700174 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700175 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
176 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
177}
178
179BOOST_AUTO_TEST_CASE(ReceiveBareData)
180{
Eric Newberry86d31872015-09-23 16:24:59 -0700181 // Initialize with Options that disables all services
182 GenericLinkService::Options options;
183 options.allowLocalFields = false;
184 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700185
186 shared_ptr<Data> data1 = makeData("/12345678");
187
188 transport->receivePacket(data1->wireEncode());
189
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700190 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700191 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
192 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
193}
194
195BOOST_AUTO_TEST_CASE(ReceiveData)
196{
Eric Newberry86d31872015-09-23 16:24:59 -0700197 // Initialize with Options that disables all services
198 GenericLinkService::Options options;
199 options.allowLocalFields = false;
200 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700201
202 shared_ptr<Data> data1 = makeData("/12345689");
203 lp::Packet lpPacket;
204 lpPacket.set<lp::FragmentField>(std::make_pair(
205 data1->wireEncode().begin(), data1->wireEncode().end()));
206 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
207
208 transport->receivePacket(lpPacket.wireEncode());
209
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700210 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700211 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
212 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
213}
214
215BOOST_AUTO_TEST_CASE(ReceiveNack)
216{
Eric Newberry86d31872015-09-23 16:24:59 -0700217 // Initialize with Options that disables all services
218 GenericLinkService::Options options;
219 options.allowLocalFields = false;
220 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700221
222 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
223 lp::Packet lpPacket;
224 lpPacket.set<lp::FragmentField>(std::make_pair(
225 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
226 lpPacket.set<lp::NackField>(nack1.getHeader());
227
228 transport->receivePacket(lpPacket.wireEncode());
229
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700230 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700231 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700232 BOOST_CHECK(receivedNacks.back().getReason() == nack1.getReason());
233 BOOST_CHECK(receivedNacks.back().getInterest() == nack1.getInterest());
Eric Newberrya98bf932015-09-21 00:58:47 -0700234}
235
Eric Newberrya1939ba2015-10-09 12:35:03 -0700236BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
237{
238 // Initialize with Options that disables all services
239 GenericLinkService::Options options;
240 options.allowLocalFields = false;
241 initialize(options);
242
243 lp::Packet lpPacket;
244 lpPacket.set<lp::SequenceField>(0);
245
246 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
247
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700248 // IDLE packet should be ignored, but is not an error
249 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700250 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
251 BOOST_CHECK_EQUAL(receivedData.size(), 0);
252 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
253}
254
Eric Newberrya98bf932015-09-21 00:58:47 -0700255BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
256
257
Eric Newberry86d31872015-09-23 16:24:59 -0700258BOOST_AUTO_TEST_SUITE(Fragmentation)
259
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700260BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
261{
262 // Initialize with Options that disable fragmentation
263 GenericLinkService::Options options;
264 options.allowFragmentation = false;
265 initialize(options);
266
267 transport->setMtu(55);
268
269 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
270 face->sendData(*data);
271
272 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
273 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
274}
275
276BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
277{
278 // Initialize with Options that enable fragmentation
279 GenericLinkService::Options options;
280 options.allowFragmentation = true;
281 initialize(options);
282
283 transport->setMtu(MTU_UNLIMITED);
284
285 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
286 face->sendData(*data);
287
288 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
289}
290
291BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
292{
293 // Initialize with Options that enable fragmentation
294 GenericLinkService::Options options;
295 options.allowFragmentation = true;
296 initialize(options);
297
298 transport->setMtu(105);
299
300 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
301 face->sendData(*data);
302
303 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
304}
305
306BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
307{
308 // Initialize with Options that enable fragmentation
309 GenericLinkService::Options options;
310 options.allowFragmentation = true;
311 initialize(options);
312
313 transport->setMtu(60);
314
315 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
316 face->sendData(*data);
317
318 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
319}
320
321BOOST_AUTO_TEST_CASE(ReassembleFragments)
322{
323 // Initialize with Options that enables reassembly
324 GenericLinkService::Options options;
325 options.allowReassembly = true;
326 initialize(options);
327
328 shared_ptr<Interest> interest = makeInterest(
329 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
330 lp::Packet packet(interest->wireEncode());
331
332 // fragment the packet
333 LpFragmenter fragmenter;
334 size_t mtu = 100;
335 bool isOk = false;
336 std::vector<lp::Packet> frags;
337 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
338 BOOST_REQUIRE(isOk);
339 BOOST_CHECK_GT(frags.size(), 1);
340
341 // receive the fragments
342 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
343 size_t sequence = 1000 + fragIndex;
344 frags[fragIndex].add<lp::SequenceField>(sequence);
345
346 transport->receivePacket(frags[fragIndex].wireEncode());
347
348 if (fragIndex > 0) {
349 BOOST_CHECK(receivedInterests.empty());
350 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
351 }
352 else {
353 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
354 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest);
355 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
356 }
357 }
358}
359
Eric Newberry86d31872015-09-23 16:24:59 -0700360BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
361{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700362 // Initialize with Options that disables reassembly
363 GenericLinkService::Options options;
364 options.allowReassembly = false;
365 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700366
367 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
368 lp::Packet packet(interest->wireEncode());
369 packet.set<lp::FragIndexField>(140);
370
371 transport->receivePacket(packet.wireEncode());
372
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700373 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700374 BOOST_CHECK(receivedInterests.empty());
375}
376
377BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
378{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700379 // Initialize with Options that disables reassembly
380 GenericLinkService::Options options;
381 options.allowReassembly = false;
382 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700383
384 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
385 lp::Packet packet(interest->wireEncode());
386 packet.set<lp::FragCountField>(276);
387
388 transport->receivePacket(packet.wireEncode());
389
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700390 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700391 BOOST_CHECK(receivedInterests.empty());
392}
393
394BOOST_AUTO_TEST_SUITE_END() // Fragmentation
395
396
Eric Newberrya98bf932015-09-21 00:58:47 -0700397BOOST_AUTO_TEST_SUITE(LocalFields)
398
Eric Newberrya98bf932015-09-21 00:58:47 -0700399BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
400{
Eric Newberry86d31872015-09-23 16:24:59 -0700401 // Initialize with Options that enables local fields
402 GenericLinkService::Options options;
403 options.allowLocalFields = true;
404 initialize(options);
405
406 shared_ptr<Interest> interest = makeInterest("/12345678");
407 lp::Packet packet(interest->wireEncode());
408 packet.set<lp::NextHopFaceIdField>(1000);
409
410 transport->receivePacket(packet.wireEncode());
411
412 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000413 shared_ptr<lp::NextHopFaceIdTag> tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
414 BOOST_REQUIRE(tag != nullptr);
415 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700416}
417
418BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
419{
Eric Newberry86d31872015-09-23 16:24:59 -0700420 // Initialize with Options that disables local fields
421 GenericLinkService::Options options;
422 options.allowLocalFields = false;
423 initialize(options);
424
425 shared_ptr<Interest> interest = makeInterest("/12345678");
426 lp::Packet packet(interest->wireEncode());
427 packet.set<lp::NextHopFaceIdField>(1000);
428
429 transport->receivePacket(packet.wireEncode());
430
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700431 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700432 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700433}
434
435BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
436{
Eric Newberry86d31872015-09-23 16:24:59 -0700437 // Initialize with Options that enables local fields
438 GenericLinkService::Options options;
439 options.allowLocalFields = true;
440 initialize(options);
441
442 shared_ptr<Data> data = makeData("/12345678");
443 lp::Packet packet(data->wireEncode());
444 packet.set<lp::NextHopFaceIdField>(1000);
445
446 transport->receivePacket(packet.wireEncode());
447
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700448 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700449 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700450}
451
452BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
453{
Eric Newberry86d31872015-09-23 16:24:59 -0700454 // Initialize with Options that enables local fields
455 GenericLinkService::Options options;
456 options.allowLocalFields = true;
457 initialize(options);
458
459 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
460 lp::Packet packet;
461 packet.set<lp::FragmentField>(std::make_pair(
462 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
463 packet.set<lp::NackField>(nack.getHeader());
464 packet.set<lp::NextHopFaceIdField>(1000);
465
466 transport->receivePacket(packet.wireEncode());
467
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700468 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700469 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700470}
471
472BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
473{
Eric Newberry86d31872015-09-23 16:24:59 -0700474 // Initialize with Options that enables local fields
475 GenericLinkService::Options options;
476 options.allowLocalFields = true;
477 initialize(options);
478
479 shared_ptr<Data> data = makeData("/12345678");
480 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000481 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700482
483 transport->receivePacket(packet.wireEncode());
484
485 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000486 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
487 BOOST_REQUIRE(tag != nullptr);
488 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700489}
490
491BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
492{
Eric Newberry86d31872015-09-23 16:24:59 -0700493 // Initialize with Options that disables local fields
494 GenericLinkService::Options options;
495 options.allowLocalFields = false;
496 initialize(options);
497
498 shared_ptr<Data> data = makeData("/12345678");
499 lp::Packet packet(data->wireEncode());
500 lp::CachePolicy policy;
501 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
502 packet.set<lp::CachePolicyField>(policy);
503
504 transport->receivePacket(packet.wireEncode());
505
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700506 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700507 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000508 BOOST_CHECK(receivedData.back().getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700509}
510
511BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
512{
Eric Newberry86d31872015-09-23 16:24:59 -0700513 // Initialize with Options that enables local fields
514 GenericLinkService::Options options;
515 options.allowLocalFields = true;
516 initialize(options);
517
518 shared_ptr<Interest> interest = makeInterest("/12345678");
519 lp::Packet packet(interest->wireEncode());
520 lp::CachePolicy policy;
521 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
522 packet.set<lp::CachePolicyField>(policy);
523
524 transport->receivePacket(packet.wireEncode());
525
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700526 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700527 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700528}
529
530BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
531{
Eric Newberry86d31872015-09-23 16:24:59 -0700532 // Initialize with Options that enables local fields
533 GenericLinkService::Options options;
534 options.allowLocalFields = true;
535 initialize(options);
536
537 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
538 lp::Packet packet(nack.getInterest().wireEncode());
539 packet.set<lp::NackField>(nack.getHeader());
540 lp::CachePolicy policy;
541 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
542 packet.set<lp::CachePolicyField>(policy);
543
544 transport->receivePacket(packet.wireEncode());
545
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700546 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700547 BOOST_CHECK(receivedNacks.empty());
548}
549
550BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
551{
552 // Initialize with Options that enables local fields
553 GenericLinkService::Options options;
554 options.allowLocalFields = true;
555 initialize(options);
556
557 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000558 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700559
560 face->sendInterest(*interest);
561
562 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
563 lp::Packet sent(transport->sentPackets.back().packet);
564 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
565 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
566}
567
568BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
569{
570 // Initialize with Options that disables local fields
571 GenericLinkService::Options options;
572 options.allowLocalFields = false;
573 initialize(options);
574
575 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000576 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700577
578 face->sendInterest(*interest);
579
580 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
581 lp::Packet sent(transport->sentPackets.back().packet);
582 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
583}
584
585BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
586{
587 // Initialize with Options that enables local fields
588 GenericLinkService::Options options;
589 options.allowLocalFields = true;
590 initialize(options);
591
592 shared_ptr<Interest> interest = makeInterest("/12345678");
593 lp::Packet packet(interest->wireEncode());
594 packet.set<lp::IncomingFaceIdField>(1000);
595
596 transport->receivePacket(packet.wireEncode());
597
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700598 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700599 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000600 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700601}
602
603BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
604{
605 // Initialize with Options that enables local fields
606 GenericLinkService::Options options;
607 options.allowLocalFields = true;
608 initialize(options);
609
610 shared_ptr<Data> data = makeData("/z1megUh9Bj");
611 lp::Packet packet(data->wireEncode());
612 packet.set<lp::IncomingFaceIdField>(1000);
613
614 transport->receivePacket(packet.wireEncode());
615
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700616 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700617 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000618 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700619}
620
621BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
622{
623 // Initialize with Options that enables local fields
624 GenericLinkService::Options options;
625 options.allowLocalFields = true;
626 initialize(options);
627
628 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
629 lp::Packet packet(nack.getInterest().wireEncode());
630 packet.set<lp::NackField>(nack.getHeader());
631 packet.set<lp::IncomingFaceIdField>(1000);
632
633 transport->receivePacket(packet.wireEncode());
634
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700635 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700636 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000637 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700638}
639
640BOOST_AUTO_TEST_SUITE_END() // LocalFields
641
642
Eric Newberrya1939ba2015-10-09 12:35:03 -0700643BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
644
645BOOST_AUTO_TEST_CASE(WrongTlvType)
646{
647 // Initialize with Options that disables all services
648 GenericLinkService::Options options;
649 options.allowLocalFields = false;
650 initialize(options);
651
652 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
653
654 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
655
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700656 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700657 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
658 BOOST_CHECK_EQUAL(receivedData.size(), 0);
659 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
660}
661
662BOOST_AUTO_TEST_CASE(Unparsable)
663{
664 // Initialize with Options that disables all services
665 GenericLinkService::Options options;
666 options.allowLocalFields = false;
667 initialize(options);
668
669 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
670 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
671
672 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
673
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700674 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700675 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
676 BOOST_CHECK_EQUAL(receivedData.size(), 0);
677 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
678}
679
680BOOST_AUTO_TEST_SUITE_END() // Malformed
681
682
Eric Newberry86d31872015-09-23 16:24:59 -0700683BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
684BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700685
686} // namespace tests
687} // namespace face
688} // namespace nfd