blob: 7efedfdb0ddc8dfc7b6eec8dc44541f4805896c0 [file] [log] [blame]
Davide Pesavento9a00fab2016-09-27 11:22:46 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento19779d82019-02-14 13:40:04 -05002/*
3 * Copyright (c) 2014-2019, Regents of the University of California,
Davide Pesavento9a00fab2016-09-27 11:22:46 +02004 * 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_CHANNEL_FIXTURE_HPP
27#define NFD_TESTS_DAEMON_FACE_CHANNEL_FIXTURE_HPP
28
29#include "face/channel.hpp"
30#include "tests/limited-io.hpp"
31
32#include <type_traits>
33
34namespace nfd {
Davide Pesavento8fd15e62017-04-06 19:58:54 -040035namespace face {
Davide Pesavento9a00fab2016-09-27 11:22:46 +020036namespace tests {
37
Davide Pesavento8fd15e62017-04-06 19:58:54 -040038using namespace nfd::tests;
39
Davide Pesavento9a00fab2016-09-27 11:22:46 +020040template<class ChannelT, class EndpointT>
41class ChannelFixture : public BaseFixture
42{
43 static_assert(std::is_base_of<Channel, ChannelT>::value,
44 "ChannelFixture must be instantiated with a type derived from Channel");
45
46public:
47 virtual
48 ~ChannelFixture() = default;
49
50 static void
51 unexpectedFailure(uint32_t status, const std::string& reason)
52 {
53 BOOST_FAIL("No error expected, but got: [" << status << ": " << reason << "]");
54 }
55
56protected:
57 uint16_t
58 getNextPort()
59 {
60 return m_nextPort++;
61 }
62
63 virtual unique_ptr<ChannelT>
64 makeChannel()
65 {
Davide Pesavento19779d82019-02-14 13:40:04 -050066 BOOST_FAIL("Unimplemented");
67 return nullptr;
Davide Pesavento9a00fab2016-09-27 11:22:46 +020068 }
69
Davide Pesavento9a00fab2016-09-27 11:22:46 +020070 virtual unique_ptr<ChannelT>
Davide Pesavento19779d82019-02-14 13:40:04 -050071 makeChannel(const boost::asio::ip::address&, uint16_t port = 0)
Davide Pesavento9a00fab2016-09-27 11:22:46 +020072 {
Davide Pesavento19779d82019-02-14 13:40:04 -050073 BOOST_FAIL("Unimplemented");
74 return nullptr;
Davide Pesavento9a00fab2016-09-27 11:22:46 +020075 }
76
77 void
78 listen(const boost::asio::ip::address& addr)
79 {
80 listenerEp = EndpointT{addr, 7030};
81 listenerChannel = makeChannel(addr, 7030);
82 listenerChannel->listen(
83 [this] (const shared_ptr<Face>& newFace) {
84 BOOST_REQUIRE(newFace != nullptr);
85 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
86 listenerFaces.push_back(newFace);
87 limitedIo.afterOp();
88 },
89 ChannelFixture::unexpectedFailure);
90 }
91
92 virtual void
93 connect(ChannelT&)
94 {
Davide Pesavento19779d82019-02-14 13:40:04 -050095 BOOST_FAIL("Unimplemented");
Davide Pesavento9a00fab2016-09-27 11:22:46 +020096 }
97
98protected:
99 LimitedIo limitedIo;
100 EndpointT listenerEp;
101 unique_ptr<ChannelT> listenerChannel;
102 std::vector<shared_ptr<Face>> listenerFaces;
103
104private:
105 uint16_t m_nextPort = 7050;
106};
107
108} // namespace tests
Davide Pesavento8fd15e62017-04-06 19:58:54 -0400109} // namespace face
Davide Pesavento9a00fab2016-09-27 11:22:46 +0200110} // namespace nfd
111
112#endif // NFD_TESTS_DAEMON_FACE_CHANNEL_FIXTURE_HPP