blob: f3c3c7b1cc2fb2fc915b3011821d27165a29be6e [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California,
4 * 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"
27#include "face/lp-face.hpp"
28#include "dummy-transport.hpp"
29
30#include "tests/test-common.hpp"
31
32namespace nfd {
33namespace face {
34namespace tests {
35
36using namespace nfd::tests;
37
38BOOST_AUTO_TEST_SUITE(Face)
39
40class GenericLinkServiceFixture : public BaseFixture
41{
42protected:
43 GenericLinkServiceFixture()
44 : service(nullptr)
45 , transport(nullptr)
46 {
Eric Newberry86d31872015-09-23 16:24:59 -070047 this->initialize(GenericLinkService::Options());
Eric Newberrya98bf932015-09-21 00:58:47 -070048 // By default, GenericLinkService is created with default options.
49 // Test cases may invoke .initialize with alternate options.
50 }
51
52 void
Eric Newberry86d31872015-09-23 16:24:59 -070053 initialize(const GenericLinkService::Options& options)
Eric Newberrya98bf932015-09-21 00:58:47 -070054 {
Eric Newberry86d31872015-09-23 16:24:59 -070055 face.reset(new LpFace(make_unique<GenericLinkService>(options),
Eric Newberrya98bf932015-09-21 00:58:47 -070056 make_unique<DummyTransport>()));
57 service = static_cast<GenericLinkService*>(face->getLinkService());
58 transport = static_cast<DummyTransport*>(face->getTransport());
59
60 face->afterReceiveInterest.connect(
61 [this] (const Interest& interest) { receivedInterests.push_back(interest); });
62 face->afterReceiveData.connect(
63 [this] (const Data& data) { receivedData.push_back(data); });
64 face->afterReceiveNack.connect(
65 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
66 }
67
68protected:
69 unique_ptr<LpFace> face;
70 GenericLinkService* service;
71 DummyTransport* transport;
72 std::vector<Interest> receivedInterests;
73 std::vector<Data> receivedData;
74 std::vector<lp::Nack> receivedNacks;
75};
76
77BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
78
79
80BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
81
82BOOST_AUTO_TEST_CASE(SendInterest)
83{
Eric Newberry86d31872015-09-23 16:24:59 -070084 // Initialize with Options that disables all services
85 GenericLinkService::Options options;
86 options.allowLocalFields = false;
87 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -070088
89 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
90
91 face->sendInterest(*interest1);
92
Eric Newberry4c3e6b82015-11-10 16:48:42 -070093 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -070094 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095 lp::Packet interest1pkt;
96 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
97 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
98 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -070099}
100
101BOOST_AUTO_TEST_CASE(SendData)
102{
Eric Newberry86d31872015-09-23 16:24:59 -0700103 // Initialize with Options that disables all services
104 GenericLinkService::Options options;
105 options.allowLocalFields = false;
106 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700107
108 shared_ptr<Data> data1 = makeData("/localhost/test");
109
110 face->sendData(*data1);
111
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700112 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700113 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700114 lp::Packet data1pkt;
115 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
116 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
117 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700118}
119
120BOOST_AUTO_TEST_CASE(SendNack)
121{
Eric Newberry86d31872015-09-23 16:24:59 -0700122 // Initialize with Options that disables all services
123 GenericLinkService::Options options;
124 options.allowLocalFields = false;
125 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700126
127 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
128
129 face->sendNack(nack1);
130
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700131 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700132 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
133 lp::Packet nack1pkt;
134 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700135 BOOST_CHECK(nack1pkt.has<lp::NackField>());
136 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
137 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700138}
139
140BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
141{
Eric Newberry86d31872015-09-23 16:24:59 -0700142 // Initialize with Options that disables all services
143 GenericLinkService::Options options;
144 options.allowLocalFields = false;
145 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700146
147 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
148
149 transport->receivePacket(interest1->wireEncode());
150
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700151 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700152 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
153 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
154}
155
156BOOST_AUTO_TEST_CASE(ReceiveInterest)
157{
Eric Newberry86d31872015-09-23 16:24:59 -0700158 // Initialize with Options that disables all services
159 GenericLinkService::Options options;
160 options.allowLocalFields = false;
161 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700162
163 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
164 lp::Packet lpPacket;
165 lpPacket.set<lp::FragmentField>(std::make_pair(
166 interest1->wireEncode().begin(), interest1->wireEncode().end()));
167 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
168
169 transport->receivePacket(lpPacket.wireEncode());
170
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700171 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700172 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
173 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
174}
175
176BOOST_AUTO_TEST_CASE(ReceiveBareData)
177{
Eric Newberry86d31872015-09-23 16:24:59 -0700178 // Initialize with Options that disables all services
179 GenericLinkService::Options options;
180 options.allowLocalFields = false;
181 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700182
183 shared_ptr<Data> data1 = makeData("/12345678");
184
185 transport->receivePacket(data1->wireEncode());
186
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700187 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700188 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
189 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
190}
191
192BOOST_AUTO_TEST_CASE(ReceiveData)
193{
Eric Newberry86d31872015-09-23 16:24:59 -0700194 // Initialize with Options that disables all services
195 GenericLinkService::Options options;
196 options.allowLocalFields = false;
197 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700198
199 shared_ptr<Data> data1 = makeData("/12345689");
200 lp::Packet lpPacket;
201 lpPacket.set<lp::FragmentField>(std::make_pair(
202 data1->wireEncode().begin(), data1->wireEncode().end()));
203 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
204
205 transport->receivePacket(lpPacket.wireEncode());
206
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700207 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700208 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
209 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
210}
211
212BOOST_AUTO_TEST_CASE(ReceiveNack)
213{
Eric Newberry86d31872015-09-23 16:24:59 -0700214 // Initialize with Options that disables all services
215 GenericLinkService::Options options;
216 options.allowLocalFields = false;
217 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700218
219 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
220 lp::Packet lpPacket;
221 lpPacket.set<lp::FragmentField>(std::make_pair(
222 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
223 lpPacket.set<lp::NackField>(nack1.getHeader());
224
225 transport->receivePacket(lpPacket.wireEncode());
226
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700227 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700228 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700229 BOOST_CHECK(receivedNacks.back().getReason() == nack1.getReason());
230 BOOST_CHECK(receivedNacks.back().getInterest() == nack1.getInterest());
Eric Newberrya98bf932015-09-21 00:58:47 -0700231}
232
Eric Newberrya1939ba2015-10-09 12:35:03 -0700233BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
234{
235 // Initialize with Options that disables all services
236 GenericLinkService::Options options;
237 options.allowLocalFields = false;
238 initialize(options);
239
240 lp::Packet lpPacket;
241 lpPacket.set<lp::SequenceField>(0);
242
243 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
244
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700245 // IDLE packet should be ignored, but is not an error
246 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700247 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
248 BOOST_CHECK_EQUAL(receivedData.size(), 0);
249 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
250}
251
Eric Newberrya98bf932015-09-21 00:58:47 -0700252BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
253
254
Eric Newberry86d31872015-09-23 16:24:59 -0700255BOOST_AUTO_TEST_SUITE(Fragmentation)
256
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700257BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
258{
259 // Initialize with Options that disable fragmentation
260 GenericLinkService::Options options;
261 options.allowFragmentation = false;
262 initialize(options);
263
264 transport->setMtu(55);
265
266 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
267 face->sendData(*data);
268
269 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
270 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
271}
272
273BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
274{
275 // Initialize with Options that enable fragmentation
276 GenericLinkService::Options options;
277 options.allowFragmentation = true;
278 initialize(options);
279
280 transport->setMtu(MTU_UNLIMITED);
281
282 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
283 face->sendData(*data);
284
285 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
286}
287
288BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
289{
290 // Initialize with Options that enable fragmentation
291 GenericLinkService::Options options;
292 options.allowFragmentation = true;
293 initialize(options);
294
295 transport->setMtu(105);
296
297 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
298 face->sendData(*data);
299
300 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
301}
302
303BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
304{
305 // Initialize with Options that enable fragmentation
306 GenericLinkService::Options options;
307 options.allowFragmentation = true;
308 initialize(options);
309
310 transport->setMtu(60);
311
312 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
313 face->sendData(*data);
314
315 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
316}
317
318BOOST_AUTO_TEST_CASE(ReassembleFragments)
319{
320 // Initialize with Options that enables reassembly
321 GenericLinkService::Options options;
322 options.allowReassembly = true;
323 initialize(options);
324
325 shared_ptr<Interest> interest = makeInterest(
326 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
327 lp::Packet packet(interest->wireEncode());
328
329 // fragment the packet
330 LpFragmenter fragmenter;
331 size_t mtu = 100;
332 bool isOk = false;
333 std::vector<lp::Packet> frags;
334 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
335 BOOST_REQUIRE(isOk);
336 BOOST_CHECK_GT(frags.size(), 1);
337
338 // receive the fragments
339 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
340 size_t sequence = 1000 + fragIndex;
341 frags[fragIndex].add<lp::SequenceField>(sequence);
342
343 transport->receivePacket(frags[fragIndex].wireEncode());
344
345 if (fragIndex > 0) {
346 BOOST_CHECK(receivedInterests.empty());
347 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
348 }
349 else {
350 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
351 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest);
352 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
353 }
354 }
355}
356
Eric Newberry86d31872015-09-23 16:24:59 -0700357BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
358{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700359 // Initialize with Options that disables reassembly
360 GenericLinkService::Options options;
361 options.allowReassembly = false;
362 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700363
364 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
365 lp::Packet packet(interest->wireEncode());
366 packet.set<lp::FragIndexField>(140);
367
368 transport->receivePacket(packet.wireEncode());
369
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700370 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700371 BOOST_CHECK(receivedInterests.empty());
372}
373
374BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
375{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700376 // Initialize with Options that disables reassembly
377 GenericLinkService::Options options;
378 options.allowReassembly = false;
379 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700380
381 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
382 lp::Packet packet(interest->wireEncode());
383 packet.set<lp::FragCountField>(276);
384
385 transport->receivePacket(packet.wireEncode());
386
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700387 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700388 BOOST_CHECK(receivedInterests.empty());
389}
390
391BOOST_AUTO_TEST_SUITE_END() // Fragmentation
392
393
Eric Newberrya98bf932015-09-21 00:58:47 -0700394BOOST_AUTO_TEST_SUITE(LocalFields)
395
Eric Newberrya98bf932015-09-21 00:58:47 -0700396BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
397{
Eric Newberry86d31872015-09-23 16:24:59 -0700398 // Initialize with Options that enables local fields
399 GenericLinkService::Options options;
400 options.allowLocalFields = true;
401 initialize(options);
402
403 shared_ptr<Interest> interest = makeInterest("/12345678");
404 lp::Packet packet(interest->wireEncode());
405 packet.set<lp::NextHopFaceIdField>(1000);
406
407 transport->receivePacket(packet.wireEncode());
408
409 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
410 BOOST_REQUIRE(receivedInterests.back().getLocalControlHeader().hasNextHopFaceId());
411 BOOST_CHECK_EQUAL(receivedInterests.back().getNextHopFaceId(), 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700412}
413
414BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
415{
Eric Newberry86d31872015-09-23 16:24:59 -0700416 // Initialize with Options that disables local fields
417 GenericLinkService::Options options;
418 options.allowLocalFields = false;
419 initialize(options);
420
421 shared_ptr<Interest> interest = makeInterest("/12345678");
422 lp::Packet packet(interest->wireEncode());
423 packet.set<lp::NextHopFaceIdField>(1000);
424
425 transport->receivePacket(packet.wireEncode());
426
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700427 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700428 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700429}
430
431BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
432{
Eric Newberry86d31872015-09-23 16:24:59 -0700433 // Initialize with Options that enables local fields
434 GenericLinkService::Options options;
435 options.allowLocalFields = true;
436 initialize(options);
437
438 shared_ptr<Data> data = makeData("/12345678");
439 lp::Packet packet(data->wireEncode());
440 packet.set<lp::NextHopFaceIdField>(1000);
441
442 transport->receivePacket(packet.wireEncode());
443
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700444 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700445 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700446}
447
448BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
449{
Eric Newberry86d31872015-09-23 16:24:59 -0700450 // Initialize with Options that enables local fields
451 GenericLinkService::Options options;
452 options.allowLocalFields = true;
453 initialize(options);
454
455 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
456 lp::Packet packet;
457 packet.set<lp::FragmentField>(std::make_pair(
458 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
459 packet.set<lp::NackField>(nack.getHeader());
460 packet.set<lp::NextHopFaceIdField>(1000);
461
462 transport->receivePacket(packet.wireEncode());
463
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700464 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700465 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700466}
467
468BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
469{
Eric Newberry86d31872015-09-23 16:24:59 -0700470 // Initialize with Options that enables local fields
471 GenericLinkService::Options options;
472 options.allowLocalFields = true;
473 initialize(options);
474
475 shared_ptr<Data> data = makeData("/12345678");
476 lp::Packet packet(data->wireEncode());
477 lp::CachePolicy policy;
478 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
479 packet.set<lp::CachePolicyField>(policy);
480
481 transport->receivePacket(packet.wireEncode());
482
483 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
484 BOOST_REQUIRE(receivedData.back().getLocalControlHeader().hasCachingPolicy());
485 BOOST_CHECK_EQUAL(receivedData.back().getCachingPolicy(),
486 ndn::nfd::LocalControlHeader::CachingPolicy::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700487}
488
489BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
490{
Eric Newberry86d31872015-09-23 16:24:59 -0700491 // Initialize with Options that disables local fields
492 GenericLinkService::Options options;
493 options.allowLocalFields = false;
494 initialize(options);
495
496 shared_ptr<Data> data = makeData("/12345678");
497 lp::Packet packet(data->wireEncode());
498 lp::CachePolicy policy;
499 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
500 packet.set<lp::CachePolicyField>(policy);
501
502 transport->receivePacket(packet.wireEncode());
503
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700504 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700505 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
506 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasCachingPolicy());
Eric Newberrya98bf932015-09-21 00:58:47 -0700507}
508
509BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
510{
Eric Newberry86d31872015-09-23 16:24:59 -0700511 // Initialize with Options that enables local fields
512 GenericLinkService::Options options;
513 options.allowLocalFields = true;
514 initialize(options);
515
516 shared_ptr<Interest> interest = makeInterest("/12345678");
517 lp::Packet packet(interest->wireEncode());
518 lp::CachePolicy policy;
519 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
520 packet.set<lp::CachePolicyField>(policy);
521
522 transport->receivePacket(packet.wireEncode());
523
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700524 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700525 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700526}
527
528BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
529{
Eric Newberry86d31872015-09-23 16:24:59 -0700530 // Initialize with Options that enables local fields
531 GenericLinkService::Options options;
532 options.allowLocalFields = true;
533 initialize(options);
534
535 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
536 lp::Packet packet(nack.getInterest().wireEncode());
537 packet.set<lp::NackField>(nack.getHeader());
538 lp::CachePolicy policy;
539 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
540 packet.set<lp::CachePolicyField>(policy);
541
542 transport->receivePacket(packet.wireEncode());
543
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700544 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700545 BOOST_CHECK(receivedNacks.empty());
546}
547
548BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
549{
550 // Initialize with Options that enables local fields
551 GenericLinkService::Options options;
552 options.allowLocalFields = true;
553 initialize(options);
554
555 shared_ptr<Interest> interest = makeInterest("/12345678");
556 interest->setIncomingFaceId(1000);
557
558 face->sendInterest(*interest);
559
560 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
561 lp::Packet sent(transport->sentPackets.back().packet);
562 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
563 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
564}
565
566BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
567{
568 // Initialize with Options that disables local fields
569 GenericLinkService::Options options;
570 options.allowLocalFields = false;
571 initialize(options);
572
573 shared_ptr<Interest> interest = makeInterest("/12345678");
574 interest->setIncomingFaceId(1000);
575
576 face->sendInterest(*interest);
577
578 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
579 lp::Packet sent(transport->sentPackets.back().packet);
580 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
581}
582
583BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
584{
585 // Initialize with Options that enables local fields
586 GenericLinkService::Options options;
587 options.allowLocalFields = true;
588 initialize(options);
589
590 shared_ptr<Interest> interest = makeInterest("/12345678");
591 lp::Packet packet(interest->wireEncode());
592 packet.set<lp::IncomingFaceIdField>(1000);
593
594 transport->receivePacket(packet.wireEncode());
595
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700596 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700597 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
598 BOOST_CHECK(!receivedInterests.back().getLocalControlHeader().hasIncomingFaceId());
599}
600
601BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
602{
603 // Initialize with Options that enables local fields
604 GenericLinkService::Options options;
605 options.allowLocalFields = true;
606 initialize(options);
607
608 shared_ptr<Data> data = makeData("/z1megUh9Bj");
609 lp::Packet packet(data->wireEncode());
610 packet.set<lp::IncomingFaceIdField>(1000);
611
612 transport->receivePacket(packet.wireEncode());
613
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700614 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700615 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
616 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasIncomingFaceId());
617}
618
619BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
620{
621 // Initialize with Options that enables local fields
622 GenericLinkService::Options options;
623 options.allowLocalFields = true;
624 initialize(options);
625
626 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
627 lp::Packet packet(nack.getInterest().wireEncode());
628 packet.set<lp::NackField>(nack.getHeader());
629 packet.set<lp::IncomingFaceIdField>(1000);
630
631 transport->receivePacket(packet.wireEncode());
632
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700633 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700634 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
635 BOOST_CHECK(!receivedNacks.back().getLocalControlHeader().hasIncomingFaceId());
Eric Newberrya98bf932015-09-21 00:58:47 -0700636}
637
638BOOST_AUTO_TEST_SUITE_END() // LocalFields
639
640
Eric Newberrya1939ba2015-10-09 12:35:03 -0700641BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
642
643BOOST_AUTO_TEST_CASE(WrongTlvType)
644{
645 // Initialize with Options that disables all services
646 GenericLinkService::Options options;
647 options.allowLocalFields = false;
648 initialize(options);
649
650 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
651
652 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
653
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700654 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700655 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
656 BOOST_CHECK_EQUAL(receivedData.size(), 0);
657 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
658}
659
660BOOST_AUTO_TEST_CASE(Unparsable)
661{
662 // Initialize with Options that disables all services
663 GenericLinkService::Options options;
664 options.allowLocalFields = false;
665 initialize(options);
666
667 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
668 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
669
670 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
671
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700672 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700673 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
674 BOOST_CHECK_EQUAL(receivedData.size(), 0);
675 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
676}
677
678BOOST_AUTO_TEST_SUITE_END() // Malformed
679
680
Eric Newberry86d31872015-09-23 16:24:59 -0700681BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
682BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700683
684} // namespace tests
685} // namespace face
686} // namespace nfd