blob: ac01c7d9a835a6a2d44a74adac1e18c27b9ae89c [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
93 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
94 BOOST_CHECK(transport->sentPackets.back().packet == interest1->wireEncode());
95}
96
97BOOST_AUTO_TEST_CASE(SendData)
98{
Eric Newberry86d31872015-09-23 16:24:59 -070099 // Initialize with Options that disables all services
100 GenericLinkService::Options options;
101 options.allowLocalFields = false;
102 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700103
104 shared_ptr<Data> data1 = makeData("/localhost/test");
105
106 face->sendData(*data1);
107
108 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
109 BOOST_CHECK(transport->sentPackets.back().packet == data1->wireEncode());
110}
111
112BOOST_AUTO_TEST_CASE(SendNack)
113{
Eric Newberry86d31872015-09-23 16:24:59 -0700114 // Initialize with Options that disables all services
115 GenericLinkService::Options options;
116 options.allowLocalFields = false;
117 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700118
119 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
120
121 face->sendNack(nack1);
122
123 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
124 lp::Packet nack1pkt;
125 BOOST_REQUIRE_NO_THROW(nack1pkt.wireDecode(transport->sentPackets.back().packet));
126 BOOST_CHECK_EQUAL(nack1pkt.has<lp::NackField>(), true);
127 BOOST_CHECK_EQUAL(nack1pkt.has<lp::FragmentField>(), true);
128}
129
130BOOST_AUTO_TEST_CASE(ReceiveBareInterest)
131{
Eric Newberry86d31872015-09-23 16:24:59 -0700132 // Initialize with Options that disables all services
133 GenericLinkService::Options options;
134 options.allowLocalFields = false;
135 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700136
137 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
138
139 transport->receivePacket(interest1->wireEncode());
140
141 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
142 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
143}
144
145BOOST_AUTO_TEST_CASE(ReceiveInterest)
146{
Eric Newberry86d31872015-09-23 16:24:59 -0700147 // Initialize with Options that disables all services
148 GenericLinkService::Options options;
149 options.allowLocalFields = false;
150 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700151
152 shared_ptr<Interest> interest1 = makeInterest("/23Rd9hEiR");
153 lp::Packet lpPacket;
154 lpPacket.set<lp::FragmentField>(std::make_pair(
155 interest1->wireEncode().begin(), interest1->wireEncode().end()));
156 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
157
158 transport->receivePacket(lpPacket.wireEncode());
159
160 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
161 BOOST_CHECK_EQUAL(receivedInterests.back(), *interest1);
162}
163
164BOOST_AUTO_TEST_CASE(ReceiveBareData)
165{
Eric Newberry86d31872015-09-23 16:24:59 -0700166 // Initialize with Options that disables all services
167 GenericLinkService::Options options;
168 options.allowLocalFields = false;
169 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700170
171 shared_ptr<Data> data1 = makeData("/12345678");
172
173 transport->receivePacket(data1->wireEncode());
174
175 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
176 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
177}
178
179BOOST_AUTO_TEST_CASE(ReceiveData)
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("/12345689");
187 lp::Packet lpPacket;
188 lpPacket.set<lp::FragmentField>(std::make_pair(
189 data1->wireEncode().begin(), data1->wireEncode().end()));
190 lpPacket.set<lp::SequenceField>(0); // force LpPacket encoding
191
192 transport->receivePacket(lpPacket.wireEncode());
193
194 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
195 BOOST_CHECK_EQUAL(receivedData.back(), *data1);
196}
197
198BOOST_AUTO_TEST_CASE(ReceiveNack)
199{
Eric Newberry86d31872015-09-23 16:24:59 -0700200 // Initialize with Options that disables all services
201 GenericLinkService::Options options;
202 options.allowLocalFields = false;
203 initialize(options);
Eric Newberrya98bf932015-09-21 00:58:47 -0700204
205 lp::Nack nack1 = makeNack("/localhost/test", 323, lp::NackReason::NO_ROUTE);
206 lp::Packet lpPacket;
207 lpPacket.set<lp::FragmentField>(std::make_pair(
208 nack1.getInterest().wireEncode().begin(), nack1.getInterest().wireEncode().end()));
209 lpPacket.set<lp::NackField>(nack1.getHeader());
210
211 transport->receivePacket(lpPacket.wireEncode());
212
213 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
214}
215
Eric Newberrya1939ba2015-10-09 12:35:03 -0700216BOOST_AUTO_TEST_CASE(ReceiveIdlePacket)
217{
218 // Initialize with Options that disables all services
219 GenericLinkService::Options options;
220 options.allowLocalFields = false;
221 initialize(options);
222
223 lp::Packet lpPacket;
224 lpPacket.set<lp::SequenceField>(0);
225
226 BOOST_CHECK_NO_THROW(transport->receivePacket(lpPacket.wireEncode()));
227
228 // IDLE packet should be ignored
229 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
230 BOOST_CHECK_EQUAL(receivedData.size(), 0);
231 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
232}
233
Eric Newberrya98bf932015-09-21 00:58:47 -0700234BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
235
236
Eric Newberry86d31872015-09-23 16:24:59 -0700237BOOST_AUTO_TEST_SUITE(Fragmentation)
238
239BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
240{
241 // TODO#3171 Initialize with Options that disables reassembly
242
243 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
244 lp::Packet packet(interest->wireEncode());
245 packet.set<lp::FragIndexField>(140);
246
247 transport->receivePacket(packet.wireEncode());
248
249 BOOST_CHECK(receivedInterests.empty());
250}
251
252BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
253{
254 // TODO#3171 Initialize with Options that disables reassembly
255
256 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
257 lp::Packet packet(interest->wireEncode());
258 packet.set<lp::FragCountField>(276);
259
260 transport->receivePacket(packet.wireEncode());
261
262 BOOST_CHECK(receivedInterests.empty());
263}
264
265BOOST_AUTO_TEST_SUITE_END() // Fragmentation
266
267
Eric Newberrya98bf932015-09-21 00:58:47 -0700268BOOST_AUTO_TEST_SUITE(LocalFields)
269
Eric Newberrya98bf932015-09-21 00:58:47 -0700270BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
271{
Eric Newberry86d31872015-09-23 16:24:59 -0700272 // Initialize with Options that enables local fields
273 GenericLinkService::Options options;
274 options.allowLocalFields = true;
275 initialize(options);
276
277 shared_ptr<Interest> interest = makeInterest("/12345678");
278 lp::Packet packet(interest->wireEncode());
279 packet.set<lp::NextHopFaceIdField>(1000);
280
281 transport->receivePacket(packet.wireEncode());
282
283 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
284 BOOST_REQUIRE(receivedInterests.back().getLocalControlHeader().hasNextHopFaceId());
285 BOOST_CHECK_EQUAL(receivedInterests.back().getNextHopFaceId(), 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700286}
287
288BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
289{
Eric Newberry86d31872015-09-23 16:24:59 -0700290 // Initialize with Options that disables local fields
291 GenericLinkService::Options options;
292 options.allowLocalFields = false;
293 initialize(options);
294
295 shared_ptr<Interest> interest = makeInterest("/12345678");
296 lp::Packet packet(interest->wireEncode());
297 packet.set<lp::NextHopFaceIdField>(1000);
298
299 transport->receivePacket(packet.wireEncode());
300
301 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700302}
303
304BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
305{
Eric Newberry86d31872015-09-23 16:24:59 -0700306 // Initialize with Options that enables local fields
307 GenericLinkService::Options options;
308 options.allowLocalFields = true;
309 initialize(options);
310
311 shared_ptr<Data> data = makeData("/12345678");
312 lp::Packet packet(data->wireEncode());
313 packet.set<lp::NextHopFaceIdField>(1000);
314
315 transport->receivePacket(packet.wireEncode());
316
317 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700318}
319
320BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
321{
Eric Newberry86d31872015-09-23 16:24:59 -0700322 // Initialize with Options that enables local fields
323 GenericLinkService::Options options;
324 options.allowLocalFields = true;
325 initialize(options);
326
327 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
328 lp::Packet packet;
329 packet.set<lp::FragmentField>(std::make_pair(
330 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
331 packet.set<lp::NackField>(nack.getHeader());
332 packet.set<lp::NextHopFaceIdField>(1000);
333
334 transport->receivePacket(packet.wireEncode());
335
336 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700337}
338
339BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
340{
Eric Newberry86d31872015-09-23 16:24:59 -0700341 // Initialize with Options that enables local fields
342 GenericLinkService::Options options;
343 options.allowLocalFields = true;
344 initialize(options);
345
346 shared_ptr<Data> data = makeData("/12345678");
347 lp::Packet packet(data->wireEncode());
348 lp::CachePolicy policy;
349 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
350 packet.set<lp::CachePolicyField>(policy);
351
352 transport->receivePacket(packet.wireEncode());
353
354 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
355 BOOST_REQUIRE(receivedData.back().getLocalControlHeader().hasCachingPolicy());
356 BOOST_CHECK_EQUAL(receivedData.back().getCachingPolicy(),
357 ndn::nfd::LocalControlHeader::CachingPolicy::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700358}
359
360BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
361{
Eric Newberry86d31872015-09-23 16:24:59 -0700362 // Initialize with Options that disables local fields
363 GenericLinkService::Options options;
364 options.allowLocalFields = false;
365 initialize(options);
366
367 shared_ptr<Data> data = makeData("/12345678");
368 lp::Packet packet(data->wireEncode());
369 lp::CachePolicy policy;
370 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
371 packet.set<lp::CachePolicyField>(policy);
372
373 transport->receivePacket(packet.wireEncode());
374
375 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
376 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasCachingPolicy());
Eric Newberrya98bf932015-09-21 00:58:47 -0700377}
378
379BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
380{
Eric Newberry86d31872015-09-23 16:24:59 -0700381 // Initialize with Options that enables local fields
382 GenericLinkService::Options options;
383 options.allowLocalFields = true;
384 initialize(options);
385
386 shared_ptr<Interest> interest = makeInterest("/12345678");
387 lp::Packet packet(interest->wireEncode());
388 lp::CachePolicy policy;
389 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
390 packet.set<lp::CachePolicyField>(policy);
391
392 transport->receivePacket(packet.wireEncode());
393
394 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700395}
396
397BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
398{
Eric Newberry86d31872015-09-23 16:24:59 -0700399 // Initialize with Options that enables local fields
400 GenericLinkService::Options options;
401 options.allowLocalFields = true;
402 initialize(options);
403
404 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
405 lp::Packet packet(nack.getInterest().wireEncode());
406 packet.set<lp::NackField>(nack.getHeader());
407 lp::CachePolicy policy;
408 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
409 packet.set<lp::CachePolicyField>(policy);
410
411 transport->receivePacket(packet.wireEncode());
412
413 BOOST_CHECK(receivedNacks.empty());
414}
415
416BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
417{
418 // Initialize with Options that enables local fields
419 GenericLinkService::Options options;
420 options.allowLocalFields = true;
421 initialize(options);
422
423 shared_ptr<Interest> interest = makeInterest("/12345678");
424 interest->setIncomingFaceId(1000);
425
426 face->sendInterest(*interest);
427
428 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
429 lp::Packet sent(transport->sentPackets.back().packet);
430 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
431 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
432}
433
434BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
435{
436 // Initialize with Options that disables local fields
437 GenericLinkService::Options options;
438 options.allowLocalFields = false;
439 initialize(options);
440
441 shared_ptr<Interest> interest = makeInterest("/12345678");
442 interest->setIncomingFaceId(1000);
443
444 face->sendInterest(*interest);
445
446 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
447 lp::Packet sent(transport->sentPackets.back().packet);
448 BOOST_CHECK(!sent.has<lp::IncomingFaceIdField>());
449}
450
451BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
452{
453 // Initialize with Options that enables local fields
454 GenericLinkService::Options options;
455 options.allowLocalFields = true;
456 initialize(options);
457
458 shared_ptr<Interest> interest = makeInterest("/12345678");
459 lp::Packet packet(interest->wireEncode());
460 packet.set<lp::IncomingFaceIdField>(1000);
461
462 transport->receivePacket(packet.wireEncode());
463
464 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
465 BOOST_CHECK(!receivedInterests.back().getLocalControlHeader().hasIncomingFaceId());
466}
467
468BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
469{
470 // 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("/z1megUh9Bj");
476 lp::Packet packet(data->wireEncode());
477 packet.set<lp::IncomingFaceIdField>(1000);
478
479 transport->receivePacket(packet.wireEncode());
480
481 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
482 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasIncomingFaceId());
483}
484
485BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
486{
487 // Initialize with Options that enables local fields
488 GenericLinkService::Options options;
489 options.allowLocalFields = true;
490 initialize(options);
491
492 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
493 lp::Packet packet(nack.getInterest().wireEncode());
494 packet.set<lp::NackField>(nack.getHeader());
495 packet.set<lp::IncomingFaceIdField>(1000);
496
497 transport->receivePacket(packet.wireEncode());
498
499 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
500 BOOST_CHECK(!receivedNacks.back().getLocalControlHeader().hasIncomingFaceId());
Eric Newberrya98bf932015-09-21 00:58:47 -0700501}
502
503BOOST_AUTO_TEST_SUITE_END() // LocalFields
504
505
Eric Newberrya1939ba2015-10-09 12:35:03 -0700506BOOST_AUTO_TEST_SUITE(Malformed) // receive malformed packets
507
508BOOST_AUTO_TEST_CASE(WrongTlvType)
509{
510 // Initialize with Options that disables all services
511 GenericLinkService::Options options;
512 options.allowLocalFields = false;
513 initialize(options);
514
515 Block packet = ndn::encoding::makeEmptyBlock(tlv::Name);
516
517 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
518
519 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
520 BOOST_CHECK_EQUAL(receivedData.size(), 0);
521 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
522}
523
524BOOST_AUTO_TEST_CASE(Unparsable)
525{
526 // Initialize with Options that disables all services
527 GenericLinkService::Options options;
528 options.allowLocalFields = false;
529 initialize(options);
530
531 Block packet = ndn::encoding::makeStringBlock(lp::tlv::LpPacket, "x");
532 BOOST_CHECK_THROW(packet.parse(), tlv::Error);
533
534 BOOST_CHECK_NO_THROW(transport->receivePacket(packet));
535
536 BOOST_CHECK_EQUAL(receivedInterests.size(), 0);
537 BOOST_CHECK_EQUAL(receivedData.size(), 0);
538 BOOST_CHECK_EQUAL(receivedNacks.size(), 0);
539}
540
541BOOST_AUTO_TEST_SUITE_END() // Malformed
542
543
Eric Newberry86d31872015-09-23 16:24:59 -0700544BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
545BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700546
547} // namespace tests
548} // namespace face
549} // namespace nfd