blob: 96edbbad779599cbebdb135ed178f3f6ae3294df [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 Pesaventoc52cd5e2022-03-05 20:40:54 -05003 * Copyright (c) 2014-2022, 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#include <boost/test/tools/output_test_stream.hpp>
Davide Pesaventocf9e1c72019-12-22 17:56:07 -050035
Junxiao Shi1f481fa2017-01-26 15:14:43 +000036namespace nfd {
37namespace tools {
38namespace nfdc {
39namespace tests {
40
41using ndn::Face;
42using ndn::KeyChain;
Alexander Afanasyeva1583702020-06-03 13:55:45 -040043using ndn::security::Validator;
44using ndn::security::ValidatorNull;
Junxiao Shi1f481fa2017-01-26 15:14:43 +000045using boost::test_tools::output_test_stream;
46
47class MakeValidatorNull
48{
49public:
50 unique_ptr<ValidatorNull>
51 operator()(Face&, KeyChain&) const
52 {
53 return make_unique<ValidatorNull>();
Davide Pesaventoc0a5a392017-08-19 16:49:30 -040054 }
Junxiao Shi1f481fa2017-01-26 15:14:43 +000055};
56
57/** \brief fixture to test status fetching routines in a \p Module
58 * \tparam M a subclass of \p Module
59 * \tparam MakeValidator a callable to make a Validator for use in \p controller;
60 * MakeValidator()(Face&, KeyChain&) should return a unique_ptr
61 * to Validator or its subclass
62 */
63template<typename M, typename MakeValidator = MakeValidatorNull>
64class StatusFixture : public MockNfdMgmtFixture
65{
66protected:
67 using ValidatorUniquePtr = typename std::result_of<MakeValidator(Face&, KeyChain&)>::type;
68
69 StatusFixture()
70 : validator(MakeValidator()(face, m_keyChain))
71 , controller(face, m_keyChain, *validator)
72 , nFetchStatusSuccess(0)
73 {
74 }
75
76protected: // status fetching
77 /** \brief start fetching status
78 *
79 * A test case should call \p fetchStatus, \p sendDataset, and \p prepareStatusOutput
80 * in this order, and then check \p statusXml and \p statusText contain the correct outputs.
81 * No advanceClocks is needed in between, as they are handled by the fixture.
82 */
83 void
84 fetchStatus()
85 {
86 nFetchStatusSuccess = 0;
Davide Pesaventoac238f22017-09-12 15:19:40 -040087 module.fetchStatus(controller,
88 [this] { ++nFetchStatusSuccess; },
89 [] (uint32_t code, const std::string& reason) {
Junxiao Shi1f481fa2017-01-26 15:14:43 +000090 BOOST_FAIL("fetchStatus failure " << code << " " << reason);
91 },
92 CommandOptions());
Davide Pesavento14e71f02019-03-28 17:35:25 -040093 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +000094 }
95
96 /** \brief prepare status output as XML and text
97 * \pre sendDataset has been invoked
98 */
99 void
100 prepareStatusOutput()
101 {
Davide Pesavento14e71f02019-03-28 17:35:25 -0400102 this->advanceClocks(1_ms);
Junxiao Shi1f481fa2017-01-26 15:14:43 +0000103 BOOST_REQUIRE_EQUAL(nFetchStatusSuccess, 1);
104
105 statusXml.str("");
106 module.formatStatusXml(statusXml);
107 statusText.str("");
108 module.formatStatusText(statusText);
109 }
110
111protected:
112 ValidatorUniquePtr validator;
113 Controller controller;
114
115 M module;
116
117 int nFetchStatusSuccess;
118 output_test_stream statusXml;
119 output_test_stream statusText;
120};
121
122/** \brief strips leading spaces on every line in expected XML
123 *
124 * This allows expected XML to be written as:
125 * \code
126 * const std::string STATUS_XML = stripXmlSpaces(R"XML(
127 * <rootElement>
128 * <element>value</element>
129 * </rootElement>
130 * )XML");
131 * \endcode
132 * And \p STATUS_XML would be assigned:
133 * \code
134 * "<rootElement><element>value</element></rootElement>"
135 * \endcode
136 */
137inline std::string
138stripXmlSpaces(const std::string& xml)
139{
140 std::string s;
141 bool isSkipping = true;
142 std::copy_if(xml.begin(), xml.end(), std::back_inserter(s),
143 [&isSkipping] (char ch) {
144 if (ch == '\n') {
145 isSkipping = true;
146 }
147 else if (ch != ' ') {
148 isSkipping = false;
149 }
150 return !isSkipping;
151 });
152 return s;
153}
154
155} // namespace tests
156} // namespace nfdc
157} // namespace tools
158} // namespace nfd
159
160#endif // NFD_TESTS_TOOLS_NFDC_STATUS_FIXTURE_HPP