blob: a8e619d07eaa3e801bfa8a5c4e9f9d6629e0522b [file] [log] [blame]
Davide Pesavento9a00fab2016-09-27 11:22:46 +02001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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_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 {
35namespace tests {
36
37template<class ChannelT, class EndpointT>
38class ChannelFixture : public BaseFixture
39{
40 static_assert(std::is_base_of<Channel, ChannelT>::value,
41 "ChannelFixture must be instantiated with a type derived from Channel");
42
43public:
44 virtual
45 ~ChannelFixture() = default;
46
47 static void
48 unexpectedFailure(uint32_t status, const std::string& reason)
49 {
50 BOOST_FAIL("No error expected, but got: [" << status << ": " << reason << "]");
51 }
52
53protected:
54 uint16_t
55 getNextPort()
56 {
57 return m_nextPort++;
58 }
59
60 virtual unique_ptr<ChannelT>
61 makeChannel()
62 {
63 BOOST_THROW_EXCEPTION(std::logic_error("unimplemented"));
64 }
65
66 /**
67 * if port == 0, use the port number returned by getNextPort()
68 */
69 virtual unique_ptr<ChannelT>
70 makeChannel(const boost::asio::ip::address& addr, uint16_t port = 0)
71 {
72 BOOST_THROW_EXCEPTION(std::logic_error("unimplemented"));
73 }
74
75 void
76 listen(const boost::asio::ip::address& addr)
77 {
78 listenerEp = EndpointT{addr, 7030};
79 listenerChannel = makeChannel(addr, 7030);
80 listenerChannel->listen(
81 [this] (const shared_ptr<Face>& newFace) {
82 BOOST_REQUIRE(newFace != nullptr);
83 connectFaceClosedSignal(*newFace, [this] { limitedIo.afterOp(); });
84 listenerFaces.push_back(newFace);
85 limitedIo.afterOp();
86 },
87 ChannelFixture::unexpectedFailure);
88 }
89
90 virtual void
91 connect(ChannelT&)
92 {
93 BOOST_THROW_EXCEPTION(std::logic_error("unimplemented"));
94 }
95
96protected:
97 LimitedIo limitedIo;
98 EndpointT listenerEp;
99 unique_ptr<ChannelT> listenerChannel;
100 std::vector<shared_ptr<Face>> listenerFaces;
101
102private:
103 uint16_t m_nextPort = 7050;
104};
105
106} // namespace tests
107} // namespace nfd
108
109#endif // NFD_TESTS_DAEMON_FACE_CHANNEL_FIXTURE_HPP