blob: 1891a970b7d9a1f9ed1078bba94528a5d4de8cb6 [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 Pesaventoe422f9e2022-06-03 01:30:23 -04003 * Copyright (c) 2014-2022, 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 Shi2d491752017-07-14 21:32:05 +000046 : netmon(make_shared<ndn::net::NetworkMonitorStub>(~0))
47 , faceSystem(faceTable, netmon)
Junxiao Shieef49a92018-11-10 12:19:36 +000048 , netdevBound(*faceSystem.m_netdevBound)
Junxiao Shi38b24c72017-01-05 02:59:31 +000049 {
50 faceSystem.setConfigFile(configFile);
51 }
52
Junxiao Shi79a92082017-08-08 02:40:59 +000053 /** \brief Copy a snapshot of NetworkInterface information to \p netmon
54 * \pre netmon contains no NetworkInterface
55 */
56 void
57 copyRealNetifsToNetmon()
58 {
59 BOOST_ASSERT(netmon->listNetworkInterfaces().empty());
60 for (const auto& netif : collectNetworkInterfaces()) {
61 auto copy = netmon->makeNetworkInterface();
62 copy->setIndex(netif->getIndex());
63 copy->setName(netif->getName());
64 copy->setType(netif->getType());
65 copy->setFlags(netif->getFlags());
66 copy->setState(netif->getState());
67 copy->setMtu(netif->getMtu());
68 copy->setEthernetAddress(netif->getEthernetAddress());
69 copy->setEthernetBroadcastAddress(netif->getEthernetBroadcastAddress());
70 for (const auto& na : netif->getNetworkAddresses()) {
71 copy->addNetworkAddress(na);
72 }
73 netmon->addInterface(copy);
74 }
75 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
84 /** \brief get ProtocolFactory from FaceSystem
85 * \tparam F ProtocolFactory subclass
86 *
87 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
88 * this fails the test case.
89 */
90 template<typename F>
91 F&
92 getFactoryById(const std::string& id)
93 {
94 F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id));
95 BOOST_REQUIRE(factory != nullptr);
96 return *factory;
97 }
98
99 /** \brief get ProtocolFactory from FaceSystem
100 * \tparam F ProtocolFactory subclass
101 *
102 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
103 * this fails the test case.
104 */
105 template<typename F>
106 F&
107 getFactoryByScheme(const std::string& scheme)
108 {
109 F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme));
110 BOOST_REQUIRE(factory != nullptr);
111 return *factory;
112 }
113
Junxiao Shi7003c602017-01-10 13:35:28 +0000114 /** \brief list faces of specified scheme from FaceTable
115 * \param scheme local or remote FaceUri scheme
116 * \param linkType if not NONE, filter by specified LinkType
117 */
118 std::vector<const Face*>
119 listFacesByScheme(const std::string& scheme,
120 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE) const
121 {
122 std::vector<const Face*> faces;
123 for (const Face& face : faceTable) {
124 if ((face.getLocalUri().getScheme() == scheme ||
125 face.getRemoteUri().getScheme() == scheme) &&
126 (linkType == ndn::nfd::LINK_TYPE_NONE ||
127 face.getLinkType() == linkType)) {
128 faces.push_back(&face);
129 }
130 }
131 return faces;
132 }
133
Junxiao Shi38b24c72017-01-05 02:59:31 +0000134protected:
135 ConfigFile configFile;
136 FaceTable faceTable;
Junxiao Shi2d491752017-07-14 21:32:05 +0000137 shared_ptr<ndn::net::NetworkMonitorStub> netmon;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000138 FaceSystem faceSystem;
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400139 face::NetdevBound& netdevBound;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000140};
141
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400142/**
143 * \brief FaceSystemFixture with a ProtocolFactory reference
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000144 */
145template<typename FactoryType>
146class FaceSystemFactoryFixture : public FaceSystemFixture
147{
148protected:
149 FaceSystemFactoryFixture()
150 : factory(getFactoryById<FactoryType>(FactoryType::getId()))
151 {
152 }
153
154protected:
155 FactoryType& factory;
156};
157
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400158/**
159 * \brief A dummy ProtocolFactory for testing FaceSystem configuration parsing.
Junxiao Shieef49a92018-11-10 12:19:36 +0000160 */
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400161class DummyProtocolFactory : public face::ProtocolFactory
Junxiao Shieef49a92018-11-10 12:19:36 +0000162{
163public:
Davide Pesaventod27841b2018-11-13 00:22:24 -0500164 using ProtocolFactory::ProtocolFactory;
Junxiao Shieef49a92018-11-10 12:19:36 +0000165
166 void
Davide Pesaventod27841b2018-11-13 00:22:24 -0500167 doProcessConfig(OptionalConfigSection configSection,
168 FaceSystem::ConfigContext& context) final
Junxiao Shieef49a92018-11-10 12:19:36 +0000169 {
170 processConfigHistory.push_back({configSection, context.isDryRun,
171 context.generalConfig.wantCongestionMarking});
172 if (!context.isDryRun) {
Davide Pesaventod27841b2018-11-13 00:22:24 -0500173 providedSchemes = newProvidedSchemes;
Junxiao Shieef49a92018-11-10 12:19:36 +0000174 }
175 }
176
Junxiao Shieef49a92018-11-10 12:19:36 +0000177public:
178 struct ProcessConfigArgs
179 {
180 OptionalConfigSection configSection;
181 bool isDryRun;
182 bool wantCongestionMarking;
183 };
184 std::vector<ProcessConfigArgs> processConfigHistory;
185
186 std::set<std::string> newProvidedSchemes;
187};
188
Davide Pesaventoe422f9e2022-06-03 01:30:23 -0400189} // namespace nfd::tests
Junxiao Shi38b24c72017-01-05 02:59:31 +0000190
191#endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP