blob: 2eb21fec8c133e65dd93315c9972f2d62cfae0a5 [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Eric Newberrya98bf932015-09-21 00:58:47 -070034
Davide Pesavento16916ae2019-03-29 23:53:26 -040035/** \brief Dummy Transport type used in unit tests.
36 *
37 * All packets sent through this transport are stored in `sentPackets`.
38 * Reception of a packet can be simulated by invoking `receivePacket()`.
39 * All persistency changes are recorded in `persistencyHistory`.
Eric Newberrya98bf932015-09-21 00:58:47 -070040 */
Davide Pesavento16916ae2019-03-29 23:53:26 -040041template<bool CAN_CHANGE_PERSISTENCY>
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040042class DummyTransportBase : public face::NullTransport
Eric Newberrya98bf932015-09-21 00:58:47 -070043{
44public:
Davide Pesavento16916ae2019-03-29 23:53:26 -040045 explicit
46 DummyTransportBase(const std::string& localUri = "dummy://",
47 const std::string& remoteUri = "dummy://",
48 ndn::nfd::FaceScope scope = ndn::nfd::FACE_SCOPE_NON_LOCAL,
49 ndn::nfd::FacePersistency persistency = ndn::nfd::FACE_PERSISTENCY_PERSISTENT,
50 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_POINT_TO_POINT,
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040051 ssize_t mtu = face::MTU_UNLIMITED,
52 ssize_t sendQueueCapacity = face::QUEUE_UNSUPPORTED)
Davide Pesavento16916ae2019-03-29 23:53:26 -040053 : NullTransport(FaceUri(localUri), FaceUri(remoteUri), scope, persistency)
Eric Newberrya98bf932015-09-21 00:58:47 -070054 {
Eric Newberrya98bf932015-09-21 00:58:47 -070055 this->setLinkType(linkType);
Junxiao Shi57df2882015-11-11 06:12:35 -070056 this->setMtu(mtu);
Eric Newberryb49313d2017-12-24 20:22:27 -070057 this->setSendQueueCapacity(sendQueueCapacity);
Eric Newberrya98bf932015-09-21 00:58:47 -070058 }
59
Davide Pesavento16916ae2019-03-29 23:53:26 -040060 using NullTransport::setMtu;
61 using NullTransport::setState;
Eric Newberrya98bf932015-09-21 00:58:47 -070062
Eric Newberryb49313d2017-12-24 20:22:27 -070063 ssize_t
64 getSendQueueLength() override
65 {
66 return m_sendQueueLength;
67 }
68
69 void
70 setSendQueueLength(ssize_t sendQueueLength)
71 {
72 m_sendQueueLength = sendQueueLength;
73 }
74
Eric Newberrya98bf932015-09-21 00:58:47 -070075 void
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040076 receivePacket(const Block& block)
Eric Newberrya98bf932015-09-21 00:58:47 -070077 {
Davide Pesaventob3a23ca2019-05-04 20:40:21 -040078 receive(block);
Eric Newberrya98bf932015-09-21 00:58:47 -070079 }
80
Davide Pesavento8728a252015-11-06 04:01:22 +010081protected:
Yanbiao Li32dab972016-11-27 12:26:09 +080082 bool
Davide Pesavento16916ae2019-03-29 23:53:26 -040083 canChangePersistencyToImpl(ndn::nfd::FacePersistency) const override
Davide Pesavento8728a252015-11-06 04:01:22 +010084 {
Davide Pesavento16916ae2019-03-29 23:53:26 -040085 return CAN_CHANGE_PERSISTENCY;
86 }
87
88 void
89 afterChangePersistency(ndn::nfd::FacePersistency old) override
90 {
91 persistencyHistory.push_back(old);
Davide Pesavento8728a252015-11-06 04:01:22 +010092 }
93
Eric Newberrya98bf932015-09-21 00:58:47 -070094private:
Eric Newberryb49313d2017-12-24 20:22:27 -070095 void
Teng Liang13d582a2020-07-21 20:23:11 -070096 doSend(const Block& packet) override
Eric Newberrya98bf932015-09-21 00:58:47 -070097 {
Teng Liang13d582a2020-07-21 20:23:11 -070098 sentPackets.push_back(packet);
Eric Newberrya98bf932015-09-21 00:58:47 -070099 }
100
101public:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400102 std::vector<ndn::nfd::FacePersistency> persistencyHistory;
Teng Liang13d582a2020-07-21 20:23:11 -0700103 std::vector<Block> sentPackets;
Eric Newberryb49313d2017-12-24 20:22:27 -0700104
105private:
Davide Pesavento16916ae2019-03-29 23:53:26 -0400106 ssize_t m_sendQueueLength = 0;
Eric Newberrya98bf932015-09-21 00:58:47 -0700107};
108
Davide Pesavento16916ae2019-03-29 23:53:26 -0400109using DummyTransport = DummyTransportBase<true>;
110
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400111} // namespace nfd::tests
Eric Newberrya98bf932015-09-21 00:58:47 -0700112
113#endif // NFD_TESTS_DAEMON_FACE_DUMMY_TRANSPORT_HPP