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 | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2019, 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 | |
| 40 | namespace nfd { |
| 41 | namespace face { |
| 42 | namespace tests { |
| 43 | |
| 44 | using namespace nfd::tests; |
| 45 | |
Davide Pesavento | cf7db2f | 2019-03-24 23:17:28 -0400 | [diff] [blame^] | 46 | class FaceSystemFixture : public virtual GlobalIoFixture |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 47 | { |
| 48 | public: |
| 49 | FaceSystemFixture() |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 50 | : netmon(make_shared<ndn::net::NetworkMonitorStub>(~0)) |
| 51 | , faceSystem(faceTable, netmon) |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 52 | , netdevBound(*faceSystem.m_netdevBound) |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 53 | { |
| 54 | faceSystem.setConfigFile(configFile); |
| 55 | } |
| 56 | |
Junxiao Shi | 79a9208 | 2017-08-08 02:40:59 +0000 | [diff] [blame] | 57 | /** \brief Copy a snapshot of NetworkInterface information to \p netmon |
| 58 | * \pre netmon contains no NetworkInterface |
| 59 | */ |
| 60 | void |
| 61 | copyRealNetifsToNetmon() |
| 62 | { |
| 63 | BOOST_ASSERT(netmon->listNetworkInterfaces().empty()); |
| 64 | for (const auto& netif : collectNetworkInterfaces()) { |
| 65 | auto copy = netmon->makeNetworkInterface(); |
| 66 | copy->setIndex(netif->getIndex()); |
| 67 | copy->setName(netif->getName()); |
| 68 | copy->setType(netif->getType()); |
| 69 | copy->setFlags(netif->getFlags()); |
| 70 | copy->setState(netif->getState()); |
| 71 | copy->setMtu(netif->getMtu()); |
| 72 | copy->setEthernetAddress(netif->getEthernetAddress()); |
| 73 | copy->setEthernetBroadcastAddress(netif->getEthernetBroadcastAddress()); |
| 74 | for (const auto& na : netif->getNetworkAddresses()) { |
| 75 | copy->addNetworkAddress(na); |
| 76 | } |
| 77 | netmon->addInterface(copy); |
| 78 | } |
| 79 | netmon->emitEnumerationCompleted(); |
| 80 | } |
| 81 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 82 | void |
| 83 | parseConfig(const std::string& text, bool isDryRun) |
| 84 | { |
| 85 | configFile.parse(text, isDryRun, "test-config"); |
| 86 | } |
| 87 | |
| 88 | /** \brief get ProtocolFactory from FaceSystem |
| 89 | * \tparam F ProtocolFactory subclass |
| 90 | * |
| 91 | * If ProtocolFactory with \p scheme does not exist or has an incompatible type, |
| 92 | * this fails the test case. |
| 93 | */ |
| 94 | template<typename F> |
| 95 | F& |
| 96 | getFactoryById(const std::string& id) |
| 97 | { |
| 98 | F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id)); |
| 99 | BOOST_REQUIRE(factory != nullptr); |
| 100 | return *factory; |
| 101 | } |
| 102 | |
| 103 | /** \brief get ProtocolFactory from FaceSystem |
| 104 | * \tparam F ProtocolFactory subclass |
| 105 | * |
| 106 | * If ProtocolFactory with \p scheme does not exist or has an incompatible type, |
| 107 | * this fails the test case. |
| 108 | */ |
| 109 | template<typename F> |
| 110 | F& |
| 111 | getFactoryByScheme(const std::string& scheme) |
| 112 | { |
| 113 | F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme)); |
| 114 | BOOST_REQUIRE(factory != nullptr); |
| 115 | return *factory; |
| 116 | } |
| 117 | |
Junxiao Shi | 7003c60 | 2017-01-10 13:35:28 +0000 | [diff] [blame] | 118 | /** \brief list faces of specified scheme from FaceTable |
| 119 | * \param scheme local or remote FaceUri scheme |
| 120 | * \param linkType if not NONE, filter by specified LinkType |
| 121 | */ |
| 122 | std::vector<const Face*> |
| 123 | listFacesByScheme(const std::string& scheme, |
| 124 | ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE) const |
| 125 | { |
| 126 | std::vector<const Face*> faces; |
| 127 | for (const Face& face : faceTable) { |
| 128 | if ((face.getLocalUri().getScheme() == scheme || |
| 129 | face.getRemoteUri().getScheme() == scheme) && |
| 130 | (linkType == ndn::nfd::LINK_TYPE_NONE || |
| 131 | face.getLinkType() == linkType)) { |
| 132 | faces.push_back(&face); |
| 133 | } |
| 134 | } |
| 135 | return faces; |
| 136 | } |
| 137 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 138 | protected: |
| 139 | ConfigFile configFile; |
| 140 | FaceTable faceTable; |
Junxiao Shi | 2d49175 | 2017-07-14 21:32:05 +0000 | [diff] [blame] | 141 | shared_ptr<ndn::net::NetworkMonitorStub> netmon; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 142 | FaceSystem faceSystem; |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 143 | NetdevBound& netdevBound; |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Junxiao Shi | 0ba6d64 | 2017-07-17 00:53:22 +0000 | [diff] [blame] | 146 | /** \brief FaceSystemFixture with a ProtocolFactory reference |
| 147 | */ |
| 148 | template<typename FactoryType> |
| 149 | class FaceSystemFactoryFixture : public FaceSystemFixture |
| 150 | { |
| 151 | protected: |
| 152 | FaceSystemFactoryFixture() |
| 153 | : factory(getFactoryById<FactoryType>(FactoryType::getId())) |
| 154 | { |
| 155 | } |
| 156 | |
| 157 | protected: |
| 158 | FactoryType& factory; |
| 159 | }; |
| 160 | |
Junxiao Shi | eef49a9 | 2018-11-10 12:19:36 +0000 | [diff] [blame] | 161 | /** \brief A dummy ProtocolFactory for testing FaceSystem configuration parsing. |
| 162 | */ |
| 163 | class DummyProtocolFactory : public ProtocolFactory |
| 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 | |
Junxiao Shi | 38b24c7 | 2017-01-05 02:59:31 +0000 | [diff] [blame] | 191 | } // namespace tests |
| 192 | } // namespace face |
| 193 | } // namespace nfd |
| 194 | |
| 195 | #endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP |