blob: 45172540184f48c5a15dbf08c3ec37f9c6e1e12b [file] [log] [blame]
Junxiao Shi1f481fa2017-01-26 15:14:43 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shib347b7f2017-07-23 14:01:58 +00002/*
Davide Pesavento14e71f02019-03-28 17:35:25 -04003 * Copyright (c) 2014-2019, Regents of the University of California,
Junxiao Shi1f481fa2017-01-26 15:14:43 +00004 * 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_TOOLS_NFDC_STATUS_FIXTURE_HPP
27#define NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP
28
29#include "mock-nfd-mgmt-fixture.hpp"
30#include "nfdc/module.hpp"
Davide Pesavento14e71f02019-03-28 17:35:25 -040031
Junxiao Shi1f481fa2017-01-26 15:14:43 +000032#include <ndn-cxx/security/validator-null.hpp>
33
Davide Pesaventocf9e1c72019-12-22 17:56:07 -050034#if BOOST_VERSION >= 105900
35#include <boost/test/tools/output_test_stream.hpp>
36#else
37#include <boost/test/output_test_stream.hpp>
38#endif
39
Junxiao Shi1f481fa2017-01-26 15:14:43 +000040namespace nfd {
41namespace tools {
42namespace nfdc {
43namespace tests {
44
45using ndn::Face;
46using ndn::KeyChain;
Junxiao Shib347b7f2017-07-23 14:01:58 +000047using ndn::security::v2::Validator;
48using ndn::security::v2::ValidatorNull;
Junxiao Shi1f481fa2017-01-26 15:14:43 +000049using boost::test_tools::output_test_stream;
50
51class MakeValidatorNull
52{
53public:
54 unique_ptr<ValidatorNull>
55 operator()(Face&, KeyChain&) const
56 {
57 return make_unique<ValidatorNull>();
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040058 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +000059};
60
61/** \brief fixture to test status fetching routines in a \p Module
62 * \tparam M a subclass of \p Module
63 * \tparam MakeValidator a callable to make a Validator for use in \p controller;
64 * MakeValidator()(Face&, KeyChain&) should return a unique_ptr
65 * to Validator or its subclass
66 */
67template<typename M, typename MakeValidator = MakeValidatorNull>
68class StatusFixture : public MockNfdMgmtFixture
69{
70protected:
71 using ValidatorUniquePtr = typename std::result_of<MakeValidator(Face&, KeyChain&)>::type;
72
73 StatusFixture()
74 : validator(MakeValidator()(face, m_keyChain))
75 , controller(face, m_keyChain, *validator)
76 , nFetchStatusSuccess(0)
77 {
78 }
79
80protected: // status fetching
81 /** \brief start fetching status
82 *
83 * A test case should call \p fetchStatus, \p sendDataset, and \p prepareStatusOutput
84 * in this order, and then check \p statusXml and \p statusText contain the correct outputs.
85 * No advanceClocks is needed in between, as they are handled by the fixture.
86 */
87 void
88 fetchStatus()
89 {
90 nFetchStatusSuccess = 0;
Davide Pesaventoac238f22017-09-12 15:19:40 -040091 module.fetchStatus(controller,
92 [this] { ++nFetchStatusSuccess; },
93 [] (uint32_t code, const std::string& reason) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +000094 BOOST_FAIL("fetchStatus failure " << code << " " << reason);
95 },
96 CommandOptions());
Davide Pesavento14e71f02019-03-28 17:35:25 -040097 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000098 }
99
100 /** \brief prepare status output as XML and text
101 * \pre sendDataset has been invoked
102 */
103 void
104 prepareStatusOutput()
105 {
Davide Pesavento14e71f02019-03-28 17:35:25 -0400106 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000107 BOOST_REQUIRE_EQUAL(nFetchStatusSuccess, 1);
108
109 statusXml.str("");
110 module.formatStatusXml(statusXml);
111 statusText.str("");
112 module.formatStatusText(statusText);
113 }
114
115protected:
116 ValidatorUniquePtr validator;
117 Controller controller;
118
119 M module;
120
121 int nFetchStatusSuccess;
122 output_test_stream statusXml;
123 output_test_stream statusText;
124};
125
126/** \brief strips leading spaces on every line in expected XML
127 *
128 * This allows expected XML to be written as:
129 * \code
130 * const std::string STATUS_XML = stripXmlSpaces(R"XML(
131 * <rootElement>
132 * <element>value</element>
133 * </rootElement>
134 * )XML");
135 * \endcode
136 * And \p STATUS_XML would be assigned:
137 * \code
138 * "<rootElement><element>value</element></rootElement>"
139 * \endcode
140 */
141inline std::string
142stripXmlSpaces(const std::string& xml)
143{
144 std::string s;
145 bool isSkipping = true;
146 std::copy_if(xml.begin(), xml.end(), std::back_inserter(s),
147 [&isSkipping] (char ch) {
148 if (ch == '\n') {
149 isSkipping = true;
150 }
151 else if (ch != ' ') {
152 isSkipping = false;
153 }
154 return !isSkipping;
155 });
156 return s;
157}
158
159} // namespace tests
160} // namespace nfdc
161} // namespace tools
162} // namespace nfd
163
164#endif // NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP