blob: 70e078d849bf2a63d464e61a5fc06c1c959cbe5e [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);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000410 shared_ptr<lp::NextHopFaceIdTag> tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
411 BOOST_REQUIRE(tag != nullptr);
412 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700413}
414
415BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
416{
Eric Newberry86d31872015-09-23 16:24:59 -0700417 // Initialize with Options that disables local fields
418 GenericLinkService::Options options;
419 options.allowLocalFields = false;
420 initialize(options);
421
422 shared_ptr<Interest> interest = makeInterest("/12345678");
423 lp::Packet packet(interest->wireEncode());
424 packet.set<lp::NextHopFaceIdField>(1000);
425
426 transport->receivePacket(packet.wireEncode());
427
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700428 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700429 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700430}
431
432BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
433{
Eric Newberry86d31872015-09-23 16:24:59 -0700434 // Initialize with Options that enables local fields
435 GenericLinkService::Options options;
436 options.allowLocalFields = true;
437 initialize(options);
438
439 shared_ptr<Data> data = makeData("/12345678");
440 lp::Packet packet(data->wireEncode());
441 packet.set<lp::NextHopFaceIdField>(1000);
442
443 transport->receivePacket(packet.wireEncode());
444
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700445 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700446 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700447}
448
449BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
450{
Eric Newberry86d31872015-09-23 16:24:59 -0700451 // Initialize with Options that enables local fields
452 GenericLinkService::Options options;
453 options.allowLocalFields = true;
454 initialize(options);
455
456 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
457 lp::Packet packet;
458 packet.set<lp::FragmentField>(std::make_pair(
459 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
460 packet.set<lp::NackField>(nack.getHeader());
461 packet.set<lp::NextHopFaceIdField>(1000);
462
463 transport->receivePacket(packet.wireEncode());
464
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700465 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700466 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700467}
468
469BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
470{
Eric Newberry86d31872015-09-23 16:24:59 -0700471 // Initialize with Options that enables local fields
472 GenericLinkService::Options options;
473 options.allowLocalFields = true;
474 initialize(options);
475
476 shared_ptr<Data> data = makeData("/12345678");
477 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000478 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700479
480 transport->receivePacket(packet.wireEncode());
481
482 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000483 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
484 BOOST_REQUIRE(tag != nullptr);
485 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700486}
487
488BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
489{
Eric Newberry86d31872015-09-23 16:24:59 -0700490 // Initialize with Options that disables local fields
491 GenericLinkService::Options options;
492 options.allowLocalFields = false;
493 initialize(options);
494
495 shared_ptr<Data> data = makeData("/12345678");
496 lp::Packet packet(data->wireEncode());
497 lp::CachePolicy policy;
498 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
499 packet.set<lp::CachePolicyField>(policy);
500
501 transport->receivePacket(packet.wireEncode());
502
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700503 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700504 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000505 BOOST_CHECK(receivedData.back().getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700506}
507
508BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
509{
Eric Newberry86d31872015-09-23 16:24:59 -0700510 // Initialize with Options that enables local fields
511 GenericLinkService::Options options;
512 options.allowLocalFields = true;
513 initialize(options);
514
515 shared_ptr<Interest> interest = makeInterest("/12345678");
516 lp::Packet packet(interest->wireEncode());
517 lp::CachePolicy policy;
518 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
519 packet.set<lp::CachePolicyField>(policy);
520
521 transport->receivePacket(packet.wireEncode());
522
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700523 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700524 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700525}
526
527BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
528{
Eric Newberry86d31872015-09-23 16:24:59 -0700529 // Initialize with Options that enables local fields
530 GenericLinkService::Options options;
531 options.allowLocalFields = true;
532 initialize(options);
533
534 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
535 lp::Packet packet(nack.getInterest().wireEncode());
536 packet.set<lp::NackField>(nack.getHeader());
537 lp::CachePolicy policy;
538 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
539 packet.set<lp::CachePolicyField>(policy);
540
541 transport->receivePacket(packet.wireEncode());
542
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700543 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700544 BOOST_CHECK(receivedNacks.empty());
545}
546
547BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
548{
549 // Initialize with Options that enables local fields
550 GenericLinkService::Options options;
551 options.allowLocalFields = true;
552 initialize(options);
553
554 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000555 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700556
557 face->sendInterest(*interest);
558
559 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
560 lp::Packet sent(transport->sentPackets.back().packet);
561 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
562 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
563}
564
565BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
566{
567 // Initialize with Options that disables local fields
568 GenericLinkService::Options options;
569 options.allowLocalFields = false;
570 initialize(options);
571
572 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000573 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700574
575 face->sendInterest(*interest);
576
577 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
578 lp::Packet sent(transport->sentPackets.back().packet);
579 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
580}
581
582BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
583{
584 // Initialize with Options that enables local fields
585 GenericLinkService::Options options;
586 options.allowLocalFields = true;
587 initialize(options);
588
589 shared_ptr<Interest> interest = makeInterest("/12345678");
590 lp::Packet packet(interest->wireEncode());
591 packet.set<lp::IncomingFaceIdField>(1000);
592
593 transport->receivePacket(packet.wireEncode());
594
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700595 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700596 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000597 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700598}
599
600BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
601{
602 // Initialize with Options that enables local fields
603 GenericLinkService::Options options;
604 options.allowLocalFields = true;
605 initialize(options);
606
607 shared_ptr<Data> data = makeData("/z1megUh9Bj");
608 lp::Packet packet(data->wireEncode());
609 packet.set<lp::IncomingFaceIdField>(1000);
610
611 transport->receivePacket(packet.wireEncode());
612
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700613 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700614 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000615 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700616}
617
618BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
619{
620 // Initialize with Options that enables local fields
621 GenericLinkService::Options options;
622 options.allowLocalFields = true;
623 initialize(options);
624
625 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
626 lp::Packet packet(nack.getInterest().wireEncode());
627 packet.set<lp::NackField>(nack.getHeader());
628 packet.set<lp::IncomingFaceIdField>(1000);
629
630 transport->receivePacket(packet.wireEncode());
631
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700632 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700633 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000634 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700635}
636
637BOOST_AUTO_TEST_SUITE_END() // LocalFields
638
639
Eric Newberrya1939ba2015-10-09 12:35:03 -0700640BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
641
642BOOST_AUTO_TEST_CASE(WrongTlvType)
643{
644 // Initialize with Options that disables all services
645 GenericLinkService::Options options;
646 options.allowLocalFields = false;
647 initialize(options);
648
649 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
650
651 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
652
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700653 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700654 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
655 BOOST_CHECK_EQUAL(receivedData.size(), 0);
656 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
657}
658
659BOOST_AUTO_TEST_CASE(Unparsable)
660{
661 // Initialize with Options that disables all services
662 GenericLinkService::Options options;
663 options.allowLocalFields = false;
664 initialize(options);
665
666 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
667 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
668
669 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
670
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700671 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700672 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
673 BOOST_CHECK_EQUAL(receivedData.size(), 0);
674 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
675}
676
677BOOST_AUTO_TEST_SUITE_END() // Malformed
678
679
Eric Newberry86d31872015-09-23 16:24:59 -0700680BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
681BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700682
683} // namespace tests
684} // namespace face
685} // namespace nfd