blob: b6143d41691f50daae138d69fe9ec10fc57bc115 [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"
Junxiao Shi2d491752017-07-14 21:32:05 +000035#include <ndn-cxx/net/network-monitor-stub.hpp>
Junxiao Shi38b24c72017-01-05 02:59:31 +000036
37namespace nfd {
38namespace face {
39namespace tests {
40
41using namespace nfd::tests;
42
Junxiao Shi7003c602017-01-10 13:35:28 +000043class FaceSystemFixture : public virtual BaseFixture
Junxiao Shi38b24c72017-01-05 02:59:31 +000044{
45public:
46 FaceSystemFixture()
Junxiao Shi2d491752017-07-14 21:32:05 +000047 : netmon(make_shared<ndn::net::NetworkMonitorStub>(~0))
48 , faceSystem(faceTable, netmon)
Junxiao Shi38b24c72017-01-05 02:59:31 +000049 {
50 faceSystem.setConfigFile(configFile);
51 }
52
53 void
54 parseConfig(const std::string& text, bool isDryRun)
55 {
56 configFile.parse(text, isDryRun, "test-config");
57 }
58
59 /** \brief get ProtocolFactory from FaceSystem
60 * \tparam F ProtocolFactory subclass
61 *
62 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
63 * this fails the test case.
64 */
65 template<typename F>
66 F&
67 getFactoryById(const std::string& id)
68 {
69 F* factory = dynamic_cast<F*>(faceSystem.getFactoryById(id));
70 BOOST_REQUIRE(factory != nullptr);
71 return *factory;
72 }
73
74 /** \brief get ProtocolFactory from FaceSystem
75 * \tparam F ProtocolFactory subclass
76 *
77 * If ProtocolFactory with \p scheme does not exist or has an incompatible type,
78 * this fails the test case.
79 */
80 template<typename F>
81 F&
82 getFactoryByScheme(const std::string& scheme)
83 {
84 F* factory = dynamic_cast<F*>(faceSystem.getFactoryByScheme(scheme));
85 BOOST_REQUIRE(factory != nullptr);
86 return *factory;
87 }
88
Junxiao Shi7003c602017-01-10 13:35:28 +000089 /** \brief list faces of specified scheme from FaceTable
90 * \param scheme local or remote FaceUri scheme
91 * \param linkType if not NONE, filter by specified LinkType
92 */
93 std::vector<const Face*>
94 listFacesByScheme(const std::string& scheme,
95 ndn::nfd::LinkType linkType = ndn::nfd::LINK_TYPE_NONE) const
96 {
97 std::vector<const Face*> faces;
98 for (const Face& face : faceTable) {
99 if ((face.getLocalUri().getScheme() == scheme ||
100 face.getRemoteUri().getScheme() == scheme) &&
101 (linkType == ndn::nfd::LINK_TYPE_NONE ||
102 face.getLinkType() == linkType)) {
103 faces.push_back(&face);
104 }
105 }
106 return faces;
107 }
108
Junxiao Shi38b24c72017-01-05 02:59:31 +0000109protected:
110 ConfigFile configFile;
111 FaceTable faceTable;
Junxiao Shi2d491752017-07-14 21:32:05 +0000112 shared_ptr<ndn::net::NetworkMonitorStub> netmon;
Junxiao Shi38b24c72017-01-05 02:59:31 +0000113 FaceSystem faceSystem;
114};
115
Junxiao Shi0ba6d642017-07-17 00:53:22 +0000116/** \brief FaceSystemFixture with a ProtocolFactory reference
117 */
118template<typename FactoryType>
119class FaceSystemFactoryFixture : public FaceSystemFixture
120{
121protected:
122 FaceSystemFactoryFixture()
123 : factory(getFactoryById<FactoryType>(FactoryType::getId()))
124 {
125 }
126
127protected:
128 FactoryType& factory;
129};
130
Junxiao Shi38b24c72017-01-05 02:59:31 +0000131} // namespace tests
132} // namespace face
133} // namespace nfd
134
135#endif // NFD_TESTS_DAEMON_FACE_FACE_SYSTEM_FIXTURE_HPP