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