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