blob: 3f3141e4fabe4c6f792b3bf33967dd5b49ef0acf [file] [log] [blame]
Eric Newberrya98bf932015-09-21 00:58:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Eric Newberryb49313d2017-12-24 20:22:27 -07002/*
Davide Pesavento16916ae2019-03-29 23:53:26 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Eric Newberrya98bf932015-09-21 00:58:47 -07004 * 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#ifndef NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
27#define NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP
28
Junxiao Shi9f5b01d2016-08-05 03:54:28 +000029#include "core/common.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070030
Davide Pesavento16916ae2019-03-29 23:53:26 -040031#include "face/null-transport.hpp"
Eric Newberrya98bf932015-09-21 00:58:47 -070032
33namespace nfd {
34namespace face {
35namespace tests {
36
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040037struct TxPacket
38{
39 Block packet;
40 EndpointId endpoint;
41};
42
Davide Pesavento16916ae2019-03-29 23:53:26 -040043/** \brief Dummy Transport type used in unit tests.
44 *
45 * All packets sent through this transport are stored in `sentPackets`.
46 * Reception of a packet can be simulated by invoking `receivePacket()`.
47 * All persistency changes are recorded in `persistencyHistory`.
Eric Newberrya98bf932015-09-21 00:58:47 -070048 */
Davide Pesavento16916ae2019-03-29 23:53:26 -040049template<bool CAN_CHANGE_PERSISTENCY>
50class DummyTransportBase : public NullTransport
Eric Newberrya98bf932015-09-21 00:58:47 -070051{
52public:
Davide Pesavento16916ae2019-03-29 23:53:26 -040053 explicit
54 DummyTransportBase(const std::string& localUri = "dummy://",
55 const std::string& remoteUri = "dummy://",
56 ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
57 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
58 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT,
59 ssize_t mtu = MTU_UNLIMITED,
60 ssize_t sendQueueCapacity = QUEUE_UNSUPPORTED)
61 : NullTransport(FaceUri(localUri), FaceUri(remoteUri), scope, persistency)
Eric Newberrya98bf932015-09-21 00:58:47 -070062 {
Eric Newberrya98bf932015-09-21 00:58:47 -070063 this->setLinkType(linkType);
Junxiao Shi57df2882015-11-11 06:12:35 -070064 this->setMtu(mtu);
Eric Newberryb49313d2017-12-24 20:22:27 -070065 this->setSendQueueCapacity(sendQueueCapacity);
Eric Newberrya98bf932015-09-21 00:58:47 -070066 }
67
Davide Pesavento16916ae2019-03-29 23:53:26 -040068 using NullTransport::setMtu;
69 using NullTransport::setState;
Eric Newberrya98bf932015-09-21 00:58:47 -070070
Eric Newberryb49313d2017-12-24 20:22:27 -070071 ssize_t
72 getSendQueueLength() override
73 {
74 return m_sendQueueLength;
75 }
76
77 void
78 setSendQueueLength(ssize_t sendQueueLength)
79 {
80 m_sendQueueLength = sendQueueLength;
81 }
82
Eric Newberrya98bf932015-09-21 00:58:47 -070083 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040084 receivePacket(const Block& block)
Eric Newberrya98bf932015-09-21 00:58:47 -070085 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040086 receive(block);
Eric Newberrya98bf932015-09-21 00:58:47 -070087 }
88
Davide Pesavento8728a252015-11-06 04:01:22 +010089protected:
Yanbiao Li32dab972016-11-27 12:26:09 +080090 bool
Davide Pesavento16916ae2019-03-29 23:53:26 -040091 canChangePersistencyToImpl(ndn::nfd::FacePersistency) const override
Davide Pesavento8728a252015-11-06 04:01:22 +010092 {
Davide Pesavento16916ae2019-03-29 23:53:26 -040093 return CAN_CHANGE_PERSISTENCY;
94 }
95
96 void
97 afterChangePersistency(ndn::nfd::FacePersistency old) override
98 {
99 persistencyHistory.push_back(old);
Davide Pesavento8728a252015-11-06 04:01:22 +0100100 }
101
Eric Newberrya98bf932015-09-21 00:58:47 -0700102private:
Eric Newberryb49313d2017-12-24 20:22:27 -0700103 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400104 doSend(const Block& packet, const EndpointId& endpoint) override
Eric Newberrya98bf932015-09-21 00:58:47 -0700105 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400106 sentPackets.push_back({packet, endpoint});
Eric Newberrya98bf932015-09-21 00:58:47 -0700107 }
108
109public:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400110 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
Davide Pesaventob3a23ca2019-05-04 20:40:21 -0400111 std::vector<TxPacket> sentPackets;
Eric Newberryb49313d2017-12-24 20:22:27 -0700112
113private:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400114 ssize_t m_sendQueueLength = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700115};
116
Davide Pesavento16916ae2019-03-29 23:53:26 -0400117using DummyTransport = DummyTransportBase<true>;
118
Eric Newberrya98bf932015-09-21 00:58:47 -0700119} // namespace tests
120} // namespace face
121} // namespace nfd
122
123#endif // NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP