blob: aa2c7a79a587461e0d0377a9447445a5be926e8e [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
216BOOST_AUTO_TEST_SUITE_END() // SimpleSendReceive
217
218
Eric Newberry86d31872015-09-23 16:24:59 -0700219BOOST_AUTO_TEST_SUITE(Fragmentation)
220
221BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragIndex)
222{
223 // TODO#3171 Initialize with Options that disables reassembly
224
225 shared_ptr<Interest> interest = makeInterest("/IgFe6NvH");
226 lp::Packet packet(interest->wireEncode());
227 packet.set<lp::FragIndexField>(140);
228
229 transport->receivePacket(packet.wireEncode());
230
231 BOOST_CHECK(receivedInterests.empty());
232}
233
234BOOST_AUTO_TEST_CASE(ReassemblyDisabledDropFragCount)
235{
236 // TODO#3171 Initialize with Options that disables reassembly
237
238 shared_ptr<Interest> interest = makeInterest("/SeGmEjvIVX");
239 lp::Packet packet(interest->wireEncode());
240 packet.set<lp::FragCountField>(276);
241
242 transport->receivePacket(packet.wireEncode());
243
244 BOOST_CHECK(receivedInterests.empty());
245}
246
247BOOST_AUTO_TEST_SUITE_END() // Fragmentation
248
249
Eric Newberrya98bf932015-09-21 00:58:47 -0700250BOOST_AUTO_TEST_SUITE(LocalFields)
251
Eric Newberrya98bf932015-09-21 00:58:47 -0700252BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceId)
253{
Eric Newberry86d31872015-09-23 16:24:59 -0700254 // Initialize with Options that enables local fields
255 GenericLinkService::Options options;
256 options.allowLocalFields = true;
257 initialize(options);
258
259 shared_ptr<Interest> interest = makeInterest("/12345678");
260 lp::Packet packet(interest->wireEncode());
261 packet.set<lp::NextHopFaceIdField>(1000);
262
263 transport->receivePacket(packet.wireEncode());
264
265 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
266 BOOST_REQUIRE(receivedInterests.back().getLocalControlHeader().hasNextHopFaceId());
267 BOOST_CHECK_EQUAL(receivedInterests.back().getNextHopFaceId(), 1000);
Eric Newberrya98bf932015-09-21 00:58:47 -0700268}
269
270BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDisabled)
271{
Eric Newberry86d31872015-09-23 16:24:59 -0700272 // Initialize with Options that disables local fields
273 GenericLinkService::Options options;
274 options.allowLocalFields = false;
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_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700284}
285
286BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropData)
287{
Eric Newberry86d31872015-09-23 16:24:59 -0700288 // Initialize with Options that enables local fields
289 GenericLinkService::Options options;
290 options.allowLocalFields = true;
291 initialize(options);
292
293 shared_ptr<Data> data = makeData("/12345678");
294 lp::Packet packet(data->wireEncode());
295 packet.set<lp::NextHopFaceIdField>(1000);
296
297 transport->receivePacket(packet.wireEncode());
298
299 BOOST_CHECK(receivedData.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700300}
301
302BOOST_AUTO_TEST_CASE(ReceiveNextHopFaceIdDropNack)
303{
Eric Newberry86d31872015-09-23 16:24:59 -0700304 // Initialize with Options that enables local fields
305 GenericLinkService::Options options;
306 options.allowLocalFields = true;
307 initialize(options);
308
309 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
310 lp::Packet packet;
311 packet.set<lp::FragmentField>(std::make_pair(
312 nack.getInterest().wireEncode().begin(), nack.getInterest().wireEncode().end()));
313 packet.set<lp::NackField>(nack.getHeader());
314 packet.set<lp::NextHopFaceIdField>(1000);
315
316 transport->receivePacket(packet.wireEncode());
317
318 BOOST_CHECK(receivedNacks.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700319}
320
321BOOST_AUTO_TEST_CASE(ReceiveCacheControl)
322{
Eric Newberry86d31872015-09-23 16:24:59 -0700323 // Initialize with Options that enables local fields
324 GenericLinkService::Options options;
325 options.allowLocalFields = true;
326 initialize(options);
327
328 shared_ptr<Data> data = makeData("/12345678");
329 lp::Packet packet(data->wireEncode());
330 lp::CachePolicy policy;
331 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
332 packet.set<lp::CachePolicyField>(policy);
333
334 transport->receivePacket(packet.wireEncode());
335
336 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
337 BOOST_REQUIRE(receivedData.back().getLocalControlHeader().hasCachingPolicy());
338 BOOST_CHECK_EQUAL(receivedData.back().getCachingPolicy(),
339 ndn::nfd::LocalControlHeader::CachingPolicy::NO_CACHE);
Eric Newberrya98bf932015-09-21 00:58:47 -0700340}
341
342BOOST_AUTO_TEST_CASE(ReceiveCacheControlDisabled)
343{
Eric Newberry86d31872015-09-23 16:24:59 -0700344 // Initialize with Options that disables local fields
345 GenericLinkService::Options options;
346 options.allowLocalFields = false;
347 initialize(options);
348
349 shared_ptr<Data> data = makeData("/12345678");
350 lp::Packet packet(data->wireEncode());
351 lp::CachePolicy policy;
352 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
353 packet.set<lp::CachePolicyField>(policy);
354
355 transport->receivePacket(packet.wireEncode());
356
357 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
358 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasCachingPolicy());
Eric Newberrya98bf932015-09-21 00:58:47 -0700359}
360
361BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropInterest)
362{
Eric Newberry86d31872015-09-23 16:24:59 -0700363 // Initialize with Options that enables local fields
364 GenericLinkService::Options options;
365 options.allowLocalFields = true;
366 initialize(options);
367
368 shared_ptr<Interest> interest = makeInterest("/12345678");
369 lp::Packet packet(interest->wireEncode());
370 lp::CachePolicy policy;
371 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
372 packet.set<lp::CachePolicyField>(policy);
373
374 transport->receivePacket(packet.wireEncode());
375
376 BOOST_CHECK(receivedInterests.empty());
Eric Newberrya98bf932015-09-21 00:58:47 -0700377}
378
379BOOST_AUTO_TEST_CASE(ReceiveCacheControlDropNack)
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 lp::Nack nack = makeNack("/localhost/test", 123, lp::NackReason::NO_ROUTE);
387 lp::Packet packet(nack.getInterest().wireEncode());
388 packet.set<lp::NackField>(nack.getHeader());
389 lp::CachePolicy policy;
390 policy.setPolicy(lp::CachePolicyType::NO_CACHE);
391 packet.set<lp::CachePolicyField>(policy);
392
393 transport->receivePacket(packet.wireEncode());
394
395 BOOST_CHECK(receivedNacks.empty());
396}
397
398BOOST_AUTO_TEST_CASE(SendIncomingFaceId)
399{
400 // 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 interest->setIncomingFaceId(1000);
407
408 face->sendInterest(*interest);
409
410 BOOST_REQUIRE_EQUAL(transport->sentPackets.size(), 1);
411 lp::Packet sent(transport->sentPackets.back().packet);
412 BOOST_REQUIRE(sent.has<lp::IncomingFaceIdField>());
413 BOOST_CHECK_EQUAL(sent.get<lp::IncomingFaceIdField>(), 1000);
414}
415
416BOOST_AUTO_TEST_CASE(SendIncomingFaceIdDisabled)
417{
418 // Initialize with Options that disables local fields
419 GenericLinkService::Options options;
420 options.allowLocalFields = false;
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_CHECK(!sent.has<lp::IncomingFaceIdField>());
431}
432
433BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreInterest)
434{
435 // Initialize with Options that enables local fields
436 GenericLinkService::Options options;
437 options.allowLocalFields = true;
438 initialize(options);
439
440 shared_ptr<Interest> interest = makeInterest("/12345678");
441 lp::Packet packet(interest->wireEncode());
442 packet.set<lp::IncomingFaceIdField>(1000);
443
444 transport->receivePacket(packet.wireEncode());
445
446 BOOST_REQUIRE_EQUAL(receivedInterests.size(), 1);
447 BOOST_CHECK(!receivedInterests.back().getLocalControlHeader().hasIncomingFaceId());
448}
449
450BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreData)
451{
452 // Initialize with Options that enables local fields
453 GenericLinkService::Options options;
454 options.allowLocalFields = true;
455 initialize(options);
456
457 shared_ptr<Data> data = makeData("/z1megUh9Bj");
458 lp::Packet packet(data->wireEncode());
459 packet.set<lp::IncomingFaceIdField>(1000);
460
461 transport->receivePacket(packet.wireEncode());
462
463 BOOST_REQUIRE_EQUAL(receivedData.size(), 1);
464 BOOST_CHECK(!receivedData.back().getLocalControlHeader().hasIncomingFaceId());
465}
466
467BOOST_AUTO_TEST_CASE(ReceiveIncomingFaceIdIgnoreNack)
468{
469 // Initialize with Options that enables local fields
470 GenericLinkService::Options options;
471 options.allowLocalFields = true;
472 initialize(options);
473
474 lp::Nack nack = makeNack("/TPAhdiHz", 278, lp::NackReason::CONGESTION);
475 lp::Packet packet(nack.getInterest().wireEncode());
476 packet.set<lp::NackField>(nack.getHeader());
477 packet.set<lp::IncomingFaceIdField>(1000);
478
479 transport->receivePacket(packet.wireEncode());
480
481 BOOST_REQUIRE_EQUAL(receivedNacks.size(), 1);
482 BOOST_CHECK(!receivedNacks.back().getLocalControlHeader().hasIncomingFaceId());
Eric Newberrya98bf932015-09-21 00:58:47 -0700483}
484
485BOOST_AUTO_TEST_SUITE_END() // LocalFields
486
487
Eric Newberry86d31872015-09-23 16:24:59 -0700488BOOST_AUTO_TEST_SUITE_END() // TestGenericLinkService
489BOOST_AUTO_TEST_SUITE_END() // Face
Eric Newberrya98bf932015-09-21 00:58:47 -0700490
491} // namespace tests
492} // namespace face
493} // namespace nfd