blob: 1d6917984c49194e15ebb279262c49c546639c68 [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/*
Junxiao Shi38b24c72017-01-05 02:59:31 +00003 * Copyright (c) 2014-2017, Regents of the University of California,
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 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 Pesavento22fba352017-10-17 15:53:51 -040035#include "test-netif.hpp"
Junxiao Shi2d491752017-07-14 21:32:05 +000036#include <ndn-cxx/net/network-monitor-stub.hpp>
Junxiao Shi38b24c72017-01-05 02:59:31 +000037
38namespace nfd {
39namespace face {
40namespace tests {
41
42using namespace nfd::tests;
43
Junxiao Shi7003c602017-01-10 13:35:28 +000044class FaceSystemFixture : public virtual BaseFixture
Junxiao Shi38b24c72017-01-05 02:59:31 +000045{
46public:
47 FaceSystemFixture()
Junxiao Shi2d491752017-07-14 21:32:05 +000048 : netmon(make_shared<ndn::net::NetworkMonitorStub>(~0))
49 , faceSystem(faceTable, netmon)
Junxiao Shi38b24c72017-01-05 02:59:31 +000050 {
51 faceSystem.setConfigFile(configFile);
52 }
53
Junxiao Shi79a92082017-08-08 02:40:59 +000054 /** \brief Copy a snapshot of NetworkInterface information to \p netmon
55 * \pre netmon contains no NetworkInterface
56 */
57 void
58 copyRealNetifsToNetmon()
59 {
60 BOOST_ASSERT(netmon->listNetworkInterfaces().empty());
61 for (const auto& netif : collectNetworkInterfaces()) {
62 auto copy = netmon->makeNetworkInterface();
63 copy->setIndex(netif->getIndex());
64 copy->setName(netif->getName());
65 copy->setType(netif->getType());
66 copy->setFlags(netif->getFlags());
67 copy->setState(netif->getState());
68 copy->setMtu(netif->getMtu());
69 copy->setEthernetAddress(netif->getEthernetAddress());
70 copy->setEthernetBroadcastAddress(netif->getEthernetBroadcastAddress());
71 for (const auto& na : netif->getNetworkAddresses()) {
72 copy->addNetworkAddress(na);
73 }
74 netmon->addInterface(copy);
75 }
76 netmon->emitEnumerationCompleted();
77 }
78
Junxiao Shi38b24c72017-01-05 02:59:31 +000079 void
80 parseConfig(const std::string& text, bool isDryRun)
81 {
82 configFile.parse(text, isDryRun, "test-config");
83 }
84
85 /** \brief get ProtocolFactory from FaceSystem
86 * \tparam F ProtocolFactory subclass
87 *
88 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
89 * this fails the test case.
90 */
91 template<typename F>
92 F&
93 getFactoryById(const std::string& id)
94 {
95 F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id));
96 BOOST_REQUIRE(factory != nullptr);
97 return *factory;
98 }
99
100 /** \brief get ProtocolFactory from FaceSystem
101 * \tparam F ProtocolFactory subclass
102 *
103 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
104 * this fails the test case.
105 */
106 template<typename F>
107 F&
108 getFactoryByScheme(const std::string& scheme)
109 {
110 F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme));
111 BOOST_REQUIRE(factory != nullptr);
112 return *factory;
113 }
114
Junxiao Shi7003c602017-01-10 13:35:28 +0000115 /** \brief list faces of specified scheme from FaceTable
116 * \param scheme local or remote FaceUri scheme
117 * \param linkType if not NONE, filter by specified LinkType
118 */
119 std::vector<const Face*>
120 listFacesByScheme(const std::string& scheme,
121 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE) const
122 {
123 std::vector<const Face*> faces;
124 for (const Face& face : faceTable) {
125 if ((face.getLocalUri().getScheme() == scheme ||
126 face.getRemoteUri().getScheme() == scheme) &&
127 (linkType == ndn::nfd::LINK_TYPE_NONE ||
128 face.getLinkType() == linkType)) {
129 faces.push_back(&face);
130 }
131 }
132 return faces;
133 }
134
Junxiao Shi38b24c72017-01-05 02:59:31 +0000135protected:
136 ConfigFile configFile;
137 FaceTable faceTable;
Junxiao Shi2d491752017-07-14 21:32:05 +0000138 shared_ptr<ndn::net::NetworkMonitorStub> netmon;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000139 FaceSystem faceSystem;
140};
141
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000142/** \brief FaceSystemFixture with a ProtocolFactory reference
143 */
144template<typename FactoryType>
145class FaceSystemFactoryFixture : public FaceSystemFixture
146{
147protected:
148 FaceSystemFactoryFixture()
149 : factory(getFactoryById<FactoryType>(FactoryType::getId()))
150 {
151 }
152
153protected:
154 FactoryType& factory;
155};
156
Junxiao Shi38b24c72017-01-05 02:59:31 +0000157} // namespace tests
158} // namespace face
159} // namespace nfd
160
161#endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP