blob: 38cb2ead7406d3db6198ad0af7be1a63ad24c114 [file] [log] [blame]
Junxiao Shi38b24c72017-01-05 02:59:31 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi2d491752017-07-14 21:32:05 +00002/*
Davide Pesaventoc5a9f812023-10-17 18:01:52 -04003 * Copyright (c) 2014-2023, Regents of the University of California,
Junxiao Shi38b24c72017-01-05 02:59:31 +00004 * 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_FACE_SYSTEM_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP
28
Junxiao Shi7003c602017-01-10 13:35:28 +000029#include "face/face.hpp"
Junxiao Shi38b24c72017-01-05 02:59:31 +000030#include "face/face-system.hpp"
Junxiao Shi64d99f22017-01-21 23:06:36 +000031#include "face/protocol-factory.hpp"
Junxiao Shi38b24c72017-01-05 02:59:31 +000032#include "fw/face-table.hpp"
33
34#include "tests/test-common.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040035#include "tests/daemon/global-io-fixture.hpp"
Davide Pesavento22fba352017-10-17 15:53:51 -040036#include "test-netif.hpp"
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040037
Junxiao Shi2d491752017-07-14 21:32:05 +000038#include <ndn-cxx/net/network-monitor-stub.hpp>
Junxiao Shi38b24c72017-01-05 02:59:31 +000039
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040040namespace nfd::tests {
Junxiao Shi38b24c72017-01-05 02:59:31 +000041
Davide Pesaventocf7db2f2019-03-24 23:17:28 -040042class FaceSystemFixture : public virtual GlobalIoFixture
Junxiao Shi38b24c72017-01-05 02:59:31 +000043{
44public:
45 FaceSystemFixture()
Junxiao Shi38b24c72017-01-05 02:59:31 +000046 {
47 faceSystem.setConfigFile(configFile);
48 }
49
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040050 /**
51 * \brief Copy a snapshot of NetworkInterface information to \p netmon.
52 * \pre netmon contains no NetworkInterface.
Junxiao Shi79a92082017-08-08 02:40:59 +000053 */
54 void
55 copyRealNetifsToNetmon()
56 {
57 BOOST_ASSERT(netmon->listNetworkInterfaces().empty());
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040058
Junxiao Shi79a92082017-08-08 02:40:59 +000059 for (const auto& netif : collectNetworkInterfaces()) {
60 auto copy = netmon->makeNetworkInterface();
61 copy->setIndex(netif->getIndex());
62 copy->setName(netif->getName());
63 copy->setType(netif->getType());
64 copy->setFlags(netif->getFlags());
65 copy->setState(netif->getState());
66 copy->setMtu(netif->getMtu());
67 copy->setEthernetAddress(netif->getEthernetAddress());
68 copy->setEthernetBroadcastAddress(netif->getEthernetBroadcastAddress());
69 for (const auto& na : netif->getNetworkAddresses()) {
70 copy->addNetworkAddress(na);
71 }
72 netmon->addInterface(copy);
73 }
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040074
Junxiao Shi79a92082017-08-08 02:40:59 +000075 netmon->emitEnumerationCompleted();
76 }
77
Junxiao Shi38b24c72017-01-05 02:59:31 +000078 void
79 parseConfig(const std::string& text, bool isDryRun)
80 {
81 configFile.parse(text, isDryRun, "test-config");
82 }
83
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040084 /**
85 * \brief Get ProtocolFactory from FaceSystem.
86 * \tparam F ProtocolFactory subclass
Junxiao Shi38b24c72017-01-05 02:59:31 +000087 *
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040088 * If ProtocolFactory with \p id does not exist or has an incompatible type,
89 * this fails the test case.
Junxiao Shi38b24c72017-01-05 02:59:31 +000090 */
91 template<typename F>
92 F&
Davide Pesaventoc5a9f812023-10-17 18:01:52 -040093 getFactoryById(const std::string& id) const
Junxiao Shi38b24c72017-01-05 02:59:31 +000094 {
95 F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id));
96 BOOST_REQUIRE(factory != nullptr);
97 return *factory;
98 }
99
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400100 /**
101 * \brief Get ProtocolFactory from FaceSystem.
102 * \tparam F ProtocolFactory subclass
Junxiao Shi38b24c72017-01-05 02:59:31 +0000103 *
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400104 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
105 * this fails the test case.
Junxiao Shi38b24c72017-01-05 02:59:31 +0000106 */
107 template<typename F>
108 F&
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400109 getFactoryByScheme(const std::string& scheme) const
Junxiao Shi38b24c72017-01-05 02:59:31 +0000110 {
111 F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme));
112 BOOST_REQUIRE(factory != nullptr);
113 return *factory;
114 }
115
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400116 /**
117 * \brief List faces of specified scheme from FaceTable.
118 * \param scheme local or remote FaceUri scheme
119 * \param linkType if not NONE, filter by specified LinkType
Junxiao Shi7003c602017-01-10 13:35:28 +0000120 */
121 std::vector<const Face*>
122 listFacesByScheme(const std::string& scheme,
123 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE) const
124 {
125 std::vector<const Face*> faces;
126 for (const Face& face : faceTable) {
127 if ((face.getLocalUri().getScheme() == scheme ||
128 face.getRemoteUri().getScheme() == scheme) &&
129 (linkType == ndn::nfd::LINK_TYPE_NONE ||
130 face.getLinkType() == linkType)) {
131 faces.push_back(&face);
132 }
133 }
134 return faces;
135 }
136
Junxiao Shi38b24c72017-01-05 02:59:31 +0000137protected:
138 ConfigFile configFile;
139 FaceTable faceTable;
Davide Pesaventoc5a9f812023-10-17 18:01:52 -0400140 shared_ptr<ndn::net::NetworkMonitorStub> netmon{make_shared<ndn::net::NetworkMonitorStub>(~0)};
141 FaceSystem faceSystem{faceTable, netmon};
Junxiao Shi38b24c72017-01-05 02:59:31 +0000142};
143
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400144/**
Davide Pesaventoaa9e3b22022-10-21 17:00:07 -0400145 * \brief FaceSystemFixture with a ProtocolFactory reference.
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000146 */
147template<typename FactoryType>
148class FaceSystemFactoryFixture : public FaceSystemFixture
149{
150protected:
151 FaceSystemFactoryFixture()
152 : factory(getFactoryById<FactoryType>(FactoryType::getId()))
153 {
154 }
155
156protected:
157 FactoryType& factory;
158};
159
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400160/**
161 * \brief A dummy ProtocolFactory for testing FaceSystem configuration parsing.
Junxiao Shieef49a92018-11-10 12:19:36 +0000162 */
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400163class DummyProtocolFactory : public face::ProtocolFactory
Junxiao Shieef49a92018-11-10 12:19:36 +0000164{
165public:
Davide Pesaventod27841b2018-11-13 00:22:24 -0500166 using ProtocolFactory::ProtocolFactory;
Junxiao Shieef49a92018-11-10 12:19:36 +0000167
168 void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500169 doProcessConfig(OptionalConfigSection configSection,
170 FaceSystem::ConfigContext& context) final
Junxiao Shieef49a92018-11-10 12:19:36 +0000171 {
172 processConfigHistory.push_back({configSection, context.isDryRun,
173 context.generalConfig.wantCongestionMarking});
174 if (!context.isDryRun) {
Davide Pesaventod27841b2018-11-13 00:22:24 -0500175 providedSchemes = newProvidedSchemes;
Junxiao Shieef49a92018-11-10 12:19:36 +0000176 }
177 }
178
Junxiao Shieef49a92018-11-10 12:19:36 +0000179public:
180 struct ProcessConfigArgs
181 {
182 OptionalConfigSection configSection;
183 bool isDryRun;
184 bool wantCongestionMarking;
185 };
186 std::vector<ProcessConfigArgs> processConfigHistory;
187
188 std::set<std::string> newProvidedSchemes;
189};
190
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400191} // namespace nfd::tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000192
193#endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP