Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2023, Regents of the University of California, |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 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 | #ifndef NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP |
| 27 | #define NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP |
| 28 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 29 | #include "face/face.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 30 | #include "face/face-system.hpp" |
Junxiao Shi | 64d99f2 | 2017-01-21 23:06:36 +0000 | [diff] [blame] | 31 | #include "face/protocol-factory.hpp" |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 32 | #include "fw/face-table.hpp" |
| 33 | |
| 34 | #include "tests/test-common.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 35 | #include "tests/daemon/global-io-fixture.hpp" |
Davide Pesavento | 22fba35 | 2017-10-17 15:53:51 -0400 | [diff] [blame] | 36 | #include "test-netif.hpp" |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 37 | |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 38 | #include <ndn-cxx/net/network-monitor-stub.hpp> |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 39 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 40 | namespace nfd::tests { |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 41 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame] | 42 | class FaceSystemFixture : public virtual GlobalIoFixture |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 43 | { |
| 44 | public: |
| 45 | FaceSystemFixture() |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 46 | { |
| 47 | faceSystem.setConfigFile(configFile); |
| 48 | } |
| 49 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 50 | /** |
| 51 | * \brief Copy a snapshot of NetworkInterface information to \p netmon. |
| 52 | * \pre netmon contains no NetworkInterface. |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 53 | */ |
| 54 | void |
| 55 | copyRealNetifsToNetmon() |
| 56 | { |
| 57 | BOOST_ASSERT(netmon->listNetworkInterfaces().empty()); |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 58 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 59 | 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 Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 74 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 75 | netmon->emitEnumerationCompleted(); |
| 76 | } |
| 77 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 78 | void |
| 79 | parseConfig(const std::string& text, bool isDryRun) |
| 80 | { |
| 81 | configFile.parse(text, isDryRun, "test-config"); |
| 82 | } |
| 83 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 84 | /** |
| 85 | * \brief Get ProtocolFactory from FaceSystem. |
| 86 | * \tparam F ProtocolFactory subclass |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 87 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 88 | * If ProtocolFactory with \p id does not exist or has an incompatible type, |
| 89 | * this fails the test case. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 90 | */ |
| 91 | template<typename F> |
| 92 | F& |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 93 | getFactoryById(const std::string& id) const |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 94 | { |
| 95 | F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id)); |
| 96 | BOOST_REQUIRE(factory != nullptr); |
| 97 | return *factory; |
| 98 | } |
| 99 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 100 | /** |
| 101 | * \brief Get ProtocolFactory from FaceSystem. |
| 102 | * \tparam F ProtocolFactory subclass |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 103 | * |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 104 | * If ProtocolFactory with \p scheme does not exist or has an incompatible type, |
| 105 | * this fails the test case. |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 106 | */ |
| 107 | template<typename F> |
| 108 | F& |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 109 | getFactoryByScheme(const std::string& scheme) const |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 110 | { |
| 111 | F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme)); |
| 112 | BOOST_REQUIRE(factory != nullptr); |
| 113 | return *factory; |
| 114 | } |
| 115 | |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 116 | /** |
| 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 Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 120 | */ |
| 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 Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 137 | protected: |
| 138 | ConfigFile configFile; |
| 139 | FaceTable faceTable; |
Davide Pesavento | c5a9f81 | 2023-10-17 18:01:52 -0400 | [diff] [blame] | 140 | shared_ptr<ndn::net::NetworkMonitorStub> netmon{make_shared<ndn::net::NetworkMonitorStub>(~0)}; |
| 141 | FaceSystem faceSystem{faceTable, netmon}; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 144 | /** |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 145 | * \brief FaceSystemFixture with a ProtocolFactory reference. |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 146 | */ |
| 147 | template<typename FactoryType> |
| 148 | class FaceSystemFactoryFixture : public FaceSystemFixture |
| 149 | { |
| 150 | protected: |
| 151 | FaceSystemFactoryFixture() |
| 152 | : factory(getFactoryById<FactoryType>(FactoryType::getId())) |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | protected: |
| 157 | FactoryType& factory; |
| 158 | }; |
| 159 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 160 | /** |
| 161 | * \brief A dummy ProtocolFactory for testing FaceSystem configuration parsing. |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 162 | */ |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 163 | class DummyProtocolFactory : public face::ProtocolFactory |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 164 | { |
| 165 | public: |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 166 | using ProtocolFactory::ProtocolFactory; |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 167 | |
| 168 | void |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 169 | doProcessConfig(OptionalConfigSection configSection, |
| 170 | FaceSystem::ConfigContext& context) final |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 171 | { |
| 172 | processConfigHistory.push_back({configSection, context.isDryRun, |
| 173 | context.generalConfig.wantCongestionMarking}); |
| 174 | if (!context.isDryRun) { |
Davide Pesavento | d27841b | 2018-11-13 00:22:24 -0500 | [diff] [blame] | 175 | providedSchemes = newProvidedSchemes; |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
| 178 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 179 | public: |
| 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 Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 191 | } // namespace nfd::tests |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 192 | |
| 193 | #endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP |