blob: 888c222e6a14ace574df83ca7382eec7c4b345ac [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"
Junxiao Shicde37ad2015-12-24 01:02:05 -070027#include "face/face.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070028#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
Junxiao Shicde37ad2015-12-24 01:02:05 -070040using nfd::Face;
41
Eric Newberrya98bf932015-09-21 00:58:47 -070042class GenericLinkServiceFixture : public BaseFixture
43{
44protected:
45 GenericLinkServiceFixture()
46 : service(nullptr)
47 , transport(nullptr)
48 {
Eric Newberry86d31872015-09-23 16:24:59 -070049 this->initialize(GenericLinkService::Options());
Eric Newberrya98bf932015-09-21 00:58:47 -070050 // By default, GenericLinkService is created with default options.
51 // Test cases may invoke .initialize with alternate options.
52 }
53
54 void
Eric Newberry86d31872015-09-23 16:24:59 -070055 initialize(const GenericLinkService::Options& options)
Eric Newberrya98bf932015-09-21 00:58:47 -070056 {
Junxiao Shicde37ad2015-12-24 01:02:05 -070057 face.reset(new Face(make_unique<GenericLinkService>(options),
58 make_unique<DummyTransport>()));
Eric Newberrya98bf932015-09-21 00:58:47 -070059 service = static_cast<GenericLinkService*>(face->getLinkService());
60 transport = static_cast<DummyTransport*>(face->getTransport());
61
62 face->afterReceiveInterest.connect(
63 [this] (const Interest& interest) { receivedInterests.push_back(interest); });
64 face->afterReceiveData.connect(
65 [this] (const Data& data) { receivedData.push_back(data); });
66 face->afterReceiveNack.connect(
67 [this] (const lp::Nack& nack) { receivedNacks.push_back(nack); });
68 }
69
70protected:
Junxiao Shicde37ad2015-12-24 01:02:05 -070071 unique_ptr<Face> face;
Eric Newberrya98bf932015-09-21 00:58:47 -070072 GenericLinkService* service;
73 DummyTransport* transport;
74 std::vector<Interest> receivedInterests;
75 std::vector<Data> receivedData;
76 std::vector<lp::Nack> receivedNacks;
77};
78
79BOOST_FIXTURE_TEST_SUITE(TestGenericLinkService, GenericLinkServiceFixture)
80
81
82BOOST_AUTO_TEST_SUITE(SimpleSendReceive) // send and receive without other fields
83
84BOOST_AUTO_TEST_CASE(SendInterest)
85{
Eric Newberry86d31872015-09-23 16:24:59 -070086 // Initialize with Options that disables all services
87 GenericLinkService::Options options;
88 options.allowLocalFields = false;
89 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -070090
91 shared_ptr<Interest> interest1 = makeInterest("/localhost/test");
92
93 face->sendInterest(*interest1);
94
Eric Newberry4c3e6b82015-11-10 16:48:42 -070095 BOOST_CHECK_EQUAL(service->getCounters().nOutInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -070096 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -070097 lp::Packet interest1pkt;
98 BOOST_REQUIRE_NO_THROW(interest1pkt.wireDecode(transport->sentPackets.back().packet));
99 BOOST_CHECK(interest1pkt.has<lp::FragmentField>());
100 BOOST_CHECK(!interest1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700101}
102
103BOOST_AUTO_TEST_CASE(SendData)
104{
Eric Newberry86d31872015-09-23 16:24:59 -0700105 // Initialize with Options that disables all services
106 GenericLinkService::Options options;
107 options.allowLocalFields = false;
108 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700109
110 shared_ptr<Data> data1 = makeData("/localhost/test");
111
112 face->sendData(*data1);
113
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700114 BOOST_CHECK_EQUAL(service->getCounters().nOutData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700115 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700116 lp::Packet data1pkt;
117 BOOST_REQUIRE_NO_THROW(data1pkt.wireDecode(transport->sentPackets.back().packet));
118 BOOST_CHECK(data1pkt.has<lp::FragmentField>());
119 BOOST_CHECK(!data1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700120}
121
122BOOST_AUTO_TEST_CASE(SendNack)
123{
Eric Newberry86d31872015-09-23 16:24:59 -0700124 // Initialize with Options that disables all services
125 GenericLinkService::Options options;
126 options.allowLocalFields = false;
127 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700128
129 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
130
131 face->sendNack(nack1);
132
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700133 BOOST_CHECK_EQUAL(service->getCounters().nOutNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700134 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
135 lp::Packet nack1pkt;
136 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700137 BOOST_CHECK(nack1pkt.has<lp::NackField>());
138 BOOST_CHECK(nack1pkt.has<lp::FragmentField>());
139 BOOST_CHECK(!nack1pkt.has<lp::SequenceField>());
Eric Newberrya98bf932015-09-21 00:58:47 -0700140}
141
142BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
143{
Eric Newberry86d31872015-09-23 16:24:59 -0700144 // Initialize with Options that disables all services
145 GenericLinkService::Options options;
146 options.allowLocalFields = false;
147 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700148
149 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
150
151 transport->receivePacket(interest1->wireEncode());
152
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700153 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700154 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
155 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
156}
157
158BOOST_AUTO_TEST_CASE(ReceiveInterest)
159{
Eric Newberry86d31872015-09-23 16:24:59 -0700160 // Initialize with Options that disables all services
161 GenericLinkService::Options options;
162 options.allowLocalFields = false;
163 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700164
165 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
166 lp::Packet lpPacket;
167 lpPacket.set<lp::FragmentField>(std::make_pair(
168 interest1->wireEncode().begin(), interest1->wireEncode().end()));
169 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
170
171 transport->receivePacket(lpPacket.wireEncode());
172
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700173 BOOST_CHECK_EQUAL(service->getCounters().nInInterests, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700174 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
175 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
176}
177
178BOOST_AUTO_TEST_CASE(ReceiveBareData)
179{
Eric Newberry86d31872015-09-23 16:24:59 -0700180 // Initialize with Options that disables all services
181 GenericLinkService::Options options;
182 options.allowLocalFields = false;
183 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700184
185 shared_ptr<Data> data1 = makeData("/12345678");
186
187 transport->receivePacket(data1->wireEncode());
188
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700189 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700190 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
191 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
192}
193
194BOOST_AUTO_TEST_CASE(ReceiveData)
195{
Eric Newberry86d31872015-09-23 16:24:59 -0700196 // Initialize with Options that disables all services
197 GenericLinkService::Options options;
198 options.allowLocalFields = false;
199 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700200
201 shared_ptr<Data> data1 = makeData("/12345689");
202 lp::Packet lpPacket;
203 lpPacket.set<lp::FragmentField>(std::make_pair(
204 data1->wireEncode().begin(), data1->wireEncode().end()));
205 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
206
207 transport->receivePacket(lpPacket.wireEncode());
208
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700209 BOOST_CHECK_EQUAL(service->getCounters().nInData, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700210 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
211 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
212}
213
214BOOST_AUTO_TEST_CASE(ReceiveNack)
215{
Eric Newberry86d31872015-09-23 16:24:59 -0700216 // Initialize with Options that disables all services
217 GenericLinkService::Options options;
218 options.allowLocalFields = false;
219 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700220
221 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
222 lp::Packet lpPacket;
223 lpPacket.set<lp::FragmentField>(std::make_pair(
224 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
225 lpPacket.set<lp::NackField>(nack1.getHeader());
226
227 transport->receivePacket(lpPacket.wireEncode());
228
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700229 BOOST_CHECK_EQUAL(service->getCounters().nInNacks, 1);
Eric Newberrya98bf932015-09-21 00:58:47 -0700230 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700231 BOOST_CHECK(receivedNacks.back().getReason() == nack1.getReason());
232 BOOST_CHECK(receivedNacks.back().getInterest() == nack1.getInterest());
Eric Newberrya98bf932015-09-21 00:58:47 -0700233}
234
Eric Newberrya1939ba2015-10-09 12:35:03 -0700235BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
236{
237 // Initialize with Options that disables all services
238 GenericLinkService::Options options;
239 options.allowLocalFields = false;
240 initialize(options);
241
242 lp::Packet lpPacket;
243 lpPacket.set<lp::SequenceField>(0);
244
245 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
246
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700247 // IDLE packet should be ignored, but is not an error
248 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700249 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
250 BOOST_CHECK_EQUAL(receivedData.size(), 0);
251 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
252}
253
Eric Newberrya98bf932015-09-21 00:58:47 -0700254BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
255
256
Eric Newberry86d31872015-09-23 16:24:59 -0700257BOOST_AUTO_TEST_SUITE(Fragmentation)
258
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700259BOOST_AUTO_TEST_CASE(FragmentationDisabledExceedMtuDrop)
260{
261 // Initialize with Options that disable fragmentation
262 GenericLinkService::Options options;
263 options.allowFragmentation = false;
264 initialize(options);
265
266 transport->setMtu(55);
267
268 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
269 face->sendData(*data);
270
271 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 0);
272 BOOST_CHECK_EQUAL(service->getCounters().nOutOverMtu, 1);
273}
274
275BOOST_AUTO_TEST_CASE(FragmentationUnlimitedMtu)
276{
277 // Initialize with Options that enable fragmentation
278 GenericLinkService::Options options;
279 options.allowFragmentation = true;
280 initialize(options);
281
282 transport->setMtu(MTU_UNLIMITED);
283
284 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
285 face->sendData(*data);
286
287 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
288}
289
290BOOST_AUTO_TEST_CASE(FragmentationUnderMtu)
291{
292 // Initialize with Options that enable fragmentation
293 GenericLinkService::Options options;
294 options.allowFragmentation = true;
295 initialize(options);
296
297 transport->setMtu(105);
298
299 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
300 face->sendData(*data);
301
302 BOOST_CHECK_EQUAL(transport->sentPackets.size(), 1);
303}
304
305BOOST_AUTO_TEST_CASE(FragmentationOverMtu)
306{
307 // Initialize with Options that enable fragmentation
308 GenericLinkService::Options options;
309 options.allowFragmentation = true;
310 initialize(options);
311
312 transport->setMtu(60);
313
314 shared_ptr<Data> data = makeData("/test/data/123456789/987654321/123456789");
315 face->sendData(*data);
316
317 BOOST_CHECK_GT(transport->sentPackets.size(), 1);
318}
319
320BOOST_AUTO_TEST_CASE(ReassembleFragments)
321{
322 // Initialize with Options that enables reassembly
323 GenericLinkService::Options options;
324 options.allowReassembly = true;
325 initialize(options);
326
327 shared_ptr<Interest> interest = makeInterest(
328 "/mt7P130BHXmtLm5dwaY5dpUM6SWYNN2B05g7y3UhsQuLvDdnTWdNnTeEiLuW3FAbJRSG3tzQ0UfaSEgG9rvYHmsKtgPMag1Hj4Tr");
329 lp::Packet packet(interest->wireEncode());
330
331 // fragment the packet
332 LpFragmenter fragmenter;
333 size_t mtu = 100;
334 bool isOk = false;
335 std::vector<lp::Packet> frags;
336 std::tie(isOk, frags) = fragmenter.fragmentPacket(packet, mtu);
337 BOOST_REQUIRE(isOk);
338 BOOST_CHECK_GT(frags.size(), 1);
339
340 // receive the fragments
341 for (ssize_t fragIndex = frags.size() - 1; fragIndex >= 0; --fragIndex) {
342 size_t sequence = 1000 + fragIndex;
343 frags[fragIndex].add<lp::SequenceField>(sequence);
344
345 transport->receivePacket(frags[fragIndex].wireEncode());
346
347 if (fragIndex > 0) {
348 BOOST_CHECK(receivedInterests.empty());
349 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 1);
350 }
351 else {
352 BOOST_CHECK_EQUAL(receivedInterests.size(), 1);
353 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest);
354 BOOST_CHECK_EQUAL(service->getCounters().nReassembling, 0);
355 }
356 }
357}
358
Eric Newberry86d31872015-09-23 16:24:59 -0700359BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
360{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700361 // Initialize with Options that disables reassembly
362 GenericLinkService::Options options;
363 options.allowReassembly = false;
364 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700365
366 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
367 lp::Packet packet(interest->wireEncode());
368 packet.set<lp::FragIndexField>(140);
369
370 transport->receivePacket(packet.wireEncode());
371
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700372 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700373 BOOST_CHECK(receivedInterests.empty());
374}
375
376BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
377{
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700378 // Initialize with Options that disables reassembly
379 GenericLinkService::Options options;
380 options.allowReassembly = false;
381 initialize(options);
Eric Newberry86d31872015-09-23 16:24:59 -0700382
383 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
384 lp::Packet packet(interest->wireEncode());
385 packet.set<lp::FragCountField>(276);
386
387 transport->receivePacket(packet.wireEncode());
388
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700389 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700390 BOOST_CHECK(receivedInterests.empty());
391}
392
393BOOST_AUTO_TEST_SUITE_END() // Fragmentation
394
395
Eric Newberrya98bf932015-09-21 00:58:47 -0700396BOOST_AUTO_TEST_SUITE(LocalFields)
397
Eric Newberrya98bf932015-09-21 00:58:47 -0700398BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
399{
Eric Newberry86d31872015-09-23 16:24:59 -0700400 // Initialize with Options that enables local fields
401 GenericLinkService::Options options;
402 options.allowLocalFields = true;
403 initialize(options);
404
405 shared_ptr<Interest> interest = makeInterest("/12345678");
406 lp::Packet packet(interest->wireEncode());
407 packet.set<lp::NextHopFaceIdField>(1000);
408
409 transport->receivePacket(packet.wireEncode());
410
411 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000412 shared_ptr<lp::NextHopFaceIdTag> tag = receivedInterests.back().getTag<lp::NextHopFaceIdTag>();
413 BOOST_REQUIRE(tag != nullptr);
414 BOOST_CHECK_EQUAL(*tag, 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700415}
416
417BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
418{
Eric Newberry86d31872015-09-23 16:24:59 -0700419 // Initialize with Options that disables local fields
420 GenericLinkService::Options options;
421 options.allowLocalFields = false;
422 initialize(options);
423
424 shared_ptr<Interest> interest = makeInterest("/12345678");
425 lp::Packet packet(interest->wireEncode());
426 packet.set<lp::NextHopFaceIdField>(1000);
427
428 transport->receivePacket(packet.wireEncode());
429
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700430 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700431 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700432}
433
434BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
435{
Eric Newberry86d31872015-09-23 16:24:59 -0700436 // Initialize with Options that enables local fields
437 GenericLinkService::Options options;
438 options.allowLocalFields = true;
439 initialize(options);
440
441 shared_ptr<Data> data = makeData("/12345678");
442 lp::Packet packet(data->wireEncode());
443 packet.set<lp::NextHopFaceIdField>(1000);
444
445 transport->receivePacket(packet.wireEncode());
446
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700447 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700448 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700449}
450
451BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
452{
Eric Newberry86d31872015-09-23 16:24:59 -0700453 // Initialize with Options that enables local fields
454 GenericLinkService::Options options;
455 options.allowLocalFields = true;
456 initialize(options);
457
458 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
459 lp::Packet packet;
460 packet.set<lp::FragmentField>(std::make_pair(
461 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
462 packet.set<lp::NackField>(nack.getHeader());
463 packet.set<lp::NextHopFaceIdField>(1000);
464
465 transport->receivePacket(packet.wireEncode());
466
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700467 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700468 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700469}
470
471BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
472{
Eric Newberry86d31872015-09-23 16:24:59 -0700473 // Initialize with Options that enables local fields
474 GenericLinkService::Options options;
475 options.allowLocalFields = true;
476 initialize(options);
477
478 shared_ptr<Data> data = makeData("/12345678");
479 lp::Packet packet(data->wireEncode());
Junxiao Shi0de23a22015-12-03 20:07:02 +0000480 packet.set<lp::CachePolicyField>(lp::CachePolicy().setPolicy(lp::CachePolicyType::NO_CACHE));
Eric Newberry86d31872015-09-23 16:24:59 -0700481
482 transport->receivePacket(packet.wireEncode());
483
484 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000485 shared_ptr<lp::CachePolicyTag> tag = receivedData.back().getTag<lp::CachePolicyTag>();
486 BOOST_REQUIRE(tag != nullptr);
487 BOOST_CHECK_EQUAL(tag->get().getPolicy(), lp::CachePolicyType::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700488}
489
490BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
491{
Eric Newberry86d31872015-09-23 16:24:59 -0700492 // Initialize with Options that disables local fields
493 GenericLinkService::Options options;
494 options.allowLocalFields = false;
495 initialize(options);
496
497 shared_ptr<Data> data = makeData("/12345678");
498 lp::Packet packet(data->wireEncode());
499 lp::CachePolicy policy;
500 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
501 packet.set<lp::CachePolicyField>(policy);
502
503 transport->receivePacket(packet.wireEncode());
504
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700505 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700506 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000507 BOOST_CHECK(receivedData.back().getTag<lp::CachePolicyTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700508}
509
510BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
511{
Eric Newberry86d31872015-09-23 16:24:59 -0700512 // Initialize with Options that enables local fields
513 GenericLinkService::Options options;
514 options.allowLocalFields = true;
515 initialize(options);
516
517 shared_ptr<Interest> interest = makeInterest("/12345678");
518 lp::Packet packet(interest->wireEncode());
519 lp::CachePolicy policy;
520 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
521 packet.set<lp::CachePolicyField>(policy);
522
523 transport->receivePacket(packet.wireEncode());
524
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700525 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700526 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700527}
528
529BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
530{
Eric Newberry86d31872015-09-23 16:24:59 -0700531 // Initialize with Options that enables local fields
532 GenericLinkService::Options options;
533 options.allowLocalFields = true;
534 initialize(options);
535
536 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
537 lp::Packet packet(nack.getInterest().wireEncode());
538 packet.set<lp::NackField>(nack.getHeader());
539 lp::CachePolicy policy;
540 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
541 packet.set<lp::CachePolicyField>(policy);
542
543 transport->receivePacket(packet.wireEncode());
544
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700545 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 1);
Eric Newberry86d31872015-09-23 16:24:59 -0700546 BOOST_CHECK(receivedNacks.empty());
547}
548
549BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
550{
551 // Initialize with Options that enables local fields
552 GenericLinkService::Options options;
553 options.allowLocalFields = true;
554 initialize(options);
555
556 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000557 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700558
559 face->sendInterest(*interest);
560
561 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
562 lp::Packet sent(transport->sentPackets.back().packet);
563 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
564 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
565}
566
567BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
568{
569 // Initialize with Options that disables local fields
570 GenericLinkService::Options options;
571 options.allowLocalFields = false;
572 initialize(options);
573
574 shared_ptr<Interest> interest = makeInterest("/12345678");
Junxiao Shi0de23a22015-12-03 20:07:02 +0000575 interest->setTag(make_shared<lp::IncomingFaceIdTag>(1000));
Eric Newberry86d31872015-09-23 16:24:59 -0700576
577 face->sendInterest(*interest);
578
579 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
580 lp::Packet sent(transport->sentPackets.back().packet);
581 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
582}
583
584BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
585{
586 // Initialize with Options that enables local fields
587 GenericLinkService::Options options;
588 options.allowLocalFields = true;
589 initialize(options);
590
591 shared_ptr<Interest> interest = makeInterest("/12345678");
592 lp::Packet packet(interest->wireEncode());
593 packet.set<lp::IncomingFaceIdField>(1000);
594
595 transport->receivePacket(packet.wireEncode());
596
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700597 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700598 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000599 BOOST_CHECK(receivedInterests.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700600}
601
602BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
603{
604 // Initialize with Options that enables local fields
605 GenericLinkService::Options options;
606 options.allowLocalFields = true;
607 initialize(options);
608
609 shared_ptr<Data> data = makeData("/z1megUh9Bj");
610 lp::Packet packet(data->wireEncode());
611 packet.set<lp::IncomingFaceIdField>(1000);
612
613 transport->receivePacket(packet.wireEncode());
614
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700615 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700616 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000617 BOOST_CHECK(receivedData.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberry86d31872015-09-23 16:24:59 -0700618}
619
620BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
621{
622 // Initialize with Options that enables local fields
623 GenericLinkService::Options options;
624 options.allowLocalFields = true;
625 initialize(options);
626
627 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
628 lp::Packet packet(nack.getInterest().wireEncode());
629 packet.set<lp::NackField>(nack.getHeader());
630 packet.set<lp::IncomingFaceIdField>(1000);
631
632 transport->receivePacket(packet.wireEncode());
633
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700634 BOOST_CHECK_EQUAL(service->getCounters().nInNetInvalid, 0); // not an error
Eric Newberry86d31872015-09-23 16:24:59 -0700635 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
Junxiao Shi0de23a22015-12-03 20:07:02 +0000636 BOOST_CHECK(receivedNacks.back().getTag<lp::IncomingFaceIdTag>() == nullptr);
Eric Newberrya98bf932015-09-21 00:58:47 -0700637}
638
639BOOST_AUTO_TEST_SUITE_END() // LocalFields
640
641
Eric Newberrya1939ba2015-10-09 12:35:03 -0700642BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
643
644BOOST_AUTO_TEST_CASE(WrongTlvType)
645{
646 // Initialize with Options that disables all services
647 GenericLinkService::Options options;
648 options.allowLocalFields = false;
649 initialize(options);
650
651 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
652
653 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
654
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700655 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700656 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
657 BOOST_CHECK_EQUAL(receivedData.size(), 0);
658 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
659}
660
661BOOST_AUTO_TEST_CASE(Unparsable)
662{
663 // Initialize with Options that disables all services
664 GenericLinkService::Options options;
665 options.allowLocalFields = false;
666 initialize(options);
667
668 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
669 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
670
671 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
672
Eric Newberry4c3e6b82015-11-10 16:48:42 -0700673 BOOST_CHECK_EQUAL(service->getCounters().nInLpInvalid, 1);
Eric Newberrya1939ba2015-10-09 12:35:03 -0700674 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
675 BOOST_CHECK_EQUAL(receivedData.size(), 0);
676 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
677}
678
679BOOST_AUTO_TEST_SUITE_END() // Malformed
680
681
Eric Newberry86d31872015-09-23 16:24:59 -0700682BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
683BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700684
685} // namespace tests
686} // namespace face
687} // namespace nfd